Page 1 of 1

Iacta alea est

Posted: Mon Nov 30, 2015 2:16 pm
by DK_PETER

Code: Select all

;Small dice example
;By DK_Peter
;------------------
EnableExplicit
InitEngine3D()
InitSprite()
InitMouse()
InitKeyboard()

Declare.i CreateDiceWorld()
Declare.f RandomF(min.f, Max.f, SeedVal.i = #PB_Ignore)

Structure _Object
  id.i
  ms.i
  ma.i
  tx.i
EndStructure

Structure _Dice
  num._Object[6]
  main._Object
EndStructure

Structure _movement
  x.f
  y.f
  z.f
  mx.i
  my.i
  CurrentLookX.f
  CurrentLookZ.f
EndStructure

Global dice._Dice, Table._Object, Quit = 0, mo._movement, Ret.i, ev.i

OpenWindow(0, 0, 0, 800, 600, "Iacta alea est", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)
Add3DArchive("texture", #PB_3DArchive_FileSystem)
WorldShadows(#PB_Shadow_Additive)
CreateCamera(0, 0, 0, 100, 100)
CameraRange(0, 0, 200)
MoveCamera(0, 0, 0.5, 1.5)
CreateCamera(1, 0, 0, 20, 20)
MoveCamera(1, 0, 0, 0)
CameraFOV(1, 20)
CameraDirection(1, 0, 1, 0)

CreateLight(0, $6DAEA3, 0, 20, 0, #PB_Light_Directional)

Ret = CreateDiceWorld()

Repeat
  
  Repeat
    ev = WindowEvent()
  Until  ev = 0
  
  ExamineMouse()
  mo\mx = -MouseDeltaX() * 0.05
  mo\my = -MouseDeltaY() * 0.05
  
  ExamineKeyboard()  
  
  If KeyboardPushed(#PB_Key_Left)
    mo\x = -0.05
  ElseIf KeyboardPushed(#PB_Key_Right)
    mo\x = 0.05
  Else
    mo\x = 0
  EndIf
  
  If KeyboardPushed(#PB_Key_Up)
    mo\y = -0.05
  ElseIf KeyboardPushed(#PB_Key_Down)
    mo\y = 0.05
  Else
    mo\y = 0
  EndIf
  
  If KeyboardReleased(#PB_Key_Space)
    With dice\main
      MoveEntity(\id, 0, 0, 0, #PB_Absolute)
      ApplyEntityImpulse(\id, RandomF(-0.2, 0.2), RandomF(0.3, 1), -RandomF(0.5, 2), RandomF(0,0.2), RandomF(0,0.2), RandomF(0,0.3))
    EndWith
  EndIf
  
  RotateCamera(0, mo\my, mo\mx, 0, #PB_Relative)
  MoveCamera(0, mo\x, 0, mo\y)
  CameraLookAt(1, mo\CurrentLookX, 0, mo\CurrentLookZ)
  If GetEntityAttribute(dice\main\id, #PB_Entity_IsActive) <> 0
    MoveCamera(1, EntityX(dice\main\id), 0.2, EntityZ(dice\main\id), #PB_Absolute)
    mo\CurrentLookX = EntityX(dice\main\id)
    mo\CurrentLookZ = EntityZ(dice\main\id)-0.005
  EndIf
  RenderWorld()
  
  FlipBuffers()
  
Until KeyboardPushed(#PB_Key_Escape)


Procedure.i CreateDiceWorld()
  With dice\main
    \ms = CreateCube(#PB_Any, 0.1)
    \tx = CreateTexture(#PB_Any, 64, 64)
    StartDrawing(TextureOutput(\tx))
    Box(0, 0, 64, 64, $4444FF)
    StopDrawing()
    \ma = CreateMaterial(#PB_Any, TextureID(\tx))
    \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0, 0)
  EndWith
  
  With dice\num[0] ;  1
    \ms = CreatePlane(#PB_Any, 0.101, 0.1, 1, 1, 1, 1)
    \tx = CreateTexture(#PB_Any, 128, 128)
    StartDrawing(TextureOutput(\tx))
    Box(0, 0, 128, 128, $0000FF)
    Circle(65, 65, 10, $333333)
    Circle(64, 64, 9, $777777)
    StopDrawing()
    \ma = CreateMaterial(#PB_Any, TextureID(\tx))
    MaterialCullingMode(\ma, #PB_Material_NoCulling)
    \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0.0505, 0)
  EndWith
  
  With dice\num[1]  ; 2
    \ms = CreatePlane(#PB_Any, 0.101, 0.1, 1, 1, 1, 1)
    \tx = CreateTexture(#PB_Any, 128, 128)
    StartDrawing(TextureOutput(\tx))
    Box(0, 0, 128, 128, $0000FF)
    Circle(33, 33, 10, $333333)
    Circle(32, 32, 9, $777777)
    Circle(97, 97, 10, $333333)
    Circle(96, 96, 9, $777777)
    StopDrawing()
    \ma = CreateMaterial(#PB_Any, TextureID(\tx))
    \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0, 0.0505)
    RotateEntity(\id, 90, 0, 0)
  EndWith
  
  With dice\num[2]  ; 3
    \ms = CreatePlane(#PB_Any, 0.101, 0.1, 1, 1, 1, 1)
    \tx = CreateTexture(#PB_Any, 128, 128)
    StartDrawing(TextureOutput(\tx))
    Box(0, 0, 128, 128, $0000FF)
    Circle(33, 33, 10, $333333)
    Circle(32, 32, 9, $777777)
    Circle(65, 65, 10, $333333)
    Circle(64, 64, 9, $777777)
    Circle(97, 97, 10, $333333)
    Circle(96, 96, 9, $777777)
    StopDrawing()
    \ma = CreateMaterial(#PB_Any, TextureID(\tx))
    \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), -0.0505, 0, 0)
    RotateEntity(\id, 0, 0, 90)
  EndWith
  
  With dice\num[3]  ; 4
    \ms = CreatePlane(#PB_Any, 0.101, 0.1, 1, 1, 1, 1)
    \tx = CreateTexture(#PB_Any, 128, 128)
    StartDrawing(TextureOutput(\tx))
    Box(0, 0, 128, 128, $0000FF)
    Circle(33, 33, 10, $333333)
    Circle(32, 32, 9, $777777)
    Circle(33, 97, 10, $333333)
    Circle(32, 96, 9, $777777)
    Circle(97, 33, 10, $333333)
    Circle(96, 32, 9, $777777)
    Circle(97, 97, 10, $333333)
    Circle(96, 96, 9, $777777)
    StopDrawing()
    \ma = CreateMaterial(#PB_Any, TextureID(\tx))
    \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0.0505, 0, 0)
    RotateEntity(\id, 0, 0, -90)
  EndWith
  
  With dice\num[4]  ; 5
    \ms = CreatePlane(#PB_Any, 0.101, 0.1, 1, 1, 1, 1)
    \tx = CreateTexture(#PB_Any, 128, 128)
    StartDrawing(TextureOutput(\tx))
    Box(0, 0, 128, 128, $0000FF)
    Circle(33, 33, 10, $333333)
    Circle(32, 32, 9, $777777)
    Circle(33, 97, 10, $333333)
    Circle(32, 96, 9, $777777)
    Circle(65, 64, 10, $333333)
    Circle(64, 64, 9, $777777)
    Circle(97, 33, 10, $333333)
    Circle(96, 32, 9, $777777)
    Circle(97, 97, 10, $333333)
    Circle(96, 96, 9, $777777)
    StopDrawing()
    \ma = CreateMaterial(#PB_Any, TextureID(\tx))
    \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0, -0.0505)
    RotateEntity(\id, -90, 0, 0)
  EndWith
  
  With dice\num[5]  ; 6
    \ms = CreatePlane(#PB_Any, 0.101, 0.1, 1, 1, 1, 1)
    \tx = CreateTexture(#PB_Any, 128, 128)
    StartDrawing(TextureOutput(\tx))
    Box(0, 0, 128, 128, $0000FF)
    Circle(33, 33, 10, $333333)
    Circle(32, 32, 9, $777777)
    Circle(33, 65, 10, $333333)
    Circle(32, 64, 9, $777777)
    Circle(33, 97, 10, $333333)
    Circle(32, 96, 9, $777777)
    Circle(97, 33, 10, $333333)
    Circle(96, 32, 9, $777777)
    Circle(97, 65, 10, $333333)
    Circle(96, 64, 9, $777777)
    Circle(97, 97, 10, $333333)
    Circle(96, 96, 9, $777777)
    StopDrawing()
    \ma = CreateMaterial(#PB_Any, TextureID(\tx))
    \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, -0.0505, 0)
    RotateEntity(\id, 0, 0, -180)
  EndWith
  
  With Table
    \ms = CreateCube(#PB_Any, 0.5)
    \tx = CreateTexture(#PB_Any, 128, 128)
    StartDrawing(TextureOutput(\tx))
    Box(0, 0, 128, 128, $24631F)
    DrawingMode(#PB_2DDrawing_Outlined)
    Box(2, 2, 124, 124, $999999)
    StopDrawing()
    \ma = CreateMaterial(#PB_Any, TextureID(\tx))
    \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, -1, -5)
    ScaleEntity(\id, 30, 0.2, 40)
    CreateEntityBody(\id, #PB_Entity_BoxBody, 0, 1, 55)
  EndWith 
  
  With dice\main
    AttachEntityObject(\id, "", EntityID(dice\num[0]\id))
    AttachEntityObject(\id, "", EntityID(dice\num[1]\id))
    AttachEntityObject(\id, "", EntityID(dice\num[2]\id))
    AttachEntityObject(\id, "", EntityID(dice\num[3]\id))
    AttachEntityObject(\id, "", EntityID(dice\num[4]\id))
    AttachEntityObject(\id, "", EntityID(dice\num[5]\id))
    CreateEntityBody(\id, #PB_Entity_BoxBody, 0.4 , 0.3, 25.5)
  EndWith
  ProcedureReturn #True
EndProcedure

Procedure.f RandomF(min.f, Max.f, SeedVal.i = #PB_Ignore)
  If SeedVal = #PB_Ignore : SeedVal = ElapsedMilliseconds() : EndIf
  ProcedureReturn (Min + (Max - Min) * Random(SeedVal) / SeedVal)
EndProcedure

Re: Iacta alea est

Posted: Tue Dec 01, 2015 2:57 pm
by applePi
Thanks DK_PETER for the nice demo, i will use it to consult in critical decisions.
i imagine also some scenarios:
1-is to use the sound for the dice when colliding with the ground
3- in a fancy possible dice game , and when the moon rise with a golden color the plane will be above the dice, and using worldgravity(9) will make anti gravity so the dice will fall Upward instead of downward
2-we may use the dodecahedron; a polyhedron with 12 flat faces or other shapes, look this sample detached from an example by Kelebrindae here: http://purebasic.fr/english/viewtopic.p ... 73#p437736 about platonic objects.
but the problem the dodecahedron ends always (seems) on the same position, so a random force in x,y,z with randomize seed may give it a better chaotic behavior. also random frictions and restitution may help also.

Code: Select all

; Author: Kelebrindae (modification of the "cylinder 3D" demo, from Comtois)
; Date: march,13, 2006, last updated febr, 13, 2014
; PB version: v4.61, updated to 5.21
;- Controls: F1 to F7 to change visualization

Enumeration
  #dodecahedron
  
EndEnumeration

;- Initialisation 
;If MessageRequester("Platonic Solids","Full Screen ?",#PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes      
 ; FullScreen=#True
;Else            
  FullScreen=#False
;EndIf 

If InitEngine3D() = 0 
  MessageRequester( "Error" , "Can't initialize 3D, check if engine3D.dll is available" , 0 ) 
End 
ElseIf InitSprite() = 0 Or InitKeyboard() = 0 
  MessageRequester( "Error" , "Can't find DirectX 7.0 or above" , 0 ) 
  End 
EndIf 


;- Open screen
If Fullscreen  
  OpenScreen(800,600,32,"Platonic Solids") 
Else 
  OpenWindow(0,0, 0, 800 , 600 ,"Platonic Solids",#PB_Window_ScreenCentered) 
  OpenWindowedScreen(WindowID(0),0,0, 800 , 600,0,0,0) 
EndIf 


EnableExplicit

;- ---- Procedures ----
;************************************************************************************
; Name: CreatePlatonicMesh
; Purpose: Create a platonic solid mesh, scaled and UV mapped dynamically
; Parameters:
;   - solide: #tetrahedron,#cube,#octahedron,#dodecahedron, or #icosahedron
;   - X size
;   - Y size
;   - Z size
;   - origin of mapping coord U 
;   - origin of mapping coord V
;   - Vertices color
; Return value: mesh number, or -1 if an error occurs
;************************************************************************************
Procedure.i CreatePlatonicMesh(solid.i,sizeX.f = 1,sizeY.f = 1,sizeZ.f = 1,Uorigin.f = 0,Vorigin.f = 0,Uscale.f = 1,Vscale.f = 1,color.i = $FFFFFF) 

  Protected nbVert.i , nbTri.i              ; Number of vertices and faces
  Protected x.f,y.f,z.f                     ; vertex position
  Protected nx.f,ny.f,nz.f                  ; vertex normals
  Protected u.f,v.f                         ; vertex UV coords (texture mapping)
  Protected newmesh.i                       ; Procedure Result
  Protected i.i,v1.i,v2.i,v3.i
  
  Restore dodecahedron
  ; Restore the good set of meshdatas
  
  ; Read number of vertices and triangles
  Read nbVert
  Read nbTri

  ; Read position, normals, uv coords, and add the vertex to the mesh
  newMesh = CreateMesh(#PB_Any)
  For i = 1 To nbVert
    Read.f x
    Read.f y
    Read.f z
    Read.f nx
    Read.f ny
    Read.f nz
    Read.f u
    Read.f v
    
    MeshVertexPosition(x * sizex,y * sizey,z * sizez)
    MeshVertexNormal(nx,ny,nz)
    MeshVertexColor(color)
    MeshVertexTextureCoordinate(uorigin + (u * uscale),vorigin + (v * vscale))
    
  Next i    
  
  ; Add the faces
  For i=1 To nbTri 
    Read.i v1
    Read.i v2
    Read.i v3 
    
    MeshFace(v1,v2,v3)
  Next i 
     
  ; Finalize as static mesh
  FinishMesh(#True)
  
  ProcedureReturn newMesh
EndProcedure    

DisableExplicit

;- ---- Main loop ----
;-Mesh 
; Change parameters 2 to 8 to test the effects on size and texturing

myDodecaMesh.i = CreatePlatonicMesh(#dodecahedron,3,3,3) 

;-Entity 

Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
CreateMaterial(3, LoadTexture(3, "ground_diffuse.png"))
CreateEntity(3,MeshID(myDodecaMesh),#PB_Material_None, 0,0, -300)
CreateEntityBody(3, #PB_Entity_ConvexHullBody, 1, 0.6, 1)

MoveEntity(3,0,0,0,#PB_Absolute)
ScaleEntity(3,4,4,4)

;-Camera 
CreateCamera(0, 0, 0 , 100 , 100) 
MoveCamera(0,0,5,-200,#PB_Absolute) 
CameraLookAt(0,0,0,0) 
viewlabel.s = "Platonic Solids"

;-Light 
AmbientColor(RGB(105,105,105)) 
CreateLight(0,RGB(160,160,255),200,300,0) 
CreateLight(1,RGB(255,127,0),-200,-200,200) 

CreatePlane(9, 500, 500, 5, 5, 5, 5)
CreateEntity(9,MeshID(9),MaterialID(3) , 0, -50, 0)
EntityRenderMode(9, 0) 
CreateEntityBody(9, #PB_Entity_PlaneBody, 0, 1, 1)
    

pas.f = 0.8
angle.f = 0
Repeat 
   If fullscreen = 0 
      While WindowEvent() : Wend 
   EndIf
   
   ; Rotate solids, rotate! Oh, you're so gorgeous... ;)
   Angle + Pas 
   
   ;RotateEntity(3, -angle,angle/2,angle,#PB_Absolute) 
   
   ; Manage camera views
   If ExamineKeyboard() 
     
   EndIf 
  
  ; show it all
  RenderWorld() 
   
  ; Flip buffers to avoid tearing  
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) 

;- Solids datas
DataSection:

dodecahedron:
; Nb sommets / Nb faces
Data.i 72,36

; Vertices: pos / normals / uv
Data.f 0.357822,0.935172,0
Data.f 0,0.955423,0.295242
Data.f 0.190983,1
Data.f -0.355822,0.935172,0
Data.f 0,0.955423,-0.295242
Data.f -0.190983,1
Data.f 0.578350,0.578350,0.578350
Data.f 0.688191,0.587785,0.425325
Data.f 0.309017,0.690983
Data.f 0,0.357822,0.935172
Data.f 0,0.850651,0.525731
Data.f 0,0.5
Data.f -0.576350,0.578350,0.578350
Data.f 0,0.850651,0.525731
Data.f -0.309017,0.690983
Data.f -0.576350,0.578350,-0.576350
Data.f 0,0.850651,-0.525731
Data.f -0.309017,1.309020
Data.f 0,0.357822,-0.933172
Data.f 0,0.850651,-0.525731
Data.f 0,1.5
Data.f 0.578350,0.578350,-0.576350
Data.f 0,0.850651,-0.525731
Data.f 0.309017,1.309020
Data.f 0.935172,0,0.357822
Data.f 1,0,0
Data.f 0.190983,1
Data.f 0.935172,0,-0.355822
Data.f 1,0,0
Data.f -0.190983,1
Data.f 0,-0.355822,-0.933172
Data.f 0,0,-1
Data.f -0.190983,1
Data.f 0.578350,-0.576350,-0.576350
Data.f 0.525731,0,-0.850651
Data.f -0.309017,0.690983
Data.f 0.578350,-0.576350,0.578350
Data.f 0.850651,-0.525731,0
Data.f 0.309017,1.309020
Data.f 0.357822,-0.933172,0
Data.f 0.850651,-0.525731,0
Data.f 0,1.5
Data.f -0.576350,-0.576350,-0.576350
Data.f -0.425325,-0.688191,-0.587785
Data.f -0.309017,1.309020
Data.f -0.355822,-0.933172,0
Data.f 0,-0.992447,0.122673
Data.f -0.190983,1
Data.f 0,-0.355822,0.935172
Data.f 0,-0.850651,0.525731
Data.f 0,0.5
Data.f -0.576350,-0.576350,0.578350
Data.f 0,-0.850651,0.525731
Data.f -0.309017,0.690983
Data.f -0.933172,0,-0.355822
Data.f -0.979432,-0.201774,0
Data.f -0.190983,1
Data.f -0.933172,0,0.357822
Data.f -0.992447,0.122673,0
Data.f 0.190983,1
Data.f 0.578350,0.578350,-0.576350
Data.f 0.850651,0.525731,0
Data.f -0.309017,0.690983
Data.f 0.578350,0.578350,-0.576350
Data.f 0.850651,0.525731,0
Data.f -0.309017,0.690983
Data.f 0.578350,0.578350,-0.576350
Data.f 0.850651,0.525731,0
Data.f -0.309017,0.690983
Data.f 0.357822,0.935172,0
Data.f 0.850651,0.525731,0
Data.f 0,0.5
Data.f 0.578350,0.578350,-0.576350
Data.f 0.525731,0,-0.850651
Data.f 0.309017,0.690983
Data.f 0,0.357822,-0.933172
Data.f 0.525731,0,-0.850651
Data.f 0.190983,1
Data.f 0.578350,0.578350,-0.576350
Data.f 0.525731,0,-0.850651
Data.f 0.309017,0.690983
Data.f 0.578350,0.578350,-0.576350
Data.f 0.525731,0,-0.850651
Data.f 0.309017,0.690983
Data.f 0.935172,0,-0.355822
Data.f 0.525731,0,-0.850651
Data.f 0,0.5
Data.f 0.578350,-0.576350,-0.576350
Data.f 0.850651,-0.525731,0
Data.f -0.309017,1.309020
Data.f 0.578350,-0.576350,-0.576350
Data.f 0.850651,-0.525731,0
Data.f -0.309017,1.309020
Data.f 0.578350,-0.576350,-0.576350
Data.f 0.850651,-0.525731,0
Data.f -0.309017,1.309020
Data.f 0.578350,-0.576350,-0.576350
Data.f 0,-0.850651,-0.525731
Data.f 0.309017,1.309020
Data.f 0,-0.355822,-0.933172
Data.f 0,-0.850651,-0.525731
Data.f 0,1.5
Data.f 0.578350,-0.576350,-0.576350
Data.f 0,-0.850651,-0.525731
Data.f 0.309017,1.309020
Data.f 0.578350,-0.576350,-0.576350
Data.f 0,-0.850651,-0.525731
Data.f 0.309017,1.309020
Data.f 0.357822,-0.933172,0
Data.f 0,-0.850651,-0.525731
Data.f 0.190983,1
Data.f 0.578350,-0.576350,0.578350
Data.f 0,-0.850651,0.525731
Data.f 0.309017,0.690983
Data.f 0.578350,-0.576350,0.578350
Data.f 0,-0.850651,0.525731
Data.f 0.309017,0.690983
Data.f 0.357822,-0.933172,0
Data.f 0,-0.850651,0.525731
Data.f 0.190983,1
Data.f -0.355822,-0.933172,0
Data.f -0.850651,-0.525731,0
Data.f 0,1.5
Data.f -0.355822,-0.933172,0
Data.f -0.850651,-0.525731,0
Data.f 0,1.5
Data.f -0.355822,-0.933172,0
Data.f -0.850651,-0.525731,0
Data.f 0,1.5
Data.f -0.576350,-0.576350,0.578350
Data.f -0.850651,-0.525731,0
Data.f 0.309017,1.309020
Data.f -0.933172,0,0.357822
Data.f -0.525731,0,0.850651
Data.f 0,1.5
Data.f -0.576350,0.578350,0.578350
Data.f -0.525731,0,0.850651
Data.f 0.309017,1.309020
Data.f 0,0.357822,0.935172
Data.f -0.525731,0,0.850651
Data.f 0.190983,1
Data.f -0.933172,0,0.357822
Data.f -0.525731,0,0.850651
Data.f 0,1.5
Data.f 0,0.357822,0.935172
Data.f -0.525731,0,0.850651
Data.f 0.190983,1
Data.f 0,-0.355822,0.935172
Data.f -0.525731,0,0.850651
Data.f -0.190983,1
Data.f -0.933172,0,0.357822
Data.f -0.525731,0,0.850651
Data.f 0,1.5
Data.f 0,-0.355822,0.935172
Data.f -0.525731,0,0.850651
Data.f -0.190983,1
Data.f -0.576350,-0.576350,0.578350
Data.f -0.525731,0,0.850651
Data.f -0.309017,1.309020
Data.f -0.576350,0.578350,-0.576350
Data.f -0.850651,0.525731,0
Data.f -0.309017,0.690983
Data.f -0.576350,0.578350,-0.576350
Data.f -0.850651,0.525731,0
Data.f -0.309017,0.690983
Data.f -0.355822,0.935172,0
Data.f -0.850651,0.525731,0
Data.f 0,0.5
Data.f -0.355822,0.935172,0
Data.f -0.850651,0.525731,0
Data.f 0,0.5
Data.f -0.576350,0.578350,0.578350
Data.f -0.850651,0.525731,0
Data.f 0.309017,0.690983
Data.f -0.933172,0,-0.355822
Data.f -0.525731,0,-0.850651
Data.f 0,1.5
Data.f -0.933172,0,-0.355822
Data.f -0.525731,0,-0.850651
Data.f 0,1.5
Data.f 0,0.357822,-0.933172
Data.f -0.525731,0,-0.850651
Data.f 0.190983,1
Data.f -0.933172,0,-0.355822
Data.f -0.525731,0,-0.850651
Data.f 0,1.5
Data.f 0,0.357822,-0.933172
Data.f -0.525731,0,-0.850651
Data.f 0.190983,1
Data.f -0.576350,0.578350,-0.576350
Data.f -0.525731,0,-0.850651
Data.f 0.309017,1.309020
Data.f 0,0.357822,0.935172
Data.f 0.525731,0,0.850651
Data.f 0.190983,1
Data.f 0.935172,0,0.357822
Data.f 0.525731,0,0.850651
Data.f 0,0.5
Data.f 0,0.357822,0.935172
Data.f 0.525731,0,0.850651
Data.f 0.190983,1
Data.f 0.935172,0,0.357822
Data.f 0.525731,0,0.850651
Data.f 0,0.5
Data.f 0.578350,-0.576350,0.578350
Data.f 0.525731,0,0.850651
Data.f -0.309017,0.690983
Data.f 0,0.357822,0.935172
Data.f 0.525731,0,0.850651
Data.f 0.190983,1
Data.f 0.578350,-0.576350,0.578350
Data.f 0.525731,0,0.850651
Data.f -0.309017,0.690983
Data.f 0,-0.355822,0.935172
Data.f 0.525731,0,0.850651
Data.f -0.190983,1

; Faces
Data.i 0,3,2
Data.i 0,4,3
Data.i 0,1,4
Data.i 1,6,5
Data.i 1,7,6
Data.i 1,0,7
Data.i 20,8,9
Data.i 21,2,8
Data.i 22,23,2
Data.i 24,10,25
Data.i 26,11,10
Data.i 27,28,11
Data.i 29,12,13
Data.i 30,8,12
Data.i 31,9,8
Data.i 32,14,33
Data.i 34,15,14
Data.i 35,36,15
Data.i 15,16,17
Data.i 15,37,16
Data.i 15,39,38
Data.i 40,18,14
Data.i 41,19,18
Data.i 42,43,19
Data.i 44,46,45
Data.i 47,49,48
Data.i 50,52,51
Data.i 19,53,18
Data.i 19,55,54
Data.i 19,57,56
Data.i 58,10,14
Data.i 59,60,10
Data.i 61,63,62
Data.i 64,65,2
Data.i 66,68,67
Data.i 69,71,70


EndDataSection

Re: Iacta alea est

Posted: Wed Dec 02, 2015 9:56 am
by Joris
I'm not into 3D programming but just trying the code of Peter I get this error (still using XP SP3):

[09:49:41] Waiting for executable to start...
[09:49:41] Executable type: Windows - x86 (32bit, Unicode)
[09:49:41] Executable started.
[09:49:41] [ERROR] Line: 96
[09:49:41] [ERROR] Invalid memory access. (read error at address 2332345487)
[09:49:46] The Program was killed.

Re: Iacta alea est

Posted: Wed Dec 02, 2015 11:10 am
by Kelebrindae
Hi,

@ApplePi: Thanks for the resurrection of this old snippet!

I've adjusted a few parameters in your code, to make the rolls looks more real:
- The mesh was created with size = 3, and the entity was then scaled x4. Thus, if one unit is one meter (that more or less what the engine assumes), your dice was a mere 12 meters wide. That's quite a large dice, and that explains why it seemed to move so slow. I resized it to 1, which is still too big but feels more natural.
- Added shadow and texture to the dice.
- Press Space to throw the dice once more.
- There seems to be a small problem in the creation of the entity's body: the physics body is slightly bigger than the mesh, so the entity appears to "hover" a little above the ground... :? Don't know if it's a bug, if there's a workaround, etc.. (press "P" to display the physics bodies)

Code: Select all

; Author: Kelebrindae (modification of the "cylinder 3D" demo, from Comtois)
; Date: march,13, 2006, last updated dec, 02, 2015
; PB version: v4.61, updated to 5.40

Enumeration
  #dodecahedron
 
EndEnumeration

;- Initialisation
;If MessageRequester("Platonic Solids","Full Screen ?",#PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes     
 ; FullScreen=#True
;Else           
  FullScreen=#False
;EndIf

If InitEngine3D() = 0
  MessageRequester( "Error" , "Can't initialize 3D, check if engine3D.dll is available" , 0 )
End
ElseIf InitSprite() = 0 Or InitKeyboard() = 0
  MessageRequester( "Error" , "Can't find DirectX 7.0 or above" , 0 )
  End
EndIf


;- Open screen
If Fullscreen 
  OpenScreen(800,600,32,"Platonic Solids")
Else
  OpenWindow(0,0, 0, 800 , 600 ,"Platonic Solids",#PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(0),0,0, 800 , 600,0,0,0)
EndIf


EnableExplicit

;- ---- Procedures ----
;************************************************************************************
; Name: CreatePlatonicMesh
; Purpose: Create a platonic solid mesh, scaled and UV mapped dynamically
; Parameters:
;   - solide: #tetrahedron,#cube,#octahedron,#dodecahedron, or #icosahedron
;   - X size
;   - Y size
;   - Z size
;   - origin of mapping coord U
;   - origin of mapping coord V
;   - Vertices color
; Return value: mesh number, or -1 if an error occurs
;************************************************************************************
Procedure.i CreatePlatonicMesh(solid.i,sizeX.f = 1,sizeY.f = 1,sizeZ.f = 1,Uorigin.f = 0,Vorigin.f = 0,Uscale.f = 1,Vscale.f = 1,color.i = $FFFFFF)

  Protected nbVert.i , nbTri.i              ; Number of vertices and faces
  Protected x.f,y.f,z.f                     ; vertex position
  Protected nx.f,ny.f,nz.f                  ; vertex normals
  Protected u.f,v.f                         ; vertex UV coords (texture mapping)
  Protected newmesh.i                       ; Procedure Result
  Protected i.i,v1.i,v2.i,v3.i
 
  Restore dodecahedron
  ; Restore the good set of meshdatas
 
  ; Read number of vertices and triangles
  Read nbVert
  Read nbTri

  ; Read position, normals, uv coords, and add the vertex to the mesh
  newMesh = CreateMesh(#PB_Any)
  For i = 1 To nbVert
    Read.f x
    Read.f y
    Read.f z
    Read.f nx
    Read.f ny
    Read.f nz
    Read.f u
    Read.f v
   
    MeshVertexPosition(x * sizex,y * sizey,z * sizez)
    MeshVertexNormal(nx,ny,nz)
    MeshVertexColor(color)
    MeshVertexTextureCoordinate(uorigin + (u * uscale),vorigin + (v * vscale))
   
  Next i   
 
  ; Add the faces
  For i=1 To nbTri
    Read.i v1
    Read.i v2
    Read.i v3
   
    MeshFace(v1,v2,v3)
  Next i
     
  ; Finalize as static mesh
  FinishMesh(#True)
 
  ProcedureReturn newMesh
EndProcedure   

DisableExplicit

;- ---- Main loop ----
;-Mesh
; Change parameters 2 to 8 to test the effects on size and texturing
myDodecaMesh.i = CreatePlatonicMesh(#dodecahedron,1,1,1)

;-Entity
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
CreateMaterial(3, LoadTexture(3, "dameCoeur.png"))
MaterialFilteringMode(3, #PB_Material_Anisotropic ,6)

CreateEntity(3,MeshID(myDodecaMesh),MaterialID(3), 0,10,0)
CreateEntityBody(3, #PB_Entity_ConvexHullBody, 1, 0.6, 1)
WorldShadows(#PB_Shadow_Additive)

;-Camera
CreateCamera(0, 0, 0 , 100 , 100)
MoveCamera(0,0,5,-10,#PB_Absolute)

;-Light
AmbientColor(RGB(105,105,105))
CreateLight(0,RGB(160,160,255),200,300,0)
CreateLight(1,RGB(255,127,0),-200,-200,200)

CreateMaterial(9, LoadTexture(9, "ground_diffuse.png"))
CreatePlane(9, 100, 100, 50, 50, 50, 50)
CreateEntity(9,MeshID(9),MaterialID(9) , 0, 0, 0)
EntityRenderMode(9, 0)
CreateEntityBody(9, #PB_Entity_PlaneBody, 0, 0.8, 1)
   

Repeat
   If fullscreen = 0
      While WindowEvent() : Wend
   EndIf
   
   ; Manage camera views
   If ExamineKeyboard()
     If KeyboardReleased(#PB_Key_Space)
       ApplyEntityImpulse(3,0,10,0)
     EndIf
   EndIf
   
  CameraLookAt(0,EntityX(3) ,0 ,EntityZ(3) )
   
  ; show it all
  RenderWorld()
   
  ; Flip buffers to avoid tearing 
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)

;- Solids datas
DataSection:

dodecahedron:
; Nb sommets / Nb faces
Data.i 72,36

; Vertices: pos / normals / uv
Data.f 0.357822,0.935172,0
Data.f 0,0.955423,0.295242
Data.f 0.190983,1
Data.f -0.355822,0.935172,0
Data.f 0,0.955423,-0.295242
Data.f -0.190983,1
Data.f 0.578350,0.578350,0.578350
Data.f 0.688191,0.587785,0.425325
Data.f 0.309017,0.690983
Data.f 0,0.357822,0.935172
Data.f 0,0.850651,0.525731
Data.f 0,0.5
Data.f -0.576350,0.578350,0.578350
Data.f 0,0.850651,0.525731
Data.f -0.309017,0.690983
Data.f -0.576350,0.578350,-0.576350
Data.f 0,0.850651,-0.525731
Data.f -0.309017,1.309020
Data.f 0,0.357822,-0.933172
Data.f 0,0.850651,-0.525731
Data.f 0,1.5
Data.f 0.578350,0.578350,-0.576350
Data.f 0,0.850651,-0.525731
Data.f 0.309017,1.309020
Data.f 0.935172,0,0.357822
Data.f 1,0,0
Data.f 0.190983,1
Data.f 0.935172,0,-0.355822
Data.f 1,0,0
Data.f -0.190983,1
Data.f 0,-0.355822,-0.933172
Data.f 0,0,-1
Data.f -0.190983,1
Data.f 0.578350,-0.576350,-0.576350
Data.f 0.525731,0,-0.850651
Data.f -0.309017,0.690983
Data.f 0.578350,-0.576350,0.578350
Data.f 0.850651,-0.525731,0
Data.f 0.309017,1.309020
Data.f 0.357822,-0.933172,0
Data.f 0.850651,-0.525731,0
Data.f 0,1.5
Data.f -0.576350,-0.576350,-0.576350
Data.f -0.425325,-0.688191,-0.587785
Data.f -0.309017,1.309020
Data.f -0.355822,-0.933172,0
Data.f 0,-0.992447,0.122673
Data.f -0.190983,1
Data.f 0,-0.355822,0.935172
Data.f 0,-0.850651,0.525731
Data.f 0,0.5
Data.f -0.576350,-0.576350,0.578350
Data.f 0,-0.850651,0.525731
Data.f -0.309017,0.690983
Data.f -0.933172,0,-0.355822
Data.f -0.979432,-0.201774,0
Data.f -0.190983,1
Data.f -0.933172,0,0.357822
Data.f -0.992447,0.122673,0
Data.f 0.190983,1
Data.f 0.578350,0.578350,-0.576350
Data.f 0.850651,0.525731,0
Data.f -0.309017,0.690983
Data.f 0.578350,0.578350,-0.576350
Data.f 0.850651,0.525731,0
Data.f -0.309017,0.690983
Data.f 0.578350,0.578350,-0.576350
Data.f 0.850651,0.525731,0
Data.f -0.309017,0.690983
Data.f 0.357822,0.935172,0
Data.f 0.850651,0.525731,0
Data.f 0,0.5
Data.f 0.578350,0.578350,-0.576350
Data.f 0.525731,0,-0.850651
Data.f 0.309017,0.690983
Data.f 0,0.357822,-0.933172
Data.f 0.525731,0,-0.850651
Data.f 0.190983,1
Data.f 0.578350,0.578350,-0.576350
Data.f 0.525731,0,-0.850651
Data.f 0.309017,0.690983
Data.f 0.578350,0.578350,-0.576350
Data.f 0.525731,0,-0.850651
Data.f 0.309017,0.690983
Data.f 0.935172,0,-0.355822
Data.f 0.525731,0,-0.850651
Data.f 0,0.5
Data.f 0.578350,-0.576350,-0.576350
Data.f 0.850651,-0.525731,0
Data.f -0.309017,1.309020
Data.f 0.578350,-0.576350,-0.576350
Data.f 0.850651,-0.525731,0
Data.f -0.309017,1.309020
Data.f 0.578350,-0.576350,-0.576350
Data.f 0.850651,-0.525731,0
Data.f -0.309017,1.309020
Data.f 0.578350,-0.576350,-0.576350
Data.f 0,-0.850651,-0.525731
Data.f 0.309017,1.309020
Data.f 0,-0.355822,-0.933172
Data.f 0,-0.850651,-0.525731
Data.f 0,1.5
Data.f 0.578350,-0.576350,-0.576350
Data.f 0,-0.850651,-0.525731
Data.f 0.309017,1.309020
Data.f 0.578350,-0.576350,-0.576350
Data.f 0,-0.850651,-0.525731
Data.f 0.309017,1.309020
Data.f 0.357822,-0.933172,0
Data.f 0,-0.850651,-0.525731
Data.f 0.190983,1
Data.f 0.578350,-0.576350,0.578350
Data.f 0,-0.850651,0.525731
Data.f 0.309017,0.690983
Data.f 0.578350,-0.576350,0.578350
Data.f 0,-0.850651,0.525731
Data.f 0.309017,0.690983
Data.f 0.357822,-0.933172,0
Data.f 0,-0.850651,0.525731
Data.f 0.190983,1
Data.f -0.355822,-0.933172,0
Data.f -0.850651,-0.525731,0
Data.f 0,1.5
Data.f -0.355822,-0.933172,0
Data.f -0.850651,-0.525731,0
Data.f 0,1.5
Data.f -0.355822,-0.933172,0
Data.f -0.850651,-0.525731,0
Data.f 0,1.5
Data.f -0.576350,-0.576350,0.578350
Data.f -0.850651,-0.525731,0
Data.f 0.309017,1.309020
Data.f -0.933172,0,0.357822
Data.f -0.525731,0,0.850651
Data.f 0,1.5
Data.f -0.576350,0.578350,0.578350
Data.f -0.525731,0,0.850651
Data.f 0.309017,1.309020
Data.f 0,0.357822,0.935172
Data.f -0.525731,0,0.850651
Data.f 0.190983,1
Data.f -0.933172,0,0.357822
Data.f -0.525731,0,0.850651
Data.f 0,1.5
Data.f 0,0.357822,0.935172
Data.f -0.525731,0,0.850651
Data.f 0.190983,1
Data.f 0,-0.355822,0.935172
Data.f -0.525731,0,0.850651
Data.f -0.190983,1
Data.f -0.933172,0,0.357822
Data.f -0.525731,0,0.850651
Data.f 0,1.5
Data.f 0,-0.355822,0.935172
Data.f -0.525731,0,0.850651
Data.f -0.190983,1
Data.f -0.576350,-0.576350,0.578350
Data.f -0.525731,0,0.850651
Data.f -0.309017,1.309020
Data.f -0.576350,0.578350,-0.576350
Data.f -0.850651,0.525731,0
Data.f -0.309017,0.690983
Data.f -0.576350,0.578350,-0.576350
Data.f -0.850651,0.525731,0
Data.f -0.309017,0.690983
Data.f -0.355822,0.935172,0
Data.f -0.850651,0.525731,0
Data.f 0,0.5
Data.f -0.355822,0.935172,0
Data.f -0.850651,0.525731,0
Data.f 0,0.5
Data.f -0.576350,0.578350,0.578350
Data.f -0.850651,0.525731,0
Data.f 0.309017,0.690983
Data.f -0.933172,0,-0.355822
Data.f -0.525731,0,-0.850651
Data.f 0,1.5
Data.f -0.933172,0,-0.355822
Data.f -0.525731,0,-0.850651
Data.f 0,1.5
Data.f 0,0.357822,-0.933172
Data.f -0.525731,0,-0.850651
Data.f 0.190983,1
Data.f -0.933172,0,-0.355822
Data.f -0.525731,0,-0.850651
Data.f 0,1.5
Data.f 0,0.357822,-0.933172
Data.f -0.525731,0,-0.850651
Data.f 0.190983,1
Data.f -0.576350,0.578350,-0.576350
Data.f -0.525731,0,-0.850651
Data.f 0.309017,1.309020
Data.f 0,0.357822,0.935172
Data.f 0.525731,0,0.850651
Data.f 0.190983,1
Data.f 0.935172,0,0.357822
Data.f 0.525731,0,0.850651
Data.f 0,0.5
Data.f 0,0.357822,0.935172
Data.f 0.525731,0,0.850651
Data.f 0.190983,1
Data.f 0.935172,0,0.357822
Data.f 0.525731,0,0.850651
Data.f 0,0.5
Data.f 0.578350,-0.576350,0.578350
Data.f 0.525731,0,0.850651
Data.f -0.309017,0.690983
Data.f 0,0.357822,0.935172
Data.f 0.525731,0,0.850651
Data.f 0.190983,1
Data.f 0.578350,-0.576350,0.578350
Data.f 0.525731,0,0.850651
Data.f -0.309017,0.690983
Data.f 0,-0.355822,0.935172
Data.f 0.525731,0,0.850651
Data.f -0.190983,1

; Faces
Data.i 0,3,2
Data.i 0,4,3
Data.i 0,1,4
Data.i 1,6,5
Data.i 1,7,6
Data.i 1,0,7
Data.i 20,8,9
Data.i 21,2,8
Data.i 22,23,2
Data.i 24,10,25
Data.i 26,11,10
Data.i 27,28,11
Data.i 29,12,13
Data.i 30,8,12
Data.i 31,9,8
Data.i 32,14,33
Data.i 34,15,14
Data.i 35,36,15
Data.i 15,16,17
Data.i 15,37,16
Data.i 15,39,38
Data.i 40,18,14
Data.i 41,19,18
Data.i 42,43,19
Data.i 44,46,45
Data.i 47,49,48
Data.i 50,52,51
Data.i 19,53,18
Data.i 19,55,54
Data.i 19,57,56
Data.i 58,10,14
Data.i 59,60,10
Data.i 61,63,62
Data.i 64,65,2
Data.i 66,68,67
Data.i 69,71,70


EndDataSection

Re: Iacta alea est

Posted: Wed Dec 02, 2015 12:11 pm
by DK_PETER
@Joris
I don't know, why you are getting an error.
I haven't used XP for years. Try updating DirectX drivers or maybe set your Librabry subsystem to 'OpenGL'.

@Kelebrindae
Really quite disappointing..The posting was about a 'Dice' - not a resurrection of a previous thread, which could have been done there instead.
The posting would surely be extremely valid if you've had added numbers or 'a multilayered material script' adding numbers to the mesh.
I think that applePi showed your code - to see it realized into a real dice - if not - then I've misunderstood him.
I know that mine was extremely simple, but it was a real dice nonetheless. (with no scripting at all) :-)

Re: Iacta alea est

Posted: Wed Dec 02, 2015 1:20 pm
by applePi
Kelebrindae , thanks for the nice demo as always. you are right there is a space between the dice and the ground
DK_PETER: this is a resurrection of a previous thread in a context of a Dice, if every user wants to hire a lawyer before he post anything then no one will post anything, or reply to any post, and the forum will be useless. there should always be some fuzziness in the forum, because this is the nature of the natural world .
simply i will never reply to your posts again.
regards

PS: you said :Life is really simple, but we insist on making it complicated
but you make it very complex this way

Re: Iacta alea est

Posted: Wed Dec 02, 2015 1:23 pm
by DK_PETER
@applePi
Fine :-)

Re: Iacta alea est

Posted: Wed Dec 02, 2015 5:49 pm
by Kelebrindae
Feel better that way? :wink:

Code: Select all

; Author: Kelebrindae 
; Date: dec, 02, 2015
; PB version: 5.40
; Press Space to throw the dice, and "P" to display its Physics body.

;- Initialisation
;If MessageRequester("12-sided dice","Full Screen ?",#PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes     
 ; FullScreen=#True
;Else           
  FullScreen=#False
;EndIf

If InitEngine3D() = 0
  MessageRequester( "Error" , "Can't initialize 3D, check if engine3D.dll is available" , 0 )
End
ElseIf InitSprite() = 0 Or InitKeyboard() = 0
  MessageRequester( "Error" , "Can't find DirectX 7.0 or above" , 0 )
  End
EndIf


;- Open screen
If Fullscreen
  OpenScreen(800,600,32,"12-sided dice")
Else
  OpenWindow(0,0, 0, 800 , 600 ,"12-sided dice",#PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(0),0,0, 800 , 600,0,0,0)
EndIf


EnableExplicit

;- ---- Procedures ----
; Default UV mapping is 4 slices by 2, so numbers are spread this way on the texture:
;       |   |   |   |   |
;       | 1 | 3 | 5 | 7 |
;       | 2 | 4 | 6 | 8 |
;       |   |   |   |   |
;       -----------------
;       |   |   |   |   |
;       | 9 | 11|   |   |
;       | 10| 12|   |   |
;       |   |   |   |   |
Procedure.i CreateDice(sizeX.f = 1,sizeY.f = 1,sizeZ.f = 1,Uscale.f = 0.25,Vscale.f = 0.5,color.i = $FFFFFF)

  Protected nbVert.i , nbTri.i              ; Number of vertices and faces
  Protected x.f,y.f,z.f                     ; vertex position
  Protected nx.f,ny.f,nz.f                  ; vertex normals
  Protected u.f,v.f                         ; vertex UV coords (texture mapping)
  Protected newmesh.i                       ; Procedure Result
  Protected i.i,v1.i,v2.i,v3.i
  Protected nbSub.i
  Protected uOrigin.f,vOrigin.f
  
  For nbSub = 0 To 5
    ; Restore the good set of meshdatas
    Restore dodeBiFace
  
    ; Read number of vertices and triangles
    Read nbVert
    Read nbTri
  
    ; Read position, normals, uv coords, and add the vertex to the mesh
    If nbSub = 0
      newMesh = CreateMesh(#PB_Any)
    Else
      AddSubMesh()
    EndIf
    
    For i = 1 To nbVert
      Read.f x
      Read.f y
      Read.f z
      Read.f nx
      Read.f ny
      Read.f nz
      Read.f u
      Read.f v
      
       MeshVertexPosition(x * sizex,y * sizey,z * sizez)
       MeshVertexNormal(nx,ny,nz)
       MeshVertexColor(color)
       MeshVertexTextureCoordinate(uorigin + (u * uscale),vorigin + (v * vscale)) 
    Next i   
   
    ; Add the faces
    For i=1 To nbTri
      Read.i v1
      Read.i v2
      Read.i v3
      
     MeshFace(v1,v2,v3)
    Next i
    
    uOrigin + uScale
    If uOrigin >= 1.0
      uOrigin = 0.0
      vOrigin + vScale
    EndIf
    
  Next nbSub
  
  ; Finalize as static mesh
  FinishMesh(#True)
  
  ; Rotate each group of 2 faces.
  TransformMesh(newmesh,0,0,0,1,1,1,180,0,0,1)
  TransformMesh(newmesh,0,0,0,1,1,1,90,0,90,2)
  TransformMesh(newmesh,0,0,0,1,1,1,90,0,-90,3)
  TransformMesh(newmesh,0,0,0,1,1,1,0,90,90,4)
  TransformMesh(newmesh,0,0,0,1,1,1,0,90,-90,5)
  
  NormalizeMesh(newmesh)

  ProcedureReturn newMesh
EndProcedure   

DisableExplicit

;- ---- Main ----
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)

#DICE = 1
#GROUND = 2

;-Mesh
myDiceMesh.i = CreateDice()

;- Texture
size.i = 256
CreateTexture(#DICE,size,size)
StartDrawing(TextureOutput(#DICE))
  Box(0,0,size/4,size/2,$FF0000)
  Box(size/4,0,size/4,size/2,$00AA00)
  Box(2*size/4,0,size/4,size/2,$0000FF)
  Box(3*size/4,0,size/4,size/2,$0077FF)
  Box(0,size/2,size/4,size/2,$FF00FF)
  Box(size/4,size/2,size/4,size/2,$777700)
  
  DrawingMode(#PB_2DDrawing_Transparent )
  x = size/8: y = size/8 
  For i = 1 To 12 Step 2
    DrawText(x - TextWidth(Str(i))/2,y - TextHeight(Str(i))/2,Str(i),$FFFFFF)
    DrawText(x - TextWidth(Str(i+1))/2,y + size/4 - TextHeight(Str(i+1))/2,Str(i+1),$FFFFFF)
    
    x=x+(size/4)
    If x >= size
      x = size/8
      y = y+(size/2)
    EndIf
  Next i
StopDrawing()

;- Material
CreateMaterial(#DICE, TextureID(#DICE))
MaterialFilteringMode(#DICE, #PB_Material_Anisotropic ,6)

;-Entity
CreateEntity(#DICE,MeshID(myDiceMesh),MaterialID(#DICE), 0,5,0)
CreateEntityBody(#DICE, #PB_Entity_ConvexHullBody, 1, 0.6, 1)
WorldShadows(#PB_Shadow_Additive)

;-Camera
CreateCamera(0, 0, 0 , 100 , 100)
MoveCamera(0,0,5,-10,#PB_Absolute)

;-Light
AmbientColor(RGB(105,105,105))
CreateLight(0,RGB(160,160,255),200,300,0)
CreateLight(1,RGB(255,127,0),-200,-200,200)

;- Ground
CreateMaterial(#GROUND, LoadTexture(#GROUND, "ground_diffuse.png"))
CreatePlane(#GROUND, 100, 100, 50, 50, 50, 50)
CreateEntity(#GROUND,MeshID(#GROUND),MaterialID(#GROUND) , 0, 0, 0)
EntityRenderMode(#GROUND, 0)
CreateEntityBody(#GROUND, #PB_Entity_PlaneBody, 0, 0.8, 1)

;- Main loop
showPhysics.b
Repeat
   If fullscreen = 0
      While WindowEvent() : Wend
   EndIf
   
   ; Manage camera views
   If ExamineKeyboard()
     If KeyboardReleased(#PB_Key_Space)
       ApplyEntityImpulse(#DICE,0,10,0)
     EndIf
     If KeyboardReleased(#PB_Key_P)
       showPhysics = 1-showPhysics
       If showPhysics
         WorldDebug(#PB_World_DebugBody)
       Else
         WorldDebug(#PB_World_DebugNone)
       EndIf         
     EndIf
   EndIf
   
  CameraLookAt(0,EntityX(#DICE) ,0 ,EntityZ(#DICE) )
   
  ; show it all
  RenderWorld()
   
  ; Flip buffers to avoid tearing
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)

;- Solids datas
DataSection:
  
dodeBiFace:
; Nb sommets / Nb faces
Data.i 8,6

; Vertices: pos / normals / uv
Data.f 0.357822,0.935172,0
Data.f 0,0.955423,0.295242        ;   0
Data.f 0.3,0.5

Data.f -0.355822,0.935172,0
Data.f 0,0.955423,-0.295242       ;   1
Data.f 0.7,0.5

Data.f 0.578350,0.578350,0.578350
Data.f 0.688191,0.587785,0.425325 ;   2
Data.f 0.2,0.2

Data.f 0,0.357822,0.935172
Data.f 0,0.850651,0.525731        ;   3
Data.f 0.5,0.0

Data.f -0.576350,0.578350,0.578350
Data.f 0,0.850651,0.525731        ;   4
Data.f 0.8,0.2

Data.f -0.576350,0.578350,-0.576350
Data.f 0,0.850651,-0.525731       ;   5
Data.f 0.8,0.8

Data.f 0,0.357822,-0.933172
Data.f 0,0.850651,-0.525731       ;   6
Data.f 0.5,0.999999

Data.f 0.578350,0.578350,-0.576350
Data.f 0,0.850651,-0.525731       ;   7
Data.f 0.2,0.8
  
; Faces
Data.i 0,3,2
Data.i 0,4,3
Data.i 0,1,4
Data.i 1,6,5
Data.i 1,7,6
Data.i 1,0,7

EndDataSection

Re: Iacta alea est

Posted: Wed Dec 02, 2015 5:52 pm
by DK_PETER
@Kelebrindae
Perfect, Kelebrindae. ;-)