Everybody is good, the following map mode and university study together, have a project in my lot OpenGLES2.0 SamplesForAndroid, I’ll write constantly learning sample, articles and code synchronization update, welcome attention, link: github.com/kenneycode/…
In our previous example, we used triangles with three independent vertices, and if you notice, the first argument when we call glDrawArrays() is using GL_TRIANGLES, so the first argument is to set the drawing mode. Using GL_TRIANGLES, GL_TRIANGLE_STRIP, and GL_TRIANGLE_FAN is a good example of using three common drawing patterns. Let’s start with a diagram.
This diagram shows the differences between the three drawing modes
So GL_TRIANGLES is what we used so far in our example, using three independent vertices to form a triangle, so triangles don’t share vertices
The GL_TRIANGLE_STRIP effect is, as its name suggests, banded. The rules for forming triangles are related to the odd and even number of vertices. If the number of vertices is odd, the order of the vertices is K-1, K-2, k, and if the number of vertices is even, the order of the vertices is K-2, K-1, k. What does that mean? So if we look at the figure above, for point two, it’s going to be v0, v1, v2, and for point three, it’s going to be v2, v1, v3, and you can see that the vertices are going to be shared, and you can see that each triangle is going to rotate in the same direction.
For example, for a triangle formed by v0, v1 and v2, we can render the vertex order v0, v1 and v2, or we can render the vertex order v0, v2 and v1. What’s the difference? In 2 d rendering, nothing too big difference, but in 3 d rendering, very useful, when we imagine playing 3 d games, there is a house in front, around the house, we can see the house from different angles, but at any point of view, there are always some metope is more than the house near the metope that we keep out, to see, but in general, The shaded wall will also be rendered, but due to OpenGL’s depth testing mechanism, the depth of the wall closer to us will be shallower, so the color buffer will be overwritten by the color of the distant wall to achieve occlusion.
So since it’s going to be occluded, can we just not render it? There is a way to do this, and it involves glCullFace, which is telling OpenGL not to render heads/tails, which is defined by glFrontFace, you can define clockwise or counterclockwise vertices to wrap around heads, and then glCullFace tells OpenGL to strip heads or tails, Notice that clockwise or counterclockwise is defined by the direction of observation,
Going back to our example, if a wall near us is front, then when we walk to the other side of the house and look at it again, it becomes reverse, because the triangle vertices are in reverse order, so we can remove them.
GL_TRIANGLE_FAN also works as its name suggests, as a fan, with the 0th point as the center of the fan, and the other points connecting point 0 to the previous point to form a triangle.
These three drawing modes have their own characteristics and can be used according to actual needs. If the requirements can be met, try to use the way of transferring fewer vertices to improve performance. For example, in our previous example, can GL_TRIANGLE_STRIP or GL_TRIANGLE_FAN be used instead to reduce the number of vertices transferred? You can think about it.
Let’s look at the effect in this example, using, from left to right, GL_TRIANGLES, GL_TRIANGLE_STRIP, and GL_TRIANGLE_FAN, which can be toggled with a button:
Code in my lot OpenGLES2.0 SamplesForAndroid project, in this paper, the corresponding is SampleDrawMode, link of the project: github.com/kenneycode/…
Thanks for reading!