Home Working with 3D models Exporting the model
Exporting the model in .OBJ format

In order to use complex 3d models in your opengl programs you first have to create in using CAD programs such as 3D Studio Max or GMax and export the geometry data in a standard format you can parse in C. After you do that you can import the model in OpenGL using a free library called GLM that comes with glut source distribution in the example folder.

Lets say you've made a 3D model in 3DS Max or GMAX. You want to get that model into you C++ OpenGL application. First thing you have to do is export it to a format that is easy to read from a C++. 3DS or MAX files are binary files with a proprietary format so it would be pretty hard to write a C++ library to read them. Hard but not imposible. There allready is .3DS loader out there and if you are interested you should google it. Of course because of the complexity of the format it has some serious limitations.

The easiest to read format is Wavefront OBJ files. This is a text format extremely easy to parse:

  • # comment

One line comment

  • v float float float

A single vertex's geometric position in space. The first vertex listed in the file has index 1, and subsequent vertices are numbered sequentially.

  • vn float float float

A normal. The first normal in the file is index 1, and subsequent normals are numbered sequentially.

  • vt float float

A texture coordinate. The first texture coordinate in the file is index 1, and subsequent textures are numbered sequentially.

  • f int int int ...

or

  • f int/int int/int int/int . . .

or

  • f int/int/int int/int/int int/int/int ...

A polygonal face. The numbers are indexes into the arrays of vertex positions, texture coordinates, and normals respectively. A number may be omitted if, for example, texture coordinates are not being defined in the model.

You get the point....it's easy.

How to export OBJ file in 3D Studio Max

3DS Max has a built in exporter for this format. Go in File/Export and select gw-obj from the file type dropdownlist, choose a location and hit Export. You will be presented with a dialog with options. Now this one depends on your version. I'll show you a screenshot of 2009 version:

 

3DS Max OBJ Export

 

In the next tutorial I will give you a library that reads this kind of file. Having that in mind I will recomand that you select "Triangles" to be exported because that library has certain bugs and doesn't work too well if you export polygons. Also you should click Map-Export and make sure 3DS Max doesn't "optimize" Textures by resizing them to 512x512. I found that to be a very annoying feature.

 

How to export OBJ file in GMAX

Gmax is old and doesn't have a built in .OBJ Exporter. However we can use the Max Script feature. Basically that lets you make a script to automate some task....such as typing down coordinates :D The script I'm going to give you works in 3DS Max but the reason I'm telling you to use the built in exporter if possible is because this script not only doesn't export materials and textures but it's also painfully slow. In a scene with 100k polygons I had to wait about 20-30 mins :(

  1. Here is what you need to do:
  2. Download the script: http://www.3dcodingtutorial.com/files/gMAX2OBJ.ms
  3. Download a grabber program because GMax's listener window doesn't have a Save option like 3DS Max does: http://www.yayap.com/GMaxSLGRAB.zip
  4. In GMAX go in MAXScript->Run Script... and select gMAX2OBJ.ms
  5. Either press F11 or select MAXScript->MAXScript Listener
  6. Launch the application GMaxSLGRAB.zip
  7. Press Grab and save the file as .OBJ, or at least rename it from XML after you saved it
  8. Open the .OBJ file with notepad and remove the first few line that tell you what to do...everything until the comments start (until the first #)

Example of Exported OBJ file

Cube.obj:


 

# 3ds Max Wavefront OBJ Exporter v0.94b - (c)2007 guruware
# File Created: 08.05.2008 10:29:56

mtllib cube.mtl

#
# object Box01
#

v -26.3156 0.0000 31.5564
v -26.3156 0.0000 -22.7949
v 35.1400 0.0000 -22.7949
v 35.1400 0.0000 31.5564
v -26.3156 29.0548 31.5564
v 35.1400 29.0548 31.5564
v 35.1400 29.0548 -22.7949
v -26.3156 29.0548 -22.7949
# 8 vertices

vn 0.0000 -1.0000 -0.0000
vn 0.0000 1.0000 -0.0000
vn 0.0000 0.0000 1.0000
vn 1.0000 0.0000 -0.0000
vn 0.0000 0.0000 -1.0000
vn -1.0000 0.0000 -0.0000
# 6 vertex normals

vt 1.0000 0.0000 0.0000
vt 1.0000 1.0000 0.0000
vt 0.0000 1.0000 0.0000
vt 0.0000 0.0000 0.0000
# 4 texture coords

g Box01
usemtl Material__25
s 2
f 1/1/1 2/2/1 3/3/1
f 3/3/1 4/4/1 1/1/1
s 4
f 5/4/2 6/1/2 7/2/2
f 7/2/2 8/3/2 5/4/2
s 8
f 1/4/3 4/1/3 6/2/3
f 6/2/3 5/3/3 1/4/3
s 16
f 4/4/4 3/1/4 7/2/4
f 7/2/4 6/3/4 4/4/4
s 32
f 3/4/5 2/1/5 8/2/5
f 8/2/5 7/3/5 3/4/5
s 64
f 2/4/6 1/1/6 5/2/6
f 5/2/6 8/3/6 2/4/6
# 12 faces



Cube.mtl:

 


# 3ds Max Wavefront OBJ Exporter v0.94b - (c)2007 guruware
# File Created: 08.05.2008 10:29:56

newmtl Material__25
Ns 10.0000
Ni 1.5000
d 1.0000
Tr 1.0000
Tf 1.0000 1.0000 1.0000
illum 2
Ka 0.0000 0.0000 0.0000
Kd 0.5880 0.5880 0.5880
Ks 0.0000 0.0000 0.0000
Ke 0.0000 0.0000 0.0000
map_Ka C:\Users\tudor\Desktop\444.jpg
map_Kd C:\Users\tudor\Desktop\444.jpg



What you have seen here is how a cube looks like after it's been exported. Cube.mtl is the material file that sais how the object looks in the light (if it shines....) and what texture it has. map_Ka and map_Kd are the textures.

 

In the next tutorial I will show you how to read these files and render the object on the screen

 

 

 
Copyright © 2010 Tudor Carean. All Rights Reserved.