Preface

In this tutorial I would like to show how 3d graphics is done today, why it's important, and how it will change the way you see 3d graphics applications.

To better understand, we'll end up creating a 3d engine with Python.

Requirements

I expect you to be familiar with Python, if you understand what class means you're probably qualified in this department.

I also expect you to understand what the terms fov (field of view), vertex, mesh..etc. mean.

But 3d graphics is hard!

No, it's not. This is an awesome area of programming you'll be able to show to your friends, there will be math(s) involved, so strap in, but it'll all be explained. If there are aspects you do not understand, simply copy my solution.

Fundamentals

To start, let's go through the basic building blocks.

Imagine we have a simple object, a cube.

There's more going on under the hood, this cube is made up of two things, vertexes and triangles.

Vertexes are essentially points in 3d space. Look around your room, and imagine a speck of dust, a single point in 3d space.

Triangles are, well just triangles, normal 2d flat triangles. However their three points are connected to specific vertexes.

Let's look at the vertexes.

On the above image of a cube, you can see there are eight points, these are the points which make up the cube.

In memory, these points each have a 3d coordinate: X, Y, Z axis. however when we go to render the cube, we map each 3d coordinate to 2d screen space. And the way we do that is surprisingly simple.

Next, let's look at the triangles.

As you can see, a triangle is now connected to three of the points. Do this 12(*) times and you'll get a cube.

*: A cube is made up of 6 faces, however to make a face with a triangle, you must use two triangles, so it ends up being 12 triangles.

Enough "fundamentals", more coding!

Alright, now that we understand the basic structure for rendering 3d shapes. Let's get more technical. We'll be doing this in Python (with Turtle).

First, we import Turtle, I will assume you already know how to use Turtle and are familiar with it's functionality. In short, it's just a graphics library aimed at kids learning how to code with graphics, and making flowers and all sorts of things... Except we'll be going much further than flowers.

import turtle

Next we need to store our object data. We need to store all our vertexes and triangles.

The camera

Alright, I won't go into the camera in this tutorial, you can look at my repl at the bottom to see how to properly implement a 3d engine, but we're taking baby steps here.

When we think of moving the camera, we think of the camera object moving, simple right? Well that's not easy to implement in a rasterized renderer. However what's easier is to move the world around it. Think about it, either you can move the camera, or move the world; it's the same effect.

As it turns out, it's a lot easier to offset the vertex positions than somehow change the perspective formula to equate the position; it would be a whole lot more complex.

So quickly solve this, let's move the camera out:

# Perspective formula,
z += 5
f = FOV / z
sx, sy = x * f, y * f

And adjust the FOV to say, 400.

Nice!

Drawing triangles

To draw triangles, consider this code. By this point you should be able to understand it: