Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert triangle strips to triangles?

Tags:

c++

c

opengl

I'm using the GPC tessellation library and it outputs triangle strips. The example shows rendering like this:

for (s = 0; s < tri.num_strips; s++)
{
    glBegin(GL_TRIANGLE_STRIP);
    for (v = 0; v < tri.strip[s].num_vertices; v++)
        glVertex2d(tri.strip[s].vertex[v].x, tri.strip[s].vertex[v].y);
    glEnd();
}

The issue is in the fact that this renders multiple triangle strips. This is the problem for me. My application renders with VBO's, perticularly 1 VBO for 1 polygon. I need a way to modify the above code so that instead it could look something more like this:

glBegin(GL_TRIANGLES);
for (s = 0; s < tri.num_strips; s++)
{
    // How should I specify vertices here?      
}
glEnd();

How could I do this?

like image 783
jmasterx Avatar asked Feb 14 '26 13:02

jmasterx


1 Answers

perticularly 1 VBO for 1 polygon

Whoa. 1 VBO per polygon won't be efficient. Kills the whole reason for vertex buffer. The idea for vertex buffer is to cram as many vertices into it as you can. You can put multiple triangle strip into one vertex buffer, or render separate primitives that are stored in one buffer.

I need a way to modify the above code so that instead it could look something more like this:

This should work:

glBegin(GL_TRIANGLES);
for (v= 0; v < tri.strip[s].num_vertices-2; v++)
    if (v & 1){
        glVertex2d(tri.strip[s].vertex[v].x, tri.strip[s].vertex[v].y);
        glVertex2d(tri.strip[s].vertex[v+1].x, tri.strip[s].vertex[v+1].y);
        glVertex2d(tri.strip[s].vertex[v+2].x, tri.strip[s].vertex[v+2].y);
    }
    else{
        glVertex2d(tri.strip[s].vertex[v].x, tri.strip[s].vertex[v].y);
        glVertex2d(tri.strip[s].vertex[v+2].x, tri.strip[s].vertex[v+2].y);
        glVertex2d(tri.strip[s].vertex[v+1].x, tri.strip[s].vertex[v+1].y);
    }
glEnd();

Because trianglestrip triangulation goes like this (numbers represent vertex indexes):

0----2
|   /|
|  / |
| /  |
|/   |
1----3

Note: I assume that vertices in trianglestrips are stored in the same order as in my picture AND that you want triangle vertices to be sent in counter-clockwise order. If you want them to be CW, then use different code:

glBegin(GL_TRIANGLES);
for (v= 0; v < tri.strip[s].num_vertices-2; v++)
    if (v & 1){
        glVertex2d(tri.strip[s].vertex[v].x, tri.strip[s].vertex[v].y);
        glVertex2d(tri.strip[s].vertex[v+2].x, tri.strip[s].vertex[v+2].y);
        glVertex2d(tri.strip[s].vertex[v+1].x, tri.strip[s].vertex[v+1].y);
    }
    else{
        glVertex2d(tri.strip[s].vertex[v].x, tri.strip[s].vertex[v].y);
        glVertex2d(tri.strip[s].vertex[v+1].x, tri.strip[s].vertex[v+1].y);
        glVertex2d(tri.strip[s].vertex[v+2].x, tri.strip[s].vertex[v+2].y);
    }
glEnd();
like image 85
SigTerm Avatar answered Feb 16 '26 03:02

SigTerm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!