Guide to AI and Data Courses · Lecture Notes
Chapter 22: Computer Graphics
For a character walking past the camera in a game to appear in the right place, at the right size and with the right light, millions of small calculations are done in every frame. Computer graphics is the course about those calculations: describing a scene with triangles, matrices and lighting equations, and then turning it into the pixels of a screen. These notes introduce the course and work through its three core ideas, transformations, the path from triangle to pixel, and the computation of light, with small examples you can solve by hand.
Learning outcomes
- Scene and image
- Describe a scene as geometry, materials, lights and a camera; explain graphics as the inverse of computer vision.
- Transformations
- Write translation, rotation and scaling as matrices in homogeneous coordinates and apply a composite transformation to a point.
- Triangle to pixel
- Work through the perspective divide, barycentric coordinates and the z-buffer by hand on a small example.
- Lighting
- Compute the ambient, diffuse and specular terms of the Phong model; decide with a quadratic equation whether a ray hits a sphere.
- Place of the course
- State what separates computer graphics from computer vision, image processing, generative models and GPU programming.
22.1What question does the course ask?
How is a realistic image produced from the description of a scene?
Computer graphics studies how to render an image from a scene described by geometry, materials, lights and a camera. It is the inverse of computer vision: vision recovers the scene from an image, graphics produces the image from the scene.
In computer vision we have an image and try to recover the scene from it. In computer graphics the direction is reversed: the scene is known and the image is computed. A scene is described by four things: the geometry of the objects, the materials of the surfaces, the lights and a camera. The question of the course is how, given these four, to compute the color of every pixel both correctly and fast enough.
"Fast enough" shapes the second half of the course. A game must produce every frame in a sixtieth of a second, so real-time graphics computes light with a rough but very fast approximation and spreads the work over the thousands of cores of a GPU. A film can spend minutes per frame, and there the physics of light is simulated far more faithfully. The same scene can be rendered in two ways, and the course introduces both.
22.1.1Two basic approaches
Rasterization is object-centered: for each triangle, find which pixels on the screen see it. Ray tracing is pixel-centered: for each pixel, send a ray from the camera and find the first surface it hits. Rasterization is fast and is the foundation of graphics cards; ray tracing gives shadows, reflection and refraction naturally but costs more. The three core sections below build the mathematics the two share: transformations, rasterization and the computation of light.
22.1.2At a glance
| Criterion | Computer Graphics |
|---|---|
| Approach | geometry and light simulation |
| Input | scene: models, lights, camera |
| Output | image or animation |
| Success measure | realism, frame rate |
| Math load | high: linear algebra, geometry |
| Data needs | none |
22.2A short history
| Year | Milestone |
|---|---|
| 1963 | Sutherland's Sketchpad shows drawing on screen with a light pen; interactive graphics begins. |
| 1975 | Phong publishes his reflection model and shading method; shiny surfaces look real. |
| 1980 | Whitted's ray tracing unifies reflection, refraction and shadows in one method. |
| 1986 | Kajiya publishes the rendering equation: the basis of physically based rendering. |
| 1995 | Toy Story becomes the first fully computer-generated feature film. |
| 2018 | Hardware ray tracing reaches consumer GPUs; real-time ray tracing begins. |
22.3Core concepts
The rest of the course is built on the concepts below. The following sections work through the most important ones with examples.
| Concept | Meaning |
|---|---|
| Homogeneous coordinates | Writing translation, rotation and scaling as 4×4 matrices. |
| Model-view-projection | The chain of transforms from object space to screen space. |
| Triangle mesh | Representing surfaces with vertices and triangles. |
| Rasterization | Finding the pixels a triangle covers. |
| Z-buffer | Visibility by keeping the nearest surface per pixel. |
| Lighting model | Ambient, diffuse and specular: Phong, Blinn-Phong. |
| Texture mapping | Wrapping an image or pattern on a surface; UV coordinates. |
| Ray tracing | Shooting rays from the camera and tracing light paths backward. |
22.4Homogeneous coordinates and transformations
Placing, rotating or enlarging an object means applying a transformation to the coordinates of its vertices. Rotation and scaling reduce to multiplication by a matrix, but translation adds a number to the coordinates and is not a linear multiplication. The fix is to add an extra coordinate to the point: instead of the 2D point (x, y) we write (x, y, 1). All three transformations then take the same form, a matrix product, and transformations applied one after another can be combined into a single matrix.
The 2D point (x, y) is written as the column vector (x, y, 1). The last column of the translation matrix T(tx, ty) is (tx, ty, 1); the rotation R(θ) by angle θ carries cos and sin in its upper left 2×2 block. In 3D the same idea uses 4×4 matrices.
Rotate the point P = (2, 1) by 90° about the origin, then translate it by (4, 1).
- Rotation matrix. R(90°) = [[0, −1, 0], [1, 0, 0], [0, 0, 1]].
- Translation matrix. T = [[1, 0, 4], [0, 1, 1], [0, 0, 1]].
- Combining. R is applied first and T second, so M = T · R = [[0, −1, 4], [1, 0, 1], [0, 0, 1]]; in a matrix product the transformation applied first is written rightmost.
- Applying it. M · (2, 1, 1) = (0·2 − 1·1 + 4, 1·2 + 0·1 + 1, 1) = (3, 3, 1).
- Check. Step by step: R · P = (−1, 2), then (−1 + 4, 2 + 1) = (3, 3). Same result.
The combined matrix is computed once and then applied to thousands of vertices of an object with one multiplication each. This is why graphics cards can do transformations so cheaply.
In a real scene the path from object space to the screen is a product of such matrices: the model matrix places the object in the world, the view matrix expresses the world relative to the camera, and the projection matrix brings 3D onto the screen. The product of the three is called the model-view-projection transform.
Ignoring the order of transformations. Apply the same two transformations in the opposite order, translate by (4, 1) first and rotate by 90° second, and P = (2, 1) first becomes (6, 2) and then goes to (−2, 6), a very different place from (3, 3). Matrix multiplication is not commutative: T · R ≠ R · T.
22.5From triangle to pixel: projection, rasterization, z-buffer
Suppose the camera looks along the z axis and the image plane is at distance d from the camera. A point (x, y, z) in camera space lands on the screen at (d·x/z, d·y/z): the perspective divide. As the distance z grows the object shrinks; near objects look large and far ones small.
Take d = 1. The point A = (2, 1, 4) lands at (2/4, 1/4) = (0.5, 0.25). Move the same point to half the distance, B = (2, 1, 2), and it lands at (2/2, 1/2) = (1, 0.5): the position on screen doubles, the object looks twice as large.
Once the vertices are on the screen, rasterization finds which pixels each triangle covers. Whether the center of a pixel is inside the triangle is tested with barycentric coordinates. In a triangle with vertices A, B, C a point is written P = λAA + λBB + λCC with λA + λB + λC = 1. If all three weights are non-negative the point is inside the triangle. The same weights are used to spread the colors, texture coordinates and depths of the vertices over the pixel.
Take the triangle with screen vertices A = (0, 0), B = (4, 0), C = (0, 4) and the pixel center (1, 1). The camera distances of the vertices are zA = 2, zB = 6, zC = 10.
- Weights. In this triangle λB = x/4 = 0.25 and λC = y/4 = 0.25; λA = 1 − 0.25 − 0.25 = 0.5. None is negative, so the pixel is inside the triangle.
- Depth. Under perspective the quantity that varies linearly on the screen is 1/z, not z: 1/z = 0.5/2 + 0.25/6 + 0.25/10 ≈ 0.3167, so z ≈ 3.16.
- Z-buffer. The buffer starts at infinity; 3.16 is written to the pixel and the triangle's color goes to the frame buffer. Then another triangle arrives at the same pixel with z = 4. Since 4 > 3.16 it is behind and is discarded. Had it arrived with z = 2.5, it would have replaced both depth and color.
The z-buffer solves visibility without sorting the triangles: each pixel keeps only the nearest depth and color seen so far.
Interpolating depth linearly on the screen. In the triangle above, adding the vertex depths directly with the weights gives 0.5·2 + 0.25·6 + 0.25·10 = 5; the correct value is about 3.16. Under perspective the relation between screen coordinates and depth is not linear; 1/z is. The same mistake on texture coordinates makes a texture look warped, which is why graphics cards use perspective-correct interpolation.
22.6Lighting and ray tracing
The color in which a surface appears at a pixel is determined by light. The most common simple model is the Phong model: the color is a sum of three terms. The ambient term imitates indirect light that reaches everywhere with a constant. The diffuse term is a matte surface scattering light equally in all directions; the more directly the light hits the surface, the brighter it is (Lambert's law). The specular term gives the bright highlight for an eye looking along the direction in which the light reflects as in a mirror.
I = ka + kd · max(0, n·L) + ks · max(0, R·V)p. Here n is the surface normal, L the unit vector toward the light, V the unit vector toward the camera, R = 2(n·L)n − L the direction of reflection, and p the shininess exponent; a larger p gives a smaller, sharper highlight.
The normal is n = (0, 0, 1), the camera is in the direction V = (0, 0, 1), and the light hits the surface at 45°: L = (1, 0, 1)/√2 ≈ (0.707, 0, 0.707). The coefficients are ka = 0.1, kd = 0.8, ks = 0.5, p = 8; the light intensity is 1.
- Diffuse. n·L = 0.707; 0.8 · 0.707 ≈ 0.566.
- Reflection direction. R = 2 · 0.707 · (0, 0, 1) − L = (−0.707, 0, 0.707).
- Specular. R·V = 0.707; 0.7078 = 0.0625; 0.5 · 0.0625 ≈ 0.031.
- Total. I = 0.1 + 0.566 + 0.031 ≈ 0.70.
The specular term is small because the camera is 45° away from the reflection direction; on a surface that reflects the light toward the camera, R·V approaches 1 and a bright highlight appears.
22.6.1Ray tracing: a ray and a sphere
In ray tracing a ray is sent from the camera through each pixel: r(t) = o + t·d, with origin o, unit direction d and distance t ≥ 0. The surface of a sphere with center c and radius r satisfies |p − c|2 = r2. Substituting the point on the ray gives a quadratic equation in t: t2 + 2(d·(o−c))t + |o−c|2 − r2 = 0. If the discriminant is negative the ray misses the sphere; otherwise the smaller positive root is the first hit.
The camera is at the origin, the sphere has center c = (0, 0, −5) and radius 1. The ray direction is d = (0, 0, −1).
- Coefficients. o − c = (0, 0, 5); B = d·(o−c) = −5; C = |o−c|2 − 1 = 24. The equation is t2 − 10t + 24 = 0.
- Discriminant. 100 − 96 = 4 > 0, so the ray hits the sphere.
- Roots. t = (10 ± 2)/2 = 4 and 6. The first hit is t = 4 at (0, 0, −4), where the ray enters the sphere; t = 6 is where it leaves.
- Normal. At the hit point n = (p − c)/r = (0, 0, 1); this normal goes into the Phong equation.
- A missing ray. With direction (0.6, 0, −0.8), B = −4, the equation is t2 − 8t + 24 = 0 and the discriminant is 64 − 96 = −32 < 0: the ray does not hit the sphere.
A ray tracer runs this test for every pixel and every object, keeps the smallest positive t, and computes the color at the hit point with Phong. For a shadow, one more ray is sent from the hit point toward the light: if something is in between, the point is in shadow.
Adding n·L as it is when it comes out negative. If the light arrives from behind the surface, n·L < 0 and the surface should be dark, not "subtract" light. That is why the formula has max(0, ·). In the same way, a negative t in the ray equation is a hit behind the camera and is discarded.
22.7How is the course taught?
22.7.1A typical workflow
In a game, a character's triangle mesh is projected to the screen by the world and camera matrices, rasterization finds the pixels each triangle covers, and a shader computes each pixel's color from light and texture. This repeats 60 times a second.
22.7.2Topics over the semester
- Raster images, color and gamma
- Vectors, matrices and 2D/3D transformations
- Viewing: camera, projection, view volume
- Triangle meshes and surface representation
- Rasterization and visibility with the z-buffer
- Shading: Phong and Blinn-Phong
- Texture mapping, UV coordinates and mipmaps
- The graphics pipeline and GPU shaders
- Ray tracing and acceleration structures (BVH)
- Curves and surfaces: Bézier, B-splines
- The rendering equation and Monte Carlo path tracing
- Animation: keyframes, skeletons, physics simulation
This is a typical semester; the order and coverage vary between universities.
22.7.3Skills you will gain
- drawing with OpenGL or WebGL
- writing GLSL shaders
- building 3D scenes with three.js
- writing a simple ray tracer
- preparing models and scenes in Blender
22.7.4Try it yourself
The Try It: Shading a Sphere with Light slide in the presentation lets you solve a typical problem of this course yourself: Every frame, a game engine computes how bright each pixel is from the surface normal and the light direction. Move the light with the sliders, switch the shading method, and click the sphere to see how one pixel is computed.
22.8Are you ready for this course?
22.8.1Prerequisites
| Course | Why it is needed |
|---|---|
| Linear Algebra | transformation matrices, projection |
| Programming with C | APIs such as OpenGL and Vulkan are used from C/C++ |
| Analytic geometry | vectors, planes, line-plane intersection: the language of ray tracing |
22.8.2Ask yourself
For every "no", the right-hand column shows where to start. If you can say "yes" to most of them, you are ready.
| Question | If not, start with |
|---|---|
| Can you rotate and translate a point with a 4×4 matrix? | Linear Algebra |
| Can you find the point where a ray hits a plane? | Line and plane equations from analytic geometry |
| Can you write a C or C++ program that uses arrays and structs? | An intro C course, then a short introduction to C++ |
| Can you explain why a GPU runs thousands of threads in parallel? | GPU Programming and Parallel Computing |
22.9Neighboring courses
22.9.1Where it overlaps
| Relation | Course | Shared ground |
|---|---|---|
| builds on | This course builds on Linear Algebra | transformation matrices, projection |
| shares methods | Shares methods with Generative Models | image and scene synthesis |
| shares methods | Shares methods with Computer Vision | camera model; graphics renders scenes, vision recovers them |
| shares methods | Shares methods with GPU Programming and Parallel Computing | graphics pipeline, shaders |
22.9.2Where it differs
- Computer Vision: Computer vision tries to recover the scene from an image; computer graphics produces the image from the scene.
- Image Processing: Image processing transforms an existing image; computer graphics builds the image from geometry and light.
- Generative Models: Generative models synthesize images learned from data; computer graphics computes them from physics and geometry.
- GPU Programming and Parallel Computing: GPU programming focuses on general parallel computing; computer graphics uses the GPU as a tool for rendering.
22.10In practice
22.10.1Applications
| Area | What is done |
|---|---|
| Games | Real-time graphics at tens of frames per second. |
| Film and animation | Physically based rendering taking minutes per frame. |
| CAD and architecture | Seeing a design before it is built. |
| Simulation | Flight and driving simulators. |
| Virtual reality | Low-latency images for two eyes. |
| Scientific visualization | Medical volume data, flow and weather models. |
22.10.2Risks and ethics
| Risk | Explanation |
|---|---|
| Photorealistic fakes | Realistic rendering makes it easy to produce a convincing photo or video of an event that never happened. |
| Photosensitive epilepsy | Fast flashing patterns can trigger seizures; in 1997 a Pokémon episode sent hundreds of children in Japan to hospital. |
| VR sickness | High latency or a low frame rate causes dizziness and nausea in VR; timing is as much a safety issue as the image. |
| Convincing but wrong | Architectural and scientific visualization approximate light and materials; a beautiful image can justify a wrong decision. |
These issues are discussed in depth in Explainable AI and AI Ethics.
22.10.3Tools and datasets
Tools: OpenGL, Vulkan, WebGL and three.js, Blender, Unity, Unreal Engine.
Classic test scenes: Utah teapot, Stanford bunny, Cornell box, Sponza atrium, Suzanne (Blender).
A graphics course gives a scene instead of a dataset: once geometry, materials, lights and camera are known, the image is computed.
22.11Common misconceptions
Computer graphics is just making games.
Film, architecture, medicine, simulation and scientific visualization are graphics too.
Ray tracing is a new invention.
The method dates from 1980; what is new is hardware that runs it in real time.
Graphics and computer vision are the same.
Each is the inverse of the other; they share the camera model but go opposite ways.
More triangles are enough for realism.
Light, shadow and material often matter more than geometric detail.
22.12Summary
- Computer graphics renders an image from a scene defined by geometry, materials, lights and a camera; it is the inverse of computer vision.
- With homogeneous coordinates translation, rotation and scaling become matrices that are combined by multiplication; order matters (T·R ≠ R·T).
- Under perspective the point (x, y, z) lands at (d·x/z, d·y/z); rasterization finds pixels with barycentric coordinates, and the z-buffer keeps the nearest surface at each pixel.
- The Phong model computes color as the sum of ambient, diffuse and specular terms; the diffuse term is proportional to n·L.
- Ray tracing sends a ray through each pixel and looks for the nearest hit; a ray and a sphere are solved with a quadratic equation. Rasterization is fast, ray tracing gives shadows and reflections naturally.
22.13Test Yourself
The point (1, 1) is scaled by 2 and then translated by (2, 3). What is the result? What if the order is reversed?
Answer
Scaling first: (2, 2); then translation: (4, 5). As a matrix, T · S. If the order is reversed, translation first gives (3, 4) and scaling then gives (6, 8); the result differs.
With d = 1, where on the screen is the point (3, 6, 3)? What happens if the point stays put in x and y but z becomes 6?
Answer
(3/3, 6/3) = (1, 2). With z = 6 it is (3/6, 6/6) = (0.5, 1): the position halves and the point moves toward the center.
In the triangle with screen vertices (0, 0), (6, 0), (0, 6), are the points (3, 1.5) and (4, 4) inside? (In this triangle λB = x/6, λC = y/6.)
Answer
For (3, 1.5): λB = 0.5, λC = 0.25, λA = 0.25; none is negative, so it is inside. For (4, 4): λB = λC = 2/3, λA = −1/3 < 0: outside.
A surface has kd = 0.6 and the light intensity is 1. What is the diffuse contribution when the light makes 60° with the normal, and when it makes 90°? (cos 60° = 0.5)
Answer
60°: 0.6 · 0.5 = 0.3. 90°: cos 90° = 0, so the contribution is 0; the light grazes the surface and does not illuminate it.
A ray leaves the origin in direction (0, 0, −1); the sphere has center (0, 0, −10) and radius 2. Where is the first hit?
Answer
o − c = (0, 0, 10); the equation is t2 − 20t + 96 = 0 (|o−c|2 − r2 = 100 − 4). The discriminant is 400 − 384 = 16; t = (20 ± 4)/2 = 8, 12. The first hit is t = 8, at (0, 0, −8).
One team wants to recover an object's 3D shape from a photograph; another wants to produce an image from a 3D model it already has. Which is a computer graphics question and which a computer vision question?
Answer
Recovering shape from a photograph is computer vision: from image to scene. Producing an image from a model is computer graphics: from scene to image. The two share the same camera model but work in opposite directions.
22.14After this course
Graphics uses linear algebra and GPU programming more concretely than any other course. From here you can move to real-time rendering, physically based rendering, or in the other direction to computer vision; generative models and neural rendering are steadily merging the two fields.
Related careers: Graphics programmer, Game engine developer, Technical artist, XR developer, Visualization engineer.
22.15Further reading
- S. Marschner and P. Shirley, Fundamentals of Computer Graphics (4th ed., 2016): covers transformations, rasterization, ray tracing and shading at textbook level.
- T. Akenine-Möller et al., Real-Time Rendering (4th ed., 2018): treats the real-time implementation of the ideas in the second and third sections of these notes in detail, together with the GPU pipeline.
- M. Pharr, W. Jakob and G. Humphreys, Physically Based Rendering (4th ed., 2023): shows the step from ray tracing to physically based rendering together with the source code of a working renderer.
22.16References
- Marschner and Shirley, Fundamentals of Computer Graphics, 4th ed., CRC Press, 2016.
- Hughes, van Dam, McGuire et al., Computer Graphics: Principles and Practice, 3rd ed., Addison-Wesley, 2013.
- Akenine-Möller, Haines, Hoffman et al., Real-Time Rendering, 4th ed., CRC Press, 2018.
- Pharr, Jakob and Humphreys, Physically Based Rendering, 4th ed., MIT Press, 2023.
Related pages: Slides · Slides (PDF) · Course page