I am learning openGL and I am having troubles getting a cube to render correctly. I have what I believed to be the correct data for the cube, but it is not quite a cube. Im not sure whats going on. Can anyone offer some advice?
#include <glad/gl.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include "linmath.h"
#include <stdlib.h>
#include <stdio.h>
static const struct
{
float x, y, z;
float r, g, b;
} vertices[36] =
{
{-1.0f,-1.0f,-1.0f, 0.f, 1.f, 0.f}, // triangle 1 : begin
{-1.0f,-1.0f, 1.0f, 0.f, 1.f, 0.f},
{-1.0f, 1.0f, 1.0f,0.f, 1.f, 0.f}, // triangle 1 : end
{1.0f, 1.0f,-1.0f,0.f, 1.f, 0.f}, // triangle 2 : begin
{-1.0f,-1.0f,-1.0f,0.f, 1.f, 0.f},
{-1.0f, 1.0f,-1.0f,0.f, 1.f, 0.f}, // triangle 2 : end
{1.0f,-1.0f, 1.0f,0.f, 1.f, 0.f},
{-1.0f,-1.0f,-1.0f,0.f, 1.f, 0.f},
{1.0f,-1.0f,-1.0f,0.f, 1.f, 0.f},
{1.0f, 1.0f,-1.0f,0.f, 1.f, 0.f},
{1.0f,-1.0f,-1.0f,0.f, 1.f, 0.f},
{-1.0f,-1.0f,-1.0f,0.f, 1.f, 0.f},
{-1.0f,-1.0f,-1.0f,0.f, 1.f, 0.f},
{-1.0f, 1.0f, 1.0f,0.f, 1.f, 0.f},
{-1.0f, 1.0f,-1.0f,0.f, 1.f, 0.f},
{1.0f,-1.0f, 1.0f,0.f, 1.f, 0.f},
{-1.0f,-1.0f, 1.0f,0.f, 1.f, 0.f},
{-1.0f,-1.0f,-1.0f,0.f, 1.f, 0.f},
{-1.0f, 1.0f, 1.0f,0.f, 1.f, 0.f},
{-1.0f,-1.0f, 1.0f,0.f, 1.f, 0.f},
{1.0f,-1.0f, 1.0f,0.f, 1.f, 0.f},
{1.0f, 1.0f, 1.0f,0.f, 1.f, 0.f},
{1.0f,-1.0f,-1.0f,0.f, 1.f, 0.f},
{1.0f, 1.0f,-1.0f,0.f, 1.f, 0.f},
{1.0f,-1.0f,-1.0f,0.f, 1.f, 0.f},
{1.0f, 1.0f, 1.0f,0.f, 1.f, 0.f},
{1.0f,-1.0f, 1.0f,0.f, 1.f, 0.f},
{1.0f, 1.0f, 1.0f,0.f, 1.f, 0.f},
{1.0f, 1.0f,-1.0f,0.f, 1.f, 0.f},
{-1.0f, 1.0f,-1.0f,0.f, 1.f, 0.f},
{1.0f, 1.0f, 1.0f,0.f, 1.f, 0.f},
{-1.0f, 1.0f,-1.0f,0.f, 1.f, 0.f},
{-1.0f, 1.0f, 1.0f,0.f, 1.f, 0.f},
{1.0f, 1.0f, 1.0f,0.f, 1.f, 0.f},
{-1.0f, 1.0f, 1.0f,0.f, 1.f, 0.f},
{1.0f,-1.0f, 1.0f,0.f, 1.f, 0.f},
};
static const char* vertex_shader_text =
"#version 110\n"
"uniform mat4 MVP;\n"
"attribute vec3 vCol;\n"
"attribute vec3 vPos;\n"
"varying vec3 color;\n"
"void main()\n"
"{\n"
" gl_Position = MVP * vec4(vPos, 1.0);\n"
" color = vCol;\n"
"}\n";
static const char* fragment_shader_text =
"#version 110\n"
"varying vec3 color;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(color, 1.0);\n"
"}\n";
static void error_callback(int error, const char* description)
{
fprintf(stderr, "Error: %s\n", description);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
int main(void)
{
GLFWwindow* window;
GLuint vertex_buffer, vertex_shader, fragment_shader, program;
GLint mvp_location, vpos_location, vcol_location;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetKeyCallback(window, key_callback);
glfwMakeContextCurrent(window);
gladLoadGL(glfwGetProcAddress);
glfwSwapInterval(1);
// NOTE: OpenGL error checks have been omitted for brevity
glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
glCompileShader(vertex_shader);
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
glCompileShader(fragment_shader);
program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
mvp_location = glGetUniformLocation(program, "MVP");
vpos_location = glGetAttribLocation(program, "vPos");
vcol_location = glGetAttribLocation(program, "vCol");
glEnableVertexAttribArray(vpos_location);
glVertexAttribPointer(vpos_location, 3, GL_FLOAT, GL_FALSE,
sizeof(vertices[0]), (void*) 0);
glEnableVertexAttribArray(vcol_location);
glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE,
sizeof(vertices[0]), (void*) (sizeof(float) * 3));
while (!glfwWindowShouldClose(window))
{
float ratio;
int width, height;
mat4x4 m, p, mvp;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float) height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
mat4x4_identity(m);
mat4x4_rotate_Z(m, m, (float) glfwGetTime());
mat4x4_rotate_Y(m, m, (float) glfwGetTime());
mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);
mat4x4_mul(mvp, p, m);
glUseProgram(program);
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
glDrawArrays(GL_POLYGON, 0, 36);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
Am I just missing a few triangles? Is there a more efficient approach to creating the cube?
The major issue is the primitive type. Since you create a cube by 12 triangles, the primitive type has to be rather than GL_POLYGON
:
glDrawArrays(GL_POLYGON, 0, 36);
glDrawArrays(GL_TRIANGLES, 0, 36);
Anyway, I recommend to construct a cube with 6 the 6 sides have 6 different colors:
struct TVetex
{
float x, y, z;
float r, g, b;
};
TVetex vertices[36];
struct TVetex2
{
float x, y;
};
struct TColor
{
float r, g, b;
};
int main(void)
{
TVetex2 v[]{ {-1, -1}, {1, -1}, {1, 1}, {-1, -1}, {1, 1}, {-1, 1} };
TColor c[]{ {1, 0, 0}, {1, 1, 0}, {0, 0, 1}, {0, 1, 0}, {1, 0, 1}, {1, 0.5, 0} };
for (int i = 0; i < 6; ++i)
{
for (int j = 0; j < 6; ++j)
{
int k = i*6 + j;
switch (i)
{
case 0: vertices[k] = { -1, v[j].x, v[j].y, c[i].r, c[i].g, c[i].b }; break;
case 1: vertices[k] = { 1, v[j].x, v[j].y, c[i].r, c[i].g, c[i].b }; break;
case 2: vertices[k] = { v[j].y, -1, v[j].x, c[i].r, c[i].g, c[i].b }; break;
case 3: vertices[k] = { v[j].y, 1, v[j].x, c[i].r, c[i].g, c[i].b }; break;
case 4: vertices[k] = { v[j].x, v[j].y, -1, c[i].r, c[i].g, c[i].b }; break;
case 5: vertices[k] = { v[j].x, v[j].y, 1, c[i].r, c[i].g, c[i].b }; break;
}
}
}
// [...]
}
Furthermore increase the viewing volume, to see the entire cube. For instance
mat4x4_ortho(p, -ratio*2.f, ratio*2.f, -2.f, 2.f, 2.f, -2.f);