CIS 565 Fall 2011 Qing Sun

Slides:



Advertisements
Similar presentations
An Introduction to the OpenGL Shading Language Keith O’Conor.
Advertisements

EECS 700: Computer Modeling, Simulation, and Visualization Dr. Shontz Chapter 2: Shader Fundamentals (continued)
GLSL Basics Discussion Lecture for CS 418 Spring 2015 TA: Zhicheng Yan, Sushma S Kini, Mary Pietrowicz.
Introduction to GLSL Patrick Cozzi University of Pennsylvania CIS Fall 2014.
Informationsteknologi Wednesday, December 12, 2007Computer Graphics - Class 171 Today’s class OpenGL Shading Language.
Maths for Computer Graphics
MAT 594CM S10Fundamentals of Spatial ComputingAngus Forbes Week 5 : GLSL Shaders Topics: Shader syntax, passing textures into shaders, per-pixel lighting,
GLSL Applications: 1 of 2 Joseph Kider Source: Patrick Cozzi – Spring 2011 University of Pennsylvania CIS Fall 2011.
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.
The programmable pipeline Lecture 10 Slide Courtesy to Dr. Suresh Venkatasubramanian.
Introduction to GLSL Patrick Cozzi University of Pennsylvania CIS Spring 2011.
Mohan Sridharan Based on slides created by Edward Angel GLSL I 1 CS4395: Computer Graphics.
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
Cg – C for Graphics Eric Vidal CS 280. History General graphics processing languages – Renderman shading language (1988) Assembly languages for graphics.
Image Processing.  a typical image is simply a 2D array of color or gray values  i.e., a texture  image processing takes as input an image and outputs.
An Introduction to the OpenGL Shading Language Benj Lipchak Rob Simpson Bill Licea-Kane.
A Really (too) Short Introduction to OpenGL Peter Rautek.
OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum.
CS 480/680 Computer Graphics Shader Applications Dr. Frederick C Harris, Jr. Fall 2011.
Shading CMSC 435/634. RenderMan Light Displacement Surface Volume Imager.
Shading CMSC 435/634.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
OpenGL Shader Language Vertex and Fragment Shading Programs.
Wilf Comp Shaders. Wilf Comp 4002 Shaders Shaders are programs that you can download onto the card Can be used.
Shader Applications Ed Angel Professor Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics.
Shaders in OpenGL Marshall Hahn. Introduction to Shaders in OpenGL In this talk, the basics of OpenGL Shading Language will be covered. This includes.
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E.
OpenGL Shading Language (GLSL)
OpenGL Shading Language (GLSL)
1 Graphics CSCI 343, Fall 2015 Lecture 21 Lighting and Shading III.
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.
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.
CS 4363/6353 SHADERS/GLSL. ANOTHER LOOK AT THE PIPELINE Vertex Processing Clipping/Assembly Rasterization Fragment Processing.
OpenGL Shading Language
GPGPU: Parallel Reduction and Scan Joseph Kider University of Pennsylvania CIS Fall 2011 Credit: Patrick Cozzi, Mark Harris Suresh Venkatensuramenan.
MATLAB Constants, Variables & Expression Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun.
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.
OpenGl Shaders Lighthouse3d.com.
Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A = PI * radius ** 2 return A def circumference(diameter):
GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global.
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.
Introduction to Programming Lecture # 43. Math Library Complex number Matrix Quadratic equation and their solution …………….…
COMP 175 | COMPUTER GRAPHICS Remco Chang1/XX13 – GLSL Lecture 13: OpenGL Shading Language (GLSL) COMP 175: Computer Graphics April 12, 2016.
GPU and Programmable Shaders Introduction to Computer Graphics CENG 477.
APS105 Malloc and 2D Arrays (text Ch6.4, Ch10.2).
Shader.
CSE 381 – Advanced Game Programming GLSL Syntax
Carl Johan Gribel, PhD student
Objectives Simple Shaders Programming shaders with GLSL
Shading CMSC 435/634.
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
Day 05 Shader Basics.
UMBC Graphics for Games
UMBC Graphics for Games
Chapter VI OpenGL ES and Shader
Introduction to Computer Graphics with WebGL
GPGPU: Parallel Reduction and Scan
Transformation Operators
Programming with OpenGL Part 3: Shaders
CS 480/680 Computer Graphics GLSL Overview.
Language Definitions Chap. 3 of Orange Book.
CS 480/680 Part 1: Model Loading.
Computer Graphics Shading in OpenGL
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:

CIS 565 Fall 2011 Qing Sun

GLSL Syntax Overview GLSL is like C without Pointers Recursion Dynamic memory allocation GLSL is like C with Built-in vector, matrix and sampler types Constructors A great math library Input and output qualifiers Allow us to write concise, efficient shaders.

GLSL Syntax: in / out / uniform Recall Shader Streaming inputOutput Uniform (constant) input Shader

GLSL Syntax: in / out / uniform Example #version 330 uniform mat4 u_ModelView; in vec3 Position; in vec3 Color; out vec3 fs_Color; void main(void) { fs_Color = Color; gl_Position = u_ModelView * vec4(Position, 1.0); } uniform : shader input constant across glDraw in : shader input varies per vertex attribute out : shader output

GLSL Syntax GLSL has a preprocesssor All Shaders have main() #version 330 #ifdef FAST_EXACT_METHOD FastExact(); #else SlowApproximate(); #endif //... many others void main(void) { }

GLSL Syntax: Types Scalar types: float, int, uint, bool Vectors are also built-in types: vec2, vec3, vec4 ivec*, uvec*, bvec* Access components three ways:.x,.y,.z,.wposition or direction.r,.g,.b,.acolor.s,.t,.p,.qtexture coordinate myColor.xyzmyColor.xgb

GLSL Syntax: Vectors Constructors vec3 xyz = vec3(1.0, 2.0, 3.0); vec3 xyz = vec3(1.0); // [1.0, 1.0, 1.0] vec3 xyz = vec3(vec2(1.0, 2.0), 3.0);

GLSL Syntax: Vectors Swizzle: select or rearrange components vec4 c = vec4(0.5, 1.0, 0.8, 1.0); vec3 rgb = c.rgb; // [0.5, 1.0, 0.8] vec3 bgr = c.bgr; // [0.8, 1.0, 0.5] vec3 rrr = c.rrr; // [0.5, 0.5, 0.5] c.a = 0.5; // [0.5, 1.0, 0.8, 0.5] c.rb = 0.0; // [0.0, 1.0, 0.0, 0.5] float g = rgb[1]; // 0.5, indexing, not swizzling

GLSL Syntax: Matrices Matrices are built-in types: Square: mat2, mat3, mat4 Rectangular: matmxn m columns, n rows Stored column major

GLSL Syntax: Matrices Constructors Accessing elements mat3 i = mat3(1.0); // 3x3 identity matrix mat2 m = mat2(1.0, 2.0, // [ ] 3.0, 4.0); // [ ] float f = m[column][row]; float x = m[0].x; // x component of first column vec2 yz = m[1].yz; // yz components of second column Treat matrix as array of column vectors Can swizzle too!

GLSL Syntax: Vectors and Matrices Matrix and vector operations are easy and fast: vec3 xyz = //... vec3 v0 = 2.0 * xyz; // scale vec3 v1 = v0 + xyz; // component-wise vec3 v2 = v0 * xyz; // component-wise mat3 m = //... mat3 v = //... mat3 mv = v * m; // matrix * matrix mat3 xyz2 = mv * xyz; // matrix * vector mat3 xyz3 = xyz * mv; // vector * matrix

GLSL Syntax: Samplers Opaque types for accessing textures uniform sampler2D colorMap; // 2D texture vec3 color = texture(colorMap, vec2(0.5, 0.5)).rgb; vec3 colorAbove = textureOffset(colorMap, vec2(0.5, 0.5), ivec2(0, 1)).rgb; vec2 size = textureSize(colorMap, 0); // Lots of sampler types: sampler1D, // sampler3D, sampler2DRect, samplerCube, // isampler*, usampler*,... // Lots of sampler functions: texelFetch, textureLod texture () returns a vec4 ; extract the components you need 2D texture uses 2D texture coordinates for lookup Samplers must be uniforms 2D texture coordinate 2D integer offset

GLSL Syntax: Samplers Textures Are like 2D arrays Were the backbone of GPGPU

GLSL Built-in Functions Selected trigonometry functions float s = sin(theta); float c = cos(theta); float t = tan(theta); float theta = asin(s); //... vec3 angles = vec3(/*... */); vec3 vs = sin(angles); Works on vectors component-wise Angles are measured in radius

GLSL Built-in Functions Exponential Functions float xToTheY = pow(x, y); float eToTheX = exp(x); float twoToTheX = exp2(x); float l = log(x); // ln float l2 = log2(x); // log2 float s = sqrt(x); float is = inversesqrt(x);

GLSL Built-in Functions Selected common functions float ax = abs(x); // absolute value float sx = sign(x); // -1.0, 0.0, 1.0 float m0 = min(x, y); // minimum value float m1 = max(x, y); // maximum value float c = clamp(x, 0.0, 1.0); // many others: floor(), ceil(), // step(), smoothstep(),...

GLSL Built-in Functions Rewrite with one function call float minimum = //... float maximum = //... float x = //... float f = min(max(x, minimum), maximum); clamp()

GLSL Built-in Functions Rewrite without the if statement float x = //... float f; if (x > 0.0) { f = 2.0; } else { f = -2.0; } sign()

GLSL Built-in Functions Rewrite without the if statement bool b = //... vec3 color; if (b) { color = vec3(1.0, 0.0, 0.0); } else { color = vec3(0.0, 1.0, 0.0); } No built-in functions required

GLSL Built-in Functions Selected geometric functions vec3 l = //... vec3 n = //... vec3 p = //... vec3 q = //... float f = length(l); // vector length float d = distance(p, q); // distance between points float d2 = dot(l, n); // dot product vec3 v2 = cross(l, n); // cross product vec3 v3 = normalize(l); // normalize vec3 v3 = reflect(l, n); // reflect // also: faceforward() and refract()

GLSL Built-in Functions reflect(-l, n) Given l and n, find r. Angle in equals angle out n lr

GLSL Built-in Functions What is wrong with this code? vec3 n = //... normalize(n); normalize() returns a new vector

GLSL Built-in Functions Selected matrix functions mat4 m = //... mat4 t = transpose(m); float d = determinant(m); mat4 d = inverse(m); Think performance before using these!

GLSL Built-in Functions Selected vector rational functions vec3 p = vec3(1.0, 2.0, 3.0); vec3 q = vec3(3.0, 2.0, 1.0); bvec3 b = equal(p, q); // (false, true, false) bvec3 b2 = lessThan(p, q); // (true, false, false) bvec3 b3 = greaterThan(p, q); // (false, false, true) bvec3 b4 = any(b); // true bvec3 b5 = all(b); // false

GLSL Built-in Functions Rewrite in one line of code bool foo(vec3 p, vec3 q) { if (p.x < q.x) { return true; } else if (p.y < q.y) { return true; } else if (p.z < q.z) { return true; } return false; } return any(lessThan(p, q));

GLSL Resources (3.30) OpenGL/GLSL Quick Reference Card GLSL Spec NShader: Visual Studio GLSL syntax highlighting