Full Source Code of initRenderToDepthTexture()
main.cpp
void initRenderToDepthTexture()
{
// Generate the depth texture ID
glGenTextures(1, &depthTex);
glBindTexture(GL_TEXTURE_2D, depthTex);
// set texture size and format, high resolution 24bit for depth
glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT24, shadowMapWidth, shadowMapHeight);
// set Interpolation and Out-of-range texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Assign the depth buffer texture to texture channel 1
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, depthTex);
// Render to Texture
// Create and set up the FBO
glGenFramebuffers(1, &shadowFBO);
glBindFramebuffer(GL_FRAMEBUFFER, shadowFBO);
// use the depth texture as the framebuffer
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
GL_TEXTURE_2D, depthTex, 0);
glDrawBuffer(GL_NONE);
GLenum result = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if( result == GL_FRAMEBUFFER_COMPLETE) {
printf("Framebuffer is complete.\n");
} else {
printf("Framebuffer is not complete.\n");
}
glBindFramebuffer(GL_FRAMEBUFFER,0);
}Last updated