I am fairly certain that my .obj model loader is working as expected, so I thought that I would post it in case someone else would be able to get something out of it.
Function loadOBJmodel:TMesh(loadfile:String)
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
Its written in BlitzMax since that the language I use, but I am certain that it wouldn't be much trouble to translate it into other languages as well.