IDAT211 – The .OBJ file

In-order to create code output the terrain I had to pick and understand a 3D model file format.

I explored a number of formats by exporting a flat square from blender and loaded the resulting file into a text editor from this I found the simplest format for me to use which was Wavefront OBJ. The advantages were that it was relatively simple to adapt my terrain code to to output, it was relatively light preventing the model file becoming too large.


mtllib mat.mtl
usemtl sand
v 1 0.0 1
v 1 0.0 2
v 2 0.0 2
v 2 0.0 1
f 1 2 3 4
usemtl sand
v 1 0.0 2
v 1 0.0 3
v 2 0.0 3
v 2 0.0 2
f 5 6 7 8
usemtl sand
v 1 0.0 3
v 1 0.0 4
v 2 0.0 4
v 2 0.0 3
f 9 10 11 12
...

mtllib – ource of the materials
usemtl – material for the face
v – location of a point
f – points to join to make a face

The output portion of my code that writes the .obj file:

for (int x = 1; x < musicArray.length-1; x++) {
 for (int y = 1; y < rows-1; y++) {
 
 data[len] = "usemtl sand";  
 if(Zarray[x][y] > 3)data[len] = "usemtl grass";
 if(Zarray[x][y] > 35)data[len] = "usemtl mountain";   
 if(Zarray[x][y] > 60)data[len] = "usemtl snow";  
      
 data[len+1] = "v " + x + " " + Zarray[x][y] + " " + y;
 data[len+2] = "v " + x + " " + Zarray[x][y+1] + " " + (y+1);
 data[len+3] = "v " + (x+1) + " " + Zarray[x+1][y+1] + " " + (y+1);
 data[len+4] = "v " + (x+1) + " " + Zarray[x+1][y] + " " + y;
 data[len+5] = "f " +face+ " " +(face+1)+ " " +(face+2)+ " " +(face+3);
 face+=4;
 len+=6;
 }
 println((x/(musicArray.length/100)) + "%");
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.