Programming with OpenGL Part 3: Shaders Ed Angel Professor of Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive.

Slides:



Advertisements
Similar presentations
Informationsteknologi Wednesday, December 12, 2007Computer Graphics - Class 171 Today’s class OpenGL Shading Language.
Advertisements

GLSL I May 28, 2007 (Adapted from Ed Angel’s lecture slides)
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
Mohan Sridharan Based on slides created by Edward Angel GLSL I 1 CS4395: Computer Graphics.
REAL-TIME VOLUME GRAPHICS Christof Rezk Salama Computer Graphics and Multimedia Group, University of Siegen, Germany Eurographics 2006 Real-Time Volume.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
An Introduction to the OpenGL Shading Language Benj Lipchak Rob Simpson Bill Licea-Kane.
Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico.
Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico.
OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum.
CS 480/680 Computer Graphics Shader Applications Dr. Frederick C Harris, Jr. Fall 2011.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
CS662 Computer Graphics Game Technologies Jim X. Chen, Ph.D. Computer Science Department George Mason University.
Programmable Pipelines Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University.
OpenGL Shader Language Vertex and Fragment Shading Programs.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Shader Applications Ed Angel Professor Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics.
CS418 Computer Graphics John C. Hart
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E.
GLSL OpenGL Programming and Reference Guides, other sources
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Vertex Buffer Objects and Shader Attributes. For Further Reading Angel 7 th Ed: –Most parts of Chapter 2. Beginning WebGL: –Chapter 1: vertex Buffer Objects,
GLSL II Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New.
Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Ed Angel Professor Emeritus of Computer Science.
Programmable Pipelines. 2 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Black Box View Geometry Processor Frame Buffer Fragment Processor.
GLSL I.
Programming with OpenGL Part 5: More GLSL Isaac Gang University of Mary Hardin-Baylor E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico.
Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico.
What are shaders? In the field of computer graphics, a shader is a computer program that runs on the graphics processing unit(GPU) and is used to do shading.
OpenGL Shading Language
OpenGl Shaders Lighthouse3d.com.
GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global.
Advanced Texture Mapping Bump Mapping & Environment Mapping (Reflection)
GLSL II Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New.
An Introduction to the Cg Shading Language Marco Leon Brandeis University Computer Science Department.
1 Introduction to Computer Graphics with WebGL ADOPTED from Ed Angel Chapter 2 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015.
COMP 175 | COMPUTER GRAPHICS Remco Chang1/XX13 – GLSL Lecture 13: OpenGL Shading Language (GLSL) COMP 175: Computer Graphics April 12, 2016.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Introduction to Computer Graphics with WebGL
Shader.
CSE Real Time Rendering
Carl Johan Gribel, PhD student
Introduction to Computer Graphics with WebGL
Objectives Simple Shaders Programming shaders with GLSL
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Programming with OpenGL Part 4: Color and Attributes
Day 05 Shader Basics.
Programming with OpenGL
Introduction to Computer Graphics with WebGL
Programming with OpenGL Part 3: Shaders
Programming with OpenGL Part 5: More GLSL
Programming with OpenGL Part 4: Color and Attributes
Mickaël Sereno Shaders Mickaël Sereno 25/04/2019 Mickaël Sereno -
Advanced Texture Mapping
Programming with OpenGL Part 5: More GLSL
CS 480/680 Computer Graphics GLSL Overview.
Language Definitions Chap. 3 of Orange Book.
CS 480/680 Part 1: Model Loading.
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science
Introduction to Computer Graphics with WebGL
CS 480/680 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

Programming with OpenGL Part 3: Shaders Ed Angel Professor of Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

2 Objectives Simple Shaders ­Vertex shader ­Fragment shaders Programming shaders with GLSL Finish first program

3 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Vertex Shader Applications Moving vertices ­Morphing ­Wave motion ­Fractals Lighting ­More realistic models ­Cartoon shaders

4 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Fragment Shader Applications Per fragment lighting calculations per vertex lighting per fragment lighting

5 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Fragment Shader Applications Texture mapping smooth shadingenvironment mapping bump mapping

6 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 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)

7 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 GLSL OpenGL Shading Language Part of OpenGL 2.0 and up High level C-like language New data types ­Matrices ­Vectors ­Samplers As of OpenGL 3.1, application must provide shaders

8 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Simple Vertex Shader in vec4 vPosition; void main(void) { gl_Position = vPosition; }

9 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Execution Model Vertex Shader GPU Primitive Assembly Application Program glDrawArrays Vertex Vertex data Shader Program

10 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Simple Fragment Program void main(void) { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); }

11 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Execution Model Fragment Shader Application Frame Buffer Rasterizer Fragment Color Shader Program

12 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Data Types C types: int, float, bool Vectors: ­float vec2, vec3, 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)

13 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 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. mat3 func(mat3 a)

14 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 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

15 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Attribute Qualifier Attribute-qualified variables can change at most once per vertex There are a few built in variables such as gl_Position but most have been deprecated User defined (in application program) ­Use in qualifier to get to shader ­in float temperature ­in vec3 velocity

16 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Uniform Qualified Variables that are constant for an entire primitive Can be changed in application and sent to shaders Cannot be changed in shader Used to pass information to shader such as the bounding box of a primitive

17 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Varying Qualified Variables that are passed from vertex shader to fragment shader Automatically interpolated by the rasterizer Old style used the varying qualifier varying vec4 color; Now use out in vertex shader and in in the fragment shader out vec4 color;

18 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Example: Vertex Shader const vec4 red = vec4(1.0, 0.0, 0.0, 1.0); out vec3 color_out; void main(void) { gl_Position = vPosition; color_out = red; }

19 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Required Fragment Shader in vec3 color_out; void main(void) { gl_FragColor = color_out; } // in latest version use form // out vec4 fragcolor; // fragcolor = color_out;

20 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Passing values call by value-return Variables are copied in Returned values are copied back Three possibilities ­in ­out ­inout (deprecated)

21 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 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

22 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 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);