Original Post
Hi! After calculating normals of a terrain, I want to do the same with my .3ds model. I've found a topic in the forum and in it somebody wrote this algorithm:
for each vertex in the mesh
find all the triangles that has the vertex
calculate its surface normal
add it to a variable
end
avarage the variable, and normalize it
end
Based on the algorithm above, I wrote this function:
void Mesh3ds::CreateNormals( void ) {
if (!VertexCount || !TriangleCount) return;
crVec3 normal, v1, v2;
Normals = new crVec3[VertexCount];
normal = crVec3( 0, 0, 0 );
int k=0;
for(unsigned long i=0; i<VertexCount; i++) {
for(unsigned long f=0; f<TriangleCount; f++) {
if (Triangles[f].a == i ) {
v1 = Vertices[Triangles.b] - Vertices[Triangles.a];
v2 = Vertices[Triangles.c] - Vertices[Triangles.a];
}
if (Triangles[f].b == i ) {
v1 = Vertices[Triangles.a] - Vertices[Triangles.b];
v2 = Vertices[Triangles.c] - Vertices[Triangles.b];
}
if (Triangles[f].c == i ) {
v1 = Vertices[Triangles.a] - Vertices[Triangles.c];
v2 = Vertices[Triangles.b] - Vertices[Triangles.c];
}
normal += normalize(cross( v1, v2 ));
k++;
}
Normals = normalize (normal/float(k));
k=0;
}
}
I tried not to divide it with k, and when adding to 'normal', not to normalize it. ( normal += cross(v1, v2); then Normals = normalize(normal); ) But it does not work. I don't have any idea about this, please help!