简体   繁体   中英

OpenGL: skip every n quad in a GL_TRIANGLE_STRIP

I am currently rendering a long path with a:

glDrawArrays(GL_TRIANGLE_STRIP, 0, 2000);

I pass my control points to a vbo like this:

for (unsigned int i = 0; i < lPoints.size() && i < rPoints.size(); ++i)
{
    if (i % 2 != 0)
    {
        vbo.AddData(&rPoints[i], sizeof(glm::vec3));
        vbo.AddData(&m_textCoords[i], sizeof(glm::vec2));
        vbo.AddData(&normal, sizeof(glm::vec3));
    }
    else
    {
        vbo.AddData(&lPoints[i], sizeof(glm::vec3));
        vbo.AddData(&m_textCoords[i], sizeof(glm::vec2));
        vbo.AddData(&normal, sizeof(glm::vec3));
    }
}

Using the image below as an example. My existing code succeeds in drawing all the quads together to form a path. But what I'd like to achieve is to draw one quad and skip one in a sequence. Ie Draw triangles [v0, v1, v2] and [v2, v1, v3]. Skip [v2, v3, v4] and [v4, v3, v5].

三角带

Been hacking away at this for a while with no success. Any suggestions?

To skip rendering of objects you have several ways:

A) Don't put them in VBO buffers.

B) Split the only glDrawArrays call into multiple calls, each one starting at a different position in the buffer (the second parameter of glDrawArrays ).

C) Set a special color (or other property) for that primitives. If the shader meets this color discard the fragment (in a fragment shader ) or don't emit vertices in a geometry shader .

D) As @Rabbid76 suggested, use indexed drawing and primitive restart.

Rather than drawing triangle strip primitves (GL_TRIANGLE_STRIP), which necessarily implies connected topology, draw triangle primitives (GL_TRIANGLES).

You can't use the same vertex stream though.

Where you currently render GL_TRIANGLE_STRIP and pass vertices:

ABCDEFGHIJ ...

在此处输入图片说明

You'll now have to pass in GL_TRIANGLES and pass in vertices both triangles of each quad (every other quad in your case).

ABC CBD EFG GFH ...

Note the winding order because in a strip, every other triangle winds the other way. (Tempting to send vertices in the order ABC BCD EFG FGH ... but that every second triangle would be counter-clockwise - opposite winding order.)

To use the same vertex stream, draw indexed primitives, and use an index buffer: 012 213 456 657 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM