Jump to content
  • Advertisement
Sign in to follow this  
AverageJoeSSU

OpenGL Deferred Shading Discussion

This topic is 3754 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I recently implemented a deferred shading algorithm in OpenGL and CG. I am curious about my algorithm's way of handling multiple lights. How do you account for an area that is lit up by a light when you render a new light. for example, I have a red light on the left and a blue light on the right, in front of a brick wall. both lights should impact the wall however my algorithm just overwrites any data on a pixel. Is there a way to blend the two lights? maybe set the pixel += or *= or something. I looked in Nvidia's SDK and didnt see anything having to do with blending lights. I also need to implement the point light shader. I have a basic idea of how to do it does anyone have any ideas of things i need to watch out for? Also, is there a way i can attach pictures? lol... i can put a link to my webspace but i would rather not (i dont want to show all the goods yet hehe). Thanks! Joe

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by AverageJoeSSU
Is there a way to blend the two lights? maybe set the pixel += or *= or something.


Usually in deferred shading you have what's called an "accumulation buffer". It's a render target that contains the lighting information. If you're not doing HDR, you can just use additive blending when rendering each light into the accumulation buffer and you'll get what you're looking for. Otherwise you'll need two accumulation buffers that you use in a round-robin kind of fashion. In that case, you'll just have to do the additive blending in the pixel shader. More recent (DX 10) 3D cards may also support additive blending on floating point render targets.

Once you've rendered all the lights into the accumulation buffer, you simply multiply it with the albedo to get the final image.

Share this post


Link to post
Share on other sites
Quote:
Original post by AverageJoeSSUmy algorithm just overwrites any data on a pixel.


well your algorithm needs to be changed :)

I dont know how you are calculating each light, but this is the normal method of compositing:

(ambient * diffusecolor) + ((light1 * shadow1) + (light2 * shadow2))*diffusecolor

if you have no shadows it's even easier:
...(light1 + light2 + etc..) * diffusecolor

each "light" term is already assumed to be colored and have the proper specular component self-shadowing etc..

with differed shading, you do most of your your caluations in world space in a pixel shader, so you c can loop through many lights in one pass.. just accumulate the lights as you would in any loop...

for l=0; if l<nlights
{
lightcolor+=lightarray[l];
l++;
}

Obviously...doing mulitple lights like this is not optimized and you may run the risk of rewriting pixels that dont need to be.

Share this post


Link to post
Share on other sites
Quote:
Original post by Harry Hunt
More recent (DX 10) 3D cards may also support additive blending on floating point render targets.


Just about every SM30-capable discrete GPU supports hardware blending with fp16. You'll sure pay for it though...

Share this post


Link to post
Share on other sites
Sweet!

So i do want to do HDR just wanted to finish the deferred shading part. YOU are saying that the accumulation buffer takes care of that! That is awesome! lol.

If i look at the xmas tree example will their acc. buffer be a good example?

I'll check that out.


"
well your algorithm needs to be changed :)
"

i am already doing that i think.... (although my specular is the only "error" left in my equation).

here you can see the result of the lighting, without specular :( ... and only one light for now...

http://www.deadbeefgames.com/GravitasPictures/LightingAlmostDone.jpg

http://www.deadbeefgames.com/GravitasPictures/LightingFin.jpg
(without specular) :(




Share this post


Link to post
Share on other sites
Quote:
Original post by AverageJoeSSU
So i do want to do HDR just wanted to finish the deferred shading part. YOU are saying that the accumulation buffer takes care of that! That is awesome! lol.


Technically, yes. HDR really only means that you do your lighting at a higher dynamic range and FP render targets will do that. However, when most people talk about HDR what they really mean is tone mapping and bloom and that is actually a separate process which is done after the lighting phase.


Quote:

If i look at the xmas tree example will their acc. buffer be a good example?


I'm not familiar with that example so I can't comment on it, but an accumulation buffer is really quite straightforward. You pretty much do what you're doing now, except you render to the accumulation buffer and only write the lighting information to it. The round-robin method works like this

ACCUM1 + ACCUM2 are two screen-sized render targets

Set ACCUM1 as your active render target
Render the first light

Set ACCUM2 as your active render target
Set ACCUM1 as an input to the light shader
Output = ACCUM1 + second light

Set ACCUM 1 as your active render target
Set ACCUM2 as an input to the light shader
Output = ACCUM2 + third light

and so on

Then you do this:

Set the backbuffer as your active render target
Set ACCUM2 (or whichever target you rendered to last) as a shader input
Set ALBEDO (the render target in the gbuffer that contains the color information) as a shader input
Output = ALBEDO * ACCUM2

Share this post


Link to post
Share on other sites
very nice... i'll give that a shot.

Only thing left then would be a shader for point lights which is nearly similar to my directional light shader i think.

edit: and fixing my lousy specular error.

Best way i can describe it is at certain angles parts of the object is black... here is my deferred shader and light shader for it.



struct fs_mrt
{
float4 color0 : COLOR0;
float4 color1 : COLOR1;
float4 color2 : COLOR2;
};

fs_mrt GbufferF( float4 oPosition : TEXCOORD4,
float2 oTexCoord : TEXCOORD0,
float3 oNormal : TEXCOORD1,
float3 Sem : TEXCOORD3,
float3 oTangent : TEXCOORD2,

uniform sampler2D Textur,
uniform sampler2D NormalMap,
uniform float4x4 ModelViewProj

)
{
fs_mrt OUT;
float3 Normal = oNormal;


float3 vnormal = tex2D(NormalMap, oTexCoord).xyz;
float4 diffuse = tex2D(Textur, oTexCoord);

float3 binormal = cross(oNormal, oTangent);
float3x3 rotMat= float3x3(oTangent,
binormal,
oNormal);

float3 snormal = mul( rotMat , vnormal );

OUT.color0 = float4( oPosition.xyz, Sem.x );
OUT.color1 = float4( snormal.xyz , Sem.y);
OUT.color2 = float4( diffuse.xyz , Sem.z );

return OUT;
}






//Blinn-Phong Lighting model, uses RTs and light variables as input.

//Note: Specular power causing black artifacts, commented out for now.

void LightF( float4 color : COLOR,

out float4 oColor : COLOR,

float2 texcoord : TEXCOORD0,

uniform float3 lightColor,
uniform float3 lightPosition,
uniform float4 Ke,
uniform float4 Ka,
uniform float4 Kd,
uniform float4 Ks,
uniform float kC,
uniform float kL,
uniform float kQ,

uniform sampler2D pos,
uniform sampler2D nor,
uniform sampler2D dif,
uniform float3 eyeP
)
{
float4 ambient = float4( 0.0, 0.0, 0.0, 0.0);
float4 diffuse = float4( 0.0, 0.0, 0.0, 0.0);
float4 specular = float4( 0.0, 0.0, 0.0, 0.0);

//Sample the G-Buffer
float4 position4 = tex2D(pos, texcoord);
float4 normal4 = tex2D(nor, texcoord);
float4 diffuse4 = tex2D(dif, texcoord);

float3 position3 = position4.xyz;
float3 normalS = normal4.xyz;//expand(normal4.xyz);
float3 normal3 = normalS;
float3 diffuse3 = diffuse4.xyz;
float3 sem = float3( position4.w , normal4.w, diffuse4.w);
float4 emission = sem.yyyy;
float4 shininess = sem.x;
float3 lightDir = float3(lightPosition.xyz - position3);

float d = length(lightDir);
lightDir = normalize(lightDir);


float attenuation = 1.0/(kC + kL * d + kQ *d *d);

float3 halfAngle = (lightDir + eyeP)/2.0;
float nDotLD = max(0.0, dot(lightDir, normal3 ) );
float nDotHA = max(0.0, dot(halfAngle, normal3 ) );

float spec_power;
if(nDotLD <= 0.0) {
spec_power = 0.0; }
else
spec_power = pow(nDotHA, shininess);

float4 LightColor = float4 (lightColor.xyz, 0.0f );
ambient = Ka;
diffuse = Kd * LightColor * nDotLD;// * attenuation; No attenuation for directional light.
specular = Ks * LightColor;// * spec_power;//* attenuation; specular power still causing black artifacts

float4 colorr = float4( diffuse3, 0.0);
oColor = (emission + ambient + diffuse) *colorr + specular;

//oColor = position4; for debugging purposes.

}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!