Page 29: GLSL Exercises 2
Spring 2026 Sample Solution
Two more GLSL exercises before we move beyond GLSL.
Exercise 1 requires lighting. You can do something simple. If we want good lighting, we’ll use three.js. But notice - you will be putting two pieces together: you need to combine lighting and the procedural texture. This need to combine will be the key factor in other shading languages.
Exercises 1 and 2 require some lighting, and 3 recommends it. For the purposes of these exercises, you can assume that the lights are in “camera coordinates” (if that’s easier for you), or use any of the methods we discussed (such as assuming that transformation is a rotation).
If you do diffuse lighting, the lighting may change as the camera moves (if the lights move with the camera). It is sufficient to create a single directional light either in the camera direction (0,0,1 in view coordinates), or from above the camera (0,1,0 in view coordinates). It is sufficient just to use diffuse lighting (since it is simple); you may want to add some ambient to it so that the parts facing away from the light are not totally black, or add some specular lighting since you have the implementation of it.
It should be clear that there is lighting (for the examples that have lighting).
For this page, you should write the shader yourself - on the next page, you can adapt a shader from somewhere. You can reuse the lighting code from a prior page.
It is OK to use ideas that you find elsewhere. Remember: if you use ideas (or code) from some shader you find someplace else, be sure to give proper attribution. If you adapt a shader from somewhere else, make sure you understand how it works!
Exercise: Lighting and Procedural
Write a shader in 06-29-01.vs and 06-29-01.fs that creates a procedural texture (one of the ones from page 5 is fine - even one of the examples) and adds lighting. A single directional light source (either in the direction of the camera line of sight or from above the camera) is sufficient. You may not need to modify 06-29-01.js (06-29-01.html), but you can if you want to.
For advanced points, try to make the light be fixed in the world (and not move with the camera).
view - look at the box and experiment with it
examine - look at the code for the box
edit - change the box's content
rubric - several steps are suggested in the rubric page - form elements on page in rubric
06-29-01.html 06-29-01.js
| Kind
Kind of rubric items: standard - expected from all students advanced - used to get a better grade creative - an opportunity to do something creative levels - rubric item graded by selected achievement level optional - not required, but encouraged |
Description | |
| standard | some lighting | |
| advanced | lighting does not move with camera |
Just do some simple lighting. If we want good lighting, we will let three.js do it for us.
Non-Exercise: Images and Lighting
In the past, we asked you to make something that uses images and lighting. Practicing lighting isn’t worth it. It’s better to use three (just wait).
Exercise: Student’s Choice
Write a shader in 06-29-02.vs and 06-29-02.fs that does something interesting and different from the other shaders you’ve made already in this workbook. You will need to modify 06-29-02.js (06-29-02.html) to set up the uniforms and load textures.
In this exercise, you must add a uniform variable that controls some parameters, and have them controlled by a slider. You can look at the examples to see how to make sliders easily with the framework code.
If you write a shader that alters the geometry of the object (moving vertices), you may want to increase the tessellation of the sphere (see page 7 for an example). You can add more example geometry if you think it shows off your shader better - but please leave the plane and sphere.
You need to not only write a pair of shaders, but also explain what they do and how they work in the Workbook Form. You can earn some advanced points for a particularly interesting shader with an explanation.
Some things to consider trying:
- alter the geometry in the vertex shader (see the displacement map examples, such as the “islands” demo)
- mix lighting with other colorings
- animate effects (see page 2 for an example)
- have procedural effects modify images (or vice versa)
- create a different pattern
- combine effects (make dirty wood grain, or a checkerboard of different woods)
Optionally, you can also add a new mesh to showcase your shader, but please keep the sphere and the plane.
Feel free to add uniform variables (for example, to add a time variable).
view - look at the box and experiment with it
examine - look at the code for the box
edit - change the box's content
rubric - several steps are suggested in the rubric page - form elements on page in rubric
06-29-02.html 06-29-02.js
| Kind
Kind of rubric items: standard - expected from all students advanced - used to get a better grade creative - an opportunity to do something creative levels - rubric item graded by selected achievement level optional - not required, but encouraged |
Description | |
| standard | Some shader of student's choice | |
| advanced | Interesting Shader | |
| creative | Exceptional Shader |
Summary: Procedural Textures
We’ve done procedural textures in GLSL. Now let’s try to move on to make even more complex things. But for this, we’ll want some better tools.
Next: Page 30 - Composition