Introduction

Learn how to program in 3D computer graphics in Python!

frustum1

Source code

This book references source code, which is at here

Approach

This book uses “mistake-driven-development”; instead of listing math formulae in their final form, this book incrementally shows, via working code, how to build complex graphics applications, making mistakes along the way, and then fixing the mistakes.

Thoughout the book, you will learn how to place objects in space, how to draw objects relative to other objects, how to add a camera which moves over time based on user input, and how to transform all the objects into the 2D pixel coordinates of the computer screen. By the end of this book, you will understand the basics of how to create first-person and third-person applications/games. I made this book to show programmers how to make the kind of graphics programs which they want to make, using math they mostly aleady know.

This book is purposely limited in scope, and the applications produced are not particurly pretty nor realistic-looking. For advanced graphics topics, you’ll need to consult other references, such as the OpenGL “red book” and “blue book”, or the following tutorials

  1. logl

  2. oglt

Although this book fills a huge gap that other books do not address, those other books are excellent reference books for advanced topics.

Pre-requisities

  1. Basic programming concepts in Python.

    1. YouTube videos

      1. Learn Python with Socratica

      2. Microsoft Python Tutorials

    2. Books

      1. https://en.wikibooks.org/wiki/A_Beginner%27s_Python_Tutorial

      2. https://learnbyexample.github.io/100_page_python_intro/preface.html

      3. https://diveintopython3.problemsolving.io/

  2. High school trigonometry

  3. Linear Algebra (optional)

    1. 3Blue1Brown - Linear Transformations

    2. 3Blue1Brown - Matrix Multiplication as Composition

Required Software

You will need to install Python. https://en.wikibooks.org/wiki/A_Beginner%27s_Python_Tutorial/Installing_Python

Before running this code, you need a virtual environment, with dependencies installed. https://docs.python.org/3/tutorial/venv.html

On Windows, if you use the Developer command prompt, run

python -m venv venv
cd venv\Scripts
activate.bat
cd ..\..\
python -m pip install --upgrade pip setuptools
python -m pip install -r requirements.txt

If you use Visual Studio on Windows, it takes care of this for you.

On a Mac or on Linux, run

python3 -m venv venv
source venv/bin/activate
python3 -m pip install --upgrade pip setuptools
python3 -m pip install -r requirements.txt

Windows

Use Visual Studio 2019 (Tested on community, but I’m sure it will work on others).

Linux

Install Python3, glfw via a package manager. Use pip and virtualenv to install dependencies

Mac

Python Python3 (via anaconda, homebrew, macports, whatever), and use pip and virtualenv to install dependencies.