// just reset the normals to an initial value of 0
for every vertex v in mesh {
v.normal = 0,0,0
}
// calculate each face normal and add it to the 3 edge-vertices
for every face f in mesh {
vertex& a = mesh.vertex_array[f.a] // refer to the 3 vertices of this face
vertex& b = mesh.vertex_array[f.b]
vertex& c = mesh.vertex_array[f.c]
vector3 normal = calc_triangle_normal(a,b,c)
a.normal += normal
b.normal += normal
c.normal += normal
}
// normalize the vertices to average
for every vertex v in mesh {
v.normal.normalize()
}
By the way, as we don't normalize the normals on the summing part, bigger faces turn normals on their vertices more towards them.. this is accurate if you do it on paper (in 2d as best)..
there are suggestions to divide them by the squared length before summing to smoothen more towards the round parts.. while looking nice, too, and being based on some good idea actually, it is just slower.. thats why i never use anything else but that..
be warned that, if you have different vertices with same position, you get a hard edge there..


This topic is locked









