Hello,
I have been reading and trying learnopengl.com and it's fun. However when trying to use it in a game, I have a few questions that I'd like to discuss.
This topic is about how to do a map with tiles in OpenGL. Think a horizontal shooter with a tiled background, or a top-view game. It's all in 2D, with a few layers on top of each other.
As an example (to make the discussion more concrete), assume my tiles are 100x100 pixels which is also the size of the tile when displayed to the screen, and I have a map of 300x200 tiles. For simplicity, let's assume a pixel has size 1x1 in OpenGL coordinates (it's all scalable a lot, many options there).
The first solution I could think of is to upload the entire map of 30000x20000 units size, with 2 triangles every 100x100, with triangles pointing to the texture that should be displayed at that point in the map (ie I share the images of the tiles across the map). Advantage: It's simple; Disadvantage: It has to skip most of the tiles, since the tot map size is larger than the size of the display. In OpenGL you just translate to the correct position to display that part of the map.
The second solution I could think of is to create the above map, but only just a little larger than the display size. In other words, it is only the displayed portion with a bit extra to enable moving around smoothly. To move further, I guess you have to re-upload the triangle data with different texture indices once every while, or you put the tile information in another texture which is indexed relative to eg the top-left corner of the displayed part (that is, every triangle first queries the tile texture to know the tile-type, and uses that to access the correct tile image for display in the triangle. Advantage: It's less triangles for the GPU (few skipped triangles if any); Disadvantage: It's more complicated to implement.
Conceptually, both look like it should work. What I don't know is which one is preferable. Likely actual mapsize is a factor here, so larger mapsize means that at some point the second solution would be better (I think??). But where is that point roughly?
Also, are there better options to display a tiled map in 2D?