OpenGL Depthmap Rendering

A quick and dirty way to render depthmaps. It requires two passes, so it’s not optimal.

I’ll be using this to pass a depthmap texture to a sobel edge detector (in GLSL) in order to draw contours by generating them in image-space. Other renders (normal-mapped, regular diffuse) will be tested too.

Please note that the proper way to do this is to use a FrameBuffer Object to render to, but since compatibility still is an issue (although they’ve been around since 2004), I’m still using the copyTexSubImage()-way. Mea culpa.

Code behind the cut.

// generate texture (in init method)
init(){...
glGenTextures(1, &depthTexture);
glBindTexture(GL_TEXTURE_2D, depthTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 512,
512, 0, GL_DEPTH_COMPONENT,GL_FLOAT, 0);}

// draw the mesh to fill the depth buffer
glDisable(GL_LIGHTING);
draw_mesh();
// copy depth buffer to texture
glBindTexture(GL_TEXTURE_2D, depthTexture);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 512, 512);
// clear screen
cls();
// enable texturing and bind the depth texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, depthTexture);
// overlay texture to fullsizequad
drawFullsizeQuad();

Comments are closed.