Possible problems during rendering
When drawing a 3D scene, we need to decide which parts are visible to the observer and which parts are not. The invisible parts should be discarded early. For example, after an opaque wall, it should not be rendered. This condition is called Hidden surface elimination.
Solution 1: oil painting algorithm
- Draw objects in the scene that are farther away from the observer, and then draw objects that are closer.
- For example, in the following legend, first draw the red part, then draw the yellow part, and finally draw the gray part to solve the problem of hidden surface elimination.
disadvantages
- If three triangles are superimposed, the oil painting algorithm will not be able to handle it.
Solution two: front and back out
background
Imagine a 3D image that, when viewed from any direction, will always have invisible faces, so why bother to draw faces that are not visible at all? If we could somehow discard this invisible data, OpenGL could improve rendering performance by more than 50%.
How do you know if a surface will appear in the observer’s field of vision?
- Any plane has 2 faces, front/back. It means you only see one side at a time.
- OpenGL can save slice shader performance by checking all faces facing the observer and rendering them, discarding the backs.
How do I tell OpenGL which face of the graph I’m drawing is heads?
- By analyzing the order of vertex data
GLfloat vertices[] = {// clockwise vertices[0], // vertex 1 vertices[2], // vertex 2 vertices[2], // vertices[0], // vertices[2], // vertices[1], // vertices[2];Copy the code
- Face: Triangular faces in the order of counterclockwise vertices
- Back side: Triangular faces joined in order by clockwise vertices
Analyze the front and back sides of the cube
- The order of the left triangle vertices is: 1 -> 2 -> 3; The vertices of the right triangle are in order 1 -> 2 -> 3
- When the observer is on the right, the triangle on the right is counterclockwise for heads, and the triangle on the left is clockwise for tails.
- When the observer is on the left, the triangle on the left is judged to be facing counterclockwise, and the triangle on the right is judged to be facing clockwise.
conclusion
The front and back sides are determined by the order in which the vertices of the triangle are defined and by the direction of the observer. The front and back sides change as the Angle of the observer changes.
Actual code operation
- Enable surface culling (default backside culling)
void glEnable(GL_CULL_FACE);
Copy the code
- Turn off surface culling (default backside culling)
void glDisable(GL_CULL_FACE);
Copy the code
- Which side (front/back) the user chooses to exclude
void glCullFace(GLenum mode); The mode parameter is GL_FRONT, GL_BACK, and GL_FRONT_AND_BACK. The default value is GL_BACKCopy the code
- The user specifies the order in which triangle vertices are wrapped around the front of the OpenGL environment
void glFrontFace(GLenum mode); The mode parameter is GL_CW(clockwise) and GL_CCW(counterclockwise). The default value is GL_CCWCopy the code
- Example: Eliminating positive implementations (1)
glCullFace(GL_BACK);
glFrontFace(GL_CW);
Copy the code
- Example: Eliminating positive implementations (2)
glCullFace(GL_FRONT);
Copy the code