Index / 001 EN
Computer Graphics from Scratch: A Programmer's Introduction to 3D Rendering cover

Computer Science

Computer Science

Computer Graphics from Scratch: A Programmer's Introduction to 3D Rendering

Gabriel Gambetta

A short introduction to how 3D images are produced, in which readers write two renderers from first principles: a raytracer and a rasterizer. Starting from nothing more than the ability to colour a single pixel, it derives each technique from basic geometry and explains it in pseudocode, with interactive demonstrations available online.

Prerequisites: Basic programming in any language and secondary-school algebra and trigonometry; the vector and matrix mathematics needed is summarized in an appendix.

Difficulty Level
Beginner
Academic Level
Undergraduate
computer graphics3d renderingraytracingrasterizationlinear algebraTextbook

01 / Reading Guide

Reading Guide

Citation

Gambetta, G. (2021). Computer Graphics from Scratch: A Programmer's Introduction to 3D Rendering. No Starch Press. Free official edition: https://gabrielgambetta.com/computer-graphics-from-scratch/

Chapter Summary

Introduction

The introduction narrows the topic to one slice of computer graphics: rendering 3D scenes. Gambetta, who taught the subject for five years as a third-year university course, aims at readers from high school to professional engineers, choosing the easiest-to-understand version of each industry-standard algorithm while avoiding hand-waving. The only primitive assumed is a function that sets the colour of one pixel. The book builds two renderers, a raytracer and a rasterizer, in language-neutral pseudocode with links to working JavaScript demos, and deliberately does not teach APIs such as OpenGL, Vulkan, Metal or DirectX.

Chapter 1: Introductory Concepts

This short chapter defines the canvas and its coordinate system, the single drawing operation PutPixel, and how colours are represented: subtractive and additive colour models, colour depth, and operations such as scaling and adding colours with clamping. It ends by introducing the scene and its 3D coordinate system, the shared foundation for both parts of the book.

Chapter 2: Basic Raytracing

Part I opens with an analogy: painting a landscape by looking through a fixed insect net and copying the colour seen through each square. From this the chapter derives the fixed camera and viewport, the mapping from canvas pixels to viewport points, the ray equation, the sphere equation, and the quadratic that gives their intersections. The result is a first raytracer that draws spheres as flat coloured circles.

Chapter 3: Light

The raytracer gains lighting. The chapter defines point, directional and ambient lights, then models matte surfaces with diffuse reflection (using surface normals and the angle to the light) and shiny surfaces with a specular term controlled by a specular exponent, combining them into a full illumination equation. The spheres now look three-dimensional.

Chapter 4: Shadows and Reflections

Objects now interact with each other. Shadows are computed by casting a ray from each point towards each light and checking for blockers; reflections are computed by recursively tracing a reflected ray, with a recursion limit to stop infinite bouncing between mirrors.

Chapter 5: Extending the Raytracer

An overview of features beyond the book's scope: arbitrary camera positioning with rotation, performance techniques (parallelization, caching, shadow optimizations, spatial structures, subsampling), other primitives such as triangles, constructive solid geometry, transparency with refraction, and supersampling. The chapter closes by explaining why raytracing has been too slow for real-time use, motivating Part II.

Chapter 6: Lines

Part II starts again from a blank canvas. The chapter develops an algorithm to draw line segments of any slope with PutPixel, and introduces the Interpolate helper that computes the values of a linear function at integer steps; the author stresses that this function is used throughout the rest of the book.

Chapter 7: Filled Triangles

Building on lines, the chapter draws wireframe triangles and then filled triangles, by sorting vertices and treating the triangle as a stack of horizontal segments whose end points are interpolated along the edges.

Chapter 8: Shaded Triangles

The filling algorithm is extended to interpolate an intensity value across the triangle, first along the edges and then across each horizontal segment, producing smooth gradients. The author points out that the same method can interpolate any vertex attribute that varies linearly on screen, which later chapters exploit.

Chapter 9: Perspective Projection

The book turns to 3D, deriving the perspective projection equation from similar triangles, examining its properties (lines project to lines), and projecting the vertices of a cube to draw it as a wireframe. Two limitations are left open: mixing geometry with projection code, and points behind the camera.

Chapter 10: Describing and Rendering a Scene

This long chapter represents objects as models made of vertices and triangles, places multiple instances of a model in the scene, and applies model and camera transforms so the camera can move. It then introduces homogeneous coordinates and expresses rotation, scaling, translation, projection and the viewport-to-canvas mapping as matrices, so that a whole pipeline of transforms can be condensed into a few matrix multiplications.

Chapter 11: Clipping

To handle geometry behind or outside the camera's view, the chapter defines a clipping volume bounded by five planes, then clips whole objects using bounding spheres and individual triangles by computing segment-plane intersections, with pseudocode and a discussion of where clipping fits in the rendering pipeline.

Chapter 12: Hidden Surface Removal

To render solid objects, the chapter compares the painter's algorithm with depth buffering, explains why the buffer stores 1/Z rather than Z, and adds back-face culling, which uses a simple algebraic test to skip triangles facing away from the camera.

Chapter 13: Shading

The illumination equation from Chapter 3 is now applied to triangles. The chapter separates illumination from shading and presents three trade-offs between speed and quality: flat shading, Gouraud shading (interpolating vertex illumination) and Phong shading (interpolating normals and computing lighting per pixel).

Chapter 14: Textures

Triangles are painted with images, using a crate texture as the running example. The chapter shows why naive interpolation of texture coordinates looks wrong under perspective and derives perspective-correct texture mapping, then improves quality with bilinear filtering, mipmapping and trilinear filtering.

Chapter 15: Extending the Rasterizer

The final chapter surveys further techniques: normal mapping, environment mapping, and two approaches to shadows in a rasterizer, stencil shadows (shadow volumes counted in a stencil buffer) and shadow mapping.

Appendix: Linear Algebra; Afterword

The appendix gives a practical introduction to points, vectors, the dot and cross products, and matrices and their products, as used in the book. The afterword suggests next topics, including global illumination, physically based rendering, voxel rendering and level-of-detail algorithms.

Key Concepts

Chapter 1: Introductory Concepts

  • Canvas and PutPixel: a rectangular grid of pixels and the single operation of setting one pixel's colour, from which everything else is built.
  • Additive colour model: representing colours as red, green and blue intensities that add together, as screens emit light.

Chapters 2–5: Raytracing

  • Ray–sphere intersection: substituting the ray equation into the sphere equation gives a quadratic whose roots are the hit points.
  • Diffuse and specular reflection: matte surfaces scatter light according to the angle between the normal and the light, while shiny surfaces add a highlight around the mirror direction.
  • Shadow ray and recursive reflection: tracing extra rays towards lights and along reflected directions to model how objects affect each other.

Chapters 6–8: Lines and Triangles

  • Linear interpolation (Interpolate): computing evenly spaced values of a linear function, the workhorse of the rasterizer.
  • Scanline triangle filling: drawing a triangle as horizontal segments between interpolated edge positions.
  • Attribute interpolation: carrying any per-vertex value (intensity, depth, texture coordinates) across a triangle.

Chapters 9–11: Projection, Scenes and Clipping

  • Perspective projection: dividing by depth so that distant points appear closer to the centre of the view.
  • Homogeneous coordinates: adding a fourth coordinate so translation and projection, like rotation and scaling, can be written as matrix multiplication.
  • Clipping volume: the region bounded by five planes that can be safely projected; geometry outside it is removed or cut.

Chapters 12–15: Visibility, Shading and Textures

  • Depth buffer: a per-pixel record (here of 1/Z) used to keep only the closest surface regardless of drawing order.
  • Gouraud and Phong shading: interpolating lighting computed at vertices, or interpolating normals and lighting each pixel.
  • Perspective-correct texture mapping and mipmapping: interpolating texture coordinates correctly under projection, and using pre-filtered smaller textures to avoid artefacts.

Critical Analysis

Strengths

  • Truly from scratch. Starting from nothing but PutPixel, the book derives each technique from simple geometry, so readers see where every equation comes from. The analogy of painting through an insect net makes the raytracing setup intuitive.
  • Two renderers compared. Building both a raytracer and a rasterizer for the same scene lets the book contrast their strengths: shadows and recursive reflections come naturally to the raytracer, while textures and speed favour the rasterizer. Chapter 13 explicitly reuses the lighting model from Chapter 3.
  • Language-neutral pseudocode with live demos. Most chapters link to working JavaScript implementations that run in a browser, so readers can check their own code against a reference and see each step's visual effect.
  • Short, focused chapters with clear summaries. Each chapter ends with a summary that says what was built and what limitation remains, which makes the progression easy to follow and to revise.

Limitations

  • No exercises. The book has no problem sets; practice comes only from implementing the renderers, so instructors need to supply assignments.
  • Simplified lighting. The lighting model (ambient, diffuse, Phong-style specular) is empirical rather than physically based; global illumination and physically based rendering are only named in the afterword.
  • No GPU or API coverage. By design, the book does not teach OpenGL, Vulkan, DirectX or shader programming, so students who need practical real-time graphics skills must learn them elsewhere.
  • Advanced topics only sketched. Chapters 5 and 15 survey acceleration structures, refraction, normal mapping and shadow techniques in overview form, without full algorithms or analysis.

Where It Fits in the Course

Gambetta is the entry point to rendering in this graphics list, alongside Rogers, Sharp and Preece's Interaction Design, which covers the human-computer interaction side of the course rather than rendering. It is a good first read before Marschner and Shirley's Fundamentals of Computer Graphics or Angel and Shreiner's Interactive Computer Graphics, which are generally described as fuller course textbooks; the latter teaches WebGL programming, the API layer this book deliberately skips.

For advanced study, Real-Time Rendering (Akenine-Möller et al.) extends the rasterization half towards modern GPU techniques, and Physically Based Rendering (Pharr, Jakob and Humphreys) takes the raytracing half to physically based light transport, the topic this book's afterword names as a next step. Students who already know basic graphics can skim Gambetta quickly or skip to the more advanced texts.

How to Use This Book

The book suits anyone who can program in some language and remembers high-school algebra and a little trigonometry; the linear algebra needed is covered in the appendix, which is worth reading first if vectors and matrices are unfamiliar.

A practical approach is to implement each renderer yourself in a language of your choice, using a library or browser canvas only to set pixels, and to compare with the linked demos when something looks wrong. Part I (Chapters 2–4) can be completed in a few sessions and gives quick visual results; Chapter 5 is a menu of optional extensions. Part II (Chapters 6–14) is longer, and Chapters 6–8 must be understood well, since Interpolate and attribute interpolation underpin everything after them. Chapters 9–12 form the geometric core of the rasterizer; Chapters 13–14 are where it starts to look realistic. Chapter 15 can be treated as a reading list.

Because there are no exercises, a self-directed reader can set their own: implement the extensions sketched in Chapters 5 and 15, such as triangles in the raytracer, a movable camera, or shadow mapping in the rasterizer.

Edition and Sources

This guide is based on the free online edition of Computer Graphics from Scratch on the author's official site, https://gabrielgambetta.com/computer-graphics-from-scratch/, which presents the text of the 2021 No Starch Press book. It draws on the table of contents, the introduction in full, every chapter (all section headings, chapter openings and end-of-chapter summaries, with central derivations such as the ray–sphere intersection, the projection equation and depth buffering with 1/Z read in detail), the linear algebra appendix and the afterword.