Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Similar documents
Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Programming with OpenGL Part 3: Shaders. Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Lecture 5 Vertex and Fragment Shaders-1. CITS3003 Graphics & Animation

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 3: Shaders

GLSL 1: Basics. J.Tumblin-Modified SLIDES from:

2D graphics with WebGL

Supplement to Lecture 22

Today s Agenda. Shaders fundamentals. Programming with shader-based OpenGL

CS 432 Interactive Computer Graphics

Objectives. Open GL Shading Language (GLSL)

Computer Graphics (CS 543) Lecture 3b: Shader Setup & GLSL Introduction

Computer Graphics (CS 4731) & 2D Graphics Systems

Shaders. Slide credit to Prof. Zwicker

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

OPEN GL BACKGROUND. Objectives. Early History of APIs. SGI and GL. PHIGS and X. Modified from Angel 6e book slides

12.2 Programmable Graphics Hardware

WebGL A quick introduction. J. Madeira V. 0.2 September 2017

Information Coding / Computer Graphics, ISY, LiTH GLSL. OpenGL Shading Language. Language with syntax similar to C

Programmable Graphics Hardware

Shader Programming 1. Examples. Vertex displacement mapping. Daniel Wesslén 1. Post-processing, animated procedural textures

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

Programming with OpenGL Part 5: More GLSL. Ed Angel Professor Emeritus of Computer Science University of New Mexico

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

OPENGL RENDERING PIPELINE

SHADER PROGRAMMING. Based on Jian Huang s lecture on Shader Programming

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

Today s Agenda. Basic design of a graphics system. Introduction to OpenGL

The Transition from RenderMan to the OpenGL Shading Language (GLSL)

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

COMP371 COMPUTER GRAPHICS

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

EDAF80 Introduction to Computer Graphics. Seminar 3. Shaders. Michael Doggett. Slides by Carl Johan Gribel,

Shader Programming. Daniel Wesslén, Stefan Seipel, Examples

Some advantages come from the limited environment! No classes. Stranight ans simple code. Remarkably. Avoids most of the bad things with C/C++.

CS4621/5621 Fall Computer Graphics Practicum Intro to OpenGL/GLSL

Programmable Shading. University of Texas at Austin CS354 - Computer Graphics

The Graphics Pipeline

OpenGL pipeline Evolution and OpenGL Shading Language (GLSL) Part 2/3 Vertex and Fragment Shaders

Shaders. Introduction. OpenGL Grows via Extensions. OpenGL Extensions. OpenGL 2.0 Added Shaders. Shaders Enable Many New Effects

The Graphics Pipeline

CSE 167: Introduction to Computer Graphics Lecture #7: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Spring Quarter 2016

Lecture 09: Shaders (Part 1)

Introduction to Shaders.

Programmable GPUs. Real Time Graphics 11/13/2013. Nalu 2004 (NVIDIA Corporation) GeForce 6. Virtua Fighter 1995 (SEGA Corporation) NV1

CS475/CS675 - Computer Graphics. OpenGL Drawing

Lecture 9(B): GPUs & GPGPU

OpenGL Programmable Shaders

Lecture 2. Shaders, GLSL and GPGPU

Sign up for crits! Announcments

GPU Programming EE Final Examination

Introduction to GLSL. Announcements. Agenda. Course Contents. Patrick Cozzi University of Pennsylvania CIS Fall 2012

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 2: First Program

Introduction to Computer Graphics with WebGL

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 4: Three Dimensions

CS GPU and GPGPU Programming Lecture 7: Shading and Compute APIs 1. Markus Hadwiger, KAUST

Shading Language Update

Mobile Application Programing: Android. OpenGL Operation

The Projection Matrix

An Overview GLUT GLSL GLEW

The OpenGL Shading Language

Shading Languages. Ari Silvennoinen Apri 12, 2004

X. GPU Programming. Jacobs University Visualization and Computer Graphics Lab : Advanced Graphics - Chapter X 1

OUTLINE. Learn the basic design of a graphics system Introduce pipeline architecture Examine software components for a graphics system

BCA611 Video Oyunları için 3B Grafik

Graphics Programming. Computer Graphics, VT 2016 Lecture 2, Chapter 2. Fredrik Nysjö Centre for Image analysis Uppsala University

Real - Time Rendering. Graphics pipeline. Michal Červeňanský Juraj Starinský

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

OUTLINE. Implementing Texturing What Can Go Wrong and How to Fix It Mipmapping Filtering Perspective Correction

CS452/552; EE465/505. Image Processing Frame Buffer Objects

CSE 167: Introduction to Computer Graphics Lecture #13: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2015

WebGL and GLSL Basics. CS559 Fall 2015 Lecture 10 October 6, 2015

Mobile Application Programing: Android. OpenGL Operation

Lecture 11 Shaders and WebGL. October 8, 2015

Models and Architectures. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Introduction to Computer Graphics. Hardware Acceleration Review

Programming with OpenGL Complete Programs Objectives Build a complete first program

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

Introductory Seminar

Shadows. Prof. George Wolberg Dept. of Computer Science City College of New York

OPENGL AND GLSL. Computer Graphics

Objectives. Coupling GLSL to Applications Example applications

Programmable shader. Hanyang University

Copyright Khronos Group 2012 Page 1. Teaching GL. Dave Shreiner Director, Graphics and GPU Computing, ARM 1 December 2012

GLSL Overview: Creating a Program

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

0.Features of the OpenGL Shading Language

CS 418: Interactive Computer Graphics. Basic Shading in WebGL. Eric Shaffer

0.Features of the OpenGL Shading Language

CS452/552; EE465/505. Image Formation

Zeyang Li Carnegie Mellon University

Blis: Better Language for Image Stuff Project Proposal Programming Languages and Translators, Spring 2017

Introduction to the OpenGL Shading Language (GLSL)

We assume that you are familiar with the following:

The Projection Matrix

CPSC 436D Video Game Programming

Objectives. Programming with OpenGL Part 5: More GLSL. Program Object. Reading a Shader. Shader Reader. Linking Shaders with Application

Transcription:

Programming with OpenGL Shaders I Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Objectives Shader Programming Basics Simple Shaders Vertex shader Fragment shaders

Shaders What is a shader (and why is it called that?) How might you use a Vertex Shader? A Fragment Shader? 2

Vertex Shader Applications Per Vertex Computation Lighting Calculations More realistic models Cartoon shaders Moving/Transforming vertices Morphing Procedural Deformation Skinning

Fragment Shaders Per Fragment Computation Per fragment lighting calculations Blending, compositing per vertex lighting per fragment lighting

Fragment Shaders Texture mapping smooth shading environment mapping bump mapping 5

Shaders History First programmable shaders were programmed in an assembly-like manner( mid 90 s??) OpenGL extensions added for vertex and fragment shaders Cg (C for graphics) C-like language for programming shaders Work(ed) with both OpenGL and DirectX (2012 deprecated) OpenGL Shading Language (GLSL) As of OpenGL 3.1, application must provide shaders (vertex and fragment)

Data Types C types: float, double, int, uint, bool Vectors: float, double, int, uint bool [2, 3, 4] vec2, dvec2, ivec2, uvec2, bvec2 Matrices: float and double only [2x2 up to 4x4] mat2x2, dmat2x2 Stored by columns (column major) Standard referencing m[row][column] C++ style constructors vec3 a =vec3(1.0, 2.0, 3.0) vec2 b = vec2(a) 8

Execution Model: vertex portion Vertex data Shader Program GPU! Memory! GPU! init()! Application! Program! mainloop()! Vertex! Shader! Vertex Primitive! Assembly! gldrawarrays

Storage Qualifiers const: read-only as in C in : input to a shader stage from a previous stage from the application as vertex attributes out: outputs from a shader stage e.g. final fragment color from fragment shader uniform: value will be specified by application before shader executes AND does not change across the primitive shared between all stages must be declared as globals e.g. color for an entire primitive such as a triangle 1 0

IN and OUT Qualified IN: User defined (in application, earlier shader stage) Use in qualifier to get to shader in float temperature in vec3 velocity OUT: User defined (in shader) Use out qualifier to get out of a shader out vec3 color

Simple Vertex Shader (pass through) in vec4 vposition; void main(void) { } gl_position = vposition;

Built-In Variables The OpenGL Shading Language defines a number of special variables for the various shader stages. These predefined variables (or built- in variables) have special properies. They are usually for communicaing with certain fixed- funcionality. By convenion, all predefined variables start with "gl_"; no user- defined variables may start with this. hop://www.opengl.org/wiki/built- in_variable_%28glsl%29 13

Execution Model: fragment portion Application! Program! Shader Program GPU Rasterizer! Fragment Fragment! Shader! Fragment Color Frame Buffer! 1 4

Simple Fragment Shader out vec4 fragcolor; void main(void) { } fragcolor = vec4(1.0, 0.0, 0.0, 1.0); 1 5

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 a shader e.g. primitive bounding box e.g. constant colors without interpolation 1 6

Uniform Qualified Example Gluint facecolorparam; facecolorparam = glgetuniformlocation(myprogobj, facecolor"); variable /* facecolor defined in shader */ GLFLoat facecolor[3]; /* facecolor set in application */ facecolor[0]= 1.0 facecolor[1] = facecolor[2] = 0.0; /* link app var to shader var */ gluniform3fv(facecolorparam, facecolor); /* 1D float*/ 1 7

18

Interpolating Variables Variables that are passed from vertex shader to fragment shader OUT variables in Vertex Shader IN variables in Fragment Shader Automatically interpolated over the primitive by the rasterizer http://www.geometrian.com/programming/tutorials/ graphicspipeline/opengl_4_rasterization.php! 1 9

Passing values in functions Can have multiple functions in a shader Call by value-return IN OUT for function arguments copied into a function values copied out into the calling arguments undefined upon entrance INOUT value copied into from arguments and out of the function back to the arguments 2 2

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 2 3

Swizzling and Selection Can refer to array elements by element using [] or selection (.) operator with x, y, z, w [position elements] r, g, b, a [color elements] s, t, p, q [texture elements] 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); hop://en.wikipedia.org/wiki/pointer_swizzling 2 4

And more CondiIonal Flow control Loops Structs...etc. 25