Author Topic: [Bmax] .3ds model loader  (Read 2332 times)

0 Members and 1 Guest are viewing this topic.

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 909
  • Karma: 67
    • View Profile
[Bmax] .3ds model loader
« on: March 12, 2009 »
Here is a function I have made, which loads a .3ds model file. It was made specific to my current openGL 3D engine, so its not 100% generic, but the way a .3ds file is processed would be the same regardless of language, and Bmax is fairly easy to read through, so I figured that someone might be able to gain something from it.

It will process vertices, faces and texture UV.

Code: [Select]
Function load3DSmodel:TMesh(file:String)
Local tmp:TMesh = TMesh.init()
Local stream:TStream, size:Int, id_node:String, size_node:Int, b:Byte, i:Int

stream = ReadStream(file)
If stream = Null Then Return tmp ' file could not be loaded, return false
size = FileSize(file)
DebugLog "Open "+file+" | Size:"+size

While Not Eof(stream)
id_node = Right(Upper(Hex(ReadShort(stream))),4)
size_node = ReadInt(stream)
DebugLog "Id="+id_node+"  size="+size_node

Select id_node
Case "4D4D" 'Main node
Case "3D3D" 'Config
Case "4000" ' OBJECT BLOCK
Repeat ' read the name of the object
b = ReadByte(stream)   
If b = 0 Exit
tmp.name :+ Chr(b)
Until b = 0
DebugLog "Object name ="+tmp.name
Case "4100" ' MESH BLOCK
Case "4110" ' VERTICES LIST
Local t:Int = ReadShort(stream)
For i = 0 To t-1
tmp.addVertex(ReadFloat(stream),ReadFloat(stream),ReadFloat(stream)) ' default colors and tex uv for now
Next
DebugLog "    Vertices: "+t
Case "4120" ' FACE LIST
Local t:Int = ReadShort(stream)
For i = 0 To t-1
tmp.addFace(ReadShort(stream),ReadShort(stream),ReadShort(stream))
ReadShort(stream)
Next
DebugLog "    Faces: "+t
Case "4140" ' TEXTURE MAPPING COORDINATES LIST
Local t:Int = ReadShort(stream)
For i = 0 To t-1
tmp.addTexUV(ReadFloat(stream),ReadFloat(stream))
Next
DebugLog "    TexCoords: "+t
Default
' no ID was found that we need, so fastforward past this chunk
SeekStream(stream,StreamPos(stream)-6+size_node)
End Select
Wend
CloseStream stream
DebugLog "*** End of file ***"
Return tmp
End Function
« Last Edit: March 12, 2009 by zawran »

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: [Bmax] .3ds model loader
« Reply #1 on: March 14, 2009 »
Thanks Zawran!  I know how much effort you put in to making this, so massive karma for sharing your hard work.

Jim
Challenge Trophies Won: