GLSL学习笔记

来源:互联网 发布:c#书籍推荐知乎 编辑:程序博客网 时间:2024/05/01 14:11

今天开始看第三章,中英双语的对比很累啊。。。。

3.1首先先给出一对简单的着色器

顶点着色器:

#version 140// Global variables// uniform qualified variables are changed at most once per primitive每个图元最多改变一次uniform float CoolestTemp;uniform float TempRange;uniform mat4  MVPMatrix;// in qualified variables are typically changed per vertex每个点改变一次in  vec4  mcVertex;in  float VertexTemp;// out qualified variables communicate from the vertex shader to// the fragment shader用来传入片元着色器的值out float Temperature;void main(){    // compute a temperature to be interpolated per fragment,    // in the range [0.0, 1.0]     Temperature = (VertexTemp - CoolestTemp) /  TempRange;    gl_Position = MVPMatrix * mcVertex;}

片元着色器:

#version 140// Global variables// uniform qualified variables are changed at most once per primitive// by the application, and vec3 declares a vector of three// floating-point numbersuniform vec3 CoolestColor;uniform vec3 HottestColor; // Temperature contains the now interpolated per-fragment// value of temperature set by the vertex shaderin float Temperature;// out qualified global variables communicate from the fragment shader// to per-fragment operations (blending) and the framebufferout vec4 FragmentColor;void main(){    // get a color between coolest and hottest colors, using    // the mix() built-in function    vec3 color = mix(CoolestColor, HottestColor, Temperature);    // make a vector of 4 floating-point numbers by appending an    // alpha of 1.0, and set this fragment’s color    FragmentColor = vec4(color, 1.0);}

两个着色器都是通过声明的uniform限定变量从应用程序接受用户定义的状态。顶点着色器通过in限定变量获得与各个顶点相关的信息。信息从顶点着色器中的out限定变量中传出进入片元着色器中的in限定变量。在顶点着色器中的out声明必须和片元着色器中的in声明一一对应。在顶点着色器和片元着色器之间的固定功能,例如图元组装、裁剪,会对每个顶点着色器传出的out变量进行插值,插值后的out变量传入片元着色器的in变量。

在着色器中利用gl_开头的内置变量与opengl固定功能进行交互,例如gl_Position。

着色器的执行之间没有直接的限制和顺序。每个顶点都会执行一次顶点着色器,每个片元都会执行一个片元着色器。信息不能在顶点之间传输,也不能在片元之间传输。


3.2数据类型

1.普通的标量(scalars)有float int uint bool

2.主要的是矢量(vector)和矩阵(matrics):
矢量类型:

vec2 Vector of two floating-point numbers
vec3 Vector of three floating-point numbers
vec4 Vector of four floating-point numbers

ivec2 Vector of two integers
ivec3 Vector of three integers
ivec4 Vector of four integers

uvec2 Vector of two unsigned integers
uvec3 Vector of three unsigned integers
uvec4 Vector of four unsigned integers

bvec2 Vector of two Booleans
bvec3 Vector of three Booleans
bvec4 Vector of four Booleans
可以利用类似结构体的办法来访问一个矢量的分量
例如将矢量position看成一个位置或方向,就可以使用position.x position.yposition.z position.w来访问四个分量
类似的还有颜色(r,g,b,a),纹理坐标(s,t,p,q)

矩阵:
mat2 2 * 2 matrix of floating-point numbers
mat3 3 * 3 matrix of floating-point numbers
mat4 4 * 4 matrix of floating-point numbers
matmxn m * n matrix of floating point numbers(column * row)

可以将矩阵当成列矢量的数组进行访问,
if transform is a mat4, transform[2] is the third column of transform. The resulting type of transform[2] is vec4

记住第一个索引是列,第二个索引是列!


3.关于取样器sampler留到纹理时再分析。


4.结构体,与C++语言类似

5.数组

数组中不包含指针,声明数组只能使用中括号

可以先声明未知大小的数组,在使用前再声明数组长度,但是一旦声明数组长度就不能再改变数组的大小了。

若没声明数组大小,则编译器会根据看到的最大索引值设成该数组的大小,这一特性能保持数组大小尽量小,对保护节省硬件资源很有帮助。


6.void类型,没什么差别,不解释。

7.声明和作用域

注意声明变量不能以“gl_”开始就行了


3.3初始化器

不能初始化in out 和uniform 变量
vec4 v=vec4(1.0,1.0,1.0,4.0);
矩阵类型的初始化是以列优先的
3.4类型转换
同C语言,不解释。


3.5限定符和着色器借口
本小节是重点,并且与第一版差别较大。
首先来四个限定符
in :For frequently changing information, from the application to a vertex shader and for interpolated values read in a fragment shader 
uniform: For infrequently changing information, from the application to either a vertex shader or a fragment shader
out :For interpolated information passed from a vertex shader to a fragment shader, and for output from a fragment shader
const: For declaring nonwritable, compile-time constant variables, as in C
已经废弃了varying变量和attribute变量。
例如:
in float Temperature;
const int NumLights = 3;
uniform vec4 LightPosition[NumLights];
out float LightIntensity;

/*未完待续*/


原创粉丝点击