I'm rendering to a depth stencil texture using a fbo and a rbo. After the rendering is done, I want to use the depth stencil texture in another fbo. I tried attaching the same rbo to the other fbo, and also attaching the texture as a depth stencil attachment to the other fbo, but it didn't work.
So how do I do that? (if its possible)
Here's the code that would render some texture onto the screen where I didn't put 1 into the stencil buffer
//Set up FBO with which I render to the depth and stencil textures
fbo.create();
fbo.bind();
depth_stencil.create();
depth_stencil.valid = true;
depth_stencil.bind();
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, ( GLsizei )w, ( GLsizei )h, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, 0 ); //depth + stencil
depth_stencil.width = w;
depth_stencil.height = h;
rbo.create();
rbo.bind();
rbo.set_storage_format( GL_DEPTH24_STENCIL8, w, h );
rbo.width = w;
rbo.height = h;
rbo.attach_to_frame_buffer( GL_DEPTH_STENCIL_ATTACHMENT, &fbo );
depth_stencil.attach_to_frame_buffer( GL_DEPTH_STENCIL_ATTACHMENT, &fbo );
fbo.check();
fbo.unbind();
//set up another fbo
another_fbo.create();
another_fbo.bind();
GLenum modes[] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers( 1, modes );
another_tex.create(); //create a texture to render to
another_tex.valid = true;
another_tex.bind();
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, ( GLsizei )w, ( GLsizei )h, 0, GL_RGBA, GL_FLOAT, 0 );
another_tex.width = w;
another_tex.height = h;
another_tex.attach_to_frame_buffer( GL_COLOR_ATTACHMENT0, &another_fbo );
depth_stencil.attach_to_frame_buffer( GL_DEPTH_STENCIL_ATTACHMENT, &another_fbo );
rbo.attach_to_frame_buffer( GL_DEPTH_STENCIL_ATTACHMENT, &another_fbo );
another_fbo.check();
another_fbo.unbind();
//bind and clear fbo (color depth stencil)
[...]
//render objects placing 1 to the stencil buffer
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
render();
//bind and clear another fbo (only clear color)
[...]
//render full-screen quad with some texture where we didnt render anything
glStencilFunc(GL_NOTEQUAL, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
some_texture.bind();
render_quad();
//unbind another fbo
[...]
//show our work
glDisable(GL_STENCIL_TEST);
another_tex.bind();
render_quad();