When I try to render a PERFECT cube, one vertex renders in the middle of the cube - (.5, .5, -.5) instead of (1, 1, 1) - and one renders one unit too high - (1, 1, 1) instead of (1, 0, 0). I really don't have a single clue how that could happen. I first checked if it had to do with the obj loading and defined the arrays separately, it didn't. This is the output of the program. This is from another perspective for better understanding of the problem.
First I initialize glfw
, create a window, make a context and set various variables:
if (!glfwInit())
{
std::cout << "Failed to initialize GLFW, press enter to close the application..." << std::endl;
std::cin.get();
return false;
}
GLFWWindow *window= glfwCreateWindow(m_Width, m_Height, m_Title, NULL, NULL);
if (!window)
{
glfwTerminate();
std::cout << "Failed to creat GLFW window, press enter to close the application..." << std::endl;
std::cin.get();
return false;
}
glfwMakeContextCurrent(m_GLFWWindow);
Then I initialize some enable some gl options and set various other variables:
glClearColor(0, 0, 0, 0);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CW);
glEnable(GL_DEPTH_TEST);
glDepthRange(0, 1);
glEnable(GL_FRAMEBUFFER_SRGB);
After that is done I declare a Vertex array and a indices array and pass them to OpenGL: (Vertex is just a structure containing three floats)
Vertex verticies[] = { Vertex(Vector3( 1, -1, -1)),
Vertex(Vector3( 1, -1, 1)),
Vertex(Vector3( -1, -1, 1)),
Vertex(Vector3( -1, -1, -1)),
Vertex(Vector3( 1, 1, -1)),
Vertex(Vector3( 1, 1, 1)),
Vertex(Vector3( -1, 1, 1)),
Vertex(Vector3( -1, 1, -1)) };
unsigned int indicies[] = { 2, 4, 1,
8, 6, 5,
5, 2, 1,
6, 3, 2,
3, 8, 4,
1, 8, 5,
2, 3, 4,
8, 7, 6,
5, 6, 2,
6, 7, 3,
3, 7, 8,
1, 4, 8};
GLu vbo;
Glu ibo;
glGenBuffers(1, &vbi);
glGenBuffers(1, &ino);
glBindBuffer(GL_ARRAY_BUFFER, m_Vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(verticies[0]) * 8, &verticies, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicies[0]) * 36, &indicies, GL_STATIC_DRAW);
Then I create a program, compile shaders and link them to the program:
GLuint program = glCreateProgram();
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
GLchar vertexCode = "vertexShader code";
GLchar fragmentCode = "fragmenShader code";
glShaderSource(vertexShader , 1, &vertexCode, NULL);
glCompileShader(vertexShader );
glShaderSource(fragmentShader, 1, &fragmentCode, NULL);
glCompileShader(fragmentShader);
glAttachShader(program , vertexShader );
glAttachShader(program , fragmentShader);
glLinkProgram(m_Program);
glValidateProgram(m_Program);
Now we're at the game loop:
while(1)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(0);
}