Sign in to follow this  
amadeux

Little problem while calculating vertex normals (OpenGL ES)

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:

[b]Original model in 3D software.[/b]


[img]http://s4.postimage.org/4fs3go91f/screenshot20110412at707.png[/img]



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


[img]http://s4.postimage.org/7ze2u0o8z/screenshot20110412at708.png[/img]



[b]Per-Fragment lighting, calculating the normals with my own routine.[/b]


[img]http://s4.postimage.org/bj027d3gj/screenshot20110412at709.png[/img]



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:

[code]vec3 normal = normalize(cross(C - A, B - A));[/code]

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?

Thank you in advance.

Share this post


Link to post
Share on other sites
It [i]may[/i] 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 [url="http://www.gamedev.net/topic/599444-dx9-help-with-terrain-generation/"]this thread[/url] for a method to do that.

Share this post


Link to post
Share on other sites
[quote name='Buckeye' timestamp='1302667899' post='4797775']
It [i]may[/i] 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 [url="http://www.gamedev.net/topic/599444-dx9-help-with-terrain-generation/"]this thread[/url] for a method to do that.
[/quote]


Hi Buck,

Thanks for answer.
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 this post


Link to post
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.

[quote]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 this post


Link to post
Share on other sites
[quote name='Buckeye' timestamp='1302674867' post='4797795']
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.

[quote]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?
[/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 this post


Link to post
Share on other sites
And here is some code:

[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]);
[/code]

Share this post


Link to post
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 [i]are[/i] 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 this post


Link to post
Share on other sites
[quote name='Buckeye' timestamp='1302676439' post='4797800']
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 [i]are[/i] 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.


[/quote]

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 this post


Link to post
Share on other sites
[quote]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 this post


Link to post
Share on other sites
[quote name='Buckeye' timestamp='1302678054' post='4797804']
[quote]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?
[/quote]


Man, look, I thank you by you intrest, by your answers.

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.

[b]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:[/b]

[img]http://s4.postimage.org/mb08w3b45/Screen_shot_2011_04_13_at_10_07_49_AM.jpg[/img]



[b]Now this, is what happens when I use normals calculated by my self:[/b]

[img]http://s4.postimage.org/4kyp9nz0v/Screen_shot_2011_04_13_at_10_07_49_AM1.jpg[/img]




It's more clearly to you now, Buck?

Share this post


Link to post
Share on other sites
I'm American, amadeux, and was probably speaking English well before you were born. Your original post consisted primarily of "Here's some images. I think something's happening with my normals around Z=0. What's wrong?" You don't mention what API you're using and, with so little else to go on, I asked questions to try to get more information, which you seem hesitant to provide, looking for [i]something[/i] of a technical nature. You assume everyone will know what you're doing and how you're doing it when you say you're "calculating normals for adjacent triangles," "disabling the calcs for the adjacents" and "using a normal buffer," seemingly assuming people are psychic. You post some more pictures showing the same behavior as the previous images and.. finally post all of 7 lines of code extracted from some routine, saying only (after being asked several times): "The indexing is correct."

The likelihood of someone looking at some images and a few lines of code, and pinpointing the problem is slim-to-none. Without any further information, I can only make general suggestions.

1. [s]Ensure there are no duplicate vertices along the Z=0 line[/s]. Oops. Sorry. You said the indexing was correct.

2. Check the data is being loaded correctly.

3. Check that the math functions such as vec3Add, vec3Subtract, vec3Cross, etc., are correct and you're using them correctly.

4. Check the logic and code elsewhere (other than the 7 lines of code you posted).

5. Check that all your variables are initialized.

Share this post


Link to post
Share on other sites
[quote name='Buckeye' timestamp='1302718507' post='4798053']
I'm American, amadeux, and was probably speaking English well before you were born. Your original post consisted primarily of "Here's some images. I think something's happening with my normals around Z=0. What's wrong?" You don't mention what API you're using and, with so little else to go on, I asked questions to try to get more information, which you seem hesitant to provide, looking for [i]something[/i] of a technical nature. You assume everyone will know what you're doing and how you're doing it when you say you're "calculating normals for adjacent triangles," "disabling the calcs for the adjacents" and "using a normal buffer," seemingly assuming people are psychic. You post some more pictures showing the same behavior as the previous images and.. finally post all of 7 lines of code extracted from some routine, saying only (after being asked several times): "The indexing is correct."

The likelihood of someone looking at some images and a few lines of code, and pinpointing the problem is slim-to-none. Without any further information, I can only make general suggestions.

1. [s]Ensure there are no duplicate vertices along the Z=0 line[/s]. Oops. Sorry. You said the indexing was correct.

2. Check the data is being loaded correctly.

3. Check that the math functions such as vec3Add, vec3Subtract, vec3Cross, etc., are correct and you're using them correctly.

4. Check the logic and code elsewhere (other than the 7 lines of code you posted).

5. Check that all your variables are initialized.
[/quote]


OK, man... you exagerrated without knowing me. "speaking English well before you were born"... uahuaha... I'm sure not... how many years you think I have? 26 or 30?... no... more than it... unfortunately... but anyway... let's focus on the question here.

Maybe I should make another question, more directly. Has any problem related to Z = 0 on the normals calculation?
If Yes, great.
If not, great too.

Review my own code is something a little obvious, don't you think?
"Oh, yeh, just now, few days later, after asking a lot, posting images, register in many foruns... I'll try to review my own code!!!"
What do you think I'm doing all these days???

OK, so if there is no exception to Z = 0, great, I'll follow by myself.

About everything else you said... bullshit!!!!

Thanks again.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this