Shadow Mapping in GLSL

Started by
4 comments, last by _the_phantom_ 18 years, 8 months ago
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
Advertisement
Yes, you need to bind the shadow map to a sampler (well, you bind the shadow map to a texture unit and then assign that texture unit number to the sampler), howver thats not the biggest source of problems I feel [smile]

When you use a vertex shader you take over ALL vertex processing functionality, this includes all automatic texture coordinate generation. As such all your texGen() function calls are infact not going todo anything. Instead you have todo this work yourself in the vertex shader.
-That- could make sense then. *laughs* I wasn't even considering that!
Thank you for the swift reply. Do you, offhand, have any links that detail the algorithm I'd need to implement the eye-linear texgen in my shader? I'll Google it, but I thought I'd ask just in case. :)

Thanks again for the swift reply!
Quote:Original post by Julian Spillane
Do you, offhand, have any links that detail the algorithm I'd need to implement the eye-linear texgen in my shader? I'll Google it, but I thought I'd ask just in case. :)
Check out section 2.11.4 in the OpenGL spec.
Thanks for the link. I've given it a look through, and I'll probaly implement it tomorrow (it's getting late).

Thanks a lot, though.
Quote:Original post by Julian Spillane
-That- could make sense then. *laughs* I wasn't even considering that!


heh, its a thing alot of people who are relatively new to shaders dont often get at first glance [wink]

Quote:
Thank you for the swift reply. Do you, offhand, have any links that detail the algorithm I'd need to implement the eye-linear texgen in my shader? I'll Google it, but I thought I'd ask just in case. :)


Just to add to Kalidor's reply, the Orange Book has a section on reproducing things like this in GLSL.

This topic is closed to new replies.

Advertisement