Lab 5: Lighting & Shaders
CS 445: Computer Graphics, Fall 2014


Due Dates: 5pm, Tuesday, Nov 4

Goals

Resources

Questions

Go over the material discussed in class and read the above references. You should be able to answer the following questions.

Part 1: Lighting and Shaders

The Base Color

Adding Lights and Phong Lighting

You are not to turn in anything just yet. You will apply what you learned in this section to part 2.

Part 2: Fractal Landscape

Before beginning, make sure you make a copy of your program in part 1. You will use it as a starting point for the next lab (Lab 6: Textures).

Overview: This part of the lab will take some work so please get started early! A summary of your tasks is listed here:

A Few More Details:

Download the zipped file containing the files FractalLandHeights.cpp and FractalLandHeights.h, which will generate a height map, i.e. a 2-dimensional grid of values that represent heights (the y-value) on a square grid (x and z). Read the comments at the top of FractalLandHeights.cpp to see what it does and how the heights are stored.

Create a new class e.g. called FractalLandShape (along the lines of the Cube or Disk) which takes the heights and generates a list of triangles (vertices) and normals. Think about how to calculate the normals efficiently. Note, the normal at a vertex needs to be the average of the normals of the surrounding triangles. Think carefully how best to do this. Work it out first with pencil and paper. We will also discuss this in class.

The colors should be stored in your FractalLandShape class as a colors array (the Color buffer). To do this, add a getColor function which calcuates a color based on an elevation (y-value). It can be called to compute the color for a given vertex. Later, you will implement the color in the shader.

In main.cpp, create a new FractalLandShape object and render it. Include a light source which animates in a circle (centered at the center of the landscape) and at a fixed height. The position should be such that it clearly lights up the landscape from above at different angles. Include a keypress toggle so that you can stop/start the movement of the light. Include a cube that sits at the light location so you know exactly where the light is. Moving the light will help you test whether your normals and light location are set properly.

Once you are confident everything has been done correctly. Modify your vertex and fragment shaders so that the color is calculated in the shaders based on the vertex's y-value. GLSL has some nice smoothing functions which you can use. The y-value needs to be extracted in the vertex shader and passed to the fragment shader. In the fragment shader you need to compute the color based on this y-value (much the way you did it in the FractalLandShape class.

Compare the coloring that results when using the color buffer versus the shader. Which is better and why?

Demonstrate your program in lab no later than 5pm on Tuesday, Nov 4. It should show your fractal landscape with the animated light source that can toggle to stop/start. The landscape should have a color ramp (e.g. blue for water, white for snow peaks, etc) implemented in the shaders. Zip together your codeblocks project (please delete the bin and obj folders) and submit via WISE.