Dark Bit Factory & Gravity
PROGRAMMING => Freebasic => Topic started by: Dr_D on December 11, 2006
-
It was ported to FreeBASIC as an experiment. We're experimenting with different file formats for our Rogue3D game. When I say we, I mean rdc and I are working on a Rogue3D game. hehehe :cheers:
I haven't fully tested any models with this yet, but it seems to be printing the correct vertices & stuff. I hope someone gets some use out of it. ;)
Const False = 0, True = Not False
#define Chunk_ID(a,b,c,d) ( ((a)) Or (b Shl(8)) Or (c Shl(16)) Or (d Shl(24)))
#define Chunk_2String(_Chunk) Chr$(_Chunk)+Chr$(_Chunk Shr 8)+Chr$(_Chunk Shr 16)+Chr$(_Chunk Shr 24)
#define ID_BB3DÂ Chunk_ID( Asc("B"), Asc("B"), Asc("3"), Asc("D") )
#define ID_TEXSÂ Chunk_ID( Asc("T"), Asc("E"), Asc("X"), Asc("S") )
#define ID_VRTSÂ Chunk_ID( Asc("V"), Asc("R"), Asc("T"), Asc("S") )
#define ID_ANIMÂ Chunk_ID( Asc("A"), Asc("N"), Asc("I"), Asc("M") )
#define ID_KEYSÂ Chunk_ID( Asc("K"), Asc("E"), Asc("Y"), Asc("S") )
#define ID_BRUSÂ Chunk_ID( Asc("B"), Asc("R"), Asc("U"), Asc("S") )
#define ID_TRISÂ Chunk_ID( Asc("T"), Asc("R"), Asc("I"), Asc("S") )
#define ID_MESHÂ Chunk_ID( Asc("M"), Asc("E"), Asc("S"), Asc("H") )
#define ID_BONEÂ Chunk_ID( Asc("B"), Asc("O"), Asc("N"), Asc("E") )
Declare Sub b3dExitChunk()
Declare Sub Read_String( Byval File_Num As Uinteger, Byref tString As String )
Declare Sub b3dSetFile( Byref file As Uinteger )
Declare Function Dump_B3D() As Integer
Declare Function b3dReadByte() As Byte
Declare Function b3dReadInt() As Integer
Declare Function b3dReadFloat() As Single
Declare Function b3dReadString() As String
Declare Function b3dReadChunk() As String
Declare Function b3dChunkSize() As Integer
Dim Shared As Integer b3d_Stack(100), b3d_file, b3d_tos
Dim As Uinteger File_Num = Freefile
b3dSetFile( File_Num )
Open "Models/B3D/Test_Cylinder.b3d" For Binary As #File_Num
  Dim As String Chunk
  Dim As Integer Version
  If b3dReadChunk()<>"BB3D" Then
    Print "Invalid b3d file"
  End If
  Version = b3dReadInt()/100
  Print Version
  If Version>0 Then
    Print "Invalid b3d file version"
  End If
  Dump_B3D( )
Close #File_Num
Sleep
Function Dump_B3D() As Integer
 Â
  Dim As String tname, Chunk
  Dim As Integer flags, n_frames, n_keys, sz, frame, blend, n_texs, k, j, tex_id
  Dim As Integer tc_sets, tc_size, n_verts, brush_id
  Dim As Integer v0,v1,v2, n_tris, n_weights, vertex_id
  Dim As Single fps = Any, fx = Any, weight = Any
  Dim As Single key_px = Any, key_py = Any, key_pz = Any
  Dim As Single key_sx = Any, key_sy = Any, key_sz = Any
  Dim As Single key_rw = Any, key_rx = Any, key_ry = Any, key_rz = Any
  Dim As Single x_pos = Any, y_pos = Any, z_pos = Any
  Dim As Single x_scl = Any, y_scl = Any, z_scl = Any
  Dim As Single w_rot = Any, x_rot = Any, y_rot = Any, z_rot = Any
  Dim As Single rot = Any, lfa = Any
  Dim As Single red = Any, grn = Any, blu = Any, alp = Any, shi = Any
  Dim As Single x = Any, y = Any, z = Any
  Dim As Single nx = Any, ny = Any, nz = Any
 Â
 Â
  Do While b3dChunkSize()
        chunk = b3dReadChunk()
    Print "Chunk: " & chunk & " size =" & b3dchunksize()
   Â
        Select Case chunk
        Case "ANIM"
            flags  = b3dReadInt()
            n_frames = b3dReadInt()
            fps   = b3dReadFloat()
      Print "Animation Flags/fps " & flags & "/" & fps
    Case "KEYS"
            flags=b3dReadInt()
            sz=4
            If flags And 1 Then sz=sz+12
            If flags And 2 Then sz=sz+12
            If flags And 4 Then sz=sz+16
            n_keys=b3dChunkSize()/sz
            If n_keys*sz=b3dChunkSize() Then
        Print "keys " & n_keys
                'read all keys in chunk
                While b3dChunkSize()
                    frame = b3dReadInt()
                    If flags And 1 Then
                        key_px = b3dReadFloat()
                        key_py = b3dReadFloat()
                        key_pz = b3dReadFloat()
          End If
          If flags And 2 Then
            key_sx = b3dReadFloat()
            key_sy = b3dReadFloat()
            key_sz = b3dReadFloat()
          End If
          If flags And 4 Then
            key_rw = b3dReadFloat()
            key_rx = b3dReadFloat()
            key_ry = b3dReadFloat()
            key_rz = b3dReadFloat()
          End If
        Wend
      Else
        Print "Illegal number of keys!!!"
      End If
    Case "TEXS"
      While b3dChunkSize()
        tname = b3dReadString()
        flags = b3dReadInt()
        blend = b3dReadInt()
        x_pos = b3dReadFloat()
        y_pos = b3dReadFloat()
        x_scl = b3dReadFloat()
        y_scl = b3dReadFloat()
        rot  = b3dReadFloat()
        Print tname
      Wend
    Case "BRUS"
      n_texs=b3dReadInt()
      'read all brushes in chunk...
      While b3dChunkSize()
        tname = b3dReadString$()
        red  = b3dReadFloat()
        grn  = b3dReadFloat()
        blu  = b3dReadFloat()
        alp  = b3dReadFloat()
        shi  = b3dReadFloat()
        blend = b3dReadInt()
        fx  = b3dReadInt()
        For k=0 To n_texs-1
          tex_id=b3dReadInt()
        Next
        Print tname
      Wend
    Case "VRTS"
      flags  = b3dReadInt()
      tc_sets = b3dReadInt()
      tc_size = b3dReadInt()
      sz=12+tc_sets*tc_size*4
      If flags And 1 Then sz = sz + 12
      If flags And 2 Then sz = sz + 16
      n_verts=b3dChunkSize()/sz
      If n_verts*sz = b3dChunkSize() Then
        Print "vertex count: " & n_verts
        'read all verts in chunk
        While b3dChunkSize()
          x = b3dReadFloat()
          y = b3dReadFloat()
          z = b3dReadFloat()
          Print x,y,z
          If flags And 1 Then
            nx = b3dReadFloat()
            ny = b3dReadFloat()
            nz = b3dReadFloat()
          End If
          If flags And 2 Then
            red = b3dReadFloat()
            grn = b3dReadFloat()
            blu = b3dReadFloat()
            lfa = b3dReadFloat()
          End If
          'read tex coords...
          For j=1 To tc_sets*tc_size
            Dim As Single t
            t = b3dReadFloat()
          Next
        Wend
      Else
        Print "Illegal number of vertices!!!"
      End If
    Case "TRIS"
      brush_id = b3dReadInt()
      sz=12
      n_tris = b3dChunkSize()/sz
      If n_tris * sz = b3dChunkSize() Then
        Print "Triangle count: " & n_tris
        'read all tris in chunk
        While b3dChunkSize()
          v0 = b3dReadInt()
          v1 = b3dReadInt()
          v2 = b3dReadInt()
         Â
        Wend
      Else
        Print "Illegal number of triangles!!!"
      End If
    Case "MESH"
      brush_id = b3dReadInt()
      Print "Brush ID " & brush_id
    Case "BONE"
      sz=8
      n_weights = b3dChunkSize()/sz
      If n_weights * sz = b3dChunkSize() Then
        'read all weights
        Print "Weights " & n_weights
        While b3dChunkSize()
          vertex_id = b3dReadInt()
          weight = b3dReadFloat()
        Wend
      Else
        Print "Illegal number of bone weights!!!"
      End If
    Case "NODE"
      tname = b3dReadString$()
      x_pos = b3dReadFloat()
      y_pos = b3dReadFloat()
      z_pos = b3dReadFloat()
      x_scl = b3dReadFloat()
      y_scl = b3dReadFloat()
      z_scl = b3dReadFloat()
      w_rot = b3dReadFloat()
      x_rot = b3dReadFloat()
      y_rot = b3dReadFloat()
      z_rot = b3dReadFloat()
      Print "name = " & tname
    End Select
   Â
    Dump_B3D()     'load the sub_chunks
    b3dExitChunk()        'exit this chunk
   Â
  Loop
  Return True
End Function
Sub b3dSetFile( Byref file As Uinteger )
    b3d_tos=0
    b3d_file = file
End Sub
Function b3dReadByte() As Byte
  Dim As Byte rbyte
  Get #b3d_file,, rbyte
    Return rbyte
End Function
Function b3dReadInt() As Integer
  Dim As Integer rInt
  Get #b3d_file,, rInt
  Return rInt
End Function
Function b3dReadFloat() As Single
  Dim As Single rFloat
  Get #b3d_file,, rFloat
  Return rFloat
End Function
Function b3dReadString() As String
  Dim As String T
  Dim As Byte ch
    Do
        ch=b3dReadByte()
        If ch=0 Then
      Return T
    End If
        T+=Chr$(ch)
  Loop
End Function
Function b3dReadChunk() As String
  Dim As Integer k, sz
  Dim As String Tag
    For k=1 To 4
        tag+=Chr$(b3dReadByte())
  Next
    sz = b3dReadInt()
    b3d_tos+=1
    b3d_stack(b3d_tos) = Seek(b3d_file)+sz
    Return Tag
End Function
Sub b3dExitChunk()
  Seek b3d_file, b3d_stack(b3d_tos)
    b3d_tos-=1
End Sub
Function b3dChunkSize() As Integer
  Dim As Integer rint
    rint = b3d_stack(b3d_tos)- Seek(b3d_file)
  Return rint
End Function
-
Nice one Dr_D, something I'm in need of is some kind of loader so this could be handy.
-
Cool. The only real problem I can see with it, is the recursion. It might hit a stack overflow, if you send a structure in the header repeatedly. But... I haven't tested... hopefully, it wont. :o
-
Looks Smart Dr D dude.
-
Thanks. ;) Anyone tried using it yet? I might have to end up using this. I don't really want to, but I'm having a hell of a time with this Lightwave loader. It seems like every time i port a new function, the loader has some other quirky function that also needs to be ported. It becomes a real pain in the arse when you don't know squat about C++.
A reason for the Lightwave loader? I model in Lightwave, almost exclusively... just because I love it. I've been working with Lightwave for a few years now. I just love the program. Those guys rock! :D
Anyway, I'd like to know if anyone reading this thread has had a successful attempt at loading, displaying, using weight-maps with this code. I haven't really tried yet because I've been working on the Lightwave loader so much. :)