[SOLVED] Behaviour of normalize in fragment shaders

Started by plummew, July 25, 2010, 19:14:48

Previous topic - Next topic

plummew

Hi,

I have a varying vec3 called tbnDirToLight passed into my fragment shader.

Anyone know why the following two bits of code in the fragment shader produce radically different results?

   vec3 tbnNormDirToLight = normalize(tbnDirToLight);

does not produce the same result as:

   vec3 tbnNormDirToLight  = tbnDirToLight;
   normalize(tbnNormDirToLight);

My initial thoughts are that the normalize function must change the vector passed to it rather than returning a new vector and leaving the input one unchanged.

and as varyings in frag shaders can't be changed, something undefined is happening.

but this would be odd as countless shader tutorials (from lighthouse3d, openGL forums etc.) use the first form of this code.

Any thoughts?

spasi

The first form is the correct one. The normalize function returns a new vec3 (all GLSL functions work this way) so there's nothing undefined happening (the varying isn't modified). Basically the normalize in the second piece of code is a no-op and is most likely optimized-away by the shader compiler.

plummew