Cube Texturing in PB 5.50 beta 1

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

Cube Texturing in PB 5.50 beta 1

Post by applePi »

i have noticed that there is six submeshes in the cube in PB 5.50 beta 1, while it was 1 in previous versions (SubMeshCount(#mesh)), i guess this is a response to the many questions by the users who ask how to texture cube faces every face have its own texture, and the replies was usually very complex.
now it is easy to do that:

Code: Select all

SetMeshMaterial(#Mesh, MaterialID [, SubMesh])
Or

Code: Select all

SetEntityMaterial(#Entity, MaterialID[,submesh] )
if you tried to run the code in PB 5.42 then you will get one texture for all the cube. so to get multi textures use PB 5.50 Beta 1

Code: Select all

InitEngine3D() 
  InitSprite() 
  
  OpenWindow(0, 0, 0, 640, 480, "cube faces texturing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(0), 0, 0, 640, 480, 0, 0, 0)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
    CreateMaterial(0, LoadTexture(0, "MRAMOR6X6.jpg"))
    MaterialCullingMode(0, #PB_Material_NoCulling)

    CreateMaterial(1, LoadTexture(1, "ValetCoeur.jpg"))
    MaterialCullingMode(1, #PB_Material_NoCulling)
    
    CreateMaterial(2, LoadTexture(2, "ground_diffuse.png"))
    MaterialCullingMode(2, #PB_Material_NoCulling)
    
    CreateMaterial(3, LoadTexture(3, "Geebee2.bmp"))
    MaterialCullingMode(3, #PB_Material_NoCulling)
    
    CreateMaterial(4, LoadTexture(4, "fw12b.jpg"))
    MaterialCullingMode(4, #PB_Material_NoCulling)
    
    CreateMaterial(5, LoadTexture(5, "Wood.jpg"))
    MaterialCullingMode(5, #PB_Material_NoCulling)
    
    
  ; Light
  CreateLight(#PB_Any, RGB(25, 25, 180), -5, 10, 5, #PB_Light_Point)
  
  ; Camera
  CreateCamera(0, 0, 0, 100, 100)
  MoveCamera(0, 2, 1.5, 3, #PB_Absolute | #PB_Local)
  CameraLookAt(0, 0, 0, 0)
  
  ; Create the cube and attach it to the scene
  CreateCube(0, 1)
  ;texturing every submesh differently
    SetMeshMaterial(0,MaterialID(0),0) 
    SetMeshMaterial(0,MaterialID(1),1) 
    SetMeshMaterial(0,MaterialID(2),2) 
    SetMeshMaterial(0,MaterialID(3),3) 
    SetMeshMaterial(0,MaterialID(4),4) 
    SetMeshMaterial(0,MaterialID(5),5)
  CreateEntity(0, MeshID(0), #PB_Material_None)
   ;Debug SubMeshCount(0)
  ;or you can use this, the additional submesh parameter are not documented yet
    ;SetEntityMaterial(0,MaterialID(0),0) 
    ;SetEntityMaterial(0,MaterialID(1),1) 
    ;SetEntityMaterial(0,MaterialID(2),2) 
    ;SetEntityMaterial(0,MaterialID(3),3) 
    ;SetEntityMaterial(0,MaterialID(4),4) 
    ;SetEntityMaterial(0,MaterialID(5),5)
    
  Repeat
    RotateEntity(0, 0,1,0, #PB_Relative)
    RenderWorld()
    FlipBuffers()
  Until WaitWindowEvent(1) = #PB_Event_CloseWindow

i have edited the first example in my old post about add mesh to mesh since if we want to add a cube then we must consider its multi submehes
http://www.purebasic.fr/english/viewtop ... 36&t=61248
an additional CreateBox() with one submesh may be suitable

now suppose you want to convert a mesh with several submeshes to only one submesh (number zero) this is the approach, but note this will not copy the animations, i don't know how the animations is encoded.

works with PB 5.42 and PB 5.50 beta 1

Code: Select all

Declare CreateMatrix()
Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)
Global Dim MeshDataInd2.PB_MeshFace(0)
#mesh = 9
#cameraSpeed = 0.5
Define.f KeyX, KeyY, MouseX, MouseY

InitEngine3D() 
InitSprite() 
  InitKeyboard()
  InitMouse()
  ExamineDesktops()
  OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "multi subMeshes to one submesh ... . press ESC to exit", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Models", #PB_3DArchive_FileSystem)
  Parse3DScripts()
  
    
    CreateMaterial(1, LoadTexture(1, "Wood.jpg"))
    MaterialCullingMode(1, #PB_Material_NoCulling)
    CreateMaterial(2, LoadTexture(2, "RustyBarrel.png")) ;Dr_Bunsen_Head.jpg
    MaterialCullingMode(2, #PB_Material_NoCulling)
        
  ; Light
    CreateLight(0, RGB(255,255,255), 0, 100, 30)
    AmbientColor(RGB(200, 200, 200))
  
  ; Camera
  CreateCamera(0, 0, 0, 100, 100)
  MoveCamera(0, 2, 5, 20, #PB_Absolute | #PB_Local)
  CameraLookAt(0, 0, -5, 0)
  CameraBackColor(0, RGB(10, 200,200))
  
  CreateMatrix() ; call proc to merge several meshes, some have several submeshes

  ;texturing every submesh differently
   SetEntityMaterial(0,MaterialID(2),0) ; 
       
    Repeat
      
      Repeat
        Event = WindowEvent()
        If Event = #PB_Event_CloseWindow
          Quit = 1
        EndIf
      Until Event = 0 
      
      If ExamineMouse()
        MouseX = -MouseDeltaX()/20 
        MouseY = -MouseDeltaY()/20
      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
        
        
       If KeyboardReleased(#PB_Key_S)
          SaveMesh(0, "test.mesh")
        EndIf         
      EndIf
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(0, KeyX, 0, KeyY)
    RotateEntity(0, 0,0.5,0, #PB_Relative)
    RenderWorld()
    FlipBuffers()
  Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1 
  
  Procedure CreateMatrix()
    CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
    LoadMesh(#mesh, "facial.mesh")
    ;CreateCube(#mesh,5): TransformMesh(#mesh,0,0,0, 5,5,5,0,0,0)
    For i=0 To SubMeshCount(#mesh) -1
      Debug "submesh"+"("+i+") = " + MeshVertexCount(#mesh,i)
    Next
    Debug "total number of vertices = " + MeshVertexCount(#mesh)
   
  ;TransformMesh(#Mesh, x, y, z, ScaleX, ScaleY, ScaleZ, RotateX, RotateY, RotateZ [, SubMesh])
   TransformMesh(#mesh,0,0,0, 0.2,0.2,0.2,0,0,0)
  Debug "submesh count = " +SubMeshCount(#mesh) 
  AddSubMesh(#PB_Mesh_TriangleList)
  For subMesh=0 To SubMeshCount(#mesh)-1
  GetMeshData(#mesh,subMesh, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate | #PB_Mesh_Normal , 0, MeshVertexCount(#mesh,subMesh)-1)
  GetMeshData(#mesh,subMesh, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(#mesh, subMesh)-1)
  ArrSize = ArraySize(MeshData())
  ;AddSubMesh(#PB_Mesh_TriangleList)
  ee+1
  For c=0 To ArrSize
      x.f = MeshData(c)\x 
      y.f = MeshData(c)\y
      z.f = MeshData(c)\z
      MeshVertexPosition(x,y,z)
      MeshVertexNormal(MeshData(c)\NormalX, MeshData(c)\NormalY, MeshData(c)\NormalZ)
      MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v) 
  Next   
   
   ArrSizeInd = ArraySize(MeshDataInd()) 
   ;fill the big MeshDataInd2 with the info in Local MeshDataInd but rearranged sequently as we want one mesh instead of several submehes
   ReDim MeshDataInd2.PB_MeshFace(j+ArrSizeInd)
   For i=0 To ArrSizeInd
     MeshDataInd2(j)\Index = MeshDataInd(i)\Index + subMeshVertxIncr
     j+1
   Next
   
   subMeshVertxIncr = subMeshVertxIncr + MeshVertexCount(#mesh,subMesh)
   
   Next
   
   ;after finishing loading the MeshDataInd2 with faces connections data we execute it and weaving the triangles (faces)
   ArrSizeInd2 = ArraySize(MeshDataInd2()) 
   ;Debug ArrSizeInd2
   For j=0 To  ArrSizeInd2 Step 3
     MeshFace(MeshDataInd2(j)\Index, MeshDataInd2(j+1)\Index,MeshDataInd2(j+2)\Index)
     
   Next
   
   FreeMesh(#mesh) 
   Debug "total vertices number after uniting submeshes = " + subMeshVertxIncr  
   Debug "submesh count after uniting merging submehes = " +SubMeshCount(0) 
      
  
  FinishMesh(#True)
      
  CreateEntity(0, MeshID(0), MaterialID(1), 0,-10,0)
  
  ;CreateEntityBody(0, #PB_Entity_StaticBody, 1, 0.2, 1)

EndProcedure
press 'S' to save "test.mesh": its size is 75 KB while the original "facial.mesh" size is 425 KB, this is due to the animations info in the original mesh, look FacialAnimation.PB in the official Examples\3D\Demos