Author Topic: [Bmax] .obj model loader  (Read 3133 times)

0 Members and 1 Guest are viewing this topic.

Offline zawran

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

Code: [Select]
Function loadOBJmodel:TMesh(loadfile:String)
Local time:Int = MilliSecs()
Local tmp:TMesh = TMesh.init()

Local sline:String, ck:String
Local v1:String, v2:String, v3:String
Local f1:String, f2:String, f3:String
Local vertices:Int, texUV:Int, vNormals:Int, faces:Int

Local filein:TStream = ReadFile(loadfile)

' locate vertex data
Repeat
sline = ReadLine(filein)
ck = Left( sline,2 )

Select ck
Case "v " ' vertex
v1 = NthField( sline," ",2 )
v2 = NthField( sline," ",3 )
v3 = NthField( sline," ",4 )
tmp.addVertex(Float(v1),Float(v2),Float(v3))
vertices:+ 1
Case "vt" ' vertex texture
v1 = NthField( sline," ",2 )
v2 = NthField( sline," ",3 )
tmp.addTexUV(Float(v1),Float(v2))
Print v1+","+v2
texUV :+ 1
Case "vn" ' vertex normal
v1 = NthField( sline," ",2 )
v2 = NthField( sline," ",3 )
v3 = NthField( sline," ",4 )
tmp.addVertexNormal(Float(v1),Float(v2),Float(v3))
vNormals :+ 1
Case "f " ' face
sline = Right(sline,Len(sline)-2) ' trim start of string
sline = Replace(sline," ","/") ' replace spaces
' vertex pointers
f1 = NthField( sline,"/",1 )
f2 = NthField( sline,"/",4 )
f3 = NthField( sline,"/",7 )
tmp.addFVpointer(Int(f1)-1,Int(f2)-1,Int(f3)-1)
' texture pointers
f1 = NthField( sline,"/",2 )
f2 = NthField( sline,"/",5 )
f3 = NthField( sline,"/",8 )
tmp.addFVTexUV(Int(f1)-1,Int(f2)-1,Int(f3)-1)
' normal pointers
f1 = NthField( sline,"/",3 )
f2 = NthField( sline,"/",6 )
f3 = NthField( sline,"/",9 )
tmp.addFVnormal(Int(f1)-1,Int(f2)-1,Int(f3)-1)
faces :+ 1
End Select

Until Eof(filein)
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
« Last Edit: March 12, 2009 by zawran »

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Re: [Bmax] .obj model loader
« Reply #1 on: March 13, 2009 »
Thanks for sharing the function!

K+
Challenge Trophies Won:

Offline TinDragon

  • Pentium
  • *****
  • Posts: 644
  • Karma: 24
    • View Profile
    • J2K's blog
Re: [Bmax] .obj model loader
« Reply #2 on: March 13, 2009 »
Nice work, I will have to look at these loaders once I move on from cubes :D


Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 909
  • Karma: 67
    • View Profile
Re: [Bmax] .obj model loader
« Reply #3 on: March 13, 2009 »
I am back at using cubes, but only because I testing some stuff, and cubes are easy to use :) Once those test are done, I will experiment a bit more with objects. Its nice to know, at others find my stuff useful :)