Said in the last section depth test, for example: the part of object A is B block, without opening the depth test cases if we draw A first distance closer to the object (B), then distance map object (A), because the distance of the bitmap after drawing, will overwrite the closer object. Some people say that you can draw the distant object A first, and then draw the closer object B, and indeed there is no problem in drawing this way, which is called the oil painting algorithm. However, imagine if the part of pixels blocked by the oil painting algorithm is drawn multiple times, which will cost a lot of overhead and waste a lot of GPU performance. Therefore, it is a good choice to enable in-depth testing when necessary. Today, however, I’m going to talk about another performance-enhancing technique — front and back culling
Why use front and back culling
Suppose you have a cube and think about how many faces you can see at most from one direction. As long as you’re not clairvoyant you can’t go more than 3 sides in any direction. So why do we draw the invisible surfaces?
We can assume that any object is divided into only two sides, a front and a back. What can be seen from the current Angle is called the front; The ones you can’t see are called tails. OpenGL does just that by checking all the faces facing the observer and rendering them; Discard the faces facing back towards the observer. This saves the performance of the chip shader.
Surrounding the order
So how do you tell OpenGL which graph you’re drawing is heads and which is tails? OpenGL uses a clever trick to analyze the wrapping order of vertex data.
When defining a set of triangular vertices, they are defined in a specific circling order, which may be Clockwise or counter-clockwise Clockwise. Each triangle is made up of three vertices, and we will set up a circle order for the three vertices from the middle of the triangle. The diagram below:
Vertex 1 is defined first, and then you can choose to define either vertex 2 or vertex 3. This choice defines the wrapping order of the triangle. The following code shows this:
floatVertices [] = {// vertices[0]; // vertices[0]; // vertices[0]; // vertices[0]; // Vertices [0]; // Vertices [0]; // vertices[2]; // vertices[2];Copy the code
Each set of three vertices that make up the triangle primitives contains a wrapping order. OpenGL will use this information when rendering primitives to determine whether a triangle is a forward or backward triangle. By default, triangles defined with counterclockwise vertices around them will be treated as positive triangles.
When defining the order of vertices, you should imagine that the corresponding triangle is facing you, so your triangle should be counterclockwise when viewed from the front. The nice thing about defining vertices this way is that the actual wrap order happens during the rasterization phase, after the vertex shader has run. These vertices are what you see from the observer’s point of view.
All the triangle vertices that the observer is facing are in the correct wrap order we specified, while the other triangle vertices are rendered in the opposite wrap order. As a result, the triangle facing us will be a forward triangle and the triangle facing back will be a backward triangle. The image below shows the effect:
For the left and right triangles in the figure above, we should define their vertex data in terms of the Angle facing the corresponding triangle, and the definition should be counterclockwise. So the front triangle is 1, 2, 3, and the back triangle is also 1, 2, 3 (if we look at the triangle from its corresponding front). However, if the triangles are drawn in 1, 2, 3 order from the observer’s current perspective, the triangles on the back will be rendered clockwise from the observer’s orientation. Although the triangle on the back was defined counterclockwise, it is now rendered clockwise. That’s the Cull we’re trying to Cull!
The relevant API
OpenGL provides a handy back-culling API, but it is disabled by default, so you must enable it in advance to take advantage of OpenGL’s back-culling feature.
glEnable(GL_CULL_FACE);Copy the code
After this line of code, all back faces will be discarded and no longer rendered. This can save up to 50% in performance when rendering snippets, but note that this only works for closed shapes like cubes. When we need some graphics that are visible on both sides in a particular scene, we must disable it again because both the forward and back sides should be visible.
glDisable(GL_CULL_FACE); // Disable both front and back cullingCopy the code
OpenGL also allows us to change the type of faces we need to cull. What if we just wanted to eliminate the forward side instead of the back side? We can call glCullFace to define this behavior
glCullFace(GL_FRONT); //GL_FRONT removes the front GL_BACK. The default value is GL_FRONT_AND_BACKCopy the code
We can also call glFrontFace to tell OpenGL that we want clockwise faces (instead of counterclockwise faces) to be positive:
glFrontFace(GL_CCW); // The default is GL_CCW, which represents anticlockwise rotation, and the other option is GL_CW, which (obviously) represents clockwise rotationCopy the code
Front and back culling is perfect for opaque convex bodies, but not for transparent or concave ones.
- OpenGL first lesson — Name explanation
-
OpenGL is introduced in lesson 2 – The common fixed storage shader
-
OpenGL introduction third lesson — matrix transformation and coordinate system
-
OpenGL entry lesson 4 — Depth
-
OpenGL entry lesson 5 — front and back elimination
-
OpenGL entry lesson 6 — Clipping and Blending
-
OpenGL entry lesson 7 — Texture
-
OpenGL is in class 8 — a supplementary case
-
OpenGL ES que
-
The application of GLKit
-
GLSL met
-
How to use custom shaders written in GLSL in iOS
-
Precision qualifier in GLSL
-
Use OpenGL to write a rotating stereo album for your girlfriend