Jump to content
  • Advertisement
Sign in to follow this  
DrZoidberg

2D Dynamic Lighting

This topic is 4773 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 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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Go shine a flashlight at the wall.

Share this post


Link to post
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.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!