简介
本文的原版为《OpenGL Super Bible 7th》,是同事给我的,翻译是原文+译文的形势。文章不属于机器直译,原因在于语言不存在一一对应的关系,我将尽可能的按照中国人看起来舒服的方式来翻译这些段子,如果段子让你感到身心愉悦,那还劳烦点个关注,追个更。如果我没有及时更新这些东西,那一定是我没好好干活导致的,欢迎同学们监督。
另外,我在东汉书院中为同学们准备了大量的游戏开发图形学方面的资料,欢迎下载东汉书院App,跟着我们,每天进步一点点,帮助同学们实现从理论到代码落地的最后一步。关于东汉书院App的问题以及大家觉得可以改进的地方,尽管提!
东汉书院,等你来玩哦~
Using Shaders(使用Shader)
As we mentioned in the introduction to the graphics pipeline in Chapter 1, OpenGL works by connecting a number of mini-programs called shaders together with fixed-function glue(如同我们在第一节中提到的,OpenGL会把一大堆shader和固定的功能组合起来完成整个渲染). When you draw, the graphics processor executes your shaders and pipes their inputs and outputs along the pipeline until pixels come out the end(当你开始绘图的时候,显卡会调用你的shader,并且正确的处理他们的输入和输出数据,直到最终产生画面). To draw anything at all, you’ll need to write at least a couple of shaders(为了画画,你至少需要写两个shader)
OpenGL shaders are written in a language called the OpenGL Shading Language(OpenGL的Shader使用一个叫OpenGL着色器语言的编写,简称glsl), or GLSL. This language has its origins in C(这个语言是从C语言发展而来), but has been modified over time to make it better suited to running on graphics processors(但是随着时间的发展,它变得更加适合在显卡上运行,所以也做出了一定的修改). If you are familiar with C, then it shouldn’t be hard to pick up GLSL(如果你会C语言编程的话,那么你学glsl是没什么难度的). The compiler for this language is built into OpenGL(glsl的编译器是内置在OpenGL里的). The source code for your shader is placed into a shader object and compiled, and then multiple shader objects can be linked together to form a program object(你写的那些shader的源码可以被放到shader对象里,然后进行编译,最后这些编译好的shader对象们可以链接成一个GPU程序). Each program object can contain shaders for one or more shader stages(每个GPU程序包含了多个shader,每个shader处理渲染管线中的一个过程). The shader stages of OpenGL are vertex shaders, tessellation control and evaluation shaders, geometry shaders, fragment shaders, and compute shaders(这些shader可以处理的部分为vertex shader、tessellation control shader、tessellation evaluation shader,geometry shader,fragment shader,compute shader). The minimal useful pipeline configuration consists of only a vertex shader (or just a compute shader)(你至少需要写一个vertex shader或者是一个compute shader才能生成一个GPU程序), but if you wish to see any pixels on the screen(如果你还想在屏幕上看见点什么东西被画出来,那么你还需要一个fragment shader), you will also need a fragment shader
Listing 2.3 shows our first vertex shader, which is about as simple as it gets(Listing2.3展示了我们的第一个vertex shader,它简直太令人兴奋了). In the first line, we have the “#version 450 core” declaration(在第一行我们写的这句是告诉编译器我们打算使用4.5版本的着色器语言), which tells the shader compiler that we intend to use version 4.5 of the shading language. Notice that we include the keyword core to indicate that we intend to use only features from the core profile of OpenGL.(注意到,俺们在代码最后还写了一个core,表示,俺们只使用核心标准的语法)
Next, we have the declaration of our main function(接下来,就是咱们的主函数的申明了), which is where the shader starts executing(这就是shader的入口函数了,跟C语言里那个main函数一毛一样的作用). This is exactly the same as in a normal C program, except that the main function of a GLSL shader has no parameters(唯一的区别就是glsl的main函数没有参数列表). Inside our main function, we assign a value to gl_Position(在咱们的主函数里,我们给gl_Position赋了一个值,它非常高兴), which is part of the plumbing that connects the shader to the rest of OpenGL(这个变量是必须要赋值的,它是整个渲染管线的一部分,你要不这么干,后