SetMeshData

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

SetMeshData

Post by applePi »

how to use SetMeshData(), GetMeshData() in the PB 5.20 beta1
thanks
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: SetMeshData

Post by Comtois »

GetMeshData() need 1 ou 2 arrays
First array to get vertices Dim MyArray.PB_MeshVertex(0) , GetMEshData() will fill it (use constants #PB_Mesh_Vertex | #PB_Mesh_Normal | #PB_Mesh_Color | #PB_Mesh_UVCoordinate)


Second array to get index (Triangle / Face) Dim MyArray.PB_MeshFace(0) and call a second time GetMeshData() with this array and constant #PB_Mesh_Face

Here is an example

Code: Select all

IncludeFile "Screen3DRequester.pb"

#CameraSpeed = 1
#NbX=30
#NbZ=30 

Global.f AngleVague, WaveFrequency, WavePeriodX, WavePeriodZ, WaveAmplitude
WaveFrequency=3  ;=waves/second
WavePeriodX  =9  ;=1/Wave lenght
WavePeriodZ  =11 ;=1/Wave lenght
WaveAmplitude=3

Define.f KeyX, KeyY, MouseX, MouseY

Global Dim MeshData.PB_MeshVertex(0)

Declare UpdateMatrix()
Declare CreateMatrix()

If InitEngine3D()
  
  Add3DArchive("Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive("Data/Packs/skybox.zip", #PB_3DArchive_Zip)
  Add3DArchive("Data/Scripts", #PB_3DArchive_FileSystem)
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    ;-Material  
    GetScriptMaterial(1, "Scene/GroundBlend")
    MaterialCullingMode(1, 1)
    
    ;-Mesh
    CreateMatrix()
    
    ;-Camera
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0,0,50,80, #PB_Absolute)
    CameraLookAt(0, 0, 0, 0)
    CameraBackColor(0, RGB(90, 0, 0))
    
    ;-Light
    CreateLight(0, RGB(255, 255, 255), 20, 150, 120)
    AmbientColor(RGB(90, 90, 90))
    
    ;- Skybox
    SkyBox("stevecube.jpg")
    
    Repeat
      Screen3DEvents()
      
      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
      
      If ExamineMouse()
        MouseX = -(MouseDeltaX()/10)
        MouseY = -(MouseDeltaY()/10)
      EndIf
      MoveCamera  (0, KeyX, 0, KeyY)
      RotateCamera(0,  MouseY, MouseX, 0, #PB_Relative)  
      
      ; Waves
      UpdateMatrix()
      AngleVague = AngleVague+WaveFrequency
      
      RenderWorld()
      
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

;-Procédures
Procedure DrawMatrix()
  Protected.l a, b, c, Nb
  Protected.w P1, P2, P3, P4
  
  For b=0 To #Nbz
    For a=0 To #NbX
      ;les coordonnées de vertex
      y.f=Sin(Radian((AngleVague+a*WavePeriodX+b*WavePeriodZ)))*WaveAmplitude 
      MeshVertexPosition(a - #NbX/2, y, b - #Nbz/2) 
      MeshVertexNormal(0,1,0) 
      MeshVertexTextureCoordinate(a/#NbX, b/#Nbz)
    Next a
  Next b
  
  Nb=#NbX+1
  For b=0 To #NbZ-1
    For a=0 To #NbX-1
      P1=a+(b*Nb)
      P2=P1+1
      P3=a+(b+1)*Nb
      P4=P3+1
      
      MeshFace(P3, P2, P1)
      MeshFace(P2, P3, P4)
    Next
  Next
EndProcedure  

Procedure CreateMatrix()
  
  CreateMesh(0, #PB_Mesh_TriangleList, #True)
  DrawMatrix()
  FinishMesh(#True)
  SetMeshMaterial(0, MaterialID(1))
  
  CreateNode(0)
  CreateEntity(0, MeshID(0), #PB_Material_None)
  ScaleEntity(0, 2, 2, 2)
  GetMeshData(0,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(0)-1)
EndProcedure

Procedure UpdateMatrix()
  Protected.l a, b, c
  
  For b=0 To #Nbz
    For a=0 To #NbX
      ;les coordonnées de vertex
      MeshData(c)\y=Sin(Radian((AngleVague+a*WavePeriodX+b*WavePeriodZ)))*WaveAmplitude 
      c + 1
    Next a
  Next b
  SetMeshData(0,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(0)-1)
EndProcedure
Please correct my english
http://purebasic.developpez.com/
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: SetMeshData

Post by Comtois »

This example show how to get all datas from robot.mesh and create a new one.

There is a bug with PB_MeshVertex, i will have a look.

Code: Select all

IncludeFile "Screen3DRequester.pb"

#CameraSpeed = 1


Define.f KeyX, KeyY, MouseX, MouseY

;- Bug in PB Structure !
Structure PB_MeshVertex2
  x.f
  y.f
  z.f
  NormalX.f
  NormalY.f
  NormalZ.f
  Color.l
  u.f
  v.f
EndStructure

Dim MeshVertexData.PB_MeshVertex2(0)
Dim MeshIndexData.PB_MeshFace(0)

If InitEngine3D()
  
  Add3DArchive("Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive("Data/Models", #PB_3DArchive_FileSystem)
  Add3DArchive("Data/Packs/skybox.zip", #PB_3DArchive_Zip)
  Add3DArchive("Data/Scripts", #PB_3DArchive_FileSystem)
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    ;-Material  
    CreateMaterial(0, LoadTexture(0, "r2skin.jpg"))
    
    ;-Mesh
    LoadMesh(0, "robot.mesh")
    
    GetMeshData(0, 0, MeshVertexData(), #PB_Mesh_Vertex | #PB_Mesh_Normal | #PB_Mesh_UVCoordinate, 0, MeshVertexCount(0)-1)
    GetMeshData(0, 0, MeshIndexData(), #PB_Mesh_Face, 0, MeshIndexCount(0)-1)
    
    ;-Copy Robot's Mesh 
    CreateMesh(1)
    For i=0 To ArraySize(MeshVertexData())
      MeshVertexPosition(MeshVertexData(i)\x,MeshVertexData(i)\y,MeshVertexData(i)\z) 
      MeshVertexNormal(MeshVertexData(i)\NormalX,MeshVertexData(i)\NormalY,MeshVertexData(i)\NormalZ) 
      MeshVertexTextureCoordinate(MeshVertexData(i)\u,MeshVertexData(i)\v)  
    Next
    For i=0 To ArraySize(MeshIndexData())
      MeshIndex(MeshIndexData(i)\Index) 
    Next
    FinishMesh(#True)
    
    CreateEntity(0, MeshID(0), MaterialID(0), 60, 0, 0)
    CreateEntity(1, MeshID(1), MaterialID(0),  0, 0, 0) 
    
    ;-Camera
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0,0,50,80, #PB_Absolute)
    CameraLookAt(0, 0, 0, 0)
    CameraBackColor(0, RGB(90, 0, 0))
    
    ;-Light
    CreateLight(0, RGB(255, 255, 255), 20, 150, 120)
    AmbientColor(RGB(90, 90, 90))
    
    ;- Skybox
    SkyBox("stevecube.jpg")
    
    Repeat
      Screen3DEvents()
      
      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
      
      If ExamineMouse()
        MouseX = -(MouseDeltaX()/10)
        MouseY = -(MouseDeltaY()/10)
      EndIf
      MoveCamera  (0, KeyX, 0, KeyY)
      RotateCamera(0,  MouseY, MouseX, 0, #PB_Relative)  
      
      RenderWorld()
      
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End
Please correct my english
http://purebasic.developpez.com/
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: SetMeshData

Post by applePi »

this is surely a very big addition thanks very much. and this is an answer to the questions about how to change specific vertices positions ; by changing its corresponding values in the array which have the mesh data then saving that array to the mesh, much better by one million than the previous approach which needs to create the whole mesh from scratch.
i suggest to add the first example as MeshManualFlag_v2.pb so the users can compare the two approaches
regards
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: SetMeshData

Post by Fred »

You should be able to directly use SetMeshData() as well after having changed the mesh vertices (instead of MeshVertexPosition() and such).
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: SetMeshData

Post by Comtois »

this example show how to use #PB_Entity_NbSubEntities, FetchEntityMaterial() and SetEntityMaterial() when an entity has SubEntity.

Code: Select all

IncludeFile "Screen3DRequester.pb"

#CameraSpeed = 1

Define.f KeyX, KeyY, MouseX, MouseY

;- Bug in PB Structure !
Structure PB_MeshVertex2
  x.f
  y.f
  z.f
  NormalX.f
  NormalY.f
  NormalZ.f
  Color.l    
  u.f
  v.f
EndStructure

Dim MeshVertexData.PB_MeshVertex2(0)
Dim MeshIndexData.PB_MeshFace(0)

If InitEngine3D()
  
  Add3DArchive("Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive("Data/Models", #PB_3DArchive_FileSystem)
  Add3DArchive("Data/Packs/skybox.zip", #PB_3DArchive_Zip)
  Add3DArchive("Data/Scripts", #PB_3DArchive_FileSystem)
  Add3DArchive("Data/Packs/Sinbad.zip", #PB_3DArchive_Zip)
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    
    ;-Mesh
    LoadMesh(0, "sinbad.mesh")
    CreateEntity(0, MeshID(0), #PB_Material_None)
    ;-Copy Robot's Mesh
    CreateMesh(1)
    
    NbSub = GetEntityAttribute(0, #PB_Entity_NbSubEntities)-1
    
    For SubEntity=0 To NbSub
      
      GetMeshData(0, SubEntity, MeshVertexData(), #PB_Mesh_Vertex | #PB_Mesh_Normal | #PB_Mesh_UVCoordinate, 0, MeshVertexCount(0,SubEntity)-1)
      GetMeshData(0, SubEntity, MeshIndexData(), #PB_Mesh_Face, 0, MeshIndexCount(0,SubEntity)-1)
      
      
      For i=0 To MeshVertexCount(0,SubEntity)-1
        MeshVertexPosition(MeshVertexData(i)\x,MeshVertexData(i)\y,MeshVertexData(i)\z)
        MeshVertexNormal(MeshVertexData(i)\NormalX,MeshVertexData(i)\NormalY,MeshVertexData(i)\NormalZ)
        MeshVertexTextureCoordinate(MeshVertexData(i)\u,MeshVertexData(i)\v) 
      Next
      For i=0 To MeshIndexCount(0,SubEntity)-1
        MeshIndex(MeshIndexData(i)\Index)
      Next
      If SubEntity < NbSub
        AddSubMesh()
      EndIf  
    Next
    
    FinishMesh(#True)
    
    CreateEntity(1, MeshID(1), #PB_Material_None,  20, 0, 0)
    For SubEntity=0 To NbSub
      Mat = FetchEntityMaterial(0, #PB_Any, 0, SubEntity)
      SetEntityMaterial(1, MaterialID(Mat), SubEntity)
    Next
    
    ;-Camera
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0,0,50,80, #PB_Absolute)
    CameraLookAt(0, 0, 0, 0)
    CameraBackColor(0, RGB(90, 0, 0))
    
    ;-Light
    CreateLight(0, RGB(255, 255, 255), 20, 150, 120)
    AmbientColor(RGB(90, 90, 90))
    
    ;- Skybox
    SkyBox("stevecube.jpg")
    
    Repeat
      Screen3DEvents()
      
      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
      
      If ExamineMouse()
        MouseX = -(MouseDeltaX()/10)
        MouseY = -(MouseDeltaY()/10)
      EndIf
      MoveCamera  (0, KeyX, 0, KeyY)
      RotateCamera(0,  MouseY, MouseX, 0, #PB_Relative) 
      
      RenderWorld()
      
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End
Please correct my english
http://purebasic.developpez.com/
Post Reply