If you find this article contains errors or problems rendering it unreadable (missing images or files, mangled code, improper text formatting, etc) please contact the editor so corrections can be made. Thank you for helping us improve this resource
Since I've been writing the DirectX tiling tutorials, I thought I'd write an OpenGL tiling tutorial. This article is intended solely for porting basic tiling methods to the OpenGL API, so if you want an intro to tiling in general, you should read the What the Heck is a Tile section in my Tiling in DirectX: Part I tutorial.
There are not that many differences between tiling in OpenGL and tiling in DirectX (DirectDraw). We still have to define a 2D char array, loop through it, capture the ID of each tile, and draw each individual tile. However, with OpenGL, instead of bit blitting tile bitmaps, we have to render quads and texture them according to the ID at the particular location in the array.
This article is not meant for the complete OpenGL newbie. The reader should be familiar to the basic form and function of the OpenGL API. For those of you who don't know OpenGL or want to brush up on your skills, I strongly suggest heading over to NeHe's site, which is chock full of beginner to advanced OpenGL tutorials.
In this section I'll be presenting all the global variables, defines, and extra functions that we'll need.
#define MAP_SIZEX 10
#define MAP_SIZEY 10
Here we define the map size (in tiles) as 10 * 10.
The use of texture is pretty self-explanatory. It will be used to hold the names of the two textures that make up our map; one in texture and one in texture.
This is the array that holds our map data. In this format, we can access any tile by map[y_tile][x_tile]. In this example, a tile defined as "1" is a wall, or a solid tile that the game characters cannot cross over. A tile with an ID# of "0" is a walkable area. We can redefine a tile’s ID# by doing map[y_tile][x_tile] = new_id;
I'm going to use NeHe's .bmp routine to load the texture bitmaps for this tutorial. Here's the code.
In this piece of code, two textures are generated from two separate 64*64 bitmaps--tile0.bmp and tile1.bmp. (Note: If your bitmaps have different names or are not in the same folder as your program, make sure to make the correct changes to the code or else the program will crash, especially since I haven't optimized the function with any error detection.) These bitmaps are loaded with the function loadbmp() and are then used for creating the textures. The textures will be used later for texturing our quad tiles according to their ID# in the map array. For an in depth explanation of very similar texture generating, check out NeHe's tutorials.
for (int t = 0; t < 2; t++)
And we free the TextureImage's after we use them.
Now, you should set up the camera with which you'll view the 3D scene. This is the way I'd do it:
Calling gluLookAt this way will set the camera back (or the scene forward) 20 units along the z axis, as well as tweaking it a little bit left and right in order to view the scene better. (Note: You can also do this simple transformation with a glTranslatef(), but personally I find this way cleaner.)
Finally, any lighting, etc. that needs to be done should be done. Make sure that you have a working OpenGL program that creates two textures from two bitmaps before going on.
Drawing the Tiles
Ok, now we're ready to finally draw the tiles. I'll go through the code of this tile drawing function step by step.
tile is our place holder variable that will be used in determining if the tile at map[y][x], for example, has an ID# of 0 or 1 when we loop through the map array.
for (int y = 0; y < MAP_SIZEY; y++)
for (int x = 0; x < MAP_SIZEX; x++)
Here we have initialized a doubly-nested loop which will run through our map array, capture each tile's ID#, and then store that ID# in tile, as shown in the next step.
tile = map[y][x];
tile now holds the ID# of the tile at location map[y][x].
Here we bind the texture that we need, either texture or texture, based on the value of tile, which holds the current tile's ID#.
This is where the actual "tiling" takes place, and a square is rendered at the correct location with the correct texture (texture[tile]).
That's it! You now have a simple OpenGL-based tile engine. Here are a couple ideas on how you can optimize it some more yourself.
The beauty of 3D is being able to be virtually everywhere in your "world." Try moving the camera (either by using gluLookAt() or something else) to suit your taste. You could even move the camera 45 degrees to give the tiles a pseudo-isometric look.
You can also hack around with this piece of code and render the square in a different location, make it a different size, or even make it a different shape.
In conclusion, I hope that a few people now have a better understanding of how to produce 2D/3D tiling with OpenGL. As usual, anyone can email me at either email@example.com or firstname.lastname@example.org for remarks, suggestions, or to report any typos/bugs there might be.