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

Author Topic: Entity/quad drawing order and VertexArrays  (Read 2218 times)

0 Members and 1 Guest are viewing this topic.

fralexxelarf

  • Newbie
  • *
  • Posts: 3
    • View Profile
Entity/quad drawing order and VertexArrays
« on: March 14, 2017, 02:46:17 pm »
Hello!

I have a RenderingSystem that loops through a vector of SpriteBatches, one for each texture atlas/spritesheet. The SpriteBatch has a VertexArray that has quads for each instance of X-type entity that uses that texture.

Spritebatch
class SpriteBatch : public sf::Drawable, public sf::Transformable
{
public:
        unsigned int m_VerticesCount;
        sf::VertexArray m_Vertices;
        sf::Texture* m_pTexture;
public:
    SpriteBatch(sf::Texture* texture)
        m_pTexture(texture), m_VerticesCount(0)
    {
            m_Vertices.setPrimitiveType(sf::Quads);
    }

    void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
            // Apply the transform
            states.transform *= getTransform();

            // Apply the tileset texture
            states.texture = m_pTexture;

            // Draw the vertex array
            target.draw(m_Vertices, states);
    }
};

RenderingSystem render method.
// Loops through all spritebatches and draws them.
void RenderingSystem::render(sf::RenderWindow& window)
{
        for (auto spritebatch : m_SpriteBatches) window.draw(*spritebatch);
}

Do you have any tips on how I would go about ordering the individual quads above/below eachother?

I've come up with this idea about moving the quads(where Z != 0) to a new, temporary SpriteBatch, then loop through all new spritebatches and draw them in order, from lowest to highest Z. (Z values would be stored relative to quad positions and also in each new SpriteBatch.) Does that sound plausible? Or would it be too slow and better to just use some list of entities?

I hope your understand my question, otherwise tell me and I will try to clarify as well as I can. :)


Thanks!
« Last Edit: March 14, 2017, 03:07:07 pm by fralexxelarf »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Drawing order and spritebatches
« Reply #1 on: March 14, 2017, 03:10:48 pm »
Whenever you add a sprite to the batcher, make it take a Z position value and then correctly order the vertices in the vertex array.
I also suggest to just use std::vector<sf::Vertex> directly, that way you're way more flexible.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

fralexxelarf

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Entity/quad drawing order and VertexArrays
« Reply #2 on: March 14, 2017, 03:35:10 pm »
Ahh ok! Then it basically becomes like ordering a vector of sf::Sprites, but vertices instead? But this assumes all sprites that could have different Z values use the same texture, right? (and then static Z value-Sprites/Vertices, like GUI and tiles in another texture & batch?)

Thanks for the fast reply.

Edit: clarification
« Last Edit: March 14, 2017, 03:43:44 pm by fralexxelarf »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Entity/quad drawing order and VertexArrays
« Reply #3 on: March 14, 2017, 04:01:46 pm »
Yes, if you want to mix the order of different texture objects, then you'll have to use multiple vertex array for the same texture.
However, many such situations can be prevented by packing textures in a smart way.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

fralexxelarf

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Entity/quad drawing order and VertexArrays
« Reply #4 on: March 14, 2017, 04:16:53 pm »
Ok nice, I think I got it then!

Thanks a lot! :)