News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Depth Cued Perspective Boxes (RgbDepthCue)

Started by kevin, May 26, 2009, 09:30:56 PM

Previous topic - Next topic

kevin

 Depth Cued Perspective Boxes (RgbDepthCue)

  This example is a variation of one of PB demos,  it simply draws a bunch of boxes perspectively.  The only difference in thids version is that boxes are now depth cued (fogged if you like) into the backdrop colour using the RgbDepthCue command.  



PlayBASIC Code: [Select]
;---------------------------------------------------------------------
; 3D perspective Example By Kevin Picone 28,Sep,2004 - Updated 27,May,2009
;---------------------------------------------------------------------
; This example shows basic projection. Use the ARROW and A & Z
; keys to control camera within the simple 3D space
;---------------------------------------------------------------------

BackDropColour=rndrgb()


Constant ProjectionX = 400
Constant ProjectionY = 300

Type tBox
X,y,Z,Width#,Height#,col
EndType

Dim Objs(150) As tBox

; create some boxes in World Space

r=500
For lp=0 To GetArrayElements(objs().tbox,1)
Objs(lp).X = RndRange(-r,r)
Objs(lp).y = RndRange(-r,r)
Objs(lp).z = RndRange(100,r*2)
Objs(lp).width = Rnd(150)
Objs(lp).height = Rnd(150)
Objs(lp).Col = RndRGB()
Next


; Clip the Camera
Xrange=500
YRange=500
ZRange=1000


CamX=00
CamY=00
CamZ=-Zrange

CreateCamera 1

Do


if ChangeBackDropTimer<Timer()

ChangeBackDropTimer=timer()+2000
BackDropColour=rndrgb()
cameraClsColour 1,BackDropColour

endif


; redirect all drawing To PB scene buffer
CaptureToScene

; Clear the scene buffer (so there's no old data in it)
ClsScene


; Calc the center of the Camera
CameraCenterX#=(GetCameraWidth(1)/2)
CameraCenterY#=(GetCameraHeight(1)/2)


; Transform And render Boxes (with Z To the screen)
For lp=0 To GetArrayElements(objs().tbox,1)

; Translate the Obejcts Z To the cameras Z
Z#=Objs(lp).Z-CamZ

; Check if the Object is in front of the camera (positive)
If Z#>0


; Depth Cue the Colour
Col=RgbDepthCue(Objs(lp).Col,BackDropColour,z#,0,1000)
if Col



; Translate the X# / Y# cooords object To the camera
X#=Objs(lp).X-CamX
Y#=Objs(lp).Y-CamY

; Calc projected X /Y coords
PX#=CameraCenterX#+((x#*ProjectionX)/Z#)
PY#=CameraCenterY#+((y#*ProjectionY)/Z#)

; Calc perspective sizing
Width#=(Objs(lp).Width*ProjectionX)/Z#
Height#=(Objs(lp).Height*ProjectionY)/Z#


; Set PB To capture the Next drawing operations at this Z# depth in the
; scene buffer
CaptureDepth Z#


; Draw the object To the scene buffer
x2#=px#-(width#/2)
Y2#=pY#-(Height#/2)

BoxC X2#,y2#,x2#+width#,y2#+height#,1,Col
endif
EndIf

Next



; Move the camera
Speed=3

If ScanCode()=44 Then Camz=Camz+-Speed
If ScanCode()=30 Then Camz=Camz+Speed

If UpKey() Then Camy=Camy+-Speed
If DownKey() Then Camy=Camy+Speed

If LeftKey() Then CamX=CamX+-Speed
If RightKey() Then CamX=CamX+speed


If CamX>Xrange Then CamX=Xrange
If CamX<-Xrange Then CamX=-Xrange
If CamY>YRange Then CamY=YRange
If CamY<-YRange Then CamY=-YRange
If CamZ>ZRange Then CamZ=ZRange
If CamZ<-ZRange Then CamZ=-ZRange

; Auto Move Camera in/out

If CamDirFlag=0
Camz=CamZ+Speed
If camz>Zrange Then CamDirflag=Not CamDirFlag
Else
Camz=Camz+-speed
If camz<-Zrange Then CamDirflag=Not CamDirFlag
EndIf



DrawCamera 1
text 0,0,fps()
Login required to view complete source code