GLSL 简介
作者: 刘鹏
日期: 2012-02-01
本文根据 nehe 上的 GLSL 教程翻译整理而来,对 GLSL 做了概括性的介绍。

什么是 GLSL

  • OpenGL Shading 与语言
  • 面向图形卡
  • 语法上与C/C++类似
  • Shading 语言写的小程序是 shader 程序,运行于 GPU
  • 最早的 shader 程序是拿汇编写成的,难很难使用,后来出现了 GLSL

为什么用 shader

  • 固定管线功能有限
  • 如,固定管线只能用 ambient/diffuse/specular/emissive 光照模型,别的无法使用,用 shader 可以实现自定义的光照模型
  • shader 还可实现更多的效果,如 Shadows, Environment Mapping, Per-Pixel Lighting, Bump Mapping, Parallax Bump Mapping, HDR

下图是未使用 shader 和使用 shader 的对比图2

No Shaders vs Shaders
No Shaders vs Shaders

Vertex Shader

  • vertex shader 操作每一个像素
  • 当调用 glVertex* 或者 glDrawArrays 时,vertex shader 作用于每一个像素
  • 使用 vertex shader,可以对每个像素做完全控制
  • vertex shader 代替定点管线的 Per-Vertex 操作将被代替,包括:
    • vertex transformation
    • Normal Transformation, Normalization 和 Rescaling
    • Lighting
    • Texture Coordinate Generation 和 Transformation

Fragment Shader

  • fragment shader 操作每一个 fragment which is produced by rasterization.
  • 使用 fragment shader,可以对每一个 fragmen 做到 100% 控制
  • fragment shader 代替固定管线的 Per-Fragment 操作,包括:
    • Texture access 和 application (Texture environments)
    • Fog

GLSL 数据类型

四个主类型:float, int, bool 和 sampler

vector 类型如下:

vec2, vec3, vec4         2D, 3D and 4D floating point vector
ivec2, ivec3, ivec4      2D, 3D and 4D integer vector
bvec2, bvec3, bvec4      2D, 3D and 4D boolean vectors

matrix 类型如下:

mat2, mat3, mat4         2x2, 3x3, 4x4 floating point matrix

Sampler 类型表示纹理,用于纹理采样。Sampler 类型必须被生命为 uniform


sampler1D, sampler2D, sampler3D    1D, 2D and 3D texture
samplerCube                        Cube Map texture
sampler1Dshadow, sampler2Dshadow   1D and 2D depth-component texture

Attributes, Uniforms 和 Varying

顶点属性,一致变量,易变变量

shader 的输入、输出有三种类型:uniforms, attributes, varying

uniforms 用于在主程序与 shader 间在传递数据,在主程序中设置 uniform 值,在 shader 程序中访问 uniform 且只读,vertex shader 和 fragment shader 都可使用;

attributes 只用在 vertex shader 中,他们是输入值用于改变顶点,如顶点位置、normal等。attributes 是只读的。

varying 用于从 vertex shader 到fragment shader 传递数据。在 fragment shader 中只读,在 vertex shader 中可读写。使用 varying 时,需要同时在 vertex shader 和 fragment shader 里声明相同的 varying。

所有的 attributes, uniforms 和 varying 必须是全局的,不能在一个函数内声明。

Built-in types

内建类型直接映射到 OpenGL 状态

vertex shader 中的 built-in attributes:


gl_Vertex             4D vector representing the vertex position
gl_Normal             3D vector representing the vertex normal
gl_color              4D vector representing the vertex color
gl_MultiTexCoordX     4D vector representing the texture coordinate of texture unit X

built-in uniforms:


gl_ModelViewMatrix                 4x4 Matrix representing the model-view matrix.
gl_ModelViewProjectionMatrix       4x4 Matrix representing the model-view-projection matrix.
gl_NormalMatrix                    3x3 Matrix representing the inverse transpose model-view matrix.
			           This matrix is used for normal transformation.

Built-in Varyings:


gl_FrontColor			   4D vector representing the primitives front color
gl_BackColor			   4D vector representing the primitives back color
gl_TexCoord[X]			   4D vector representing the Xth texture coordinate

built-in types which are used for shader output:


gl_Position				4D vector representing the final processed vertex position. Only
					available in vertex shader.
gl_FragColor				4D vector representing the final color which is written in the frame
					buffer. Only available in fragment shader.
gl_FragDepth				float representing the depth which is written in the depth buffer.
					Only available in fragment shader.

Generic Type

定义自己的 attribute,uniforms 和 varying:


uniform sampler2D my_color_texture;
uniform mat4 my_texture_matrix;

varying vec3 vertex_to_light_vector;
varying vec3 vertex_to_eye_vector;

attribute vec3 tangent;
attribute vec3 binormal;

GLSL 语言细节

参见原文1

SeeAlso

  1. GLLS:An Introduction
  2. Integrating Shaders into Your Game Engine