Vol. 3 Iss. 1 - April 2001

Graphics Coding, Part 5
3D Rotations and Backface Culling

By Sane <sane@telia.com>

Note: I must confess that this article, in its original state, was one of my worst, if not the worst article I've ever done gramatically. Thanks go to Freedy for doing a load of corrections. In fact he needed to make corrections to most of the sentences.

Welcome folks, to the fifth part of this series. I don't remember exactly what I said I'd write in this part, and I'm too busy (a.k.a lazy) to find out since I'm writing an article. (duh)

Anyways... I know what I think I should be writing about; 3D rotations, backface culling and maybe something else I'll come up with as I write.

3D rotations

I don't think I need to tell you why 3D rotations are good, so I won't (email me if you still need reasons though). As you might already know if you've got some graphics programming experience, the following is used to rotate a point around coordinate (0,0) in 2D space:


Since angle is actually representing the Z rotation angle, it's not all that hard to use the code to rotate around other axes by just changing place of the axes.
Here's how you do it:

Rotation around the Z axis:


Rotation around the X axis:


Rotation around the Y axis:


Of course you don't really need to write the code for the variables that don't change, I just wrote it cause it might help some of you understand it better.

The rotation that the previously shown code does is always around coordinate (0,0,0).

Now, you might wonder what you should do if you want to rotate something around another point. The answer is actually quite simple: If it isn't 0, make it 0! By that I mean that when you rotate some stuff you must first translate it (that is you subtract or add as needed) the point's coordinates so that the point you want to rotate around is mapped to 0,0,0. You do that by subtracting and adding before you rotate, and after you have done the rotation you just add what you subtracted and subtract what you added before.

If all else fails, you may understand how it's done by looking at this picture:

Backface culling

Backface culling is a way to avoid drawing the faces (polys) that are turned away from you. There are several reasons why that's good, but one main reason is that it'll realy decrease the amount of polys drawn. 'Cause of that, the program will run faster, and in most cases; a lot faster.

The difference between polys that are considered to be turned towards you and ones that aren't is the order of the points; clockwise or counter-clockwise. They will be turned towards or away from you, as they should when you rotate them, in this image you'll see why:

Here's some code for only drawing polys that have their vertices ordered in a counter-clockwise order(thanks go to Qasir who originally gave this code to me):

IF (y1-y2)*(x2-x3)<(x1-x2)*(y2-y3) THEN
 'Draw the stuff here

Note that the X and Y values need to be relative to the point of view for the code to work as it should, so if you have some kind of a camera implementation you should calculate the X and Y values according to the camera view. Next time I'll write about how to load 3D models, and maybe some stuff about animation or something. If there's anything special you want me to write about, always remember to email me at sane@telia.com.