1 representation of 3D objects in OpenGL
In 3D space, a scene is a collection of objects or models. In 3D rendering, all objects are composed of triangles. This is because a triangle can represent a plane, and a 3D object is composed of one or more planes. For example, it represents a very complex 3D terrain, and its door is represented by many triangles.
|
|
Terrain after rendering |
Complex terrain is also composed of triangles. |
(Picture from Terrian editor on this site) |
Therefore, in OpenGL, we only need to specify one or more triangles to represent any 3D objects. How to specify a triangle? OpenGL provides three methods for specifying triangles: A single triangle, a triangle bar, and a triangle slice.
Specify a single triangle. This is the simplest and most direct method. That is, to call a specific OpenGL function, input three vertex coordinates and specify a triangle. For example:
|
Input three vertices (V1, V2, V3) to specify a triangle |
Triangle Bar. This method is suitable for drawing multiple triangles at the same time and there is at least one common edge between these triangles. A triangle bar specifies one or more vertices based on a single triangle. These vertices form a new triangle together with the previous vertex in order. This process is demonstrated.
|
|
Specify three vertices to determine the first triangle. |
Specify the fourth vertex to share one side with the previous triangle. |
|
Specify 5th vertices to continue advancing |
Triangle slice. In a triangle slice, all vertices are arranged by a center point in a slice. For example, a triangle slice with v1 as the center.
Since triangles can represent any image, why do we need to use triangle bars and triangle slices? This is because in the OpenGL rendering pipeline, each vertex must be transformed. For some triangle groups connected together, using a triangle bar or a triangle slice reduces the number of vertices, which means that the vertex calculation is reduced and the rendering speed is improved. For example, the third triangle slice describes four triangles. If we pass all the four triangles to OpenGL as a single triangle, we need 3x4 = 12 vertices. After using a triangle slice, we only use 6 vertices. This saves half of the computing workload!
2. OpenGL rendering pipeline
After we pass the triangle to be drawn to OpenGL, OpenGL also needs to do a lot of work to complete 3D space projection to the screen. This series of processes is called OpenGL's rendering pipeline. Generally, the OpenGL rendering process is as follows:
2.1 view Transformation
After a scenario is determined, if we want to move an object or implement roaming in the scenario, we must transform the model view. The model view transformation can move or rotate one or more objects as needed. For example, if we want to move forward along the Z axis in a 3D space, we only need to move all the objects to N Units in the-z direction. If we want to look left, we should render all the objects along the Y axis and rotate n angles to the right. This process is demonstrated.
2.2 hide on the back
In some closed 3D objects, the surface inside the object is always invisible. For these invisible planes, we can use the back to hide and ignore the rendering of the plane to increase the rendering speed. To hide the back of a triangle, we must pay attention to the round of the triangle when creating the triangle. In general, OpenGL is a counter-clockwise wound surface by default. In the triangle shown in, if the vertex is transmitted to OpenGL in the order of V1-> V3-> V2, OpenGL considers the triangle to be positive on the screen.
When using the back to hide the image, we must always follow the rules of using the counter-clockwise method on the front when passing the image to OpenGL. To enable the hidden function on the back, you only need to call the function:
Glable (gl_cull_face );
Of course, we can also change the OpenGL settings to decide whether to hide the front or back of an object. Call the following functions:
Glcullface (gl_front );
To hide the front or call
Glcullface (gl_back );
To hide the back.
2.3 light Rendering
If you enable light rendering and specify its normal for each vertex, OpenGL recalculates the color of the vertex based on its normal state and the position and nature of the light source. The use of light effects can greatly improve the authenticity of the screen. We will talk about lighting in chapter 6.
2.4 tailoring
Tailoring is to remove or crop objects that are not in the visible space or that are half in the visible space to ensure that the images that are not supposed to appear on the screen do not appear.
2.5 projection
To display an object in a 3D space on the screen, a projection is required. There are two ways of projection: Parallel projection and Perspective Projection. In parallel projection, distant objects are as large as near objects. This projection is mainly used in computer-aided design (CAD), because this projection has no stereoscopic effect, therefore, pivot projection is generally used. In Perspective Projection, objects in the distance will become smaller. Therefore, in perspective projection, the visible space is an equal part (or a body ). It indicates the principle of projection transformation.
2.6 view Space Transformation
After a 3D image is projected into a 2D image, we need to scale the image to a window or screen. This process is called the view space transformation. For general games, the visible space should be the whole screen or form. However, the visible space can also be a subset of it.
2.7 Grating
After all the transformations of 2D images are completed, You Need To raster them to display them on the screen or save them as BMP images. Raster is actually the process of converting the transformed 2D vector into a bitmap.
2.8 draw
In this step, the Raster image is displayed on the screen by Windows GDI.