tettoffensive
tettoffensive

Reputation: 672

Disconnecting 2 vertical "lines" with glDrawArrays GL_TRIANGLE_STRIP

Is it possible with just one set of array buffer data formatted for a GL_TRIANGLE_STRIP to separate/disconnect the lines. Bascially I have a bunch of vertical lines spaced across the viewport in a 2D ortho projection. Each line is 2 triangles (so 4 vertices for a triangle strip). There is a diagonal line that connects to the next line over that is undesirable.

Am I stuck with either changing to GL_TRIANGLES or separating out the strips into separate arrays?

Upvotes: 2

Views: 1297

Answers (1)

Reto Koradi
Reto Koradi

Reputation: 54592

In higher versions of OpenGL (ES 3.0, or any recent version of full OpenGL), you would have a few options:

  1. Primitive restart. To use this, you call (in ES 3.0):

    glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
    

    and then use the highest possible index for the index format (e.g. 0xffff for GL_UNSIGNED_SHORT) at any point in the index sequence where you want to start a new primitive.

  2. Use instanced rendering, or calls from the glMultiDraw*() family to render multiple primitives with a single draw call.

  3. Repeat indices.

The last one is the only real option in ES 2.0, so I'll elaborate on it some more. While this approach may seem somewhat dirty (it does to me...), it has been used for a long time, and is actually perfectly safe. The idea is that you repeat vertices to connect the "separate" primitives with degenerate triangles, that do not draw any pixels because two of their vertices are the same.

To make this work, you repeat the last index of one primitive, as well as the first of the next primitive. Say you have two triangle strips of 4 vertices each, with the following indices:

i1 i2 i3 i4
i5 i6 i7 i8

If you want to draw this as a single triangle strip, your index sequence is:

i1 i2 i3 i4 i4 i5 i5 i6 i7 i8

Note how i4 (last index of first original primitive) and i5 (first index of second original primitive) are repeated. This allows the generation of the invisible degenerate triangles that connect the two original primitives.

Upvotes: 5

Related Questions