Deferred lighting

Started by
27 comments, last by Narf the Mouse 12 years ago

well in my case its absolutely basic stuff



PixelToFrame Diffused_PixelShader( VertexShaderOutput input ) : COLOR0
{

float3 normal = normalize(cross(ddy(input.PosDummy), ddx(input.PosDummy)));

float4 diffuse1 = dot( DiffuseLightDirection, normal ) * DiffuseIntensity * DiffuseColor;

float4 ambient = AmbientIntensity * AmbientColor;

PixelToFrame Output = (PixelToFrame)0;
Output.Color = input.Color;
Output.Color.rgb *= saturate((diffuse1/2)+ambient) * ambient ;

return Output;
}


I don't really understand much of what each line is doing (besides multiplying vectors), I really only know what my first line really does, as that was explained to me in another post.


You need to saturate your dot product to eliminate having areas go completely black with normals that point in the same direction as the light, this is way the bottoms of your trees go black. You do realise that for deffered shading you should create a G-buffer, which contains normal, albedo and other stuff you need when rendering the light?

This article will show you the basic directional light example in XNA, this is forward shading though, but the equations stay the same.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Advertisement
well I made some changes to the code, following both your example and the example of the web page you referred, but it looks exactly the same.



float4 diffuse1 = DiffuseColor * dot(normal , normalize(DiffuseLightDirection * DiffuseIntensity) ) ;
float4 ambient = AmbientColor * AmbientIntensity ;
PixelToFrame Output = (PixelToFrame)0;
Output.Color = input.Color;
Output.Color.rgb *= saturate( ambient + diffuse1);


[attachment=7959:shaded test2.png]
Ok, from your comments, it seems you're not sure what the math is doing.

You should probably pick up a good math book that explains vectors and matrices (I recommend "3D Math Primer for Graphics and Game Development", by Fletcher Dunn and Ian Parberry. I have Version 1, which is excellent. I can't comment on Version 2.

Start here, after you've got that book: Link

well I made some changes to the code, following both your example and the example of the web page you referred, but it looks exactly the same.



float4 diffuse1 = DiffuseColor * dot(normal , normalize(DiffuseLightDirection * DiffuseIntensity) ) ;
float4 ambient = AmbientColor * AmbientIntensity ;
PixelToFrame Output = (PixelToFrame)0;
Output.Color = input.Color;
Output.Color.rgb *= saturate( ambient + diffuse1);


[attachment=7959:shaded test2.png]

You are still forgetting to saturate the result of the dot operation. The dot product of a normal vector that's in the same direction as the light direction will end up being in between 0 .. -1. For light calculations you want to ignore the minus dot product values and just make them 0 so they don't contribute any diffuse light. if you then have one of these black pixels and add your ambient light value to it, you should end up with a darker looking patch that is not black. If there is no ambient light then the pixel should be black if diffuse light doesn't hit it.

The book narf recommended is really good to get to grips with what is going on in these calculations.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

actually I'm half way reading it.... really interesting.
night creature, that solution worked great, so light should be from 0 to 1... and saturate clams values from 0 to 1... and if a number tend to go way below 0 is hard to get them up to do something ... got it now.

the other question I have is, how come some areas are hard to be differentiated, I know I still need shadows but seems like something is still missing here.

look at center of the following image, you can hardly tell there are cubes here.

[attachment=7992:shaded test3.png]

night creature, that solution worked great, so light should be from 0 to 1... and saturate clams values from 0 to 1... and if a number tend to go way below 0 is hard to get them up to do something ... got it now.

the other question I have is, how come some areas are hard to be differentiated, I know I still need shadows but seems like something is still missing here.

look at center of the following image, you can hardly tell there are cubes here.

[attachment=7992:shaded test3.png]

Ok, so you are reading that book.Yaay. :)

I'd suggest attenuation, but directional lights don't have that, being just a direction and not a position. It's useful to know, though, so I'll go ahead and explain it.

Attenuation, in this context, is the reduction in light levels from being farther from the light. It is calculated as:

Attenuation = 1.0 / ( ( Constant ) + ( Linear * Distance ) + ( Quadratic * Distance * Distance ) )

You then multiply this by your light colour. If you don't saturate attenuation, but do saturate the final light colour, near areas saturate to white. You may or may not want this automatic effect.

Now, to explain it, including the probably-obvious stuff, just to be thorough:
Constant provides a flat reduction (or increase, for less than 1.0) in light.
Linear divides your light by some multiple of distance. It provides a steady and quite useful reduction in light.
Quadratic is physically realistic; it mirrors the formula for energy "loss" over distance. It reduces your light by the square of the distance, multiplied by some value.
Distance is the distance to a vertex (for vertex lighting; much less accurate, but much faster) or a pixel (for pixel lighting; much more accurate, but much slower).

Put them all together and you have a complete equation for attenuation.

Now, what you're missing right now is probably fog. Simple fog works like this:
Position := CameraPosition
Attenuation := Attenuation formula
Colour := A float4 Colour.
Range := Anything beyond this doesn't need to be drawn.
On := a true/false switch.

Position is the center of your fog fading; this should be your camera position for most instances.

You then use that as follows:
Distance := Pixel/Vertex position - Fog.Position
Interpolation := Attenuation (we're going to use this to fade from easily visible to fogged).
Output.Colour := Saturate( (Colour you calculated * Interpolation ) + ( Fog.Colour * ( 1.0 - Interpolation ) ) )

This is an alpha interpolation for fog; as distance increases, attenuation will also (provided you set either Linear or Quadratic to a positive number) decrease.
Since interpolation is equal to attenuation, as distance increases, the final colour will be closer to your fog colour.
the attenuation stuff seems great, but as you pointed out, I don't have distance, so I think I will be using this for when I do my point lights in the world.

Now on that fog topic, this will be making my colors blend to the fog color, and when the object are beyond my range, they will turn into the fog color, right? as distance increases the attenuation value should decrease making my color fade out

the attenuation stuff seems great, but as you pointed out, I don't have distance, so I think I will be using this for when I do my point lights in the world.

Now on that fog topic, this will be making my colors blend to the fog color, and when the object are beyond my range, they will turn into the fog color, right? as distance increases the attenuation value should decrease making my color fade out

Yep. It's not the most complex or detailed fog in the world, but it does the job and is used, even today.

night creature, that solution worked great, so light should be from 0 to 1... and saturate clams values from 0 to 1... and if a number tend to go way below 0 is hard to get them up to do something ... got it now.

the other question I have is, how come some areas are hard to be differentiated, I know I still need shadows but seems like something is still missing here.

look at center of the following image, you can hardly tell there are cubes here.

[attachment=7992:shaded test3.png]


I'd work through that book first to be honest, for light equations and a solid understanding of the math involved is necessary. Especially when spotlights, projected lights and shadows come along.
Having a solid understanding of 3D math will help as well when more advanced effects and shaders are presented to you.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

This topic is closed to new replies.

Advertisement