Mesh deformation in PB5.20?

Everything related to 3D programming
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Mesh deformation in PB5.20?

Post by Psychophanta »

I am aware about these:
http://www.purebasic.fr/english/viewtop ... 16&t=20099
http://www.purebasic.fr/english/viewtop ... 492#130492

How to implement it in PB5.20?
I don't undestand why the native Structure 'PB_MeshFace' has only a field:

Code: Select all

Structure PB_MeshFace
    Index.l
EndStructure
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
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 »

The index depends on the type of mesh
#PB_Mesh_TriangleList : the mesh will be composed of a list of triangles (default).
#PB_Mesh_TriangleStrip: the mesh will be composed of a list of connected triangles (vertices are shared).
#PB_Mesh_TriangleFan : the mesh will be composed of a list of triangles sharing the same central vertex point.
#PB_Mesh_PointList : the mesh will be composed of a list of points.
#PB_Mesh_LineList : the mesh will be composed of a list of lines.
#PB_Mesh_LineStrip : the mesh will be composed of a list of connected lines (vertices are shared).
For type # PB_Mesh_LineList it takes 2 consecutive index to define a line
For type # PB_Mesh_TriangleList it takes 3 consecutive index to define a triangle.

You must adapt the use of indexes depending on the type of mesh.

If the mesh is loaded from a file, the type is necessarily # PB_Mesh_TriangleList.

Here is an example that shows how to use it :

Code: Select all

IncludeFile "Screen3DRequester.pb"

Structure Vector3
  x.f
  y.f
  z.f
EndStructure

Macro SubVector3(n, v, w)
  n\x = v\x - w\x
  n\y = v\y - w\y
  n\z = v\z - w\z
EndMacro

Define.Vector3 Normal, v1, v2, v3

#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)
Global Dim MeshDataInd.PB_MeshFace(0)

Declare UpdateMatrix()
Declare CreateMatrix()

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    Add3DArchive("Data/Textures", #PB_3DArchive_FileSystem)
    Add3DArchive("Data/Packs/skybox.zip", #PB_3DArchive_Zip)
    Add3DArchive("Data/Scripts", #PB_3DArchive_FileSystem)
    Parse3DScripts()
    
    ;-Material 
    CreateMaterial(1, LoadTexture(1, "MRAMOR6X6.jpg"))
    MaterialCullingMode(1, 1)
    
    ;-Mesh
    CreateMatrix()
    
    ;-Camera
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0,0,150,80, #PB_Absolute)
    CameraLookAt(0, 0, 0, 0)
    CameraBackColor(0, RGB(90, 0, 0))
    
    ;-Light
    CreateLight(0, RGB(255, 255, 255), 5, 15, 5)
    AmbientColor(RGB(30, 30, 30))
    
    ;- 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 Normalize(*V.Vector3)
  Protected.f magSq, oneOverMag
  
  magSq = *V\x * *V\x + *V\y * *V\y + *V\z * *V\z
  If magsq > 0
    oneOverMag = 1.0 / Sqr(magSq)
    *V\x * oneOverMag
    *V\y * oneOverMag
    *V\z * oneOverMag
  EndIf 
  
EndProcedure

Procedure NormalFace(*n.Vector3, *v1.Vector3, *v2.Vector3, *v3.Vector3)
  Protected.Vector3 v2v1, v3v1
  SubVector3(v2v1, *v2, *v1)
  SubVector3(v3v1, *v3, *v1)
  
  *n\x = v2v1\y * v3v1\z - v2v1\z * v3v1\y
  *n\y = v2v1\z * v3v1\x - v2v1\x * v3v1\z
  *n\z = v2v1\x * v3v1\y - v2v1\y * v3v1\x
EndProcedure  

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, 3, 3, 3)
  
  GetMeshData(0,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(0, 0)-1)
  GetMeshData(0,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(0, 0)-1)
  
EndProcedure

Procedure UpdateMatrix()
  Protected a, b, c, i, j
  Protected N.Vector3, V1.Vector3, V2.Vector3, V3.Vector3
  Protected.f magSq, oneOverMag
  
  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
      ;Init les normales
      MeshData(c)\NormalX = 0
      MeshData(c)\NormalY = 0
      MeshData(c)\NormalZ = 0
      c + 1
    Next a
  Next b
  
  ;Calcul les normales
  For j=0 To ArraySize(MeshDataInd())-2 Step 3
    
    V1\x = MeshData(MeshDataInd(j  )\Index)\x
    V1\y = MeshData(MeshDataInd(j  )\Index)\y
    V1\z = MeshData(MeshDataInd(j  )\Index)\z
    V2\x = MeshData(MeshDataInd(j+1)\Index)\x
    V2\y = MeshData(MeshDataInd(j+1)\Index)\y
    V2\z = MeshData(MeshDataInd(j+1)\Index)\z  
    V3\x = MeshData(MeshDataInd(j+2)\Index)\x
    V3\y = MeshData(MeshDataInd(j+2)\Index)\y
    V3\z = MeshData(MeshDataInd(j+2)\Index)\z    
    NormalFace(@N, @V1, @V2, @V3)
    ;Normalize(@N)
    MeshData(MeshDataInd(j  )\Index)\NormalX + N\x
    MeshData(MeshDataInd(j  )\Index)\NormalY + N\y
    MeshData(MeshDataInd(j  )\Index)\NormalZ + N\z
    MeshData(MeshDataInd(j+1)\Index)\NormalX + N\x
    MeshData(MeshDataInd(j+1)\Index)\NormalY + N\y
    MeshData(MeshDataInd(j+1)\Index)\NormalZ + N\z  
    MeshData(MeshDataInd(j+2)\Index)\NormalX + N\x
    MeshData(MeshDataInd(j+2)\Index)\NormalY + N\y
    MeshData(MeshDataInd(j+2)\Index)\NormalZ + N\z     
  Next
  
  For i=0 To ArraySize(MeshData())
    
    magSq = MeshData(i)\NormalX * MeshData(i)\NormalX + 
            MeshData(i)\NormalY * MeshData(i)\NormalY + 
            MeshData(i)\NormalZ * MeshData(i)\NormalZ 
    
    If magsq > 0
      oneOverMag = 1.0 / Sqr(magSq)
      MeshData(i)\NormalX * oneOverMag
      MeshData(i)\NormalY * oneOverMag
      MeshData(i)\NormalZ * oneOverMag
    EndIf 
    
  Next 
  
  SetMeshData(0,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Normal, 0, MeshVertexCount(0, 0)-1)
EndProcedure
Last edited by Comtois on Thu Jul 11, 2013 5:33 pm, edited 1 time in total.
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 »

Thank you!
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
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 »

Please, a short functional tip using SetMeshData() function with the #PB_Mesh_Face flag mode ?
There is nothing about it in the examples directoriy in the package, and the manual is not very explicit about it :?
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 »

unless otherwise, i think #PB_Mesh_Face with SetMeshData are unnecessary
look as an example to the following program witch you can alter the position of the vertex 3 by pressing arrow keys, the only important thing is using SetMeshData with #PB_Mesh_Vertex, but #PB_Mesh_Face can be used for getting the info for a specific index V1\x = MeshData(MeshDataInd(j )\Index)\x ie to get the x coordinates of the indexj. i am still studying the subject

Code: Select all

Enumeration
   #MESH
   #LIGHT
   #CAMERA
   #mainwin
 EndEnumeration
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 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 KeyboardPushed(#PB_Key_S)
          SaveMesh(0,"pyramid.mesh")
        
        EndIf
        
      EndIf
      rot.f+0.6
      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)
    MeshVertexColor(RGB(255, 0, 0))
    MeshVertexPosition(20, -20, -20)
    MeshVertexColor(RGB(0, 255, 0))
    MeshVertexPosition(0, -20, -40)
    MeshVertexColor(RGB(255, 220, 0))
        
    MeshVertexPosition(0, 10,-30)
    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
  
  ;V1\x = MeshData(MeshDataInd(j  )\Index)\x
  ;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
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 »

applePi wrote:unless otherwise, i think #PB_Mesh_Face with SetMeshData are unnecessary
I guess it is a nonsense .
That's the reason of this thread, but i am waiting for the experts to confirm it.

Thanks applePi
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
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 »

Psychophanta wrote:Please, a short functional tip using SetMeshData() function with the #PB_Mesh_Face flag mode ?
There is nothing about it in the examples directoriy in the package, and the manual is not very explicit about it :?
changing the buffer index is not implemented. You can only read it. I can make the change, but I do not see the point, why would you change this buffer? Have you an example ?
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 »

Manual wrote:SetMeshData()

Syntax

Result = SetMeshData(#Mesh, SubMesh, DataArray(), Flags, FirstIndex, LastIndex)
Description

Set internal mesh data, like vertices, face etc.
Parameters

#Mesh The mesh to use.
SubMesh The submesh to set the data to. The first submesh index is 0 (main mesh).
DataArray() The array containing the data to set. It has to be an array of type "PB_MeshVertex" or "PB_MeshFace" depending of the specified flags.
Flags Specifies which kind of data needs to be set. It can be one of the following values:
#PB_Mesh_Vertex: DataArray() is an array of type "PB_MeshVertex".
#PB_Mesh_Face : DataArray() is an array of type "PB_MeshFace".

Combined with:
#PB_Mesh_UVCoordinate : Set the UV coordinate information (only for #PB_Mesh_Vertex flag)
#PB_Mesh_Normal : Set the normal information (only for #PB_Mesh_Vertex flag)
#PB_Mesh_Color : Set the color information (only for #PB_Mesh_Vertex flag)

The "PB_MeshVertex" and "PB_MeshFace" structures are defined like this:
Structure PB_MeshVertex
x.f
y.f
z.f
NormalX.f ; only used if #@pb_mesh_normalflag is set
NormalY.f ;
NormalZ.f ;
u.f ; only used if #@pb_mesh_uvcoordinateflag is set
v.f ;
Color.l ; only used if ##PB_Mesh_Color flag is set
EndStructure

Structure PB_MeshFace
Index.l
EndStructure


FirstIndex, LastIndex First and last index to set the data to.

Return value

Returns nonzero on success and zero on failure.
See Also

GetMeshData()
Supported OS

All
So Manual says the mesh data can be changed:
a) By changing its vertex (i don't understad why to name it as 'index' if its correct name is 'vertex', can you explain this useless confussion?. If 'index' simply means the Vertex Index Number inside the mesh why don't name it as "Vertex"?)
b) By changing its faces. How?
Last edited by Psychophanta on Thu Jul 11, 2013 6:01 pm, edited 1 time in total.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
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 »

a mesh is composed of two buffers :
- vertices 's buffer
- index's buffer (used to define the 'face').

SetMeshData() can change the vertices buffer, I have not implemented the change of index buffer because I did not see the use.
If someone show me that it is necessary, I can add it.
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 »

Comtois wrote: I have not implemented the change of index buffer because I did not see the use.
:o
I find it very necessary.
At the end, any mesh is a sequence of vertex, so user shluld be able to change the 3D position of any of them.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
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 »

Psychophanta wrote:
Comtois wrote: I have not implemented the change of index buffer because I did not see the use.
:o
I find it very necessary.
Have you an example ?
Last edited by Comtois on Thu Jul 11, 2013 6:11 pm, edited 1 time in total.
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 »

Psychophanta wrote:At the end, any mesh is a sequence of vertex, so user shluld be able to change the 3D position of any of them.
it's already the case in the example I showed above
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 »

Ok,
then will you quit the PB_MeshFace Structure and the #PB_Mesh_Face flag for the function please?
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
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 »

I do not know what is the fastest , update the doc or make the change ?
I'll probably make the change, even though I still do not see the interest.
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 »

Comtois wrote:I do not know what is the fastest , update the doc or make the change ?
I'll probably make the change, even though I still do not see the interest.
:lol:
The interest is to make things the powerful and simplest as possible without loosing functionality.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply