Is CG normalize buggy or is it just me?

I’m trying to learn shader programming and it’s not helping when I run into bugs like this:

Normalizing the normal map

float3 normal = normalize( 2*tex2D(tex_0, l_texcoord0) - 1);

This doesn’t produce right normal. The resulting picture is too dark.

float3 normal = 2*tex2D(tex_0, l_texcoord0) - 1);
normal = normalize(normal)

Separating the normalize into its own line produces the correct result.

Has anybody else run into thse problems before? Is there any resources (websites) that detail these gotchas?

Actually… it’s not a bug.

tex2D returns a float4, so that means the fourth component is also taken into account
In your second code, you cast the four-component color into a float3 first, so that the result of normalisation will be different as the fourth component is no longer taken into respect.

The fixed version of your first code is:

float3 normal = normalize( 2*tex2D(tex_0, l_texcoord0).rgb - 1); 

There are more ways to do it, e.g. using the f3tex2D fixes it too.

Ah. Ok. Thanks for the clarification!