隨著近年來繪圖卡的進步, 已在渲染管線中的頂點(vertex)和片斷(fragment)層次中,加入更具彈性的新功能。 達到在這個層次中,使用片斷和頂點著色器的可編程性。
最初這個功能是以組合語言撰寫著色器來達到的。組合語言對開發者的使用是不直觀而複雜的。OpenGL ARB 建立了 OpenGL 著色語言,為 GPU 的程式設計提供更加直觀的方法,當維護開放標準的時候,就有助於帶動 OpenGL 的歷史。
最初 OpenGL 1.5 是以擴充形式引入,後來 OpenGL ARB 在 OpenGL 2.0 核心中正式納入 GLSL。自 1992 年建立的OpenGL 1.0 起,OpenGL 2.0 是第一個 OpenGL 的大修改版。
使用 GLSL 有如下好處:
// 以上轉貼自 wiki http://zh.wikipedia.org/wiki/GLSL
OpenGL的GLSL基本編寫
資料參考聯結
GLSL Language Specification, Version 4.20.11
OpenGL Fragment Shader Specification
OpenGL Vertex Shader Specification
OpenGL Shader Objects Specification
The official OpenGL website
電子書 OpenGL Shading Language Third Edition
有兩個檔案來分別對應 GL_VERTEX_SHADER和GL_FRAGMENT_SHADER
以是基本範例
//檔案vertex.v.glsl for GL_VERTEX_SHADER
//attribute vec2 coord2d;
// attribute: 變數修飾語之一, 代表會從應用程式傳入(input)的變數. // vec4: 代表包含有 4 個元素的向量 // // 頂點的位置 attribute vec4 Position; // attribute 代表唯讀的變數 // 頂點的原始顏色 attribute vec4 SourceColor; // varying: 變數修飾語之一, 代表會從 vertex shader(目前這個檔案) // 傳遞(output)到 fragment shader 的變數 varying vec4 DestinationColor;
void main(void)
{
//gl_Position = vec4(coord2d, 0.0, 1.0);
// 將頂點的原始顏色(SourceColor)設定給要傳給 fragment shader // 的顏色(DestinationColor), OpenGL 會用內插法來計算其值. DestinationColor = SourceColor; // gl_Position: 在 vertex shader 中必須設定的內建輸出變數, // 代表每個頂點最終的位置. // // 在此設為原始的位置. gl_Position = Position;
}
//檔案fragment.f.glsl for GL_FRAGMENT_SHADER
// varying: 變數修飾語之一, 代表會從 vertex shader
// 傳遞(input)到 fragment shader(目前這個檔案)的變數.
// 而且必須與在 vetex shader 中所定義的變數一致. 否則會回報compile error
//
// lowp: 代表使用最低的精密度(可提升效能). (medp: 中精密度; highp: 高 精密度)
//
varying lowp vec4 DestinationColor;
void main(void)
{
//gl_FragColor[0] = gl_FragCoord.x/640.0;
//gl_FragColor[1] = gl_FragCoord.y/480.0;
//gl_FragColor[2] = 0.5;
//gl_FragColor[3] = floor(mod(gl_FragCoord.y, 2.0));
// gl_FragColor: 在 fragment shader 中必須設定的內建輸出變數, // 代表每個像素最終的顏色. // // 在此設為由 fragment shader 傳入的顏色, 再經由 OpenGL 內插(interpolated)求值. gl_FragColor = DestinationColor; //也可分別設定如前五行所設
};
參考網頁
http://itouchs.blogspot.tw/2012/06/opengl-es-20-tutorial-part-1-2.html
備註: vertex shader 會傳給 fragment shader 每個頂點的顏色值, 而當 fragment shader 要決定某個像素的顏色時, 會利用內插法來計算.
常用資料形態
參考網頁
In GLSL, the types
vec2, vec3, and vec4
represent 2D, 3D, and 4D floating-point vectors. (There are also types
for integer and boolean vectors, which are not discussed here.) Vector
variables are defined as you would expect if C, C++ or Java had these
types:vec2 a2DVector; vec3 three_dimensional_vector; vec4 vector4;
The data types for floating-point 2×2, 3×3, and 4×4 matrices are:
mat2, mat3, and mat4:mat2 m2x2; mat3 linear_mapping; mat4 trafo;
Shader-Centric View of OpenGL (VS = Vertex Shader, FS = Fragment
Shader)”.
Floating-point Precision in 3rd Generation Devices
The fragment shading language also defines the
following built-in variables:
- gl_FragData[0]
gl_FragDatais an array of output colors that has only one element. This exists in OpenGL ES only for compatibility reasons; usegl_FragColorinstead.- gl_FragCoord
- Input variable that contains window coordinates for the current fragment, which is useful for image processing.
- gl_FrontFacing
- Boolean input variable; true for front-facing primitives, false for back-faces. Use this to implement two-sided lighting.
- gl_PointCoord
- This is an input texture coordinate that's used only for point sprite rendering; we'll cover it in the section called “Rendering Confetti, Fireworks, and More: Point Sprites”.





