[OpenGL] understand some basic problems

Source: Internet
Author: User


Preface


For a long time, the more you learn, the more important the basic knowledge is. When learning unity shader, I always think about what the shader is? What is the shader pipeline? How do they work? What are the restrictions? And so on. However, unity is not responsible for telling you these issues. It focuses on how, not what and why. If you want to understand some problems in depth, you still need to learn from Gl or dx. I will learn about the eighth version of GL longshu later ~ Of course, I will not give up unity.


This article aims to answer some basic questions. We always rant about them...



What is OpenGL?


This problem is very simple. It is the application interface, that is, the API, used to accessGraphics hardware. Compared with dx, OpenGL has a major feature of cross-platform. In other words, it is a hardware-independent interface that can run on a variety of different types of graphics hardware systems, or even completely a software (without graphics hardware ).


OpenGL isClient-server system. The program we write is a client, and the openg implementation provided by our computer graphics hardware manufacturer is the server. In some OpenGL implementations (such as some applications related to X Window System), the client and server may run on different machines and use network connections in the middle. In this case, the client can initiate the OpenGL command, convert it to the specific Protocol of the window system, and send it to the server. Then, the client executes OpenGL on the server for image display.



Why does OpenGL not provide window operations?


I often ask this question... Why should we use so many third-party libraries to write a gl! Can't I draw any windows by myself! In fact, OpenGL cannot be blamed, which is the result of its advantages-the characteristics of cross-platform. Because it does not depend on hardware and systems, it does not contain functions that execute window tasks, or process user input. These functions are provided by the application or system we use.



Why does OpenGL not read 3D models or images?


I used to complain that, after so many years of development of OpenGL, how can I not even provide a simple drawing requirement for reading 3D models !!! Well, this is also caused by its cross-platform features. And the above 1.1, these operations are closely related to the system storage format, OpenGL does not care ~ We must build 3D objects from geometric element sets such as points, lines, triangles, and patches.



What is shader?


This is a special type of function executed on the graphic hardware. We can understand that shader is a small program compiled for graphics processing units (GPUs. OpenGL contains a compilation tool to compile the shader Source Code Compiled by us into code that can be run on the GPU. In OpenGL, we can use four types of shader stages. The most common is vertex shaders, which can process vertex data and fragment shaders, which process fragments generated after raster.


Vertex Shaders and fragment shaders are essential parts of each OpenGL program.


What is the use of shader?


Please refer to the next section ~



Rendering pipeline)


The rendering pipeline is a series of sequential processing stages used to convert the data in our applications to OpenGL to generate a final image. Is the pipeline used by opengl4.3. (Very different from earlier versions) (Source: OpenGL programming guide 8th edition)




OpenGL starts from the provided geometric data (vertices and geometric elements) and first uses a series of shader stages to process it: vertex shading, tessellation shading (which includes two types of shaders), finally geometry shading, and then pass it to the raster Program (Rasterizer ). The raster generates fragment for each element in the cropping area (Clipping Region), and then executes a fragment shader for each fragment.


As you can see, shaders is everywhere! Not all stages are required. As mentioned above, only vertex and fragment shaders are required. Both tessellation and geometry shaders are optional.


Next, we will give a more in-depth explanation of each stage. The Difficulty coefficient of the following content is five stars (for beginners .)! However, please stick to it! They are important!



Prepare to send data to OpenGL


OpenGL requires that all data be stored inCache objectBuffer objects ). Cache objects are some memory space managed by OpenGL. "If you want me to do something, please put your stuff on my site first !" There are many ways to put data in these caches, but the most common is to use glbufferdata () for implementation. Of course, there are some other steps here, which we will discuss later.



Send data to OpenGL



After we initialize the cache, we can call OpenGL to draw the geometric elements to the screen. Such operations include gldrawarrays (). We will talk about it later. This painting process means that we pass the vertex data to the OpenGL server.



Vertex Shading



For each vertex drawn by drawing commands, OpenGL calls a vertex shader to process information about this vertex. The complexity of vertex shaders can change a lot. Some of them are simple, that is, simply copying data and passing it to the next pipeline stage. We call this a pass-through shader; some are very complicated, A lot of operations will be performed to calculate the screen position of the vertex (usually completed using the transformation matrix, which will be discussed later), and illumination may be performed to calculate the color of the vertex, or other technologies.


Generally, an application contains multiple vertex shader, but only one is active at a time ).



Tessellation Shading



Tessellation, which can be translated into a surface subdivision by reading the Thais class. After vertex shader processes information about each vertex, if the tessellation shader stage is activated, it will continue to process the data. Later we will see that tessellation uses patchs to describe the shape of an object, and allows refined (Tessellate) relatively simple patch set to increase the number of geometric elements, improve the smoothness and authenticity of the model. In the tessellation shading stage, two shaders can be used to control the Patch data and generate the final shape.


For more information, see this article (although DirectX ...)



Geometry Shading



This phase allows processing of separate geometric elements before rastering, including creating new elements. This stage is also optional, but it will be useful! I will talk about it later.



Primitive assembly



The previous pipeline stages were all operated on vertices, and they finally came to this stage with information about how to organize and construct these vertices into geometric elements. The primitive Assembly stage organizes vertices into their related geometric elements to prepare for cropping and rastering.



Cropping (clipping)


Sometimes, some vertices are outside the viewport, and the elements related to these vertices need to be cropped. This operation is automatically completed by OpenGL.



Rasterization)


After cropping, the updated elements are sent to the raster program to generate fragments. SoWhat is fragment?What about it? A fragment can be viewed as"Candidate pixel"These pixels are in an area of the frame cache. A fragment can still be rejected (reject) and will never update its related pixel location. Processing fragments is the next two phases of the task-fragment shading and per-fragment operations.



Fragment Shading



The last stage we can program to control the color is the fragment shading. In this phase, we use a shader to determine the final color of the fragment (in fact, the per-fragment operation can still modify the final color in the next phase) and its depth value (depth value ). In fragment shaders, we can perform very powerful texture ing. If a fragment shader thinks that a fragment should not be drawn, it can end a fragment processing process. This process is called the fragment discard.


We can differentiateVertex shading (including tessellation and geometry shading) and fragment Shading: Vertex shading determines the position of an element on the screen, and fragment shading uses this information to determine the fragment color.



Per-fragment operation


In addition to the fragment shader, we can also execute other fragment processing operations, which is also the final processing of a single fragment. In this phase, the visibility of a fragment is determined by depth testing (that is, Z-buffering) and stencel testing.


If a fragment successfully passes all the checks, it will directly write it into the frame cache and update its pixel color (or it may be a depth value ). If blending is enabled, the fragment color will be mixed with the current pixel color to generate a new color and then written into the frame cache.





[OpenGL] understand some basic problems

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.