Jump to content
  • Advertisement
Sign in to follow this  
SilverLogic

OpenGL calculating normals for lighting...

This topic is 5217 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

Hey guys, i am learning about lighting in opengl, i think i understand the basics, but i have an assignment and i have to use the teachers base code, but i dont get how to use his ways, to find the normal. in my own demo when i was drawing polygons i just did it like this glVertex3f(x, y, z); glNormal3f(x, y, z); and it seemed to work fine, but the teachers code looks like this:
	for (int i = 0; i < g_TriangleCount; i++)
	{
		int VtxIdx0 = g_pIndices[3*i];
		int VtxIdx1 = g_pIndices[3*i + 1];
		int VtxIdx2 = g_pIndices[3*i + 2];
		
		glVertex3fv((float *)&g_pVertices[VtxIdx0]);
		glVertex3fv((float *)&g_pVertices[VtxIdx1]);
		glVertex3fv((float *)&g_pVertices[VtxIdx2]);
	}
I dont know quite how to go about using this to define the normals, i tryed a few things with it but i cant get it to look right

Share this post


Link to post
Share on other sites
Advertisement
Basically your teacher did right , he is not using glnormal3f for lighting computation, you should provide a normal for each vertex , you should do something like that

// for each triangle

glBegin(GL_TRIANGLES);

// first vertex

glNormal3f( 1,0,0);
glVertex3f(x,y,z);
.
.
// second vertex
.
.
// third vertex

glEnd();

note that glnormal3f is just an example you should provide the normals at each vertex and to do that there is a fair elegant
and fast algorithm , if you want to know details, email me beacuse the code is very long

Share this post


Link to post
Share on other sites
To find the normal for a triangle, take a couple of vectors which lie on the plane of the triangle (edges are usually easy to find) and then take the vector cross product of them ([google]). Normalise the result and you've got a triangle normal.

Then remember that vertex normals are typically the avaraged vector of all triangle normals which it lies on.

Share this post


Link to post
Share on other sites
Quote:
Original post by SilverLogic
glVertex3f(x, y, z); glNormal3f(x, y, z);

Note that the above vertex will not use the normal specified above. When you call glVertex* the current normal (and color etc.) is applied to the vertex. Calls to glNormal afterwards hence won't affect the vertex.
Also, I assume that wasn't directly copied from the code. Unless you're drawing a unit sphere, you probably don't want to set the normal equal to the vertex (you set both to (x,y,z) above [grin])

Share this post


Link to post
Share on other sites
OrangyTang gave you the method. For each triangle, you can use the three vertices to get two vectors, and then take the cross product of them and so on.
What exactly is the problem - the maths or how to implement it?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by SilverLogic

for (int i = 0; i

If your teacher provided you the normals, then given this code I'd assume that there should be a g_pNormals array, and that it should be accessed like the verts.

If they were not provided, then you have to calculate them manually with the verts. Notice the code pulls a float pointer from the vertex data using an index array for each call to glVertex3fv. If that's enough data to produce a three dimensional vertex, then it's exactly what you need to start calculating surface normals as well.

Share this post


Link to post
Share on other sites
Quote:
Original post by SilverLogic
this is all fine and dandy but i still dont know how to pull the normal from the teachers code o_O

I'm not gonna do all your work for you [razz]

Heres some hints:
- It looks the indicies are in runs of 3 that define each triangle.
- From that you can calculate a per-face normal without too much difficulty. I'd store these temp normals in another array that runs parallel with the indicies.
- From that you can run though the indices and find out which face normals to avarage to build your vertex normals. Bung them in another concurrent array.
- Convert the rendering to use your new per-vertex normals.

Share this post


Link to post
Share on other sites
Quote:
Original post by SilverLogic

for (int i = 0; i < g_TriangleCount; i++)
{
int VtxIdx0 = g_pIndices[3*i];
int VtxIdx1 = g_pIndices[3*i + 1];
int VtxIdx2 = g_pIndices[3*i + 2];

glVertex3fv((float *)&g_pVertices[VtxIdx0]);
glVertex3fv((float *)&g_pVertices[VtxIdx1]);
glVertex3fv((float *)&g_pVertices[VtxIdx2]);
}



Arg! Forums and cookies and anonymous posts... ugh. As I was saying anonymously...

Quote:

If your teacher provided you the normals, then given this code I'd assume that there should be a g_pNormals array, and that it should be accessed like the verts.

If they were not provided, then you have to calculate them manually with the verts. Notice the code pulls a float pointer from the vertex data using an index array for each call to glVertex3fv. If that's enough data to produce a three dimensional vertex, then it's exactly what you need to start calculating surface normals as well.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!