Model Loader in Java

I recently wrote a simple model loader in Java which is what this blog post is going to be about. I started off researching on the most easiest filetype to approach this issue. My first idea was to go ahead with FBX but I found out that FBX comes either in ASCII (easier to work with) and also in a binary format which is a proprietary of Autodesk. So I decided to work with OBJ files which were easier to parse and handle. In the future, I plan to extend this loader to incorporate other filetypes like COLLADA.

I started off by looking at an OBJ file I downloaded off the internet for free. The model I worked with was a T-Rex. An OBJ file mainly consists of three parts: vertices, facesĀ and normals.

Vertices are points in 3D space. This means is has a position in space which is represented using x, y and z. Here is a snippet of the vertices that came with the T-Rex model:

v -39.437401 11.184499 6.216902

Here, the vertex has values x = -39.437401, y = 11.184499 and z = 6.216902

Normals are used by your 3D application to determine the direction that light will bounce off of geometry. This is very helpful to get control over how the light reacts to certain materials on your 3D objects.

The line connecting different vertices are edges. A face is formed from edges. So closed edges together form a face. Faces are important as shading material are applied on them to create a better looking model. The model I load does not incorporate shading just yet. It only has a mesh of the model being rendered.

f 53/90 52/91 81/92 66/93

Faces are prefixed with ‘f’ and have two parts. It has a vector that is related it to it and also a normal. The first half in each set (53,52,81,66) are the verticesĀ and the second half are the normals. This constitutes a face.

Once we have these three values, we’re set to load the model. I used LWJGL (which is a library that uses OpenGL) to load the model to the screen.

Screen Shot 2016-02-22 at 00.17.29.pngScreen Shot 2016-02-22 at 01.00.04.png