0
\$\begingroup\$

I recently started making a Terraria clone using the LÖVE library, which is based in OpenGL.


In Terraria, for each tile, there is a large texture with all possible combinations for merging with neighbouring tiles. They usually only support tiles of the same type, and sometimes other materials, like dirt. As a result, they only need a single vertex array.

Terraria uses a single vertex array to convey merging with neighbouring tiles.


In Starbound, it seems to be simpler. A tutorial I found notes the use of basic 8x8 blocks, with a few edges drawn if the block is next to a block of a different type.


I want to implement a similar mechanic, but I encountered a serious issue. A single vertex array does not allow z-ordering, which I would need.

  • My first idea was to use one vertex array for each type of block, but that would eat a lot of memory.

  • My second idea was to use the fact that OpenGL vertex array draws primitives in order. I could use some complicated data structure to represent tiles, so I would always know where to place their vertices in the array. However, that would either require moving lots of vertices or lots of memory.

What is the best way to implement this?

\$\endgroup\$
  • \$\begingroup\$ @Gnemlock Questions like How'd they do it: Millions of tiles in Terraria have many up votes, but you are right, I haven't noticed that in help center. Thanks for edit. \$\endgroup\$ – Franz Wexler Dec 27 '16 at 10:50
  • \$\begingroup\$ Good spot. Policy changes, over time, mean good questions from old may conflict with newer policy. I've flagged the question for review; even in these cases, I often see the question closed, though they are not necessarily deleted (is it can be viewed, but no answers can be posted.) \$\endgroup\$ – Gnemlock Dec 27 '16 at 12:56
1
\$\begingroup\$

I found a way myself.

The key is to have two vertex arrays. In first you store solid blocks. In second you store edges, only if they are visible. Then you simply draw solid blocks first, then edges.

It is easy to add third array to store in-corners.

Vertex arrays look like these (from left: first, second, third array, final result)

vertex arrays when blending tiles

Hope this helps somebody!

\$\endgroup\$
  • \$\begingroup\$ Great answer, in that this could be universally applied to other environments. \$\endgroup\$ – Gnemlock Dec 27 '16 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.