I got a lot of feedback from the first article, much of it complaining about its simplicity. Well, soon you'll be screaming at me to go back to the simple stuff. This time, I'll be discussing basic vector math and we will also be taking a look at planes. The following information is the kind of stuff that you need to understand if you wish to succeed in the world of 3-D graphics. So listen up well, and soon you'll be doing all kinds of neat things.
[size="5"]What is a Vector?
When I discussed points I mentioned that a vector consisted of similar properties to a point. Well here is the point structure again.
struct Point
{
float x;
float y;
float z;
};
struct Vector
{
float x;
float y;
float z;
};
When we speak of magnitude we speak of length, and direction obviously means which way the vector is pointing.
Figure 1. A coordinate system with a vector
Imagine that each axis on this coordinate system is 5.0 units in length. You can see our vector < 2.0[sub]x[/sub], 4.0[sub]y[/sub], 0.0[sub]z[/sub] >, which is pointing 4.0 units up the y axis and 2.0 units across the x axis. How long is it? Let's find out.
[size="5"]How long is your Vector?
In many vector operations which are performed, prior knowledge of the length of the vector is required. To find the length of a vector you take the square root of the sum of squares of each vector component as shown below.
|V| = sqrt( x[sup]2[/sup] + y[sup]2[/sup] + z[sup]2[/sup] )
Note that our vector V is denoted using the | | symbol. This means that we are referring to the vector's length. Using the above formula our code may look something like this:
inline float Length( Vector &v )
{
return( sqrtf( (v.x*v.x) + (v.y*v.y) + (v.z*v.z) ) )
}
[size="5"]Normalized Vector
Also known as the unit vector, a normalized vector's length is equal to exactly 1. To find the normal vector we must calculate the length, and then divide each component of the vector by that length.
Normal = V/ |V|
V = ( 3.0[sub]x[/sub], 6.0[sub]y[/sub], 2.0[sub]z[/sub] )
|V| = 7.0
V.x /= |V|
V.y /= |V|
V.z /= |V|
V.x = 0.428
V.y = 0.857
V.z = 0.285
|V| = 1.0
So we have our vector V with its values described above. We calculate the magnitude of the vector and we find that its length is 7.0. Following that we divide each component by the magnitude. If we calculate the length of the vector again we now find that it is equal to 1.0, the very value which makes it a normal vector. Remember that although the vector's length may now be equal to 1.0 its direction is still the same. Here is a way we can do it in code.
inline Vector Normalize( Vector &v )
{
float temp = 1/Length( v );
v.x *= temp;
v.y *= temp;
v.z *= temp;
return( v );
}
[size="5"]Vector Operations
We can perform operations on vectors like we would on our everyday numbers such as addition, subtraction, and multiplication (though multiplication has two different interpretations).
[size="3"]Addition
inline Vector AddVect( Vector &v1, Vector &v2 )
{
Vector v;
v.x = v1.x + v2.x;
v.y = v1.y + v2.y;
v.z = v1.z + v2.z;
return( v );
}
[size="3"]Subtraction
inline Vector SubVect( Vector &v1, Vector &v2 )
{
Vector v;
v.x = v1.x - v2.x;
v.y = v1.y - v2.y;
v.z = v1.z - v2.z;
return( v );
}
[size="3"]Multiplication with a Scalar Number
To increase the length of a vector we can multiply with a scalar number.
inline Vector ScaleIncrease( Vector &v, float &s )
{
v.x *= s;
v.y *= s;
v.z *= s;
return( v );
}
To decrease the length of a vector we can divide with a scalar number.
inline Vector ScaleDecrease( Vector &v, float &s )
{
v.x /= s;
v.y /= s;
v.z /= s;
return( v );
}
The Dot Product is a vector operation which will return a scalar value (single number), which for unit vectors is equal to the cosine of the angle between the two input vectors (for non-unit vectors, it is equal to the length of each multiplied by the cosine, as shown in the equation below). We can represent the Dot Product equation with the ? symbol.
U ? V = |U| * |V| * cos(q)
alternatively
( x1, y1, z1 ) ? ( x2, y2, z2 ) = x1y1 + x2y2 + z1z2
In Figure 5, there are two vectors pointing in different directions. Where those vectors meet you can find the dot product. Before we do anything else let's take a look at the dot product code.
inline float DotProduct( Vector &v1, Vector &v2 )
{
return( (v1.x*v2.x) + (v1.y*v2.y) + (v1.z*v2.z) );
}
It won't take long in your exploration of 3-D graphics before you find yourself using the dot product frequently. Among other things, it is useful in backface culling, lighting and collision detection.
[size="5"]Cross Product
Another useful vector operation is the cross product. Unlike the dot product which returns a scalar value the cross product actually returns a vector. The vector which is returned is perpendicular to the two input vectors.
inline Vector CrossProduct( Vector &v1, Vector &v2 )
{
Vector v;
v.x = ( v1.y * v2.z ) - ( v1.z * v2.y );
v.y = ( v1.z * v2.x ) - ( v1.x * v2.z );
v.z = ( v1.x * v2.y ) - ( v1.y * v2.x );
return( v );
}
Figure 6. Vector calculated using the cross product
[size="5"]What is a Plane?
A plane is like a huge piece of paper. Each triangle making up our models lies in its own plane.
ax + by + cz + d = 0
The above equation describes our plane. The < a, b, c > triplet describes the normal of our plane. The normal is the vector which is perpendicular to all of the points which lie on the plane. We learned how to calculate the normal when we were learning about the cross product. The d component is a scalar value which represents the distance from the plane to the origin < 0[sub]x[/sub], 0[sub]y[/sub], 0[sub]z[/sub] >. The triplet < x,y,z > is the point which solves the equation. You will understand this in a moment.
In this diagram, the normal points away from the origin, so the distance is negative. If the normal pointed toward the origin, the distance would be a positive value. Obviously if the plane goes through the origin then the distance would be equal to zero.
[size="5"]Constructing a Plane
// global variables
Vector norm;
float distance;
void ConstructPlane( Vector &p0, Vector &p1, Vector &p2 )
{
norm = CrossProduct( SubVect(p2 ,p1), SubVect( p0,p1) );
norm = Normalize(norm);
distance = -DotProduct( norm, p1 );
}
[size="5"]Defining the location of a point in relation to a plane
Defining the location of a point in relation to a plane is one of the most common operations we can do with planes. There are three possible cases when perfoming this operation. They are: front of the plane, back of the plane, and coplanar with the plane. So, how do we tell the difference between the front of the plane and the back of the plane? The front is defined as the side which the normal sticks out of, so obviously the back of the plane is the opposite side.
Figure 8. Point Classification
V ? N + D
Looking at Figure 8, we can see that vector V is at the back of the plane. How did we come to this conclusion? We find the dot product of our vector V with the normal and add the distance. If we get a negative value returned then the point is behind the plane and if we get a positive value, the point is in front of the plane. If the point lies on the plane then it is coplanar.
The returned value is the distance from the plane. Remember that if that value is equal to zero than we are indeed coplanar with the plane.
[size="5"]Conclusion
If you have made it this far then well done. Compared to what we have ahead of us this stuff is relatively simple but those who haven't studied vector math before may find it a little confronting. Don't worry to much, once you start using this stuff more everything will fall into its place. Next time we dwelve into the mysteries of the matrix. Well I guess this is goodbye, I'll see you next time.
Love me? Hate me? Questions? Suggestions? How about you e-mail me [email="bully@myrealbox.com?subject=TW3DG_2"]here[/email].