I am trying to implement a tiled terrain using Irrlicht, but I ran into problems. Currently one tile is made up from two triangles that form equilateral quads. To minimize memory costs, I am using four vertices for every tile that get shared with adjacent tiles, with the indexbuffer containing the indices for the two triangles (for example the first tile is made up from the sequence [0, 1, 2, 2, 1, 3]). In addition to that, I am also using a texture-atlas that contains several different tile-textures. The problem now is obvious: how do I render the primitives efficiently with the correct texture attached to them, without giving up the shared vertices between tiles (the corner of each tile appears in four triangles, if it's an inner tile)?
With shared vertices I can only set one texture coordinate to each vertex, so I already got the problem, that my textures get flipped horizontally or/and vertically depending on the tile-number (that is obvious, because for example the texture.u-coordinates of the vertices in x-direction is 0-1-0-1-0..., so the first tile-primitive got texture-coordinates interpolated from 0 to 1 and the second from 1 to 0, so the texture appears vertically flipped in the second tile). I addressed that problem already by using four different indexbuffers together with four corresponding texturematrices that flip the texture coordinates of the tile's vertices (the four matrices are identity, flipU, flipV and flipUV).
So I got rid of that problem, but another one remains: I'd have to change the texture for every tile and couldn't use something like driver->drawTrianglePrimitiveList for all tiles (because after every tile I usually have to change the texture). One solution to that would be to sort all indexbuffers, so that I can perform a single setTexture() call for all the tiles with the same texture, and just iterate over all the different tile-textures.
The problem with that solution would be, that it's probably not very efficient if the tiles are allowed to change their texture over time, for example if I wanted to write a terrain-editor where I can just "paint" the texture onto one or more tiles at once. Every time a texture changes, all indexbuffers will have to be sorted again.
On the other hand, if I used four unique vertices for every tile, I wouldn't have any of the stated problems, but two others:
1) The memory usage would go up (what I wouldn't like to let happen).
2) In a terrain-editor, if I wanted to change the height of one tile, I would have to identify all the corner points of that tile and all adjacent tiles and change their height (what may result in messy code and maybe performance issues?)
So my question now is: Can you help me identifying the optimal solution to address the above problems or do you have any other suggestions as to how to implement such a tiled terrain? I'd like to hear your opinions!
