#include once "fbSPE.bi"
#include once "glMesh.bi"

ChDir ExePath()

' NOTE: from this 3D objects the Y axis points in Z direction
' use BodySetOrientation(body,MatrixX(-90)) to fix it

const as string MD_BOX      = "2.md"  ' 1.00 x 1.00 x 1.00
const as string MD_SPHERE   = "28.md" ' 0.50 x 0.50 x 0.50
const as string MD_CAPSULE  = "29.md" ' 0.25 x 0.25 x 0.50
const as string MD_CAPSULE2 = "31.md" ' 0.75 x 0.75 x 2.00
const as string MD_DISK     = "18.md" ' 1.00 x 1.00 x 0.10

const as string MD_HEX      = "11.md" ' 0.25 x 0.25 x 0.5
const as string MD_DODE     = "16.md" ' 0.50 x 0.50 x 0.50
const as string MD_OVAL     = "20.md" ' 0.50 x 1.00 x 2.00

const as string MD_ROUND    = "14.md" ' 40.0 x 40.0 x 1.00
const as string MD_RING     = "21.md" ' ?.?? x ?.?? x ?.??

const as string MD_BARREL   = "19.md" ' 0.50 x 0.50 x 0.50

const as string MD_BARREL2  = "24.md" ' ?.?? x ?.?? x ?.??
const as string MD_POOL     = "25.md" ' 1.25 x 0.50 x 1.25
const as string MD_RAMP     = "26.md" ' 0.50 x 2.00 x 0.50

const as string MD_CORNER   = "15.md" ' ?.?? x ?.?? x ?.??
const as string MD_CORNER2  = "27.md" ' ?.?? x ?.?? x ?.??

const as string MD_STAIRS   = "12.md" ' 10.0 x 10.0 x 0.50
const as string MD_STAIRS2  = "13.md"

const as string MD_STARX    = "22.md" ' ?.?? x ?.?? x ?.??
const as string MD_BONE     = "23.md" ' 0.50 x 1.50 x 0.50
const as string MD_CAPS     = "32.md" ' 1.00 x 1.00 x 3.00


const as integer NBODIES = 300

' -1.0 ... +1.0
function rnd2 as single
  return rnd-rnd
end function

dim as SPERIGIDBODY ptr bodies(NBODIES)
dim as GLMesh ground,sphere




InitOpenGL(640,480)

' create world
dim as SPEWORLD ptr world = CreateWorld()
WorldSetGravity(world,SPEVECTOR(0,-9.81,0))
' create a shape object
dim as SPESHAPE ptr shape = WorldCreateShape(world)

' create a static box for the ground in bodies(0)
ground.load MD_BOX,1000,100,1000
ShapeInitializeForStatic(shape,ground.pVertex,sizeof(SPEVERTEX),ground.pIndex,ground.nIndex\3)
bodies(0) = WorldAddRigidBody(world,shape)
BodySetPosition(bodies(0),SPEVECTOR(0,-50,0))

' create N spheres 1x1x1
sphere.load MD_SPHERE,2,2,2
ShapeInitializeAsSphere(shape,0.5)
for i as integer = 1 to NBODIES
  bodies(i) = WorldAddRigidBody(world,shape)
  BodySetPosition(bodies(i),SPEVECTOR(rnd2*20,20+rnd2*20,rnd2*20))
  BodySetOrientation(bodies(i),MatrixXYZ(rnd*360,rnd*360,rnd*360))
  BodySetAngularVelocity(bodies(i),SPEVECTOR(rnd,rnd,rnd))
next
' free the shape object (optional)
WorldReleaseShape(world,shape)

' simple simualtion and render loop
Dim e As FB.EVENT
do
  If (ScreenEvent(@e)) Then
    Select Case e.type
    Case FB.EVENT_KEY_PRESS
      If (e.scancode = FB.SC_ESCAPE) Then exit do
    Case FB.EVENT_WINDOW_CLOSE : exit do
    end select
  End If
  ' one step in simulation
  WorldDirectStep(world)
  
  BeginFrame ' clear scene draw sky, ground with fog
  'ground.render BodyGetTransform(bodies(0))
    ' draw n boxes
    for i as integer = 1 to NBODIES
      sphere.render BodyGetTransform(bodies(i))
    next
  EndFrame ' flip the OpenGL context

  sleep 10
loop

sphere.Release
ground.Release
ReleaseWorld(world)





