3D Transformations
3D Transformations
Home at http://3Dblackhole.base.org
NOTE: Some part of functions and some members of structures are not
explained here, but we'll discuss them in other tutorials of the series.
They're usually displayed in totality for the sake of similarity between the
different tutorials and with the complete 3D engine.
----------------------------------------------------------------------------
* Saving coordinates
* Implementing a matrix system
* Implementing a trigonometric system
* Creating the transformation matrices
* How to create perspective
* Transforming objects
Saving coordinates
It's now time to start coding some starfield simulator... So what will be
our fundamental structure, in which the description of every object will be
stored? To answer this question, we'll ask another one: What are the types
of coordinates we need? The most obvious are:
typedef struct
{
short x, y;
}_2D;
typedef struct
{
float x, y, z;
}_3D;
And here comes the structure for a set of coordinates that we'll call
vertex, since the word "vertex" refers to the meeting of two or more edges
of a poyhedron. Our vertices can simply be regarded as vectors described
with different systems.
typedef struct
{
_3D Local;
_3D World;
_3D Aligned;
}Vertex_t;
We're gonna store all our matrices in 4x4 array of floating-point numbers.
So we'll have this matrix when we'll need transfromations:
float matrix[4][4];
Quite simple? Now, the big part with matrix, multiplying two matrices
together. It's now time to try to understand the big formula in the maths
tutorial, with this code:
Did you get it now?? Isn't it way clearer? Now let's do the product of a
vector by a matrix:
I know almost every C compiler in the world comes up with a maths library
with trigonometric functions, but don't ever use them every time you need
one simple sine!! The computation of sines and cosines is a multitude of
factorials and divides! Build yourself trigonometric tables instead. First
decide the number of degrees you want, then allocate some place to hold the
values:
Then use a macro that will bother about putting every degree positive, and
doing the wrap-around when it goes over the number of degrees, then return
you the good value. If you use a number of degree which is a power of two,
you can use a "&", which is much faster, instead of a "%" to do so. For a
256-degrees based trigonometric system:
void M3D_Init()
{
int d;
for(d=0; d<256; d++)
{
SinTable[d]=sin(d*PI/128.0);
CosTable[d]=cos(d*PI/128.0);
}
}
MAT_Mult(matrix,ymat,mat1);
MAT_Mult(mat1,xmat,mat2);
MAT_Mult(mat2,zmat,matrix);
}
How can you create the illusion of something seeming close to you, and
something far away on a 2D screen?? The question of perspective has always
been a problem for artists and programmers. Different methods has been used.
But as strange as it can seem, the most realistic one only consist in a
simple division. Here's the formula we will use to project our 3D world on a
2D screen:
P( f ):(x, y, z)==>( fx / z + XOrigin, fy / z + YOrigin )
Here f is the "focal distance". It represents the distance from the viewer
to the screen, and is usually between 80 and 200. XOrigin and YOrigin are
the coordinates of the center of the video display. You want to know what a
Project function would look like?
You like the line z = 1?? I hate divides by zero... They're everywhere...
Transforming objects
Now that you have all the necessary tools to transform vertices, you should
know the main steps you need to execute.
----------------------------------------------------------------------------
E-mail me at [email protected]
Back to Jerome St-Louis's Homepage
Back to The 3D Coding BlackHole