# length of a vector normal

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

## Recommended Posts

Hi there, i have a directional light that has a normal of (0,0,-10) As my vertex normal decreases from (0, 0, 1) to (0, 0, 0.1), my vertex fades to black. Why is this? As far as my primitive mind theorizes, to vertex normal are of 1 and 0.1, are facing in the exact same direction i.e. straight at the light. So why is one at full brightness and the other dark? Secondly, based on this, with a light normal at (0,1,1), will a vertex normal of (0,-1,-1) be at full lighting or (0, -0.7, -0.7). (in real space, going from position (0, 0, 0) to (0, 0.7, 0.7) is actually a distance of 1

##### Share on other sites
Don't you need to normalize the normals before using them for lighting?

##### Share on other sites
I guess not, as i am rotating a triangle with appropriate lighting without normalizing (by the way what does it mean to normalize normals? and how do i do that?) and it lights correctly.

I have found the answer to my second question:
(0, -0.7, -0.7) yields the maximum lighting because the distance from origin (assumed at (0,0,0)) to this point is 1. Anything over this has no effect e.g. (0,-1,-1) is a distance of 1.415 which is greater than 1 and has no effect, it stays at maximum lighting.

##### Share on other sites
Quote:
 Original post by chosendlI guess not, as i am rotating a triangle with appropriate lighting without normalizing (by the way what does it mean to normalize normals? and how do i do that?) and it lights correctly.I have found the answer to my second question:(0, -0.7, -0.7) yields the maximum lighting because the distance from origin (assumed at (0,0,0)) to this point is 1. Anything over this has no effect e.g. (0,-1,-1) is a distance of 1.415 which is greater than 1 and has no effect, it stays at maximum lighting.

Normalizing a normal is making it of unit length. As you found out in your second question, a normal must be of unit length for it to light correctly. One typically normalizes a vector by dividing its components by the magnitude of the vector. In your case, 1 / 1.415 is roughly 0.7, so, normalizing (0, -1, -1) yields (0, -.7, -.7).

-jouley

##### Share on other sites
are there any easy to use functions associated with normalizing vectors (save me the time of doing it myself!)

##### Share on other sites
Quote:
 Original post by chosendlare there any easy to use functions associated with normalizing vectors (save me the time of doing it myself!)

That depends on the vector implementation you're using. The GLSL and HLSL shader languages have a built-in normalize function. Most other libraries let you call a normalize-method on a vector. If you have a homebrew vector class, you'll have to do it yourself, something like this:
void Vector3::normalize() {   float len = sqrt(x*x + y*y + z*z);   x /= len;   y /= len;   z /= len;}

##### Share on other sites
If you're using a vector maths library, there ought to be a normalising function in that. Here's a simple one for 3D vectors if you're rolling your own (pseudo-C#):
void Normalise(){ double mag = Magnitude; if(mag == 0) throw new ArgumentException("Can't normalise zero length vector!"); double fac = 1/mag; x *= fac; y *= fac; z *= fac;}double Magnitude { get { return Math.Sqrt((x*x)+(y*y)+(z*z)); } }

##### Share on other sites
If you're using DirectX, the Vector3 class has a function called .Normalize( ) that normalizes a vector. If you're using OpenGL, there's no native functionality, but you can call glEnable(GL_NORMALIZE), and it will normalize normals for you at a slight performance cost. If you're using some other vector class with OpenGL, though, it's likely to have some built-in functionality that you can use. If you're using anything without some other vector class, you may want to consider finding/writing one; here's a recent thread with one, free to use.

-jouley

Thanx guys!

• 10
• 9
• 48
• 12
• 10
• ### Forum Statistics

• Total Topics
631385
• Total Posts
2999705
×