UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on December 02, 2009, 03:14:57 PM

Title: Manual Projection/Scene Perspective
Post by: kevin on December 02, 2009, 03:14:57 PM
 Manual Projection/Scene Perspective

   This is quick mod of the one of the demos from the PB example pack.  The original version zooms the scene in /out,  this version sets up a test scene for scrolling Left/right...  The scene has perspective, so foreground objects appear larger than background objects.
 


[pbcode]

Constant ProjectionX = 400
Constant ProjectionY = 300

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

Dim Objs(150) As tBox



XRange=20000
Width=500
Height=1000
gap =400



   BlockRow(Xrange,Width,Height,1000,gap)

   BlockRow(Xrange,Width,Height,750,gap)

   BlockRow(Xrange,Width,Height,500,gap)

   BlockRow(Xrange,Width,Height,250,gap)




CamX=00
CamY=00
CamZ=-500

CreateCamera 1

Do

  ; redirect all drawing To PB scene buffer
      CaptureToScene   
   
  ; Clear the scene buffer
       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
   
          ; 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,Objs(lp).Col

         EndIf
         
      Next
      
          

  ; Move the camera   
    Speed=3
 

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

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



   

   DrawCamera 1
      text 0,0,fps()

   Sync
Loop



Function BlockRow(Xrange,Width,Height,Depth,Gap)

for lp=-Xrange to Xrange step Width+Gap
   index=getfreecell(Objs())
   objs(index)=new tbox
   
  Objs(index).X = lp
  Objs(index).y = 0
  Objs(index).z = depth

  Objs(index).width = width
  Objs(index).height = height
  Objs(index).Col = RndRGB()   


next



EndFunction

[/pbcode]


Related Examples

 Z Depth Perspective For Sprites (http://www.underwaredesign.com/forums/index.php?topic=3528.0)
 2.5 Rugby (http://www.underwaredesign.com/forums/index.php?topic=2980.0)
 Mip Mapped Image Zoomer (http://www.underwaredesign.com/forums/index.php?topic=3617.0)