# normalize() producing different results

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

## Recommended Posts

I've come across something weird in HLSL. Well, maybe it's not that weird, but it was unexpected for me. There's probably a simple explanation. Anyway...

when I do this in my normal mapping shader:

float3 normal = normalize(2.0f*tex2D(TextureSamplerN, PSIn.TexCoords) - 1.0f);


...I get a different result to if I do this:

float3 normal = 2.0f*tex2D(TextureSamplerN, PSIn.TexCoords) - 1.0f;
normal = normalize(normal);


It seems like the latter one produces the correct result when I run my program, but I have no idea why they're different. Any ideas?

Thanks!

##### Share on other sites

Ahh, so simple :) Thanks!

##### Share on other sites

Is it ever meaningful to normalize a float4 anyway? Apart from the perspective division I can't think of a situation where you'd want to do that rather than normalize the underlying float3.

##### Share on other sites

Is it ever meaningful to normalize a float4 anyway? Apart from the perspective division I can't think of a situation where you'd want to do that rather than normalize the underlying float3.

For normalizing a vector (or turning a position into a vector), you'd use a float3, for normalizing a quaternion you'd use a float4.

Often in graphics you want to normalize a bunch of weights -- if you've got 4 and your weighting system is based on Euclidean distance, you might find normalizing a float4 handy too

Though usually normalizing weights looks more like weights /= dot(weights, (float4)1)...

##### Share on other sites

For normalizing a vector (or turning a position into a vector), you'd use a float3, for normalizing a quaternion you'd use a float4.

Often in graphics you want to normalize a bunch of weights -- if you've got 4 and your weighting system is based on Euclidean distance, you might find normalizing a float4 handy too
Though usually normalizing weights looks more like weights /= dot(weights, (float4)1)...

I hadn't thought of quaternions, good point!

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 18
• 19
• 11
• 9