CS 488: Lab 8 – Moon

Welcome to lab, which is a place where you and your peers complete exercises designed to help you learn the ideas discussed in the preceding lectures. It is intended to be a time where you encounter holes in your understanding and talk out loud with your peers and instructor.

Your instructor will bounce around between breakout rooms to check in with you. However, you are encouraged to request assistance if you find your progress blocked.

Designate one of your group to be the host. This individual will be responsible for setting up a Live Share session in Visual Studio Code and submitting your work. No team member should dominate or be expected to carry the group. All members should be writing code and contributing ideas.


Host, follow these steps:

  1. Open Visual Studio Code.
  2. Click File / Open Folder, create a new folder, and open it.
  3. With the Live Share extension installed, select View / Command Palette, and choose Live Share: Start Collaborative Session.
  4. Copy the invitation link to your chat.

Non-hosts, join the session.


Your task in this lab is to add detail to surfaces using textures. Follow these steps to complete this lab:

  • Render a sphere that looks like the moon in the sky. Texture it using a moon texture from NASA. The 1024×512 JPEG image is a good option. Normalize the latitude and longitude iterators to generate texture coordinates for every vertex.
  • Using the same shader program as the moon, render a quadrilateral that looks like flat terrain. Texture it using an appropriate image, like this one of grass.
  • Use a Camera that allows the user to advance and strafe along the scene and look around with the mouse.
  • An orthographic projection offers no sense of depth. Use a perspective matrix instead. Add these four methods to your Matrix4 class:
    get(r, c) {
      return this.elements[c * 4 + r];
    set(r, c, value) {
      this.elements[c * 4 + r] = value;
      return this;
    static fovPerspective(fovY, aspect, near, far) {
      let y = near * Math.tan(fovY * 0.5 * Math.PI / 180);
      let x = y * aspect;
      return this.frustumPerspective(-x, x, -y, y, near, far);
    static frustumPerspective(left, right, bottom, top, near, far) {
      let m = new Matrix4();
      m.set(0, 0, 2 * near / (right - left));
      m.set(1, 1, 2 * near / (top - bottom));
      m.set(2, 2, (near + far) / (near - far));
      m.set(0, 2, (right + left) / (right - left));
      m.set(1, 2, (top + bottom) / (top - bottom));
      m.set(2, 3, 2 * far * near / (near - far));
      m.set(3, 2, -1);
      m.set(3, 3, 0);
      return m;
    Then in your onSizeChanged listener, generate your projection matrix with this call:
    clipFromEye = Matrix4.fovPerspective(30, aspectRatio, 0.01, 1000);
    Adapt this code to the names you’ve used. The string “fov” stands for field of view, and the call states that we want to see a wedge of the world that is 30-degrees tall. The final two parameters state how far from the eye the near and far clipping planes will be. They must be positive for perspective matrices.
  • Submit your index.js on Crowdsource. Enter the eIDs for your team members. If you need to make changes after you’ve already submitted, just reload the page and resubmit. If you haven’t finished by the end of the scheduled lab time, you are free to continue working. However, the submission must be made before the end of the day to receive credit.


Leave a Reply

Your email address will not be published. Required fields are marked *