Results 1 to 5 of 5

Thread: DirectDraw 7 - Lighting

  1. #1

    Thread Starter
    Member
    Join Date
    May 2001
    Location
    New Zealand
    Posts
    45

    DirectDraw 7 - Lighting

    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!

  2. #2
    Zaei
    Guest
    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:
    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
    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:
    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
    Z.

  3. #3
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    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:
    1. Dim Temp1 As Long, Temp2 As Long
    2.  
    3. LightX = 150
    4. LightY = 150
    5. LightRadius = 50
    6.  
    7. For y = LightX-LightRadius To LightX+LightRadius Step 3
    8.   For x = LightY-LightRadius To LightYX+LightRadius
    9.     Temp2 = Sqr((LightX-x)*(LightX-x)+(LightY-y)*(LightY-y))
    10.     Temp2 = LightRadius - Temp2
    11.  
    12.     Temp1 = ArrayPic(x,y) + Temp2
    13.     If Temp1 > 255 Then Temp1 = 255
    14.     ArrayPic(x,y) = Temp1
    15.  
    16.     Temp1 = ArrayPic(x+1,y) + Temp2
    17.     If Temp1 > 255 Then Temp1 = 255
    18.     ArrayPic(x+1,y) = Temp1
    19.  
    20.  
    21.     Temp1 = ArrayPic(x+21,y) + Temp2
    22.     If Temp1 > 255 Then Temp1 = 255
    23.     ArrayPic(x+2,y) = Temp1
    24.  
    25.   Next x
    26. Next y

    Ok, ArrayPic() is the array that has the image, the variables are pretty much self-exaplnatory

    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
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  4. #4

    Thread Starter
    Member
    Join Date
    May 2001
    Location
    New Zealand
    Posts
    45

    Thanks!

    I got the code to work in my RPG - yay!!! It now has lighting at nitetime!!

  5. #5
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Thank you, I never tested that technique
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width