Tiling in OpenGL
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.
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.
Basically, the function loads a .bmp and stores it as a AUX_RGB_ImageRec type, which will then be used to attach the texture data to the textures we generate.
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.
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.
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 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.
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 firstname.lastname@example.org or email@example.com for remarks, suggestions, or to report any typos/bugs there might be.
Visit my website at www.cfxweb.net/lpsoftware
This article is © 2000 Martin Estevao. This article may not be re-printed without the author's consent.