Blitz Basic Course

3D Fundamentals


The main parts of the program that are worth discussing about are the projection routine and the code that performs the rotation.

Let's start with the projection part: the idea comes from a friend of mine (Giorgio Fornara). The diagram that you got with this article will help (look at it now). The idea is very easy (but ingenious): when you draw a 3-dimensional object on a sheet of paper you have already done the projection you are searching for because the sheet of paper is 2-dimensional!

I'll start with the diagram description: the three blue axis are the ones of our 3-dimensional space, the two black axis are the axis of the 2-dimensional screen (well, to be precise the Y axis must be reversed in sign because the Y screen axis usually points downward). The red lines form our object (for the sake of clarity I've not drawn the hidden lines) and the green lines are the 2-dimensional coordinates of the point P we are searching for. In the program the two angles alpha and beta are both equal to 30 degrees, but they need not be the same.

From now on I'll refer to the screen coords with the uppercase letters X and Y, and to the 3-space coords with lowercase letters x, y and z; the Y coordinate of P is written P_Y, the y coordinate of P is represented by P_y, and so on... Now the task is easy: P_Y is just equal to P_z minus the segment given by P_x * Sin(beta) (the one delimited by the blue dashed lines) minus the other little segment given by P_y * Sin(alpha). In a similar way we determine the other coordinate: P_X is equal to P_x * Cos(beta) minus P_y * Cos(alpha). So we have the following relations:

   P_X =       P_x * Cos(beta) - P_y * Cos(alpha)

   P_Y = P_z - P_x * Sin(beta) - P_y * Sin(alpha)

To obtain the formulas used in the program you have only to subtract these quantities from the coordinates of the center of the screen.

By the way, I successed in drawing the hidden edges with dashed lines using a (subtle) trick. The trick works only if you have only one hidden vertex at a time: you compute which vertex has the most negative sum of its three coords P_x + P_y + P_z and draw every line departing from this vertex with a dashed pattern. I'm still searching for a generalization of this idea to allow many points to be hidden at the same time (if you have any suggestion I have no E-Mail address but you can write to Fabio Soft at fsoft@intercom.it)

And now we come to the routine that performs the rotations. The formulas used here are only the product of the rotation matrix about the z axis times the rotation matrix about the x axis. Namely: the rotation about the z axis is represented in linear algebra notation by the matrix


   |  Cos(theta)     Sin(theta)     0  |
   |                                   |
   |  -Sin(theta)    Cos(theta)     0  |
   |                                   |
   |  0              0              1  |

where theta is the angle representing the amount of the rotation; the rotation about the x axis is represented by the matrix

   |  1     0              0           |
   |                                   |
   |  0     Cos(phi)       Sin(phi)    |
   |                                   |
   |  0     -Sin(phi)      Cos(phi)    |

where phi is the angle representing the amount of the rotation. Now we perform the product (rows times colums, as usual) of these two matrices and obtain:

         |  Cos(theta)     Sin(theta)Cos(phi)      Sin(theta)Sin(Phi)   |
         |                                                              |
   R =   |  -Sin(theta)    Cos(theta)Cos(phi)      Cos(theta)Sin(phi)   |
         |                                                              |
         |  0              -Sin(phi)               Cos(phi)             |

If the new coords are (x', y', z') and the old ones are (x, y, z) we have

   |  x' |        |  x  |
   |     |        |     |
   |  y' |  =  R  |  y  |
   |     |        |     |
   |  z' |        |  z  |

that is to say

x' =   x*Cos(theta) + y*Sin(theta)Cos(phi) + z*Sin(theta)Sin(Phi)
y' = - x*Sin(theta) + y*Cos(theta)Cos(phi) + z*Cos(theta)Sin(phi)
z' =                - y*Sin(phi)           + z*Cos(phi)

and these are the formulas you can see in the program.

Well, I have chosen to rotate my cube around the x and z axis, but you can perform rotations around whichever axis you like by multipling the correct matrices and linking the angles to mouse movements.

The rest of the code is devoted to opening the window, reading the input and writing the output: remarks about this operations are written in the program itself.


Click here to see the code.

Main Page


    Written By: Andrea Galimberti   e-mail: fsoft@intercom.it
                Via E.Villoresi
                Turbigo (MI)
                ITALY               tel:    (ITA) - (0)331 - 871009