5

I am trying the calculate normal per vertex. But I do something wrong. When I run the code I see this:

enter image description here

Here Is my code, Note that vertex1 is vertex before the current vertex and vertex2 is vertex after the current vertex.

for (int j = 0; j < meshes[t].face[i].numOfPoints; j++)
            {
                if (normalSetChange)
                {
                    vector3D vertex1, vertex2;

                    if ((j < meshes[t].face[i].numOfPoints - 1) && (j > 0))
                    {
                        vertex1 = vertexes[meshes[t].face[i].vertex[j + 1]] - vertexes[meshes[t].face[i].vertex[j]];
                        vertex2 = vertexes[meshes[t].face[i].vertex[j - 1]] - vertexes[meshes[t].face[i].vertex[j]];
                    }
                    else if (j < meshes[t].face[i].numOfPoints - 1)
                    {
                        vertex1 = vertexes[meshes[t].face[i].vertex[j + 1]] - vertexes[meshes[t].face[i].vertex[j]];
                        vertex2 = vertexes[meshes[t].face[i].vertex[meshes[t].face[i].numOfPoints - 1]] - vertexes[meshes[t].face[i].vertex[j]];
                    }
                    else if (j > 0)
                    {
                        vertex1 = vertexes[meshes[t].face[i].vertex[0]] - vertexes[meshes[t].face[i].vertex[j]];
                        vertex2 = vertexes[meshes[t].face[i].vertex[j - 1]] - vertexes[meshes[t].face[i].vertex[j]];
                    }

                    normalSet = vector3D(vertex1.y * vertex2.z - vertex1.z * vertex2.y,
                                             vertex1.z * vertex2.x - vertex1.x * vertex2.z, 
                                             vertex1.x * vertex2.y - vertex1.y * vertex2.x);

                    normalLength = sqrt(normalSet.x * normalSet.x + normalSet.y * normalSet.y + normalSet.z * normalSet.z);

                    normalSet.x /= normalLength;
                    normalSet.y /= normalLength;
                    normalSet.z /= normalLength;

                    writePolygonLineVCN(PolygonLineVCN(vertexes[meshes[t].face[i].vertex[j]], vertexestexCoordinate[meshes[t].face[i].texCoordinate[j]], normalSet), newFile[workOnCPU]);
                }
                else
                    writePolygonLineVCN(PolygonLineVCN(vertexes[meshes[t].face[i].vertex[j]], vertexestexCoordinate[meshes[t].face[i].texCoordinate[j]], vertexesNormals[meshes[t].face[i].normal[j]]), newFile[workOnCPU]);
            }
user2320928
  • 279
  • 1
  • 7
  • 17
  • You are computing normals per triangle, not per vertex. You should do a weighted average (using the angle or the area of the triangle) of the normals of every triangle adjacent to a vertex, and use that. – sbabbi Aug 29 '13 at 19:57
  • In models, sometimes there are normal for each vertex and that is what I am looking for. – user2320928 Aug 29 '13 at 19:59

1 Answers1

15

You are computing normals per triangle, not per vertex. In fact you can clearly see "solid" normals in the image you posted.

In order to compute "smooth" normals, you need to assign to each vertex a normal which is an average of the normals of the triangles adjacent to that vertex.

Here's some pseudocode, which computed the weighted average of the normals based on the angle between the two edges adjacent to the vertex. (Maybe someone uses the area of the triangle as weight, I don't know if there is an universally accepted way to do it).

vector3D triangleNormalFromVertex(int face_id, int vertex_id) {
   //This assumes that A->B->C is a counter-clockwise ordering
   vector3D A = mesh.face[face_id].vertex[vertex_id];
   vector3D B = mesh.face[face_id].vertex[(vertex_id+1)%3];
   vector3D C = mesh.face[face_id].vertex[(vertex_id+2)%3];


   vector3D N = cross(B-A,C-A);
   float sin_alpha = length(N) / (length(B-A) * length(C-A) );
   return normalize(N) * asin(sin_alpha);
}

void computeNormals() {
    for (vertex v in mesh) {
        vector3D N (0,0,0);
        for (int i = 0;i < NumOfTriangles;++i) {
            if (mesh.face[i].contains(v) ) {
                int VertexID = index_of_v_in_triangle(i,v); //Can be 0,1 or 2
                N = N + triangleNormalFromVertex(i,VertexID);
            }
        }
        N = normalize(N);
        add_N_to_normals_for_vertex_v(N,v);
    }
}
num3ric
  • 686
  • 5
  • 15
sbabbi
  • 11,070
  • 2
  • 29
  • 57
  • OK, But what if my face wasnt a triangle? I need to find my triangle from the polygon? – user2320928 Aug 30 '13 at 11:17
  • 1
    @user2320928 It's better to always work with triangles in modern OpenGL. So you should triangulate your faces before you compute the normals. – sbabbi Aug 30 '13 at 15:33