Home Working with 3D models Importing the model in C++
Importing the model in C++

For this tutorial I'm going to assume you know how to export the 3D model into .OBJ format from 3DS Max (or GMAX). If you don't please read
How to export a 3D model from 3DS Max in a format you can load in OpenGL

We will be working with a library that came with GLUT in the example folder. It's not something I made. It's standard and written by the opengl guys. I'd say it's safe to use it in your school project too. I modified it a bit to suite my needs to add some extra texture support (only few lines of code were modified - if you want to know what I did just compare the 2 files with an ascii file comparing tool. Google it as there are lots of free tools to do this for you). Get it from here and read a bit: How to get GLM

The example project that loads a box with a texture on it can be found here. It's as clean as you can get...no extra code you don't need. I'd suggest using it as a base for your projects. The model box.obj, the material file box.mat and the tga texture should be in the same folder.

Download Example Project

Download load_model_example.cpp only

The GLM I provided you has support for .TGA textures. TGA are the most commenly used types of textures in games. To convert a .bmp or .jpg image to .TGA you can use Irfan View. It's a free tool. Google it. Of course there are many other that will do the job but I particularly like Irfan View.

And now the explanations:

First of all include glm.h to have access to all the model structures and functions.

Then you should declare a pointer to the model data:

GLMmodel* pmodel1 = NULL;

 

Then you could make yourself a function draw you model:

void drawmodel_box(void)
{

// Load the model only if it hasn't been loaded before
// If it's been loaded then pmodel1 should be a pointer to the model geometry data...otherwise it's null
if (!pmodel1)
{
// this is the call that actualy reads the OBJ and creates the model object
pmodel1 = glmReadOBJ("box.obj");
if (!pmodel1) exit(0);
// This will rescale the object to fit into the unity matrix
// Depending on your project you might want to keep the original size and positions you had in 3DS Max or GMAX so you may have to comment this.
glmUnitize(pmodel1);
// These 2 functions calculate triangle and vertex normals from the geometry data.
// To be honest I had some problem with very complex models that didn't look to good because of how vertex normals were calculated
// So if you can export these directly from you modeling tool do it and comment these line
// 3DS Max can calculate these for you and GLM is perfectly capable of loading them
glmFacetNormals(pmodel1);
glmVertexNormals(pmodel1, 90.0);
}
// This is the call that will actually draw the model
// Don't forget to tell it if you want textures or not :))
glmDraw(pmodel1, GLM_SMOOTH| GLM_TEXTURE);

}

 

Call your function from the display function....of course after doing the positioning you need to display it where you want on the screen. Do the translates, rotates and scaling as you need.

Make sure the current folder contains box.obj and it's material file and texture. In the example I offered the box.obj was generated from GMAX that offers no support for material files, so I made one manualy and called it box.mat:


newmtl box
map_Kd textura_paralelipied.tga


Then in the generated box.obj I added 2 line (the ones in bold). If you use 3D Studio Max to export the model you don't have to worry about makeing the material file by hand. 3D Studio max will generate it for you if you check the box that says: "use materials" and "generate material libraries". Also you should make sure 3D Studio Max doesn't optimize your textures by resizing them to 512x512. There should be a checkbox for that one too.


# -----------------
# Start of obj file
g Box01
mtllib box.mat
usemtl box

v -62.0579 -41.4791 0.0
v 58.8424 -41.4791 0.0
v -62.0579 22.1865 0.0
v 58.8424 22.1865 0.0
v -62.0579 -41.4791 39.8714
v 58.8424 -41.4791 39.8714
v -62.0579 22.1865 39.8714
v 58.8424 22.1865 39.8714

vt 0.843206 0.405444 0.000499517
vt 0.482802 0.71377 0.9995
vt 0.478066 0.404023 0.000499636
vt 0.482802 0.716612 0.9995
vt 0.841627 0.688332 0.000499517
vt 0.482013 0.981029 0.9995
vt 0.480434 0.688332 0.000499636
vt 0.485959 0.978188 0.9995
vt 0.450102 0.00618343 0.000499547
vt 0.45247 0.509304 0.000499547
vt 0.000499517 0.512146 0.000499547
vt 0.000499517 0.512146 0.000499547
vt -0.0010791 0.00618302 0.000499547
vt 0.450102 0.00618343 0.000499547
vt 0.000499517 0.512009 0.9995
vt 0.450891 0.510588 0.9995
vt 0.45247 0.995237 0.9995
vt 0.45247 0.996658 0.9995
vt 0.000499636 0.9995 0.9995
vt 0.000499517 0.51343 0.9995
vt 0.478855 0.405444 0.000500023
vt 0.841627 0.408286 0.000499576
vt 0.83847 0.688332 0.000499576
vt 0.83847 0.688332 0.000499576
vt 0.477276 0.694016 0.000500023
vt 0.478855 0.405444 0.000500023
vt 0.482802 0.71377 0.9995
vt 0.845574 0.71377 0.999501
vt 0.844784 0.976767 0.999501
vt 0.844784 0.976767 0.999501
vt 0.482802 0.716612 0.9995
vt 0.842417 0.710929 0.9995
vt 0.843995 0.975346 0.9995
vt 0.843995 0.975346 0.9995
vt 0.478066 0.404023 0.000499636
vt 0.841627 0.688332 0.000499517

vn 0.0 0.0 -1.0
vn 0.0 0.0 -1.0
vn 0.0 0.0 1.0
vn 0.0 0.0 1.0
vn 0.0 -1.0 0.0
vn 0.0 -1.0 0.0
vn 1.0 0.0 0.0
vn 1.0 0.0 0.0
vn 0.0 1.0 0.0
vn 0.0 1.0 0.0
vn -1.0 0.0 0.0
vn -1.0 0.0 0.0

f 1/9/1 3/10/1 4/11/1
f 4/12/2 2/13/2 1/14/2
f 5/15/3 6/16/3 8/17/3
f 8/18/4 7/19/4 5/20/4
f 1/21/5 2/22/5 6/23/5
f 6/24/6 5/25/6 1/26/6
f 2/27/7 4/28/7 8/29/7
f 8/30/8 6/6/8 2/2/8
f 4/31/9 3/32/9 7/33/9
f 7/34/10 8/8/10 4/4/10
f 3/35/11 1/1/11 5/36/11
f 5/5/12 7/7/12 3/3/12

# end of obj file
# ---------------


 
Copyright © 2010 Tudor Carean. All Rights Reserved.