GLSL I Open GL Shading Language Objectives Shader
GLSL I Open. GL Shading Language
Objectives • Shader applications Vertex shaders Fragment shaders • Programming shaders Cg GLSL 1 2
Vertex Shader Applications • Moving vertices Morphing Wave motion Fractals • Lighting More realistic models Cartoon shaders GLSL 1 3
Fragment Shader Applications Per fragment lighting calculations per vertex lighting GLSL 1 per fragment lighting 4
Fragment Shader Applications Texture mapping smooth shading GLSL 1 environment mapping bump mapping 5
Writing Shaders • First programmable shaders assembly like • Open. GL extensions added for vertex and fragment shaders • Cg (C for graphics): C like language for programming shaders Works with both Open. GL and Direct. X Interface to Open. GL complex • Open. GL Shading Language (GLSL) GLSL 1 6
GLSL • Open. GL Shading Language • Part of Open. GL 2. 0 • High level C like language • New data types Matrices Vectors Samplers • Open. GL state available through built in variables GLSL 1 7
Simple Vertex Shader const vec 4 red = vec 4(1. 0, 0. 0, 1. 0); void main(void) { gl_Position = gl_Projection. Matrix *gl_Model. View. Martrix*gl_Vertex; gl_Front. Color = red; } GLSL 1 8
Execution Model GLSL 1 9
Simple Fragment Program void main(void) { gl_Frag. Color = gl_Color; } GLSL 1 10
Execution Model GLSL 1 11
Data Types • C types: int, float, bool • Vectors: float vec 2, vec 3, vec 4 Also int (ivec) and boolean (bvec) • Matrices: mat 2, mat 3, mat 4 Stored by columns Standard referencing m[row][column] • C++ style constructors vec 3 a = vec 3(1. 0, 2. 0, 3. 0) vec 2 b = vec 2(a) GLSL 1 12
Pointers • No pointers in GLSL • Can use C structs can be copied back from functions • matrices and vectors are basic types • can be passed into output from GLSL functions, e. g. , matrix 3 func(matrix 3 a) GLSL 1 13
Qualifiers • GLSL has many of same qualifiers as C/C++ such as const • Need others due to nature of execution model • Variables can change Once per primitive Once per vertex Once per fragment At any time in application • Vertex attributes interpolated by rasterizer into fragment attributes GLSL 1 14
Attribute Qualifier • Attribute qualified variables can change at most once per vertex Cannot be used in fragment shaders • Built in (Open. GL state variables) gl_Color gl_Model. View. Matrix • User defined (in application program) attribute float temperature attribute vec 3 velocity GLSL 1 15
Uniform Qualified • Variables that are constant for an entire primitive • Can be changed in application outside scope of gl. Begin and gl. End • Cannot be changed in shader • Used to pass information to shader • such as bounding box of primitive GLSL 1 16
Varying Qualified • Variables passed from vertex shader to fragment shader • Automatically interpolated by rasterizer • Built in Vertex colors Texture coordinates • User defined Requires user defined fragment shader GLSL 1 17
Example: Vertex Shader const vec 4 red = vec 4(1. 0, 0. 0, 1. 0); varying vec 3 color_out; void main(void) { gl_Position = gl_Model. View. Projection. Matrix*gl_Vertex; color_out = red; } GLSL 1 18
Required Fragment Shader varying vec 3 color_out; void main(void) { gl_Frag. Color = color_out; } GLSL 1 19
Passing values • call by value-return • Variables copied in • Returned values copied back • Three possibilities in out inout GLSL 1 20
Operators and Functions • Standard C functions Trigonometric Arithmetic Normalize, reflect, length • Overloading of vector and matrix types mat 4 a; vec 4 b, c, d; c = b*a; // column vector stored as 1 d array d = a*b; // row vector stored as 1 d array GLSL 1 21
Swizzling and Selection • Can refer to array elements by element using [] or selection (. ) operator with x, y, z, w r, g, b, a s, t, p, q a[2], a. b, a. z, a. p are the same • Swizzling operator lets us manipulate components vec 4 a; a. yz = vec 2(1. 0, 2. 0); GLSL 1 22
Swizzling • To convert external names, array indices, or references within a data structure into address pointers when the data structure is brought into main memory from external storage (also called "pointer swizzling"); this may be done for speed in chasing references or to simplify code (e. g. by turning lots of name lookups into pointer dereferences). The converse operation is sometimes termed "unswizzling". GLSL 1 23
- Slides: 23