Results 1 to 2 of 2

Thread: Drawing edges of triangles

  1. #1
    Newbie Newbie
    Join Date
    Jul 2015

    Drawing edges of triangles

    I am trying to get a grip on modern OpenGL and have a question about drawing triangles with edges to show a mesh. After some initial bafflement I have determined that geometry shaders are not present. The following snippet gets what I want on the screen and uses one buffer for the element table:

    Code :
    glPolygonOffset(2.0f, 2.0f);
    glDrawElements(GL_TRIANGLES, numelmi, GL_UNSIGNED_INT, off);
    glUniform3fv(t->uni_color, 1, black);
    for (i=0; i<numelmi; i+=3) {
        glDrawElements(GL_LINE_LOOP, 3, GL_UNSIGNED_INT, off);
        off += 3*sizeof(GLuint*);

    but it calls glDrawElements a zillion times which doesn't look right. Is it possible to inform OpenGL that all the LINE_LOOPs are 3 lines long before the glDrawElements?

    An alternative would be to create a new element buffer for LINEs and call glDrawElements once. This adds a chunk of extra code and uses more memory on the graphics card both of which I would like to avoid. It seems to me to be a fairly common task that probably has a simple solution. Many thanks for any pointers.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    You can use primitive restarting (glEnable() with GL_PRIMITIVE_RESTART (OpenGL 3.1+) or GL_PRIMITIVE_RESTART_FIXED_INDEX (OpenGL 4.3+)), which allows you to use the same element buffer for both triangles and line loops. Although there may be a performance penalty for this.

    Another option would be to use the same element array for drawing a single line strip, with the alpha of every third vertex set to zero so that the invalid lines (between the last vertex of each triangle and the first vertex of the next triangle) can be discarded by using flat shading and an alpha test. You would still need an extra element buffer for the missing edges (between the last and first vertex of each triangle), but that would only need to have one third of the edges.

    Or you could have a buffer containing all of the edges but rendered as a line strip rather than individual lines (again, using alpha to disable the segments between distinct triangles). That would only require four indices per triangle rather than six.

    Assuming that you're using shaders, you don't actually need to store alpha values for the vertices, as the alpha can be calculated in the vertex shader from gl_VertexID.

Similar Threads

  1. GL_SMOOTH interpolation causes highlights on edges of triangles
    By zaphara in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-03-2013, 08:11 PM
  2. Superposition of a triangles and its edges...
    By keziah23li98 in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 02-15-2003, 04:00 AM
  3. How to display shaded triangles and their edges?
    By imported_Alexey in forum OpenGL: Advanced Coding
    Replies: 6
    Last Post: 09-11-2002, 07:13 AM
  4. Only drawing some edges..
    By barsanpipu in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 03-18-2002, 07:39 AM
  5. How many triangles, edges per n vertices
    By rsm_00x in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-13-2002, 09:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Proudly hosted by Digital Ocean