Re: [osg-users] Varying shader variable /= 0 in vertex shader, is == 0 in fragment shader

2008-11-03 Thread Andreas Lindmark
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


[osg-users] Varying shader variable /= 0 in vertex shader, is == 0 in fragment shader

2008-11-03 Thread Engvall Åsa
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