Archived

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

dta1

Calculating Normals

Recommended Posts

dta1    122
I''ve made this fuction for Normal Calculation as my OpenGL book almost told me to do. float CalculateNormal( float point1[3], float point2[3], float point3[3], float normal[3] ) { float vector1[3], vector2[3]; vector1[0] = point1[0] - point2[0]; vector1[1] = point1[1] - point2[1]; vector1[2] = point1[2] - point2[2]; vector2[0] = point2[0] - point3[0]; vector2[1] = point2[1] - point3[1]; vector2[2] = point2[2] - point3[2]; normal[0] = vector1[1] * vector2[2] - vector1[2] * vector2[2]; normal[1] = vector1[2] * vector2[0] - vector1[0] * vector2[2]; normal[0] = vector1[1] * vector2[2] - vector1[2] * vector2[2]; double lenght = sqrt((normal[0]*normal[0]) + (normal[1]*normal[1]) + (normal[2]*normal[2]) ); for (int idx = 0; idx < 3; idx++) normal[idx] /= lenght; return normal[0], normal[1], normal[2]; } Now I wonder how I can use it in a good way (I wan''t to calculate the normals for a couple of quads). I could do like this if I wanted to calculate for a triangle (It would almost work I think, you would need to use the CalulateNormal-fuction three times because the glNormal3f needs 3 parameters) float TriangleVector1 = {0.0f,0.0f,0.0f} float TriangleVector2 = {1.0f,1.0f,0.0f} float TriangleVector3 = {1.0f,0.0f,1.0f} glBegin(); glNormal3f(CalcutlateNormal(TriangleVector1,TriangleVector2,TriangleVector3)); glVertex3f(TriangleVector1); glVertex3f(TriangleVector2); glVertex3f(TriangleVector3); glEnd(); I would be glad if someone could explain normals more briefly and show me how to use them in a good way. ( I don''t want to use glEnable(GL_NORMALIZE) )

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
The normal of a triangle is a vector that points perpendicularly to all of the edges of the triangle. For example, draw a triangle on a piece of paper, the normal sticks straight up out of the paper (or straight into the paper depending on which way the triangle is facing). To calculate the normal, you need two vectors. In a triangle, you pick two of the edges. Then you take the cross product of those two edges and you have the normal. The normal of a quadrilateral is also a vector that is perpendicular to all of the edges of the quadrilateral. To get the normal of a quad, you still only need two vectors. So you pick any two edges of the quadrilateral. What this means is that if you give your CalculateNormal() function the first three points in your quadrilateral, it will calculate the normal for you. You only have to call the function once, because a quad has only one normal.

FragLegs

BTW - make sure that the three points you pass to the function are not colinear, or you might get some screwy answers.

Share this post


Link to post
Share on other sites
dta1    122
quote:
Original post by Anonymous Poster
The normal of a triangle is a vector that points perpendicularly to all of the edges of the triangle. For example, draw a triangle on a piece of paper, the normal sticks straight up out of the paper (or straight into the paper depending on which way the triangle is facing). To calculate the normal, you need two vectors. In a triangle, you pick two of the edges. Then you take the cross product of those two edges and you have the normal. The normal of a quadrilateral is also a vector that is perpendicular to all of the edges of the quadrilateral. To get the normal of a quad, you still only need two vectors. So you pick any two edges of the quadrilateral. What this means is that if you give your CalculateNormal() function the first three points in your quadrilateral, it will calculate the normal for you. You only have to call the function once, because a quad has only one normal.

FragLegs

BTW - make sure that the three points you pass to the function are not colinear, or you might get some screwy answers.


Thank you

Share this post


Link to post
Share on other sites
v71    100
well if you fin the normal in this way the normals will be always the same, you have a point normal if you find the averaged normal shared beetween multiple verticies belonging to every quad in a model , i hope it makes sense , sorry for my english....

Share this post


Link to post
Share on other sites