简体   繁体   中英

Drawing OBJ file with openGL

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 the teapot its suppose to look like 在此处输入图片说明

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM