webgl and glsl basics · webgl and glsl basics cs559 – fall 2016 lecture 14 october 24 2016 ....

82
WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016

Upload: others

Post on 09-Oct-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

WebGL and GLSL Basics

CS559 – Fall 2016 Lecture 14

October 24 2016

Page 2: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Review …

Page 3: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Hardware Rasterization

For each point: Compute barycentric coords Decide if in or out

.7,  .7,  -­‐.4   1.1,  0,  -­‐.1  

.9,.05,.05  

.33,.33,.33  

Page 4: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

1988: The Personal Iris

Page 5: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

A Pipeline

transf   light   project   raster   shade   write  1  

Page 6: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

The pipeline (1988) Applica>on  Program  

Graphics    Driver  

Vertex  Processing  

(TCL)  

Command  Buffer  (Triangle  Queue)  

Vertex  

Que

ue  

Vertex  

Cache  

Assembly  

Triangle  Processing   Rasterize   Pixel  

Processing  Pixel  

Que

ue  

Pixel  Tests  

Frame  Buffer  

Texture  Memory   Render  to  texture  

Vertex  shading  

Geometry  Shading  

Pixel  Shading  

Page 7: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

The pipeline (2006-current) Applica>on  Program  

Graphics    Driver  

Vertex  Processing  

(TCL)  

Command  Buffer  (Triangle  Queue)  

Vertex  

Que

ue  

Vertex  

Cache  

Assembly  

Triangle  Processing   Rasterize   Pixel  

Processing  Pixel  

Que

ue  

Pixel  Tests  

Frame  Buffer  

Texture  Memory   Render  to  texture  

Vertex  shading  

Geometry  Shading  

Pixel  Shading  

Page 8: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

The parts you have to program 1988-2014 Now (in addition to above)

Applica>on  Program  

Graphics    Driver  

Vertex  Processing  

(TCL)  

Command  Buffer  (Triangle  Queue)  

Vertex  

Que

ue  

Vertex  

Cache  

Assembly  

Triangle  Processing   Rasterize   Pixel  

Processing  Pixel  

Que

ue  

Pixel  Tests  

Frame  Buffer  

Texture  Memory   Render  to  texture  

Vertex  shading  

Geometry  Shading  

Pixel  Shading  

Page 9: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

A Triangle’s Journey

Page 10: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

A Program to Draw a Triangle

The complete WebGL thing we need Doing each necessary steps Just one triangle… http://jsbin.com/fowoku/edit

Page 11: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Just a Triangle

Page 12: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

HTML like you are used to

Page 13: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

A Lot of Code

Page 14: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Look at the process inside-out

We’ll start with the end of the pipeline And work backwards…

Page 15: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

The parts you have to program 1988-2014 Now (in addition to above)

Applica>on  Program  

Graphics    Driver  

Vertex  Processing  

(TCL)  

Command  Buffer  (Triangle  Queue)  

Vertex  

Que

ue  

Vertex  

Cache  

Assembly  

Triangle  Processing   Rasterize   Pixel  

Processing  Pixel  

Que

ue  

Pixel  Tests  

Frame  Buffer  

Texture  Memory   Render  to  texture  

Vertex  shading  

Geometry  Shading  

Pixel  Shading  

Page 16: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Applica>on  Program  

Graphics    Driver  

Vertex  Processing  

(TCL)  

Command  Buffer  (Triangle  Queue)  

Vertex  

Que

ue  

Vertex  

Cache  

Assembly  

Triangle  Processing   Rasterize   Pixel  

Processing  Pixel  

Que

ue  

Pixel  Tests  

Frame  Buffer  

Texture  Memory   Render  to  texture  

Vertex  shading  

Geometry  Shading  

Pixel  Shading  

I  warned  you:    This  step  will  get  to  be  preXy  exci>ng  

Page 17: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Applica>on  Program  

Graphics    Driver  

Vertex  Processing  

(TCL)  

Command  Buffer  (Triangle  Queue)  

Vertex  

Que

ue  

Vertex  

Cache  

Assembly  

Triangle  Processing   Rasterize   Pixel  

Processing  Pixel  

Que

ue  

Pixel  Tests  

Frame  Buffer  

Texture  Memory   Render  to  texture  

Vertex  shading  

Geometry  Shading  

Pixel  Shading  

We  have  to  program  this  step!    The  program  is  called  the:  

 Fragment  Shader  

Page 18: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Applica>on  Program  

Graphics    Driver  

Vertex  Processing  

(TCL)  

Command  Buffer  (Triangle  Queue)  

Vertex  

Que

ue  

Vertex  

Cache  

Assembly  

Triangle  Processing   Rasterize   Pixel  

Processing  Pixel  

Que

ue  

Pixel  Tests  

Frame  Buffer  

Texture  Memory   Render  to  texture  

Vertex  shading  

Geometry  Shading  

Pixel  Shading  

Pixel  in  à  Pixel  out,      each  independent    All  we  do  is  change  its  values    

Page 19: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Applica>on  Program  

Graphics    Driver  

Vertex  Processing  

(TCL)  

Command  Buffer  (Triangle  Queue)  

Vertex  

Que

ue  

Vertex  

Cache  

Assembly  

Triangle  Processing   Rasterize   Pixel  

Processing  Pixel  

Que

ue  

Pixel  Tests  

Frame  Buffer  

Texture  Memory   Render  to  texture  

Vertex  shading  

Geometry  Shading  

Pixel  Shading  

Coming  aXrac>ons…    This  step  will  get  to  be  preXy  exci>ng  

Page 20: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

The Pixel Shader

Given information about the pixel Compute color Optionally, compute other things

Page 21: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

How to program it

Use a Shading Language We’ll use a language called GLSL Compiler built into WebGL Language specifics as we go…

Page 22: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

A (boring) Fragment Shader

void main(void) {

gl_FragColor = vec4(0.0, 1.0, 1.0, 1.0); }

Page 23: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

A (boring) Fragment Shader

void main(void) {

gl_FragColor = vec4(0.0, 1.0, 1.0, 1.0); }

Shaders  define  a  main  func>on  that  take  no  arguments  return  no  values    

Page 24: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

A (boring) Fragment Shader

void main(void) {

gl_FragColor = vec4(0.0, 1.0, 1.0, 1.0); }

GLSL  Shaders  operate  by  side  effects  on  special  variables    (they  look  like  globals)  

Page 25: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

A (boring) Fragment Shader

void main(void) {

gl_FragColor = vec4(0.0, 1.0, 1.0, 1.0); }

GLSL  has  types  useful  in  graphics  Like  4  vectors  

This  is  opaque  yellow  (even  colors  are  4-­‐vectors)  

Page 26: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Applica>on  Program  

Graphics    Driver  

Vertex  Processing  

(TCL)  

Command  Buffer  (Triangle  Queue)  

Vertex  

Que

ue  

Vertex  

Cache  

Assembly  

Triangle  Processing   Rasterize   Pixel  

Processing  Pixel  

Que

ue  

Pixel  Tests  

Frame  Buffer  

Texture  Memory   Render  to  texture  

Vertex  shading  

Geometry  Shading  

Pixel  Shading  

Vertex  Processing    We  have  to  program  this  part  too  Also  in  GLSL  

Page 27: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Applica>on  Program  

Graphics    Driver  

Vertex  Processing  

(TCL)  

Command  Buffer  (Triangle  Queue)  

Vertex  

Que

ue  

Vertex  

Cache  

Assembly  

Triangle  Processing   Rasterize   Pixel  

Processing  Pixel  

Que

ue  

Pixel  Tests  

Frame  Buffer  

Texture  Memory   Render  to  texture  

Vertex  shading  

Geometry  Shading  

Pixel  Shading  

Process  each  vertex  independently    Transform  –  compute  x’  and  n’  Clip  Light  –  compute  c’    

Page 28: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

What data about vertices?

Inputs: Position Other Stuff

Outputs:

Position Other Stuff

Vertex  A/ributes  from  applica>on  

varying  proper>es  to  framgment  Shaders  (remember  interpola>on)  

Page 29: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

The simplest vertex shader

attribute vec3 pos; void main(void) { gl_Position = vec4(pos, 1.0); } }

Page 30: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

The simplest vertex shader

attribute vec3 pos; void main(void) { gl_Position = vec4(pos, 1.0); } }

Shaders  define  a  main  func>on  that  take  no  arguments  return  no  values    

Page 31: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

The simplest vertex shader

attribute vec3 pos; void main(void) { gl_Position = vec4(pos, 1.0); } }

Shaders  output  by  side  effects:  seang  special  variables    

Page 32: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

The simplest vertex shader

attribute vec3 pos; void main(void) { gl_Position = vec4(pos, 1.0); } }

Shaders  get  input  by  reading  special  variables    

Page 33: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Special Variables

Built in (magic) gl_Position – output of vertex shader gl_FragColor – output of frag shader

User Defined

attributes – inputs to vertex shader varying – output from vertex to fragment uniform – “constant” over triangle group

Page 34: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

The simplest vertex shader

attribute vec3 pos; void main(void) { gl_Position = vec4(pos, 1.0); } }

We  are  defining  our  own  special  variable  

Page 35: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

The simplest vertex shader

attribute vec3 pos; void main(void) { gl_Position = vec4(pos, 1.0); } }

Cool  GLSL  feature:  type  conversions  

Page 36: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

No Transformation?

I will assume the position is already in the right coordinate system. The rasterizer (and everything else) works in Normalized Device Coordinates (NDC) -1 to 1 in each dimension

Page 37: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Applica>on  Program  

Graphics    Driver  

Vertex  Processing  

(TCL)  

Command  Buffer  (Triangle  Queue)  

Vertex  

Que

ue  

Vertex  

Cache  

Assembly  

Triangle  Processing   Rasterize   Pixel  

Processing  Pixel  

Que

ue  

Pixel  Tests  

Frame  Buffer  

Texture  Memory   Render  to  texture  

Vertex  shading  

Geometry  Shading  

Pixel  Shading  

Start  here  Setup  modes  (window,  …)  Setup  transform,  lights  Draw  a  triangle  Posi>on,  color,  normal  

Page 38: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

In JavaScript using WebGL…

Page 39: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

The beginning

Page 40: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

The beginning

This should look like HTML5 Canvas You can have multiple contexts (draw “canvas” over WebGL)

Page 41: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Now about those shaders…

Get them into strings Use a library to read them from resources

Page 42: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Run the compiler!

Page 43: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Error Checking

Here I checked for errors (since I often have syntax errors)

You should check for errors everywhere

Page 44: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Run the compiler again!

Need to compile both shaders

Page 45: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Link the shaders together…

Shaders always work in pairs Need to connect them

Page 46: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Setup the special variables

Important to communicate with shaders

Page 47: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

The simplest vertex shader

attribute vec3 pos; void main(void) { gl_Position = vec4(pos, 1.0); } }

Javascript  needs  to  connect  to  the  “pos”  variable  

Page 48: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Communicating an attribute

We give it an array of attributes Assign it to a position We have to ask which position

Page 49: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

OK, Now for our triangle

How do we get this data to the hardware? Need to do a block transfer

Page 50: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Applica>on  Program  

Graphics    Driver  

Vertex  Processing  

(TCL)  

Command  Buffer  (Triangle  Queue)  

Vertex  

Que

ue  

Vertex  

Cache  

Assembly  

Triangle  Processing   Rasterize   Pixel  

Processing  Pixel  

Que

ue  

Pixel  Tests  

Frame  Buffer  

Texture  Memory   Render  to  texture  

Vertex  shading  

Geometry  Shading  

Pixel  Shading  

Need  to  get  the  ver>ces  to  the  hardware  fast!    (normally  more  than  3)  

Page 51: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Key Idea: Buffer

Create a buffer buffer = a block of memory on the GPU

Copy the data into the buffer Must be a special JavaScript object:

Float32Array (array of fixed types)

Page 52: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Now to draw

First we have to clear the screen Notice that color is a 4-vector I don’t really need the z-buffer

Page 53: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Now we actually draw the triangle

Notice that we use the shaders and the buffer

Page 54: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

All that for a triangle!

Page 55: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Is it really 100 lines of code?

Not really – lots of comments Build wrappers to be more concise

you do the same thing over and over But there are lots of steps

and you should understand them

Page 56: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Two triangles…

Can you see where these triangles will go? (remember they are in NDC)

Page 57: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Change the array sizes

Page 58: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Two triangles

Page 59: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

How do we color them differently?

Page 60: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Color per vertex

Add an attribute for each vertex so we can pass a color for each

Have the vertex shader output the color

varying variable for fragment shader Have the fragment shader input the color

Page 61: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

A (boring) Fragment Shader

void main(void) {

gl_FragColor = vec4(0.0, 1.0, 1.0, 1.0); }

Page 62: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

A (less boring) Fragment Shader

precision highp float; varying vec3 outColor; void main(void) {

gl_FragColor = vec4(outColor, 1.0); }

Our  own  magic  variable!  

Page 63: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

A (less boring) Fragment Shader

precision highp float; varying vec3 outColor; void main(void) {

gl_FragColor = vec4(outColor, 1.0); }

Required  so  the  shaders  can  talk  

Page 64: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Connecting Shaders

varying variables connect shaders the output of a vertex shader becomes the input to a fragment shader The 3 vertices of a triangle are interpolated

Page 65: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

The simplest vertex shader

attribute vec3 pos; void main(void) { gl_Position = vec4(pos, 1.0); } }

Page 66: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

The (almost) simplest vertex shader

attribute vec3 pos; varying vec3 outColor; void main(void) { gl_Position = vec4(pos, 1.0); } outColor = vec3(1.0,0.0,1.0); }

Page 67: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Two purple triangles

hXp://jsbin.com/wecaci/edit?js,output  

Page 68: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Make color an input as well

attribute vec3 pos; attribute vec3 inColor; varying vec3 outColor; void main(void) { gl_Position = vec4(pos, 1.0); } outColor = inColor; }

Page 69: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Remember…

We can’t pass values directly to a fragment we don’t even know what they will be!

We pass attributes of vertices

which can then pass them to fragments

Page 70: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Now to connect to JavaScript…

Page 71: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Colors per vertex

Page 72: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Put them in a buffer

Page 73: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

When we draw, use 2 buffers

Page 74: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Two triangles…

hXp://jsbin.com/digupi/edit?js,output  

Page 75: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Apply a transformation

One transformation for the triangle group It is constant over the “drawArrays” call This is a uniform variable

hXp://jsbin.com/>rapu/19/edit?js,output  

Page 76: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Simplifying the Code

There is stuff you do over and over and … Write it once and use it often Or let someone else write it once… This is where twgl comes in

Page 77: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Compile two vertex programs

For each… run the compiler check for errors

Link them together Attach to the attributes Set up to specify the uniforms

Page 78: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Do it by hand… Do it with twgl var shaders = twgl.createProgramInfo(gl, ["vs", "fs"]);

Yes, one line… And it grabs the string from script tags so they are separate from your JS program. But the documentation is terrible.

Page 79: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

How about those shaders…

They do very specific things you need to understand the pipeline

They have 3 kinds of weird variables you need to understand the model

They are written in a cool language you’ll pick it up quickly

The language has a bunch of useful stuff look at the quick reference card

Page 80: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Learning Shader Programming

Connecting your program to shaders is hard So, don’t bother… (yet) Use a Shader IDE that lets you focus on shaders Gives you an object, a program, …

Page 81: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

Some things about GLSL

Very strongly typed float x = 1; // error! integer and float

Cool “sub-vector” access: vec3 v; v.xy (a 2-vector) vec4(v,1) (a 4-vector) vec4(v.xy, v.zx)

Page 82: WebGL and GLSL Basics · WebGL and GLSL Basics CS559 – Fall 2016 Lecture 14 October 24 2016 . Review … Hardware Rasterization For each point: Compute barycentric coords Decide

More cool stuff about GLSL

Lots of handy math functions They know it’s for graphics!

Limited control structures

parallel execution means all the same Conditional functions

step, softstep, …