Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: VertexArray with different textures per vertex  (Read 1908 times)

0 Members and 1 Guest are viewing this topic.

robkruger

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
VertexArray with different textures per vertex
« on: November 27, 2019, 11:21:19 am »
I'm trying to make a terraria clone, and draw the tiles with a VertexArray. The problem is that I don't see a way to set a custom texture for every vertex. Is the only solution I can do to create a container for every tile type and draw a VertexArray for every container?

sf::VertexArray va(sf::Quads, 68 * 18);
    int y = 0;
    int x = 0;
    sf::Texture sprite_sheet;
    sprite_sheet = sfml_resources::get().get_spritesheet();
    unsigned i = 0;
    for(tile& t : m_game.get_tiles()){
      y = (17 * (t.get_state() / 16));
      x = (18 * t.get_state()) % (16 * 18);
      sf::Vertex* quad = &va[i];
      const int tx = static_cast<int>(t.get_x());
      const int ty = static_cast<int>(t.get_y());
      quad[0].position = sf::Vector2f(tx, ty);
      quad[1].position = sf::Vector2f(tx + 32, ty);
      quad[2].position = sf::Vector2f(tx + 32, ty + 32);
      quad[3].position = sf::Vector2f(tx, ty + 32);
      quad[0].texCoords = sf::Vector2f(x, y);
      quad[1].texCoords = sf::Vector2f(x + 16, y);
      quad[2].texCoords = sf::Vector2f(x + 16, y + 16);
      quad[3].texCoords = sf::Vector2f(x, y + 16);
      i += 4;
    }
    m_window.draw(va, &sprite_sheet);

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: VertexArray with different textures per vertex
« Reply #1 on: December 01, 2019, 11:08:07 pm »
For a single vertex array, you can use only one texture; this is true.

However, you can assign different parts of the texture for each primitive.

Each sf::Vertex has a texture co-ordinate that specifies which part of the texture to use. By using texture co-ordinate from different areas of the texture, different pieces can be used, simulating different textures per primitive. You have this in your code example. However, you can repeat or use different parts of that texture however you like so every 'tile' that is the same is only in the texture once but drawn multiple times.

A common and simple example is a regular grid of images within one texture and using each 'cell' of that grid as a separate image for a specific quad. This is the simplest version and is used in tilemaps a lot. Search for 'sprite sheet' and you'll see lots of similar examples although you may notice that some do not conform to a regular grid. This isn't as important as it at first seems as you can assign any co-ordinates to any vertex. The only thing you have to really do is keep track of which parts of the texture contains a separate image.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything