.obj File Loader Demo
The Wavefront .obj file format holds all the information you need to load a simple object: positions, face indices, vertex normals and more. In this demo we load the classic utah teapot.
Controls and some things to try:
- Rotate the scene with your mouse and move with WS + the arrow keys.
- Switch to the basic shader program to visualize vertices with random colors.
Thankfully the .obj file format is actually very easy to parse. Especially since in most cases obj files will have only triangulated faces, but note that this is not required by the format. This is a good demo for lighting models, additionally we can optionally set random colors on the vertices so the shape can be observed easily without lighting. Below is a snippet of the format from wikipedia.
# List of geometric vertices, with (x, y, z, [w]) coordinates, w is optional and defaults to 1.0.
v 0.123 0.234 0.345 1.0
v ...
...
# List of texture coordinates, in (u, [v, w]) coordinates, these will vary between 0 and 1. v, w are optional and default to 0.
vt 0.500 1 [0]
vt ...
...
# List of vertex normals in (x,y,z) form; normals might not be unit vectors.
vn 0.707 0.000 0.707
vn ...
...
# Parameter space vertices in (u, [v, w]) form; free form geometry statement (see below)
vp 0.310000 3.210000 2.100000
vp ...
...
# Polygonal face element (see below)
f 1 2 3
f 3/1 4/2 5/3
f 6/4/1 3/5/3 7/6/5
f 7//1 8//2 9//3
f ...
...
# Line element (see below)
l 5 8 1 2 4 9