Author Topic: [3D-Model Loader] for *.OBJ-Files  (Read 3998 times)

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5068
  • O Rly?
    • View Profile
[3D-Model Loader] for *.OBJ-Files
« on: 2012-Sep-02 »
Hi, here a *.OBJ-File (Wavefront OBJ file) loader for GLBasic.

See attachment

sourcecode:
Code: (glbasic) [Select]
// --------------------------------- //
// Project: OBJ Loader
// Start: Sunday, September 02, 2012
// IDE Version: 10.283


TYPE TVector3
x#;y#;z# // position
ENDTYPE

TYPE TVector2
x#;y#
ENDTYPE

TYPE TFace
vertex[3] AS TVector3
tex_coord[3] AS TVector2
vertex_norm[3] AS TVector3
ENDTYPE


TYPE TObj

filename$
texturename$

vertex[] AS TVector3
tex_coord[] AS TVector2
vertex_norm[] AS TVector3
faces[] AS TFace

filehandle%
tex_handle%
XObj_handle%

FUNCTION LOAD: name$

self.filehandle = GENFILE() // set the filehandle
self.filename$ = name$  //set filename

// if file exist

IF DOESFILEEXIST(self.filename$) AND OPENFILE(self.filehandle, self.filename$, 1)
//DEBUG "All OK, file exist!\n"

WHILE ENDOFFILE(self.filehandle) = 0
LOCAL tmp$, check$, splt$[]

READLINE self.filehandle, tmp$
check$ = MID$(tmp$, 0, 2)

SELECT check$
CASE "v "
SPLITSTR(tmp$,splt$[]," ")
//DEBUG "->"+splt$[1]+" : "+splt$[2]+" : "+splt$[3]+"<-\n"
LOCAL v AS TVector3

v.x = splt$[1]
v.y = splt$[2]
v.z = splt$[3]

DIMPUSH self.vertex[], v

CASE "vt"
SPLITSTR(tmp$,splt$[]," ")
//DEBUG "->"+splt$[1]+" : "+splt$[2]+"<-\n"
LOCAL v AS TVector2

v.x = splt$[1]
v.y = splt$[2]

DIMPUSH self.tex_coord[], v
CASE "vn"
SPLITSTR(tmp$,splt$[]," ")
//DEBUG "->"+splt$[1]+" : "+splt$[2]+" : "+splt$[3]+"<-\n"
LOCAL v AS TVector3

v.x = splt$[1]
v.y = splt$[2]
v.z = splt$[3]

DIMPUSH self.vertex_norm[], v
CASE "us"
SPLITSTR(tmp$,splt$[]," ")
self.texturename$ = splt$[1]
//DEBUG self.texturename$+"\n"
CASE "f "
LOCAL s$[]
SPLITSTR(tmp$,splt$[]," ")

LOCAL t AS TFace

FOR i = 1 TO 3
SPLITSTR(splt$[i],s$[],"/")
//DEBUG "->"+s$[0]+" : "+s$[1]+" : "+s$[2]+"<-\n"
LOCAL v=s$[0] , vt=s$[1], vn=s$[2]

t.vertex[i-1] = self.vertex[v-1]
t.tex_coord[i-1] = self.tex_coord[vt-1]
t.vertex_norm[i-1] = self.vertex_norm[vn-1]

NEXT

DIMPUSH self.faces[], t
ENDSELECT

WEND

// free some memory!
REDIM self.vertex[0]
REDIM self.tex_coord[0]
REDIM self.vertex_norm[0]

self.tex_handle = GENSPRITE()
LOADSPRITE self.texturename$, self.tex_handle

self.XObj_handle = GENX_OBJ()

X_OBJSTART self.XObj_handle
FOREACH t IN self.faces[]
X_OBJNEWGROUP
FOR i = 0 TO 2
X_OBJADDVERTEX t.vertex[i].x, t.vertex[i].y, t.vertex[i].z, t.tex_coord[i].x, t.tex_coord[i].y, RGB(0xff, 0xff, 0xff)
NEXT
NEXT
X_OBJEND

ENDIF
ENDFUNCTION

FUNCTION DRAW:
X_SETTEXTURE self.tex_handle, -1
X_DRAWOBJ self.XObj_handle, 0
ENDFUNCTION

ENDTYPE

[attachment deleted by admin]
« Last Edit: 2012-Sep-02 by Schranz0r »
I <3 DGArray's :D

PC:
AMD Ryzen 7 1700 @3.9GHz, 16GB HyperX Fury 3000MHz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline Slim

  • Mc. Print
  • *
  • Posts: 12
    • View Profile
Re: [3D-Model Loader] for *.OBJ-Files
« Reply #1 on: 2012-Sep-03 »
Thank you.

Offline Marmor

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 924
  • 96A285CC
    • View Profile
    • my youtube channel
Re: [3D-Model Loader] for *.OBJ-Files
« Reply #2 on: 2012-Sep-03 »
great !!!

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5068
  • O Rly?
    • View Profile
Re: [3D-Model Loader] for *.OBJ-Files
« Reply #3 on: 2012-Sep-03 »
thx, but it's not perfect...
there are to many different versions from *.obj out there :/

It's just a "basic"-loader :D
I <3 DGArray's :D

PC:
AMD Ryzen 7 1700 @3.9GHz, 16GB HyperX Fury 3000MHz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline mentalthink

  • Prof. Inline
  • *****
  • Posts: 3372
  • Integrated Brain
    • View Profile
Re: [3D-Model Loader] for *.OBJ-Files
« Reply #4 on: 2012-Sep-04 »
Very interesting info and Code... Thanks for sharing!!! :nw: :nw:

jestermon

  • Guest
Re: [3D-Model Loader] for *.OBJ-Files
« Reply #5 on: 2012-Sep-04 »
That source code takes me way back - to an obj loader I wrote about 10 years ago for Vpython. Interesting how similar the code looks - I guess the basics never change :)
http://jestermon.weebly.com/uploads/1/6/2/1/1621282/objloader.zip

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4336
    • View Profile
    • FUED
Re: [3D-Model Loader] for *.OBJ-Files
« Reply #6 on: 2012-Sep-04 »
...
there are to many different versions from *.obj out there :/
...

Yeah there are.

Current lightwave 3d obj struggles with the obj->ddd converter. It also struggles on a couple 3d apps around.
Strangely, if I send the obj to blender and save it again, it works fine

I´m tempted in giving this a try to see the results.
I wonder if it will preserve surfaces name or a vertex normal map (ie for the smooth/flat polys).

Gud job!