While attempting to render a 3D object using OpenGL (and the GLFW library), the model experiences lots of flickering. Im reading the .obj
file using a library that I've written on my own.
Written below is my render function:
Unfortunately, in order to understand how faces and vertices are being inputted, I will have to provide all my code, which is linked:
Zipped code along with executable and sample .obj
:
Source
Im using .obj files from here to test the program. Right now, the program doesn't support normals and textures, which isnt an issue since most of the models on the site dont have them. Also, (right now) it only reads from "123.obj" so the file should'nt be named anything else. And it only accepts a single space, not more than that.
float render()
{
glfwSetTime(0.0f);
int win_width;
int win_height;
glfwGetWindowSize(&win_width, &win_height);
float win_aspect = (float)win_width / (float)win_height;
glViewport(0, 0, win_width, win_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90, win_aspect, 0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 50.0, 0, 0, 0, 0.0, 1.0, 0.0);
glEnable(GL_DEPTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_NORMALIZE);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glRotatef(angle , 0 , 1, 0);
glColor3f(0.0f, 0.0f, 0.0f);
int index = 0;
for(int a = 0; a < faces.size(); a++)
{
if(faces[a].vertices.size() == 3)
{
glBegin(GL_TRIANGLES);
}
else
{
glBegin(GL_QUADS);
}
for(int b = 0; b < faces[a].vertices.size(); b++)
{
index = faces[a].vertices[b];
glVertex3f(vertices[index].Dimensions[_x], vertices[index].Dimensions[_y], vertices[index].Dimensions[_z]);
}
glEnd();
}
glfwSwapBuffers();
return (float)glfwGetTime();