Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL. Updating vertex buffer with glBufferData

I'm using OpenGL to implement some kind of batched drawing. For this I create a vertex buffer to store data.

Note: this buffer generally will update on each frame, but will never decrease size (but still can increase).

My question is: is it technically correct to use glBufferData (with streaming write-only mode) for updating it (instead of e.g. glMapBuffer)? I suppose there's no need to map it, since full data is updated, so I just send a full pack at once. And if the current buffer size is less, than I'm sending, it will automatically increase, won't it? I'm just now sure about the way it really works (maybe it will recreate buffer on each call, no?).

like image 762
Celestis Avatar asked Jan 04 '13 10:01

Celestis


2 Answers

It would be better to have buffer with fixed size and do not recreate it every frame.

You can achieve this by:

  • creating buffer with max size, for instance space for 1000 verts
  • update only the beginning of the buffer with new data. So if you changed data for 500 verts then fill ony first half of the buffer using glMapBuffer
  • change count of drawn vertices when drawing. You can, for instance, use only some range of verts (eg. from 200 to 500) from the whole 1000 verts buffer. Use glDrawArrays(mode, first, count)

ideas from comments:

  • glMapBufferRange and glBufferSubData could also help
  • also consider double buffering of buffers

link: http://hacksoflife.blogspot.com/2010/02/double-buffering-vbos.html

hope that helps

like image 122
fen Avatar answered Sep 20 '22 06:09

fen


In addition to what fen and datenwolf said, see Chapter 22 of OpenGL Insights; in particular, it includes timings for a variety of hardware & techniques.

like image 33
Calvin1602 Avatar answered Sep 17 '22 06:09

Calvin1602