Hello,
The goal of my project is to manually edit depth and color buffer values. The first version of this project used glDrawPixels() and it worked fine, but it was also painfully slow. To speed up the process, some people suggested that I should use glTexSubImage2D() along with FBOs instead of the rather slow glDrawPixels() to edit RGB or zbuffer values. However, I can’t seem to successfully copy data to textures using glTexSubImage2D().
The code starts by initialising the FBOs along with its textures. I’ve decided to use a texture for the depth buffer as I need to read and write to it every frame.
glGenFramebuffers(1,&FBOtest);
glBindFramebuffer(GL_FRAMEBUFFER, FBOtest);
glGenTextures(1,&TEXtest);
glBindTexture(GL_TEXTURE_2D, TEXtest);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 1920, 1080, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D,0);
glGenTextures(1,&DEPTHtest);
glBindTexture(GL_TEXTURE_2D, DEPTHtest);
glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 1920, 1080, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D,0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, TEXtest, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, DEPTHtest, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
Later on, the program enters into a rendering loop where the color and depth textures should be edited following some pattern. tabDepth is an array representing the depth buffer values for each pixel and needs to be copied to the depth texture.
//Rendering
glBindFramebuffer(GL_FRAMEBUFFER, FBOtest);
... // tabDepth editing algorithm
glPushAttrib(GL_VIEWPORT_BIT);
glViewport(0,0,1920,1080);
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,1920,1080,GL_DEPTH_COMPONENT,GL_FLOAT,&tabDepth);
glReadPixels(900,500,1,1,GL_DEPTH_COMPONENT,GL_FLOAT, &depthpixel); //Read random pixel
glPopAttrib();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
...
printf("Depth = %.3lf
",depthpixel);[/
In this case, for every frame, depthpixel has a value of 0.000, while tabDepth[][] is worth 0.35f. Therefore, glTexSubImage2D() seems to either fail to write correctly or writes somewhere else and glReadPixels() doesn’t read the correct buffer. I suspect that the problem lies in those lines, but I’m having trouble figuring it out.
Does anyone know where my error might be?
Thanks.