[PB4.3] Ogre example

Share your advanced PureBasic knowledge/code with the community.
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

[PB4.3] Ogre example

Post by djes »

Here's a new Ogre 3d example for the PB team, showing a simple material loading. :)

Full archive is here, containing necessary files : http://djes.free.fr/purebasic/bouncing_ball_example.zip

Code: Select all

;
; ------------------------------------------------------------
;
;   Bouncing ball example
;
;    (c) 2008 - djes
;
; ------------------------------------------------------------
;

IncludeFile "Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY

Enumeration
  #MESH_BALL
EndEnumeration

Enumeration 0 Step 10
  #ENTITY_BALL
EndEnumeration

Structure ball
	x.f
	y.f
	z.f
	sprite_nb.i
	speedx.f
	speedy.f
	speedz.f
	rot.f
	transp.i
EndStructure

Global Dim balls.ball(100)

If InitEngine3D()

  Add3DArchive("data\", #PB_3DArchive_FileSystem  )
  
  InitSprite()
  InitKeyboard()
  InitMouse()

  OpenConsole()

  If Screen3DRequester()
    
    refresh_rate.l  =  85
    SetRefreshRate(refresh_rate)
    SetFrameRate(refresh_rate)

    ;--- Load our mesh and creates 10 entities

    LoadMesh   (#MESH_BALL , "bouncing_ball.mesh")
    Parse3DScripts()

    For u=0 To 9

      CreateEntity(#ENTITY_BALL + u, MeshID(#MESH_BALL), #PB_Material_None )

      balls(u)\x = Random(5000) - Random(5000)
      balls(u)\y = Random(100)
      balls(u)\z = -Random(50100)
      balls(u)\speedx = 0
      balls(u)\speedy = 0
      balls(u)\speedz = 50

      balls(u)\speedy = Random(25)

      EntityRenderMode(#ENTITY_BALL + u, #PB_Entity_Solid  )

    Next u    

    ;--- Lights

    ;AmbientColor(RGB(255, 255, 200))
        
    CreateLight(0, RGB(255, 255, 120))
    CreateLight(1, RGB(0, 200, 50))
    CreateLight(2, RGB(255, 64, 25))

    LightSpecularColor(0, RGB(255, 255, 100))
    LightSpecularColor(1, RGB(0, 200, 50))
    LightSpecularColor(2, RGB(255, 64, 25))

    LightLocate(0, -40000, 40000, 20000)
    LightLocate(1, 00, 30000, -40000)
    LightLocate(2, 40000, -20000, -30000)       
   
     ;--- Camera

    ;Little computation to convert our pixels window to the % ogre
    level_min_x  = 0
    level_min_y  = 0
    level_max_x  = 1024
    level_max_y  = 768
    level_width  = level_max_x - level_min_x
    level_height = level_max_y - level_min_y
    CreateCamera(0, (level_min_x * 100) / ScreenWidth, (level_min_y * 100) / ScreenHeight, (level_width * 100) / ScreenWidth, (level_width * 100) / ScreenWidth)

    cam_x.f    = 0
    cam_y.f    = 0
    cam_z.f    = 2000
    CameraLocate(0, cam_x, cam_y, cam_z)
    CameraBackColor(0, RGB(0, 0, 0))

    ;--- Fog

    Fog(0, 1, 40000, 50000)
                                             
    Repeat

      Screen3DEvents()
      
      ClearScreen(RGB(0, 0, 0))
            
      If ExamineKeyboard()
      
        If KeyboardPushed(#PB_Key_Left)
          cam_x - 10
        EndIf
        If KeyboardPushed(#PB_Key_Right)
          cam_x + 10
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          cam_z - 10
        EndIf 
        If KeyboardPushed(#PB_Key_Down)
          cam_z + 10
        EndIf
                
      EndIf
      
      CameraLocate(0, cam_x, cam_y, cam_z)

      ;--- Entities move

      i.f+0.5
  
      For u=0 To 9
        EntityLocate(#ENTITY_BALL + u, balls(u)\x, balls(u)\y, balls(u)\z )
        RotateEntity(#ENTITY_BALL + u, 0, i * 5, 0)
  
        balls(u)\y + balls(u)\speedy
        balls(u)\speedy - 0.5
  
        If balls(u)\y < -400
          balls(u)\y = -390
          balls(u)\speedy = 25
        EndIf
  
        balls(u)\z + balls(u)\speedz
  
        If balls(u)\z > 100
          balls(u)\x = Random(5000) - Random(5000)
          balls(u)\z = -50000
        EndIf
  
      Next u    
       
      ;--- Rendering

      RenderWorld()
      Screen3DStats()
      FlipBuffers(2)

    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1

  EndIf
    
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
  
End
Edit : Title and file link
Last edited by djes on Fri Oct 31, 2008 1:56 pm, edited 2 times in total.
dige
Addict
Addict
Posts: 1410
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Post by dige »

Looks great! :D
With PB4.30 you need to declare varibles inside structures..
Is that the correct choice for sprite_nb and transp ?

Code: Select all

Structure ball
   x.f
   y.f
   z.f
   sprite_nb.l
   speedx.f
   speedy.f
   speedz.f
   rot.f
   transp.l
EndStructure 
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Post by djes »

dige wrote:Looks great! :D
With PB4.30 you need to declare varibles inside structures..
Is that the correct choice for sprite_nb and transp ?

Code: Select all

Structure ball
   x.f
   y.f
   z.f
   sprite_nb.l
   speedx.f
   speedy.f
   speedz.f
   rot.f
   transp.l
EndStructure 
Thank you :) I had removed the .l, in fact I should have changed it to .i, as it's the default integer type by now.
Post Reply