Objectives. Open GL Shading Language (GLSL)

Similar documents
3D Game Programming Programmable Pipelines. Ming-Te Chi Department of Computer Science, National Chengchi University

Supplement to Lecture 22

GLSL II. Objectives. Coupling GLSL to Applications Example applications

Objectives. Coupling GLSL to Applications Example applications

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

Programming with OpenGL Part 5: More GLSL. Ed Angel Professor 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

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

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

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

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

Computer Graphics (CS 4731) & 2D Graphics Systems

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

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

Programmable Graphics Hardware

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

12.2 Programmable Graphics Hardware

Shadows & Programmable Shaders. Dr. Alexander G. Gee. Texture Mapping. Buffers Texture Mapping OpenGL Texture Mapping Compositing and Blending

Lecture 3. Programming with OpenGL GLUT + GLEW. CS 354 Computer Graphics Sunday, January 20, 13

An Introduction to 3D Computer Graphics, Stereoscopic Image, and Animation in OpenGL and C/C++ Fore June

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

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

2D graphics with WebGL

Reading Material. Vertex- and Fragment Shaders. Pipeline. Vertex Shader. Primitive Assembly. Input data can be. Produces

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

- Surface Rendering -

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

OpenGL: What s Coming Down the Graphics Pipeline. Dave Shreiner ARM

Lecture 09: Shaders (Part 1)

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

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

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

OpenGL: What s Coming Down the Graphics Pipeline

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

Programming shaders & GPUs Christian Miller CS Fall 2011

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

CS 432 Interactive Computer Graphics

Introduction to the OpenGL Shading Language (GLSL)

Programmable shader. Hanyang University

The Basic Computer Graphics Pipeline, OpenGL-style. Introduction to the OpenGL Shading Language (GLSL)

Computer Graphics (CS 543) Lecture 8c: Environment Mapping (Reflections and Refractions)

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

Waves, Displacement, Reflections. Arun Rao CS 594

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

CSE 4431/ M Advanced Topics in 3D Computer Graphics. TA: Margarita Vinnikov

Programmable Graphics Hardware

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

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

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

CSE 167: Lecture #8: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

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

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

Intro to GPU Programming (OpenGL Shading Language) Cliff Lindsay Ph.D. Student CS WPI

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

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

CMPS160 Shader-based OpenGL Programming. All slides originally from Prabath Gunawardane, et al. unless noted otherwise

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

GPU Programming EE Final Examination

Vertex & Fragment shaders

Today. Rendering - III. Outline. Texturing: The 10,000m View. Texture Coordinates. Specifying Texture Coordinates in GL

Sign up for crits! Announcments

Tuesday, 23 March OpenGL Shading Language

Shading Language Update

GLSL. OpenGL Shading Language. OpenGL 1.5 Logical Diagram. OpenGL 2.0 Logical Diagram

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

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

Objectives. OpenGL - Shaders. GLSL A Quick Review

Zeyang Li Carnegie Mellon University

Shaders. Slide credit to Prof. Zwicker

GLSL Language Details

Design Focus. GLSL Language Details. Additions from C++ Additions for Graphics

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

1 of 5 9/10/ :11 PM

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

Technical Game Development II. Reference: Rost, OpenGL Shading Language, 2nd Ed., AW, 2006 The Orange Book Also take CS 4731 Computer Graphics

Lab 9 - Metal and Glass

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

0.Features of the OpenGL Shading Language

0.Features of the OpenGL Shading Language

GLSL v1.20. Scott MacHaffie Schrödinger, Inc.

Levy: Constraint Texture Mapping, SIGGRAPH, CS 148, Summer 2012 Introduction to Computer Graphics and Imaging Justin Solomon

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

OpenGL Programmable Shaders

Programming Graphics Hardware

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

Computer Graphics with OpenGL ES (J. Han) Chapter 6 Fragment shader

Technical Game Development II. Reference: Rost, OpenGL Shading Language, 2nd Ed., AW, 2006 The Orange Book Also take CS 4731 Computer Graphics

CMPS160 Shader-based OpenGL Programming

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

Introduction to the OpenGL Shading Language

Introduction to Shaders.

Technical Game Development II. Reference: Rost, OpenGL Shading Language, 2nd Ed., AW, 2006 The Orange Book IMGD 4000 (D 10) 1

CSE 4431/ M Advanced Topics in 3D Computer Graphics. TA: Margarita Vinnikov

CS GPU and GPGPU Programming Lecture 3: GPU Architecture 2. Markus Hadwiger, KAUST

1. Write a shader program that animates the morphing of Figure 'A' to Figure 'B' and back.

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

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

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

CS GPU and GPGPU Programming Lecture 3: GPU Architecture 2. Markus Hadwiger, KAUST

Curvature based rendering

Transcription:

Open GL Shading Language (GLSL) Objectives Shader applications Vertex shaders Fragment shaders Programming shaders Cg GLSL Coupling GLSL to Applications Example applications 1

Vertex Shader Applications Moving vertices Morphing Wave motion Fractals Lighting More realistic models Cartoon shaders 2

Fragment Shader Applications Per fragment lighting calculations per vertex lighting per fragment lighting 3

Fragment Shader Applications Texture mapping smooth shading environment mapping bump mapping 4

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) 5

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 6

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; } 7

Execution Model 8

Simple Fragment Program void main(void) { gl_fragcolor = gl_color; } 9

Execution Model 10

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) 11

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) 12

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 13

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 14

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 15

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 16

Required Fragment Shader varying vec3 color_out; void main(void) { gl_fragcolor = color_out; } 17

Passing values call by value-return Variables are copied in Returned values are copied back Three possibilities in out inout 18

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 19

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); 20

Linking Shaders to OpenGL OpenGL Extensions ARB_shader_objects ARB_vertex_shader ARB_fragment_shader OpenGL 2.0 Almost identical to using extensions Avoids extension suffixes on function names 21

Program Object Container for shaders Can contain multiple shaders Other GLSL functions GLuint myprogobj; myprogobj = glcreateprogram(); /* define shader objects here */ gluseprogram(myprogobj); gllinkprogram(myprogobj); 22

Reading a Shader Usual method of passing a shader is as a null-terminated string using the function glshadersource Shader are added to the program object and compiled If the shader is in a file, we can write a reader to convert the file to a string 23

Shader Reader #include <stdio.h> char* readshadersource(const char* shaderfile) { FILE* fp = fopen(shaderfile, "r"); char* buf; struct stat *statbuf; long size; if(fp == NULL) return(null); stat(shaderfile,&statbuf); buf = (char*) malloc(statbuf.st_size + 1 * sizeof(char)); fread(buf, 1, statbuf.st_size, fp); buf[size] = '\0'; */null termination*/ fclose(fp); return buf; } 24

Adding a Vertex Shader GLint vshader; GLunit myvertexobj; GLchar vshaderfile[] = my_vertex_shader ; GLchar* vsource = readshadersource(vshaderfile); glshadersource(myvertexobj, 1, vsource, NULL); myvertexobj = glcreateshader(gl_vertex_shader); glcompileshader(myvertexobj); glattachobject(myprogobj, myvertexobj); 25

Vertex Attributes Vertex attributes are named in the shaders Linker forms a table Application can get index from table and tie it to an application variable Similar process for uniform variables 26

Vertex Attribute Example GLint colorattr; colorattr = glgetattriblocation(myprogobj, "mycolor"); /* mycolor is name in shader */ GLfloat color[4]; glvertexattrib4fv(colorattrib, color); /* color is variable in application */ 27

Uniform Variable Example GLint angleparam; angleparam = glgetuniformlocation(myprogobj, "angle"); /* angle defined in shader */ /* my_angle set in application */ GLfloat my_angle; my_angle = 5.0 /* or some other value */ gluniform1f(angleparam, my_angle); 28

Wave Motion Vertex Shader uniform float time; uniform float xs, zs, // frequencies uniform float h; // height scale void main() { vec4 t =gl_vertex; t.y = gl_vertex.y + h*sin(time + xs*gl_vertex.x) + h*sin(time + zs*gl_vertex.z); gl_position = gl_modelviewprojectionmatrix*t; } 29

Particle System uniform vec3 init_vel; uniform float g, m, t; void main() { vec3 object_pos; object_pos.x = gl_vertex.x + vel.x*t; object_pos.y = gl_vertex.y + vel.y*t + g/(2.0*m)*t*t; object_pos.z = gl_vertex.z + vel.z*t; gl_position = gl_modelviewprojectionmatrix* vec4(object_pos,1); } 30

Modified Phong Vertex Shader I void main(void) /* modified Phong vertex shader (without distance term) */ { gl_position = gl_modelviewprojectionmatrix * gl_vertex; vec4 ambient, diffuse, specular; vec4 eyeposition = gl_modelviewmatrix * gl_vertex; vec4 eyelightpos = gl_lightsource[0].position; vec3 N = normalize(gl_normalmatrix * gl_normal); vec3 L = normalize(eyelightpos.xyz eyeposition.xyz); vec3 E = normalize(eyeposition.xyz); vec3 H = normalize(l + E); 31

Modified Phong Vertex Shader II /* compute diffuse, ambient, and specular contributions */ float Kd = max(dot(l, N), 0.0); float Ks = pow(max(dot(n, H), 0.0), gl_frontmaterial.shininess); float Ka = 0.0; ambient = Ka*gl_FrontLightProduct[0].ambient; diffuse = Kd*gl_FrontLightProduct[0].diffuse; specular = Ks*gl_FrontLightProduct[0].specular; } gl_frontcolor = ambient+diffuse+specular; 32

Modified Phong Vertex Shader II /* compute diffuse, ambient, and specular contributions */ float Kd = max(dot(l, N), 0.0); float Ks = pow(max(dot(n, H), 0.0), gl_frontmaterial.shininess); float Ka = 0.0; ambient = Ka*gl_FrontLightProduct[0].ambient; diffuse = Kd*gl_FrontLightProduct[0].diffuse; specular = Ks*gl_FrontLightProduct[0].specular; } gl_frontcolor = ambient+diffuse+specular; 33

Pass Through Fragment Shader /* pass-through fragment shader */ void main(void) { gl_fragcolor = gl_color; } 34

Vertex Shader for per Fragment Lighting varying vec3 N, L, E, H; void main() { gl_position = gl_modelviewprojectionmatrix * gl_vertex; vec4 eyeposition = gl_modelviewmatrix * gl_vertex; vec4 eyelightpos = gl_lightsource[0].position; N = normalize(gl_normalmatrix * gl_normal); L = normalize(eyelightpos.xyz eyeposition.xyz); E = normalize(eyeposition.xyz); H = normalize(l + E); } 35

Fragment Shader for Modified Phong Lighting I varying vec3 N; varying vec3 L; varying vec3 E; varying vec3 H; void main() { vec3 Normal = normalize(n); vec3 Light = normalize(l); vec3 Eye = normalize(e); vec3 Half = normalize(h); 36

Fragment Shader for Modified Phong Lighting II } float Kd = max(dot(normal, Light), 0.0); float Ks = pow(max(dot(half, Normal), 0.0), gl_frontmaterial.shininess); float Ka = 0.0; vec4 diffuse = Kd * gl_frontlightproduct[0].diffuse; vec4 specular = Ks * gl_frontlightproduct[0].specular; vec4 ambient = Ka * gl_frontlightproduct[0].ambient; gl_fragcolor = ambient + diffuse + specular; 37

Vertex vs Fragment Lighting per vertex lighting per fragment lighting 38

Samplers Provides access to a texture object Defined for 1, 2, and 3 dimensional textures and for cube maps In shader: uniform sampler2d mytexture; Vec2 texcoord; Vec4 texcolor = texture2d(mytexture, texcoord); In application: texmaplocation = glgetuniformlocation(myprog, mytexture ); gluniform1i(texmaplocation, 0); /* assigns to texture unit 0 */ 39

Fragment Shader Applications Texture mapping smooth shading environment mapping bump mapping 40

Cube Maps We can form a cube map texture by defining six 2D texture maps that correspond to the sides of a box Supported by OpenGL Also supported in GLSL through cubemap sampler vec4 texcolor = texturecube(mycube, texcoord); Texture coordinates must be 3D 41

Environment Map Use reflection vector to locate texture in cube map. See Cube Maps 42

Environment Maps with Shaders Environment map usually computed in world coordinates which can differ from object coordinates because of modeling matrix May have to keep track of modeling matrix and pass it shader as a uniform variable Can also use reflection map or refraction map (for example to simulate water) 43

Reflection Map Vertex Shader varying vec3 R; void main(void) { gl_position = gl_modelviewprojectionmatrix* gl_vertex; vec3 N = normalize(gl_normalmatrix*gl_normal); vec4 eyepos = gl_modelviewmatrix*gl_vertex; R = reflect( eyepos.xyz, N); } 44

Refelction Map Fragment Shader varying vec3 R; uniform samplercube texmap; void main(void) { gl_fragcolor = texturecube(texmap, R); } 45

Bump Mapping Perturb normal for each fragment Store perturbation as textures 46

Normalization Maps Cube maps can be viewed as lookup tables 1-4 dimensional variables Vector from origin is pointer into table Example: store normalized value of vector in the map Same for all points on that vector Use normalization map instead of normalization function Lookup replaces sqrt, mults and adds 47