Hi osg-users!

I'm trying to write my first shader, it is a very simple test program which 
sets the color on a sphere. 
The color depends on whether the sphere has moved relative to its position in 
the previous frame.

I use two varying variables, which are set in the vertex shader, see the code 
below. If the sphere has moved, the variable called displacementNDC is /= 0.
Then I expect that it should still be /= 0 in the fragment shader, but by some 
reason the value is == 0.
The other varying variable, "green", does not change when going from vertex 
shader to fragment shader.
(Just for test, I also tried to put the variable called currentPosClip in a 
varying variable. Testing for currentPosClip == 0 in the vertex shader returned 
false, but it returned true in the fragment shader.)

Please, can anybody explain what is happening? Why are the values changing to 


-----------Vertex shader ------------------
uniform mat4 prevModelViewProjectionMatrix;
varying vec2 displacementNDC;
varying vec4 green;     

void main()    
    // Calculate the previous and current position in clip space.
    vec4 currentPosClip = gl_ModelViewProjectionMatrix*gl_Vertex;       // 
Transforms vertex position 
    vec4 previousPosClip = prevModelViewProjectionMatrix*gl_Vertex;     // from 
object space to clip space.

    gl_Position = ftransform(); 

    // Calculate displacement in NDC space.
    vec2 displacementNDC = currentPosClip.xy/currentPosClip.w - 

    // If the displacement is zero, varying variable "green" is set to the RGBA 
value for green.
    if (displacementNDC == vec2(0,0))                                   // 
Object has not moved.
        green = vec4(0, 1, 0, 0);
// Object has moved.
        green = vec4(0.0);
-----------End vertex shader ------------------

-----------Fragment shader ------------------
varying vec2 displacementNDC;
varying vec4 green;     

void main()
    if (displacementNDC == vec2(0,0))
        gl_FragColor = vec4(1,0,0,1) + green;
        gl_FragColor = vec4(0,0,1,1) + green;
-----------End fragment shader ------------------

When the sphere has moved, the resulting color should be blue. But it becomes 

osg-users mailing list

Reply via email to