Reusing G-Buffer

I would like to render a scene. I would like to render it in 2 passes and blend everything togheter. Is it possible to use the same G-Buffer. When can I clear it?

If a remove the comment in the code below only the first pass is visible on the screen. With the comment everthing I wont is visible on the screen but whit “tracks” when I move the camera around.

 38 void GBuffer::bindFrameBuffer(){
 39     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_frame);
 40     glDrawBuffers(3, m_buffers);
 41 //    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 42 }

103 void RenderVisitor::deferredShading(vector<Light> lights, Camera *camera){
104     glUseProgram(m_deferredShadingProgram->getID());
105     m_gBuffer->bindFrameBuffer();
106     //Render the whole scene in to FBO 
107     for(unsigned int i = 0; i < m_renderList.size(); i++){
108         m_renderList[i].render();
109     }
110     //renderLights(lights,camera); 
111     m_gBuffer->unBindFrameBuffer();
112 
113     m_gBuffer->bindTextures();
114     glUseProgram(m_deferredShadingProgram->getID());
115 
116     glEnable(GL_BLEND);
117     for(unsigned int i = 0; i < lights.size() ; i ++){
118         if(lights[i].isOn()){
119             lights[i].apply(GL_LIGHT0);
120             drawQuad();
121         }
122     }
/***********************************************************/
Second pass. Only code above this line appears on the screen.
If the comment [b]41 //    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[/b] is present everything apears on the screen but whit tracks.
123     //Here I wont to use the same g-buffer. 
124     glUseProgram(m_lightToGBufferProgram->getID());
125     m_gBuffer->bindFrameBuffer();
126     renderLights(lights,camera);
127     m_gBuffer->unBindFrameBuffer();
128     //Render the lights scene in to FBO 
129 
130     m_gBuffer->bindTextures();
131     glUseProgram(m_renderLightsProgram->getID());
132     drawQuad();
133     glDisable(GL_BLEND);
134     reset();
135 }

Yes, you can reuse it like that - via glClear. Most games call glClear(gl_depth_buffer_bit) 2-10 times per frame.
It’s just like any memory-array operation; you can clear/destroy the data when you no longer need it.

Thanks you that is what I am trying to do now.
Now that is what I am doing. I clear it this way

36         void clear() { bindFrameBuffer();
 37                        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 38                        unBindFrameBuffer(); }

Any I clear the FBO after I have rendered the quad to the screen. Any Idea why only my first pass(code above /***********************************************************/) is visible on the screen? If I don’t render the quads on the first pass then my light system is visible. I don’t clean the screen right? Only the FBO.

63 void rendervisitor::deferredshading(vector<light> lights, camera *camera){        
264     
265     m_gbuffer->clear();
266     m_gbuffer->bindframebuffer(); 
267     //render the whole scene in to fbo 
268     for(unsigned int i = 0; i < m_renderlist.size(); i++){
269         m_renderlist[i].render();
270     } 
271     m_gbuffer->unbindframebuffer();
272              
273     m_gbuffer->bindtextures();
274     gluseprogram(m_deferredshadingprogram->getid());          
275     glenable(gl_blend);
276     for(unsigned int i = 0; i < lights.size() ; i ++){
277         if(lights[i].ison()){ 
278             lights[i].apply(gl_light0);
279             drawquad();
280         }
281     }
282     m_gbuffer->clear();
283  /***********************************************************/
284     gluseprogram(m_lighttogbufferprogram->getid());
285     m_gbuffer->bindframebuffer(); 
286     renderlights(lights,camera);
287     m_gbuffer->unbindframebuffer(); 
288     m_gbuffer->bindtextures();    
289     gluseprogram(m_deferredshadingprogram->getid());
290     setcameralight(camera); 
291     drawquad();
292     gldisable(gl_blend);
293     m_gbuffer->clear(); 
294     reset(); 
295 }

seems like I am having a blending issue. I have 3 different directional lights on blue coming in from the front, one red from the left and one green from the right. They are stored in order lights[0] = blue, lights[1] = green and lights[2] = red.

When all three lites are applied when I render the quad only the blue(index 0) are visible. If I turn of the blue light. Only index 1(the red one) are visible. To see the green light(index 2), I have to turn of light index 0 and 1.

All three lights.

Light 0 an 1.

Light 0 off, 1 and 2 on.

Light 0 and 1 off, 2on.

Edit:

 39 void Light::apply(const GLuint light){
 40  /*   
 41     std::cout << "Applying new light "<< std::endl; 
 42     std::cout << light << std::endl; 
 43     std::cout << m_position << std::endl; 
 44     std::cout << m_ambient << std::endl; 
 45     std::cout << m_diffuse << std::endl; 
 46     std::cout << m_specular<< std::endl;*/
 47     glLightfv(light, GL_POSITION, m_position.ptr());
 48     glLightfv(light, GL_AMBIENT, m_ambient.ptr());
 49     glLightfv(light, GL_DIFFUSE, m_diffuse.ptr());
 50     glLightfv(light, GL_SPECULAR, m_specular.ptr());
 51 }

Code for applying each lite. I did a print and the values seems correct.

glClear() is governed by a lot of stuff: you should have depth-write/test enabled, the viewport should cover the cleared area, etc.

Why do you bind and unbind the FBO around glClear? The unbind seems out of place.

The problem was that GL_DEPTH_TEST was enabled when drawing the quad in the loop. The problem is solved.

Thanks!