In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to use OpenGL face culling method". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use OpenGL face culling method" can help you solve the problem.
Now we draw the following cube with OpenGL:
No matter how we rotate the cube, we can only see three sides at most when we look at it from either direction.
So for OpenGL, the other three invisible faces do not have to draw it at all, thus improving the performance of drawing.
Noodle removal
Now that it's time to throw away the invisible, here's the problem:
How to determine which side can be seen and which side can't?
In OpenGL, it is allowed to check all faces facing the viewer and render them, while discarding all faces facing the viewer, which saves the operation of the clip shader.
So, all we have to do is tell OpenGL which side is the front and which is the back.
Determine the positive and negative sides by the order of vertex connection
When we draw a shape through a triangle, we define the order in which vertices are connected, which may be clockwise or counterclockwise.
Vertex connection order
In the image above, the triangle on the left is clockwise and the right is counterclockwise.
OpenGL uses the clockwise or counterclockwise direction of the triangle to determine whether the triangle is positive or negative.
By default, the counterclockwise vertex connection order is defined as the front of the triangle.
Both counterclockwise and clockwise are relative to the direction of the observer.
When defining the vertex order, you should imagine that the corresponding triangle is facing you, so the defined triangle vertex direction should be counterclockwise.
The advantage of this definition is that the actual connection order of the triangle vertices occurs during the rasterization phase, that is, after the vertex shader runs, the vertices are seen from the observer's point of view.
For the image above, the connection order of the left triangle 1-> 2-> 3 is clockwise, which is seen when the observer is in front of the screen. If the observer is behind the screen, the connection order is still 1-> 2-> 3, then it is counterclockwise.
This is why the vertex order of triangles should be defined on the assumption that the triangles are facing you, ensuring that they are defined counterclockwise, and that clockwise and counterclockwise directions can change according to the direction of the observer.
As shown below:
Observation of counterclockwise and clockwise triangles
The vertex order of triangles is 1-> 2-> 3. When we define this order, we assume that the observer is facing the triangle, so it is all defined counterclockwise.
But viewed from the right glasses, the right triangle is counterclockwise and the left triangle is clockwise, because for the right triangle, the observer direction is the same as the hypothetical direction when the order was defined, while for the left triangle, the observer direction is opposite to the hypothetical direction when defining the order, so it is clockwise from the opposite direction.
In this way, under the optimization of face culling, the right side is visible and the left side is invisible, that is, the front side is visible to the observer and the negative side is not visible.
Read a lot of articles, did not say: why to define the direction of the triangle counterclockwise, but the observation has become clockwise, it is because the original definition of counterclockwise direction is actually linked to the direction of the observer.
When the observer direction of the image above changes to the left, then the vertex connection order is still 1-> 2-> 3, the order of the triangles on the left is counterclockwise as the vertex order was defined, and the vertex order of the triangles on the right is clockwise invisible.
Understand this, for face culling more clearly.
Specific use
In OpenGL, you can use the following methods to open face culling:
1glEnable (GLES20.GL_CULL_FACE)
Face culling is off by default.
After the open faces are removed, all faces facing back to the viewer are discarded, saving rendering performance.
In addition, OpenGL provides other features to select faces to weed out.
1 public static native void glCullFace (
2 int mode
3)
There are three modes to choose from:
GL_BACK: culling only back faces
GL_FRONT: only positive faces are excluded
GL_FRONT_AND_BACK: weeding out front and back faces
The initial value of glCullFace is GL_BACK, and only the back faces are excluded.
In addition to the faces you need to weed out, you can also tell OpenGL to define a clockwise face (rather than a counterclockwise face) as a positive face by calling the glFrontFace method.
1 public static native void glFrontFace (
2 int mode
3)
It has two options:
GL_CCW: represents a positive face counterclockwise
GL_CW: represents a positive face clockwise
The glFrontFace mode value is GL_CCW, and the counterclockwise direction is positive.
Now it is assumed that the opening face is removed, and the positive direction is removed, clockwise:
1 glEnable (GL_CULL_FACE)
2 glCullFace (GL_FRONT)
3 glFrontFace (GL_CW); this is the end of the introduction of "how to use OpenGL face culling method". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.