# Little problem while calculating vertex normals (OpenGL ES)

This topic is 2750 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hy guys,

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.

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

Per-Fragment lighting, calculating the normals with my own routine.

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 there 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.
I also tried normalize the normals once, just after sum all normals of a vertex, but nothing seems interfere in the bug when Z = 0.

Did you seen this before? Do you know how to solve that?
Am I missing an additional step in the normal calculation to deal with -Z?

##### Share on other sites
It may have nothing to do with the z-coordinate in particular. It looks like you're using an indexed mesh but you're only calculating one normal per vertex. Have you tried averaging the normals for all triangles adjacent to each vertex?

See this thread for a method to do that.

##### Share on other sites

It may have nothing to do with the z-coordinate in particular. It looks like you're using an indexed mesh but you're only calculating one normal per vertex. Have you tried averaging the normals for all triangles adjacent to each vertex?

See this thread for a method to do that.

Hi Buck,

The image in that post is really confused... orthographic is really confused... heheh
Anyway, I took the point in that article.

But yeh, bro, I'm already calculating the normals for adjacent triangles. As I said, I'm dealing with it using a normal buffer.

Look, if I disable the calculations for the adjacents this is the result:

----------- REMOVED TO AVOID CONFUSION -----------

As you see, some normals are completely lost, but again... my problem on the Z = 0 persists....

As I said before, I already tried calculate the areas for all triangles (even adjacents) to make adjusts, calculate the dot product between the edges to use as comparison to avoid problems with smooth edges... but everything give me the same problem.

This is so weird...

Any other idea?

##### Share on other sites
Have you double-checked that you're accessing the vertices by index correctly? The black triangles may indicate an incorrect winding order resulting in a normal that's 180[sup]o[/sup] out.

Perhaps you should post your code for calculating the normals.

As I said, I'm dealing with it using a normal buffer.[/quote]
Not sure why that would necessarily have anything to do with the problem. Can you explain?

##### Share on other sites

Have you double-checked that you're accessing the vertices by index correctly? The black triangles may indicate an incorrect winding order resulting in a normal that's 180[sup]o[/sup] out.

Perhaps you should post your code for calculating the normals.

As I said, I'm dealing with it using a normal buffer.

Not sure why that would necessarily have anything to do with the problem. Can you explain?
[/quote]

hehehe

No, no, no... man... Forget the image with the black area!! I just showed you that picture to you see what happens IF (and only IF) I didn't calculate the adjacent triangles...

My final result is pretty good, no black areas... my only problem is with that strange line at Z = 0. Just it....

##### Share on other sites
And 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); 

##### Share on other sites
You may be missing something significant. If you're not calculating any normals for the image with the black area, how is it lit? If you are calculating a normal, why isn't it lit correctly (or, at least, "pretty good")? It may be indicative of the problem.

In any case, check your indexing and post some code. Just talking about the code doesn't provide enough information.

##### Share on other sites

You may be missing something significant. If you're not calculating any normals for the image with the black area, how is it lit? If you are calculating a normal, why isn't it lit correctly (or, at least, "pretty good")? It may be indicative of the problem.

In any case, check your indexing and post some code. Just talking about the code doesn't provide enough information.

Man, what are u talking about?
Are u understanding what I'm saying?
Did you understand when I said that the black areas does not exist!!!???
That image (with black bottom) was just to you SEE what happens IF I HAD NOT calculating the adjacent area!!!

The code is above, just look....

##### Share on other sites
Did you understand when I said that the black areas does not exist!!!???[/quote]
You didn't say the mesh was incomplete, or that you were drawing only a portion of the object. I assumed that the mesh was complete - i.e., there were triangles on the bottom of the teapot. I didn't realize the bottom was open or not being drawn (other than the 2 triangles that were drawn).

As mentioned above, have you checked that your indexing is correct ( and reflects a correct winding order )? Have you tried generating a simpler object that exhibits the same behavior?

EDIT: Do you zero out the normal buffer before you loop through the face-normal calc? Edited by Buckeye

##### Share on other sites

Did you understand when I said that the black areas does not exist!!!???

You didn't say the mesh was incomplete, or that you were drawing only a portion of the object. I assumed that the mesh was complete - i.e., there were triangles on the bottom of the teapot. I didn't realize the bottom was open or not being drawn (other than the 2 triangles that were drawn).

As mentioned above, have you checked that your indexing is correct ( and reflects a correct winding order )? Have you tried generating a simpler object that exhibits the same behavior?

EDIT: Do you zero out the normal buffer before you loop through the face-normal calc?
[/quote]

But you can't understand what I'm saying. You are not american, right? So please, dude, try to use a translator to check again what I've said.

Look, I'll try explain again very easy to you take it at the translator:

1 - "The black area doesn't exist" doesn't meant something like "that vertices don't exist, my mesh has a hole". Just forget that picture with black at the bottom, OK? I never said the mesh was incomplete. The Mesh IS complete. OK?

2 - Yes, the indexing is correct.

3 - Yes, the normal buffer is 0 at the beginning (I mean, a vector with the three values equals to 0 - {0,0,0}).

4 - I'll explain to you again:
- I opened this topic saying "my normal has a problem".
- Then you said: "Check the adjacents triangles".
- Then I answered: "Yes, dude, I'm already calculating.... Look, IF I was not calculating (and only if), this should be the result... (image with black bottom)"

I think you got confused at that point. When I showed the image with the bottom. So to make it absolutely clear to you, I'll show other images.

This is what happens when I use the normals from OBJ file, this image is perfect, is exactly what I want when calculate normals by myself:

Now this, is what happens when I use normals calculated by my self:

It's more clearly to you now, Buck?

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
JoeJ
18

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632998
• Total Posts
3009805
• ### Who's Online (See full list)

There are no registered users currently online

×