Hello Guest

[SOLVED] Behaviour of normalize in fragment shaders

  • 2 Replies
  • 6701 Views
[SOLVED] Behaviour of normalize in fragment shaders
« on: July 25, 2010, 19:14:48 »
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?
« Last Edit: July 26, 2010, 08:55:23 by plummew »

*

Offline spasi

  • *****
  • 2261
    • WebHotelier
Re: Behaviour of normalize in fragment shaders
« Reply #1 on: July 25, 2010, 22:42:42 »
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.

Re: Behaviour of normalize in fragment shaders
« Reply #2 on: July 26, 2010, 08:48:30 »

Thanks - makes perfect sense.