# OpenGL making up normals

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

## Recommended Posts

assuming using opengl and only triangles .... i'm trying to figure out how to make up per vertex normals. i thought of just averageing together all the normals for all triangles touching a vertex, but then i realized that a triangle on the edge would have a messed up surface normal because the avereage of its normals would not be pulled in the right directions. i tried weighted averages but this didn't work, the same problem came up. i think i can do it by taking the normal of the face OPOSITE the vertex and using it to change the surface normal that i'm trying to turn into a vertex normal, but i'm not sure if this means my meshes will be individual polygons. does anyone know about anything to do this?

##### Share on other sites
that link said to do what i said wouldn't work, it would only work corectly if every vertex was connected to the same number of triangles (or maybe if the mesh was closed). if a vertex just floats off into the middle of nowhere, it is only conected to one triangle, won't the normal for the whole plane be wrong? the lighting calculations will be relying on the vertex on the edge of the mesh to 'pull' the other two back twoards the right direction, but it won't do this because it has not been combinded with any other triangles

##### Share on other sites
meh, i'm not sure what you mean, because I use the system I linked to above on a 512*512 terrain and there are no signs of messed up normals.

This is important because not all verts have the same number of tris attached to them, for example corner verts only touch 2 tris, top, side and bottom verts only touch 3 (or 4, i forget which) and inside verts can touch up to 6 (or 8, meh i need to draw stuff at some point.. ) tris.

Just visulise it, take your hands and angle 'em so your index and middle finger touch (dont worry about your thumbs), the top and bottom of your hands for two verts and the bottom of your little fingers forming the 3rd verts. Now, imagine lines coming out at right angles, perform a cunning bit of maths in your head and you'll notice that adding them together will result in a normal pointing in the right direction.

##### Share on other sites
Remember that If you are accumulating the face normals for each vertex you dont have to divide the accumulated vector by the number of faces attached to the vertex.

If you know that the face normals are already unit vectors you can simply accumulate them and then normalise the result to give you the vertex normal.

##### Share on other sites
Are you saying your lighting will be messed up because the normals from two surfaces facing totaly diffrent directions are being averaged?

 \C   |A  \   |   \ _____    |--  |B   |

Average(A+B)=C ? is that the problem, if so you need to implement Sharp Edge preservation, you do this by only averageing face normals that are with in "Edge Tolarence" you can do this by takeing the DotProduct of both normals

if ( DotProduct(A,B) > Cos(Tolarence) ) // where Tolarence is The radian measure of an angle, so if you choose 70 degree convert to radians, then the above situation will not happen cause those normals are 90 degrees apart.
{
Take_This_Into_Account_For_Averaging();
}

Perhapes this helped?

##### Share on other sites

ok phantom gotcha. i thought that it wouldn't work but i guess it will.

i was worried about when working with a mesh from a dodgey moddeler that comes with no normals, and has triangles that go off into space. earlier i was worried about what to do with triangles that a bsp process choped up (especialy if they needed to be smooth shaded) but in that case it is easily possible to find the normal at any point using the source triangle's normals.

1. 1
Rutin
19
2. 2
3. 3
JoeJ
15
4. 4
5. 5

• 18
• 19
• 10
• 13
• 17
• ### Forum Statistics

• Total Topics
631694
• Total Posts
3001751
×