Skip to main content
GameDev.net gamedev.net
🔒 Locked

Calculating vector normals.

Started by acw83 Mar 9, 2000 at 7:23 PM 1 replies 550+ views
Original Post
acw83
acw83
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.
ThomasAtwood
ThomasAtwood
// 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;


acw83
acw83
Thanks, I''ll implement that ASAP!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.