Un code permettant de créer un cube mesh composé de 6 submeshs matérialisant chacune des faces.
: CreateCubeMesh(1) 1 est l'identifiant du mesh.
Les faces sont composées de submeshs numérotés de 0 à 5.
va permettre de texturer chacune de ces faces.
).
Vous pouvez aussi utiliser la fonction ScaleEntity() pour redimensionner le cube et en faire un parallélépipède par exemple.
■ Avec ce code, les faces supérieur et inférieur d'un cube seront texturées avec la texture Caisse.png. Les autres faces seront texturées avec la texture Geebee2.bmp. Ces textures se trouvent dans le dossier d'installation de PureBasic.
Le cube tombe sur un sol. Pour le faire rebondir utiliser la touche Espace.
Escape permet de quitter l'application.
Code : Tout sélectionner
EnableExplicit
Global Event, Collide.b
Declare CreateCubeMesh(Mesh.i, Size.f = 1.0)
Declare AddMesh(Mesh, NewX.f=0 , NewY.f=0, NewZ.f=0, ScaleX.f=1, ScaleY.f=1, ScaleZ.f=1, RotateX.f=0, RotateY.f=0, RotateZ.f=0)
InitEngine3D() : InitKeyboard() : InitSprite()
OpenWindow(0,0,0,1024,768,"Demo CreateCubeMesh")
OpenWindowedScreen(WindowID(0),0,0,1024,768)
;Les textures se trouvent dans le dossier d'installation de PureBasic
Add3DArchive(#PB_Compiler_Home + "Examples\3D\Data\Textures", #PB_3DArchive_FileSystem)
;Caméra, Lumiere & Ombre
CreateCamera(0, 0, 0, 100, 100)
CameraBackColor(0, RGB(245, 222, 179))
MoveCamera(0, 5, 15, 60)
CameraLookAt(0, 0, 0, 0)
CreateLight(0, RGB(255, 255, 255), -100, 200, 100)
WorldShadows(#PB_Shadow_Additive)
;Création des materiaux (Les textures)
CreateMaterial(0, TextureID(LoadTexture(-1, "Dirt.jpg"))) ;Sol
CreateMaterial(1, TextureID(LoadTexture(-1, "Caisse.png"))) ;Dessus et dessous du cube
CreateMaterial(2, TextureID(LoadTexture(-1, "Geebee2.bmp")));Les autres faces
;Creation du sol ((Identifiant 0))
CreatePlane(0, 500, 500, 100, 100, 25, 25)
CreateEntity(0, MeshID(0), MaterialID(0))
CreateEntityBody(0, #PB_Entity_StaticBody, 1, 0.1, 1)
;Création du cube (Identifiant 1)
; 1 - Création du mesh : CreateCubeMesh(#Mesh, Size.f)
CreateCubeMesh(1)
; 2 - Application du material sur chaque face du mesh
SetMeshMaterial(1, MaterialID(1), 0) ;UP face
SetMeshMaterial(1, MaterialID(1), 1) ;Down face
SetMeshMaterial(1, MaterialID(2), 2) ;Front face
SetMeshMaterial(1, MaterialID(2), 3) ;Back face
SetMeshMaterial(1, MaterialID(2), 4) ;Left face
SetMeshMaterial(1, MaterialID(2), 5) ;Right face
; 3 - Creation de l'entité (physic)
CreateEntity(1, MeshID(1), #PB_Material_None, 0, 10, 10)
CreateEntityBody(1, #PB_Entity_BoxBody, 1, 1, 1)
ScaleEntity(1, 3, 3, 3)
While #True
;Evenement window
Repeat
Event = WindowEvent()
Select Event
Case #PB_Event_CloseWindow
End
EndSelect
Until Event = 0
;Evenement clavier
If ExamineKeyboard()
If KeyboardReleased(#PB_Key_Escape)
Break
EndIf
If KeyboardReleased(#PB_Key_Space) And Collide = #True
ApplyEntityImpulse(1, 0, 15, 0)
EndIf
EndIf
;Le cube est en l'air ou Collision avec le sol ?
If Not EntityCollide(0, 1)
RotateEntity(1, 1, 1, 1, #PB_Relative)
Collide = #False
Else
Collide = #True
EndIf
RenderWorld()
FlipBuffers()
Wend
;Ce qui suit peut etre placé dans un include (module ou pas)
;Result = CreateCubeMesh(#Mesh, Size)
Procedure CreateCubeMesh(Mesh.i, Size.f = 1.0)
Protected Dim SubMesh(5)
Protected CubeMesh = CreateMesh(#PB_Any, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
If Mesh = #PB_Any
Mesh = CreateMesh(#PB_Any, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
Else
CreateMesh(Mesh, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
EndIf
AddMesh(CreatePlane(-1, Size, Size, 1, 1, 1, 1), 0.0, Size/2, 0.0, 1, 1, 1, 0, 180, 0) ;SubMesh 0 UP face
AddMesh(CreatePlane(-1, Size, Size, 1, 1, 1, 1), 0.0, -Size/2, 0.0, 1, 1, 1, 180, 180, 0) ;SubMesh 1 Down face
AddMesh(CreatePlane(-1, Size, Size, 1, 1, 1, 1), 0.0, 0, Size/2, 1, 1, 1, -90, 180, 0) ;SubMesh 2 FRont face
AddMesh(CreatePlane(-1, Size, Size, 1, 1, 1, 1), 0.0, 0, -Size/2, 1, 1, 1, -90, 0, 0) ;SubMesh 3 BacK face
AddMesh(CreatePlane(-1, Size, Size, 1, 1, 1, 1), -Size/2, 0, 0.0, 1, 1, 1, -90, 0, -90) ;SubMesh 4 LeFt face
AddMesh(CreatePlane(-1, Size, Size, 1, 1, 1, 1), Size/2, 0, 0.0, 1, 1, 1, -90, 0, 90) ;SubMesh 5 RighT face
FinishMesh(#True)
ProcedureReturn Mesh
EndProcedure
Procedure AddMesh(Mesh, NewX.f=0 , NewY.f=0, NewZ.f=0, ScaleX.f=1, ScaleY.f=1, ScaleZ.f=1, RotateX.f=0, RotateY.f=0, RotateZ.f=0)
Protected Dim MeshData.PB_MeshVertex(0)
Protected Dim MeshDataInd.PB_MeshFace(0)
Protected ArrSize, ArrSizeInd, c, i, x.f, y.f, z.f
TransformMesh(Mesh, NewX,NewY,NewZ, ScaleX,ScaleY,ScaleZ, RotateX,RotateY,RotateZ)
GetMeshData(Mesh,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate, 0, MeshVertexCount(Mesh)-1)
GetMeshData(Mesh,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(Mesh, 0)-1)
ArrSize = ArraySize(MeshData())
For c=0 To ArrSize
x = MeshData(c)\x
y = MeshData(c)\y
z = MeshData(c)\z
MeshVertexPosition(x,y,z)
MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v)
Next
ArrSizeInd = ArraySize(MeshDataInd())
For i=0 To ArrSizeInd Step 3
MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
Next
AddSubMesh(#PB_Mesh_TriangleList)
EndProcedure
La procédure AddMesh() est pratique pour créer des meshs personnalisés.