December 03, 2020, 08:24:12 PM

Author Topic: [bb] Read mesh and write to bb data by markcw [ 1+ years ago ]  (Read 484 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Read mesh and write to bb data
Author : markcw
Posted : 1+ years ago

Description : This is another 2-part code entry. It is designed to pack meshes inside a blitz executable and then create them from data labels.

The first part reads a mesh, or mesh hierarchy, and writes it to a bb data file. The second part reads the data, as an include, and creates the mesh.

It uses a special format to store the data which is:
nMeshes, nVerts, nTris, onetriuvset, xposition, yposition, zposition, pitch, yaw, roll, vertexx, vertexy, vertexz... vertexu, vertexv... vertex0, vertex1, vertex2

It was written to store vertex morphed meshes that have a single texture.

Limitations are: only stores one uv coord set; doesn't store any texture reference; mesh hierarchy is lost and ends up all on same hierarchy level; no animation.

The onetriuvset value is used to tell whether the mesh data has any face/uv data. This is to save space for vertex morphing use.

Code :
Code: BlitzBasic
  1. ;Read Mesh Write Data, on 8/10/06
  3. Graphics3D 640,480,0,2
  4. SetBuffer BackBuffer()
  6. camera=CreateCamera() ;init world
  7. PositionEntity camera,0,0,-5
  8. light=CreateLight()
  9. RotateEntity light,45,45,0
  11. fileout$=""
  12. filein$="yourname.x" ;mesh in
  14. meshtype=0 ;set mesh type
  15. Select meshtype
  16.  Case 1
  17.   mesh=LoadAnimMesh(filein$)
  18.  Default
  19.   mesh=LoadMesh(filein$)
  20. End Select
  22. ok=ReadMeshWriteData(mesh,filein$,fileout$,1,0)
  24. ;Main loop
  25. While Not KeyHit(1) Or KeyHit(57) ;esc or space keys
  26.  RenderWorld
  28.  TurnEntity mesh,0.3,0.2,0.1
  30.  If KeyHit(17) wf=Not wf : WireFrame wf ;W key
  32.  Text 0,0,"ok="+ok+" filein$="+filein$+" fileout$="+fileout$
  34.  Flip
  35. Wend
  37. Function ReadMeshWriteData(mesh,filein$,fileout$,onelabel,onetriuvset)
  38.  ;Saves a mesh, or mesh hierarchy, as a bb data file
  39.  ;mesh=mesh handle, filein$="mesh.x/3ds/b3d", fileout$=""
  40.  ;onelabel=one data label per mesh, 0=all/1=one
  41.  ;onetriuvset=one triangle/uv set per mesh, 0=all/1=one/2=none
  43.  Local file,child,surf,nmeshes,dotlen,si,mi,vi,vx#,vy#,vz#
  45.  ;Write
  46.  If fileout$="" Then Return False ;fail code
  47.  file=WriteFile(fileout$)
  48.  If Not file Then Return False ;write fail
  50.  ;write title comment
  51.  WriteStringAscii(file,";"+fileout$)
  52.  WriteByte file,13 : WriteByte file,10 ;newline
  54.  ;calculate number of meshes
  55.  child=mesh
  56.  While child
  57.   For si=1 To CountSurfaces(child) : nmeshes=nmeshes+1 : Next
  58.   child=NextChild(child)
  59.  Wend
  60.  If onelabel=0 Then nmeshes=1
  62.  ;write mesh/each child
  63.  child=mesh
  64.  While child
  65.   For si=1 To CountSurfaces(child)
  66.    surf=GetSurface(child,si) ;next surface
  67.    mi=mi+1 ;mesh index
  68.    dotlen=Instr(filein$,".",1)
  70.    ;write data label
  71.    If onelabel=0 Or mi=1
  72.     WriteByte file,13 : WriteByte file,10 ;newline
  73.     WriteStringAscii(file,"."+Left(filein$,dotlen-1))
  74.     If onelabel=0 Then WriteStringAscii(file,"_mesh"+mi)
  75.     WriteStringAscii(file,"_"+Mid(filein$,dotlen+1,Len(filein$)-dotlen))
  76.    EndIf
  78.    ;write mesh data
  79.    WriteByte file,13 : WriteByte file,10 ;newline
  80.    WriteStringAscii(file,"Data ")
  81.    WriteStringAscii(file,nmeshes) ;1st value is nMeshes
  82.    WriteStringAscii(file,","+CountVertices(surf)) ;2nd value is nVerts
  83.    WriteStringAscii(file,","+CountTriangles(surf)) ;3rd value is nTris
  84.    WriteStringAscii(file,","+onetriuvset) ;4th value is onetriuvset
  85.    WriteStringAscii(file,","+EntityX(child,1)) ;5th value is x position
  86.    WriteStringAscii(file,","+EntityY(child,1)) ;6th value is y position
  87.    WriteStringAscii(file,","+EntityZ(child,1)) ;7th value is z position
  88.    WriteStringAscii(file,","+EntityPitch(child,1)) ;8th value is pitch
  89.    WriteStringAscii(file,","+EntityYaw(child,1)) ;9th value is yaw
  90.    WriteStringAscii(file,","+EntityRoll(child,1)) ;10th value is roll
  91.    For vi=0 To CountVertices(surf)-1 ;vertices, nVerts*3
  92.     vx#=VertexX(surf,vi) : If Abs(vx#)<0.001 Then vx#=0 ;prevent errors
  93.     vy#=VertexY(surf,vi) : If Abs(vy#)<0.001 Then vy#=0
  94.     vz#=VertexZ(surf,vi) : If Abs(vz#)<0.001 Then vz#=0
  95.     WriteStringAscii(file,","+vx#)
  96.     WriteStringAscii(file,","+vy#)
  97.     WriteStringAscii(file,","+vz#)
  98.    Next
  99.    If onetriuvset<2 And (onetriuvset=0 Or mi=1)
  100.     For vi=0 To CountVertices(surf)-1 ;uv coords, nVerts*2
  101.      WriteStringAscii(file,","+VertexU(surf,vi,0))
  102.      WriteStringAscii(file,","+VertexV(surf,vi,0))
  103.     Next
  104.     For vi=0 To CountTriangles(surf)-1 ;triangles, nTris*3
  105.      WriteStringAscii(file,","+TriangleVertex(surf,vi,0))
  106.      WriteStringAscii(file,","+TriangleVertex(surf,vi,1))
  107.      WriteStringAscii(file,","+TriangleVertex(surf,vi,2))
  108.     Next
  109.    EndIf
  111.   Next
  112.   child=NextChild(child)
  113.  Wend
  115.  CloseFile file
  116.  Return True ;success code
  118. End Function
  120. Function WriteStringAscii(file,ascii$)
  121.  ;file=file handle, ascii$=ascii string
  123.  Local i,char$
  124.  For i=1 To Len(ascii$)
  125.   char$=Mid(ascii$,i,1)
  126.   WriteByte(file,Asc(char$))
  127.  Next
  129. End Function
  131. Function NextChild(ent)
  132.  ;Returns next child of entity as if it was on the same hierarchy level
  133.  ;"NextChild(entity)", by Beaker
  135.  If CountChildren(ent)>0
  136.   Return GetChild(ent,1)
  137.  EndIf
  138.  Local foundunused=False
  139.  Local foundent=0,parent,sibling
  140.  While foundunused=False And ent<>0
  141.   parent=GetParent(ent)
  142.   If parent<>0
  143.    If CountChildren(parent)>1
  144.     If GetChild(parent,CountChildren(parent))<>ent
  145.      For siblingcnt=1 To CountChildren(parent)
  146.       sibling=GetChild(parent,siblingcnt)
  147.       If sibling=ent
  148.        foundunused=True
  149.        foundent=GetChild(parent,siblingcnt+1)
  150.       EndIf
  151.      Next
  152.     EndIf
  153.    EndIf
  154.   EndIf
  155.   ent=parent
  156.  Wend
  157.  Return foundent
  159. End Function

Comments :

markcw(Posted 1+ years ago)

 This is the second part. It creates meshes from the data labels.
Code: [Select]
;Read Data Write Mesh, on 8/10/06

Graphics3D 640,480,0,2
SetBuffer BackBuffer()

camera=CreateCamera() ;init world
PositionEntity camera,0,0,-5
RotateEntity light,45,45,0

Restore yourname_x ;yourname_mesh1_x ;mesh data label

mesh=ReadDataWriteMesh() ;mesh out

If CountChildren(mesh)=0 ;one mesh
 EntityTexture mesh,tex
Else ;multiple mesh
 For i=1 To CountChildren(mesh)
  child=GetChild(mesh,i) : EntityTexture child,tex

;Main loop
While Not KeyHit(1) Or KeyHit(57) ;esc or space key

 TurnEntity mesh,0.3,0.2,0.1

 If KeyHit(17) wf=Not wf : WireFrame wf ;W key

 Text 0,0,"children="+CountChildren(mesh)


Include ""

Function ReadDataWriteMesh()
 ;Creates a mesh, or mesh hierarchy, from a data label

 Local mesh,child,surf,nmeshes,nverts,ntris,onetriuvset,mi,vi
 Local px#,py#,pz#,rx#,ry#,rz#,vx#,vy#,vz#,vu#,vv#,v0,v1,v2

 ;create mesh
 mesh=CreateMesh() : child=mesh
 For mi=1 To nmeshes
  Read nmeshes ;1st value is nMeshes
  If nmeshes>1 Then child=CreateMesh(mesh)
  Read nverts ;2nd value is nVerts
  Read ntris ;3rd value is nTris
  Read onetriuvset ;4th value is onetriuvset
  Read px# : Read py# : Read pz# ;5th..7th value is xyz position
  PositionEntity child,px#,py#,pz#
  Read rx# : Read ry# : Read rz# ;8th..10th value is pitch/yaw/roll
  RotateEntity child,rx#,ry#,rz#
  For vi=0 To nverts-1 ;vertices, nVerts*3
   Read vx# : Read vy# : Read vz#
  If onetriuvset<2 And (onetriuvset=0 Or mi=1)
   For vi=0 To nverts-1 ;uv coords, nVerts*2
    Read vu# : Read vv#
    VertexTexCoords surf,vi,vu#,vv#
   For vi=0 To ntris-1 ;triangles, nTris*3
    Read v0 : Read v1 : Read v2
  UpdateNormals child ;set normals

 Return mesh ;mesh handle

End Function

Function ReadDataMorphMesh(mesh)
 ;Vertex morphs a mesh from a data label

 Local surf,nmeshes,nverts,ntris,onetriuvset,vi,vx#,vy#,vz#,vu#,vv#

 ;vertex morph mesh
 Read nmeshes ;1st value is nMeshes
 Read nverts ;2nd value is nVerts
 Read ntris ;3rd value is nTris
 Read onetriuvset ;4th value is onetriuvset
 Read vx# : Read vy# : Read vz# ;5th..7th value is xyz position
 Read vx# : Read vy# : Read vz# ;8th..10th value is pitch/yaw/roll
 For vi=0 To nverts-1 ;vertices, nVerts*3
  Read vx# : Read vy# : Read vz#
  VertexCoords surf,vi,vx#,vy#,vz#
 If onetriuvset=0
  For vi=0 To nverts-1 ;uv coords, nVerts*2
   Read vu# : Read vv#
   VertexTexCoords surf,vi,vu#,vv#

 Return mesh ;mesh handle

End Function


SimplePortal 2.3.6 © 2008-2014, SimplePortal