3D projection
Encyclopedia : 3 : 3D : 3DP : 3D projection
The following algorithm was a standard on early computer simulations and videogames, and it is still in use with heavy modifications for each particular case. This article describes the simple, general case.
Data necessary for projection
Data about the objects to render is usually stored as a collection of points, linked together in triangles. Each point is a set of three numbers, representing its X,Y,Z coordinates from an origin relative to the object they belong to. Each triangle is a set of three such points. In addition, the object has three coordinates X,Y,Z and some kind of rotation, for example, three angles alpha, beta and gamma, describing its position and orientation relative to a "world" reference frame.
Last comes the observer (or camera). The observer has a set of three X,Y,Z coordinates and three alpha, beta and gamma angles, describing the observer's position and the direction in which it is pointing.
All this data is usually stored using floating point values, although many programs convert them to integers at various points in the algorithm to speed up the calculations.
- Warning: The author has used the × symbol to denote multiplication of matrices, e.g. `A×B' to mean `A times B'. This is confusing since in this field this symbol is instead used to indicate the cross product of vectors, i.e. `A×B' usually means `A cross B'. The two operators are different. In matrix algebra A times B is usually written as `A B'. I have NOT edited the rest of the document to reflect this.
First step: world transform
The first step is to transform the point's coordinates, taking into account the position and orientation of the object they belong to. This is done using a set of four matrices: (The matrix we use is column major, i.e. v' = Matrix*v, the same in OpenGL but different in Directx)
- [\begin1 & 0 & 0 & x \\0 & 1 & 0 & y \\0 & 0 & 1 & z \\0 & 0 & 0 & 1 \end] — object translation
- [\begin1 & 0 & 0 & 0 \\0 & \cos \alpha & -\sin \alpha & 0 \\0 & \sin \alpha & \cos \alpha & 0 \\0 & 0 & 0 & 1 \end] — rotation about the x-axis
- [\begin\cos \beta & 0 & \sin \beta & 0 \\0 & 1 & 0 & 0 \\-\sin \beta & 0 & \cos \beta & 0 \\0 & 0 & 0 & 1\end] — rotation about the y-axis
- [\begin\cos \gamma & -\sin \gamma & 0 & 0 \\\sin \gamma & \cos \gamma & 0 & 0 \\0 & 0 & 1 & 0 \\0 & 0 & 0 & 1\end] — rotation about the z-axis.
Note that unlike multiplication between numbers, the order used to multiply the matrices is significant; changing the order will change the results too. When dealing with the three rotation matrices, a fixed order is good for the necessity of the moment that must be chosen. The object should be rotated before it is translated, since otherwise the position of the object in the world would get rotated around the centre of the world, wherever that happens to be.
World transform = Translation × Rotation
To complete the transform in the most general way possible, another matrix called the scaling matrix is used to scale the model along the axes. This matrix is multiplied to the four given above to yield the complete world transform. The form of this matrix is:
- [\begins_x & 0 & 0 & 0 \\0 & s_y & 0 & 0 \\0 & 0 & s_z & 0 \\0 & 0 & 0 & 1 \end] — where sx, sy, and sz are the scaling factors along the three co-ordinate axes.
World transform = Translation × Rotation × Scaling
(as in some computer graphics book or some computer graphic programming API such as Directx, it use mactrics with translation vectors in the bottom row, in this scheme, the order of matrices would be reversed.)
- [\begins_x\cos \gamma \cos \beta & -s_y\sin \gamma \cos \beta & s_z\sin \beta & x \\s_x\cos \gamma \sin \beta \sin \alpha + s_x\sin \gamma \cos \alpha & s_y\cos \gamma \cos \alpha - s_y\sin \gamma \sin \beta \sin \alpha & -s_z\cos \beta \sin \alpha & y \\s_x\sin \gamma \sin \alpha - s_x\cos \gamma \sin \beta \cos \alpha & s_y\sin \gamma \sin \beta \cos \alpha + s_y\sin \alpha \cos \gamma & s_z\cos \beta \cos \alpha & z \\0 & 0 & 0 & 1\end] — final result of Translation × x × y × z × Scaling.
Second step: camera transform
The second step is virtually identical to the first one, except for the fact that it uses the six coordinates of the observer instead of the object, and the inverses of the matrixes should be used, and they should be multiplied in the opposite order. (Note that (A×B)-1=B-1×A-1.) The resulting matrix can transform coordinates from the world reference frame to the observer's one.
The camera typically looks in its z direction, the x direction is typically left, and the y direction is typically up.
- [\begin1 & 0 & 0 & -x \\0 & 1 & 0 & -y \\0 & 0 & 1 & -z \\0 & 0 & 0 & 1 \end] — inverse object translation (the inverse of a translation is a translation in the opposite direction).
- [\begin1 & 0 & 0 & 0 \\0 & \cos \alpha & \sin \alpha & 0 \\0 & -\sin \alpha & \cos \alpha & 0 \\0 & 0 & 0 & 1 \end] — inverse rotation about the x-axis (the inverse of a rotation is a rotation in the opposite direction. Note that sin(−x) = −sin(x), and cos(−x) = cos(x)).
- [\begin\cos \beta & 0 & -\sin \beta & 0 \\0 & 1 & 0 & 0 \\\sin \beta & 0 & \cos \beta & 0 \\0 & 0 & 0 & 1\end] — inverse rotation about the y-axis.
- [\begin\cos \gamma & \sin \gamma & 0 & 0 \\-\sin \gamma & \cos \gamma & 0 & 0 \\0 & 0 & 1 & 0 \\0 & 0 & 0 & 1\end] — inverse rotation about the z-axis.
- Camera transform = inverse rotation × inverse translation
- Transform so far = camera transform × world transform.
Third step: perspective transform
The resulting coordinates would be good for an isometric projection or something similar, but realistic rendering requires an additional step to simulate perspective distortion. Indeed, this simulated perspective is the main aid for the viewer to judge distances in the simulated view.
A perspective distortion can be generated using the following 4×4 matrix:
- [\begin1/\tan\mu & 0 & 0 & 0 \\0 & 1/\tan\nu & 0 & 0 \\0 & 0 & \frac & \frac \\0 & 0 & 1 & 0\end]
F is a positive number representing the distance of the observer from the front clipping plane, which is the closest any object can be to the camera. B is a positive number representing the distance to the back clipping plane, the farthest away any object can be. If objects can be at an unlimited distance from the camera, B can be infinite, in which case (B + F)/(B − F) = 1 and −2BF/(B − F) = −2F.
If you are not using a Z-buffer and all objects are in front of the camera, you can just use 0 instead of (B + F)/(B − F) and −2BF/(B − F). (Or anything you want.)
All the calculated matrices can be multiplied together to get a final transformation matrix. One can multiply each of the points (represented as a vector of three coordinates) by this matrix, and directly obtain the screen coordinate at which the point must be drawn. The vector must be extended to four dimensions using homogeneous coordinates:
- [\beginx' \\y' \\z' \\\omega' \\\end=\begin\end \times \begin\end \times \begin\end \times \beginx \\y \\z \\1 \\\end.]
Remember that are the final coordinates, where is typically the bottom left corner of the screen, is the top right corner of the screen, is the bottom right corner of the screen and is the top left corner of the screen.
If using a Z-buffer, a z
Objects should only be drawn where −1 ≤ z
Simple version
- [X_} = X_} - \frac} + \mathrm} \times X_}]
- [Y_} = Y_} - \frac} + \mathrm} \times Y_}]
See also
- Computer graphics
- 3D computer graphics
- Graphics card
- Transform and lighting
- Texture mapping
- Perspective distortion
From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.
