Author Topic: [Bmax] .ase model loader  (Read 2472 times)

0 Members and 1 Guest are viewing this topic.

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 909
  • Karma: 67
    • View Profile
[Bmax] .ase model loader
« on: March 12, 2009 »
Here is a function I have made, which loads a .ase model file. It was made specific to my current openGL 3D engine, so its not 100% generic, but the way a .ase 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, normals, faces.

Code: [Select]
Function loadASEmodel:TMesh(loadfile:String)
Local tmp:TMesh = TMesh.init()

Local v1:String, v2:String, v3:String
Local f1:Int, f2:Int, f3:Int, l:String
Local a:Int, b:Int, i:Int, vertices:Int, faces:Int

Local filein:TStream = ReadFile(loadfile)

Repeat
l = ReadLine(filein)
Until Instr( l, "*MESH_NUMVERTEX" ) ' find the point where the vetrice And face number is
l = Trim( l ) ' remove all unnecessary spaces
l = Right( l, Len( l )-16 ) ' find the vertex number
vertices = Int( l )

l = ReadLine(filein) ' read next line to get face number
l = Trim( l ) ' remove all unnecessary spaces
l = Right( l, Len( l )-15 ) ' find the face number
faces = Int( l )

l = ReadLine(filein) ' skip one line

For a = 1 To vertices
l = ReadLine( filein )
l = Trim( l ) ' remove all unnecessary spaces
l = Right( l, Len( l )-11 ) ' find the vertex data
l = Replace( l, " ", "," )
v1 = NthField( l, Chr(9), 2 )
v2 = NthField( l, Chr(9), 3 )
v3 = NthField( l, Chr(9), 4 )
tmp.addVertex(Float(v1),Float(v2),Float(v3))
Next

Repeat
l = ReadLine(filein)
Until Instr( l, "*MESH_FACE_LIST {" ) ' locate face list
For a = 1 To faces
l = ReadLine(filein)
l = Trim( l ) ' remove all unnecessary spaces
l = Right( l, Len( l ) - 10 )
l = Left(l, 61)
f1 = Int(Mid(l, 13,5))
f2 = Int(Mid(l, 21,5))
f3 = Int(Mid(l, 29,5))
tmp.addFace(f1,f2,f3)
Next

Repeat
l = ReadLine(filein)
Until Instr( l, "*MESH_NORMALS {" ) ' find the point where the vetrice And face normals are
For a = 1 To faces
l = ReadLine(filein)
l = Trim( l )
l = Right( l, Len( l )-19)
If Left( l, 1 ) = Chr(9) Then l = Right( l, Len( l )-1 )
l = Replace( l, Chr(9), "," )
v1 = NthField( l, ",", 1 )
v2 = NthField( l, ",", 2 )
v3 = NthField( l, ",", 3 )
tmp.addFaceNormal(Float(v1),Float(v2),Float(v3))
For b = 1 To 3
l = ReadLine(filein)
l = Trim( l )
l = Right( l, Len( l$ )-20)
If Left( l, 1 ) = Chr(9) Then l = Right( l, Len( l )-1 )
l = Replace( l, Chr(9), "," )
v1 = NthField( l, ",", 1 )
v2 = NthField( l, ",", 2 )
v3 = NthField( l, ",", 3 )
tmp.addVertexNormal(Float(v1),Float(v2),Float(v3))
Next
Next

CloseFile(filein)
Print "V:"+vertices
Print "F:"+faces
Return tmp
End Function

' Given a String, a delimiter, And a n -- returns the nth Field
Function NthField:String(s:String, delim:String, n:Int)
Local o:Int = 1
For Local i:Int = 1 To n - 1
o = Instr(s, delim, o)
If o = 0 Then
Return ""
End If
o = o + 1
Next
Local p:Int = Instr(s, delim, o)
If p = 0 Then
Return Mid(s, o)
Else
Return Mid(s, o, p - o)
End If
End Function

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: [Bmax] .ase model loader
« Reply #1 on: March 13, 2009 »
Can I ask what is the ase format used by?
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 909
  • Karma: 67
    • View Profile
Re: [Bmax] .ase model loader
« Reply #2 on: March 13, 2009 »
ASE stands for Ascii Scene Export. This is an ascii text based format associated with 3DS Max.