Книга: DirectX 8 Programming Tutorial

Step 3: Multiply Matrices

Step 3: Multiply Matrices

Now that we have our matrices, we need to multiply them together to create one transformation matrix for each cube. We will use the DirectX function D3DXMatrixMultiply to do this. Cube 1 is easy, we want it to rotate around the x axis. Becuase it's center is on the x axis, all we need to do is use the x axis rotation matrix, that means that we don't need to multiply any matrices together because we already have this matrix defined. Cubes 2-5 are a bit different, they are not on the axis that we want to rotate them around. So, what we need to do is move them on to their rotation axis, rotate them, then move them back to their starting positions. If we just rotate them without moving them, the cubes will just swing around the axis rather than staying in the same position and rotating. Fig 5.6 shows a cube swinging around the y axis. Fig 5.7 shows a cube being moved to the origin, rotated, them moved back.


Fig 5.6


Fig 5.7

We have the matrix for cube 1, so now we need to create a matrix for cubes 2, 3, 4 and 5. The follow code snippet shows how to do this. Notice the order in which the matrices are multiplied together. If you change this order, you will get a different result.

//Combine the matrices to form 4 transformation matrices
D3DXMatrixMultiply(&matTransformation2, &matMoveRight9, &matRotationY);
D3DXMatrixMultiply(&matTransformation2, &matTransformation2, &matMoveLeft9);
D3DXMatrixMultiply(&matTransformation3, &matMoveLeft9, &matRotationZ);
D3DXMatrixMultiply(&matTransformation3, &matTransformation3, &matMoveRight9);
D3DXMatrixMultiply(&matTransformation4, &matMoveLeft27, &matRotationUser1);
D3DXMatrixMultiply(&matTransformation4, &matTransformation4, &matMoveRight27);
D3DXMatrixMultiply(&matTransformation5, &matMoveDown15, &matRotationY);
D3DXMatrixMultiply(&matTransformation5, &matTransformation5, &matRotationX);
D3DXMatrixMultiply(&matTransformation5, &matTransformation5, &matRotationZ);
D3DXMatrixMultiply(&matTransformation5, &matTransformation5, &matMoveUp15);
D3DXMatrixMultiply(&matTransformation5, &matTransformation5, &matScaleUp1p5);

Step 4: Applying the Transformations

Now that we have a transformation matrix for each cube, we need to apply them and then render the cubes. To apply a transformation matix, we use the SetTransform method. When you call SetTransform with a transformation matrix, all further objects that are rendered will have that matrix appied to them. So, for each cube we need to call SetTransform with that cubes transformation matrix, and then render that cube. The code snippet below shows this:

//Apply the transformations and render our objects
m_pD3DDevice->SetTransform(D3DTS_WORLD, &matRotationX);
m_pCube1->Render();
m_pD3DDevice->SetTransform(D3DTS_WORLD, &matTransformation2);
m_pCube2->Render();
m_pD3DDevice->SetTransform(D3DTS_WORLD, &matTransformation3);
m_pCube3->Render();
m_pD3DDevice->SetTransform(D3DTS_WORLD, &matTransformation4);
m_pCube4->Render();
m_pD3DDevice->SetTransform(D3DTS_WORLD, &matTransformation5);
m_pCube5->Render();

The full new render method for this tutorial is shown below. One thing to note is that I have moved the code from SetupPerspective() into the SetupCamera() function (just trying to keep the code simple).

void CGame::Render() {
 D3DXMATRIX matRotationX, matRotationY, matRotationZ, matRotationUser1;
 D3DXMATRIX matMoveRight27, matMoveLeft27, matMoveRight9, matMoveLeft9, matMoveDown15, matMoveUp15;
 D3DXMATRIX matTransformation2, matTransformation3, matTransformation4, matTransformation5;
 D3DXMATRIX matScaleUp1p5;
 if (m_pD3DDevice == NULL) {
  return;
 }
 //Clear the back buffer and depth buffer
 m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
 //Begin the scene
 m_pD3DDevice->BeginScene();
 //Setup camera and perspective
 SetupCamera();
 //Create the rotation transformation matrices around the x, y and z axis
 D3DXMatrixRotationX(&matRotationX, timeGetTime()/400.0f);
 D3DXMatrixRotationY(&matRotationY, timeGetTime()/400.0f);
 D3DXMatrixRotationZ(&matRotationZ, timeGetTime()/400.0f);
 //Create the rotation transformation matrices around our user defined axis
 D3DXMatrixRotationAxis(&matRotationUser1, &D3DXVECTOR3(1.0f, 1.0f, 0.0f), timeGetTime()/400.0f);
 //Create the translation (move) matrices
 D3DXMatrixTranslation(&matMoveRight27, 27.0, 0.0, 0.0);
 D3DXMatrixTranslation(&matMoveLeft27, –27.0, 0.0, 0.0);
 D3DXMatrixTranslation(&matMoveRight9, 9.0, 0.0, 0.0);
 D3DXMatrixTranslation(&matMoveLeft9, –9.0, 0.0, 0.0);
 D3DXMatrixTranslation(&matMoveDown15, 0.0, –15.0, 0.0);
 D3DXMatrixTranslation(&matMoveUp15, 0.0, 15.0, 0.0);
 //Create a scale transformation
 D3DXMatrixScaling(&matScaleUp1p5, 1.5, 1.5, 1.5);
 //Combine the matrices to form 4 transformation matrices
 D3DXMatrixMultiply(&matTransformation2, &matMoveRight9, &matRotationY);
 D3DXMatrixMultiply(&matTransformation2, &matTransformation2, &matMoveLeft9);
 D3DXMatrixMultiply(&matTransformation3, &matMoveLeft9, &matRotationZ);
 D3DXMatrixMultiply(&matTransformation3, &matTransformation3, &matMoveRight9);
 D3DXMatrixMultiply(&matTransformation4, &matMoveLeft27, &matRotationUser1);
 D3DXMatrixMultiply(&matTransformation4, &matTransformation4, &matMoveRight27);
 D3DXMatrixMultiply(&matTransformation5, &matMoveDown15, &matRotationY);
 D3DXMatrixMultiply(&matTransformation5, &matTransformation5, &matRotationX);
 D3DXMatrixMultiply(&matTransformation5, &matTransformation5, &matRotationZ);
 D3DXMatrixMultiply(&matTransformation5, &matTransformation5, &matMoveUp15);
 D3DXMatrixMultiply(&matTransformation5, &matTransformation5, &matScaleUp1p5);
 //Apply the transformations and render our objects
 m_pD3DDevice->SetTransform(D3DTS_WORLD, &matRotationX);
 m_pCube1->Render();
 m_pD3DDevice->SetTransform(D3DTS_WORLD, &matTransformation2);
 m_pCube2->Render();
 m_pD3DDevice->SetTransform(D3DTS_WORLD, &matTransformation3);
 m_pCube3->Render();
 m_pD3DDevice->SetTransform(D3DTS_WORLD, &matTransformation4);
 m_pCube4->Render();
 m_pD3DDevice->SetTransform(D3DTS_WORLD, &matTransformation5);
 m_pCube5->Render();
 //End the scene
 m_pD3DDevice->EndScene();
 //Filp the back and front buffers so that whatever has been rendered on the back buffer
 //will now be visible on screen (front buffer).
 m_pD3DDevice->Present(NULL, NULL, NULL, NULL);
 //Count Frames
 m_dwFrames++;
}

Once you have made these changes, you should finish up with five rotating cubes (shown below).


Оглавление книги


Генерация: 3.415. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз