I have created a deferred renderer using OpenGL that seems to be working great for exactly one frame. Then it renders just blackness. For the code below I have flattened the architecture of the render quite a lot, but I think everything relevant is included. If more context is needed you can look here.
This first piece is run at program initialization:
// corresponds to deferredRenderer.Bind(); glViewport(0, 0, display.GetWidth(), display.GetHeight()); glClearColor(0, 0, 0, 1); glFrontFace(GL_CW); glCullFace(GL_BACK); glDepthFunc(GL_LEQUAL);
Then the loop begins. First the renderer is bound for object/material pass:
// corresponds to deferredRenderer.BindForObjectPass(); gBuffer.BindAsDrawFrameBuffer(); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Then this code is then run for every object (each has its own shader):
materialShader.Bind(); diffuseTexture->Bind(0); shader.SetUniform("u_diffuse", 0); glm::mat4 modelViewMatrix = camera.GetViewMatrix() * transform.GetModelMatrix(); glm::mat4 projectionMatrix = camera.GetProjectionMatrix(); materialShader.SetUniform("u_model_view_matrix", modelViewMatrix); materialShader.SetUniform("u_projection_matrix", projectionMatrix); glBindVertexArray(vertexArray); glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0); glBindVertexArray(0);
After all objects have been renderered, the light pass begins. At this stage in development it's just one shader with a hardcoded light:
// corresponds to deferredRenderer.RenderLightPass(); display.BindAsDrawFrameBuffer(); glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT); screenSpaceShader.Bind(); gBuffer.GetAlbedoTexture().Bind(10); screenSpaceShader.SetUniform("u_albedo", 10); gBuffer.GetNormalTexture().Bind(11); screenSpaceShader.SetUniform("u_normals", 11); glBindVertexArray(vertexArray); glDrawArrays(GL_TRIANGLE_STRIP, 0, 8); glBindVertexArray(0);
And finally the backbuffer is switched to the front:
After this the renderer is bound for object pass again and continues to loop.
Note that the first frame renders exactly as it should, so I think it's safe to assume it's at least somewhat correct. The fact that it changes after one full frame also tells me that it probably has something to do with the gl state being in a strange state after the first loop around. I have also made sure that the gBuffer renderbuffer is complete, so that shouldn't be a/the problem