Tiling in OpenGL
Covers basic tiling in OpenGL using textured quads to represent the tiles.
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.
[size="5"]Prerequisites
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.
[size="5"]Setup
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 10GLuint texture[2]; char map[10][10] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};I'm going to use NeHe's .bmp routine to load the texture bitmaps for this tutorial. Here's the code.
AUX_RGBImageRec *loadbmp(char *filename)
{
FILE *file = NULL;
if (!filename)
return NULL;
file = fopen(filename, "r");
if (file)
{
fclose(file);
return auxDIBImageLoad(filename);
}
return NULL;
}int load_gl_textures()
{
AUX_RGBImageRec *TextureImage[2];
memset(TextureImage,0,sizeof(void *)*1);
TextureImage[0] = loadbmp("tile0.bmp");
TextureImage[1] = loadbmp("tile1.bmp");
glGenTextures(2, &texture[0]); // We are generating two textures
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,
TextureImage[0]->sizeY, 0, GL_RGB,
GL_UNSIGNED_BYTE, TextureImage[0]->data);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[1]->sizeX,
TextureImage[1]->sizeY, 0, GL_RGB,
GL_UNSIGNED_BYTE, TextureImage[1]->data); for (int t = 0; t < 2; t++)
{
if (TextureImage[t])
{
if (TextureImage[t]->data)
{
free(TextureImage[t]->data);
}
free(TextureImage[t]);
}
}
return(1);
}Now, you should set up the camera with which you'll view the 3D scene. This is the way I'd do it:
gluLookAt(10.0f, 8.0f, 20.0f, 10.0f, 8.0f, 0.0f, 0.0f, 1.0f, 0.0f);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.
[size="5"]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.
int draw_tiles(GLvoid)
{
int tile; for (int y = 0; y < MAP_SIZEY; y++)
{
for (int x = 0; x < MAP_SIZEX; x++)
{ tile = map[y][x]; glBindTexture(GL_TEXTURE_2D, texture[tile]); glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(float(x), float(y), 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(float(x + 1), float(y), 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(float(x + 1), float(y + 1), 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(float(x), float(y + 1), 0.0f);
glEnd();
}
}
return (1);
}[size="5"]Conclusion
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="lpsoftware@gdnmail.net"]lpsoftware@gdnmail.net[/email] or [email="lpsoftware@home.com"]lpsoftware@home.com[/email] for remarks, suggestions, or to report any typos/bugs there might be.
Visit my website at www.cfxweb.net/lpsoftware
This article is (C) 2000 Martin Estevao. This article may not be re-printed without the author's consent.
Related Tutorials
Procedural Generation of 2D Tile Maps in Games
The article explains how to build a procedural 2D cave map generator using cellular automata. It covers map representat…
Localizing A Game Into French — Which Variant Should You Choose?
So, you’re making an awesome indie game, and now you’re thinking about localizing it into French? Great idea! There ar…
How Long Does It Take To Localize An Indie Game?
So you’re planning on localizing your indie game, but you’re not sure how much time to schedule in. While the timing o…
Creating game videos: best practices and pitfalls to avoid
Game video production: practical tips on how to create a game trailer or teaser that you can be proud of. Give your aud…
Adopting CI/CD: How Midwinter Entertainment iterates at speed with the help of IMS
Game development was once like one long sprint: a huge effort until you reached the finish line — at which point you co…
GameDev.net
5 Things to Consider When Making a Video to Promote Your App or Game
How can you show an app or game in your video in a way that attracts new users? Let’s take a look at what to go by when…
Discussion
More from Martin Estevao
Comparing Shadow Mapping Techniques with Shadow Explorer
New Incentives and a Whole New Platform From The Intel AppUp developer program
The final installment in this series on Intel's AppUp program details additional incentives and opportunities for devel…
The Game Maker
This book excerpt contains the first chapter of the book that introduces beginning game developers to the Game Maker pr…
Discussion