Albus Dumbledore
Albus Dumbledore

Reputation: 12616

Trouble using triangle strips in OpenGL ES 2.0

I'd like to render a full-screen aligned mesh using TRIANGLE_STRIPs. It consists of 9 vertices and 8 triangles, and should look something like:

A----B----C
| 1 /| 3 /|
|  / |  / |
| /  | /  |
|/ 2 |/ 4 |
D----E----F
| 5 /| 7 /|
|  / |  / |
| /  | /  |
|/ 6 |/ 8 |
G----H----I

Vertex A has the coordinates (-1, -1, 0), while vertex I is at (1, 1, 0). One might also say that this consists of 9 vertices positioned in 3 columns and 3 rows.

I'm trying to do this using the TRIANGLE_STRIP mode.

The indices that I supply to glDrawElements are as follows:

AD BE CF F
DG EH FI I

The CFF actually should produce a degenerated triangle so that one could skip to the next line.

What I'm having trouble with is calculating the right number to provide to glDrawElements. I'm doing it like that:

glDrawElements(GL_TRIANGLE_STRIPS, number_of_elelments,
    GLES20.GL_UNSIGNED_SHORT, buffer);

First I though it should be the number of visible triangles:

number_of_elements = 2 * (cols - 1) * (rows - 1); // 8

But it was rendering half the the rectangles.

Then I remembered the degenerated rectangles and decided to include them, too:

number_of_elements = 2 * (cols - 1) * (rows - 1) + rows; // 10

It rendered more rectangles but still not all.

Then I tred by trial and error to guess what number_of_elements should be, and I could get all rectangles to be shown, so I think it's not a problem with the rest of the setup.

Any ideas what I'm doing wrong?

Upvotes: 1

Views: 2747

Answers (1)

Christian Rau
Christian Rau

Reputation: 45948

First of all, your comment is correct and you have to provide the number of indices and not triangles. To clarify terms, an element is not a triangle and neither a vertex, it's an index (or a vertex referenced by an index).

Furthermore your drawing order is broken, anyway. You not only have to repeat F, but also D (introducing 4 instead of just 2 degenerate triangles), otherwise you will get a triangle with vertices F, D and G (which you don't want). But on the other hand you don't need to repeat I (if you don't have another row). So your rendering order would be something like:

AD BE CF F
D DG EH FI

But to figure out how to automize this in a loop depending on the number of rows and columns is your task now.

Upvotes: 1

Related Questions