Hey all,
I have just finished my implementation of PSM in my engine, and I'm running into an issue: when running in the fixed-function pipeline the depth texture is projected perfectly onto the scene. However, when I run it through my shaders all I end up with is white (when binding only the depth texture). Here are my GLSL vert and fragment shaders. Note:I've edited the shaders down to highlight the specific problem.
shadow.vert
varying vec4 vProjShadow;
void main()
{
vProjShadow = gl_TextureMatrix[2] * gl_Vertex;
gl_Position = ftransform();
}
shadow.frag
uniform sampler2DShadow ShadowMap;
varying vec4 vProjShadow;
void main()
{
vec4 tempColor;
tempColor = shadow2DProj(ShadowMap, vProjShadow).r;
gl_FragColor = tempColor;
}
It seems to me as if the problem lies in the "gl_TextureMatrix[2]" line of my shader, but I have no clue why since I have bound all of my texture generation and eye-linear space coord generations to texture unit 2. Just for clarification, I have pasted my depth-texture generation code below.
Create the texture:
glActiveTextureARB(GL_TEXTURE2_ARB);
glClientActiveTextureARB(GL_TEXTURE2_ARB);
// create texture id, to which the p-buffer is bound
glGenTextures( 1, &textureID );
glBindTexture( GL_TEXTURE_2D, textureID );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glTexParameteri( GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL );
Just before rendering:
glClientActiveTextureARB(GL_TEXTURE2_ARB);
glActiveTextureARB(GL_TEXTURE2_ARB);
static float genS[] = { 1.0f, 0.0f, 0.0f, 0.0f };
static float genT[] = { 0.0f, 1.0f, 0.0f, 0.0f };
static float genR[] = { 0.0f, 0.0f, 1.0f, 0.0f };
static float genQ[] = { 0.0f, 0.0f, 0.0f, 1.0f };
glEnable( GL_TEXTURE_2D );
glEnable( GL_TEXTURE_GEN_S );
glEnable( GL_TEXTURE_GEN_T );
glEnable( GL_TEXTURE_GEN_R );
glEnable( GL_TEXTURE_GEN_Q );
glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR );
glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR );
glTexGeni( GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR );
glTexGeni( GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR );
glTexGenfv( GL_S, GL_EYE_PLANE, genS );
glTexGenfv( GL_T, GL_EYE_PLANE, genT );
glTexGenfv( GL_R, GL_EYE_PLANE, genR );
glTexGenfv( GL_Q, GL_EYE_PLANE, genQ );
glMatrixMode( GL_TEXTURE );
glLoadIdentity();
glTranslatef ( 0.5f, 0.5f, 0.5f );
glScalef ( 0.5f, 0.5f, 0.5f );
glMultMatrixf( m_mS.matrix );
glMatrixMode(GL_MODELVIEW);
Thank you so much for taking the time to read this and help me with my problem.
Oh, also, should I be binding my "ShadowMap" shadow sampler to a texture unit? I've tried it with and without and have had no results either way. Thank you again!
Best regards,
Julian Spillane