Jump to content
  • Advertisement
Sign in to follow this  
tanmay_therock

OpenGL Polygon Mesh not shading properly

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys, I was trying to load a polygon mesh file using a OpenGL loop. I have the mesh specifined in a file in the following form : Set of Vertex co-ordinates; followed by Set of indices forming the triangles V1(x,y,z) V2(x,y,z) .... .. . T1(0,1,2) T2(0,1,2) .... .. . like in VRML files. I load the mesh properly, but cant shade it properly. This is what I am doing: Read in the data.... calculate face normals calculate vertex normals enable Smooth shading and call display loop. Pass the vertex-normal with each vertex THe rendering gets the vertices right which means I read the data properly, but the rendering has all black and white triangles closely spaced (like zebra) making it look real weird. actually it should be all white.. shaded with white that is. Any suggestions? Normal calculation shd be the culprit, but I just dont see the where I am going wrong... heres the normal calculation loop :
for (long int i=0;i<numberOfFaces;i++)  // Computing the face normals
	{
		struct Vector a;
		a.x=Coord[Index[0]].x - Coord[Index[1]].x;
		a.y=Coord[Index[0]].y - Coord[Index[1]].y;
		a.z=Coord[Index[0]].z - Coord[Index[1]].z;

		struct Vector b;
		b.x=Coord[Index[1]].x - Coord[Index[2]].x;
		b.y=Coord[Index[1]].y - Coord[Index[2]].y;
		b.z=Coord[Index[1]].z - Coord[Index[2]].z;

		faceNormal = Cross(a,b);
		faceNormal=Normalize(faceNormal);
         }

	for (i=0;i<numberOfVertices;i++)    // Computing the vertex normals
	{
		verNormal.x=0; verNormal.y=0; verNormal.z=0;
		int ctr=0;
		for(long int j=0;j<lnumberOfFaces;j++)
		{
		 if (Index[j][0] == i || Index[j][1] == i || Index[j][2] == i)
		  {
					verNormal.x += faceNormal[j].x;
					verNormal.y += faceNormal[j].y;
					verNormal.z += faceNormal[j].z;

					ctr++;
		   }
                }
	}
	verNormal.x /= ctr;
	verNormal.y /= ctr;
	verNormal.z /= ctr;

	verNormal=Normalize(verNormal)


all help appreciated... thank you cheers Tanmay

Share this post


Link to post
Share on other sites
Advertisement
the calculations seem to be correct. is it possible your mesh data alternates it winding order?

Share this post


Link to post
Share on other sites
How do I check that?

The model I have, I converted it to VRML and that renders properly... I put in the same co-ordinates, indices and normals I use.... and that bloody thing renders it... man this is very VERY frustrating for me and I have been on this for the last complete week... and it still is not rendering .....

some1 please help .... really irritated on this thing as its stopping the next steps for me...

how do I post the concerned file so that some1 of u might be able to render it and check.


thanx

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!