Note this code works, but two questions at the bottom.
Good stuff, I got this working this morning with a texture map, here’s some code:
(Note I attached a texture as well so you can render it from the camera view to verify the depth component is working)
// This is your texture depth buffer
glGenTextures(1, &id_FboDepth);
glBindTexture(GL_TEXTURE_2D, id_FboDepth);
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_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, *width, *height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0);
// This is optional, but it’s a texture for storing the pixels //as your camera sees the scene
glGenTextures(1, &id_FboTexture);
glBindTexture(GL_TEXTURE_2D, id_FboTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, *width, *height, 0, GL_RGBA, GL_FLOAT, NULL);
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);
glBindTexture(GL_TEXTURE_2D, 0);
// this binds it to the FBO:
glGenFramebuffers(1, &id_Fbo);
glBindFramebuffer(GL_FRAMEBUFFER, id_Fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, id_FboTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, id_FboDepth, 0);
status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status == GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT ||
//status == GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS || // this is undefined for some reason
status == GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT ||
status == GL_FRAMEBUFFER_UNSUPPORTED)
{
return false;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
Note if you want to remove the image component of the FBO, just remove the id_FboTexture generation part and add:
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
to the part where the Framebuffer is bound to stop it from trying to write pixel data to the image buffer.
You can now bind the depth buffer as a texture.
[b]Question however: This is the format that they want when we use the textureProj function correct? They don’t want us to bind a renderbuffer to a sampler correct?
Also, the line:
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, *width, *height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
with the texture depth creation. The only part I couldn’t reason out is why we’re using GL_UNSIGNED_BYTE instead of something like GL_LUMINANCE which the GL spec recommends using for depth textures.[/b]