It gives me great pleasure to introduce Jonathan Palencia; our new technical 3D blogger. He really knows his stuff and has a plan already jotted out for the series, so I’ll leave you in his capable hands:
Welcome to 3D101! Before anything please read the following to make sure you’re on the right place; this is the first part of the 3D101 tutorial which will deal with 3D Fundamentals from scratch. If you’re attempting to build your own 3D engine or you just want to learn more about the actual underlying infrastructure, algorithms and techniques then you’re on the right place.
1. Let’s Talk About Dimensions
Now let’s begin by trying to define what this stuff really is. 3D stands for “Three Dimensional Space”, and a dimension is -mathematically speaking- the minimum number of coordinates required to describe any location in that space, and physically speaking a dimension describes the level of movement of an object on that space. Coordinates are usually written as a tuple of N-numbers within parenthesis, each coordinate means something on the specific dimension it is in. For example in 2D space as taught on high school we have the x-coordinate which stands for horizontal movement and y-coordinate for vertical movement. A location is described with this coordinates as (x, y). As an example, if we have coordinates (2,-4) it describes a point located 2 units to the right, and 4 units down.
Imagine a 1D space, as its name implies it needs one coordinate to describe any location in it, graphically speaking a 1D space is just a line because in a line you just need one coordinate (number) to describe any position in it, such as (10), or (-3). The level of movement is simply horizontal, you move to the left and to the right only. The tuple for 1D space is (x), “x” coordinate meaning horizontal movement.
Similarly a 2D space needs two coordinates to describe any location, graphically a 2D space is the Cartesian Coordinate Plane. The level of movement on a 2D space is now much more free than 1D, you can move left, right, up and down. The tuple for 2D space is (x, y) where “x” coordinate is for horizontal movement, and “y” coordinate for vertical movement.
Now that you have the idea, you can realize that a 3D space needs three coordinates, and that graphically speaking 3D is exactly the world we live in. The level of movement is amazingly fluid and free for all of us living in it, we can move left, right, up, down, back and forth. The tuple for 3D space is (x, y, z) where “x” and “y” have the same meaning as in previous spaces, and “z” now indicates “depth”.
For imagination’s sake, why don’t we take this one step further? Imagine now a 4D space, it’s harder isn’t it? We know it needs four coordinates (x, y, z, t) but what is the last coordinate for? It could be … Hmmm. Maybe time!! The “t” coordinate could specify a moment in time, meaning we can be on a location (x, y, z) at any given time (t) which means we could travel through time back and forth without issues of any kind simply by changing our “t” coordinate! Maybe some creatures somewhere in the universe are able to do this -or maybe not- but well, it feels good to imagine that it might be possible.
2. Showing 3D Objects on a 2D Screen
The display screen is a 2D surface, and we need to show 3D objects on it, How in earth can that be done? One simple word to answer all questions: Projection.
A projection is a linear transformation P from one coordinate space to another such that the source image remains as unchanged as possible. – For example, in real life light bounces off objects and that is what our eyes register, we are not seeing the entire object, but rather just the light reflected from them that falls on our retina. This partial reflection we see is a projection of the 3D object on our 2D retina. When our brain processes the images received from both eyes we get the feeling we’re seeing in 3D, but that’s only because our amazing brains are able to fill-in-the-blanks to determine the depth of what we’re seeing, this is known as depth perception.
To display 3D objects on our display screen all we need is to project 3D coordinates on a 2D plane we’d like to see. There are several equations that need to be figured out first to perform this projection, but for simplicity’s sake I’ll just write them here. If you want more information about this you can search internet for “Linear Projection”.
The following equations represent a simplified linear transformation from R3 (3D space) to R2 (2D space). Note that the equations have been slightly modified to match actual screen coordinate system (inverted Y-axis).
X2D = 0.5*ScreenWidth + Scale*(X3D / (Z3D + ZNearPlane))
Y2D = 0.5*ScreenHeight – Scale*(Y3D / (Z3D + ZNearPlane))
The ScreenWidth and ScreenHeight variables represent the resolution of the display mode currently active. Scale is a small factor used for the entire stage, a usual value is 256. And the ZNearPlane defines the Z-plane where the eye is located on (nearest plane), a regular value is 256 as well.
3. Rotations in 3D Space
Now we’re able to have a bunch of 3D points and convert them to 2D for visualization, but without the ability to rotate the 3D points all we have is an static object on the screen, with no life, and no meaning. Rotating objects in 3D space is similar to rotating in 2D, with the exception that in 2D we only have one plane (XY), and in 3D we have three planes (XY, YZ, and XZ) meaning we have to perform one rotation on one of those planes depending on the kind of movement we want.
The following equations define rotations for the X, Y and Z axis respectively. Instead of memorizing each equation, just memorize the XY-plane rotation (Z-rotation) and then try to realize how to figure out the others.
Z’ = Z*COS(Θ) – Y*SIN(Θ)
Y’ = Z*SIN(Θ) + Y*COS(Θ)
X’ = X*COS(Θ) – Z*SIN(Θ)
Z’ = X*SIN(Θ) + Z*COS(Θ)
X’ = X*COS(Θ) – Y*SIN(Θ)
Y’ = X*SIN(Θ) + Y*COS(Θ)
Using these simple equations now you can move your objects freely in any direction, then project them to 2D space and show them on your display.