# 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.

## 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 on other sites
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 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 on other sites
well i know that, i know normals are n=abxac or n=(b-a)x(c-a), but i dont know how to get this value using the structure he used...

##### Share on other sites
Quote:
 Original post by SilverLogicglVertex3f(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 on other sites
this is all fine and dandy but i still dont know how to pull the normal from the teachers code o_O

##### 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 on other sites
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 on other sites
Quote:
 Original post by SilverLogicthis 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 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.

1. 1
2. 2
Rutin
23
3. 3
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 11
• ### Forum Statistics

• Total Topics
633653
• Total Posts
3013158
×