Layers shader OK left - right, rubbish up - down

Hiya,

I decided to try to get to grips with GLSL, with a little project, to have a shader that appears to be one texture behind another. The vertex shader is:

uniform float depth;
varying vec2 displacement;

void main()
{
vec3 ecPosition = vec3(gl_ModelViewMatrix * gl_Vertex);
vec2 viewVecx = normalize(ecPosition.xz);
vec2 viewVecy = normalize(ecPosition.yz);

vec3 tnorm3 = gl_NormalMatrix * gl_Normal;
vec2 tnormx = normalize(-vec2(tnorm3.xz));
vec2 tnormy = normalize(-vec2(tnorm3.yz));

gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = gl_MultiTexCoord1;

if (viewVecx.x > tnormx.x) displacement.x = sin(acos(dot(viewVecx, tnormx))) * depth/dot(viewVecx, tnormx);
else if (viewVecx.x < tnormx.x) displacement.x = -sin(acos(dot(viewVecx, tnormx))) * depth/dot(viewVecx, tnormx);

if (viewVecy.y > tnormy.y) displacement.y = sin(acos(dot(viewVecy, tnormy))) * depth/dot(viewVecy, tnormy);
else if (viewVecy.y < tnormy.y) displacement.y = -sin(acos(dot(viewVecy, tnormy))) * depth/dot(viewVecy, tnormy);

gl_FrontColor = gl_Color;
gl_Position = ftransform();

}

and the fragment shader, including some debugging stuff:

uniform sampler2D tex1;
uniform sampler2D tex2;
varying vec2 displacement;

void main()
{
vec4 color1;
vec4 color2;
vec2 coordinates = gl_TexCoord[0].xy;
coordinates.x = coordinates.x + displacement.x/10.0;
coordinates.y = coordinates.y + displacement.y/10.0;

color1 = gl_Color * texture2D(tex1, coordinates);
color2 = gl_Color * texture2D(tex2, gl_TexCoord[1].xy);

/*if (displacement.x >= 0.0 && displacement.x <= 10.0) gl_FragColor = vec4(displacement.x, 0.0, 0.0, 1.0);
else if (displacement.x <= -0.0 && displacement.x >= -10.0) gl_FragColor = vec4(0.0, -displacement.x, 0.0, 1.0);
else gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);

if (displacement.y >= 0.0 && displacement.y <= 10.0) gl_FragColor = vec4(displacement.y, 0.0, 0.0, 1.0);
else if (displacement.y <= -0.0 && displacement.y >= -10.0) gl_FragColor = vec4(0.0, -displacement.y, 0.0, 1.0);
else gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);*/

gl_FragColor = mix(color1, color2, 0.4);

}

A value of 1.0 works fine for the depth.

It works more or less as expected, so that the “front” texture stays where it is relative to the vertices, but the “back” texture moves around, according to a perspective type effect, when you rotate the plane a bit (on the y axis).
However, even though the code to apply the effect for rotations on the x axis is practically identical, I get a strange sort of tearing of the “back” texture. This can be seen clearly by uncommenting the debugging stuff and commenting out the last line in the fragment shader. I’d be really grateful if anyone could point out the flaw in my design.

Thank you,

Ian.

ah I figured it out. In case anyone looks at this in the future, the vertex shader is:

uniform float depth;
varying vec2 displacement;

void main()
{
vec3 ecPosition = vec3(gl_ModelViewMatrix * gl_Vertex);
vec2 viewVecx = normalize(ecPosition.xz);
vec2 viewVecy = normalize(ecPosition.yz);

vec3 tnorm3 = gl_NormalMatrix * gl_Normal;
vec2 tnormx = normalize(-vec2(tnorm3.xz));
vec2 tnormy = normalize(-vec2(tnorm3.yz));

gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = gl_MultiTexCoord1;

if (viewVecx.x > tnormx.x) displacement.x = sin(acos(dot(viewVecx, tnormx))) * depth/dot(viewVecx, tnormx);
else if (viewVecx.x < tnormx.x) displacement.x = -sin(acos(dot(viewVecx, tnormx))) * depth/dot(viewVecx, tnormx);

if (viewVecy.x > tnormy.x) displacement.y = -sin(acos(dot(viewVecy, tnormy))) * depth/dot(viewVecy, tnormy);
else if (viewVecy.x < tnormy.x) displacement.y = sin(acos(dot(viewVecy, tnormy))) * depth/dot(viewVecy, tnormy);

gl_FrontColor = gl_Color;
gl_Position = ftransform();
}

the difference being that I took the terms x and y too literally, and although tnormy is a vec2 which takes y and z from tnorm3, the resulting components in tnormy are accessed by x and y, not y and z like I had originally. OK, well I’m quite pleased with myself.

Happy shading.

Ian.