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;
gl_TexCoord = gl_MultiTexCoord0;
normal = normalize(gl_NormalMatrix * gl_Normal);
lightDir = normalize(vec3(gl_LightSource.position));
mat4 projection = mat4(
projY = gl_ProjectionMatrix * gl_ModelViewMatrix * projection * gl_Vertex;
gl_Position = ftransform();
position = gl_Position.xyz;//gl_Position.xyz;
vec4 color = texture2D(normalTex,gl_TexCoord.st);
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;
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,