Problems with deferred shading & light volumes

Hi everyone,

I’m trying to use light volumes in my deferred renderer and have been having problems shading in the pixels inside the light volume…or at least if they were being shaded. Depending on where my camera is it will shade or not shade. I’ve uploaded a video of what’s happening on youtube here. I’m currently doing this for point lights so the light volumes will be sphere meshes.

I’m trying to get view-space position from my depth texture, I think there’s something wrong with calculating my texcoords:

vertex shader code:


varying vec3 pos;

void main()
{
    pos = gl_ModelViewMatrix * gl_Vertex;

    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

pixel shader:


uniform sampler2D depthTex;
varying vec3 pos;

// dimensions of screen
uniform vec2 dimensions = vec2(1280, 720);

void main()
{
   vec4 spos = gl_ProjectionMatrix * pos;
   spos.xy /= spos.w;

   vec2 texCoord = spos.xy * 0.5 + 0.5;
   texCoord += 0.5/dimensions;

   // access depth
   float Z = texture2D(depthTex, texCoord).r;

   vec4 H = vec4((texCoord.x * 2.0) - 1.0, (texCoord.y * 2.0) - 1.0, Z, 1.0);

   vec4 D = gl_ProjectionMatrixInverse * H;
   // should be in view-space?
   vec3 mvpos = D.xyz/D.w;	

   // do lighting ...
}

This is pretty much closest I’ve got to shading in pixels inside the light volumes, I’ve tried various different ways of getting the view-space position from the depth texture and it all ended in no shaded pixels and frustration. I’d appreciate any help!

If it helps, the way I’ve done it is when I created the FBO for the lighting, I attached the depth texture id from the MRT FBO to it so in the light FBO:

glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, MRT_FBO.getDepthId(), 0);

and when I do the lighting I bind the light FBO without clearing the depth buffer:

LIGHT_FBO.bind();
glClear(COLOR_BIT);
disableDepthTest, disableDepthWrites;
dir_light_shader->enable(); draw_dir_lights();
enableDepthTest, disableDepthWrites; depthfunc(GL_LEQUAL);
pt_light_shader->enable(); draw_pt_lights();

This probably might affect the depth buffer?

ok, I think I may have solved the problem by doing this:


float Z = texture2D(depthTex, texCoord).r * 2.0 - 1.0;

The lights don’t seem to be moving now. The problem of shading the pixels inside of the volume - I can sometimes see the walls being shaded but sometimes they’re not being shaded. I can see that the sphere mesh is being shaded but not any of the pixels inside the mesh. Would this have something to do with the depth buffer settings?