Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: rendering an indexed triangle array with vbo

  1. #1
    Junior Member Newbie
    Join Date
    May 2017
    Posts
    7

    rendering an indexed triangle array with vbo

    Good day to all, i have an array of triangles where each element is an index to a corresponding vertex in a vertex buffer. Now i have uploaded the vbo and ibo into a vao , lets pretend i want to draw only a small set of triangles in a random order, let's say i want to draw triangle 5, 24, 32 ,16 ,8 and so on . How can i achieve this without rebuilding the ibo for each frame ?
    sorry but i am pretty new to opengl.
    Thanks for your time

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,687
    Use multiple draw calls; the parameters to all glDraw* calls allow you to specify a first (which with glDrawElements is the offset to the first) and count, so use them, because you don't have to draw the entire buffer with each draw call.

  3. #3
    Junior Member Newbie
    Join Date
    May 2017
    Posts
    7
    Quote Originally Posted by mhagain View Post
    Use multiple draw calls; the parameters to all glDraw* calls allow you to specify a first (which with glDrawElements is the offset to the first) and count, so use them, because you don't have to draw the entire buffer with each draw call.
    Sorry i didn't understand completely what you meant, wouldn't this be very inefficient to have a drawcall for each triangle ????

  4. #4
    Advanced Member Frequent Contributor arekkusu's Avatar
    Join Date
    Nov 2003
    Posts
    870
    glMultiDrawElements.

  5. #5
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,408
    I'd try both glMultiDrawElements() and building the index array each frame, and see which is faster.

    I wouldn't take it for granted that glMultiDrawElements() will win for single-triangle batches.

  6. #6
    Senior Member OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,687
    Quote Originally Posted by vincent71 View Post
    Sorry i didn't understand completely what you meant, wouldn't this be very inefficient to have a drawcall for each triangle ????
    It depends.

    If you really really want to draw them without rebuilding your index buffer then this is one way of doing so, and it's a tradeoff versus the cost of multiple draw calls versus the cost of rebuilding the index buffer.

    glMultiDrawElements will also work but some drivers may implement that as a loop over a bunch of glDrawElements calls.

    A third way, that's probably going to seem unintuitive, is glBegin(GL_TRIANGLES), a bunch of glArrayElement calls (3 per triangle), then glEnd. This lets you keep your vertex buffer and dynamically index it without using an index buffer and may be fastest of all.

  7. #7
    Junior Member Newbie
    Join Date
    May 2017
    Posts
    7
    The problem of rebuilding the index buffer is not that horrible, i'd like to update only the index buffer without using glbufferdata , since i have read around that its not the most efficient way to do , using gldrawelement is another deprecated method, i think that the only viable way is to use glmultidrawelement

  8. #8
    Senior Member OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,687
    There's nothing deprecated about glDrawElements.

  9. #9
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,408
    Quote Originally Posted by vincent71 View Post
    The problem of rebuilding the index buffer is not that horrible, i'd like to update only the index buffer without using glbufferdata , since i have read around that its not the most efficient way to do
    There are 3 basic options: glBufferData(), glBufferSubData(), and glMapBuffer[Range]().

    Replacing the entire buffer with glBufferData() is likely to be preferable to using glBufferSubData() to overwrite a portion of it. The former allows the implementation to allocate a new block of memory for the buffer and "orphan" the old block (flag it to be deleted once the last command referencing it has completed). The latter has to wait until the GPU has finished reading from the region being replaced.

    Similarly, glMapBufferRange() is preferable to glMapBuffer(), as the latter supports various flags to allow the GPU to avoid unnecessary copies and/or pipeline stalls.

  10. #10
    Junior Member Newbie
    Join Date
    May 2017
    Posts
    7
    As far as i can understand the *range instruction replace only a portion leaving the size untouched, i need to draw an indexed triangle buffer, in the sense that i have a bound vertex buffer to a vao.
    Now i have read that if i bound to the same vao an element array , the gldrawelements 's last parameter is considered as an offset to the already bound buffer, if element array is not bound the last parameter
    is considered to be an indices , correct me if i am wrong.
    P.S.
    English is not my native language, sorry for that, further i have read about double buffering the vbos , where can i find some concrete code ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •