|
Okay, now that we are all excited about making a 3D graphics engine, where do we start? Well, the first thing that we need is to be able to position our objects in the virtual world. If we don't know where things are, we will not be able to draw them. We will use the Cartesian coordinate system to describe positions in space.
The yellow tick marks on the diagram show a general idea, these are measured axises. You can measure along each axis a specified number of units to get to where you are going. Because each of the axis is perpendicular to each other, you can specify each 3D point with one unique coordinate triple. Each point has one and only one coordinate.
|
To the right is one example, the red point. As you can see, it is located at point (3,3,0). This means that is is three units along the X axis, three units along the Y axis, and zero units along the Z axis. The orange arrow shows an alternative notation that is will be useful soon. It is vector notation. Instead of thinking of the point as a point, we think of it as a position vector. In practice, both are stored as (X,Y,Z) coordinates, but using vector ideas will make future ideas easier to understand.
|
Before we dive into vector manipulations, we need some notation to deal with them. Here is how we will define a vector:
This shows that each vector has three components, seperated by commas. Notice that there is a difference between vector and scalar numbers. A scalar number is just a number, such as 1 or 5 or 10.2. A vector is a three dimensional object. To describe a vector requires the three scalar values: X, Y, and Z.
The main purpose of this article is to familiarize you with vectors and manipulations applicable to them. In the next article, we will start off with this foundational knowledge and start coding. Before we can really understand what is going on though, we must know the basics. Here are the main manipulations that we will want to do to vectors:
Vectors are a very nice mathematical idea. Understanding them is key to understanding 3D graphics. A vector is really a simple thing. It consists of a head, denoted with the arrow and a tail. A vector can be moved around in three space without changing it's properties. Because of this, we only need to specify where the head of the vector is. The tail then, we can assume is at the origin.V = (x, y, z)
The dot product (which is very hard to draw :) is a very important operation in 3D graphics. The mathematical definition is shown below:
U*V = UxVx + UyVy + UzVz
U*V = |U||V|cost (t is the angle theta between the two vectors)
As you can see, the operation is denoted with the * operator. It takes two vectors as input and returns a scalar value as output. What is it used for? Well, one very important thing is that we can find the angle between two vectors with it:
(UxVx + UyVy + UzVz)/(|U||V|) = cost
cos-1((UxVx + UyVy + UzVz)/(|U||V|)) = t
Finding the angle between two vectors will be important when we need to do backface culling, and when we need to light our surfaces. In these cases, both of the vectors will be unit vectors, so the division drops out (|U||V| = 1). This makes it a very fast test.
Also with the dot product, you can find the length of a projection of one vector on another. The picture to the right shows projAB. This stands for "The projection of A onto B". A projection can be thought of as the shadow cast onto B by A. The math behind this is:
U*V |projUV| = --- |V| |
Okay, from this you can derive the entire vector. To get the vector, you simply multiply this by the unit vector in the direction of V. The projection would be this then:
V U*V U*V |projUV| = --- * --- = V ---- |V| |V| |V|2 |
Projections can be used to calculate things such as shadows, and other goods stuff. Mainly though, we will be using the dot product to find the angle between vectors.
Cross Product:
As you can see, this is a very ugly equation. Where did it come from? Well if you are a matrices kind of person, it was calculated by taking the determinate of the matrix to the right. (With the i, j, and k seperating the individual components of the result). If you aren't a matrix kind of person, don't worry about it... we will just use the equation.
This formula is VERY useful for calculating the normal vector to two other vectors. "Normal" in this sense means perpendicular. If you plug two vectors into this equation, you get back the vector that is 90 degrees to both of the original two vectors. Something that you might find useful to know is the the length of the cross product is equal to the area of the parrellelagram formed by the two original vectors. Although we won't use this part of the identity. It could be useful for finding out the area of a polygon.
One very important use for the cross product will to find the normal vector of a plane. The normal vector to a plane is one that sticks straight out of it. We will find this by taking two vectors that run along the plane, and computing the cross product of them. This normal vector will be quite useful for things like shading the surface and determining visibility.
Finally, the last major piece of foundation before we can start coding. The Cross product is undeniably the hardest one of these identities to calculate, but it can also be the most useful. The cross product's operator is the "x" operation. Here is the definition:
UxV = (UyVz - UzVy, UzVx - UxVz, UxVy - UyVx)
Today we covered a lot of theoretical stuff that will be very important when we start coding in the next article. I hope that you fully understand the importance of vectors and some fundemental operations on them. Next article, I promise, we will start coding some 3D stuff. Until then, read through this thoroughly and make sure that you understand it.