SlideShare a Scribd company logo
GLSL I
Objectives
• Shader applications
• Vertex shaders
• Fragment shaders
• Programming shaders
• Cg
• GLSL
2
Vertex Shader Applications
• Moving vertices
• Morphing
• Wave motion
• Fractals
• Lighting
• More realistic models
• Cartoon shaders
3
Fragment Shader Applications
Per fragment lighting calculations
4
per vertex lighting per fragment lighting
Fragment Shader Applications
Texture mapping
5
smooth shading environment
mapping
bump mapping
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)
6
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
7
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;
}
8
Execution Model
9
Simple Fragment Program
void main(void)
{
gl_FragColor = gl_Color;
}
10
Execution Model
11
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)
12
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)
13
Qualifiers
•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
14
Attribute Qualifier
•Attribute-qualified variables can change at
most once per vertex
•Cannot be used in fragment shaders
•Built in (OpenGL state variables)
•gl_Color
•gl_ModelViewMatrix
•User defined (in application program)
•attribute float temperature
•attribute vec3 velocity
15
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
16
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
17
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;
}
18
Required Fragment Shader
varying vec3 color_out;
void main(void)
{
gl_FragColor = color_out;
}
19
Passing values
• call by value-return
• Variables are copied in
• Returned values are copied back
• Three possibilities
• in
• out
• inout
20
Operators and Functions
• Standard C functions
• Trigonometric
• Arithmetic
• Normalize, reflect, length
• Overloading of vector and matrix types
mat4 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
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
vec4 a;
a.yz = vec2(1.0, 2.0);
22

More Related Content

Similar to GLSL

OpenGL Shading Language
OpenGL Shading LanguageOpenGL Shading Language
OpenGL Shading Language
Jungsoo Nam
 
Programmable Piplelines
Programmable PiplelinesProgrammable Piplelines
Programmable Piplelines
Syed Zaid Irshad
 
Introduction of openGL
Introduction  of openGLIntroduction  of openGL
Introduction of openGL
Gary Yeh
 
Hpg2011 papers kazakov
Hpg2011 papers kazakovHpg2011 papers kazakov
Hpg2011 papers kazakov
mistercteam
 
CeedMath & CeedGL, Let's talk 3D...
CeedMath & CeedGL, Let's talk 3D...CeedMath & CeedGL, Let's talk 3D...
CeedMath & CeedGL, Let's talk 3D...
rsebbe
 
Programming with OpenGL
Programming with OpenGLProgramming with OpenGL
Programming with OpenGL
Syed Zaid Irshad
 
Game Programming 12 - Shaders
Game Programming 12 - ShadersGame Programming 12 - Shaders
Game Programming 12 - Shaders
Nick Pruehs
 
OpenGL for 2015
OpenGL for 2015OpenGL for 2015
OpenGL for 2015
Mark Kilgard
 
GFX Part 6 - Introduction to Vertex and Fragment Shaders in OpenGL ES
GFX Part 6 - Introduction to Vertex and Fragment Shaders in OpenGL ESGFX Part 6 - Introduction to Vertex and Fragment Shaders in OpenGL ES
GFX Part 6 - Introduction to Vertex and Fragment Shaders in OpenGL ES
Prabindh Sundareson
 
Getting started with open gl es 2
Getting started with open gl es 2Getting started with open gl es 2
Getting started with open gl es 2
matthewgalaviz
 
Hardware Shaders
Hardware ShadersHardware Shaders
Hardware Shaders
gueste52f1b
 
Clean architecture for shaders unite2019
Clean architecture for shaders unite2019Clean architecture for shaders unite2019
Clean architecture for shaders unite2019
Abhilash Majumder
 
Demystifying shaders
Demystifying shadersDemystifying shaders
Demystifying shaders
Bruno Opsenica
 
Demystifying shaders
Demystifying shadersDemystifying shaders
Demystifying shaders
Functional Imperative
 
CGV.pptx
CGV.pptxCGV.pptx
GLSL
GLSLGLSL
2D graphics
2D graphics2D graphics
2D graphics
Muhammad Rashid
 
Open gl basics
Open gl basicsOpen gl basics
Open gl basics
saad siddiqui
 
Computer Graphics - Lecture 01 - 3D Programming I
Computer Graphics - Lecture 01 - 3D Programming IComputer Graphics - Lecture 01 - 3D Programming I
Computer Graphics - Lecture 01 - 3D Programming I
💻 Anton Gerdelan
 
Graphical Objects and Scene Graphs
Graphical Objects and Scene GraphsGraphical Objects and Scene Graphs
Graphical Objects and Scene Graphs
Syed Zaid Irshad
 

Similar to GLSL (20)

OpenGL Shading Language
OpenGL Shading LanguageOpenGL Shading Language
OpenGL Shading Language
 
Programmable Piplelines
Programmable PiplelinesProgrammable Piplelines
Programmable Piplelines
 
Introduction of openGL
Introduction  of openGLIntroduction  of openGL
Introduction of openGL
 
Hpg2011 papers kazakov
Hpg2011 papers kazakovHpg2011 papers kazakov
Hpg2011 papers kazakov
 
CeedMath & CeedGL, Let's talk 3D...
CeedMath & CeedGL, Let's talk 3D...CeedMath & CeedGL, Let's talk 3D...
CeedMath & CeedGL, Let's talk 3D...
 
Programming with OpenGL
Programming with OpenGLProgramming with OpenGL
Programming with OpenGL
 
Game Programming 12 - Shaders
Game Programming 12 - ShadersGame Programming 12 - Shaders
Game Programming 12 - Shaders
 
OpenGL for 2015
OpenGL for 2015OpenGL for 2015
OpenGL for 2015
 
GFX Part 6 - Introduction to Vertex and Fragment Shaders in OpenGL ES
GFX Part 6 - Introduction to Vertex and Fragment Shaders in OpenGL ESGFX Part 6 - Introduction to Vertex and Fragment Shaders in OpenGL ES
GFX Part 6 - Introduction to Vertex and Fragment Shaders in OpenGL ES
 
Getting started with open gl es 2
Getting started with open gl es 2Getting started with open gl es 2
Getting started with open gl es 2
 
Hardware Shaders
Hardware ShadersHardware Shaders
Hardware Shaders
 
Clean architecture for shaders unite2019
Clean architecture for shaders unite2019Clean architecture for shaders unite2019
Clean architecture for shaders unite2019
 
Demystifying shaders
Demystifying shadersDemystifying shaders
Demystifying shaders
 
Demystifying shaders
Demystifying shadersDemystifying shaders
Demystifying shaders
 
CGV.pptx
CGV.pptxCGV.pptx
CGV.pptx
 
GLSL
GLSLGLSL
GLSL
 
2D graphics
2D graphics2D graphics
2D graphics
 
Open gl basics
Open gl basicsOpen gl basics
Open gl basics
 
Computer Graphics - Lecture 01 - 3D Programming I
Computer Graphics - Lecture 01 - 3D Programming IComputer Graphics - Lecture 01 - 3D Programming I
Computer Graphics - Lecture 01 - 3D Programming I
 
Graphical Objects and Scene Graphs
Graphical Objects and Scene GraphsGraphical Objects and Scene Graphs
Graphical Objects and Scene Graphs
 

More from Syed Zaid Irshad

Operating System.pdf
Operating System.pdfOperating System.pdf
Operating System.pdf
Syed Zaid Irshad
 
DBMS_Lab_Manual_&_Solution
DBMS_Lab_Manual_&_SolutionDBMS_Lab_Manual_&_Solution
DBMS_Lab_Manual_&_Solution
Syed Zaid Irshad
 
Data Structure and Algorithms.pptx
Data Structure and Algorithms.pptxData Structure and Algorithms.pptx
Data Structure and Algorithms.pptx
Syed Zaid Irshad
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
Syed Zaid Irshad
 
Professional Issues in Computing
Professional Issues in ComputingProfessional Issues in Computing
Professional Issues in Computing
Syed Zaid Irshad
 
Reduce course notes class xi
Reduce course notes class xiReduce course notes class xi
Reduce course notes class xi
Syed Zaid Irshad
 
Reduce course notes class xii
Reduce course notes class xiiReduce course notes class xii
Reduce course notes class xii
Syed Zaid Irshad
 
Introduction to Database
Introduction to DatabaseIntroduction to Database
Introduction to Database
Syed Zaid Irshad
 
C Language
C LanguageC Language
C Language
Syed Zaid Irshad
 
Flowchart
FlowchartFlowchart
Flowchart
Syed Zaid Irshad
 
Algorithm Pseudo
Algorithm PseudoAlgorithm Pseudo
Algorithm Pseudo
Syed Zaid Irshad
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
Syed Zaid Irshad
 
ICS 2nd Year Book Introduction
ICS 2nd Year Book IntroductionICS 2nd Year Book Introduction
ICS 2nd Year Book Introduction
Syed Zaid Irshad
 
Security, Copyright and the Law
Security, Copyright and the LawSecurity, Copyright and the Law
Security, Copyright and the Law
Syed Zaid Irshad
 
Computer Architecture
Computer ArchitectureComputer Architecture
Computer Architecture
Syed Zaid Irshad
 
Data Communication
Data CommunicationData Communication
Data Communication
Syed Zaid Irshad
 
Information Networks
Information NetworksInformation Networks
Information Networks
Syed Zaid Irshad
 
Basic Concept of Information Technology
Basic Concept of Information TechnologyBasic Concept of Information Technology
Basic Concept of Information Technology
Syed Zaid Irshad
 
Introduction to ICS 1st Year Book
Introduction to ICS 1st Year BookIntroduction to ICS 1st Year Book
Introduction to ICS 1st Year Book
Syed Zaid Irshad
 
Using the set operators
Using the set operatorsUsing the set operators
Using the set operators
Syed Zaid Irshad
 

More from Syed Zaid Irshad (20)

Operating System.pdf
Operating System.pdfOperating System.pdf
Operating System.pdf
 
DBMS_Lab_Manual_&_Solution
DBMS_Lab_Manual_&_SolutionDBMS_Lab_Manual_&_Solution
DBMS_Lab_Manual_&_Solution
 
Data Structure and Algorithms.pptx
Data Structure and Algorithms.pptxData Structure and Algorithms.pptx
Data Structure and Algorithms.pptx
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
 
Professional Issues in Computing
Professional Issues in ComputingProfessional Issues in Computing
Professional Issues in Computing
 
Reduce course notes class xi
Reduce course notes class xiReduce course notes class xi
Reduce course notes class xi
 
Reduce course notes class xii
Reduce course notes class xiiReduce course notes class xii
Reduce course notes class xii
 
Introduction to Database
Introduction to DatabaseIntroduction to Database
Introduction to Database
 
C Language
C LanguageC Language
C Language
 
Flowchart
FlowchartFlowchart
Flowchart
 
Algorithm Pseudo
Algorithm PseudoAlgorithm Pseudo
Algorithm Pseudo
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
 
ICS 2nd Year Book Introduction
ICS 2nd Year Book IntroductionICS 2nd Year Book Introduction
ICS 2nd Year Book Introduction
 
Security, Copyright and the Law
Security, Copyright and the LawSecurity, Copyright and the Law
Security, Copyright and the Law
 
Computer Architecture
Computer ArchitectureComputer Architecture
Computer Architecture
 
Data Communication
Data CommunicationData Communication
Data Communication
 
Information Networks
Information NetworksInformation Networks
Information Networks
 
Basic Concept of Information Technology
Basic Concept of Information TechnologyBasic Concept of Information Technology
Basic Concept of Information Technology
 
Introduction to ICS 1st Year Book
Introduction to ICS 1st Year BookIntroduction to ICS 1st Year Book
Introduction to ICS 1st Year Book
 
Using the set operators
Using the set operatorsUsing the set operators
Using the set operators
 

Recently uploaded

Introduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.pptIntroduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.ppt
Dwarkadas J Sanghvi College of Engineering
 
Introduction to verilog basic modeling .ppt
Introduction to verilog basic modeling   .pptIntroduction to verilog basic modeling   .ppt
Introduction to verilog basic modeling .ppt
AmitKumar730022
 
Mechatronics material . Mechanical engineering
Mechatronics material . Mechanical engineeringMechatronics material . Mechanical engineering
Mechatronics material . Mechanical engineering
sachin chaurasia
 
Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
aryanpankaj78
 
smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...
um7474492
 
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
nedcocy
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
upoux
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
Roger Rozario
 
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
Paris Salesforce Developer Group
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
Supermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdfSupermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdf
Kamal Acharya
 
TIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptxTIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptx
CVCSOfficial
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
VANDANAMOHANGOUDA
 
Operational amplifiers and oscillators notes
Operational amplifiers and oscillators notesOperational amplifiers and oscillators notes
Operational amplifiers and oscillators notes
ShachiPGowda
 
Generative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdfGenerative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdf
mahaffeycheryld
 
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
PIMR BHOPAL
 
Bayesian Decision Theory details ML.pptx
Bayesian Decision Theory details ML.pptxBayesian Decision Theory details ML.pptx
Bayesian Decision Theory details ML.pptx
amrita chaturvedi
 

Recently uploaded (20)

Introduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.pptIntroduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.ppt
 
Introduction to verilog basic modeling .ppt
Introduction to verilog basic modeling   .pptIntroduction to verilog basic modeling   .ppt
Introduction to verilog basic modeling .ppt
 
Mechatronics material . Mechanical engineering
Mechatronics material . Mechanical engineeringMechatronics material . Mechanical engineering
Mechatronics material . Mechanical engineering
 
Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
 
smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...
 
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
 
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
Supermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdfSupermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdf
 
TIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptxTIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptx
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
 
Operational amplifiers and oscillators notes
Operational amplifiers and oscillators notesOperational amplifiers and oscillators notes
Operational amplifiers and oscillators notes
 
Generative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdfGenerative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdf
 
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
 
Bayesian Decision Theory details ML.pptx
Bayesian Decision Theory details ML.pptxBayesian Decision Theory details ML.pptx
Bayesian Decision Theory details ML.pptx
 

GLSL

  • 2. Objectives • Shader applications • Vertex shaders • Fragment shaders • Programming shaders • Cg • GLSL 2
  • 3. Vertex Shader Applications • Moving vertices • Morphing • Wave motion • Fractals • Lighting • More realistic models • Cartoon shaders 3
  • 4. Fragment Shader Applications Per fragment lighting calculations 4 per vertex lighting per fragment lighting
  • 5. Fragment Shader Applications Texture mapping 5 smooth shading environment mapping bump mapping
  • 6. 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) 6
  • 7. 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 7
  • 8. 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; } 8
  • 10. Simple Fragment Program void main(void) { gl_FragColor = gl_Color; } 10
  • 12. 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) 12
  • 13. 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) 13
  • 14. Qualifiers •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 14
  • 15. Attribute Qualifier •Attribute-qualified variables can change at most once per vertex •Cannot be used in fragment shaders •Built in (OpenGL state variables) •gl_Color •gl_ModelViewMatrix •User defined (in application program) •attribute float temperature •attribute vec3 velocity 15
  • 16. 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 16
  • 17. 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 17
  • 18. 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; } 18
  • 19. Required Fragment Shader varying vec3 color_out; void main(void) { gl_FragColor = color_out; } 19
  • 20. Passing values • call by value-return • Variables are copied in • Returned values are copied back • Three possibilities • in • out • inout 20
  • 21. Operators and Functions • Standard C functions • Trigonometric • Arithmetic • Normalize, reflect, length • Overloading of vector and matrix types mat4 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 21
  • 22. 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 vec4 a; a.yz = vec2(1.0, 2.0); 22