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?
#sfml vertex buffer requires 20x more vertices than required for an unknown reason
20 messages · Page 1 of 1 (latest)
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.
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);
}
};
m_VertexBuffer.update(&m_vertices[startIndex], byteCount, byteOffset);
this does not look right
the count and offset are not in bytes
.
Hm yeah i thought so, this is the only code i didn't write myself as i have no idea how to update the vertex buffer correctly
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
that indeed appears to be the case
well, I suppose preallocate the vertex buffer with the right size upfront
thats a good idea, i guess i could find a way to manage items that are being "added" or "removed" by moving them in or out of the screen view
i seem to be running into an issue where nothing is displayed.
with sf::vector<sf::Vertex> Vertices and sf::VertexBuffer Buffer
if i was to run
Buffer.create(Vertices.size());
Buffer.update(Vertices.data(), Vertices.size(), 0);
every time i update a Vertex in the Vertices vector, would i have to call the Buffer.update() function?
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
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
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.