PDA

View Full Version : FBO: depth is not stored, while the depth test is working



Goossen
12-29-2016, 08:55 AM
My question was answered elsewhere. The problem is not in this code, but in reading the depth buffer.

I want to render color and depth to an FBO, so i can merge it later with an other FBO. The depth test is working, but the depth texture is still empty after rendering (all pixels have value 0) and I have no idea why. Does anybody knows what I am doing wrong?

This is how I create the FBO:

struct FBO {
uint color;
uint depth;
uint fbo;
};
FBO createFBO(int width, int height) {
FBO fbo;
fbo.color = createFBOTexture(width, height, false);
fbo.depth = createFBOTexture(width, height, true);
fbo.fbo = createFBOBuffer(fbo.color, fbo.depth);

return fbo;
}
uint createFBOTexture(int width, int height, bool isDepthBuffer) {
uint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

if (isDepthBuffer)
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 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_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

int i = glGetError();
if (i)
std::cout << "Error while creating the FBO: " << gluErrorString(i) << '\n';

return texture;
}
uint generateFrameBuffer(uint color, uint depth)
{
int mipmapLevel = 0;

//Generate FBO
uint fbo;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);

//Attatch textures to the FBO
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, mipmapLevel);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth, mipmapLevel);

//Error check
int i = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (i != GL_FRAMEBUFFER_COMPLETE)
std::cout << "ERROR: frambuffer is not ok, status: " << i << '\n';
else {
int i = glGetError();
if (i)
std::cout << "Error while creating the FBO: " << gluErrorString(i) << '\n';
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);

return fbo;
}

This is how I render to the FBO:

glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);

glBindFramebuffer(GL_FRAMEBUFFER, fbo_pointcloud.fbo);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawPointCloud(); //Here I draw points with the depreciated fixed pipeline
glFlush();