2
\$\begingroup\$

I've had this little problem for a while now, where for some reason my calculations for per-vertex normals results in a weird distortion or warping, skewing my lighting, shading, and reflection passes.

In spherical geometry, it noticeably distorts at the beginning and end, resulting in an imperfect transition in a cubemap reflection: Sphere side view Sphere top down view

In rectangular geometry, normals are quite warped and skewed on an angle: Cube top down view

Same perspective (top down), but switched over to the normal map: Cube normal map top down view Cube normal map perspective


My approach for calculating per-vertex normals is to cycle through all the vertices in a mesh, while also looping through each triangle in the mesh (inner loop). I check if the desired vertex exists in the triangle, and if so I sum that triangle's normal into the normal for that vertex. After all the normal's have been summed together for a given vertex, I normalize it.

My implementation of the above is as follows:

QVector<GLushort> *i = getIndices();
QVector<QVector3D> *n = getNormals();
int total = i->size();
n->resize(total);

if ( total > 0 ) 
{
    // Loop through each vertex in mesh
    for (int x = 0; x < total; x++) 
    { 
        QVector3D normal = QVector3D(0,0,0);

        // Loop through each triangle in mesh
        for (int y = 0; y < total; y+=3)
            if (doesTriangleContainVertex( i->at(y), i->at(y+1), i->at(y+2), i->at(x)) )
                normal += calculateTriangleNormal( i->at(y), i->at(y+1), i->at(y+2) );

        n->replace(x,normal.normalized());
    }
}

My calculation for a normal for a given triangle is just the cross product of (vertex2-vertex1 and vertex3-vertex1).

Does anyone have any ideas as to why this is happening? The underlying shading methods work fine, at least with per face normals, but that obviously gives a faceted look, which I'm trying to avoid.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.