Slight Lens Flare Problem ???

Blitz3D Forums/Blitz3D Programming/Slight Lens Flare Problem ???

Rogue Vector(Posted 2003) [#1]
Hi,

I have a lens flare system the works fine when the camera moves and the light source is fixed.

However, it doesn't work in reverse.

That is, when the camera is static and the light source orbits around it.

Does anyone know how to code around this?

Cheers,

RV


Odds On(Posted 2003) [#2]
you need to be more specific... otherwise I have to make a lot of assumptions. I assume you're using the lens flare that comes with one of the Blitz demos, I assume you're probably moving the wrong thing... if I remember correctly there's a sun flare and a sun pivot, you have to move the pivot.

If you post an example (code that will compile to show the problem) I could be a lot more specific.


Rogue Vector(Posted 2003) [#3]
Okay, here's my code:

;Load in Flare images
Function LoadLenses()

;Load 2D lens flare images
LensFOrig1 = LoadSprite( "Resources\Sprites\lens1.jpg" )
If LensFOrig1 = 0 Then RuntimeError "file 'lens1.jpg' does not exist!"
HideEntity LensFOrig1 ;hide as these entitys only used for CopyEntity()

LensFOrig2 = LoadSprite( "Resources\Sprites\lens2.jpg" )
If LensFOrig2 = 0 Then RuntimeError "file 'lens2.jpg' does not exist!"
HideEntity LensFOrig2

LensFOrig3 = LoadSprite( "Resources\Sprites\lens3.jpg" )
If LensFOrig3 = 0 Then RuntimeError "file 'lens3.jpg' does not exist!"
HideEntity LensFOrig3

LensFOrig4 = LoadSprite( "Resources\Sprites\lens4.jpg" )
If LensFOrig4 = 0 Then RuntimeError "file 'lens4.jpg' does not exist!"
HideEntity LensFOrig4

End Function


;Create the Lens Flares
Function CreateLensFlares()

LensFlare1 = CopyEntity( LensFOrig1, hud_overlay ) ;primary flare
ScaleSprite LensFlare1, .35, .35 ;scale according to screen size and size of sprite picture
EntityAlpha LensFlare1, .3 ;set alpha blend amount
EntityOrder LensFlare1, -2 ;place in front of other entitys
HideEntity LensFlare1 ;hide initially

LensFlare2 = CopyEntity( LensFOrig3, hud_overlay ) ;first halo
ScaleSprite LensFlare2, .25, .25 ;scale is 1/2 of lens flare #1
EntityAlpha LensFlare2, .3
EntityOrder LensFlare2, -3 ;place in front of previous lens flare
HideEntity LensFlare2

LensFlare3 = CopyEntity( LensFOrig3, hud_overlay ) ;small burst
ScaleSprite LensFlare3, .125, .125 ;scale is 1/4 of lens flare #1
EntityAlpha LensFlare3, .2
EntityOrder LensFlare3, -4 ;place in front of previous lens flare
HideEntity LensFlare3

LensFlare4 = CopyEntity( LensFOrig2, hud_overlay ) ;next halo
ScaleSprite LensFlare4, .27, .27 ;same scale as lens flare #1
EntityAlpha LensFlare4, .2
EntityOrder LensFlare4, -5 ;place in front of previous lens flare
HideEntity LensFlare4

LensFlare5 = CopyEntity( LensFOrig4, hud_overlay ) ;next burst
ScaleSprite LensFlare5, .25, .25 ;scale is 1/2 of lens flare #1
EntityAlpha LensFlare5, .15
EntityOrder LensFlare5, -6 ;place in front of previous lens flare
HideEntity LensFlare5

LensFlare6 = CopyEntity( LensFOrig2, hud_overlay ) ;next halo
ScaleSprite LensFlare6, .125, .125 ;scale is 1/4 of lens flare #1
EntityAlpha LensFlare6, .2
EntityOrder LensFlare6, -7 ;place in front of previous lens flare
HideEntity LensFlare6

LensFlare7 = CopyEntity( LensFOrig3, hud_overlay ) ;next burst
ScaleSprite LensFlare7, .125, .125 ;scale is 1/4 of lens flare #1
EntityAlpha LensFlare7, .2
EntityOrder LensFlare7, -8 ;place in front of previous lens flare
HideEntity LensFlare7

LensFVisible% = False ;lens flares are hidden by default

FreeEntity LensFOrig1
FreeEntity LensFOrig2
FreeEntity LensFOrig3
FreeEntity LensFOrig4

End Function


Function UpdateLensFlares(sun, cam, d_height, d_width)

If LensFlareON=False

HideEntity LensFlare1
HideEntity LensFlare2
HideEntity LensFlare3
HideEntity LensFlare4
HideEntity LensFlare5
HideEntity LensFlare6
HideEntity LensFlare7

Else

;-- is the sun in view? and make sure it's not blocked by other blocking entities
If EntityInView( sun, cam ) = True And EntityVisible( cam, sun ) = True

;determine sun visible x/y coords using camera 3d to 2d conversion
CameraProject cam, EntityX(sun,True), EntityY(sun,True), EntityZ(sun,True)
lx = ProjectedX() ;position on 2d screen
ly = ProjectedY()

;determine center of screen
cx = d_width / 2
cy = d_height / 2

;determine vector from center to sun's x/y screen position
vx = cx - lx
vy = cy - ly
If vx = 0 And vy = 0
distance# = 1.0 ;to fix sqr(0) fail
Else
distance# = Sqr( (vx*vx)+(vy*vy) ) ;determine length of vector (pythagoras)
EndIf

;normalise vector (determine 1 unit size in x and y coordinates)
nx# = vx / distance#
ny# = vy / distance#

;== calculate lens flare positions

;calculate lens flare 1 position (maximum distance)
f1x = cx - (nx# * distance)
f1y = cy - (ny# * distance)

;calculate lens flare 2 position (half distance)
f2x = cx - (nx# * (distance/2)) ;could optimize but leaving as divide for readability
f2y = cy - (ny# * (distance/2))

;calculate lens flare 3 position (one third of distance)
f3x = cx - (nx# * (distance/3))
f3y = cy - (ny# * (distance/3))

;calculate lens flare 4 position (one eight distance)
f4x = cx - (nx# * (distance/8))
f4y = cy - (ny# * (distance/8))

;calculate lens flare 5 position
f5x = cx - (nx# * (-distance/2))
f5y = cy - (ny# * (-distance/2))

;calculate lens flare 6 position
f6x = cx - (nx# * (-distance/4))
f6y = cy - (ny# * (-distance/4))

;calculate lens flare 7 position
f7x = cx - (nx# * (-distance/5.5))
f7y = cy - (ny# * (-distance/5.5))


;=== position lens flares entities (relative to camera hud fixed pivot)

PositionEntity LensFlare1, f1x, f1y, 1
PositionEntity LensFlare2, f2x, f2y, 1
PositionEntity LensFlare3, f3x, f3y, 1
PositionEntity LensFlare4, f4x, f4y, 1
PositionEntity LensFlare5, f5x, f5y, 1
PositionEntity LensFlare6, f6x, f6y, 1
PositionEntity LensFlare7, f7x, f7y, 1

;make flares visible? (if not already visible)
If LensFVisible% = False
ShowEntity LensFlare1
ShowEntity LensFlare2
ShowEntity LensFlare3
ShowEntity LensFlare4
ShowEntity LensFlare5
ShowEntity LensFlare6
ShowEntity LensFlare7

LensFVisible% = True
EndIf


;sun not visible from camera
Else
;lens flares previously visible? then hide them
If LensFVisible% = True
HideEntity LensFlare1
HideEntity LensFlare2
HideEntity LensFlare3
HideEntity LensFlare4
HideEntity LensFlare5
HideEntity LensFlare6
HideEntity LensFlare7

LensFVisible% = False
EndIf
EndIf
EndIf

End Function


;Example Usage ------------------------------------------

;Sun = CreatePivot()
;PositionEntity Sun, -199.0, 1000.0, -220.0
;LoadLenses()
;CreateLensFlares()
;EntityParent Sun, SkyBox


;Main Loop
;Repeat
;.
;.
;TurnEntity SkyBox, 0, -4*g_DeltaT, 0
;UpdateLensFlares(Sun, g_cam, g_scrheight, g_scrwidth)
;.
;.
;UpdateWorld()
;Renderworld()
;Flip
;Until KeyHit(1)=True
;-------------------------------------------------------


Cheers,

RV