Saturday, July 23, 2022

Primer: Importing WaveFront OBJ Models

Overview 
Thus far we have worked with the cube geometric object to understand the concepts. In real world, more complex and detailed geometric objects or models are used. They are generated by sophisticated softwares such as blender that can be exported to plethora image formats.
Wavefront OBJ is one such image format.

Details
Wavefront OBJ file is an ASCII text based image format that can be parsed to obtain geometry information such as vertices, texture coordinates and normals along with triangles for rendering. Also, material properties such as ambient, diffuse and specular colors along with texture files can also be extracted.
The usually comes with a pair files. .obj  files contain geometry information and .mtl. contain material properties. Texture files are separate.

The following lists an example.
crate.obj
# Blender v3.0.0 OBJ File: ''
# www.blender.org
mtllib crate.mtl
o crate
v 0.467223 -0.137344 -0.716128
v -0.334052 -0.667593 -0.439009
v -0.818424 0.179215 -0.219235
v -0.017149 0.709464 -0.496354
v 0.818424 -0.179215 0.219235
v 0.017149 -0.709464 0.496354
v 0.334052 0.667593 0.439009
v -0.467223 0.137344 0.716128
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
vn 0.5395 -0.1586 -0.8269
vn -0.3857 -0.7709 -0.5069
vn -0.9450 0.2069 -0.2532
vn -0.0198 0.8192 -0.5731
vn 0.9450 -0.2069 0.2532
vn 0.0198 -0.8192 0.5731
vn 0.3857 0.7709 0.5069
vn -0.5395 0.1586 0.8269
usemtl _Crate1Material
s 1
f 1/1/1 2/2/2 3/3/3
f 3/3/3 4/4/4 1/1/1
f 5/5/5 6/6/6 2/7/2
f 2/7/2 1/8/1 5/5/5
f 7/9/7 8/10/8 6/11/6
f 6/11/6 5/12/5 7/9/7
f 4/13/4 3/14/3 8/15/8
f 8/15/8 7/16/7 4/13/4
f 2/17/2 6/6/6 8/15/8
f 8/15/8 3/18/3 2/17/2
f 5/5/5 1/19/1 4/20/4
f 4/20/4 7/16/7 5/5/5

The keyword mtllib represents the material library to use.
The keyword v represents x,y,z coordinates of a vertex.
The keyword vt represents u,v coordinates of a texture point.
The keyword vn represents x,y,z coordinates of a normal.
The keyword usemtl represents the material to use as described in the mtl file.
The keyword f represents three vertices of the triangle in the form of vertex/texture/normal.
For example in the above case, f 1/1/1 refers to (0.467223, -0.137344 ,-0.716128)/(1.0, 0.0)/(0.5395, -0.1586, -0.8269).

crate.mtl
# Blender MTL File: 'None'
# Material Count: 1

newmtl _Crate1Material
Ns 225.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 2

The keyword newmtl represents a material description.
The keyword Ns represents shininess of the specular color
The keyword Ka represents ambient color

The keyword Kd represents diffuse color
The keyword Ks represents specular color
The keyword Ke represents emissive color
The keyword map_Kd represents diffuse texture file. 



No comments:

Post a Comment