CPSC 424 Computer Graphics Fall 2025

CPSC 424 Lab 5: Lighting

Due: Tue 10/7 at the start of lab

Lab 4 focused on incorporating shading (applying a lighting model to determine pixel colors) into the graphics pipeline. This lab builds on that foundation and shifts the focus more to lighting in the scene — multiple lights, different types of lights, and placing lights in the scene.

Successful completion of this lab means that you:

Collaboration and Use of AI

This is an individual lab. You may get technical help from others, but the effort and ideas that go into producing solutions should be yours.

You may use AI as outlined in the Use of AI policy — Copilot's inline coding suggestions, explain, fix, review and comment features but not code generation from English prompts. Also:

Handin

Hand in your work by copying your ~/cs424/lab5 folder into your handin folder (/classes/cs424/handin/username, where username is your username).

Check that the result is that your files are contained in /classes/cs424/handin/username/lab5 — if not, fix it!


Preliminaries

Setup

Make sure that your lab5 directory is named exactly like that and is at the same level in your workspace directory. This is important so that the relative path names used to access common files remain the same so your program doesn't break when you hand it in.

Reference

Your best reference sources are the slides from class (which pull out and organize the key points), the examples from class (which put all the pieces together), and the textbook.


Exercises

Toggling Lights

The provided lab5.html file contains the same diffuse+specular per-vertex shading as the provided lighting-demo.html from lab 4 with one addition — an enabled flag for the light so the light can be turned on and off.

[Optional] Phong Shading

You are encouraged to replace the provided shaders with your Phong shaders from lab 4, but if you don't have those working or would rather not use them for some other reason, it is fine to continue with the provided shaders. Note that you will need to add support for the enabled flag to your shaders.

Structs

To manage the many shader parameters needed for materials and for lights, it is convenient to bundle related properties into structs.

Edit lab5.html for this exercise.

When you are done, the program should work just like it did before you started this part.

Multiple Lights

To support multiple lights, we change the single light parameter to an array of lights in the shader.

Continue editing lab5.html for this part.

You should now be able to toggle the checkboxes for all of the lights. Look at the getLights function to see how the lights are configured. Leave the first four as they are, but feel free to set up your own lights for the others (see the TODO: configure the remaining lights as desired comment).

Spotlights and Attenuation

We have so far only been working with point lights and directional lights, and also haven't captured the notion of attenuation. Spotlights and attenuation were discussed in class — they add additional properties (a0, a1, and a2 for attenuation; direction, cutoff angle, and an exponent capturing the falloff of light intensity away from the spotlight's direction for spotlights) and are handled by multipliers on the diffuse and specular terms in the lighting equation.

Technical note:

Continue editing lab5.html for this part.

[Optional] Global Ambient

The provided lighting equation in lab5.html has no ambient term, and the lighting equation from lab 4 includes an ambient contribution from each light in the scene. Optionally add a global ambient light type — these light(s) would contribute only ambient terms to the equation, not (also) diffuse and specular.

WC, EC, OC Lights

The provided scene.html contains a scene with several objects, including a rotating teapot. It is similar to lab5.html in terms of the lighting model and shaders — the difference in the program structure is the addition of animation.

The user interface provides a radio button to select the lighting in the scene. In the provided code, only the default option (viewer light) does anything.

Edit scene.html for this exercise.