# CMSC 435/634: Introduction to Computer Graphics

## Assignment 5 Shading Due April 25, 2014 (Late submission April 27, 2014)

### The Assignment

For this assignment, modify any of the terrain programs from this class: the original terrain, or your solution to either assignment 3 or 4. Create an entirely procedural GLSL fragment shader (no texture accesses) to draw three different types of ground cover. These can be grass, sand, moss, pebbles, snow, etc. Your shader should control the surface normals and gloss as well as the basic surface color. Also, your shader should only use one surface type for any pixel, but should procedurally decide where to place each type of ground cover, based on elevation, noise, or other parameters you see fit.

The following code implements a simple Perlin-like noise function:

// hash from 2D coordinate to integer (using two rounds of tiny encryption algorithm with key=0)
int hash(ivec2 v) {
v.x += (v.y<<4)^(v.y-0x61C88647)^(v.y>>5);
v.y += (v.x<<4)^(v.x-0x61C88647)^(v.x>>5);
v.x += (v.y<<4)^(v.y+0x3C6EF372)^(v.y>>5);
v.y += (v.x<<4)^(v.x+0x3C6EF372)^(v.x>>5);
return v.y;
}

// "Modified noise" (from Graphics Hardware 2005)
float noise(vec2 p) {
vec2 i = floor(p);          // integer part of point coordinate
vec2 f = p - i;             // fractional part of coordinate
vec2 b = (3.-2.*f)*f*f;     // smooth blend factors

// hash values at four corners
ivec4 h = ivec4(hash(ivec2(i.x   , i.y   )),
hash(ivec2(i.x   , i.y+1.)),
hash(ivec2(i.x+1., i.y   )),
hash(ivec2(i.x+1., i.y+1.)));

// random gradients at four corners determined by bits of hash
vec4 g = (f.x-vec4(0,0,1,1)) * vec4((h & 1)*2 - 1) +
(f.y-vec4(0,1,0,1)) * vec4((h & 2) - 1);

// blend together contribution from each corner
return mix(mix(g.x,g.z, b.x), mix(g.y,g.w, b.x), b.y);
}

The following sites may also prove useful:

• Steve May's RMan Notes: notes on RenderMan shader creation, though the strategies employed will work just as well in GLSL
• The GLSL Language Specification: Everything about GLSL, especially the list of built-in functions.
• ShaderToy: More inspiration than resource: a huge collection of WebGL fragment shaders, all running on just a single screen-filling rectangle.

When copying files from one CVS directory to another, be careful not to copy the directory named CVS or its contents. This directory contains the information to connect your files to their location and version in the repository, so if you copy it over, you can end up checking into the previous project's directory in the repository. You can always check out a fresh copy to be sure.

### 634 only

The blend between each material type should transition organically, for example with moss appearing in the cracks between rocks before it entirely takes over. You can do this by using a combination of noise and a computed transition map in computing your selection function. I the rock to moss example, the rock transition function might be between the rocks and high on the rock surfaces so the crack regions transition first.

### Extra credit

For up to 25 points of extra credit, make your shader transition with time: grass growing in dirt, snow covering then melting, etc. To do the extra credit, you will need to pass the current time from GLFW into the shader, which will require following the example of other shader parameters in the sample application to add and update a new one each frame.

Extra credit is only available if you submit by the original deadline or use your free late. If you submit late with the late penalty, you will not be eligible for any extra credit points. Also, you will only be considered for extra credit if you mention your extra credit work in your readme.

### Strategy

Think first (as always). You can create and call functions in GLSL, which will probably make a more sensable program organization than one huge main function. Tackle one surface type at a time, building up color, then normals, then gloss. After you build the basic shaders of each type, then worry about selecting between them.

Also, keep in mind the shader debugging strategies demonstrated in class. Especially the use of shader reloading without restarting the program, and the mapping of values to color to see how they change across the surface.

### What to turn in

Turn in this assignment electronically by checking your source code into your assn5 CVS directory by 11:59 PM on the day of the deadline. Do your development in the assn5 directory so we can find it. As always, double check that you have submitted everything we need to build and run your submission, but not any generated files. In particular, no executables, or any of the other files xcode or visual studio generate beyond the ones already checked in.

Be sure to include the details of the system you used for the assignment in your README in case we have problems.