Jump to content

  • Log In with Google      Sign In   
  • Create Account


Deferred lighting


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
28 replies to this topic

#1 winsrp   Members   -  Reputation: 273

Like
0Likes
Like

Posted 26 March 2012 - 05:54 PM

So every time I read deferred lighting/shadowing/techniques/what ever, the example always end up like a flashlight or a light bulb, and looks great if you are going to make a nice dungeon full of lights.

On with the questions.

when a directional deferred light is on, can it be like the sun, or its always like a flash light, and If I want it to behave like the sun, I need to put it a million miles away from target?

If I have a single deferred light as sun, how does it do global illumination?

How can I make that cool effect of a sun shining... (not the lens one, just like a glow in the sky)

tx.

Sponsor:

#2 Washu   Senior Moderators   -  Reputation: 4992

Like
1Likes
Like

Posted 26 March 2012 - 07:16 PM

A directional light would typically be a light which just has a direction vector. Thus all rays emitted from the light source are considered to be parallel. The sun would be a typical example of a directional light.

A flashlight is typically a spotlight, which can be represented as a cone, or a set of nested cones for things like the penumbra.

Light bulbs tend to be point light sources, that is they are a source with a position that emit lights in all directions. However, you can treat many common "point" light sources as spotlights as well (inset lamps typically have a reflector on the backside to direct light that hits the back of the socket outwards). You can often represent point lights as spheres, with the radius of the sphere indicating the area that the light illuminates.

More importantly, in deferred rendering all lights can be seen as a form of geometry (with the exception of directional lights and global illumination/ambient). Thus any form of geometry you want can represent the light emitted by a source.

As for getting the bright blob in the sky... that's typically done through HDR.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#3 winsrp   Members   -  Reputation: 273

Like
0Likes
Like

Posted 26 March 2012 - 09:06 PM

wow lots of info... ok, so the sun can be represented with a directional deferred light... now I have (yet) another question, if I have a cube, and I use normals for my sides, and I have a light pointing straight from up to down.

-my top side is bright and nice
-the sides seem kinda shaded, looks cool
-bottom is black.. not shaded, just plain old black.

Now I have what is supposed to be ambient light, shouldn't that ambient light give some color to the bottom of the cube?

example, it does not matters what I put as ambient light, it always turns out black (see bottom of "tree")

shaded test.png

#4 Washu   Senior Moderators   -  Reputation: 4992

Like
0Likes
Like

Posted 26 March 2012 - 11:54 PM

You want to take the average of the face normals for smooth shading.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#5 NightCreature83   Crossbones+   -  Reputation: 2739

Like
1Likes
Like

Posted 27 March 2012 - 02:35 AM

Also final color equation should be something like:
finalColor = ambientLightAmount + diffuseLightAmount + specularLightAmount;

This offcourse can change depending on textures and which lighting model you use but generally speaking thats the ammount of light for each pixel on the screen.
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#6 winsrp   Members   -  Reputation: 273

Like
0Likes
Like

Posted 27 March 2012 - 09:00 AM

hmm maybe that was my problem, I had something like

saturate(ambietlight + diffuselight)

I'll give it a try, since saturate seems to normalize my values.

#7 NightCreature83   Crossbones+   -  Reputation: 2739

Like
0Likes
Like

Posted 28 March 2012 - 01:15 AM

hmm maybe that was my problem, I had something like

saturate(ambietlight + diffuselight)

I'll give it a try, since saturate seems to normalize my values.

No that equation is fine, the saturate just clamps the values between 0..1. Specular isn't always necessary and just adds a highlight anyway. You might want to check that your ambient light amount isn't zero or that your "normal dot lightVec" is positive. As soon as the light amounts go to negative this will show up as black in the color scene, especially when you saturate the values.
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#8 winsrp   Members   -  Reputation: 273

Like
0Likes
Like

Posted 29 March 2012 - 09:16 AM

seems tricky all this light stuff, in order to get a desent lighted scene, i had to do something like

saturate((diffuse/2)+ ambient) + ambient

really weird, all the other equations I tried end up with the bottom pitch black.

I'm also thinking that it might be best if I add like an if to the HLSL code to exclude the lighting when the normals are pointing down.

#9 NightCreature83   Crossbones+   -  Reputation: 2739

Like
1Likes
Like

Posted 29 March 2012 - 09:58 AM

seems tricky all this light stuff, in order to get a desent lighted scene, i had to do something like

saturate((diffuse/2)+ ambient) + ambient

really weird, all the other equations I tried end up with the bottom pitch black.

I'm also thinking that it might be best if I add like an if to the HLSL code to exclude the lighting when the normals are pointing down.

Can you show us the shader you are using for this lighting equation, or try using the built in function "lit", only works in HLSL 2.0.
But usually the pixel shader looks something like this:
float3 normal = normalize(input.normal);
float3 lightVector = normalize(input.lightVector);
float ndotl = saturate( dot(normal, lightVector) ); //Saturate here is necessary to avoid the case where ndotl < 0 or when ndotl > 0
float4 diffuseLight = diffuseColor * ndotl;
float4 ambientLight = ambientLightColor * ambientLightStrength;
float4 color = saturate( ambientLight + diffuseLight );

If in the above code ndotl = 0 then the resulting output light color is shaded by the ambient color unless it's strength factor is 0, only if both are 0 will black show up.
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#10 winsrp   Members   -  Reputation: 273

Like
0Likes
Like

Posted 29 March 2012 - 11:03 PM

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.

#11 NightCreature83   Crossbones+   -  Reputation: 2739

Like
0Likes
Like

Posted 30 March 2012 - 01:13 AM

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, Mad Max

#12 winsrp   Members   -  Reputation: 273

Like
0Likes
Like

Posted 30 March 2012 - 09:40 PM

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);

shaded test2.png

#13 Narf the Mouse   Members   -  Reputation: 318

Like
2Likes
Like

Posted 30 March 2012 - 10:26 PM

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

#14 NightCreature83   Crossbones+   -  Reputation: 2739

Like
1Likes
Like

Posted 02 April 2012 - 04:27 AM

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);

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, Mad Max

#15 winsrp   Members   -  Reputation: 273

Like
0Likes
Like

Posted 02 April 2012 - 10:04 AM

actually I'm half way reading it.... really interesting.

#16 winsrp   Members   -  Reputation: 273

Like
0Likes
Like

Posted 02 April 2012 - 10:12 AM

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.

shaded test3.png

#17 Narf the Mouse   Members   -  Reputation: 318

Like
0Likes
Like

Posted 02 April 2012 - 03:31 PM

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.

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.

#18 winsrp   Members   -  Reputation: 273

Like
0Likes
Like

Posted 02 April 2012 - 05:10 PM

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

#19 Narf the Mouse   Members   -  Reputation: 318

Like
0Likes
Like

Posted 02 April 2012 - 05:20 PM

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.

#20 NightCreature83   Crossbones+   -  Reputation: 2739

Like
0Likes
Like

Posted 02 April 2012 - 05:49 PM

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.

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, Mad Max




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS