Author Topic: 2d directional lighting  (Read 1131 times)

0 Members and 1 Guest are viewing this topic.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1666
  • Karma: 133
    • View Profile
2d directional lighting
« on: March 23, 2013 »
hello guys im trying to create directional lighting atm. just too run over a flat image. the final effect im after is a strong spot light that i can swing round the image with sin and cos illuminating different parts.. as seen in lots of old school stuff.

the way im thinking right now and i may be way off here but here goes

im trying to create a light map the same size as the image along the lines of
for y = 0 to imageheight-1
for x = 0 to imagewidth-1
lightmap(x,y) = sqr( ( ( x - LightXpos / 2 ) ^ 2 ) + ( (  Y - LightYpos / 2 ) ^ 2 )  )

which gives me a map with smooth interpolated values from the central point Of LightX, LightY outwords then i fudge my lightmap in with my image rgb values. its almost working i just need too scale the values properly.

now my question is, how do you guys achieve this effect ive looked all night for tutorials and stuff but all i get is posts for unity and opengl which is no good. i just want to make sure the way i have it down in my mind sounds about right.

cheers for any help!
« Last Edit: March 23, 2013 by ninogenio »
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1666
  • Karma: 133
    • View Profile
Re: 2d directional lighting
« Reply #1 on: March 23, 2013 »
i threw this togher in five too show you guys roughly how im doing it. the benefit of how im doing it here is that if you put a solid object in the light map ie a pole it will cause the light too split up...

also too do it this way i would probably use a reduced res light map maybe 128*128 or something then scale it up for speed.

let me know if you guys think there are other ways of doing it cheers.


the code is seriously unoptimised it can run much much faster.

Code: [Select]
#Include "Tinyptc_ext.Bi"
#Include "Windows.Bi"
Option Explicit

Const XRes = 640
Const YRes = 480
Dim Shared As Double WndOrgX = XRes/2
Dim Shared As Double WndOrgY = YRes/2

RANDOMIZE TIMER

Type TimerType
   
    Frequency As LARGE_INTEGER
    LiStart As LARGE_INTEGER
    LiStop As LARGE_INTEGER
    LlTimeDiff As LONGLONG
    MDuration As Double

End Type

Declare Sub         MyLine( byval x1 as integer, byval y1 as integer, byval x2 as integer, byval y2 as integer, ByVal Col As Integer )
Declare Sub         PtcOpen()
Declare Sub         StartTimer( TempTimer As TimerType Ptr )
Declare Sub         DestroyTimer( TempTimer As TimerType Ptr )
Declare Function    NewTimer() As TimerType Ptr
Declare Function    GetTimerMs( TempTimer As TimerType Ptr ) As Double
Declare Function    GetTimerSec( TempTimer As TimerType Ptr ) As Double

Dim Shared As Double Col1 = 0,Col2 = 0, Col3 = 0, mx = 0, my = 0
Dim Shared As Double TimerVal
Dim Shared As Double angle
Dim Shared Zoom As Integer
Dim Shared As Integer Buffer( XRes * YRes )
Dim Shared As Integer LightMap( Xres * Yres )
Dim Shared As TimerType Ptr FrameTimer, DemoTimer
Dim Shared As Integer LightMapX, LightMapY
FrameTimer = NewTimer()
DemoTimer = NewTimer()

PtcOpen()
While ( GetAsyncKeyState( VK_ESCAPE ) <> -32767 )
   
    Dim As Integer X,Y
    Dim As Integer PreXorCol
    StartTimer( FrameTimer )
   
    For x = 0 To Xres - 1
        For y = 0 To Yres - 1
           
           
        Next
    Next
   
    Dim As Integer PreX,PreY,Pre1Y,Pre1X
    Dim As Integer ArrayIndex
    PreX = LightMapX / 2
   
    For y=0 To Yres - 1
       
        PreY = ( ( y - LightMapY / 2 ) ^ 2 )
        Pre1Y = ( ( Y / 12 ) And 1 )
        For x=0 To Xres - 1
            PreXorCol = 255*( ( ( X / 12 ) And 1 )  Xor Pre1Y )
            Col1 = PreXorCol
            Col2 = PreXorCol
            Col3 = PreXorCol
           
            ArrayIndex = x + (y*Xres-1)
            Lightmap( ArrayIndex ) =255-Sqr( ( ( x - PreX ) ^ 2 ) + PreY )/1.2
       
            Col1 += LightMap( ArrayIndex )
           
            if Col1 > 255 Then Col1=255
            if Col1 < 0 Then Col1 = 0

            Col2 = Col1
            Col3 = Col1
            Buffer( ArrayIndex ) = Rgb(Col1,Col2,Col3)
           
        Next
    Next
   
    TimerVal = GetTimerMs( DemoTimer )/4
    LightMapX = (Yres)+Sin( TimerVal/320*3.14 ) * 450
    LightMapY = (Xres)+Cos( TimerVal/320*3.14 ) * 450
   
    Ptc_Update( @Buffer(0) )
    do
    Loop While (  GetTimerMs( FrameTimer ) <= 1000.0/60.0 )'60fps Clamp
   
Wend
DestroyTimer( FrameTimer )
DestroyTimer( DemoTimer )


Sub PtcOpen()
   
    Ptc_AllowClose(0)
    Ptc_SetDialog(1,"NinosFirstTunnel"+CHR$(13)+"FullScreen",0)
    Ptc_SetFlip(1)
    If ( Ptc_Open( "Tunnel", XRes, YRes ) = 0 ) Then
        End - 1
    End If
   
End Sub


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'infamous line algorythm found years ago some where in google world'
'ported From c                                                     '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub MyLine( byval x1 as integer, byval y1 as integer, byval x2 as integer, byval y2 as integer, ByVal Col As Integer )
   
    dim i, deltax, deltay, numpixels as integer
    dim d, dinc1, dinc2 as integer
    dim x, xinc1, xinc2 as integer
    dim y, yinc1, yinc2 as integer
   
    'If X1 < 1 Then X1 = 1
    'If X1 > Xres Then X1 = Xres-1
    'If X2 < 1 Then X2 = 1
    'If X2 > Xres Then X2 = Xres-1
   
    'If Y1 < 1 Then Y1 = 1
    'If Y1 > Yres Then Y1 = Yres-1
    'If Y2 < 1 Then Y2 = 1
    'If Y2 > Yres Then Y2 = Yres-1
   
    'calculate deltaX and deltaY
    deltax = abs(Cast(integer,x2 - x1))
    deltay = abs(Cast(integer,y2 - y1))
         
    'initialize
    if ( deltax >= deltay ) then
        'If x is independent variable
        numpixels = deltax + 1
        d = ( 2 * deltay ) - deltax
        dinc1 = deltay shl 1
        dinc2 = ( deltay - deltax ) shl 1
        xinc1 = 1
        xinc2 = 1
        yinc1 = 0
        yinc2 = 1
    else
        'if y is independent variable
        numpixels = deltay + 1
        d = ( 2 * deltax ) - deltay
        dinc1 = deltax shl 1
        dinc2 = ( deltax - deltay ) shl 1
        xinc1 = 0
        xinc2 = 1
        yinc1 = 1
        yinc2 = 1
    endif
         
    'move the right direction
    if ( int(x1) > int(x2) ) then
        xinc1 = -xinc1
        xinc2 = -xinc2
    endif
         
    if ( int(y1) > int(y2) ) then
        yinc1 = -yinc1
        yinc2 = -yinc2
    endif
         
    X = Cast(Integer,X1)
    Y = Cast(Integer,Y1)
         
    'draw the pixels
    For I = 1 To NumPixels
        If Y > 2 And Y < Yres - 2 Then
            If X > 2  and X < Xres - 2 Then
                Buffer( Y*Xres+X ) = Col
            End If
        End If
       
        If ( d < 0 ) Then
            d = d + dinc1
            x = x + xinc1
            y = y + yinc1
        Else
            d = d + dinc2
            x = x + xinc2
            y = y + yinc2
        EndIf
    Next
         
End Sub



Function NewTimer() As TimerType Ptr
   
    Dim As TimerType Ptr TempTimer
   
    TempTimer = CAllocate( SizeOf( TimerType ) )
    QueryPerformanceFrequency( @TempTimer->Frequency )
    NewTimer = TempTimer
   
End Function



Sub StartTimer( TempTimer As TimerType Ptr )
   
    QueryPerformanceCounter( @TempTimer->LiStart )
   
End Sub



Function GetTimerMs( TempTimer As TimerType Ptr ) As Double
   
    QueryPerformanceCounter( @TempTimer->LiStop )
    TempTimer->LlTimeDiff = TempTimer->LiStop.QuadPart - TempTimer->LiStart.QuadPart
    TempTimer->MDuration = Cast( Double, TempTimer->LlTimeDiff ) * 1000.0 / Cast( Double , TempTimer->Frequency.QuadPart )
    GetTimerMs = TempTimer->MDuration
   
End Function



Function GetTimerSec( TempTimer As TimerType Ptr ) As Double
   
    QueryPerformanceCounter( @TempTimer->LiStop )
    TempTimer->LlTimeDiff = TempTimer->LiStop.QuadPart - TempTimer->LiStart.QuadPart
    TempTimer->MDuration = Cast( Double, TempTimer->LlTimeDiff ) * 1000.0 / Cast( Double , TempTimer->Frequency.QuadPart )
    GetTimerSec = TempTimer->MDuration/1000.0
   
End Function



Sub DestroyTimer( TempTimer As TimerType Ptr )
   
    If ( TempTimer ) Then
        DeAllocate( TempTimer )
    EndIf
   
End Sub
« Last Edit: March 23, 2013 by ninogenio »
Challenge Trophies Won: