Generating Normals

Started by
6 comments, last by eSCHEn 19 years, 2 months ago
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.
- relpats_eht
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.
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
- relpats_eht
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);
thanx for the help, i now have normals working perfectly using
glNormal3f(((vy[l-1]-vy[l])*(vz[l-1]-vz[l-1][s+1&gt;=SLICES ? 0 : s+1]))-((vz[l-1]-vz[l])*(vy[l-1]-vy[l-1][s+1&gt;=SLICES ? 0 : s+1])), ((vz[l-1]-vz[l])*(vx[l-1]-vx[l-1][s+1&gt;=SLICES ? 0 : s+1]))-((vx[l-1]-vx[l])*(vz[l-1]-vz[l-1][s+1&gt;=SLICES ? 0 : s+1])), ((vx[l-1]-vx[l])*(vy[l-1]-vy[l-1][s+1&gt;=SLICES ? 0 : s+1]))-((vy[l-1]-vy[l])*(vx[l-1]-vx[l-1][s+1&gt;=SLICES ? 0 : s+1]))); <br><br>just so i know, how does the magnitude part affect the outcome?<br><br><!–EDIT–><span class=editedby><!–/EDIT–>[Edited by - relpats_eht on February 15, 2005 5:37:10 PM]<!–EDIT–></span><!–/EDIT–>
- relpats_eht
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.
ok, just one last question, say i wanted these normals to point inward instead of outward, how would I do that?
- relpats_eht
Just reverse the order you call the vertices in, i.e. from clockwise to counter-clockwise or vice versa.

--
Cheers,
Darren Clark

This topic is closed to new replies.

Advertisement