I have a OpenGLSL shader which access a depth texture and then performs some calculations. However, additionally I would like to have a mipmapped version of the depth texture and this is where things go wrong.
What works…
- I can set up a depth texture
- I can access the depth texture in my shader (with mipmapping turned off).
- I can mipmap my depth texture (I know it works because I can use glGetTexImage() and pull the mipmapped versions off the card to main memory).
What doesn’t
- Once I turn on mipmapping I can no longer access it correctly in my shader.
Code snippets…
To set up the depth texture:
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32_ARB , x, y, width, height, 0);
To mipmap the depth texture:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 2);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glCopyTexImage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT32_ARB , x, y, (int)baseWidth/2, (int)baseHeight/2, 0);
glCopyTexImage2D(GL_TEXTURE_2D, 2, GL_DEPTH_COMPONENT32_ARB , x, y, (int)baseWidth/4, (int)baseHeight/4, 0);
glCopyTexImage2D(GL_TEXTURE_2D, 3, GL_DEPTH_COMPONENT32_ARB , x, y, (int)baseWidth/8, (int)baseHeight/8, 0);
each pass (the mipmaps are generated automatically after this call):
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, x,y, width, height);
quick and easy GLSL shader to test it out:
uniform sampler2D DepthImage;
vec4 fragDepth = texture2D(DepthImage, gl_TexCoord[0].st, 0.00);
if (fragDepth.r == 1.0)
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
else
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
When I have mipmapping turned off I get green where objects are located and red everywhere else (as I should). However, once I turn on mipmapping all I get back is red everywhere (in every channel).
Anyone have any ideas?
P.S. I know I know, mipmapping a depth texture will not give me correct depth information. I don’t need the “correct” values.