Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


newbie question on glBufferData?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 KaiserJohan   Members   -  Reputation: 1197

Like
0Likes
Like

Posted 30 September 2012 - 02:28 PM

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?

Edited by KaiserJohan, 30 September 2012 - 02:54 PM.


Sponsor:

#2 clb   Members   -  Reputation: 1785

Like
3Likes
Like

Posted 30 September 2012 - 03:14 PM

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)
Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

#3 KaiserJohan   Members   -  Reputation: 1197

Like
0Likes
Like

Posted 01 October 2012 - 02:14 AM

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?

#4 Brother Bob   Moderators   -  Reputation: 8428

Like
2Likes
Like

Posted 01 October 2012 - 02:29 AM

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.

#5 beans222   Members   -  Reputation: 1131

Like
0Likes
Like

Posted 01 October 2012 - 12:29 PM

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

 


#6 clb   Members   -  Reputation: 1785

Like
1Likes
Like

Posted 02 October 2012 - 04:00 AM

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.
Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS