Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
96 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

  Contents

 Introduction
 Drawing the Tiles

 Printable version

 


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;

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].

glBindTexture(GL_TEXTURE_2D, texture[tile]);

Here we bind the texture that we need, either texture[0] or texture[1], based on the value of tile, which holds the current tile's ID#.

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); }

This is where the actual "tiling" takes place, and a square is rendered at the correct location with the correct texture (texture[tile]).

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 lpsoftware@gdnmail.net or lpsoftware@home.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.