user2320928
user2320928

Reputation: 309

calculate normal per vertex OpenGL

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]);
            }

Upvotes: 5

Views: 19200

Answers (1)

sbabbi
sbabbi

Reputation: 11181

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);
    }
}

Upvotes: 15

Related Questions