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 14

Thread: glPrimitiveRestartIndex()

  1. #1
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    125

    glPrimitiveRestartIndex()

    Hi ,
    I am trying to use glPrimitiveRestartIndex() to draw GL_LINES. Here is code :

    const GLfloat data[] =
    {
    -0.9f, -0.6f,
    -0.9f, -0.9f,
    -0.6f, -0.9f,
    -0.6f, -0.6f,

    -0.3f,-0.6f,
    -0.3f,-0.9f,
    0.0f,-0.9f,
    0.0f,-0.6f
    };

    GLuint indices [] = {0, 1, 2, 3 ,666, 4 , 5 ,6 , 7 };

    glPrimitiveRestartIndex(666);
    glDrawElements(GL_LINES, 9 , GL_UNSIGNED_INT ,(GLvoid *)0);

    Now, with this code it draws only 3 parallel lines instead of 4.
    But when i increase the count in glDraw to 10, it draws 4 lines correctly. Is it what expected?
    When i replace GL_LINES with GL_TRIANGLE_FAN with count=9, it correctly draws 2 quads.

    Please let me know where i am going wrong.

    Thanks in advance,

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Jan 2007
    Posts
    964
    It doesn't make sense to use primitive restart with lines because each line is a discrete primitive. If you were using GL_LINE_STRIP primitive restart would work well and enable you to begin a new strip on a restart index.

  3. #3
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    125
    Quote Originally Posted by mhagain View Post
    It doesn't make sense to use primitive restart with lines because each line is a discrete primitive. If you were using GL_LINE_STRIP primitive restart would work well and enable you to begin a new strip on a restart index.
    Thanks for reply.. !!
    I know this is silly thing. but can you tell me whats wrong with it..?
    I think it should draw 4 lines with count 9 . Is my driver buggy?

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Jan 2007
    Posts
    964
    Your driver isn't buggy, your code is.

    When drawing with GL_LINES, each two verts specifies a line. That is all. Two verts, one line, next two verts, another line. There is no restarting needed. So 0, 1 is a line. 2, 3 is a line. 666 is a restart index but there is no current primitive to restart - the last line has already been completed. 4, 5 should be a line and 6, 7 should be a line, but because you've put that index 666 in there you've screwed up - you're right into undefined behaviour country.

    So only use primitive restart with primitives that it makes sense to restart. That's why I mentioned line strip earlier - in a line strip each additional single vert continues the strip from the previous vert. If you want to stop this and begin a new strip, that's when you'd use primitive restart.

  5. #5
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,716
    In addition to what mhagain said, you forgot to enable primitive restarting: `glEnable(GL_PRIMITIVE_RESTART);` Obviously you should disable it when done.

  6. #6
    Advanced Member Frequent Contributor
    Join Date
    Jan 2007
    Posts
    964
    Quote Originally Posted by Alfonse Reinheart View Post
    In addition to what mhagain said, you forgot to enable primitive restarting: `glEnable(GL_PRIMITIVE_RESTART);` Obviously you should disable it when done.
    Ahhhh, good catch.

    That would make 0, 1 the first line, 2, 3 the next, 666, 4 the third, 5, 6 the fourth and 7 the start of a fifth which is incomplete. Because there is no index 666 in your array (and because hardware T&L is forgiving of this kind of thing) you don't get the third line and hence you only see lines 1, 2 and 4.

    I should also note that the original NV primitive restart specifies that it's actually legal to use with all primitive types (even points), which I presume was an accomodation to allow primitive restart to be left constantly enabled and a restart index constantly specified. Not sure if that made it into core, although it's interesting that the D3D equivalent also behaves this way (with the exceptions being that you can't disable it and you're stuck with 0xffff or 0xffffffff as restart indexes).

  7. #7
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    125
    Quote Originally Posted by mhagain View Post
    Ahhhh, good catch.

    That would make 0, 1 the first line, 2, 3 the next, 666, 4 the third, 5, 6 the fourth and 7 the start of a fifth which is incomplete. Because there is no index 666 in your array (and because hardware T&L is forgiving of this kind of thing) you don't get the third line and hence you only see lines 1, 2 and 4.

    I should also note that the original NV primitive restart specifies that it's actually legal to use with all primitive types (even points), which I presume was an accomodation to allow primitive restart to be left constantly enabled and a restart index constantly specified. Not sure if that made it into core, although it's interesting that the D3D equivalent also behaves this way (with the exceptions being that you can't disable it and you're stuck with 0xffff or 0xffffffff as restart indexes).

    I enabled and disabled GL_PRIMITIVE_RESTART after use.

    its not particular to any index, i tried with 0xffff 0xffffff 0xffffffff but the result is same in all cases.
    Also, i am able to see 1, 2 ,3 lines and not 4th one. after replacing count to 10 it draws 4th but fails while drawing other primitives.
    Looks completely alien

  8. #8
    Junior Member Newbie
    Join Date
    May 2010
    Posts
    27
    You are sending in "(GLvoid *)0" into glDrawElements, do you use buffer objects?
    If not then you should pass in "indices" instead.


  9. #9
    Advanced Member Frequent Contributor
    Join Date
    Jan 2007
    Posts
    964
    Please try to understand.

    Primitive restart will have no effect when drawing with GL_LINES because each line is it's own primitive.

    The first vertex in a line begins the line.
    The second vertex in a line ends it.
    The next vertex will begin a new line.

    Lines restart themselves automatically for you. There is nothing to restart and no need to restart manually. You cannot restart after the first vertex in a line because the line is incomplete. You will draw nothing. You cannot restart after the second vertex in a line because the line is now complete - there is nothing to be restarted.

    This has been the way of it since OpenGL 1.0 and what you are trying to do makes no sense.

  10. #10
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    125
    Worked perfectly with Nvidia card , previously trying on AMD which looks buggy.

Posting Permissions

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