In-depth understanding of OpenGL introductory tutorial

1. Introduction to OpenGL

  • OpenGL is a cross-language, cross-platform application programming specification for rendering 2D and 3D vector graphics .
  • OpenGL contains a series of functions that can manipulate graphics and images, but OpenGL does not implement these functions. OpenGL only stipulates how each function should be executed and its output value (similar to the interface), so OpenGL is only a strict specification .
  • The OpenGL specification strictly specifies how each function should be executed, as well as their output values. As for how each internal function is implemented (Implement), it will be decided by the developer of the OpenGL library. The developer of the actual OpenGL library is usually the manufacturer of the graphics card .
  • OpenGL is designed to be output only, so it only provides rendering functions . The core API has no concept of windowing systems, audio, printing, keyboard/mouse, or other input devices. While this may seem like a limitation at first, it allows the rendering code to be completely independent of the operating system it's running on, so OpenGL allows for cross-platform development .
  • OpenGL does not provide a shader compiler, but the graphics card driver completes the shader compilation work , that is, as long as the graphics card driver supports compiling GLSL, it can run, so OpenGL can be cross-platform .
  • Core mode and immediate rendering mode:
    Early OpenGL uses immediate rendering (Immediate mode, that is, fixed rendering pipeline) is easy to use and understand, but the efficiency is too low. Starting from OpenGL3.2, the immediate rendering mode is abandoned, and the use of the core mode (Core-profile) is encouraged.
    Core mode: It requires users to really understand OpenGL and graphics programming, which is somewhat difficult, but it provides more flexibility, higher efficiency, and a deeper understanding of graphics programming.
  • Extension:
    A major feature of OpenGL is the support for extensions. When a graphics card company proposes a new feature or a major optimization in rendering, it is usually implemented in the driver in an extended manner .
  • State machine:
    OpenGL itself is a huge state machine (State Machine): a series of variables describe how OpenGL should run at the moment . The state of OpenGL is usually called the OpenGL context (Context) . We usually use the following ways to change the OpenGL state: setting options, operating buffers . Finally, we render using the current OpenGL context .
  • Object:
    The OpenGL library is written in C language , and it also supports the derivation of many languages, but its core is still a C library. Since some language structures of C are not easily translated to other high-level languages , some abstraction layers were introduced during the development of OpenGL. "Object (Object)" is one of them .
    One advantage of using objects is that in the program, we can define more than one object and set their options, and each object can have different settings . When we perform an operation that uses OpenGL state, we only need to bind the object with the required settings .

Two, OpenGL program

 We all know that the program runs on the CPU , and the program code is in the memory. In our usual concept, only the CPU and memory are involved, but the rendering program using OpenGL is different, which involves the GPU and video memory , as shown in the figure below. Show:
insert image description here

From memory to video memory

  • Our program runs on the CPU, and the data of the program is stored in memory .
  • When we need to render a scene, we may need some vertex data, texture data, shader parameters, etc. of objects in the scene. These data are specified by our program, they are initially stored in external memory, and we read them into memory when our program is running on the CPU .
  • There is video memory in the graphics card , and this buffer can store data needed for a series of rendering scenes such as graphics buffer, depth buffer, texture, and vertex buffer. Using these data, execute the rendering instructions of the GPU to render the scene we want.
  • So the question is, how do we transfer the data in the memory to the video memory?
  • There is no doubt that the answer to all this lies in OpenGL. Through OpenGL related functions, our program running on the CPU can send data to the video memory, and store the data required for rendering the scene in the video memory buffer of the graphics card .

OpenGL rendering state machine

  • When the data required for rendering has been transferred to the video memory, how do we execute the rendering commands of the GPU?
  • The rendering instructions of the GPU are very low-level, involving hardware parameters such as the type of graphics card, so it is impossible for us to call such complex instructions. But we have OpenGL.
  • Rendering is a fixed process, and we cannot connect to the underlying instructions of the GPU. Can we build a general-purpose, hardware-independent rendering model ? Of course you can, this is OpenGL.
  • Rendering is a process, and there are many options that can be set in this process. We call these options the state of OpenGL . The so-called state machine has many parameters as the state, and it will react differently in different state situations , such as showing different rendering results under different scene options.
  • OpenGL is a complete rendering state machine. The Khronos organization developed and maintained OpenGL. In other words: they established the state machine model of OpenGL . This model is used for rendering and it contains many parameters as state. For this model, the Khronos organization formulated the functions that OpenGL should contain , including some state setting functions (State-changing Function) and state application functions (State-using Function). Use the state setting function to change the OpenGL context, that is, the state of OpenGL, and use the state application function to perform some operations according to the current state of OpenGL.
  • As mentioned in the introduction, OpenGL is just a specification and has no specific function implementation. As mentioned above, OpenGL is just a rendering model that contains many states, and it formulates a set of function specifications required for rendering. So how do we build a bridge between the mathematical model of OpenGL and the underlying rendering instructions of the GPU? That is, who should implement the functions formulated by OpenGL? This is usually what the graphics card developer should think about. The graphics card developer develops the OpenGL driver for the specific graphics card based on the OpenGL model specification, that is, the graphics card will execute specific rendering instructions in a specific OpenGL state .
  • In this way, we only need to interact with OpenGL, a general-purpose, hardware-independent state machine, to execute the GPU rendering instructions we need. The interaction between the program and OpenGL is nothing more than changing the state of OpenGL , such as setting the options of OpenGL, or operating the buffer, by calling the state setting function . Behaviors such as performing rendering operations are implemented through state application functions . In short, the rendering state is set first, and then the rendering operation is performed based on the state.

3. GLFW and GLEW

 The book continues from the above, OpenGL only provides rendering functions, and the core API does not have input concepts such as creating windows, keyboard monitoring, etc. , so if we want to perform rendering, we must display the rendered screen on the computer window, what should we do?
 Just use GLFW, GLFW is a cross-platform OpenGL application framework that supports functions such as window creation, input and event acceptance . Its members start with the GLFW form .
 As mentioned above, when we want to use an OpenGL function, we need to check whether the current platform supports this function, and return the pointer of this function. This is undoubtedly a very troublesome thing, so is there any convenient way?
 Just use GLEW, which is a cross-platform C++ extension library based on the OpenGL graphics interface . GLEW can automatically identify all OpenGL advanced extension functions supported by the current platform . As long as the glew.h header file is included, all functions of gl , glu, glext, wgl, and glx can be used . GLEW supports various operating systems that are currently popular. Its members often begin with gl.

4. Others

 This article refers to and cites the contents of the referenced article .
 The content of this article is only my personal impressions in OpenGL learning, if there is any inappropriateness, please feel free to enlighten me.

Guess you like

Origin blog.csdn.net/qq_51563654/article/details/130233937