back

3D Programming

Disclaimer and introduction

I have no experience in 3D Game Programming. I have very little experience in 3D at all, except what little I learned a long time ago and have used every once in a while since then. So please do not expect this to be a revelation, this is soemthing I write for my own memory while I'm reading a book on the subject.

The book in question is 3D Game Engine Design by David H. Eberly (ISBN1-55860-593-2).

So if you really want to learn 3D Game Engine Design, go read the book. This is just a text that I write to try to understand and remember what the book says.

Vector Algebra

3D Programming is so much Vector Algebra and Linear Algebra it's going to blow your mind. You better have studied this at a university level or you're never going to understand why you're doing any of this. Sure, you might memorize the how's, but the why's are always going to be beyond your reach.

The basic problem we're trying to solve in 3D Programming is what to do with a cube, which obviously has 3 dimensions (width, height and depth) so that it will appear on a screen which has only 2 dimensions (width and height). Of course, most games on the market do not comprise of a flying cube being attacked by other different-sized cubes, shooting cubes at eachother, so obviously the cube can also be a much more complex 3D shape.

Transformations

So to make a game work, there are some transformations that need to be performed. If you think about it, they all make sense. We also try to represent all transformations as linear algebra matrix equations. Preferrably these equations should be multiplications. The reasons for this might become clear later. I sure don't remember why right now. :) First there is the scaling operation.

Scaling

Scaling is needed to make things shrink or enlarge. Very useful if you're going to create a world with different sized-stones, for example. Instead of creating 100 different stones of all sizes, make just 2 or 3 and then resize them to fit the map. No one looks at stones anyway (unless they are The Stones and I think would be mad at us for shrinking them). I bet some other uses than this also come to mind, just think about it.

A scaling matrix is a diagonal matrix where each element on the diagonal represents how much we want to scale x, y and z (width, height and depth) respectively. Normally you want to scale them all the same, which makes the scaling matrix rather simple:
D=sI
Where D is the scaling matrix, s is a scalar value, I is the identity matrix. Just multiply this matrix with your vector and your vector will be scaled by s.

Rotation

This one is trickier. Without any proof, the book just says that a rotational matrix, is such a matrix if the inverse of the matrix is the same as the tranposition of that matrix. This means that R-1=RT and also that RRT=I.

One can view a rotations as being a vector U=[u0,u1,u2] and an angle theta. The vector represents a line through space which is the axis around which the object or world is rotated theta degrees. Most likely, though the book does not mention this, the vector should be a normal vector, that is, its length must be 1, which would preserve the lengths of all the transformed vectors and only rotate them.

In this described view, we get a matrix S:

S=[ 0u2-u1 ]
-u20u0
u1-u00

and a rotational equation as such:
R=I+(sin(theta))S+(1-cos(theta))S2
where R is the rotational matrix. Multiply this matrix with a vector and the result is the rotated vector.

forward
Written 2004-10-04 by Daniel Hellsson
Copyright (C) 2004 by Unseen Technology
All rights reserved