Skip to content

Lights and Textures

You'll wrap a cube in a photo on the GPU, shade it with Blinn-Phong from a moving lamp, fly a camera around it, and find out what mipmaps are for.

About

This is the weekend where the triangle grows up into a scene. You'll load an image into a texture, map it onto a cube with UV coordinates, add a depth test, and write a lighting model in the fragment shader with ambient, diffuse and specular terms. A camera you can fly with the mouse and keyboard turns a demo into something you can poke at. The thing nobody tells beginners is that normals need their own matrix when you scale things unevenly, and the thing everyone learns the hard way is that textures look like sparkling garbage in the distance until you turn on mipmaps.

At a glance

Per requirement
25 XP
Completion bonus
150 XP
Requirements
5
Takes
One weekend
Skill level
Intermediate (months of practice)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own
Locked for now
Earn two more Common badges in Graphics Programming to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Texture a cube with a photo you took yourself. Load the photo (an image library is fine here, since the point is the GPU side), upload it, and sample it in the fragment shader with UVs you set per vertex. Turn on the depth test or the back faces will draw over the front. Take a screenshot of the textured cube. Photo
  2. 2
    Light it with ambient, diffuse and specular from a point light that moves. Compute the light in the fragment shader using the world-space normal and the half vector for the specular highlight. Draw a small cube where the light is so you can see it moving. Film the highlight sliding across the faces as the lamp orbits. Video
  3. 3
    Fly a camera with the mouse and WASD. Build the view matrix from a position, a yaw and a pitch, and clamp the pitch so you can't flip over backwards. Film yourself flying around the cube. Video
  4. 4
    Show the difference mipmaps make. Lay a checkerboard texture on a large floor stretching to the horizon and take a screenshot with mipmaps off, then on. The far end should go from shimmering noise to smooth grey. Post both screenshots side by side. Photo
  5. 5
    Draw a hundred cubes with one draw call. Use instancing with a per-instance transform rather than a loop of draw calls. Print the frame time on screen. Take a screenshot of the field of cubes with the frame time visible. Photo