开发者

Little problem while calculating vertex normals (OpenGL ES)

开发者 https://www.devze.com 2023-02-24 11:12 出处:网络
I\'m having a little problem with the vertex normal. The result seems a little strange. First a look at the images:

I'm having a little problem with the vertex normal.

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

Original model in 3D software.

Little problem while calculating vertex normals (OpenGL ES)

Per-Fragment lighting, with mesh imported directly from an OBJ file (using normals from file).

Little problem while calculating vertex normals (OpenGL ES)

Per-Fragment lighting, calcu开发者_开发问答lating the normals with my own routine.

Little problem while calculating vertex normals (OpenGL ES)

Did you notice? Using the normals from OBJ file everything is OK. But when I use my own routine to calculate the normals based on vertices something seems wrong.

The normals seems strange exactly when the Z becomes negative (using a Right-Hand orientation). On the other parts of the object everything is OK, but just one imaginary line across the mesh seems strange. This problem persists on others meshes, always at the same place, the Z = 0.

My routine to calculate the normals is that one, which everyone knows, assuming a triangle with vertices ABC:

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

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

I've seen some guys saying about calculate the area of each triangle and multiply it by the normal or even checking against the dot product between the normal on the buffer and the new normal, but I've already tried those approachs and I caught just little changes, but still with the same problem.

Did you seen this before? Do you know how to solve that?

Here is some 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]);


Depending on how you compute per fragment lighting (i.e. if you clamp your dot product in the 0-1 range or not), what you see might just be a problem of inverted normals. Depending on how you select A, B, C, the normal at a vertex could point in either direction. To tell apart the outside and the inside of a triangle you usually need some more info, e.g. the winding of the vertices. Are you taking this into account?


Are you normalizing the result of "And then adding the normal result to an already calculated normal buffer.". I'm not sure you have to, but it won't hurt to add it. And, while your at it, check for NaN and infinity.

Do you check in your normalize function if the length of the vector coming in is bigger than 0?

I have a suspicion that you've got a nasty bug somewhere in your vector library, especially since the problem only occurs for the Z axis.

0

精彩评论

暂无评论...
验证码 换一张
取 消