# Effect of vertex winding on normals and shading

I have implemented a Phong and Gourand shader for a triangle mesh that is being imported from an STL file. It appears to be working fairly allright but some triangles seem to be what I can only describe as upside down due to lack of better wording. This effect is visible in both shading models albeit a bit smoother in Phong. Here is an example:

On something basic like a cube the rendering is perfect. I am no expert but from what I have read, I am lead to suspect that perhaps the winding of the vertices in the mesh might be causing normals that are somewhat incorrect. Could this be possible and if so, how does one correct for that?

The effect of interpolating the normals by either division or by area weight does not really make too much of a difference. I can also confirm that the normals that I am calculating are basically the same as those that come with the STL as calculated by FreeCAD. The manually calculated normals are slightly better though because they are area weighed. They are calculated as follows:

glm::vec3 e1 = vecs[1] - vecs[0];
glm::vec3 e2 = vecs[2] - vecs[0];
faceNorms[i] = glm::cross(e1, e2);


with "vecs" being the 3 points of my vertices. I then sum up all these area-weighed normals for each face that uses a normal and finally normalize that.

PS. FreeCAD renders the mesh in a "flat" fashion and I guess thereby avoids this issue but unfortunately rendering "flat" with GLES 2.0 is a bit of a performance waste from what I have read.

• Your problem is caused by blending radically different normal vectors together. You could divide the vertices into groups based on the angles between their surface normals, or another heurestic, and then only combine these groups, instead of forcing all vertices with the same position to have the same normal. Yes, some vertex positions must be duplicate, but this is necessary to preserve the visual appearance of the artwork, the data simply does need to be stored. You will end up duplicating vertices because of texture coordinates as well. – MickLH Jan 11 '16 at 20:59
• @MickLH I luckily don't have to worry about textures. Regarding separate vertices though I guess what you are saying makes sense. Unfortunately I am very uneducated with 3D graphics and would appreciate it if you could perhaps point me towards some reading material on this specific problem as I have not come across anything for this type of thing yet. – Gerharddc Jan 11 '16 at 21:09
• Have you enabled GL_CULL_FACE ? If it is, any CW or CCW (depending your glCullFace(mode) setting) will be missing. It helps for debugging. – elenfoiro78 Jan 11 '16 at 21:24
• @Gerharddc An answer would depend on the artistic style you would like to achieve. We can discuss that in chat, if you want me to write an answer. – MickLH Jan 11 '16 at 22:19
• @MickLH Ok I've sent a chat. – Gerharddc Jan 12 '16 at 5:40

## 1 Answer

Ok so the problem was not winding. It seems that MickLH is right in that one cannot simply combine all the shared points in a mesh into one vertex. Only vertices used in triangles with similar normals can be combined otherwise one gets this strange effect.