Little problem when calculating vertex normals (OpenGL ES)

I have a little problem with a normal top.

The result seems a little strange. First look at the images:

Original model in 3D software.

http://img233.imageshack.us/img233/4421/screenshot20110412at707.png

Perforated lighting with a grid imported directly from an OBJ file (using normals from a file).

http://img97.imageshack.us/img97/2960/screenshot20110412at708.png

Perfraction lighting, calculating normals using my own program.

http://img508.imageshack.us/img508/2960/screenshot20110412at708.png

You noticed? Using normals from an OBJ file is all right. But when I use my own procedure to calculate vertex-based normals, something seems wrong.

The normals seem strange when Z becomes negative (using the orientation of the right hand). In other parts of the object, everything is in order, but only one imaginary line on the grid seems strange. This problem persists on other grids, always in the same place, Z = 0.

My normal calculation routine is what everyone knows, assuming a triangle with ABC vertices:

vec3 normal = normalize(cross(C - A, B - A));

And then adding the normal result to the already calculated normal buffer.

I saw some guys talking about calculating the area of ​​each triangle and multiplying it by normal or even checking on a dot product between normal in the buffer and the new normal, but I already tried these approaches and I caught only small changes, but still with the same a problem.

Have you seen this before? Do you know how to solve this?

Here is the code:

 // Looping at each triangle vec3 distBA = vec3Subtract(vB, vA); vec3 distCA = vec3Subtract(vC, vA); vec3 normal = vec3Cross(distBA, distCA); // Each normalBuffer represents one vertex. normalBuffer[i1] = vec3Add(normal, normalBuffer[i1]); normalBuffer[i2] = vec3Add(normal, normalBuffer[i2]); normalBuffer[i3] = vec3Add(normal, normalBuffer[i3]); // After pass through all faces/triangles. // Looping at each Vertex structure. normal = vec3Normalize(normalBuffer[i]); 

Thanks in advance.

+4
source share
2 answers

Depending on how you calculate the lighting per fragment (i.e. if you pinch your point product in the range 0-1 or not), what you see may just be a problem with inverted normals. Depending on how you choose A , B , C , the normal at the vertex can point in any direction. To separate the exterior and interior of a triangle, you usually need additional information, for example. winding peaks. Do you take this into account?

+1
source

You normalize the result "And then add the normal result to the already calculated normal buffer." I'm not sure what you need, but that will not hurt to add it. And while you're on it, check NaN and infinity.

Do you check your normalized function if the length of the input vector is greater than 0?

I have a suspicion that you have an unpleasant error somewhere in your vector library, especially since the problem only occurs for the Z axis.

0
source

Source: https://habr.com/ru/post/1347824/


All Articles