MULTIMEDIA

DirectX 10 Game Programming : 3D Primer - Matrix Scaling, Matrix Translation

1/12/2013 3:27:17 AM

Matrix Scaling

One of the transformations you can apply to an object is scaling. Scaling is the ability to shrink or grow an object by a certain factor. For instance, if you have a square centered on the origin that was two units wide and two units tall and you scaled it twice its size, you would have a square that now went four units in each direction. Figure 1 shows an example of the scaling effect.

Figure 1. A square scaled in both the X and Y directions.


Remember the 1s that were placed into the identity matrix? Those 1s control the scaling on each of the three axes. When vertices are transformed using this matrix, their X, Y, and Z values are altered based on the scaling values. By changing the X axis value to a 2 and the Y axis to a 3, the objects will be scaled twice their size in the X direction and three times their size in the Y. An example ScaleMatrix array is shown next. The variables scaleX, scaleY, and scaleZ show where the appropriate scaling values would go.

float ScaleMatrix [4][4] = {
    scaleX, 0.0f,    0.0f,     0.0f,
    0.0f,   scaleY,  0.0f,     0.0f,
    0.0f,   0.0f,    scaleZ,   0.0f,
    0.0f,   0.0f,    0.0f,     1.0f
};

Direct3D has a function called D3DXMatrixScaling that will generate a matrix for you based on scaling values you pass in.

D3DXMATRIX * D3DXMatrixScaling (
     D3DXMATRIX * pOut,
     FLOAT sx,
     FLOAT sy,
     FLOAT sz
);

Matrix Translation

The act of moving an object is called translation. Translation allows for an object to be moved along any of the three axes by specifying the amount of movement needed. For example, if you want to move an object right along the X axis, you would need to translate that object an appropriate number of units in the positive X direction. To do so, you would need to create a translation matrix. This matrix will then cause any objects it affects to be moved to the right. Figure 2 shows an example of a square being translated.

Figure 2. A square translated to the right along the X axis.


Again, I’ll start with the identity matrix and change it to add in the variables that affect translation. Take a look at the following translation matrix; the variables moveX, moveY, and moveZ show you where the translation values would go. If you wanted to translate an object 4 units to the right, you would replace moveX with the value 4.

float TranslationMatrix [4][4] = {
    1.0f,   0.0f,   0.0f,  0.0f,
    0.0f,   1.0f,   0.0f,  0.0f,
    0.0f,   0.0f,   1.0f,  0.0f,
    moveX,  moveY,  moveZ, 1.0f
};

The Direct3D function D3DXMatrixTranslation is used to easily create a translation matrix with any values you specify.

D3DXMATRIX* D3DXMatrixTranslation(
  D3DXMATRIX *pOut,
  FLOAT x,
  FLOAT y,
  FLOAT z
);
Other  
 
Most View
Top 10 Home Computers - Q1 2013
Samsung Galaxy Tab 2 10.1 Android Tablet
Learn How To… Stick Notes On Your Desktop
SharePoint 2010 : Planning Your Security Model - Overview of SharePoint Security Elements
Sony Vaio Pro 11 New High-End Ultrabook Review (Part 2)
Wake Up Your Wi-Fi (Part 1)
Motorola Xoom 2 - General Tablet Use
Samsung NX300 Camera – Large Touchscreen And Wonderful Image Quality (Part 2)
Steinberg Cubase 7 – The Fantastic Success (Part 1)
BlackBerry Development : Pushing Data to External Users - Web Signals (part 4) - Building a Web Signal - Pushing Data to Subscribers
Top 10
SQL Server 2012 : Policy Based Management - Evaluating Policies
SQL Server 2012 : Defining Policies (part 3) - Creating Policies
SQL Server 2012 : Defining Policies (part 2) - Conditions
SQL Server 2012 : Defining Policies (part 1) - Management Facets
Microsoft Exchange Server 2010 : Configuring Anti-Spam and Message Filtering Options (part 4) - Preventing Internal Servers from Being Filtered
Microsoft Exchange Server 2010 : Configuring Anti-Spam and Message Filtering Options (part 3) - Defining Block List Exceptions and Global Allow/Block Lists
Microsoft Exchange Server 2010 : Configuring Anti-Spam and Message Filtering Options (part 2) - Filtering Connections with IP Block Lists
Microsoft Exchange Server 2010 : Configuring Anti-Spam and Message Filtering Options (part 1) - Filtering Spam and Other Unwanted E-Mail by Sender, Filtering Spam and Other Unwanted E-Mail by Recipien
Microsoft Exchange Server 2010 : Creating and Managing Remote Domains (part 3) - Configuring Messaging Options for Remote Domains , Removing Remote Domains
Microsoft Exchange Server 2010 : Creating and Managing Remote Domains (part 2) - Creating Remote Domains