GLSL Texture Mapping and Projections

Hello,



I'm working on a glsl projector with a texture as a shadow map. I'm experiencing few troubles with the projections, since the result I get are dependent of the objects and shouldn't, it actually should only be dependant on the position in the scene and the position in the screen.



So I've got one texture for the shadow, the texture actually takes all the screen. For the textures coordinates, I have 2 projections :

  • One for the projection on the XZ plan (projection from the top)
  • One for the projection on the screen



    These are the  vert and frag shaders I devellopped :



    Vertex:


varying vec4 projY;

void main()
{
   gl_TexCoord[0] = gl_MultiTexCoord0;
   
   normal = normalize(gl_NormalMatrix * gl_Normal);      
   lightDir = normalize(vec3(gl_LightSource[0].position));
   
   mat4 projection = mat4(
      1.0,0.0,0.0,0.0,
      0.0,0.0,0.0,0.0,
      0.0,0.0,1.0,0.0,
      0.0,0.0,0.0,1.0
   );

   projY = gl_ProjectionMatrix  *   gl_ModelViewMatrix * projection *  gl_Vertex;
   
   gl_Position = ftransform();
   position = gl_Position.xyz;//gl_Position.xyz;
}



Fragment :

void main()
{
   vec4 color = texture2D(normalTex,gl_TexCoord[0].st);
   vec4 shadowColor;
   vec3 ct;
   vec3 cf;
   float intensity;
   float at;
   float af;

   intensity = max(dot(lightDir,normalize(normal)),0.0);
   cf = intensity * (gl_FrontMaterial.diffuse).rgb +                    
      gl_FrontMaterial.ambient.rgb;      

   af = gl_FrontMaterial.diffuse.a;            
   ct = color.rgb;      
   at = color.a;
   
   
   //XY projection
   shadowColor = texture2D(shadowTex,(vec2(projY.x/projY.z,projY.y/projY.z)+1.0)*0.5);


   gl_FragColor  = vec4(shadowColor * ct * cf,af * at);
}



The shadow is mixed with the object texture.

I think the problem comes from the fact that I'm doing the XZ projection before  the ModelView transformation. I tried to do it after but no result...
It works well on one specific object, but textures coordinates are not good when I have too different objects for example.

Would be great if someone see the problem!

Thanks in advance,
Boris