Skip to main content
gamedev.net
🔒 Locked

Deferred Rendering Point Lights.

Started by MDiaz727 Aug 28, 2011 at 10:26 PM 0 replies 1.5k views
Original Post
MDiaz727
MDiaz727
So I've been implementing Deferred Rendering into my project but I'm having trouble with the Point Light implementation.
I've been doing everything exactly like in this tutorial but it's not working. : http://www.catalinzima.com/tutorials/deferred-rendering-in-xna/point-lights/

I just get really bad light artifacts, I don't know if it's my GBuffer that is messed up or my Point Light glsl code.

The way I store my depth in my GBuffer is like this, but I'm not sure if this is correct.

Vert Shader:

gl_Position = gModelViewproj * vec4(gVertex, 1.0);

gDepthBuffer.x = gl_Position.z;
gDepthBuffer.y = gl_Position.w;

gNormalBuffer = gModel * vec4(gNormal, 0.0);


Frag Shader:

gl_FragData[0] = gDepthBuffer.x / gDepthBuffer.y;

gl_FragData[1] = 0.5 * (normalize(gNormalBuffer) + 1.0;
gl_FragData[1].a = 1.0;

gl_FragData[2] = texture2D(gTexMap0, gAlbedoBuffer);


Everything in my depth buffer is very close to white but the closer things they get closer to grey.
Not sure if this is correct.

The Deferred Rendering works good with directional lights but Point Lights don't work.
Now the code I use to calculate the position from the depth buffer for the point lights is this:

Vert Shader

vec4 vWorldPos = gModel * vec4(gVertex, 1.0);
vec4 vViewPos = gView * vWorldPos;

gl_Position = gProjection * vviewPos;

gScreenPos = gl_Position;


Frag Shader

// Obtain Screen Position.
gScreenPos.xy /= gScreenPos.w;

// Obtain Texture coords.
vec2 vTex = 0.5 * (vec2(gScreenPos.x, -gScreenPos.y) + 1.0);

// Half Pixel
vec2 vHalf;
vHalf.x = 0.5 / 1024.0; // My screen resolution width
vHalf.y = 0.5 / 768.0; // height

vTex -= vHalf;

// Get Normal Data
vec4 vNormaldata = texture2D(gNormalBuffer, vTex);
vec3 vNormal = 2.0 * vNormalData.xyz - 1.0;

// Read Depth
float fDepth = texture2D(gDepthBuffer, vTex).r;

// Calculate screenspace position.
vec4 vPosition;
vPosition.xy = gScreenPos.xy;
vPosition.z = fDepth;
vPosition.w = 1.0;

// Convert to world space
vPosition = vPosition * mInvertViewProjection;
vPosition /= vPosition.w;

// Calculate light stuff
vec4 vlightVector = vLightPos - vPosition;

// do diffuse lighting stuff........


I'm really not sure which way to go about this. Also all my buffers are of the same format for now, I don't know if that would cause any issues right now but I dont think it will. The format Im using for my GBuffers is:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

I'm not sure if it's my depth buffer thats messed up or when I calculate my world space position.
Thanks in advance.

Topic Locked

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

Sign in to reply to this topic.