Can someone tell me some code to make a light, which will brighten the sorrounding pixels in a 20 pixel radius in DirectDraw \ VB6??? Thanks!
Printable View
Can someone tell me some code to make a light, which will brighten the sorrounding pixels in a 20 pixel radius in DirectDraw \ VB6??? Thanks!
Dont know how to actually implement it in DDraw, but using a 2D Distance function, get the distance from the center to the pixels to be lit, then, determine current RGB values of the pixel, then, using a linear interpolation function, increase the color by an amount determined by the distance from the center.
[code]
XXXXXXXXXX
XXXX1XXXXX
XXX121XXXX
XX12321XXX
X1234321XX
1234C4321X
X1234321XX
XX12321XXX
XXX121XXXX
XXXX1XXXXX
[code]
So, you have values from 1 to 4, Passed to an interpolation function:
You will have to figure out how to get the R, G, and B components of the color, but that is the rest of it. Pass a value from 0.0 to 1.0 as s, get it from the distance by doing "Dist / Total Radius":Code:Function ColorInt(Col1 as RGBTRIPLE, Col2 as RGBTRIPLE, s as single) as long
Dim r as integer
Dim g as integer
Dim b as integer
r = Col1.rgbRed + s * (Col2.rgbRed - Col1.rgbRed)
//Green
//Blue
ColorInt = RGB(r, g, b)
end function
Z.Code:X X X X X X X X X X
X X X X [1/4] X X X X X
X X X 1 2 1 X X X X
X X 1 2 3 2 1 X X X
X 1 2 3 4 3 2 1 X X
1 2 3 4 C 4 [3/4] 2 1 X
X 1 2 3 [4/4] 3 2 1 X X
X X 1 2 3 2 1 X X X
X X X 1 2 1 X X X X
X X X X 1 X X X X X
Look, it's not that hard, you just use GetLockedArray() to get the pixels. Look in this thread:
http://forums.vb-world.net/showthrea...&postid=403348
As to drawing the actual lights, well, it is a bit harder. A very simple way to do it is like this:
VB Code:
Dim Temp1 As Long, Temp2 As Long LightX = 150 LightY = 150 LightRadius = 50 For y = LightX-LightRadius To LightX+LightRadius Step 3 For x = LightY-LightRadius To LightYX+LightRadius Temp2 = Sqr((LightX-x)*(LightX-x)+(LightY-y)*(LightY-y)) Temp2 = LightRadius - Temp2 Temp1 = ArrayPic(x,y) + Temp2 If Temp1 > 255 Then Temp1 = 255 ArrayPic(x,y) = Temp1 Temp1 = ArrayPic(x+1,y) + Temp2 If Temp1 > 255 Then Temp1 = 255 ArrayPic(x+1,y) = Temp1 Temp1 = ArrayPic(x+21,y) + Temp2 If Temp1 > 255 Then Temp1 = 255 ArrayPic(x+2,y) = Temp1 Next x Next y
Ok, ArrayPic() is the array that has the image, the variables are pretty much self-exaplnatory :p
It basically gets the value of that pixel, gets the distance between the pixel and the light, inverts this value so it will be smaller the further away it is from the light, adds this value to each of the color's 3 RGB values (lights add up colors, it's that easy - no alpha blending :) ), and makes sure it's not above 255 so you don't get overflow errors.
If you never messed with RGB values it might seem a bit confusing, in that case read a couple of good tutorials if you wanna understand how it works :p
I got the code to work in my RPG - yay!!! It now has lighting at nitetime!!
Thank you, I never tested that technique ;)