|
Post by ninadeshinibida on Oct 23, 2011 8:18:42 GMT -5
Firs we most know what is a .obj
this kind of files contains Vertex data of a Poligonal 3d model
a simple obj file looks like this
V 0 0 0 vt 0 0 vn 0 0 0 f 0/0/0 0/0/0 0/0/0
the "v" contains the Vertex X,Y,Z GEOMETRY coords values of each point of a model x y z v 0 0 0
this data draws the solid geometry of the model (Shape).
The "vt" contains the Vertex U,V TEXTURE coords values of each point of a model
u v vt 0 0
this data draws the texture of the model (color).
The "vn" contains the Vertex X,Y,Z NORMAL coords values of each point of a model
x y z vn 0 0 0
this data draws the Normals of the model (LIght an shadow).
Each vertex ask for a "v","vt" and "vn" , so in an model of three points you will see:
V 0 0 0 V 0 0 0 V 0 0 0 vt 0 0 vt 0 0 vt 0 0 vn 0 0 0 vn 0 0 0 vn 0 0 0
or
(Vert1) (Vert2) (Vert3) V 0 0 0 V 0 0 0 V 0 0 0
(Vert1)(Vert2)(Vert3) vt 0 0 vt 0 0 vt 0 0
(Vert1) (Vert2) (Vert3) vn 0 0 0 vn 0 0 0 vn 0 0 0
The f contains the Order of how to draw Vertex data to compose the model
f v/vt/vn v/vt/vn v/vt/vn
or f Geometry/Texture/Normal Geometry/Texture/Normal Geometry/Texture/Normal
the "f" also have two formats TRIANGLES and QUADS
in the Triangles format you will read three values before the f
(point1)(point2)(point3)= Triangle f v/vt/vn v/vt/vn v/vt/vn
in the Quads format you will read Four values before the f
(point1)(point2)(point3)(point4)= Quad f v/vt/vn v/vt/vn v/vt/vn v/vt/vn
Triangles Format is the most common because all Quads can split on triangles
some professional modelers like Maya, 3D MAX or blender adds extra data in the file so don´t confuse
|
|
|
Post by ninadeshinibida on Oct 23, 2011 8:20:16 GMT -5
theres some code to read and display and obj
'getting the vertex data and the geometry faces Order dir=OpenFileRead(WhereToOpen$+objName$) do Type$=lcase$(ReadText(dir, True)) if Type$="v" then'-----------------------------Vertex Info Verts=Verts+1 Vert#(Verts)(0)=val(ReadText(dir, True)) Vert#(Verts)(1)=val(ReadText(dir, True)) Vert#(Verts)(2)=val(ReadText(dir, True)) elseif Type$="vt" then'------------------------textureinfo Textures=Textures+1 Texture#(Textures)(0)=val(ReadText(dir, True)) Texture#(Textures)(1)=val(ReadText(dir, True)) elseif Type$="f" then'-----------------------------Geometry Face Info(Order) Faces=Faces+1 Face(Faces)(0)=val(ReadText(dir, True)) Face(Faces)(1)=val(ReadText(dir, True)) Face(Faces)(2)=val(ReadText(dir, True)) endif loop until EndOfFile(dir)
'getting the texture faces order
do Type$=ReadChar(dir) if Type$="f" then TexFaces=TexFaces+1 TexPoint=-1 elseif Type$="/" then'----------------------------Texture Face Info(Order) TexPoint=TexPoint+1 FakeTexFace(TexFaces)(TexPoint)=val(ReadText(dir, True)) endif loop until EndOfFile(dir(a)) for b=1 to TexFaces if FakeTexFace(b)(0)<>0 then Decode=Decode+1 TexFace(Decode)(0)=FakeTexFace(b)(0) TexFace(Decode)(1)=FakeTexFace(b)(1) TexFace(Decode)(2)=FakeTexFace(b)(2) endif next TexFaces=Decode
|
|
|
Post by ninadeshinibida on Oct 23, 2011 8:21:33 GMT -5
now you have read froom the file Geometry and Shape of the model get the normals order sometimes don´t works so i recomend you to calculate them froom vertex geometry data like this
'Making normals
for a=1 to Faces(objindex) f=Face(objindex)(a) FNorm#(a) = Normalize(CrossProduct(Vert#(objindex)(f(1)) - Vert#(objindex)(f(0)), Vert#(objindex)(f(2)) - Vert#(objindex)(f(1)))) next 'Calculate per-vertex normals for a=1 to Verts(objindex) for b=1 to Faces(objindex) for c=0 to 2 if Face(objindex)(b)(c)=a then Normal#(objindex)(a)=Normal#(objindex)(a)+FNorm#(b) endif next next Normal#(objindex)(a)=Normalize(Normal#(objindex)(a)) next
|
|
|
Post by ninadeshinibida on Oct 23, 2011 8:22:57 GMT -5
all the data is now set lets draw
'GL Display
glBegin(GL_triangles) for i = 1 to Faces'---------------------------Geometry Faces glnormal3fv(Normal#(Face(i)(0)))'-------------Point0 GLTEXCOORD2Fv(Texture#(TexFace(i)(0))) glVertex3fv(Vert#(Face(i)(0))) glnormal3fv(Normal#(Face(i)(1)))'-------------Point1 GLTEXCOORD2Fv(Texture#(TexFace(i)(1))) glVertex3fv(Vert#(Face(i)(1))) glnormal3fv(Normal#(Face(i)(2)))'-------------Point2 GLTEXCOORD2Fv(Texture#(TexFace(i)(2))) glVertex3fv(Vert#(Face(i)(2))) next glend()
feel free to modify the code if some one wants to add or correct this litttle explanation please do it
I hope it helps on your game making
|
|
|
Post by shadow008 on Oct 23, 2011 16:15:49 GMT -5
This is my bit on the topic. dl.dropbox.com/u/5319168/Object%20Loader.zipStill a work in progress. Uses the GLShaderLanguage so OpenGL 2.0 is required(plugin included). Still fixing the vertex normals and working on the crude VBO display method(plugin also included). Just threw it all together in one big zip file so there might be some random files in there. Do whatever you want with it, I dont mind.
|
|