#### Archived

This topic is now archived and is closed to further replies.

# Calculating Vertex Normals

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

## Recommended Posts

I am using Direct3d and am having trouble with vertex normals. I know how to calculate surface normals, that''s not the problem. I want to take the average of 3 surface normals so it will blend triangles together. How do you take the average of surface normals? *** Triality ***

##### Share on other sites
It''s actually very simple once you have your surface normals. You loop through all the vertices, check which surfaces that uses that vertex, adds their normals together and divides that by the number of polygons where it was used.
Something like this:
for all vertices {   for all polygons   {       for all vertices in the polygon       {           if vertex == current_vert            {              normx += polygon.normx;              normy += polygon.normy;              normz += polygon.normz;              num_verts_used ++;            }       }    }      v_normx = normx / num_verts_used;      v_normy = normy / num_verts_used;      v_normz = normz / num_verts_used;}

Greets Tobias

##### Share on other sites
You loop through all the vertices and find out which faces use them, then add all the normals from those faces together and divide by the number of faces. Here''s some code in C++, cause i can''t explain very well.
******************************************************
void object::Generate_Vertex_Normals(void)
{
int total=0;
normal average; // "normal" class
for(int curr_vert=0;curr_vert {
for(int curr_face=0;curr_face {
if(curr_face.vertex1==curr_vert)
{
average+=vertices[curr_vert];
total++;
continue;
}
if(curr_face.vertex2==curr_vert)
{
// same stuff inside
}
/* do this for as many verts are in a face, i use triangles */
}
average/=total;
verticies[curr_vert].normal=average;
average.reset();
total=0;
} // end for curr_vert
}

******************************************
THis is something along the lines of what I used. It should work witha little modificiations, i just typed it up now, so it hasn''t been tested or anything. The code assumes that you uses classes with overloaded operators for dividing normals and such. Cheers!

##### Share on other sites
A very usefull addition, is to check the surface Normals for the arc between them, and create seperate Vertices for surfaces with degrees for example 60° up, so when you have a cube, and you wan''t it to be a cube it is cube this way.
And not a cube which is lighted like a sphere (ok many games do that, to save computing power, cause the number of Vertices isn''t that high).
You can get the degree between two Vectors with the DotProduct (the Normals should be normalized).

Lars

• 10
• 34
• 29
• 9
• 15