void DirectXRender::setCamera2d(IND_Camera2d *pCamera2d) { D3DXMATRIX mScale, mRotate, mMatLookAt, mMatView, mMatProjection; D3DXMatrixIdentity (&mScale); D3DXMatrixIdentity (&mRotate); D3DXMatrixIdentity (&mMatLookAt); D3DXMatrixIdentity (&mMatView); D3DXMatrixIdentity (&mMatProjection); //Setup point-to-pixel scale ratio on camera transform D3DXMATRIX mMatPointPixel; D3DXMatrixScaling(&mMatPointPixel,_info._pointPixelScale,_info._pointPixelScale,1.0f); D3DXMatrixMultiply(&mMatView,&mMatView,&mMatPointPixel); //Buffer D3DVec3 structs from our camera 3d vectors // -0.5f due to pixel to texel conversion. Done in camera to make less calculations (only on camera instead than on every vertex) D3DXVECTOR3 d3dpos (pCamera2d->_pos._x-0.5f,pCamera2d->_pos._y-0.5f,pCamera2d->_pos._z); D3DXVECTOR3 d3dlook (pCamera2d->_look._x,pCamera2d->_look._y, pCamera2d->_look._z); D3DXVECTOR3 d3dup (pCamera2d->_up._x,pCamera2d->_up._y, pCamera2d->_up._z); D3DXVECTOR3 d3dright (pCamera2d->_right._x,pCamera2d->_right._y, pCamera2d->_right._z); // ----- Rotation ----- // Roll is rotation around the z axis (_look) D3DXMatrixRotationAxis (&mRotate, &d3dlook, D3DXToRadian(pCamera2d->_angle - pCamera2d->_prevAngle)); //To apply roll we rotate up and right about the look vector (using our roll matrix) D3DXVec3TransformCoord (&d3dright, &d3dright, &mRotate); D3DXVec3TransformCoord (&d3dup, &d3dup, &mRotate); //Put the values back again to camera. pCamera2d->_right._x = d3dright.x; pCamera2d->_right._y = d3dright.y; pCamera2d->_right._z = d3dright.z; pCamera2d->_up._x = d3dup.x; pCamera2d->_up._y = d3dup.y; pCamera2d->_up._z = d3dup.z; //Make prev and actual angle in camera equal pCamera2d->setAngle(pCamera2d->_angle); // Build the view matrix from the camera axis //Utility DirectX function has use of 'at' vector instead of 'look' vector. Compute it: D3DXVECTOR3 d3dAt (d3dpos.x + d3dlook.x,d3dpos.y + d3dlook.y,d3dpos.z + d3dlook.z); D3DXMatrixLookAtLH(&mMatLookAt,&d3dpos,&d3dAt,&d3dup); D3DXMatrixMultiply(&mMatView, &mMatView, &mMatLookAt); // ---- Zoom ---- if (pCamera2d->_zoom != 1.0f) { D3DXMatrixScaling (&mScale, pCamera2d->_zoom, pCamera2d->_zoom, pCamera2d->_zoom); D3DXMatrixMultiply (&mMatView, &mMatView, &mScale); } // ----- Set transformation ----- _info._device->SetTransform(D3DTS_VIEW, &mMatView); // ----- Projection matrix ----- D3DXMatrixOrthoLH(&mMatProjection, static_cast<float>( _info._viewPortWidth), static_cast<float>( _info._viewPortHeight), -2048.0f, 2048.0f); _info._device->SetTransform(D3DTS_PROJECTION, &mMatProjection); }
/*! \b Parameters: \arg \b pCamera3d ::IND_Camera3d object that defines a camera. \b Operation: This function sets a 3d camera. See the methods of ::IND_Camera3d for information on how you can manipulate the camera. */ void DirectXRender::setCamera3d(IND_Camera3d *pCamera3d) { D3DXMATRIX mTrans, mMatView, mMatProjection; D3DXMatrixIdentity(&mMatView); D3DXMatrixIdentity(&mMatProjection); // ----- View matrix ----- pCamera3d->_up = IND_Vector3 (0.0f, 1.0f, 0.0f); pCamera3d->_look = IND_Vector3 (0.0f, 0.0f, 1.0f); pCamera3d->_right = IND_Vector3 (1.0f, 0.0f, 0.0f); //Buffer D3DVec3 structs from our camera 3d vectors D3DXVECTOR3 d3dpos (pCamera3d->_pos._x,pCamera3d->_pos._y,pCamera3d->_pos._z); D3DXVECTOR3 d3dlook (pCamera3d->_look._x,pCamera3d->_look._y, pCamera3d->_look._z); D3DXVECTOR3 d3dup (pCamera3d->_up._x,pCamera3d->_up._y, pCamera3d->_up._z); D3DXVECTOR3 d3dright (pCamera3d->_right._x,pCamera3d->_right._y, pCamera3d->_right._z); // Yaw is rotation around the y axis (_up) // Create a matrix that can carry out this rotation D3DXMATRIX yawMatrix; D3DXMatrixRotationAxis(&yawMatrix, &d3dup, D3DXToRadian(pCamera3d->_yaw)); // To apply yaw we rotate the _look & _right vectors about the _up vector (using our yaw matrix) D3DXVec3TransformCoord(&d3dlook, &d3dlook, &yawMatrix); D3DXVec3TransformCoord(&d3dright, &d3dright, &yawMatrix); // Pitch is rotation around the x axis (_right) // Create a matrix that can carry out this rotation D3DXMATRIX pitchMatrix; D3DXMatrixRotationAxis(&pitchMatrix, &d3dright, D3DXToRadian(pCamera3d->_pitch)); // To apply pitch we rotate the _look and _up vectors about the _right vector (using our pitch matrix) D3DXVec3TransformCoord(&d3dlook, &d3dlook, &pitchMatrix); D3DXVec3TransformCoord(&d3dup, &d3dup, &pitchMatrix); // Roll is rotation around the z axis (_look) // Create a matrix that can carry out this rotation D3DXMATRIX rollMatrix; D3DXMatrixRotationAxis(&rollMatrix, &d3dlook, D3DXToRadian(pCamera3d->_roll)); // To apply roll we rotate up and right about the look vector (using our roll matrix) // Note: roll only really applies for things like aircraft unless you are implementing lean D3DXVec3TransformCoord(&d3dright, &d3dright, &rollMatrix); D3DXVec3TransformCoord(&d3dup, &d3dup, &rollMatrix); // Build the view matrix from the transformed camera axis mMatView._11 = pCamera3d->_right._x; mMatView._12 = pCamera3d->_up._x; mMatView._13 = pCamera3d->_look._x; mMatView._21 = pCamera3d->_right._y; mMatView._22 = pCamera3d->_up._y; mMatView._23 = pCamera3d->_look._y; mMatView._31 = pCamera3d->_right._z; mMatView._32 = pCamera3d->_up._z; mMatView._33 = pCamera3d->_look._z; mMatView._41 = - D3DXVec3Dot(&d3dright, &d3dright); mMatView._42 = - D3DXVec3Dot(&d3dright, &d3dup); mMatView._43 = - D3DXVec3Dot(&d3dright, &d3dlook); // ---- Zoom ---- D3DXMatrixScaling(&mTrans, pCamera3d->_zoom, pCamera3d->_zoom, pCamera3d->_zoom); D3DXMatrixMultiply(&mMatView, &mTrans, &mMatView); _info._device->SetTransform(D3DTS_VIEW, &mMatView); // ----- Projection matrix ----- // Fov projection if (!pCamera3d->isOrthoProjection()) { D3DXMatrixPerspectiveFovLH(&mMatProjection, // output pCamera3d->_fov, // Fov vertical pCamera3d->_aspect, // Relación de aspecto del viewport pCamera3d->_nearClippingPlane, // Near clipping plane z pCamera3d->_farClippingPlane); // Far clipping plane z } // Orthographic projection else { D3DXMatrixOrthoLH(&mMatProjection, pCamera3d->_orthoWidth, pCamera3d->_orthoHeight, pCamera3d->_nearClippingPlane, pCamera3d->_farClippingPlane); } _info._device->SetTransform(D3DTS_PROJECTION, &mMatProjection); }