Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualDracoLacertae

Posted 04 September 2012 - 02:20 PM

I see a couple of problems here:

1. The glNormal3f should be put before the call to glVertex3f, otherwise all the normals will be off by 1.
2.  For each triangle you are only pushing 1 vertex.  You wrote:

// this vertex is the said triangle, verts attached
		 vpos.x = mesh.vertices[ mesh.triangleList[i].Vertex[0] ].x;
		 vpos.y = mesh.vertices[ mesh.triangleList[i].Vertex[1] ].y;  
		 vpos.z = mesh.vertices[ mesh.triangleList[i].Vertex[2] ].z;
		 // this vertex is the said triangle, normals attached
		 npos.x = mesh.normals[ mesh.triangleList[i].Normal[0] ].x;
		 npos.y = mesh.normals[ mesh.triangleList[i].Normal[1] ].y;
		 npos.z = mesh.normals[ mesh.triangleList[i].Normal[2] ].z;

		 glVertex3f(vpos.x, vpos.y, vpos.z);
		 //glNormal3f(npos.x, npos.y, npos.z);

and I think you meant:

for (j=0;j<3;j++)
{
		 // this vertex is the said triangle, verts attached
		 vpos.x = mesh.vertices[ mesh.triangleList[i].Vertex[j] ].x;
		 vpos.y = mesh.vertices[ mesh.triangleList[i].Vertex[j] ].y;
		 vpos.z = mesh.vertices[ mesh.triangleList[i].Vertex[j] ].z;
		 // this vertex is the said triangle, normals attached
		 npos.x = mesh.normals[ mesh.triangleList[i].Normal[j] ].x;
		 npos.y = mesh.normals[ mesh.triangleList[i].Normal[j] ].y;
		 npos.z = mesh.normals[ mesh.triangleList[i].Normal[j] ].z;
		 glNormal3f(npos.x, npos.y, npos.z);
		 glVertex3f(vpos.x, vpos.y, vpos.z);
}

What you did was take the x coord of the 1st vertex, and y coord of the 2nd vertex and the z coord of the 3rd vertex!

Also, BTW, as soon as you get this rendering correctly, the 1st thing you should do is render with a VBO, or, at a minimum, a vertex array.
The format you have already is kind of similar to a vertex array, you just need to rearrange the data a little bit.  

edit:

What I do when I load on obj file:   In many cases, obj files use the same vertex/normal/texcoord index: i.e.   1/1/1  4/4/4 10/10/10, etc.  Not always!  Sometimes some models will re-use texcoord indices, resulting in off vertices like 10/5/10, 11/6/11, etc.  OpenGL Vertex Arrays / VBO doesn't allow mis-matched vertices like this.  So when I load an OBJ file, I make a map from 'obj file' vertex spec to opengl vertex index, copying when necessary.  So, 1/1/1 and 1/1/2 are 2 completely different vertices in GL.

#1DracoLacertae

Posted 04 September 2012 - 02:10 PM

I see a couple of problems here:

1. The glNormal3f should be put before the call to glVertex3f, otherwise all the normals will be off by 1.
2.  For each triangle you are only pushing 1 vertex.  You wrote:

// this vertex is the said triangle, verts attached
		 vpos.x = mesh.vertices[ mesh.triangleList[i].Vertex[0] ].x;
		 vpos.y = mesh.vertices[ mesh.triangleList[i].Vertex[1] ].y;   
		 vpos.z = mesh.vertices[ mesh.triangleList[i].Vertex[2] ].z;
		 // this vertex is the said triangle, normals attached
		 npos.x = mesh.normals[ mesh.triangleList[i].Normal[0] ].x;
		 npos.y = mesh.normals[ mesh.triangleList[i].Normal[1] ].y;
		 npos.z = mesh.normals[ mesh.triangleList[i].Normal[2] ].z;
 
		 glVertex3f(vpos.x, vpos.y, vpos.z);
		 //glNormal3f(npos.x, npos.y, npos.z);

and I think you meant:

for (j=0;j<3;j++)
{
		 // this vertex is the said triangle, verts attached
		 vpos.x = mesh.vertices[ mesh.triangleList[i].Vertex[j] ].x;
		 vpos.y = mesh.vertices[ mesh.triangleList[i].Vertex[j] ].y;
		 vpos.z = mesh.vertices[ mesh.triangleList[i].Vertex[j] ].z;
		 // this vertex is the said triangle, normals attached
		 npos.x = mesh.normals[ mesh.triangleList[i].Normal[j] ].x;
		 npos.y = mesh.normals[ mesh.triangleList[i].Normal[j] ].y;
		 npos.z = mesh.normals[ mesh.triangleList[i].Normal[j] ].z;
		 glNormal3f(npos.x, npos.y, npos.z); 
		 glVertex3f(vpos.x, vpos.y, vpos.z);
}

What you did was take the x coord of the 1st vertex, and y coord of the 2nd vertex and the z coord of the 3rd vertex!

Also, BTW, as soon as you get this rendering correctly, the 1st thing you should do is render with a VBO, or, at a minimum, a vertex array.
The format you have already is kind of similar to a vertex array, you just need to rearrange the data a little bit.

PARTNERS