TransformMesh troubles (and a solution)

Everything related to 3D programming
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

TransformMesh troubles (and a solution)

Post by applePi »

okay, i have found the solution while writing this post, so i have decided to continue this post (complaint) and added the solution at the end

when we do this :

Code: Select all

CreateCylinder(#cylinder,0.5, 0.5)
  ;TransformMesh(#Mesh, x, y, z, ScaleX, ScaleY, ScaleZ, RotateX, RotateY, RotateZ [, SubMesh])
  TransformMesh(#cylinder,0,0,0,  0.5, 10, 0.5, 0,0,0)
  CreateEntity(#cylinder, MeshID(#cylinder), MaterialID(4), 0, 2, 0)
such as in this code:

Code: Select all


Enumeration
#material = 20
#plane
#camera
#cylinder
EndEnumeration
#CameraSpeed = 0.2
Define.f KeyX, KeyY, MouseX, MouseY

InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()

ExamineDesktops()

OpenWindow(0,0,0,DesktopWidth(0), DesktopHeight(0),"arrow keys and mouse to move / rotate camera",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,DesktopWidth(0), DesktopHeight(0),1,0,0,#PB_Screen_WaitSynchronization)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)

Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

 ;-------------  Materials  -----------------------------------------
    CreateMaterial(3, LoadTexture(3, "ground_diffuse.png"))
    CreateMaterial(4, LoadTexture(4, "nskinrd.jpg"))
  
   ;--------------  Camera  -----------------------------------------

  CreateCamera(#Camera, 0, 0, 100, 100)
  MoveCamera(#camera, 0.03, 2.594, -3.774, #PB_Absolute)
  CameraLookAt(#camera, 0, 1, 0)


;- -------------  Light  ---------------------------------------------
  CreateLight(0, $FFFFFF, 0, 90, 0)
  AmbientColor(RGB(255,255,255))
;- -------------  World  -------------------------------------------
  ;WorldGravity(-9)
  SkyBox("desert07.jpg")
  ;----------------- the big wheel-----------------------------------------
  CreateMaterial(#material, LoadTexture(#material, "MRAMOR6X6.jpg"))
CreatePlane(#plane, 30, 30, 5, 5, 2, 2)
CreateEntity(#plane, MeshID(#plane), MaterialID(#material), 0,-2.5,0.0)

  CreateCylinder(#cylinder,0.5, 0.5)
  ;TransformMesh(#Mesh, x, y, z, ScaleX, ScaleY, ScaleZ, RotateX, RotateY, RotateZ [, SubMesh])
  TransformMesh(#cylinder,0,0,0,  0.5, 10, 0.5, 0,0,0)
  CreateEntity(#cylinder, MeshID(#cylinder), MaterialID(4), 0, 2, 0)
  
 NormalizeMesh(#cylinder)
 BuildMeshTangents(#cylinder)
 

Repeat
  WindowEvent()
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
  ExamineKeyboard()
  
        ; Use arrow keys and mouse to rotate camera and fly in/out
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed/2
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed/2
        Else
          KeyY = 0
        EndIf


  If KeyboardReleased(#PB_Key_C) ; check camera position
    Debug CameraX(#camera): Debug CameraY(#camera): Debug CameraZ(#camera)
  EndIf 
     
  RotateCamera(#camera, MouseY, MouseX, 0, #PB_Relative)
  MoveCamera  (#camera, KeyX, 0, KeyY)
  
  RenderWorld()
  
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow
and move the mouse down slightly the cylinder will disappear, also it will disappear in other camera views
this is also noticed by DK_PETER here with the cups examples: http://purebasic.fr/english/viewtopic.p ... 66#p464958

But if we do this:

Code: Select all

CreateCylinder(#cylinder,0.5, 0.5)
  CreateEntity(#cylinder, MeshID(#cylinder), MaterialID(4), 0, 2, 0)
  ScaleEntity(#cylinder, 0.5, 10, 0.5)
  MoveEntity(#cylinder, 0, 2,0, #PB_Absolute)
in the following code , it will work, we see the cylinder any where we look at it "fairly".

Code: Select all


Enumeration
#material = 20
#plane
#camera
#cylinder
EndEnumeration
#CameraSpeed = 0.5
Define.f KeyX, KeyY, MouseX, MouseY

InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()

ExamineDesktops()

OpenWindow(0,0,0,DesktopWidth(0), DesktopHeight(0),"arrow keys and mouse to move / rotate camera",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,DesktopWidth(0), DesktopHeight(0),1,0,0,#PB_Screen_WaitSynchronization)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)

Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

 ;-------------  Materials  -----------------------------------------
    CreateMaterial(3, LoadTexture(3, "ground_diffuse.png"))
    CreateMaterial(4, LoadTexture(4, "nskinrd.jpg"))
  
   ;--------------  Camera  -----------------------------------------

  CreateCamera(#Camera, 0, 0, 100, 100)
  MoveCamera(#camera, 0.03, 2.594, -3.774, #PB_Absolute)
  CameraLookAt(#camera, 0, 1, 0)


;- -------------  Light  ---------------------------------------------
  CreateLight(0, $FFFFFF, 0, 90, 0)
  AmbientColor(RGB(255,255,255))
;- -------------  World  -------------------------------------------
  
  SkyBox("desert07.jpg")
  ;----------------- the big wheel-----------------------------------------
  CreateMaterial(#material, LoadTexture(#material, "MRAMOR6X6.jpg"))
CreatePlane(#plane, 30, 30, 5, 5, 2, 2)
CreateEntity(#plane, MeshID(#plane), MaterialID(#material), 0,-2.5,0.0)

  CreateCylinder(#cylinder,0.5, 0.5)
  CreateEntity(#cylinder, MeshID(#cylinder), MaterialID(4), 0, 2, 0)
  ScaleEntity(#cylinder, 0.5, 10, 0.5)
  MoveEntity(#cylinder, 0, 2,0, #PB_Absolute)
 

Repeat
  WindowEvent()
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
  ExamineKeyboard()
  
        ; Use arrow keys and mouse to rotate camera and fly in/out
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed/2
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed/2
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed/2
        Else
          KeyY = 0
        EndIf


  If KeyboardReleased(#PB_Key_C) ; check camera position
    Debug CameraX(#camera): Debug CameraY(#camera): Debug CameraZ(#camera)
  EndIf 
     
  RotateCamera(#camera, MouseY, MouseX, 0, #PB_Relative)
  MoveCamera  (#camera, KeyX, 0, KeyY)
  
  RenderWorld()
  
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow
Just a show case, TransformMesh are extremely valuable tool, either we need to correct the code somehow or TransformMesh needs some modification

solution: we add UpdateMeshBoundingBox(#cylinder) after the TransformMesh in the first example above.