Badge

Karl Zylinski

Game programmer

Hi,
Since I’m really interested in 3D engine programming I’m trying to learn all the important pieces needed in order to write one. This post is about one of them.


All games need models! I grew tired of testing my OpenGL applications against primitives, so I just of hacked together a .obj model loader. Obj is a very simple format, it does not support animation, but it’s a good starting point! The final loader is shown above with a model made by my roomie Johan Zdybal.

The obj-files are very simple, all the vertices in the model are specified like this:
v 0.168040 4.573096 10.398131
v 0.168040 -0.818835 10.398131
v 1.465187 4.573096 10.398131
v 1.465187 -0.818835 10.398131
v 1.481749 4.569535 -10.036242
v 1.481749 1.310191 -9.994753

v means that the line specifies a vertex. The three following numbers are the vertices coordinates x, y and z delimited by a space.

The texture coordinates, ranging from 0 to 1 are specified like this:
vt 0.083441 0.475771
vt 0.083441 0.258662
vt 0.069730 0.227006
vt 0.069730 0.461473
vt 0.095671 0.911054
vt 0.095671 0.992455

Note that these are just context-less texture coords, they are in this pure form not mapped to any vertex, that happens at a later stage.

Then comes the vertex-normals, these specify in which direction our vertices point:
vn 0.945424 0.280501 0.165809
vn -0.955727 -0.272520 0.110988
vn -0.907657 0.087183 0.410558
vn -0.357291 -0.276682 -0.892071
vn 0.356456 -0.479082 -0.802134
vn -0.389661 -0.811027 -0.436348
vn 0.391081 -0.854599 -0.341637

These are just like the vertices specified with x, y and z. Just like the texture coords, these are just pure normals with no context, we do not at this stage know which vertices they’re associated with.

Lastly comes the oh so important faces:
f 136/444/213 140/668/376 137/667/216
f 137/667/216 140/668/376 141/675/378
f 138/149/215 137/145/216 142/573/379
f 142/573/379 137/145/216 141/574/378
f 135/690/214 138/689/215 139/692/377
f 139/692/377 138/689/215 142/691/379
f 140/200/376 139/199/377 66/547/380
f 66/547/380 139/199/377 63/548/381

Each of these lines specify a triangle! In order to specify a triangle you probably want 3 vertices, 3 texture coordinates and 3 vertex normals. Each line here specifies this! These lines are formatted like this:
f vertexindex/texcoordindex/normalindex vertexindex/texcoordindex/normalindex vertexindex/texcoordindex/normalindex

Each index simply points to a specific vertex, texture coord or normal. They are indexed in the order they appear in the obj-file, the first specified vertex has index 1, the second has index 2 and so forth! (No zero based index?! Turds!)

That kind of it for now, I’m not showing any code since it’s too fugly.
XOXO


1 Comment to “Bits and pieces: Obj model loader”

  1. [...] is another blog entry about one of the fundamental building blocks of 3D game engines. The previous post covered loading models using a obj model loader. This post will describe how height maps can be [...]

Leave a Reply