Hi!
I’m having some problems implementing projective texturing using GLSL.
------------ Vertex Shader ------------
uniform mat4 g_projectorMatrix;
void main(void)
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_TexCoord[0] = g_projectorMatrix * gl_Vertex;
}
------------ Fragment Shader ------------
uniform sampler2D g_projectorTexture;
void main(void)
{
gl_FragColor = texture2DProj(g_projectorTexture, gl_TexCoord[0]);
}
The scene consists of the following quad:
glVertex3f(-10.0f, -10.0f, -30.0f);
glVertex3f(10.0f, -10.0f, -30.0f);
glVertex3f(10.0f, 10.0f, -30.0f);
glVertex3f(-10.0f, 10.0f, -30.0f);
g_projectorMatrix is setup using the following code:
float projectorFOV = 1.57f;
float projectorAspectRatio = 1.0f;
float projectorNear = 1.0f;
float projectorFar = 1000.0f;
float f = 1.0f / tan(projectorFOV * 0.5f);
Matrix4x4f projection(
f / projectorAspectRatio, 0.0f, 0.0f, 0.0f,
0.0f, f, 0.0f, 0.0f,
0.0f, 0.0f, (projectorFar + projectorNear) / (projectorNear - projectorFar), (-2.0f * projectorFar * projectorNear) / ( projectorNear - projectorFar), 0.0f, 0.0f, -1.0f, 0.0f);
Matrix4x4f scaleAndBias(
0.5f, 0.0f, 0.0f, 0.5f,
0.0f, 0.5f, 0.0f, 0.5f,
0.0f, 0.0f, 0.5f, 0.5f,
0.0f, 0.0f, 0.0f, 1.0f);
Matrix4x4f projectorMatrix = scaleAndBias * projection;
Since the projector is positioned at 0, 0, 0 looking down -Z the projector “view” matrix is ignored.
What am I missing here?
It seems like the q-coordinate of gl_TexCoord[0] equals 0 since the following fragment shader produce a yellow quad:
uniform sampler2D g_projectorTexture;
void main(void)
{
if(gl_TexCoord[0].q == 0.0)
gl_FragColor = vec4(1.0, 1.0, 0.0, 0.0);
else
gl_FragColor = texture2DProj(g_projectorTexture, gl_TexCoord[0]);
}
Any ideas?
Yours sincerely,
Michael