0
votes

I am having an issue with displaying all of my objects with openGL. I have 5 different obj files:cube, teapot, lamp, a man, and a car. The cube, lamp and man draw just fine however the teapot and car do not render correctly.

I have a method that reads in the obj file, and i have made printouts to double check if the output matches the obj file and they are all correct.

Here is my code for the draw() and i think that the issue is in here

glBegin(GL_TRIANGLES);
for(int i = 0; i < f_count; i++){
    if(f[i].type == 'c'){
        GLfloat * color = new GLfloat[4];
        color[0] = f[i].x;
        color[1] = f[i].y;
        color[2] = f[i].z;
        color[3] = 1;

        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
    }
    else{
        glVertex3f(vn[(f[i].vn1-1)].x, vn[(f[i].vn1-1)].y, vn[(f[i].vn1-1)].z); // normals
        glVertex3f(vn[(f[i].vn2-1)].x, vn[(f[i].vn2-1)].y, vn[(f[i].vn2-1)].z); // normals
        glVertex3f(vn[(f[i].vn3-1)].x, vn[(f[i].vn3-1)].y, vn[(f[i].vn3-1)].z); // normals

        glVertex3f(v[(f[i].v1-1)].x, v[(f[i].v1-1)].y, v[(f[i].v1-1)].z);
        glVertex3f(v[(f[i].v2-1)].x, v[(f[i].v2-1)].y, v[(f[i].v2-1)].z);
        glVertex3f(v[(f[i].v3-1)].x, v[(f[i].v3-1)].y, v[(f[i].v3-1)].z);   
    }
}
glEnd();

the first 3 are the normals and the other 3 are the vertices Here is my header file with my structs

 struct NormalVector
 {
     GLfloat x, y, z;
 };

 struct Vertex
 {
     GLfloat x, y, z, w;
 };

 struct Faces
 {
     int v1,v2,v3, vn1,vn2,vn3;
     char type;
     GLfloat x,y,z;
 };

I am not sure what I am doing wrong or if I am doing anything wrong with the code I presented.. however this is where I "feel" the error is located.

EXPLANATION:

Vertex *v is the vertex pointer VectorNormal *vn is the normal vector pointer Faces *f is the faces pointer

in the draw() method, it loops through the f struct pointer and checks the type, if its a color type, it adds the glMaterialfv(..,..,..); If it is not, then it grabs the index value of the faces and pass its through the Vertex struct pointer and Vector Normal struct pointer.

Here is my Teapot Here is my teapot Here is the teapot its suppose to look like enter image description here

1
Is there anything else that's different about the cube, lamp and man vs. the teapot and the car? Are all the objects loaded from .obj files and they're all supposed to be drawn with lighting? - svk
why was this downvoted? - DWolf
I know modern OpenGL has a very steep learning curve but immediate mode (glVertex and the like) is deprecated since OpenGL 3. - Marius

1 Answers

3
votes

You provide normals via glNormal(), not glVertex().

Also, glNormal() only latches some state. Nothing really gets sent to OpenGL until the next glVertex() call, at which point the latched color/normal/position state is bundled up and passed down the pipeline.

Only the most recent glNormal() call will have any effect because it will overwrite any other glNormal() since the last glVertex().

So you need to interleave your glNormal() calls:

glNormal3f(vn[(f[i].vn1-1)].x, vn[(f[i].vn1-1)].y, vn[(f[i].vn1-1)].z);
glVertex3f(v[(f[i].v1-1)].x, v[(f[i].v1-1)].y, v[(f[i].v1-1)].z);

glNormal3f(vn[(f[i].vn2-1)].x, vn[(f[i].vn2-1)].y, vn[(f[i].vn2-1)].z);
glVertex3f(v[(f[i].v2-1)].x, v[(f[i].v2-1)].y, v[(f[i].v2-1)].z);

glNormal3f(vn[(f[i].vn3-1)].x, vn[(f[i].vn3-1)].y, vn[(f[i].vn3-1)].z);
glVertex3f(v[(f[i].v3-1)].x, v[(f[i].v3-1)].y, v[(f[i].v3-1)].z);