#sfml vertex buffer requires 20x more vertices than required for an unknown reason

20 messages · Page 1 of 1 (latest)

azure elk
#

if we were to have 100 circles, each with 10 points defining it. we can do 100 * 10 * 3 to calculate how many vertices are needed. however i found that i need to multiply all of this by another x20 to get all of these circles to render. I checked all of my code while debugging, and all of the vertices are there, but they will not be rendered without there being another x20 vertices being added to the buffer?

tardy vesselBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

azure elk
#

this is the code:

class Buffer
{
    sf::VertexBuffer m_VertexBuffer;
    std::vector<sf::Vector2f> m_positions;
    std::vector<sf::Vertex> m_vertices;

public:
    // constructor and detructor
    explicit Buffer(const unsigned int maxObjects, const unsigned int ObjectPoints)
        : m_maxObjects(maxObjects), m_ObjectPoints(ObjectPoints)
    {
        m_totalExpectedVertecies = m_maxObjects * m_ObjectPoints * 3 * 20;
        // preparing containers for oncoming objects
        m_VertexBuffer = sf::VertexBuffer(sf::PrimitiveType::Triangles, sf::VertexBuffer::Stream);
        m_VertexBuffer.create(m_totalExpectedVertecies);

        m_positions.reserve(m_maxObjects);
        m_vertices.reserve(m_totalExpectedVertecies);
    }
    ~Buffer() = default;


    // appending an item to the Buffer
    void add(const sf::Vector2f position, const float radius, const sf::Color color)
    {
        // Create the vertex data for the circle
        const std::vector<sf::Vector2f> triangles = createTriangleVertecies(radius);

        // Append the vertices to m_vertices and m_positions
        const auto startIndex = static_cast<unsigned int>(m_vertices.size());
        m_vertices.resize(startIndex + triangles.size());
        m_positions.push_back(position);

        for (unsigned int i = 0; i < triangles.size(); i++)
        {
            m_vertices[startIndex + i].position = position + triangles[i];
            m_vertices[startIndex + i].color = color;
        }

        // Update the vertex buffer with the new vertices
        const std::size_t byteOffset = startIndex * sizeof(sf::Vertex);
        const std::size_t byteCount = m_ObjectPoints * sizeof(sf::Vertex);
        m_VertexBuffer.update(&m_vertices[startIndex], byteCount, byteOffset);
    }
};
unreal falcon
#

m_VertexBuffer.update(&m_vertices[startIndex], byteCount, byteOffset);
this does not look right

#

the count and offset are not in bytes

azure elk
azure elk
#

i fixed the issue:

// appending an item to the Buffer
    void add(const sf::Vector2f position, const float radius, const sf::Color color)
    {
        // Create the vertex data for the circle
        const std::vector<sf::Vertex> triangles = createTriangleVertecies(radius, position);

        // Append the vertices to m_vertices
        const auto startIndex = static_cast<unsigned int>(m_vertices.size());
        m_vertices.resize(startIndex + triangles.size());

        for (unsigned int i = 0; i < triangles.size(); i++)
        {
            m_vertices[startIndex + i].position = position + triangles[i].position;
            m_vertices[startIndex + i].color = color;
        }

        // Update the vertex buffer with the new vertices
        m_VertexBuffer.update(m_vertices.data(), m_vertices.size(), 0);
    }
#

i dont know if this is a bad way of doing it, it seems like i am overiting all the m_VertexBuffer data every time i am adding an object

unreal falcon
azure elk
unreal falcon
#

well, I suppose preallocate the vertex buffer with the right size upfront

azure elk
azure elk
unreal falcon
#

yes

#

but usually you'll upload a model or something in the vertex buffer and apply a transformation matrix to each vertex in the gpu pipeline; you don't transform each vertex individually on the CPU and then upload it to the gpu

#

obviously if you want to edit specific vertices, like in an editor, then you need to update the vertex buffer

azure elk
# unreal falcon but usually you'll upload a model or something in the vertex buffer and apply a ...

well i might me doing something wrong here, i dont really understand what you are saying but perhaps looking at my problem from a larger scope may help.

i have 2 particle simulations, the first one is about 1 million pixels that move around every frame, the second one is about 100,000 circles that do the same.
im using a vertex array for both and i've been hearing that vertex buffers are more fit for the task, but i still have a feeling that they are better for more static Vertices

tardy vesselBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.