Object Files

Object data defines a set of polygons and each polygon could have its own material property specified by a material name defined in the material library file (*.mtl).

Example:
g Box01
mtllib ./box.mt 
v -2.5 -2.5 10
v -2.5 2.5 10
v 2.5 2.5 10
v 2.5 -2.5 10
v -2.5 -2.5 15
v 2.5 -2.5 15
v 2.5 2.5 15
v -2.5 2.5 15 
# 8 verticies
vn 0 0 -1
vn 0 0 1
vn 0 -1 0
vn 1 0 0
vn 0 1 0
vn -1 0 0
# 6 normals
vt 0 0
vt 0 1
vt 1 1
vt 1 0
# 4 texture coordinates

usemtl m1
f 1/1/1 2/2/1 3/3/1

f 3/3/1 4/4/1 1/1/1
usemtl m2
f 5/1/2 6/2/2 7/3/2
f 7/3/2 8/4/2 5/1/2
usemtl m3
f 1/1/3 4/2/3 6/3/3
f 6/3/3 5/4/3 1/1/3
usemtl m1
f 4/1/4 3/2/4 7/3/4
f 7/3/4 6/4/4 4/1/4
usemtl m2
f 3/1/5 2/2/5 8/3/5
f 8/3/5 7/4/5 3/1/5
usemtl m3
f 2/1/6 1/2/6 5/3/6
f 5/3/6 8/4/6 2/1/6


g obj_name: specify the object name
mtllib filename: specify the material library file
v x y z: define the position of a set of vertex. The first vertex is indexed by 1 and subsequent vertices are numbered sequentially
# comment: indicate this line is a comment

vn x y z: define a set of normal vector. The first normal vector is indexed by 1 and subsequent vertices are numbered sequentially

vt u v: define a set of texture coordinate
usemtl material_name: specify which material defined in the material library file you want to use.

f v1/t1/n1 v2/t2/n2 v3/t3/n3: define a polygon consisted of 3 vertex. 
(v1,v2,v3) is the index of vertex positoin array for each vertex. 
(t1,t2,t3) is the index of texture coordinate array for each vertex. 
(n1,n2,n3) is the index of normal vector array for each vertex.


You can see more informations in 
http://netghost.narod.ru/gff/graphics/summary/waveobj.htm


Material Files

Example:
# Max2Mtl Version 4.0 Mar

newmtl m1
Ka 0.0 0.0 0.0
Kd 1.0 0 1.0
Ks 0.3 0.3 0.3
d 1.0
Ns 2.2
illum 2
newmtl m2
Ka 0.0 0.0 0.0
Kd 0.0 1 0.0
Ks 0.3 0.3 0.3
d 1.0
Ns 2.2
illum 2
newmtl m3
Ka 0.0 0.0 0.0
Kd 0.0 0.0 1.0
Ks 0.3 0.3 0.3
d 1.0
Ns 2.2
illum 2


# comment: indicate this line is a comment
newmtl material_name: create a new material
Ka r g b: define the AMBIENT term
Kd r g b: define the DIFFUSE term
Ks r g b: define the SPECULAR term
d value: see the reference.
Ns value: define the Phong specular component.
illum value: see the reference.


You can see more informations in http://en.wikipedia.org/wiki/Wavefront_.obj_file#Material_template_library


How to use loader

In order to make you concentrate on writing OpenGL code, we provide a loader to help you load *.obj file.
You can simply add the loader to your workspace, and use the class named mesh to load *.obj file.
(You can download Example for use, it includes mesh.cpp and mesh.h.)

The class mesh has members as follows:
class material {
float Ka[4];
float Kd[4];
float Ks[4];
float Ns;
...
std::string map_Kd;
...
}

class Vertex {
int v;
int n;
int t;
}

class Vec3 {
float ptr[3];
}

class FACE {
Vertex v[3];
int m;
}

std::vector<material> mList;
vector<Vec3> vList;
vector<Vec3> nList;
vector<Vec3> tList;
vector<FACE> faceList;
int vTotal;
int nTotal;
int tTotal;
int fTotal;

class Vertex
v:
 index of vList

n: index of nList
t: index of tList
class FACE
v:
 three vertices
m: index of mList
mList: an array of all materials.

vList: an array of all vertex position.

nList: an array of all normal vector.
tList: an array of all texture coordinate.
faceList: an array of all faces (polygons).
vTotal: the number of vertices.
nTotal: the number of normal vectors.
tTotal: the number of texture coordinate.
fTotal: the number of faces.

so here is an example to load an object file:

#include "mesh.h"
mesh *object;

main()
{

object = new mesh("box.obj");
delete object;

}

and after you load the object file, you can use

 

int lastMaterial = -1;
for(size_t i=0;i < object->fTotal;++i)
{

// set material property if this face used different material
if(lastMaterial != object->faceList[i].m)
{

lastMaterial = (int)object->faceList[i].m;
glMaterialfv(GL_FRONT, GL_AMBIENT , object->mList[lastMaterial].Ka);
glMaterialfv(GL_FRONT, GL_DIFFUSE , object->mList[lastMaterial].Kd);
glMaterialfv(GL_FRONT, GL_SPECULAR , object->mList[lastMaterial].Ks);
glMaterialfv(GL_FRONT, GL_SHININESS, &object->mList[lastMaterial].Ns);

//you can obtain the texture name by object->mList[lastMaterial].map_Kd
//load them once in the main function before mainloop
//bind them in display function here

}

glBegin(GL_TRIANGLES);
for (size_t j=0;j<3;++j)
{

//textex corrd. object->tList[object->faceList[i][j].t].ptr
glNormal3fv(object->nList[object->faceList[i][j].n].ptr);
glVertex3fv(object->vList[object->faceList[i][j].v].ptr);

}
glEnd();

}

 

to access each polygon data directly.

Go to top