Mesh deformation in PB5.20?

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

Re: Mesh deformation in PB5.20?

Post by applePi »

i have done some tests today and found that the vertex is not the index, in the pyramid example above there are 4 vertices which make 12 indexes from 0 to 11, ie 3 for every face, and every vertex can have several indexes like this picture:
Image
in the following program press S to show a list of indices attach to every vertex. so it is useless to change the index coordinates since every vertex have several indices ie Labels. if someone else have more suggestions please post. it is good Psychophanta posted his question since now i know more about some secrets. and Comtois have done good job. since this is a beta version and needs any suggestion. i think the current situation are good.
@Comtois: in line 88 RotateEntity(0,0,rot,0) uncomment it: i can't make the pyramid rotate around itself but it is rotating in a big circle

Code: Select all

Enumeration
   #MESH
   #LIGHT
   #CAMERA
   #mainwin
 EndEnumeration
Structure Vector3
  x.f
  y.f
  z.f
EndStructure
Define.Vector3 v1, v2, v3 

Global x.f = 0
Global y.f = 10
Global z.f = -30
Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)
ExamineDesktops()
If OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "press S to show indices, press Arrows change vertex 3 ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)


Define.f KeyX, KeyY
Declare UpdateMatrix()
Declare CreateMatrix()
Declare DrawMatrix()

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
  Add3DArchive("/", #PB_3DArchive_FileSystem)
  
  InitSprite()
  InitKeyboard()
  OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0)-60, 0, 0, 0)
    
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    MaterialShadingMode(0, #PB_Material_Wireframe     )
    MaterialCullingMode(0, #PB_Material_NoCulling)
    CreateMatrix()
            
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, 50, #PB_Absolute)
    CameraFOV(0, 70)
    CameraBackColor(0, $330000)
    CameraLookAt(0,0,0,0)
        
    CreateLight(0, RGB(255,255,255), 10, 60, -10)
    AmbientColor(RGB(90, 90, 90))
       
    Repeat
      Event = WindowEvent()
                  
      If ExamineKeyboard()
        
        If KeyboardReleased(#PB_Key_F2)
          MaterialShadingMode(0, #PB_Material_Wireframe)
        ElseIf KeyboardReleased(#PB_Key_F3)
          MaterialShadingMode(0, #PB_Material_Solid)
        EndIf
        
        If KeyboardPushed(#PB_Key_Right)
          x + 0.4 :UpdateMatrix()
        ElseIf KeyboardPushed(#PB_Key_Left) 
          x - 0.4 :UpdateMatrix()
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          y + 0.4:UpdateMatrix()
        ElseIf KeyboardPushed(#PB_Key_Down)
          y - 0.4 :UpdateMatrix()
        ElseIf KeyboardReleased(#PB_Key_S)
          ;SaveMesh(0,"pyramid.mesh")
          For i=0 To 11
          vx = MeshData(MeshDataInd(i)\Index)\x
          vy = MeshData(MeshDataInd(i)\Index)\y
          vz = MeshData(MeshDataInd(i)\Index)\z
          
          Debug "index "+Str(indx) +"  = " + Str(vx)+"  "+Str(vy)+"  " +Str(vz)+"  "
          indx + 1
        Next
        EndIf
        
      EndIf
      rot.f+0.6
      ;RotateEntity(0,0,rot,0)
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End


Procedure DrawMatrix()
    MeshVertexPosition(-20, -20, -20) ;vertex 0
    MeshVertexColor(RGB(255, 0, 0))
    MeshVertexPosition(20, -20, -20)  ; vertex 1
    MeshVertexColor(RGB(0, 255, 0))
    MeshVertexPosition(0, -20, -40)   ; vertex 2
    MeshVertexColor(RGB(255, 220, 0))
        
    MeshVertexPosition(0, 10,-30)     ; vertex 3
    MeshFace(0, 1, 2)
    MeshFace(1, 2, 3)
    MeshFace(1, 0, 3)
    MeshFace(0, 3, 2)
    
EndProcedure  
Procedure CreateMatrix()
  
  CreateMesh(0, #PB_Mesh_TriangleList, #True)
  DrawMatrix()
  FinishMesh(#True)
  SetMeshMaterial(0, MaterialID(0))
    
  CreateEntity(0, MeshID(0), #PB_Material_None)
  
  ScaleEntity(0, 2, 2, 2)
  GetMeshData(0,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(0)-1)
  GetMeshData(0,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(0, 0)-1)
EndProcedure


Procedure UpdateMatrix()
      MeshData(3)\x = x
      MeshData(3)\y = y
      MeshData(3)\z = z
  
      ;vx = MeshData(MeshDataInd(10)\Index)\x
      ;vy = MeshData(MeshDataInd(10)\Index)\y
      ;vz = MeshData(MeshDataInd(10)\Index)\z
      ;MessageRequester("", Str(vx)+"  "+Str(vy)+"  " +Str(vz)+"  ")
      ;Debug Str(vx)+"  "+Str(vy)+"  " +Str(vz)+"  "
  ;MessageRequester("", Str(MeshIndexCount(0, 0)-1))
        
      SetMeshData(0,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(0)-1)
      SetMeshData(0,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshVertexCount(0)-1)
EndProcedure
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: Mesh deformation in PB5.20?

Post by Comtois »

applePi wrote:@Comtois: in line 88 RotateEntity(0,0,rot,0) uncomment it: i can't make the pyramid rotate around itself but it is rotating in a big circle
your mesh is not centered on the y-axis

Code: Select all

Enumeration
   #MESH
   #LIGHT
   #CAMERA
   #mainwin
 EndEnumeration
Structure Vector3
  x.f
  y.f
  z.f
EndStructure
Define.Vector3 v1, v2, v3

Global x.f = 0
Global y.f = 10
Global z.f = -30
Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)
ExamineDesktops()
If OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "press S to show indices, press Arrows change vertex 3 ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)


Define.f KeyX, KeyY
Declare UpdateMatrix()
Declare CreateMatrix()
Declare DrawMatrix()

If InitEngine3D()
 
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
  Add3DArchive("/", #PB_3DArchive_FileSystem)
 
  InitSprite()
  InitKeyboard()
  OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0)-60, 0, 0, 0)
   
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    MaterialShadingMode(0, #PB_Material_Wireframe     )
    MaterialCullingMode(0, #PB_Material_NoCulling)
    CreateMatrix()
           
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, 90, #PB_Absolute)
    CameraFOV(0, 70)
    CameraBackColor(0, $330000)
    CameraLookAt(0,0,0,0)
       
    CreateLight(0, RGB(255,255,255), 10, 60, -10)
    AmbientColor(RGB(90, 90, 90))
       
    Repeat
      Event = WindowEvent()
                 
      If ExamineKeyboard()
       
        If KeyboardReleased(#PB_Key_F2)
          MaterialShadingMode(0, #PB_Material_Wireframe)
        ElseIf KeyboardReleased(#PB_Key_F3)
          MaterialShadingMode(0, #PB_Material_Solid)
        EndIf
       
        If KeyboardPushed(#PB_Key_Right)
          x + 0.4 :UpdateMatrix()
        ElseIf KeyboardPushed(#PB_Key_Left)
          x - 0.4 :UpdateMatrix()
        EndIf
       
        If KeyboardPushed(#PB_Key_Up)
          y + 0.4:UpdateMatrix()
        ElseIf KeyboardPushed(#PB_Key_Down)
          y - 0.4 :UpdateMatrix()
        ElseIf KeyboardReleased(#PB_Key_S)
          ;SaveMesh(0,"pyramid.mesh")
          For i=0 To 11
          vx = MeshData(MeshDataInd(i)\Index)\x
          vy = MeshData(MeshDataInd(i)\Index)\y
          vz = MeshData(MeshDataInd(i)\Index)\z
         
          Debug "index "+Str(indx) +"  = " + Str(vx)+"  "+Str(vy)+"  " +Str(vz)+"  "
          indx + 1
        Next
        EndIf
       
      EndIf
      rot.f+0.6
      RotateEntity(0,0,rot,0)
      RenderWorld()
      FlipBuffers()
     
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
 
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End


Procedure DrawMatrix()
    MeshVertexPosition(-20, -20, 10) ;vertex 0
    MeshVertexColor(RGB(255, 0, 0))
    MeshVertexPosition(20, -20, 10)  ; vertex 1
    MeshVertexColor(RGB(0, 255, 0))
    MeshVertexPosition(0, -20, -10)   ; vertex 2
    MeshVertexColor(RGB(255, 220, 0))
       
    MeshVertexPosition(0, 10,0)     ; vertex 3
    MeshFace(0, 1, 2)
    MeshFace(1, 2, 3)
    MeshFace(1, 0, 3)
    MeshFace(0, 3, 2)
   
EndProcedure 
Procedure CreateMatrix()
 
  CreateMesh(0, #PB_Mesh_TriangleList, #True)
  DrawMatrix()
  FinishMesh(#True)
  SetMeshMaterial(0, MaterialID(0))
   
  CreateEntity(0, MeshID(0), #PB_Material_None)
 
  ScaleEntity(0, 2, 2, 2)
  GetMeshData(0,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(0)-1)
  GetMeshData(0,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(0, 0)-1)
EndProcedure


Procedure UpdateMatrix()
      MeshData(3)\x = x
      MeshData(3)\y = y
      MeshData(3)\z = z
 
      ;vx = MeshData(MeshDataInd(10)\Index)\x
      ;vy = MeshData(MeshDataInd(10)\Index)\y
      ;vz = MeshData(MeshDataInd(10)\Index)\z
      ;MessageRequester("", Str(vx)+"  "+Str(vy)+"  " +Str(vz)+"  ")
      ;Debug Str(vx)+"  "+Str(vy)+"  " +Str(vz)+"  "
  ;MessageRequester("", Str(MeshIndexCount(0, 0)-1))
       
      SetMeshData(0,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(0)-1)
      SetMeshData(0,0, MeshDataInd(), #PB_Mesh_Face, 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: Mesh deformation in PB5.20?

Post by Comtois »

here's how to automatically centering a mesh

Code: Select all

Enumeration
  #MESH
  #LIGHT
  #CAMERA
  #mainwin
EndEnumeration
Structure Vector3
  x.f
  y.f
  z.f
EndStructure
Define.Vector3 v1, v2, v3

Global x.f = 0
Global y.f = 10
Global z.f = -30
Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)
ExamineDesktops()
If OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "press S to show indices, press Arrows change vertex 3 ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  
  Define.f KeyX, KeyY
  Declare ChangeMesh()
  Declare UpdateMatrix()
  Declare CreateMatrix()
  Declare DrawMatrix()
  
  If InitEngine3D()
    
    Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
    Add3DArchive("/", #PB_3DArchive_FileSystem)
    
    InitSprite()
    InitKeyboard()
    OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0)-60, 0, 0, 0)
    
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    MaterialShadingMode(0, #PB_Material_Wireframe     )
    MaterialCullingMode(0, #PB_Material_NoCulling)
    CreateMatrix()
    
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, 120, #PB_Absolute)
    CameraFOV(0, 70)
    CameraBackColor(0, $330000)
    CameraLookAt(0,0,0,0)
    
    CreateLight(0, RGB(255,255,255), 10, 60, -10)
    AmbientColor(RGB(90, 90, 90))
    
    ChangeMesh()
    
    Repeat
      Event = WindowEvent()
      
      If ExamineKeyboard()
        
        If KeyboardReleased(#PB_Key_F2)
          MaterialShadingMode(0, #PB_Material_Wireframe)
        ElseIf KeyboardReleased(#PB_Key_F3)
          MaterialShadingMode(0, #PB_Material_Solid)
        EndIf
        
        If KeyboardPushed(#PB_Key_Right)
          x + 0.4 :UpdateMatrix()
        ElseIf KeyboardPushed(#PB_Key_Left)
          x - 0.4 :UpdateMatrix()
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          y + 0.4:UpdateMatrix()
        ElseIf KeyboardPushed(#PB_Key_Down)
          y - 0.4 :UpdateMatrix()
        ElseIf KeyboardReleased(#PB_Key_S)
          ;SaveMesh(0,"pyramid.mesh")
          For i=0 To 11
            vx = MeshData(MeshDataInd(i)\Index)\x
            vy = MeshData(MeshDataInd(i)\Index)\y
            vz = MeshData(MeshDataInd(i)\Index)\z
            
            Debug "index "+Str(indx) +"  = " + Str(vx)+"  "+Str(vy)+"  " +Str(vz)+"  "
            indx + 1
          Next
        EndIf
        
      EndIf
      rot.f+0.6
      RotateEntity(0,rot,rot,rot)
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

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  
  SetMeshData(0,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(0)-1)
EndProcedure


Procedure DrawMatrix()
  MeshVertexPosition(-20, -20, -20) ;vertex 0
  MeshVertexColor(RGB(255, 0, 0))
  MeshVertexPosition(20, -20, -20)  ; vertex 1
  MeshVertexColor(RGB(0, 255, 0))
  MeshVertexPosition(0, -20, -40)   ; vertex 2
  MeshVertexColor(RGB(255, 220, 0))
  
  MeshVertexPosition(0, 10,-30)     ; vertex 3
  MeshFace(0, 1, 2)
  MeshFace(1, 2, 3)
  MeshFace(1, 0, 3)
  MeshFace(0, 3, 2)
  
EndProcedure 

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

Procedure UpdateMatrix()
  MeshData(3)\x = x
  MeshData(3)\y = y
  MeshData(3)\z = z
  
  ;vx = MeshData(MeshDataInd(10)\Index)\x
  ;vy = MeshData(MeshDataInd(10)\Index)\y
  ;vz = MeshData(MeshDataInd(10)\Index)\z
  ;MessageRequester("", Str(vx)+"  "+Str(vy)+"  " +Str(vz)+"  ")
  ;Debug Str(vx)+"  "+Str(vy)+"  " +Str(vz)+"  "
  ;MessageRequester("", Str(MeshIndexCount(0, 0)-1))
  
  SetMeshData(0,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(0)-1)
  SetMeshData(0,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshVertexCount(0)-1)
EndProcedure
Please correct my english
http://purebasic.developpez.com/
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: Mesh deformation in PB5.20?

Post by Psychophanta »

Good example @applePi.

By the way @Comtois, imho, there is a much more elegant way for Set/GetMeshData() function , which is to use a pointer for the vertex sequence instead an Array. It would make things easier and more versatile for the programmer.
If a pointer is used as input parameter for the funcion there is always the possibility to pass an array, but using an array as input parameter, then the possibilities are cutted off.

Please NOTE that the source code (for example the tips you and applePi have wrote in this thread) won't need to change not even a character; i.e. it would work the same as:

Code: Select all

SetMeshData(0,0,MeshData(),#PB_Mesh_Vertex|#PB_Mesh_Normal,0,MeshVertexCount(0,0)-1)
or

Code: Select all

SetMeshData(0,0,@MeshData(),#PB_Mesh_Vertex|#PB_Mesh_Normal,0,MeshVertexCount(0,0)-1)
or

Code: Select all

SetMeshData(0,0,@MeshData(0),#PB_Mesh_Vertex|#PB_Mesh_Normal,0,MeshVertexCount(0,0)-1)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Mesh deformation in PB5.20?

Post by applePi »

thanks Comtois, i first thought that i can't rotate the pyramid correctly at that place when the vertex 2 have the coordinates 0,-20, -40. but after your correction i have moved the pyramid to my first place and it rotates correctly around itself in that place, and i have moved the object after the correction to any place in the screen and it will rotate correctly around itself
this is moving the object center to my first place after the correction
MoveEntity(0,0,-5,-30,#PB_Absolute)
RotateEntity(0,0,rot,0)

so just a suggestion i don't know if it is practical to the RotateEntity such as adding Local or global keyword, Local ie to rotate around its local axis wherever it is located. in any way it is solved here
Post Reply