newbie question on glBufferData?

Started by
4 comments, last by clb 11 years, 7 months ago
Hello,

I have a method like this for a first-try to draw triangles in OpenGL 3.3 (Vec3 is from glm library):

void RenderOpenGL::DrawTriangle(const Vec3& pointA, const Vec3& pointB, const Vec3& pointC)
{
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vec3), glm::value_ptr(pointA), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vec3), glm::value_ptr(pointB), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vec3), glm::value_ptr(pointC), GL_STATIC_DRAW);

PrimitiveInfo primInfo;
primInfo.Mode = GL_TRIANGLES;
primInfo.Count = 3;
mPrimitiveInfo.push_back(primInfo);
}


I would then go through my Vector<PrimitiveInfo> mPrimitiveInfo and draw as the elements indicated.

From what I read on http://www.opengl.or...lBufferData.xml each consequent call to glBufferData will overwrite the old.
So how would you actually buffer this? Say I'm calling this method to draw three triangles, how would I actually buffer it without loosing the old data?

EDIT: and as a follow-up, how should you actually manage the VBOs? Right now I have a single buffer object to which I write data to but as you progress how would you distribute them?
Advertisement
glBufferData doesn't "queue up" the data you pass in, but specifies the full data contents. So, to specify a single triangle in a VBO, do

Vec3 points[3] = { pointA, pointB, pointC };
glBufferData(GL_ARRAY_BUFFER, sizeof(Vec3)*3, points, GL_STATIC_DRAW);


(Note however, that you never want to draw just a single triangle in a VBO, but you want to batch as many of them in a single array as possible for best batching)
Would it be wise to store all primitives I want to draw in one big vector and then in a "Render" method send them to the glBuffer and draw them?
Also, you would only have to rebuffer and redraw all of them if the content of that vector changed between frames right?
The purpose of the buffer object is to store your vertex data so there's where you store it, not in some external vector only to copy it to the buffer when rendering. Load the buffer with data once and update it when it has to change.
Don't forget about glBufferSubData to alter a buffer without changing its size.
It's possible to pass a null pointer to glBufferData to just allocate the space.

If the buffer is holding just the single triangle, then clb's solution is best.

New C/C++ Build Tool 'Stir' (doesn't just generate Makefiles, it does the build): https://github.com/space222/stir

Btw, as a note, on Android, I was in a habit of using glBufferData to initially create a VBO, then using glBufferSubData in all subsequent calls to update the full contents of that VBO, e.g. for per-frame particles. What I noticed was that it was slower than just directly using glBufferData each frame to update the particles, combined with manual double-buffering of the VBOs.

This topic is closed to new replies.

Advertisement