• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Lighting effects

## Recommended Posts

I''m currently woring on a 2d RPG and due to the language I workwith at my school, I have no support for doing any lighting opperations. I''ve been looking around to see if I could find any algithorims that would show me how do making lights. I had to make up my own in the mean time. If anyone knows any links or can comment on how to optomise my code it would be greatly appreciated.

//RGB light colour value

real r1,g1,b1
//objects RGB value

real r2,g2,b2
// new RGB colour

//distance from light source

real distance
//max light range

int maxDistance
//light intensity

real power
//pixels x,y location

int x,y
//lights x,y location

int lightX,lightY

//distance from pixel to light position

distance=sqrt(((lightX-x)**2)+((lightY-y)**2))
//intensity of light between 0-1

power = 1-(distance/maxDistance)

//get the RGB value of the destination pixel

GetColour(xLocation,yLocation,r,g,b)
//make new colour by finding the diff. in values

//taking a percentage of it and adding to the

//original

r3=((r1-r2)*power)+r2
g3=((g1-g2)*power)+g2
b3=((b1-b2)*power)+b2

drawdot(xLocation,yLocation,r3,g3,b3)

This is is incredably slow( 2-3 frames with one light). If anyone has an idea that would be great

##### Share on other sites
First of all, what does your code look like without the lighting?

I''m suspecting that the main bottleneck lies with GetColour/DrawDot. Consider using an array for your sprite and for your target and copying the target to the screen every frame. (the implementation will depend on your API)
I''m assuming the Getcolour line should use r2,g2,b2, right?

Secondly, keep your types straight - either do it all in floating point variables (real), or use all integers.

Another good idea is taking out the sqrt() and also using maxdistance squared. Apart from being faster, it''ll also look better.

Depending on how good the compiler is, consider unrolling the (lightx-x)2 to (lightx-x)*(lightx-x).

- hillip@xenoage.de''>JQ
Full Speed Games. Period.

##### Share on other sites
Thanks for the tips there Jonny. That sqrt was killing it. I''ve got it work quite a bit faster now (from 2 FPS to 60) with absolutly no changes in the actual way I was implementing it. I''m having a bit of a problem now, where the light is too strong even at the edges. I think it might be a simple error(the type that are really hard to see) so I have to work on that. But thanks, you helped a lot.

• ### Forum Statistics

• Total Topics
628288
• Total Posts
2981845

• 11
• 10
• 10
• 11
• 17