# Introduction

TL;DR: This book will not teach you how to use OpenGL or DirectX; instead, it can teach you how OpenGL and DirectX work. In practice you won’t write a software renderer for production use, but understanding how and why a renderer works will let you use OpenGL and DirectX more effectively.

Computer Graphics is a fascinating topic - how do you make Toy Story out of algorithms and some geometric data? Interestingly enough, it’s a mysterious topic not only for the average cinema fan, but also for world-class engineers who just haven’t been exposed to it.

Computer Graphics is also a frighteningly broad topic. From 3D rendering to photographic image filters, from fonts to particle systems, there’s a multitude of disciplines that can be categorized under CG. This work focuses exclusively on 3D rendering.

Computer Graphics from scratch is my humble attempt to demystify that slice of Computer Graphics in an accessible way. It can be easily understood by high-school students, while covering the same topics of an university course. It is, in fact, based on my years of teaching the subject at my university.

There’s little pre-requisite knowledge, nor hardware or software dependencies. The only primitive used in the book is a method that lets us set the color of a pixel - hence “from scratch”. The algorithms are conceptually simple, and the math is straightforward. There is some high-school level trigonometry. There’s some basic Linear Algebra as well, but I’m including an appendix that can be consulted as necessary.

This work is divided in two main parts, Raytracing and Rasterization, which focus on the two main ways to make pretty pictures out of data. The Common concepts chapter introduces some basic knowledge necessary to understand these two parts.

The focus of this work is not on performance, but on clear conceptual exposition. The sample code is, well, sample code, written in the most clear way possible, which may not be the most efficient way to implement the algorithms. Where there are different ways to do something, I’ve chosen the easiest to understand.

The “end result” of this work is two complete, fully functional renderers: a raytracer and a rasterizer. Although they follow very different approaches, they produce similar results when used to render a simple scene:

While their sets of features have considerable overlap, they aren’t identical, so this book covers their specific strengths:

The book provides sample code throughout the text, as somewhat informal pseudocode; it also provides fully working implementations written in JavaScript that can run directly on your browser, rendering to a canvas element.

This work should give you all the knowledge necessary to write software renderers. Although in the age of GPUs few people have good reasons to write a pure software renderer, the experience of writing one is valuable for the following reasons:

1. Shaders. The first GPUs had their algorithms hardcoded in hardware, but in modern ones you’re expected to write your own shaders. In other words, you’re still implementing big chunks of rendering software, except it now runs on the GPU.

2. Understanding. Whether you’re using a fixed pipeline or writing your own shaders, understanding what’s going on behind the scenes lets you use the fixed pipeline better and write better shaders.

3. Fun. Few areas of Computer Science have the kind of immediately visible results offered by Computer Graphics. The sense of accomplishment you get when your SQL query runs just right is nothing compared to what you feel the first time you get ray traced reflections right. I taught Computer Graphics at my university for 5 years. I often wondered why I enjoyed teaching the same thing semester after semester for so long; in the end, what made it worth it was seeing the faces of my students light up and use their first renders as desktop backgrounds.

## Work in progress

This is a work in progress. Here’s a non-exhaustive to do list:

• Texture filtering
• Normal mapping
• Textures in the raytracer
• Reflection in the rasterizer
• Expand the CSG and Refraction sections into chapters
• Linear algebra appendix
• Make demos interactive
• Clean up demo code

Feel free to contribute or to correct mistakes - all the text, diagrams and demos are in Github. PRs welcome!