#### Archived

This topic is now archived and is closed to further replies.

# Verify normal calcs

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

## Recommended Posts

The answer is probably staring me in the face again, but I guess I just need someone to verify my thinking! I am still getting to grips with 3D and learning about normals at the moment. I calculate my normal from the two vectors I produce from the three points of my triangle, and also draw the triangle and a line from the center point of the triangle to the center point plus the normal. It certainly looks correct on screen for various triangles, but I want to verify it. I guess the two ways are to take the dot product of the normal and the plane and check if it's zero, or check the angle between the plane and the normal for 90 degrees. Am I doing this correctly below? Thanks in advance.
// setup three points of triangle and draw it

VECTOR p1,p2,p3;
p1.Set(0.0f,0.0f,0.0f);
p2.Set(2.0f,2.0f,0.0f);
p3.Set(1.0f,1.0f,-2.0f);
glBegin(GL_TRIANGLES);
glColor3f(0.5f,0.0f,0.0f);
glVertex3f(p1.x,p1.y,p1.z);
glVertex3f(p2.x,p2.y,p2.z);
glVertex3f(p3.x,p3.y,p3.z);
glEnd();

// calculate 2 vectors for those 3 points

VECTOR v1,v2;
v1.x = p2.x - p1.x;
v1.y = p2.y - p1.y;
v1.z = p2.z - p1.z;
v2.x = p3.x - p2.x;
v2.y = p3.y - p2.y;
v2.z = p3.z - p2.z;

// calculate and display normal as a line projecting

// out from center of triangle

VECTOR normal, centerTriangle;
normal.Cross(v1,v2);
normal.Normalize();
centerTriangle.Set((p1.x+p2.x+p3.x)/3.0f,(p1.y+p2.y+p3.y)/3.0f,(p1.z+p2.z+p3.z)/3.0f);
glBegin(GL_LINES);
glColor3f(0.5f,0.5f,0.5f);
glVertex3f(centerTriangle.x,centerTriangle.y,centerTriangle.z);
glVertex3f(centerTriangle.x+normal.x,centerTriangle.y+normal.y,centerTriangle.z+normal.z);
glEnd();

// Verify normal

VECTOR plane;
plane = centerTriangle - p1; // ?? is this correct ??

GLfloat dotP = DotProduct(plane,normal);
GLfloat angle = acos(dotP)*(360/(2*M_PI));

[edited by - Fahrenheit451 on April 11, 2004 6:46:09 AM] [edited by - Fahrenheit451 on April 11, 2004 7:01:23 AM]

##### Share on other sites
quote:
Original post by Fahrenheit451
...I guess the two ways are to take the dot product of the normal and the plane and check if it's zero, or check the angle between the plane and the normal for 90 degrees...

or you could take the cross product of the normal and any vector in the plane and check if the result is also in the plane, or you could .... probably quite a few ways, but your way is a good way.

// Verify normalVECTOR plane;  // assuming plane is any vector in the planeplane = centerTriangle - p1; // if centertriangle is correctly calculated then this is correct...// also guaranteed valid by definition of your plane...plane = p1 - p2;  plane = p2 - p1; plane = p2 - p3;plane = p1 - p3;...// normalize the plane vector GLfloat dotP = DotProduct(plane,normal); // this will give the right result if both vectors are normalized GLfloat angle = acos(dotP)*(360/(2*M_PI));   // pretty redundant calculation if checking for zero

since 360/(2*PI) != 0 ,(in my universe anyway) you could check the dotP directly. -(do you want the angle in radians or degrees? this would yield degrees - as you of course already know)
if you normalize both the plane and the normal vector the calculation would be (in radians of course)
angle = acos(dotP)

The mathematically right formula would be: (in some quasi mathcode)
cos(alpha) = (v*u)/(|v||u|)
read as v dot u divided by abs(v) times abs(u)

[edited by - JOL on April 11, 2004 8:42:53 AM]

##### Share on other sites
Thanks a lot JOL. I guess I was thinking that the two vectors had to be "joined" to properly work out the dot product, but it appears not.

Cheers

##### Share on other sites
quote:
Original post by Fahrenheit451
I guess I was thinking that the two vectors had to be "joined" to properly work out the dot product, but it appears not.

I understand what you mean but you must remember to diffrentiate between points and directions. And they are actually joined in origo...
The normal is already calculated and actually not ''on'' the plane anymore, it emerges from origo and describes a direction and not a point. Calculating a vector lying in the plane also emerges from origo. You should always remember to distinguish between a vector pointing at a point in the plane, and a vector itself lying in the plane.
Points and directions, same but different.

1. 1
Rutin
38
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633350
• Total Posts
3011473
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!