PDA

View Full Version : calculating normal vectors



chuks
08-05-2003, 09:09 AM
Hi,
part of my project involves using opengl. I will like to know how to calculate normal vectors (for glNormal3f) for a large set of scanned object. assumming the data is something like this.....

glBegin(GL_TRIANGLES);
glNormal3f ();
glVertex3f(7.4350,-2.47203, -45.1869);
glVertex3f(71.6133, 6.12588, -53.2960);
glVertex3f( 50.12, 10.45, 23.00);
glNormal3f ();
glVertex3f(74.4350, -2.47203, -45.1869);
glVertex3f( 77.6763, -2.29052,-38.1057);
glVertex3f( 82.0971, -7.86762, -36.3902);
glNormal3f ();
glVertex3f( 74.4350, -2.47203, -45.1869);
glVertex3f( 71.6133, 6.12588, -53.2960);
glVertex3f( 67.6907, -1.83262, -61.1861);
glNormal3f ();
glVertex3f( 74.4350, -2.47203, -45.1869);
........etc
any contribution/reference will be appreciated.
chuks

phoenix_wrath
08-05-2003, 10:36 AM
My best guess (If I got you correctly) would to enable normalization of the Normals using glEnable(GL_NORMALIZE)
and then either of the ways :

Fast one : Since you are drawing triangles, use a display list, and make a small average calculus of every 3 vertices. this is not incorrect but it is fast and close.

Slow one : calculate the plane equasion of the three vertices and find it's normal. then set it as the normal.

both should do the trick. the only thing remaining is the question of time .....

Thank you .

Ostsol
08-05-2003, 11:00 AM
Take the cross product of (coord1 - coord3) and (coord2 - coord3). The cross product is calculated as:

out.x = (v1.y * v2.z) - (v1.z * v2.y)
out.y = (v1.z * v2.x) - (v1.x * v2.z)
out.z = (v1.x * v2.y) - (v1.y * v2.x)

Then normalize the result.

mmshls
08-05-2003, 11:04 AM
Since you have all the actual data, you can precompute the normals for all the triangles and fill in the calls to glNormal.
http://prodigy.openglforums.com/reference/tutorials/normals/normals.php

It's not an advanced topic.

chuks
08-06-2003, 06:55 AM
Hi,
Thanks a lot guys.Is working so nice now.