Of course!
Thank you very much! Sometimes it's hard to see such easy things,  I think this 
happens to other people too... Doesn't it? :-)
Getting this answer was a perfect start of the working day. Now I feel very 
happy and will take further steps into the shader world. 
 
Asa

________________________________

Från: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] För Andreas Lindmark
Skickat: den 3 november 2008 17:31
Till: OpenSceneGraph Users
Ämne: Re: [osg-users] Varying shader variable /= 0 in vertex shader,is == 0 in 
fragment shader


This line:
vec2 displacementNDC = currentPosClip.xy/currentPosClip.w - 
previousPosClip.xy/previousPosClip.w;
should be
displacementNDC = currentPosClip.xy/currentPosClip.w - 
previousPosClip.xy/previousPosClip.w;

Otherwise you will be declaring a new variable with the name displacementNDC 
the main function as scope.

/Andreas


2008/11/3 Engvall Åsa <[EMAIL PROTECTED]>


        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 zero? 

        Regards, 
        Asa 

        -----------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 - 
previousPosClip.xy/previousPosClip.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); 
            else                                                                
        // 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; 
            else 
                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 red. 


        _______________________________________________
        osg-users mailing list
        osg-users@lists.openscenegraph.org
        
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
        
        


_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to