GLSL Texture Mapping and Projections


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 :


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(

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

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 +                    

   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,