week 3 lecture 4: part 2: glsl i based on interactive computer graphics (angel) - chapter 9
TRANSCRIPT
![Page 1: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/1.jpg)
Week 3 Lecture 4: Part 2: GLSL I
Based on Interactive Computer Graphics (Angel) - Chapter 9
![Page 2: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/2.jpg)
2Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
• Up to GLSL 1.5 Vertex and Fragment Shader Only
Default variables (e.g. projection matrix etc)
Uniform, Varying and Attributes
Covered in Lectures 1 and 2
• GLSL 1.5
• Geometry shader
• In, Out, Variables
• Removed Default Variables
• Differences Covered in GLSL Lecture 3
• GLSL 3.x
• Tessellation
• Differences Covered in GLSL Lecture 3
Shader Versions
![Page 3: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/3.jpg)
3Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Objectives
•Shader applications Vertex shaders
Fragment shaders
•Programming shaders GLSL
![Page 4: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/4.jpg)
4Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Vertex Shader Applications
•Moving vertices Morphing
Wave motion
Fractals
•Lighting More realistic models
Cartoon shaders
![Page 5: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/5.jpg)
5Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Fragment Shader Applications
Per fragment lighting calculations
per vertex lighting per fragment lighting
![Page 6: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/6.jpg)
6Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Fragment Shader Applications
Texture mapping
smooth shading environment mapping
bump mapping
![Page 7: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/7.jpg)
7Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Writing Shaders
•First programmable shaders were programmed in an assembly-like manner
•OpenGL extensions added for vertex and fragment shaders
•Cg (C for graphics) C-like language for programming shaders
Works with both OpenGL and DirectX
Interface to OpenGL complex
•OpenGL Shading Language (GLSL)
![Page 8: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/8.jpg)
8Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
GLSL
•OpenGL Shading Language•Part of OpenGL 2.0•High level C-like language•New data types
Matrices Vectors Samplers
•OpenGL state available through built-in variables
![Page 9: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/9.jpg)
9Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Simple Vertex Shader
const vec4 red = vec4(1.0, 0.0, 0.0, 1.0);
void main(void)
{
gl_Position = gl_ProjectionMatrix
*gl_ModelViewMartrix*gl_Vertex;
gl_FrontColor = red;
}
![Page 10: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/10.jpg)
10Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
const vec4 red = vec4(1.0, 0.0, 0.0, 1.0);
void main(void)
{
gl_Position = gl_ProjectionMatrix
*gl_ModelViewMartrix*gl_Vertex;
gl_FrontColor = red;
}
Vector Primitive Built In Variables* Current Vertex
*upto shader 1.5
Simple Vertex Shader
![Page 11: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/11.jpg)
11Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Execution Model (Up To 1.5)
![Page 12: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/12.jpg)
12Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Simple Fragment Program
void main(void)
{
gl_FragColor = gl_Color;
}
Current Fragment
![Page 13: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/13.jpg)
13Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Execution Model
![Page 14: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/14.jpg)
14Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Data Types
• C types: int, float, bool• Vectors:
float vec2, vec 3, vec4 Also int (ivec) and boolean (bvec)
• Matrices: mat2, mat3, mat4 Stored by columns Standard referencing m[row][column]
• C++ style constructors vec3 a =vec3(1.0, 2.0, 3.0) vec2 b = vec2(a) vec4 c = vec4(a, b)
![Page 15: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/15.jpg)
15Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Pointers
•There are no pointers in GLSL•We can use C structs which
can be copied back from functions•Because matrices and vectors are basic types they can be passed into and output from GLSL functions, e.g.
matrix3 func(matrix3 a)
![Page 16: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/16.jpg)
16Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Qualifiers (Up To 1.4)
• GLSL has many of the same qualifiers such as const as C/C++
• Need others due to the nature of the execution model
• Variables can change Once per primitive Once per vertex Once per fragment At any time in the application
• Vertex attributes are interpolated by the rasterizer into fragment attributes (e.g. colour)
![Page 17: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/17.jpg)
17Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Attribute Qualifier
•Attribute-qualified variables can change at most once per vertex
Cannot be used in fragment shaders
•Built in (OpenGL state variables)gl_Colorgl_ModelViewMatrix
•User defined (in application program)attribute float temperatureattribute vec3 velocity
![Page 18: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/18.jpg)
18Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Uniform Qualified
•Variables that are constant for an entire primitive
•Can be changed in application outside scope of glBegin and glEnd
•Cannot be changed in shader•Used to pass information to shader such as the bounding box of a primitive
![Page 19: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/19.jpg)
19Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Varying Qualified
•Variables that are passed from vertex shader to fragment shader
•Automatically interpolated by the rasterizer
•Built in Vertex colors
Texture coordinates
•User defined Requires a user defined fragment shader
![Page 20: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/20.jpg)
20Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Example: Vertex Shader
const vec4 red = vec4(1.0, 0.0, 0.0, 1.0);
varying vec3 color_out;
void main(void)
{
gl_Position =
gl_ModelViewProjectionMatrix*gl_Vertex;
color_out = red;
}
![Page 21: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/21.jpg)
21Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Required Fragment Shader
varying vec3 color_out;
void main(void)
{
gl_FragColor = color_out;
}
![Page 22: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/22.jpg)
22Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Passing values
• call by value-return•Variables are copied in•Returned values are copied back•Three possibilities
in
out
inout
![Page 23: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/23.jpg)
23Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Operators and Functions
•Standard C functions Trigonometric
Arithmetic
Normalize, reflect, length
•Overloading of vector and matrix typesmat4 a;
vec4 b, c, d;
c = b*a; // a column vector stored as a 1d array
d = a*b; // a row vector stored as a 1d array
![Page 24: Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9](https://reader035.vdocuments.us/reader035/viewer/2022062423/5697c0291a28abf838cd72fc/html5/thumbnails/24.jpg)
24Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
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, qa[2], a.b, a.z, a.p are the same
•Swizzling operator lets us manipulate componentsvec4 a;a.yz = vec2(1.0, 2.0);