Prelude to a Greater UnderstandingI 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 3D graphics. So listen up well, and soon you’ll be doing all kinds of neat things. 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; }; And introducing our vector structure… struct Vector { float x; float y; float z; }; "Holy Jamoly", I can hear you say, "They look exactly the same". Well, yes that is true, but where they differ is in what they represent mathematically. Remember a point is used to reference a particular position in our coordinate system. Well rather than position, a vector represents two new concepts: magnitude and direction. When we speak of magnitude we speak of length, and direction obviously means which way the vector is pointing.
Imagine that each axis on this coordinate system is 5.0 units in length. You can see our vector < 2.0_{x}, 4.0_{y}, 0.0_{z} >, 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. 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^{2} + y^{2} + z^{2} ) 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) ) ) } Using the vector in Figure 1, we will find the length. The vector length equation is perfomed on our vector < 2.0_{x} , 4.0_{y}, 0.0_{z} >, and we find the length equal to 4.47214.
Normalized VectorAlso 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_{x}, 6.0_{y}, 2.0_{z} ) V = 7.0 V.x /= V
V.x = 0.428
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 ); } Notice how we cached the division. That’s a nice optimization because multiplication is faster than division.
