Jump to content
• Advertisement

Generating Normals

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Not a big question, im just a little confused on the subject. All I want to know is what the function call to glNormal3f would be to generate a normal, knowing the X, Y, and Z coordinates of the 3 corners of the triangle of corse.

Share this post

Share on other sites
Advertisement
glNormal3f won't generate normals - it assigns a known normal vector to the vertex/face you specify next or have specified before.
To calculate normals you will need to calculate the cross product for each face., e.g. there's no OpenGL API function for that.

Share this post

Share on other sites
yes, that is exactly what i ment
ive never been too good at reading formulas though, so could you please give me an example function call to glNormal3f, with lets just say the variables for the points being p1x, p1y, p1z; p2x, p2y, p2z; p3x, p3y, p3z for easy readablity?

Thank You,
relpats_eht

Share this post

Share on other sites
cut and paste from my engine

v1x=p1x - p2x;
v2x=p1x - p3x;

v1y=p1y - p2y;
v2y=p1y - p3y;

v1z=p1z - p2z;
v2z=p1z - p3z;

n[0]=(v1y*v2z)-(v1z*v2y);
n[1]=(v1z*v2x)-(v1x*v2z);
n[2]=(v1x*v2y)-(v1y*v2x);

m=magnitude(n);

n[0]=n[0]/m;
n[1]=n[1]/m;
n[2]=n[2]/m;

the magnitude thingy is pretty usefull, but can be ignored.

then just call glNormal3fv(n);

Share this post

Share on other sites
thanx for the help, i now have normals working perfectly using
glNormal3f(((vy[l-1][s]-vy[l][s])*(vz[l-1][s]-vz[l-1][s+1>=SLICES ? 0 : s+1]))-((vz[l-1][s]-vz[l][s])*(vy[l-1][s]-vy[l-1][s+1>=SLICES ? 0 : s+1])), ((vz[l-1][s]-vz[l][s])*(vx[l-1][s]-vx[l-1][s+1>=SLICES ? 0 : s+1]))-((vx[l-1][s]-vx[l][s])*(vz[l-1][s]-vz[l-1][s+1>=SLICES ? 0 : s+1])), ((vx[l-1][s]-vx[l][s])*(vy[l-1][s]-vy[l-1][s+1>=SLICES ? 0 : s+1]))-((vy[l-1][s]-vy[l][s])*(vx[l-1][s]-vx[l-1][s+1>=SLICES ? 0 : s+1])));

just so i know, how does the magnitude part affect the outcome?

[Edited by - relpats_eht on February 15, 2005 5:37:10 PM]

Share this post

Share on other sites
Quote:
 Original post by relpats_eht[lots of psycho code]just so i know, how does the magnitude part affect the outcome?

Normals have to be normalized to work correctly (i.e. length (or magnitude if you like) == 1.0)

So all components of the normal vector is diveded by the magnitude:
n[0]=n[0]/m;
n[1]=n[1]/m;
n[2]=n[2]/m;

You could use glEnable(GL_NORMALIZE); instead, so OpenGL normalizes the normals on the fly, but that wastes precious cpu time....

Edit: BTW you don't want to do that inside the glNormal* call, you want to calculate the normals as a preprocessing step.

Share this post

Share on other sites
ok, just one last question, say i wanted these normals to point inward instead of outward, how would I do that?

Share this post

Share on other sites
Just reverse the order you call the vertices in, i.e. from clockwise to counter-clockwise or vice versa.

Share this post

Share on other sites

• Advertisement
• Advertisement

• Popular Contributors

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5
• Advertisement

• 14
• 12
• 9
• 12
• 37
• Forum Statistics

• Total Topics
631424
• Total Posts
3000008
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!