2D Dynamic Lighting

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

Recommended Posts

I began writting some code for dynamic lighting using allegro. It works alright considering it's done pixel by pixel. The only problem for what I'm going to be using it for is the horribly bright spot in the center of the light. I'm not sure how i can do this any other way though. Here is the code which calculates the intensity of the light.
float intensity;
if (u - light.r == 0 && v - light.r == 0) {
intensity = light.i;
}
else {
int a = u - light.r;
int b = v - light.r;
float d = (a * a) + (b * b);
intensity = light.i / (d);
}


(u, v) are the current offsets from a circle with it's top left corner at (0, 0). light.r is the radius, and light.i is the constant intensity of the light. light.i must be set at least to 500,000 for the light to "show up"! All that's doing is finding the current distance from the center of the light and then dividing the intensity by that distance squared. After it finds the intensity it just looks at the current pixel increases it's rgb components by that intensity and then moves on. The light looks nice except that the transition from super bright to "average light intensity" is too abrubt. Any suggestions?

Share on other sites
cheat and use something other than the squared distance (i don't know if just linear attenuation will look good, it probably depends on the specifics of your game).

Share on other sites
Go shine a flashlight at the wall.

Share on other sites
oh, i should project the light onto a 2d surface and not just draw it 2d?

Share on other sites
I can see a few issues with your current implementation.

First off, distance from one point to another is the square root of the sum of the squares, not just the sum of the squares.

Second, dividing the intensity by the distance is still going to ensure that the center is excessively bright and it will dim out fairly quickly.

For example, you said that you need your light set to 500,000 to even "show up." So let's walk through this...

500,000 / 1 = 500,000        / 2 = 250,000        / 3 = 166,666...        / 4 = 125,000
As you can see, the closer you are to the center, your light is going to be exponentially brighter.

What several people do for lighting calculations like this is have an inner cone and an outer cone.

The calculation for the inner cone is simply comparing the sum of the squares to the square of the inner cone radius (to save the square root). If the sum of the squares is less, you return full intensity.

Then you test if the sum of the squares is greater than the square of the outer cone radius. If it is, then you return 0 intensity.

Finally, you do a linear interpretation between the inner cone radius and the outer cone radius based off of the true distance from the center point to return the falloff intensity.

Hope that helps.

1. 1
Rutin
49
2. 2
3. 3
4. 4
5. 5

• 11
• 16
• 9
• 10
• 13
• Forum Statistics

• Total Topics
633004
• Total Posts
3009851
• Who's Online (See full list)

There are no registered users currently online

×