Sign in to follow this  
relpats_eht

Generating Normals

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


Link to post
Share on other sites
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


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


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


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


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


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