Jump to content
  • Advertisement

Archived

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

acw83

Calculating vector normals.

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I hate to get technical, but I am coding light in OpenGL and need to find my triangle''s normals. I store my tri''s in counter-clockwise form if it matters. Here is my code: -------- // Normalize a vector. void Reduce2Unit(float vector[3]) { float length; // Calculate the vector''s length. length = (float)sqrt((vector[0] * vector[0]) + (vector[1] * vector[1]) + (vector[2] * vector[2])); // Correct divide by zero. if( length == 0 ) length = 1; // Normalize. vector[0] /= vector[0]; vector[1] /= vector[1]; vector[2] /= vector[2]; } // Calculate a normal based on three counter-clockwise points. void CalcNormal(float v[3][3], float out[3]) { float v1[3],v2[3]; static const int x = 0; static const int y = 1; static const int z = 2; // Calculate two vectors from the three points. v1[x] = v[0][x] - v[1][x]; v1[y] = v[0][y] - v[1][y]; v1[z] = v[0][z] - v[1][z]; v2[x] = v[1][x] - v[2][x]; v2[y] = v[1][y] - v[2][y]; v2[z] = v[1][z] - v[2][z]; // Take the cross product of the two vectors to get // the normal vector which will be stored in out. 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]; // Normalize the vector (shorten length to one). Reduce2Unit(out); } ------- That code is straiht from a book. I am testing it using a triangle that faces the camera exactly. The normal SHOULD come out to be (0, 0, 1) exactly and that value works(I hard coded it). Using QuickWatch in VC6 I checked the result and it showed it to be something odd like (Ex007F, Ex007f, 1). That wasn''t iot exactly but you get the idea. Needless to say, my lighting will not work with those values. Does anyone know what I am doing wrong???? I can send the entire source if need be.

Share this post


Link to post
Share on other sites
Advertisement
// Normalize.
vector[0] /= vector[0];
vector[1] /= vector[1];
vector[2] /= vector[2];
}
this should be:
vector[0] /= length;
vector[1] /= length;
vector[2] /= length;


Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!