1
\$\begingroup\$

Imagine yourself a vertex array in OpenGL representing blocks in a platform game.

Vertex array enter image description here

But some vertices may be not used.

Vertex array with unused vertice enter image description here

The environment is dynamic, so there always some vertex may suddenly become invisible.

What is the best way to make them not draw? Graphic cards are complicated and it's hard to predict what is best approach.

Few best ways I can think of:

  1. delete and move all vertices after deleted one to fill freed space (sounds extremely inefficient)
  2. set positions to 0
  3. set transparency to maximum

I could of course benchmark, but what on my computer works faster doesn't have to on other.

\$\endgroup\$
3
  • \$\begingroup\$ Why a down vote? \$\endgroup\$ – Franz Wexler Dec 29 '16 at 18:57
  • \$\begingroup\$ Why not: only write out the vertices for blocks that actually exist? \$\endgroup\$ – user253751 Jan 4 '17 at 2:53
  • \$\begingroup\$ @immibis Blocks can be added between existing blocks and existing blocks can be removed. Blocks would quickly become placed in random indices in array or I would need to move lots of data. \$\endgroup\$ – Franz Wexler Jan 5 '17 at 16:10
1
\$\begingroup\$

Just don't draw them.

For example, if you have 3 quads in a buffer, and you only wish to draw quads 0 and 2:

glDrawArrays (GL_QUADS, 0, 4);
glDrawArrays (GL_QUADS, 8, 4);

There's no rule saying that you must draw the entire buffer: glDrawArrays has first and count parameters for a reason - to allow you to select a subrange of the buffer to draw.

Of course, that means doing multiple draw calls, but that's a tradeoff versus the complexity and overhead of modifying a buffer.

As an alternative to multiple draw calls, you could look at glMultiDrawArrays (which may be more efficient, although some drivers may implement it as a loop over multiple glDrawArrays calls), or indexing: keep the vertex buffer static but build a dynamic index buffer. Because indices are much smaller than vertices, this can often be done much more efficiently.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.