Sign in to follow this  
Daniel Wilson

When to normalize vectors in a shader?

Recommended Posts

Hi, I am currently working on a shader and the result is not quite right. One question that bugs me I cannot seem to find the answer to is why and when exactly we normalize vectors in a shader. I understand that it seems to only be important for direction vectors such as the light, but is it important to do so in certain spaces with other vectors aswell? I took a naive approach at first and normalized all the time. I am not so much concerned with efficiency, just why and when it is okay or not okay to normalize a vector.

For example, the shader I am writing at them moment requires the vector from the view position to the vertex position in world space. Some shaders I see normalize this when it is in the fragment shader, but does that not destroy the value of the vector for further calculations?

Share this post


Link to post
Share on other sites
a nice little article here will explain some of the issues:

[url="http://www.lighthouse3d.com/tutorials/glsl-tutorial/normalization-issues/"]http://www.lighthouse3d.com/tutorials/glsl-tutorial/normalization-issues/[/url]

its mainly an issue caused by the interpolation of vectors, but explained in more detail in the above

Share this post


Link to post
Share on other sites
When representing a direction, it's often stored as a normalized vector ([i]i.e. a point on the unit-sphere[/i]).
You normalize vectors when they represent directions, but aren't already normalized ([i]such as when several normals have been interpolated[/i]).
Normalizing a vector that represents something else ([i]like a position[/i]) doesn't make sense ([i]unless you're trying to calculate the direction from the origin to that position[/i]).

Share this post


Link to post
Share on other sites
The basic problem is that normalization involves a square root, and a square root does not plot as a straight line - it's a curve. Linear interpolation is not going to preserve that. Edited by mhagain

Share this post


Link to post
Share on other sites
[quote name='mhagain' timestamp='1343912804' post='4965512']
The basic problem is that normalization involves a square root, and a square root does not plot as a straight line - it's a curve. Linear interpolation is not going to preserve that.
[/quote]
It doesn't really have anything to do with square root being plotted as a curve. It has to do with the fact that you can use the Pythagorean theorem, which involves a square root, to determine the length of the vector. And by dividing the vector through its length, you get a vector with the length 1 -> a unit vector. Edited by CryZe

Share this post


Link to post
Share on other sites
There isn't really a general rule when to normalize or not... You use normalized vectors when you need a normalized vector. So face normals or light directions often have to be normalized for the dot products/reflections you do during lighting calculation to make sense. On the other hand when you want to raycast something, normalization of the ray direction is often not required. There isn't really a way around understanding the math behind the operations you do. Edited by japro

Share this post


Link to post
Share on other sites
[quote name='japro' timestamp='1343923791' post='4965575']
There isn't really a general rule when to normalize or not... You use normalized vectors when you need a normalized vector. So face normals or light directions often have to be normalized for the dot products/reflections you do during lighting calculation to make sense. On the other hand when you want to raycast something, normalization of the ray direction is often not required. There isn't really a way around understanding the math behind the operations you do.
[/quote]

Yes I thought so, I just figured maybe there was some rules like "you shouldn't normalize in view space". Thanks for the tips. I shall try and keep any normalizing to a minimum though for now and see how it goes [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this