Hello,
I am attempting to save the both the frag and depth buffers to a texture for use with subsequent render passes. To verify that the depth buffer is correctly being rendered to the texture by displaying the texture to the screen (as well as saving it as a TGA), where the result is a texture completely populated with values of 1.0. After looking at the documentation and several examples, my code is as follows:
glActiveTexture(GL_TEXTURE0);
// ----- THE DEPTH TEXTURE --------
glGenTextures(1, &depthTextureId);
glBindTexture(GL_TEXTURE_2D, depthTextureId);
glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT, 1024, 780, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
GLfloat border[] = {1.0f, 0.0f,0.0f,0.0f };
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER );
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);
glBindTexture(GL_TEXTURE_2D, 0);
// ------ THE RENDER TEXTURE ------
glGenTextures(1, &renderTex);
glBindTexture(GL_TEXTURE_2D, renderTex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024 , 780, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
// ---- setup the FBO ------
glGenFramebuffers(1, &fboHandle);
glBindFramebuffer(GL_FRAMEBUFFER, fboHandle);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTextureId, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderTex, 0);
GLenum drawBuffs[] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, drawBuffs);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
The first pass code is as follows:
glEnable(GL_DEPTH_TEST);
glViewport(0,0, 1024, 780);
// make the FBO active
glBindFramebuffer(GL_FRAMEBUFFER, fboHandle);
// do a gl clear, and set the color to white
glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 projection = glm::perspective<float>(90, double(m_ActiveMovie->m_screen.Width())/double(m_ActiveMovie->m_screen.Height()), 0.10, 600.0);
glm::mat4 view = glm::lookAt<float>(glm::vec3(0,0,300), // eye
glm::vec3(0,0,0), // center
glm::vec3(0,1,0) // up
);
/* Draw several objects with shaders that work correctly at around z = +/- 20 */
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_DEPTH_TEST);
glFlush();
glFinish();
And the resultant textures are rendered to the screen by drawing a quad over the screen and using:
glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Draw some background objects */
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, renderTex);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, depthTextureId);
m_Shaders[SWF_RENDER].SetUniformI("Tex1", pVar);
/* Draw quad to screen and swap buffers */
Where pVar lets me switch between the two textures at run time. The renderTex displays correctly, however, like I said, the depthTextureId does not. Additionally, I should mention that the final render shader is using a Sampler2D.
If anyone has any ideas as to why this is not working, please let me know. Thanks.