modifying centerpoint of a mesh

Everything related to 3D programming
User avatar
A.D.
User
User
Posts: 98
Joined: Tue Oct 06, 2009 9:11 pm

modifying centerpoint of a mesh

Post by A.D. »

Hello,

is it possible to change the centerpoint of a mesh after loading or manual creation? I have models which are made only in world coordinates, so
i can't rotate them around their own axis. I need to modify their center and then move them back to their previous positions. I thought
i could do it with TransformMesh or with MoveEntity(2,0,0,0,#PB_Absolute|#PB_Local).


Alexander
Repeat
PureBasic
ForEver
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: modifying centerpoint of a mesh

Post by applePi »

Comtois provide a procedure ChangeMesh() on how to automatically centering a mesh here
http://www.purebasic.fr/english/viewtop ... 15#p417666
User avatar
A.D.
User
User
Posts: 98
Joined: Tue Oct 06, 2009 9:11 pm

Re: modifying centerpoint of a mesh

Post by A.D. »

Great ! Thank you!

One more thing: i need to put the mesh to its previous position. I tried to calculate the original position with EntityBoundingBox:

minx.f= EntityBoundingBox(#m2, #PB_Entity_MinBoundingBoxX|#PB_Entity_WorldBoundingBox)
maxx.f= EntityBoundingBox(#m2, #PB_Entity_MaxBoundingBoxX|#PB_Entity_WorldBoundingBox)
miny.f= EntityBoundingBox(#m2, #PB_Entity_MinBoundingBoxY|#PB_Entity_WorldBoundingBox)
maxy.f= EntityBoundingBox(#m2, #PB_Entity_MaxBoundingBoxY|#PB_Entity_WorldBoundingBox)
minz.f= EntityBoundingBox(#m2, #PB_Entity_MinBoundingBoxZ|#PB_Entity_WorldBoundingBox)
maxz.f= EntityBoundingBox(#m2, #PB_Entity_MaxBoundingBoxZ|#PB_Entity_WorldBoundingBox)
corx.f = (-maxx - minx) /2.0
cory.f = (-maxy - miny) /2.0
corz.f = (-maxz - minz) /2.0

But this does not help a lot :(
Repeat
PureBasic
ForEver
User avatar
A.D.
User
User
Posts: 98
Joined: Tue Oct 06, 2009 9:11 pm

Re: modifying centerpoint of a mesh

Post by A.D. »

solved
Repeat
PureBasic
ForEver
Fred
Administrator
Administrator
Posts: 18384
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: modifying centerpoint of a mesh

Post by Fred »

Just putting 'solved' without telling a bit about the solution isn't a good forum habit as it doesn't help other people who could have the same issue ;).
User avatar
A.D.
User
User
Posts: 98
Joined: Tue Oct 06, 2009 9:11 pm

Re: modifying centerpoint of a mesh

Post by A.D. »

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:

Code: Select all

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)

*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.


complete code:

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
Repeat
PureBasic
ForEver
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: modifying centerpoint of a mesh

Post by IdeasVacuum »

Where are the files cube?.mesh? Or, what are their sizes?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply