I am making an opengl-es app and I have always drawn quads by passing in 6 vertex indices per quad thus breaking a quad into two triangles. I thought all you could make in 3d graphics was quads and triangles..? I noticed in blender however if you use the decimate modifier you can make faces with a dozen or more verts. Can opengl-es render this? How do you render a face made up of a dozen verts? I'm not sure how it works from an opengl under the hood perspective. Does opengl just know to draw every time it receives 3 vertex indices? If so that would be convenient.
1 Answers
OpenGL ES, as well as the Core Profile of desktop OpenGL, only support primitive types that consist of triangles. Older version of OpenGL supported additional primitive types like GL_QUADS
and GL_POLYGON
, but they were not directly supported by recent hardware, and mostly redundant anyway.
What is supported in addition to separate triangles (GL_TRIANGLES
primitive type) are triangle strips (GL_TRIANGLE_STRIP
) and triangle fans (GL_TRIANGLE_FAN
). They allow you to draw sets of connected triangles without specifying 3 vertices per triangle.
The following figure (borrowed from the Red Book) shows how triangles are formed from the specified vertices for each of these primitive types:
For example, where you currently use 6 vertex indices for a quad, you can use only 4 with a triangle mesh or triangle fan. Some people claim that reducing the number of indices does not generally help performance, as long as you share the vertices.
If you have an input format that uses polygons with arbitrary numbers of vertices, that's still easy to handle as long as the polygons are convex. If you look at the triangle fan in the figure above, you will notice that the vertex ordering is actually the same as what it would be for a polygon. So a convex polygon can be represented directly by a triangle fan.
If you would rather stick with GL_TRIANGLES
for your rendering, and the input has n-sided polygons, you'll have to split them into triangles. Again, that's very easy as long as the polygons are convex. You can use the same kind of triangulation as the one shown for the triangle fan, except that you generate indices for each triangle separately. My answer here elaborates on this some more: Converting quadriladerals in an OBJ file into triangles?.
Non-convex polygons are a different matter. You will have to split them into triangles using algorithms that range from moderately simple to fairly complex. You should be able to find plenty of literature if you use a search term like "polygon triangulation algorithm". It's also possible to draw concave polygons with clever use of the stencil buffer in OpenGL. But I doubt that most modelling programs generate concave polygons, so that's a much rarer use case.

- 1
- 1

- 53,228
- 8
- 93
- 133