2.1 THE OPENGL PIPELINE OpenGL流水线

2.1 THE OPENGL PIPELINE
  Modern 3D graphics programming utilizes a pipeline, in which the process of converting a 3D scene to a 2D image is broken down into a series of steps. OpenGL and DirectX both utilize similar pipelines.

现代3D图形编程应用了流水线(pipeline),在流水线里,转换3D场景到2D图片的过程被分解为一系列的步骤. OpenGL和DirectX使用了相似的流水线.
  A simplified overview of the OpenGL graphics pipeline is shown in Figure 2.2 (not every stage is shown, just the major ones we will study). The C++/OpenGL application sends graphics data into the vertex shader—processing proceeds
through the pipeline, and pixels emerge for display on the monitor.

简化过的OpenGL图形流水线如图所示(只显示了我们会学习到的重要的主要的部分).C++/OpenGL应用将图形数据发送到vertex shader,处理过程往前通过流水线, 然后,像素显露在显示器上.
  The stages shaded in blue (vertex, tessellation, geometry, and fragment) are programmable in GLSL. It is one of the responsibilities of the C++/OpenGL application to load GLSL programs into these shader stages, as follows:

标注蓝色的阶段(vertex, tessellation, gemometry, 和 fragment)是可在GLSL中编程的. C++/OpenGL应用有责任将GLSL程序加载到这些shader,如下,

  1. It uses C++ to obtain the GLSL shader code, either from text files or hardcoded as strings.
    第一步,使用C++得到GLSL shader代码, 或者来自于文本文件,或者来自于硬编码的字符串.
  2. It then creates OpenGL shader objects and loads the GLSL shader code into them.
    第二步,创建OpenGL shader对象并且将GLSL shader代码加载到其中.
  3. Finally, it uses OpenGL commands to compile and link objects and install them on the GPU.
    第三步,使用OpenGL命令以编译链接对象,并将其安装到GPU.

In practice, it is usually necessary to provide GLSL code for at least the vertex and fragment stages, whereas the tessellation and geometry stages are optional. Let’s walk through the entire process and see what takes place at each step.

实践中,至少提供 vertex, fragement阶段的GLSL代码是必要的, tessellation 和 geometry阶段是可选的. 接下里看每一个具体的情况.

2.1.1 C++/OpenGL Application
 The bulk of our graphics application is written in C++. Depending on the purpose of the program, it may interact with the end user using standard C++ libraries. For tasks related to 3D rendering, it uses OpenGL calls. As described in the previous chapter, we will be using several additional libraries: GLEW (OpenGL Extension Wrangler), GLM (OpenGL Mathematics), SOIL2 (Simple OpenGL Image Loader), and GLFW (Graphics Library Framework).

图形应用的很大一部分是用C++写的.根据程序的目的,程序可能和终端用户通过标准的C++库进行交互.对于与3D rendering有关系任务, 会使用OpenGL调用.如前章所述,我们会使用一些额外的库,GLEW,GLEW (OpenGL Extension Wrangler), GLM (OpenGL Mathematics), SOIL2 (Simple OpenGL Image Loader), and GLFW (Graphics Library Framework
 The GLFW library includes a class called GLFWwindow on which we can draw 3D scenes. As already mentioned, OpenGL also gives us commands for installing GLSL programs onto the programmable shader stages and compiling them. Finally, OpenGL uses buffers for sending 3D models and other related graphics data down the pipeline.

GLFW库包括叫做GLFWwindow的类,我们可以基于其画3D场景.已经提到过,OpenGL
 Before we try writing shaders, let’s write a simple C++/OpenGL application that instantiates a GLFWwindow and sets its background color. Doing that won’t require any shaders at all! The code is shown in Program 2.1. The main() function shown in Program 2.1 is the same one that we will use throughout this textbook. Among the significant operations in main() are: (a) initializes the GLFW library, (b) instantiates a GLFWwindow, (c) initializes the GLEW library, (d) calls the function “init()” once, and (e) calls the function “display()” repeatedly.
 The “init()” function is where we will place application-specific initialization tasks. The display() method is where we place code that draws to the GLFWwindow.In this example, the glClearColor() command specifies the color value to be applied when clearing the background—in this case (1,0,0,1), corresponding to the RGB values of the color red (plus a “1” for the opacity component). We then use the OpenGL call glClear(GL_COLOR_BUFFER_BIT) to actually fill the color buffer with that color.

posted on 2020-11-28 16:32  Shawn X.Y. Bai  阅读(351)  评论(1)    收藏  举报

导航