3D graphics with Game Maker Part 4
Page 2 of 3
<1 | 2 | 3>
Single-page view
3D graphics with Game Maker Part 4

Next, I'll create an object called Building. In my case, I place the following in its Create Event:

myModel=createFromObj(working_directory+"\Models\Buildings\Bulding.obj",false);

Here, "myModel" is the handle that I'll use to refer to the model that I import. I'll use this in any functions that require the model as an argument. The importer script that we've inserted, createFromObj, takes the .OBJ model file you specify and translates it into GM's model format, returning the handle once it's complete. This script takes two arguments – the path to the model I want to import (as a string), and an argument that tells it whether to flip the face normals on import. Generally we don't want this, so I've set it to false.

Now that the model is imported, we can draw it. We do this through a single command in our Draw function, after we've done all our translations as usual:

d3d_transform_set_identity();
d3d_transform_add_translation(x,y,z);
d3d_model_draw(myModel,0,0,0,-1);
d3d_transform_set_identity();

d3d_model_draw() is a standard Game Maker draw command. The first argument tells GM which model to draw – in this case, "myModel", which I declared in the Create event. The next three arguments are the x, y, and z coordinates where the model must be drawn. Here they're set to 0, since I'm translating using my transformations. The final argument allows you to specify a texture. In this case, we don't want one, so we pass -1.

And voila! When we run the game with our building in it, we see it drawn in all its glory!

Building in the game

Let's go back a bit. You'll have noticed that when we specified a texture for this model, we didn't have the usual hrepeat and vrepeat arguments that we've been using for the primitives. This is because texturing for models works a little differently. You will need to texture your model externally, in your 3D program, by UV mapping it. The process is, once again, different depending on the package you use, but once the UV mapping is done, all you need to do is export the .OBJ with UV data, and import the texture you mapped into GM as well. For example, I've UV mapped the following texture to my buildings using my 3D program, then exported my model in .OBJ with the UV data included:

Someone flattened my building!
Someone flattened my building!

Next, I've imported this same texture into GM as a sprite, which I've called BuildingTex, and assigned it a handle in the Create event, just like we did in the last instalment:

myTex=sprite_get_texture(BuildingTex,0);

Now when I do my draw code, it looks like this:

d3d_transform_set_identity();
d3d_transform_add_translation(x,y,z);
d3d_model_draw(myModel,0,0,0,myTex);
d3d_transform_set_identity();

And when I run the game, it looks a little something like this:

Textured building



Words from the readers
I'll see what I can do for you.
Posted by Chippit at 22:17:29 on 29 June 2009
Can you please release the gmk of this tut, because I have follow everything in this tut but somethings aren't working correctly and I dunno why? Thanks.
Posted by Giorgio at 22:52:04 on 22 June 2009
Have your say: