Trying to remake a mesh

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:

Trying to remake a mesh

Post by Psychophanta »

Why does not work if i remake a mesh?
2 ways to try it, key '2' and key '3':

Code: Select all

InitEngine3D(#PB_Engine3D_NoLog,#PB_Compiler_Home+"Compilers\Engine3d.dll"):InitSprite():InitKeyboard():InitMouse()
OpenWindow(0,0,0,1280,720,"Esc to exit",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,WindowWidth(0),WindowHeight(0),0,0,0)
CreateCamera(0,0,0,100,100):MoveCamera(0,0,2,11):CameraBackColor(0,$446666)
CreateLight(0,$ffffff,10,10,0)
CreateShaderMaterial(0,#PB_Material_ColorShader)
SetMaterialColor(0,#PB_Material_AmbientColor|#PB_Material_DiffuseColor,$4455ff):MaterialShininess(0,64,$ffffff)
MaterialCullingMode(0,#PB_Material_NoCulling)
lgs.f=1.0  ; longitud de sección
r.f=0.2  ; radio de sección
ni.a=6; <- número de segmentos en base
nj.a=3; <- número de segmentos en altura
ns.a=nj; <- numero de secciones (juntas)
Dim arrayvertices.MeshVertex(0)
Dim arraycaras.MeshFace(0)
Macro original()
  CreateCylinder(0,r,lgs*nj,ni,nj,0)
  nvertices.i=MeshVertexCount(0,0); <- para el numero de vertices
  indicesvertices.i=MeshIndexCount(0,0); <- para el numero de caras
  GetMeshData(0,0,arrayvertices(),#PB_Mesh_Vertex|#PB_Mesh_UVCoordinate|#PB_Mesh_Normal|#PB_Mesh_Color|#PB_Mesh_Tangent,0,nvertices-1)
  GetMeshData(0,0,arraycaras(),#PB_Mesh_Face,0,indicesvertices-1)
  CreateEntity(0,MeshID(0),MaterialID(0))
EndMacro
Macro remake()
  CreateMesh(0,#PB_Mesh_TriangleStrip,#PB_Mesh_Static)
  SetMeshData(0,0,arrayvertices(),#PB_Mesh_Vertex|#PB_Mesh_UVCoordinate|#PB_Mesh_Normal|#PB_Mesh_Color|#PB_Mesh_Tangent,0,nvertices-1)
  SetMeshData(0,0,arraycaras(),#PB_Mesh_Face,0,indicesvertices-1)
  CreateEntity(0,MeshID(0),MaterialID(0))  
EndMacro
Macro remake2()
  CreateMesh(0,#PB_Mesh_TriangleStrip,#PB_Mesh_Static)
  For i.i=0 To nvertices-1
    MeshVertex(arrayvertices(i)\x,arrayvertices(i)\y,arrayvertices(i)\z,arrayvertices(i)\u,arrayvertices(i)\v,arrayvertices(i)\Color,arrayvertices(i)\NormalX,arrayvertices(i)\NormalY,arrayvertices(i)\NormalZ)
    MeshVertexTangent(arrayvertices(i)\TangentX,arrayvertices(i)\TangentY,arrayvertices(i)\TangentZ)
    MeshIndex(i)
  Next
  FinishMesh(1)
  CreateEntity(0,MeshID(0),MaterialID(0))  
EndMacro
original()
Repeat:While WindowEvent():Wend
  ExamineKeyboard():ExamineMouse()
  If KeyboardReleased(#PB_Key_1)
    FreeEntity(0)
    FreeMesh(0)
    original()
  ElseIf KeyboardReleased(#PB_Key_2)
    FreeEntity(0)
    FreeMesh(0)
    remake()
  ElseIf KeyboardReleased(#PB_Key_3)
    FreeEntity(0)
    FreeMesh(0)
    remake2()
  ElseIf KeyboardReleased(#PB_Key_F12):alambres.a!1
    If alambres:MaterialShadingMode(0,#PB_Material_Wireframe)
    Else:MaterialShadingMode(0,#PB_Material_Solid)
    EndIf
  EndIf
  RenderWorld()
  FlipBuffers():Delay(11)
Until KeyboardPushed(#PB_Key_Escape)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
miso
Enthusiast
Enthusiast
Posts: 466
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Trying to remake a mesh

Post by miso »

I could not figure out how this setmeshdata thing should work. :S
Here's two solution though, that scans and can recreate a mesh (using your original code):
Edit: It does not check for submeshes.

Code: Select all

InitEngine3D(#PB_Engine3D_NoLog,#PB_Compiler_Home+"Compilers\Engine3d.dll"):InitSprite():InitKeyboard():InitMouse()
OpenWindow(0,0,0,1280,720,"Esc to exit",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,WindowWidth(0),WindowHeight(0),0,0,0)
CreateCamera(0,0,0,100,100):MoveCamera(0,0,2,11):CameraBackColor(0,$446666)
CreateLight(0,$ffffff,10,10,0)
CreateShaderMaterial(0,#PB_Material_ColorShader)
SetMaterialColor(0,#PB_Material_AmbientColor|#PB_Material_DiffuseColor,$4455ff):MaterialShininess(0,64,$ffffff)
MaterialCullingMode(0,#PB_Material_NoCulling)

lgs.f=1.0  ; longitud de sección
r.f=0.2  ; radio de sección
ni.a=6; <- número de segmentos en base
nj.a=3; <- número de segmentos en altura
ns.a=nj; <- numero de secciones (juntas)
Global Dim arrayvertices.MeshVertex(0)
Global Dim arraycaras.MeshFace(0)
Macro original()
  ;CreateCylinder(0,r,lgs*nj,ni,nj,0)
  CreateCube(0,1)
  ;CreateSphere(0,1,50,50)
  ;Debug SubMeshCount(0)
  BuildMeshTangents(0)
  nvertices.i=MeshVertexCount(0); <- para el numero de vertices
  ReDim arrayvertices(nvertices)
  
  indicesvertices.i=MeshIndexCount(0); <- para el numero de caras
  ReDim arraycaras(indicesvertices)
  GetMeshData(0,0,arrayvertices(),#PB_Mesh_Vertex|#PB_Mesh_UVCoordinate|#PB_Mesh_Normal|#PB_Mesh_Color|#PB_Mesh_Tangent,0,nvertices-1)
  GetMeshData(0,0,arraycaras(),#PB_Mesh_Face,0,indicesvertices-1)
  CreateEntity(0,MeshID(0),MaterialID(0))
  
EndMacro
Macro remake()
  If IsMesh(0):FreeMesh(0):EndIf
  CreateMesh(0,#PB_Mesh_TriangleList,#PB_Mesh_Static)
  For i.i=0 To ArraySize(arrayvertices())
    MeshVertexPosition(arrayvertices(i)\x,arrayvertices(i)\y,arrayvertices(i)\z)
    MeshVertexNormal(arrayvertices(i)\NormalX,arrayvertices(i)\Normaly,arrayvertices(i)\Normalz)
    MeshVertexTextureCoordinate(arrayvertices(i)\u,arrayvertices(i)\v)
    MeshVertexColor(arrayvertices(i)\Color)
  Next
  
  For i.i=0 To ArraySize(arraycaras())-2 Step 3
    MeshFace(arraycaras(i)\Index,arraycaras(i+1)\Index,arraycaras(i+2)\Index)
  Next
  FinishMesh(#True)
  CreateEntity(0,MeshID(0),MaterialID(0))  
EndMacro

Macro remake2()
  If IsMesh(0):FreeMesh(0):EndIf
  CreateMesh(0,#PB_Mesh_TriangleList,#PB_Mesh_Static)
  For i.i=0 To ArraySize(arrayvertices())
    MeshVertex(arrayvertices(i)\x,arrayvertices(i)\y,arrayvertices(i)\z,arrayvertices(i)\u,arrayvertices(i)\v,arrayvertices(i)\Color,arrayvertices(i)\NormalX,arrayvertices(i)\NormalY,arrayvertices(i)\NormalZ)
  Next
  
  For i.i=0 To ArraySize(arraycaras())
    MeshIndex(arraycaras(i)\index)
  Next
  FinishMesh(#True)
  CreateEntity(0,MeshID(0),MaterialID(0))  
EndMacro

original()
Repeat:While WindowEvent():Wend
  ExamineKeyboard():ExamineMouse()
  If KeyboardReleased(#PB_Key_1)
    If IsEntity(0):FreeEntity(0):EndIf
    If IsMesh(0):FreeMesh(0):EndIf
    original()
  ElseIf KeyboardReleased(#PB_Key_2)
    If IsEntity(0):FreeEntity(0):EndIf
    If IsMesh(0):FreeMesh(0):EndIf
    remake()
  ElseIf KeyboardReleased(#PB_Key_3)
    If IsEntity(0):FreeEntity(0):EndIf
    If IsMesh(0):FreeMesh(0):EndIf
    remake2()
  ElseIf KeyboardReleased(#PB_Key_F12):alambres.a!1
    If alambres:MaterialShadingMode(0,#PB_Material_Wireframe)
    Else:MaterialShadingMode(0,#PB_Material_Solid)
    EndIf
  EndIf
  If IsEntity(0) : RotateEntity(0,0.5,0.5,0.5,#PB_Relative):EndIf
  RenderWorld()
  FlipBuffers():Delay(11)
Until KeyboardPushed(#PB_Key_Escape)

User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: Trying to remake a mesh

Post by Psychophanta »

Well, looks like there are not finished things for SetMeshData() after CreateMesh().
Besides of it, looks like there is not the same to build a vertex doing: MeshVertex() with all the parameters, than adding position, then normals, etc.

So, the current approach, in which 'remake2()' does not work:

Code: Select all

InitEngine3D(#PB_Engine3D_NoLog,#PB_Compiler_Home+"Compilers\Engine3d.dll"):InitSprite():InitKeyboard():InitMouse()
OpenWindow(0,0,0,1280,720,"Esc to exit",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,WindowWidth(0),WindowHeight(0),0,0,0)
CreateCamera(0,0,0,100,100):MoveCamera(0,0,2,11):CameraBackColor(0,$446666)
CreateLight(0,$ffffff,10,10,0)
CreateShaderMaterial(0,#PB_Material_ColorShader)
SetMaterialColor(0,#PB_Material_AmbientColor|#PB_Material_DiffuseColor,$4455ff):MaterialShininess(0,64,$ffffff)
MaterialCullingMode(0,#PB_Material_NoCulling)

lgs.f=1.0  ; longitud de sección
r.f=0.2  ; radio de sección
ni.a=6; <- número de segmentos en base
nj.a=3; <- número de segmentos en altura
ns.a=nj; <- numero de secciones (juntas)
Global Dim arrayvertices.MeshVertex(0)
Global Dim arraycaras.MeshFace(0)
Macro original()
  CreateCylinder(0,r,lgs*nj,ni,nj,0)
  ;CreateSphere(0,1,50,50)
  ;Debug SubMeshCount(0)
  BuildMeshTangents(0)
  nvertices.i=MeshVertexCount(0); <- para el numero de vertices
  ReDim arrayvertices(nvertices)
  
  nindicesvertices.i=MeshIndexCount(0); <- para el numero de caras
  ReDim arraycaras(nindicesvertices)
  GetMeshData(0,0,arrayvertices(),#PB_Mesh_Vertex|#PB_Mesh_UVCoordinate|#PB_Mesh_Normal|#PB_Mesh_Color|#PB_Mesh_Tangent,0,nvertices-1)
  GetMeshData(0,0,arraycaras(),#PB_Mesh_Face,0,nindicesvertices-1)
  CreateEntity(0,MeshID(0),MaterialID(0))
  
EndMacro
Macro remake()
  CreateMesh(0,#PB_Mesh_TriangleList,#PB_Mesh_Static)
  For i.i=0 To nvertices
    MeshVertex(arrayvertices(i)\x,arrayvertices(i)\y,arrayvertices(i)\z,arrayvertices(i)\u,arrayvertices(i)\v,arrayvertices(i)\Color,arrayvertices(i)\NormalX,arrayvertices(i)\NormalY,arrayvertices(i)\NormalZ)
    MeshVertexTangent(arrayvertices(i)\TangentX,arrayvertices(i)\TangentY,arrayvertices(i)\TangentZ)
  Next
  For i.i=0 To nindicesvertices
    MeshIndex(arraycaras(i)\index)
  Next
  FinishMesh(1)
  CreateEntity(0,MeshID(0),MaterialID(0))
EndMacro
Macro remake2()
  CreateMesh(0,#PB_Mesh_TriangleList,#PB_Mesh_Static)
  SetMeshData(0,0,arrayvertices(),#PB_Mesh_Vertex|#PB_Mesh_UVCoordinate|#PB_Mesh_Normal|#PB_Mesh_Color|#PB_Mesh_Tangent,0,nvertices-1)
  SetMeshData(0,0,arraycaras(),#PB_Mesh_Face,0,nindicesvertices-1)
  CreateEntity(0,MeshID(0),MaterialID(0))
;   FinishMesh(0)
;   NormalizeMesh(0,0)
;   UpdateMeshBoundingBox(0)
EndMacro

original()
Repeat:While WindowEvent():Wend
  ExamineKeyboard():ExamineMouse()
  If KeyboardReleased(#PB_Key_1)
    FreeEntity(0)
    FreeMesh(0)
    original()
  ElseIf KeyboardReleased(#PB_Key_2)
    FreeEntity(0)
    FreeMesh(0)
    remake()
  ElseIf KeyboardReleased(#PB_Key_3)
    FreeEntity(0)
    FreeMesh(0)
    remake2()
  ElseIf KeyboardReleased(#PB_Key_F12):alambres.a!1
    If alambres:MaterialShadingMode(0,#PB_Material_Wireframe)
    Else:MaterialShadingMode(0,#PB_Material_Solid)
    EndIf
  EndIf
  If IsEntity(0) : RotateEntity(0,0.5,0.5,0.5,#PB_Relative):EndIf
  RenderWorld()
  FlipBuffers():Delay(11)
Until KeyboardPushed(#PB_Key_Escape)
http://www.zeitgeistmovie.com

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