Yes, Fred, you're right, i should have posted it. Sorry folks ! To get the original position of the mesh before centering it i made a temporary entity of the mesh and used the EntityBoundingBox() command to get the boundaries:
*NOTE: UpdateMeshBoundingBox() should be called after using Comtois ChangeMesh() procedure on the mesh. If the box isn't updated the entity may dissapear when the cam gets to close to the mesh.
Code: Select all
;
; ------------------------------------------------------------
;
; PureBasic - Mesh (Skeleton Animation)
;
; (c) 2002 - Fantaisie Software
;
; ------------------------------------------------------------
;
#PB_Material_SpecularColor = 1
#PB_Material_AmbientColor = 2
IncludeFile "Screen3DRequester.pb"
Define.f KeyX, KeyY, MouseX, MouseY
#CameraSpeed = 8
#RobotMesh = 0
#RobotTexture = 0
#Robot = 0
#m2=4
#m3=5
Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)
Procedure ChangeMesh()
Protected.f minx, miny, minz, maxx, maxy, maxz
Protected.f corx, cory, corz
minx = 999999
miny = 999999
minz = 999999
maxx = -999999
maxy = -999999
maxz = -999999
For i=0 To ArraySize(MeshData())
If MeshData(i)\x > maxx
maxx = MeshData(i)\x
EndIf
If MeshData(i)\x < minx
minx = MeshData(i)\x
EndIf
If MeshData(i)\y > maxy
maxy = MeshData(i)\y
EndIf
If MeshData(i)\y < miny
miny = MeshData(i)\y
EndIf
If MeshData(i)\z > maxz
maxz = MeshData(i)\z
EndIf
If MeshData(i)\z < minz
minz = MeshData(i)\z
EndIf
Next
corx = (-maxx - minx) /2.0
cory = (-maxy - miny) /2.0
corz = (-maxz - minz) /2.0
; MessageRequester("", "minx = " + StrF(minx,2) + Chr(10) +
; "miny = " + StrF(miny,2) + Chr(10) +
; "minz = " + StrF(minz,2) + Chr(10) +
; "maxx = " + StrF(maxx,2) + Chr(10) +
; "maxy = " + StrF(maxy,2) + Chr(10) +
; "maxz = " + StrF(maxz,2) + Chr(10) +
; "Correction x = " + StrF(corx,2) + Chr(10) +
; "Correction y = " + StrF(cory,2) + Chr(10) +
; "Correction z = " + StrF(corz,2), #PB_MessageRequester_Ok )
For i=0 To ArraySize(MeshData())
MeshData(i)\x + corx
MeshData(i)\y + cory
MeshData(i)\z + corz
Next
EndProcedure
If InitEngine3D()
Add3DArchive("textures/", #PB_3DArchive_FileSystem)
InitSprite()
InitKeyboard()
InitMouse()
If Screen3DRequester()
Parse3DScripts()
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 0, 60,-100,#PB_Absolute)
;CameraBackColor(0, RGB(0, 0, 128))
;WorldDebug(#PB_World_DebugEntity)
LoadMesh (#RobotMesh , "cube1.mesh")
LoadMesh (#m2 , "cube2.mesh")
LoadMesh (#m3 , "cube3.mesh")
CreateEntity(#m2, MeshID(#m2), #PB_Material_None)
minx.f= EntityBoundingBox(#m2, #PB_Entity_MinBoundingBoxX|#PB_Entity_LocalBoundingBox)
maxx.f= EntityBoundingBox(#m2, #PB_Entity_MaxBoundingBoxX|#PB_Entity_LocalBoundingBox)
miny.f= EntityBoundingBox(#m2, #PB_Entity_MinBoundingBoxY|#PB_Entity_LocalBoundingBox)
maxy.f= EntityBoundingBox(#m2, #PB_Entity_MaxBoundingBoxY|#PB_Entity_LocalBoundingBox)
minz.f= EntityBoundingBox(#m2, #PB_Entity_MinBoundingBoxZ|#PB_Entity_LocalBoundingBox)
maxz.f= EntityBoundingBox(#m2, #PB_Entity_MaxBoundingBoxZ|#PB_Entity_LocalBoundingBox)
corx.f = EntityBoundingBox(#m2, #PB_Entity_MinBoundingBoxX|#PB_Entity_LocalBoundingBox) + ((maxx.f-minx.f)/2.0)
cory.f = EntityBoundingBox(#m2, #PB_Entity_MinBoundingBoxY|#PB_Entity_LocalBoundingBox) + ((maxy.f-miny.f)/2.0)
corz.f = EntityBoundingBox(#m2, #PB_Entity_MinBoundingBoxZ|#PB_Entity_LocalBoundingBox) + ((maxz.f-minz.f)/2.0)
;Debug corx : Debug cory : Debug corz
FreeEntity(#m2)
GetMeshData(#m2,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(#m2)-1)
GetMeshData(#m2,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(#m2)-1)
ChangeMesh()
SetMeshData(#m2,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(#m2)-1)
;NormalizeMesh(#m2)
UpdateMeshBoundingBox(#m2)
CreateEntity(#Robot, MeshID(#RobotMesh), #PB_Material_None)
CreateEntity(#m2, MeshID(#m2), #PB_Material_None)
CreateEntity(#m3, MeshID(#m3), #PB_Material_None)
; ScaleEntity(#m2, 0.2,0.2,0.2)
; ScaleEntity(#m3, 0.2,0.2,0.2)
; ScaleEntity(#RobotMesh, 0.2,0.2,0.2)
MoveEntity(#m2,corx.f,cory.f,corz.f, #PB_Absolute)
CameraLookAt(0,EntityX(#m2),EntityY(#m2),EntityZ(#m2))
Repeat
Screen3DEvents()
If ExamineMouse()
MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
EndIf
If ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left)
KeyX = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Right)
KeyX = #CameraSpeed
Else
KeyX = 0
EndIf
If KeyboardPushed(#PB_Key_Up)
KeyY = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Down)
KeyY = #CameraSpeed
Else
KeyY = 0
EndIf
EndIf
RotateEntity(#m2, 0, 0.3, 0, #PB_Relative)
RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
MoveCamera (0, KeyX, 0, KeyY)
RenderWorld()
Screen3DStats()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
EndIf
Else
MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf
End