Skip to main content
2 of 3
added 6 characters in body
Sandalfoot
  • 774
  • 3
  • 9

You'd draw a tile map in OpenGL the same way you'd draw a sprite that has multiple frames in a single texture; by adjusting the texture coordinates such that you only drew a portion of the texture onto the quad that represents the tile.

A rough way to do it would be to create a mesh of quads/triangles that represents your tile map; each tile's world coordinates would be equal to the tile's X,Y index in the grid * the size of the tile (make sure to use orthographic projection!);

Then, each tile's UV coordinates into the master tiles texture would be something like;

(1 / tiles across) * textureXindex

So if your texture had four tiles across (same thing in an ortho projection) and you wanted tile #2;

textureWidth = 1 / 4;

textureWidth * 2 = 0.5 (left texture U coordinate)

the above value (0.5) + textureWidth (right texture U coordinate)

Same for the Y/V coordinate. You could also calculate the tile width by dividing the total width of the tile texture in pixels by the width of a tile in pixels, which will give you the same decimal value as the 1/4 calculation above.

Sandalfoot
  • 774
  • 3
  • 9