CPSC 424 | Computer Graphics | Fall 2025 |
This lab introduces WebGL.
Successful completion of this lab means that you:
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:
Hand in your work by copying your ~/cs424/lab2 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/lab2 — if not, fix it!
Create a directory ~/cs424 to hold your files for this course.
Create a directory ~/cs424/workspace to act as your workspace for WebGL programs.
Copy the directory /classes/cs424/lib and its contents into your ~/cs424/workspace directory. You should end up with a folder lib inside your workspace directory, with files inside of it.
Copy the directory /classes/cs424/lab2 and its contents into your ~/cs424/workspace directory. You should end up with a folder lab2 inside your workspace directory, with files inside of it.
Make sure that your lab2 and lib directories are named exactly like that and are 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.
The lib directory you copied contains one file:
webgl-utils.js contains some utility routines for setting up shader programs.
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.
Web development requires a text editor (for writing code), a web browser (for testing code), and a local web server (in order for loading external files, such as image textures, to work). We will be using Visual Studio Code (VS Code) for editing and viewing WebGL programs and it provides most of these things (along with some useful development-related functionality), is free to download and use, and is available for Mac, Windows, and Linux.
VS Code is available on the Linux systems on campus — in Lansing 310, on the dual-boot computers in Demarest 002, and through the Linux VDI. Note: there is currently an issue with Live Preview on the Linux-boot computers (Lansing 310, Demarest 002) — for those systems, you'll need the workaround described below. Live Preview works fine in the VDI.
Run VS Code. (You can find in on the main menu on the Linux systems.)
Install the Live Preview extension: go to
View→Extensions or click on the Extensions
icon on the left side of the VS Code window, then
type "live preview" into the search box. The one you want is
called "Live Preview" and is from Microsoft. Click on the
"Install" button.
Configure some key settings: go to
File→Preferences→Settings or click on the Settings
icon in the
bottom left corner of the VS Code window.
VS Code reports usage data to Microsoft. Type "telemetry" in the Settings search box to find the Telemetry Settings. Locate the Telemetry: Telemetry Level setting and choose "off" (or whatever level you prefer).
Turn on periodic auto save: type "auto save" in the search box. Locate the Files: Auto Save setting and choose "afterDelay", then go to File→Auto Save to toggle auto save on and off (there will be a checkmark if it is active)
Turn on format-on-save so code is autoformatted each time it is saved: type "format on save" in the search box. Locate the Editor: Format On Save setting and make sure the check box is checked.
(Optional — come back to this later if you want to set it up) To use Copilot:
Install the GitHub Copilot extension: go to
View→Extensions or click on the Extensions
icon on the left side of the VS Code window, then
type "copilot" into the search box. The one you want is
called "GitHub Copilot" and is from GitHub. Click on the
"Install" button.
Sign up for GitHub Copilot Free: click on the GitHub Copilot extension in the Extensions panel, then scroll through the information about the extension until you find the "Getting access to GitHub Copilot" section.
While you can open individual files, it is more convenient to open the folder that contains your files instead. When opening a new folder for the first time, you may be asked if you trust the author of these files. You should in this case, but it is wise not to blindly run things you've downloaded if you aren't sure of the trustworthiness of the source.
Opening a folder will probably also open the Explorer sidebar,
where you can browse the contents of the folder. If the Explorer
sidebar doesn't open or gets lost at some point, you can open it
with View→Explorer or click on the on the left side of the VS Code window
Create a new file with File→New File... This will prompt you for a filename, then open the Create File dialog for you to select what folder the file should go into.
From the Explorer sidebar, you can open a file for temporary viewing with a single click ("temporary" means the tab displaying the file contents will go away if you click on another file without making any changes) or for persistent viewing (it will stay open until closed) with a double-click or by dragging it onto an editor pane.
You can have multiple files open at once — they appear on separate tabs within an editor pane. If you want to view two (or more) things at the same time, you can reconfigure the current layout (and add, remove, or rearrange editor panes) with View→Editor Layout. Opening or creating a new file will add a tab for that file in the current editor pane. You can drag a file's tab to the title bar of another editor pane to change what pane it is in, or drag it into the main content area of a pane to either move the tab or create a new editor pane depending on where you drop it.
Open your ~/cs424/workspace folder: File→Open Folder and then navigate to and select (so that it is highlighted) but do not open the folder you want to open.
Open the lab2/hello-webgl.html file.
VS Code is extensible and only the core features appear on menus — additional functionality, as well as functionality provided by extensions, is accessed via the Command Palette. The Command Palette can be opened with View→Command Palette... or the keyboard shortcut ctrl-shift-P. You can then search for the desired command or browse the list. Recently-used commands will be at the top.
VS Code provides syntax highlighting, auto indentation, auto close (for tags, parens, brackets), suggested/auto completions, and various other helpful assists for writing code.
One additional feature to be aware of (and use!) is auto format — access via the Command Palette and search for Format Document or Format Selection, or use the keyboard shortcuts: ctrl-shift-I (format document) or ctrl-K ctrl-F (format selection). If there's no active selection, Format Selection will format the current line.
Live Preview shows you how a web browser would render your document, updating as you type. Once the extension has been installed, you can open a preview pane through the Command Palette (View→Command Palette or ctrl-shift-P) — search for "live preview" and select "Live Preview: Show Preview (Internal Browser)" — or by right-clicking on the file in the Explorer and choosing "Show Preview".
Open a Live Preview for your lab2/hello-webgl.html file. You should see something in the display — if there is an error message, something isn't set up correctly.
There is currently a problem with Live
Preview on the Linux-boot computers (Lansing 310 and
Demarest 002) — on these machines, opening Live
Preview will send the CPU usage to 100% but nothing else
appears to happen. (Alternatively, you may get a new
empty tab.) Quit and restart VS Code to kill the
runaway process. The workaround for Live Preview is to
start a web browser manually — start Firefox from
the Applications menu — and
type /home/username/cs424/workspace into
the URL/search box. Navigate to the particular page you
want to view from there. This view is not live so you
will need to refresh (reload) the page each time you
make changes to the code in order to see an updated view.
Live Preview updates as you edit the file, so you may want to close that tab once you've viewed your file.
JavaScript error messages are usually displayed to the web console. From the Live Preview tab, click on the hamburger menu (three horizontal lines) in the upper right corner and choose "Open Devtools Pane". Click on the "Console" tab at the top of the devtools pane to see the web console.
In Firefox, click on the hamburger menu (three horizontal lines) in the upper right corner and choose More Tools→Web Developer Tools. Click on the "Console" tab at the top of the developr tools pane to see the web console.
Note: No projection, viewing, or modeling transforms have been set up yet, so remember that you are working in clip coordinates — keep the coordinates for the triangles within the range -1 to 1.
Make a copy of the provided hello-webgl.html file called lab2a.html, then modify lab2a.html as directed.
Extend the one primitive drawn so that it contains at least three triangles with different orientations (i.e. don't give all the vertices in each additional triangle the same z coordinate). Include at least one instance where two triangles overlap in x and y but have different depths (z). Feel free to change the coordinates of the existing triangle so it doesn't take up as much of the window.
Technical notes:
Make a copy of the provided hello-webgl.html file called lab2b.html, then modify lab2b.html as directed.
Add a second primitive with different vertices and colors and of a type other than gl.TRIANGLES.
The vertex shader in the provided hello-webgl.html takes (x,y,z) coordinates and a color for each vertex, allowing for multicolored primitives with any orientation in 3D. In this exercise, you'll modify the shader to only deal with flat (in the xy plane) single-color primitives. The end result of this exercise should be a program which draws a single-color triangle with coordinates (-0.9,-0.8), (0.9,-0.8), (0,0.9) in the z=0 plane. The choice of color is up to you.
Technical notes:
Make a copy of the provided hello-webgl.html file called lab2c.html, then modify lab2c.html as directed.
Modify the shaders:
The vertex shader should now take a 2D point (x,y), a z coordinate, and a color. The (x,y) coordinates are per-vertex, but the z coordinate and color are per-primitive. Which should be attributes and which uniforms?
Update the body of the vertex shader accordingly.
Are any changes needed for the fragment shader? If so, make them.
Update the passing of information to the shaders. This involves making changes to:
In this exercise you'll extend the previous program to draw three flat triangles in different colors, however, all of the coordinates will be sent to the GPU at once instead of sending the coordinates for each triangle separately.
Technical notes:
Make a copy of your lab2c.html file from the previous exercise called lab2d.html, then modify lab2d.html as directed.
Add to the coords array so that it includes (x,y) coordinates for three triangles. Have at least one instance where triangles overlap.
Create (regular JavaScript) arrays for the three z coordinates and the three triangle colors.
Write a loop to repeat for each triangle: set up the uniforms for that triangle, then draw just that triangle's vertices from the whole array of vertices. The second and third parameters for gl.drawArrays are important here. Note that setting up the coords attribute is not part of the loop!