DirectX 10 Game Programming : 3D Primer - Vectors

12/20/2012 3:32:00 AM

Vectors have multiple uses in 3D graphics, from describing distance and direction to speed. Unlike a point, which has only a position, a vector has both a direction and a length (magnitude), allowing it to be utilized to determine which direction a polygon is facing, which direction an object or particle is heading or just describe its position. Typically, vectors are designated as arrows with a head and tail showing the direction and magnitude. Figure 1 shows an example of a vector within a coordinate system.

Figure 1. An example vector.

Vectors typically fall into two categories, free vectors and fixed vectors. The vector shown in Figure 5.6 is an example of a free vector.

A free vector is a vector that can be placed in an arbitrary location within a coordinate system and its meaning doesn’t change.

A fixed vector remains fixed to the origin of the coordinate system. This places the tail of these vectors at the origin while the head is placed at a location in space. Because fixed vectors are centered on the origin, it allows them to be used to designate a position. This position is comprised of three scalar values called components. The number of components within the vector corresponds to the number of axes. For example, if the coordinate system is describing 3D space, then three components are needed to describe a position within it. For example, the vector <3, 2, 7> corresponds to a position 3 units away from the origin on the X axis, 2 units away on the Y axis, and 7 units away on the Z axis.

Direct3D has a few types defined in the D3DX library that are useful when working with vectors.

The D3DXVECTOR2 type is a structure containing two components, an X and a Y.

typedef struct D3DXVECTOR2 {
    FLOAT x;
    FLOAT y;

The D3DXVECTOR3 type contains three components, an X, Y, and a Z, within its structure.

typedef struct D3DXVECTOR3 {
    FLOAT x;
    FLOAT y;
    FLOAT z;

While you could define these types yourself, the Direct3D structures contain more than just the definitions of the embedded components. The structures contain functions and operator overloads relevant to the specific vector type, allowing the vectors to be manipulated easily.


If you want to see all the functionality provided the vector types by D3DX, look at the D3DX10math.h header file in the DirectX SDK.

When using vectors you’re bound to come across a few different types; here’s a small list:

  • Position Vector— A type of vector that is used to describe a position within a coordinate system, with the tail being at the origin and the head at a point.

  • Normal Vector— A vector that is perpendicular to a plane. This is useful for determining whether a polygon is front or back facing.

  • Unit Vectors— A vector that has a length of 1. Not all vectors require a large length. When creating a directional light, only the direction of the vector is important.

  • Zero Vectors— A vector with a length of 0.

Determining the Length of a Vector

Occasionally it is useful to know the length of a vector—since the length or magnitude of the vector can be used as acceleration or velocity when applied to a game object. The length is also used as an input when normalizing the vector.

To calculate the length of a vector, each of the components must be first squared and then added together. Finally, the square root of this number is taken to give the output length.

sqrt(vectorX * vectorX + vectorY * vectorY + vectorZ * vectorZ);

Direct3D provides a function called D3DXVec3Length that can be used to calculate a vector length.


Normalize a Vector

Normalizing a vector is the process of reducing any length vector into a unit vector. This is best done when only the direction of a vector is needed and the length is unimportant. Vectors can be normalized simply by dividing each of the components by the vector’s length.

vectorX = (vectorX / length);
vectorY = (vectorY / length;
vectorZ = (vectorZ / length);

The final vector will still point in the same direction, but the length of the vector is reduced to 1.

Direct3D offers a function to perform this for you called D3DXVec3Normalize.

D3DXVECTOR3 * D3DXVec3Normalize(
    D3DXVECTOR3 *pOut,

This function takes two parameters. The first parameter is a pointer to the D3DXVECTOR3 object to be filled with the normalize vector. The last parameter is the original vector.

Cross Product

A cross product of a vector is used to calculate a normal vector. A normal vector or normal is used when performing lighting to determine the orientation of a particular polygon. The polygon’s orientation is used to figure out how much light the polygon is receiving.

Normals can be calculated on a polygon or vertex basis and have vastly different visual results. Because there are more vertices in an object than polygons, calculating normals on a per-vertex basis is more complicated but yields much better visual results.

Calculating the normal vector using the cross product requires two vectors from an existing polygon. The resulting vector will be perpendicular to the input vectors.

newVectorX = (vector1Y * vector2Z) - (vector1Z * vector2Y);
newVectorY = (vector1Z * vector2X) - (vector1X * vector2Z);
newVectorZ = (vector1X * vector2Y) - (vector1Y * vector2X);

Direct3D provides the D3DXVec3Cross function for calculating the cross product.


The D3DXVec3Cross function takes three parameters. The first parameter is the output vector for the calculation. The second and third parameters are the existing polygon vectors.

Dot Product

The final vector calculation I’ll go over is the dot product. The dot product is used to determine the angle between two vectors and is commonly used for back-face culling. Back-face culling is the process by which polygons that are not visible are removed to reduce the number of polygons being drawn. If two vectors have an angle less than 90 degrees, then the dot product is a positive value; otherwise, the dot product is negative. The sign of the dot product is what’s used to determine whether a polygon is front or back facing. Polygons facing away from the viewer are not shown.

The dot product is calculated based on two existing vectors of a polygon. The components of the two vectors are multiplied and then added together to create the dot product. Before calculating the dot product, always normalize the two input vectors.

Float dotProduct = Vector1X * vector2X + vector1Y * vector2Y + vector1Z *

Direct3D contains the function D3DXVec3Dot for calculating the dot product.


The D3DXVec3Dot function takes the two input vectors as parameters.

Top 10
Review : Sigma 24mm f/1.4 DG HSM Art
Review : Canon EF11-24mm f/4L USM
Review : Creative Sound Blaster Roar 2
Review : Philips Fidelio M2L
Review : Alienware 17 - Dell's Alienware laptops
Review Smartwatch : Wellograph
Review : Xiaomi Redmi 2
Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
Popular Tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8
Visit movie_stars's profile on Pinterest.