Reputation: 13712
I've been using glDrawElements
to draw triangles that describe objects. However, I'm trying to load some OFF objects I found online and I saw this describing the indices of the vertices:
...
4 195 209 210 196
4 196 210 211 197
3 197 211 15
3 0 212 198
4 198 212 213 199
4 199 213 214 200
...
My question is, how do I switch between drawing elements described by 3 indices of vertices and 4 (and any other number of indices). Currently I can only load in OFF files that use 3 indices to describe the vertices:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
int size; glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glDrawElements(GL_TRIANGLES, size/sizeof(GLushort), GL_UNSIGNED_SHORT, 0);
Any tips or references/pointers to tutorials or anything related to help me generalize things?
Upvotes: 1
Views: 316
Reputation: 54642
As long as the polygons are convex, triangulating them is easy. One approach is that you split each polygon into triangles, and then draw with primitive type GL_TRIANGLES
. For a polygon with n
vertices, you will end up with n - 2
triangles defined by the vertices with the following 0-based indices:
0 1 2
0 2 3
0 3 4
...
0 n-2 n-1
For your example, the sequence of indices would be:
195 209 210 195 210 196
196 210 211 196 211 197
197 211 15
0 212 198
198 212 213 198 213 199
199 213 214 199 214 200
The more elegant approach is to draw each polygon with primitive type GL_TRIANGLE_FAN
. The order of vertices for a triangle fan is exactly the same as for a polygon, so you can simply specify the indices in order:
0 1 2 3 ... n-1
To separate the polygons, you can use primitive restart. Enable it with:
glPrimitiveRestartIndex(0xffff);
glEnable(GL_PRIMITIVE_RESTART);
and then insert the restart index between polygons. For your example:
195 209 210 196 0xffff
196 210 211 197 0xffff
197 211 15 0xffff
0 212 198 0xffff
198 212 213 199 0xffff
199 213 214 200
Upvotes: 1
Reputation: 14750
The first entry of your dataset is a quad, which can be split into 2 triangles using vertices {1,2,3} and {1,3,4} (ie {195,209,210} and {195,210,196} for the first entry). Use the same scheme for each quad you have in your dataset. If you get inverted tris, try flipping the order of the vertices ({195,210,209} instead of {195,209,210} for instance).
Upvotes: 0