Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problem with deferred rendering

Started by madRenEGadE May 3, 2012 at 7:59 PM 8 replies 1.6k views
Original Post
madRenEGadE
madRenEGadE
Hello all,

I am currently trying to implement a deferred renderer.
I am mostly following this tutorial: http://bat710.univ-lyon1.fr/~jciehl/Public/educ/GAMA/2007/Deferred_Shading_Tutorial_SBGAMES2005.pdf

Here are some screenshot from my current state. The test scene consists of two cubes and a point light.

1. This first screenshot looks correct
Screenshot3.jpg

2. The second screenshot is taken from the opposite side. Here you can see that the cube which is further away overlaps the near cube:
Screenshot4.jpg

3. Because I think this is a problem related to the depth buffer I rendered the depth buffer to the screen and everything was white. Then I manually created a texture and filled it with the z-values. The next two screenshots show this manually created depth texture:

Screenshot1.jpg

Screenshot2.jpg


Does anyone have an idea what the problem could be? If you need source code of specific parts just let me know.

Thx in advance.
Hyunkel
Hyunkel
Yeah, that doesn't look like a deferred rendering related issue.
Your depth buffer seems to be set up incorrectly.
Check that you have depth writing enabled and that your depth buffer is properly bound during your geometry pass.
madRenEGadE
madRenEGadE
Depth writing is enabled:


pass GeometryPass
{
Cullface = Back;
CullFaceEnable = false;
BlendEnable = false;
DepthFunc = LEqual;
Texture2DEnable[0] = false;
Texture2DEnable[1] = false;
ClearColor = float4(1, 1, 1, 1);
ClearDepth = 1.0;
ColorMask = bool4(false, false, false, false);
DepthMask = true;
DepthTestEnable = true;
}


So maybe the buffer is not bound...
madRenEGadE
madRenEGadE
This is the code for creating the depth buffer:


depthTexture = memoryManager->construct(Texture());
depthTexture->bind();
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture->getID(), 0);


Then I bind the framebuffer with

glBindFramebuffer(GL_FRAMEBUFFER, id);


Afterwards I render my stuff using:

GLenum buffers[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3};
glDrawBuffers(4, buffers);
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
renderStuff();


Could it be that the depth buffer is changed by the following pass which renders the accumulated buffer to the screen? This would explain why the depth buffer is completely white for the whole screen. But all other passed have DepthTestEnable set to false...
madRenEGadE
madRenEGadE
renderStuff does the following:

GLenum buffers[] = {GL_COLOR_ATTACHMENT0 + firstAttachment, GL_COLOR_ATTACHMENT1 + firstAttachment, GL_COLOR_ATTACHMENT2 + firstAttachment, GL_COLOR_ATTACHMENT3 + firstAttachment};

glDrawBuffers(frameBuffer->getNumColorAttachments()-1, buffers);
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);

const Math::Matrix4 view(viewport->getCamera()->getViewMatrix());

Effect* effect = effects->get("Default");
effect->activate();

while (effect->hasNextPass())
{
renderGeometry(drawCallList, effect, view);
effect->gotoNextPass();
}

effect->deactivate();


renderGeometry() just sets the material properties for the shader and renders the vertex buffers
Ashaman73
Ashaman73

renderGeometry() just sets the material properties for the shader and renders the vertex buffers

Maybe show this method too, there must be something different when rendering the two cubes ...
madRenEGadE
madRenEGadE

void DeferredRenderer::renderGeometry(const DrawCallOptimizer::DrawCalls& drawCallList, Effect* effect,
const Math::Matrix4& viewMatrix)
{
Texture* texture = 0;

for (auto i = drawCallList.begin(); i != drawCallList.end(); ++i)
{
for (auto t = 0u; t < i->material->textures.size(); ++t)
{
texture = textures->get(i->material->textures[t]);
texture->bind(t);
effect->set(t, texture);
}

effect->set("Diffuse", i->material->diffuse);
effect->set("Specular", i->material->specular);
const Math::Matrix4 mv(i->transform * viewMatrix);
effect->set("ModelView", mv);
effect->set("ModelViewProjection", mv * (viewport->getCamera()->getProjectionMatrix()));
vertexBuffers->get(i->vertexBuffer)->render(indexBuffers->get(i->indexBuffer));

for (auto t = 0u; t != i->material->textures.size(); ++t)
{
textures->get(i->material->textures[t])->unbind(t);
}
}
}


Thanks for taking the time to help me!
madRenEGadE
madRenEGadE
Ok I found out why my depth buffer was not correct.

At first I forgot to set the VertexProgram in my CGFX shader. So nothing got rendered to the depth buffer ;)
The second thing was that the depth buffer was not cleared because DepthMask was set to false from the previous frame...

Now the depth buffer looks alright when rendered to the screen. But the final image still looks like the first two screenshots from my initial post...
madRenEGadE
madRenEGadE
Finally solved it... I fixed the overlapping (second screenshot) by enabling the depth test and disabling the depth mask in the shader passes after the geometry pass.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.