With OpenGL rendering, we need to decide which parts are visible to the observer and which parts are not. We need to do something with the invisible parts, otherwise the following happens.So we give the following scheme:

1. Oil painting algorithm: first draw objects far from the observer (Picture 1), and then draw objects close to the observer (Picture 2). As shown in the figure below.

However, there is a drawback to this method, if the overlay of images is disordered and complex, it is not clear when drawing the correct sequence! , as shown in the figure below:So that gives us method 2.

2, the front and back out

OpenGL can do this by checking all the faces facing the observer and rendering them, thereby discarding the backs, to address the performance of the slice shader. So we need to analyze the order of the vertex data to distinguish between the front and back of the graph.

Face: Triangular faces joined in counterclockwise order of vertices

Back: Triangular faces joined in clockwise order of vertices

OpenGL methods:

• Enable surface culling (default is back culling)

void glEnable(GL_CULL_FACE);
Copy the code

• Off surface raised (default backside culled)

Void glDisable (GL_CULL_Face);Copy the code

• The user chooses which face to remove

void glCullFace(GLenum mode); Model parameters: GL_FRONT, GL_BACK,GL_FRONT_AND_BACK. By default, GL_BACK uses the user's finger to order the frontCopy the code

The effect after elimination using front and back is as follows: As can be seen from the figure above, we have solved the problem of abnormal display caused by hidden faces, but we have the problem as shown in the figure above, which is related to display problems caused by depth.

1. What is depth?

The depth is the Z value of the pixel from the camera’s position in the 3D world.

What is a depth buffer

A depth buffer is an area of memory dedicated to holding the depth value of each pixel on the screen. The greater the depth, the greater the Z value.

3. Significance of depth buffer

When not using depth test, if we will draw a close objects, in drawing a remote object, objects at a distance because after drawing, would bring them close cover object, with the depth buffer, the order of the objects to draw is not so important, in fact as long as there is a depth buffer, OpenGL to write the depth values of pixels which, Unless glDepthMask(GL FALSE) is used to disable writing.

Therefore, when we start the depth test, we should first compare the depth value of the pixel corresponding to the surface with the value in the current depth buffer. If it is larger than the value in the depth buffer, this part will be discarded; otherwise, the depth buffer and color buffer will be updated respectively with the depth value and color value corresponding to the pixel. This process is called depth testing. The depth buffer and the color buffer correspond one to one. In-depth testing is done in pixels, not in the currently drawn graph.

4. How to enable depth testing?

glEnable(GL_DEPTH_TEST).
Copy the code

In use we need to note that if we need to use depth testing in the project, we need to clear the depth buffer before drawing. If you do not clear the color/depth buffer, data will remain, resulting in drawing exceptions.

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Copy the code

The effect picture after opening the in-depth test is as follows:

However, in some complex scenes, the depth values will be the same, which will lead to the ZFighting flicker problem.

ZFighting flicker: that is, OpenGL cannot correctly judge the depth value of the drawing of the same plane, resulting in unpredictable in-depth test results and staggered flicker of displayed textures). The following processing is needed to make the polygon offset produce some slight changes in its depth value, so as to achieve the effect of distinguishing the depth value of the two graphics. Specify offset

void glPolygonOffet(Glfloat factor,Glfloat units);
Copy the code

In general, simply assigning -1.0 and -1 to glPolygonOffet will suffice. So we can draw a more realistic scene.