PF prefab file reader (deepMesh custom 3D primitives)

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
D.J.Peters
Posts: 8631
Joined: May 28, 2005 3:28
Contact:

PF prefab file reader (deepMesh custom 3D primitives)

Post by D.J.Peters »

only usefull for users of deepMesh

Code: Select all

' *.pf prefab file reader (deepMesh custom 3D primitives)
#define DEBUG
#ifdef DEBUG
# define DPRINT(msg) open err for output as #99:print #99,msg:close 99#
#else
# define DPRINT(msg) :
#endif

#macro ReadChunk(n)
  get #hfile,,n##_count
  dprint("read " & #n & " " & n##_count.value & " times")
  if n##_count.value>0 then
    p##n##s=new P##n[n##_count.value]
    for i as integer=0 to n##_count.value-1
      dprint("read " & #n & " [" & i & "]")
      p##n##s[i]=new PF_##n
      p##n##s[i]->read hFile
    next
  end if
#endmacro


type PF_INT
  declare sub read(hFile as integer)
  as integer value
end type
sub PF_INT.read(hFile as integer)
  get #hFile,,value
  dprint("PF_INT    " & value)
end sub

type PF_UINT
  declare sub read(hFile as integer)
  as uinteger value
end type
sub PF_UINT.read(hFile as integer)
  get #hFile,,value
  dprint("COLOR      &H" & hex(value,0))
end sub
type PF_FLOAT
  declare sub read(hFile as integer)
  as single value
end type
sub PF_FLOAT.read(hFile as integer)
  get #hFile,,value
  dprint("PF_FLOAT  " & value)
end sub

type PF_STRING
  declare sub read(hFile as integer)
  as integer     nChars
  as zstring ptr pChars
end type
sub PF_STRING.read(hFile as integer)
  get #hFile,,nChars
  pChars = callocate(nChars+1)
  if nChars>0 then
    get #hFile,,*cptr(ubyte ptr,pChars),nChars
  dprint("PF_STRING " & *pChars)
  end if
end sub
  
type PF_FLOAT2
  declare sub read(hFile as integer)
  as PF_FLOAT u,v
end type
sub PF_FLOAT2.read(hFile as integer)
  dprint("FLOAT2 uv  ")
  u.read hFile
  v.read hFile
end sub

type PF_FLOAT3
  declare sub read(hFile as integer)
  as PF_FLOAT x,y,z
end type
sub PF_FLOAT3.read(hFile as integer)
  dprint("FLOAT3 xyz ")
  x.read hFile
  y.read hFile
  z.read hFile
end sub

type PF_TEXTURE
  declare sub read(hFile as integer)
  as PF_STRING tex_filename       ' Texture filename
  as PF_INT    tex_wrap           ' Irrlicht enum EMF_TEXTURE_WRAP
end type
type  PTEXTURE as PF_TEXTURE ptr
type PPTEXTURE as PTEXTURE ptr
sub PF_TEXTURE.read(hFile as integer)
  tex_filename.read hFile
  tex_wrap.read     hFile
end sub

type PF_VERTEX
  declare sub read(hFile as integer)
  as PF_FLOAT3 position           ' x,y,z vertex position
  as PF_FLOAT3 normal             ' x,y,z vertex normal
  as PF_FLOAT2 UV0                ' u,v texture coord set 0
  as PF_FLOAT2 UV1                ' u,v texture coord set 1
  as PF_UINT   color              ' ARGB Hexadecimal vertex color
end type
type  PVERTEX as PF_VERTEX ptr
type PPVERTEX as PVERTEX ptr
sub PF_VERTEX.read(hFile as integer)
  position.read hFile
  normal.read   hFile
  uv0.read      hFile
  uv1.read      hFile
  color.read    hFile
end sub 

type PF_TRIANGLE
  declare sub read(hFile as integer)
  as PF_INT tv0                   ' Triangle vertex 0
  as PF_INT tv1                   ' Triangle vertex 1
  as PF_INT tv2                   ' Triangle vertex 2
end type
type  PTRIANGLE as PF_TRIANGLE ptr
type PPTRIANGLE as PTRIANGLE ptr
sub PF_TRIANGLE.read(hFile as integer)
  DPRINT("PF_TRIANGLE.tv0,tv1,tv2")
  tv0.read hFile
  tv1.read hFile
  tv2.read hFile
end sub

type PF_MESHBUFFER
  declare sub read(hFile as integer)
  as PF_INT     vertex_count      ' Number of vertices to read
  as PPVERTEX    pVertexs
  as PF_INT     triangle_count    ' Number of triangles to read
  as PPTRIANGLE  pTriangles
end type
type PMESHBUFFER as PF_MESHBUFFER ptr
type PPMESHBUFFER as PMESHBUFFER ptr
sub PF_MESHBUFFER.read(hFile as integer)
  ReadChunk(vertex)
  ReadChunk(triangle)
end sub


type PF_FILE
  declare function Load(FileName as string) as integer
  as PF_STRING formatID           ' "IDM_PF_100"
  as PF_INT     meshbuffer_count  ' Number of meshbuffers to read
  as PPMESHBUFFER pMeshBuffers
end type

function PF_FILE.Load(FileName as string) as integer
  dim as integer hFile = FreeFile()
  if open(FileName,for binary,access read,as #hFile) then
    return 0
  end if
  formatID.read hFile
  if *formatID.pChars<>"IDM_PF_100" then
    close #hFile
    return 0
  end if
  dprint(*formatID.pChars)
  ReadChunk(meshbuffer)

  close #hfile
  return -1
end function

' 
' main
'
dim as PF_FILE Prefab
chdir exepath
if not Prefab.Load("teapot.pf") then
  print "error: !"
  beep:sleep:end
end if
sleep
Post Reply