Page 1 of 1

sorting Mesh data with SortStructuredArray

Posted: Sat Jan 25, 2014 12:19 pm
by applePi
we may need someday to sort a mesh vertex positions data either on y or x or z. for a not connected just points mesh the figure shape will stay the same , it is only we moved a point data to another index. but for a connected points (triangles) the mesh may be under a chaos since what was connected in a special way now change their indices
in general we may find it useful someday in an unexpected case
if you want to not sorting the mesh data just comment line 52 SortStructuredArray ... and you will see the normal Barrel mesh
Barrel.mesh ,ninja.mesh are torn. but facial.mesh still the same

Code: Select all

Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)
Global ArrSize 
Global zz.f
Enumeration
  #mainwin
  #mesh
  #entity
  #tex
  #light
  #camera
EndEnumeration

InitEngine3D()
InitSprite()

ExamineDesktops()
OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "sort mesh data .... press Up / Down to move camera", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)
InitKeyboard()
InitMouse()
SetFrameRate(60)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Models", #PB_3DArchive_FileSystem)

CreateCamera(#camera, 0, 0, 100, 100)
MoveCamera(#camera, 0, 2, 24 )
CameraLookAt(#camera, 0, 0, 0)

CreateLight(#light,RGB(255,255,255),-100,40,30)
AmbientColor(RGB(255,255,255))


LoadTexture(#tex, "RustyBarrel.png")
CreateMaterial(#tex, TextureID(#tex))
;MaterialShadingMode(#tex, #PB_Material_Wireframe )

CreateMesh(#mesh, #PB_Mesh_PointList, #PB_Mesh_Dynamic)
SetMeshMaterial(#mesh, MaterialID(#tex))

LoadMesh(#mesh, "Barrel.mesh")
CreateEntity(#entity, MeshID(#mesh), MaterialID(#tex))

GetMeshData(#mesh, 0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(#mesh,0)-1)
ArrSize = ArraySize(MeshData())
Debug "ArraySize(MeshData()) =  " + Str(ArrSize)
For c=0 To ArrSize
  ;Debug MeshData(c)\y ; data not sorted on y yet
Next c

SortStructuredArray(MeshData(), #PB_Sort_Ascending , OffsetOf(PB_MeshVertex\y), TypeOf(PB_MeshVertex\y))
SetMeshData(#mesh,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(#mesh,0)-1)

For c=0 To ArrSize
  ;Debug MeshData(c)\y ; data sorted on y
Next c

Repeat
  
  Event = WindowEvent()
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Down)
    zz.f = 1
    MoveCamera(#camera, 0, 0, zz)
    ElseIf KeyboardPushed(#PB_Key_Up)
    zz.f =  - 1
    MoveCamera(#camera, 0, 0, zz)
  
  EndIf
  
  yy + 1
  RotateEntity(#entity, 0, yy, 0)
  
  
  RenderWorld()
  FlipBuffers()  

Until KeyboardPushed(#PB_Key_Escape) Or Event = #PB_Event_CloseWindow