Notes for February 2 class -- Introduction to Shaders

3D Coordinate system

WebGL exists in a 3D world:

  • x goes to the right
  • y goes up
  • z goes forward (out of the screen)
This is called a right hand coordinate system.

For now we are going to be doing all of our work starting with the x,y plane. Specifically we will be working with a square that extends from -1 → +1 in both x and y.

 

Square as a triangle strip

Our geometry will be a square.

In WebGL everything is made of triangles, so we will need two triangles.

We define these as a triangle strip.

In a triangle strip, every three successive vertices makes a new triangle, so we will need to specify a total of four vertices, as in the figure to the right.
 

Z-buffer algorithm

The GPU (Graphics Processing Unit) renders using a "Z-buffer algorithm"

For each animation frame, this algorithm consists of two successive steps:

  1. For each vertex:
    The GPU runs a vertex shader to:

    • Find which pixel of the image contains this vertex;
    • Set up "varying" values to be interpolated.

  2. For each triangle:
    The GPU interpolates from vertices to pixels.
    For each pixel:
    The GPU runs a fragment shader to:

    1. Compute color;

    2. If this is the nearest fragment at the pixel, replace color and depth at this pixel in the image.
 

Vector3 object

One data structure that will be very useful is a vector of length 3, which we can use to represent x,y,z coordinates, as seen in the figure to the right.

In Javascript we can define this object via a constructor, which contains all of its properties that can change per instance, as well as a prototype, which contains properties that do not change from one instance to another (such as functions to do such operations as setting values).

 

The Vector3 object will grow in capability as the semester progresses, but to the right is a starter version.

Note that the x,y and z properties, which change from instance to intance, are defined in the constructor itself.

The set property, which will be the same function for all instances, is defined in the prototype.

 
function Vector3() {
   this.x = 0;
   this.y = 0;
   this.z = 0;
}
Vector3.prototype = {
   set : function(x,y,z) {
      this.x = x;
      this.y = y;
      this.z = z;
   },
}

Uniform variables

GLSL (for "GL Shading Language") is the C-like language that is used for shaders on the GPU. One of its key constructs is a uniform variable.

Uniform variables on the GPU have the same value at every pixel. They can (and often do) change over time.

By convention, a uniform variable name starts with the letter 'u'.

For your assignment, I have create some useful uniform variables:

   float uTime;    // time elapsed, in seconds

   vec3  uCursor;  // mouse position and state
                   //    uCursor.x goes from -1 to +1
                   //    uCursor.y goes from -1 to +1
                   //    uCursor.z is 1 when mouse down, 0 when mouse up.
 

Vertex shaders

A vertex shader is a program that you (the application programmer/artist) writes which gets run on the GPU at every vertex. It is written in the special purpose language GLSL.

To the right is a very simple vertex shader program. An "attribute" is a constant value that is passed in from the CPU. In this case, it is aPosition, the x,y,z position of each vertex in the scene. wIt is of type vec3, which means that it consists of three GLSL floating point numbers.

 


   attribute vec3 aPosition;
   varying   vec3 vPosition;
   void main() {
      gl_Position = vec4(aPosition, 1.0);
      vPosition = aPosition;
   }

One of the most powerful things that a vertex shader can do is set "varying" variables. These values are then interpolated by the GPU across the pixels of any triangles that use this vertex. That interpolated value will then be available to fragment shaders at each pixel.

For example, "varying" variable vPosition is set by this vertex shader. By convention, the names of varying variables start with the letter 'v'.

This vertex shader does two things:

  • By setting gl_Position, it determines at which pixel of the image this vertex will appear.

  • It sets varying variable vPosition to equal the attribute position aPosition for this vertex.

Fragment shaders

A fragment shader is a program that you (the application programmer) writes which is run at every pixel.

Because pieces of different triangles can be visible at each pixel (eg: when triangles are very small, or pixels where an edge of one triangle partly obscures another triangle), in general we are really defining the colors of fragments of pixels, which is why these are called fragment shaders.

Since our vertex shader has set a value for vPosition, any fragment shader we write will be able to make use of this variable, whose values will now have been interpolated down to the pixel level.

 

Guides to WebGL

There are many guides on-line to WebGL. I find this Tutorial on WebGL Fundamentals to be particularly clear and easy to follow for people who are new to WebGL.

I also find this Quick Reference Card to be very useful when writing WebGL shaders. I think you will find the list of built-in functions on the last page to be particularly useful.

 

Assignment 1

Your assignment for this week is to start with the code in this zip file, which we went over in class, and replace the fragment shader in index.html with your own fascinating and wonderful and original fragment shader.

Important: Create something of your own. Don't just make a minor variation of what I did in class. Have fun with it.

Your homework is due before the start of next week's class.