Main Menu

Programming in 3D

Started by lancel00t, March 09, 2008, 09:18:11 AM

Previous topic - Next topic

lancel00t

I now have some of the basics of PlayBasic under my command and I am progressing quickly on a small 2D board game that I had previouslly been working on in C++. The 2D board game features some 3D elements by design and I am having some issues figuring out how to begin 3D programming with PlayBasic. So far I understand that you can use ColdSteel or the PlayBasic builtin functions but not both of them together. The hard part is that after more than a cursory examination of both the ColdSteel and PlayBasic websites I really don't see any tutorials, faqs, or introductory examples etc. on how to begin 3D programming with PlayBasic. If anyone could lend me a hand that would be great, thank you.

kevin

#1
  
Quoteintroductory examples

  There's bunch of examples on the forums.  Here's a few from the Source Code forum.

 City Scape (login required)  -   X file Loader (login required)  -   Perspective Platformer (login required)  -     3D terrain (login required)

  plus there's a bunch of tidbits post in the WIP thread    Here (login required) - In Particular take notice of the entity sprite features.  This is how 3d geometry is made.




To make a 3d scene you need 4 things



1) Set up the appropriate the 3D Display mode

         - To render 3d scenes the display needs a Z buffer attached to it.   The Z buffer is a screen sized array of pixel depths.  When the video hardware draws a pixel to the screen (from a triangle), pixels are only plotted when they're in front of existing pixel (if any).   So pixels closer to the viewer are drawn in front of others.   Which means 3D scenes can have polygons that intersect each other at pixel level.  

         - To reset the Zbuffer after rendering.  We must clear the screen,   CLS will not only clear the pixels from the screen buffer, but it also reset the Z buffer depths to their far point.



2) Some geometry to look at  (a entity sprites)

          - All 3D geometry is made up of  vertices and face definitions.   Vertex are points and the face definitions are the triangles/quads. These simply connect verts together to form a 'face' - a polygon in other words.    

           To create a object that contains a quad polygon.   We need 4 vertex (the corner points) and 1 face.  

           
                So to make a quad that is 100x * 200Y and centered

       ; Set the sprite to have space for 4 unique vertex
       SpriteVertexQuantity thisSprite,4

              ; set the verts
         PokeSpriteVertex  Thissprite,0, -50,-100,0  
         PokeSpriteVertex  Thissprite,1, 50,-100,0
         PokeSpriteVertex  Thissprite,2, 50,100,0
         PokeSpriteVertex  Thissprite,3, -50,100,0
                This defines the the 4 points.


             ; Set the number of faces
       SpriteFaceQuantity thisSprite,1

           ; Define this face zero to be a polygon made up from attached 4 vertex.

         ThisFace=0        
         PokeSpriteFaceVerts ThisSprite,ThisFace,4

            ; Define the edges around the face.
          ThisColour=Rgb(255,255,255)   ;  RGB colour at the face edges

           ; Coordinates on the texture in scalers
          u1#=0  
          v1#=0
          u2#=1
          v2#=1


        ; Set this face to use Vertex 0 (the top left coord of our rect)
     PokeSpriteface ThisSprite,ThisFace,0,0 ,u1#,v1#,ThisColour

        ; Set this face to use Vertex 1 (the top right coord of our rect)
     PokeSpriteface ThisSprite,ThisFace,1,1 ,u2#,v1#,ThisColour

        ; Set this face to use Vertex 2 (the bottom right coord of our rect)
     PokeSpriteface ThisSprite,ThisFace,2,2 ,u2#,v2#,ThisColour

        ; Set this face to use Vertex 2 (the bottom left coord of our rect)
     PokeSpriteface ThisSprite,ThisFace,3,3 ,u1#,v2#,ThisColour

             Done
           


           Note:- Faces are single sided in 3D and therefore must be defined in clock wise order.  In other words for the face to be visible to the viewer the face must connect to the verts in order.  Other wise it'll be pointing away from the camera.




3) Setup a camera to view 3d Space from

    Define and populate the camera (viewer) structure.


;  Dim PB Camera type
 Dim Camera as PBCamera Pointer
 Camera = new PBCamera

; Camera Position  (back 1000 untis from the worlds orgin (0,0,0)
Camera.pos.X=0.0  
Camera.pos.y=0.0  
Camera.pos.z=-1000.0


; RotationMode 0= 2d With Perspective down Z axis (z rotation supported Camera.Angle.Z)
; RotationMode 1= 3D ZYX rotation
Camera.RotationMode=1


// Set Camera Projection  
Camera.Projection.x=((GetScreenWidth()/2.0)*1000)/(400)
Camera.Projection.y=((GetScreenHeight()/2.0)*1000)/(400)

// Middle of Camera
Camera.ViewPoint.X=GetScreenWidth()/2.0  
Camera.ViewPoint.y=GetScreenHeight()/2.0  








4) Rendering  

        - Once you have some geometry and a camera to represent the viewer in 3d space.  You call the DrawPerspectiveSprites function.  

 
; DRaw the Sprite List from the Camera Viewer Perspectively
DrawPerspectiveSprites Camera






Commands



Undocumented Commands In PlayBasic V1.7x (login required)