Language:EN
Pages: 5
Rating : ⭐⭐⭐⭐⭐
Price: \$10.99
Page 1 Preview

# Direct chap page understanding transformations and viewportspout- dxvec dot

06 direct3d chap06 6/19/03 5:22 PM Page 105

105

pOut->_44 = 1.0f;

return pOut;

RIGHT UP
Yw Yc

Figure 6.14

The Up, Right, and

Xc
Zw

To visualize camera rotation around a camera axis, imagine a player sitting in the cockpit of an F-22. If the player pushes his foot pedals in the F-22 for the left or right, the Look and the Right vectors have to be rotated around the Up vector or y-axis (yaw effect). If he pushes his flight stick to the right or left, the Up and Right vectors have to be rotated around the Look vector or z-axis (roll effect). If he pushes the flight stick forward or backward, you have to rotate the Look and Up vectors around the Right vector or x-axis (pitch effect).

D3DXMatrixLookAtLH() returns the view matrix. You can see how the vectors are filled in the columns. The first three values of the first column store the Up vector, the first three values

106 6.

D3DXMatrixLookAtLH() is very helpful for building a follow-up camera. The Look vector points to the object the camera should follow. The position vector of the camera is moved around this object or positioned behind or in front of it. Pseudocode for the function might look like this:

static FLOAT tic = -200.0f * rand();

vPos.y = vLook.y + (FLOAT)(1.0f + 0.3f * sin(tic * 0.33f)); // slightly up and down move:

more lively

This function is designed for simple uses. For example, it is perfect for a head-mounted tracking display or a follow-up camera, but it won’t help you build a space shooter or flight simulator camera. Therefore, a second class of camera techniques is more useful for these types of games. There are two implementations—one where the camera is rotated around vectors and one where the camera is rotated around arbitrary axes using quaternions. I will start with the camera class that rotates the camera around its axis.

The 1steps5 Example

107

Understanding Transformations and Viewports

static D3DXVECTOR3 vCameraLook=D3DXVECTOR3(0.0f,0.0f,1.0); // look vector static D3DXVECTOR3 vCameraUp=D3DXVECTOR3(0.0f,1.0f,0.0f); // up vector static D3DXVECTOR3 vCameraRight=D3DXVECTOR3(1.0f,0.0f,0.0f); // right vector static D3DXVECTOR3 vCameraPos=D3DXVECTOR3(0.0f,0.0f,-5.0f); // position vector

// base vector regeneration
D3DXVec3Normalize(&vCameraLook, &vCameraLook);
D3DXVec3Cross(&vCameraRight, &vCameraUp, &vCameraLook); // Cross Product of the UP and LOOK Vector
D3DXVec3Normalize(&vCameraRight, &vCameraRight);
D3DXVec3Cross(&vCameraUp, &vCameraLook, &vCameraRight); // Cross Product of the RIGHT and LOOK Vector
D3DXVec3Normalize(&vCameraUp, &vCameraUp);

// Matrices for pitch, yaw and roll
D3DXMatrixRotationAxis(&matPitch, &vCameraRight, fPitch ); D3DXMatrixRotationAxis(&matYaw, &vCameraUp, fYaw );
D3DXMatrixRotationAxis(&matRoll, &vCameraLook, fRoll);

108 6.

// rotate the LOOK & UP Vectors about the RIGHT Vector
D3DXVec3TransformCoord(&vCameraLook, &vCameraLook, &matPitch); D3DXVec3TransformCoord(&vCameraUp, &vCameraUp, &matPitch);

// rotate the RIGHT & UP Vectors about the LOOK Vector
D3DXVec3TransformCoord(&vCameraRight, &vCameraRight, &matRoll); D3DXVec3TransformCoord(&vCameraUp, &vCameraUp, &matRoll);

Now that you have seen how you can rotate a camera around all three axes in 3D by rotating two vectors around a rotation vector, you are prepared to dive into a more sophisticated example that does the same thing using a quaternion.

109

fRoll = fPitch = fYaw = 0.0f;
D3DXVECTOR3 vPos(0.0f, 0.0f, 0.0f);
static D3DXMATRIX matView = D3DXMATRIX(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f,-5.0f, 1.0f);
// Process keyboard input
if (m_bKey[VK_UP]) fPitch += fOneStep; // Pitch Up
if (m_bKey[VK_DOWN]) fPitch -= fOneStep; // Pitch Down if (m_bKey[VK_LEFT]) fYaw += fOneStep; // Turn Left if (m_bKey[VK_RIGHT]) fYaw -= fOneStep; // Turn Right if (m_bKey[‘C’]) fRoll += fOneStep; // Rotate Left if (m_bKey[‘X’]) fRoll -= fOneStep; // Rotate Right if (m_bKey[VK_HOME]) vPos.z += fOneStep; // Move Forward if (m_bKey[VK_END]) vPos.z -= fOneStep; // Move Backward if (m_bKey[VK_NUMPAD4]) vPos.x -= fOneStep; // Slide Left
if (m_bKey[VK_NUMPAD6]) vPos.x += fOneStep; // Slide Right
if (m_bKey[VK_NUMPAD8]) vPos.y += fOneStep; // Slide Down if (m_bKey[VK_NUMPAD2]) vPos.y -= fOneStep; // Slide Up

// Update position and view matrices
D3DXMATRIX matR, matTemp;
D3DXQuaternionRotationYawPitchRoll (&qR, fYaw, fPitch, fRoll); D3DXMatrixRotationQuaternion (&matR, &qR);
D3DXMatrixMultiply (&matView, &matR, &matView);
D3DXMatrixTranslation (&matTemp, vPos.x, vPos.y, vPos.z); D3DXMatrixMultiply (&matView, &matTemp, &matView);
D3DXMatrixInverse (&matTemp, NULL, &matView);

You are viewing 1/3rd of the document.Purchase the document to get full access instantly

Immediately available after payment