void RenderManager::BuildSkyBoxViewMatrix(GameObject g) { g.SetRotate(0.0f, 0.0f, 0.0f); Matrix4x4 mat; mat = BuildModelMatrix(g) * BuildViewMatrix(); ConvertToOpenGLMatrix(mat, modelViewMatrix); }
//----------------------------------------------------------------------------- // Purpose: Sets the view point. //----------------------------------------------------------------------------- void CCamera::SetViewPoint(const Vector &ViewPoint) { if ( m_ViewPoint != ViewPoint ) { m_ViewPoint = ViewPoint; BuildViewMatrix(); } }
LJMatrix4 BuildViewMatrix(const LJVector3& pos, const LJVector3& center, const LJVector3& up) { LJVector3 d = ljm::normalize(pos - center); LJVector3 u = ljm::normalize(up); LJVector3 r = ljm::normalize(ljm::cross(u, d)); u = ljm::cross(d, r); return BuildViewMatrix(r, u, d, pos); }
void RenderManager::PrepareLights() { std::vector<LightSource>::iterator it; int i = 0; for (it = lightObjects.begin(); it != lightObjects.end(); it++) { lights[i] = it->GetLightAsStruct(BuildViewMatrix()); i++; } }
//----------------------------------------------------------------------------- // Purpose: Moves the camera along the camera's forward axis. // Input : fUnits - World units to move the camera. //----------------------------------------------------------------------------- void CCamera::MoveForward(float fUnits) { if (fUnits != 0) { m_ViewPoint[0] -= m_ViewMatrix[CAMERA_FORWARD][0] * fUnits; m_ViewPoint[1] -= m_ViewMatrix[CAMERA_FORWARD][1] * fUnits; m_ViewPoint[2] -= m_ViewMatrix[CAMERA_FORWARD][2] * fUnits; BuildViewMatrix(); } }
//----------------------------------------------------------------------------- // Purpose: Moves the camera along the camera's up axis. // Input : fUnits - World units to move the camera. //----------------------------------------------------------------------------- void CCamera::MoveUp(float fUnits) { if (fUnits != 0) { m_ViewPoint[0] += m_ViewMatrix[CAMERA_UP][0] * fUnits; m_ViewPoint[1] += m_ViewMatrix[CAMERA_UP][1] * fUnits; m_ViewPoint[2] += m_ViewMatrix[CAMERA_UP][2] * fUnits; BuildViewMatrix(); } }
void Camera::Update(float dt, float offsetHeight) { mDXInput->Update() ; // Find the net direction the camera is traveling in (since the // camera could be running and strafing). D3DXVECTOR3 dir(0.0f, 0.0f, 0.0f); if( mDXInput->KeyDown(DIK_W) ) dir += m_vForward; if( mDXInput->KeyDown(DIK_S) ) dir -= m_vForward; if( mDXInput->KeyDown(DIK_D) ) dir += m_vRight; if( mDXInput->KeyDown(DIK_A) ) dir -= m_vRight; // Move at mSpeed along net direction. D3DXVec3Normalize(&dir, &dir); D3DXVECTOR3 newPos = m_vEyePoint + dir * mSpeed * dt; m_vEyePoint = newPos ; if (m_vEyePoint.y < 1.0f) { m_vEyePoint.y = 1.0f ; } if (m_vEyePoint.y > 2.0f) { m_vEyePoint.y = 2.0f ; } // We rotate at a fixed speed. float pitch = mDXInput->MouseDY() / 360.0f; float yAngle = mDXInput->MouseDX() / 360.0f; // Rotate camera's look and up vectors around the camera's right vector. D3DXMATRIX R; D3DXMatrixRotationAxis(&R, &m_vRight, pitch); D3DXVec3TransformCoord(&m_vForward, &m_vForward, &R); D3DXVec3TransformCoord(&m_vUp, &m_vUp, &R); // Rotate camera axes about the world's y-axis. D3DXMatrixRotationY(&R, yAngle); D3DXVec3TransformCoord(&m_vRight, &m_vRight, &R); D3DXVec3TransformCoord(&m_vForward, &m_vForward, &R); // Rebuild the view matrix to reflect changes. BuildViewMatrix(); }
//----------------------------------------------------------------------------- // Purpose: Sets the pitch in degrees, from [MIN_PITCH..MAX_PITCH] //----------------------------------------------------------------------------- void CCamera::SetPitch(float fDegrees) { if (m_fPitch != fDegrees) { m_fPitch = fDegrees; if (m_fPitch > MAX_PITCH) { m_fPitch = MAX_PITCH; } else if (m_fPitch < MIN_PITCH) { m_fPitch = MIN_PITCH; } BuildViewMatrix(); } }
//----------------------------------------------------------------------------- // Purpose: Sets the yaw in degrees, from [0..360) //----------------------------------------------------------------------------- void CCamera::SetYaw(float fDegrees) { while (fDegrees >= 360) { fDegrees -= 360; } while (fDegrees < 0) { fDegrees += 360; } if (m_fYaw != fDegrees) { m_fYaw = fDegrees; BuildViewMatrix(); } }
//----------------------------------------------------------------------------- // Purpose: Sets the roll in degrees, from [0..360) //----------------------------------------------------------------------------- void CCamera::SetRoll(float fDegrees) { while (fDegrees >= 360) { fDegrees -= 360; } while (fDegrees < 0) { fDegrees += 360; } if (m_fRoll != fDegrees) { m_fRoll = fDegrees; BuildViewMatrix(); } }
void RenderManager::BuildModelViewMatrix(GameObject g) { Matrix4x4 mvMatrix; Matrix4x4 model = BuildModelMatrix(g); Matrix4x4 view = BuildViewMatrix(); mvMatrix = model * view; ConvertToOpenGLMatrix(mvMatrix, modelViewMatrix); float mv[3][3]; mv[0][0] = mvMatrix(0,0); mv[1][1] = mvMatrix(1,1); mv[2][2] = mvMatrix(2,2); mv[1][0] = mvMatrix(0,1); mv[2][0] = mvMatrix(0,2); mv[2][1] = mvMatrix(1,2); mv[0][1] = mvMatrix(1,0); mv[0][2] = mvMatrix(2,0); mv[1][2] = mvMatrix(2,1); float det = mv[0][0] * (mv[1][1]*mv[2][2] - mv[1][2]*mv[2][1]) - mv[0][1] * (mv[1][0] * mv[2][2] - mv[1][2] * mv[2][0]) + mv[0][2] * (mv[1][0] * mv[2][1] - mv[1][1] * mv[2][0]); mv[0][0] = (mv[1][1]*mv[2][2] - mv[1][2]*mv[2][1])/det; mv[0][1] = -(mv[1][0]*mv[2][2] - mv[1][2]*mv[2][0])/det; mv[0][2] = (mv[1][0]*mv[2][1] - mv[1][1]*mv[2][0])/det; mv[1][0] = -(mv[0][1]*mv[2][2] - mv[0][2]*mv[2][1])/det; mv[1][1] = (mv[0][0]*mv[2][2] - mv[0][2]*mv[2][0])/det; mv[1][2] = -(mv[0][0]*mv[2][1] - mv[0][1]*mv[2][0])/det; mv[2][0] = (mv[0][1]*mv[1][2] - mv[0][2]*mv[1][1])/det; mv[2][1] = -(mv[0][0]*mv[1][2] - mv[0][2]*mv[1][0])/det; mv[2][2] = (mv[0][0]*mv[1][1] - mv[0][1]*mv[1][0])/det; Matrix4x4 normMatrix(Matrix4x4::IDENTITY); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { normMatrix.elem[i][j] = mv[j][i]; } } ConvertToOpenGLMatrix(normMatrix, normalMatrix); }
//----------------------------------------------------------------------------- // Purpose: Constructor. //----------------------------------------------------------------------------- CCamera::CCamera(void) { m_ViewPoint.Init(); m_fPitch = 0.0; m_fRoll = 0.0; m_fYaw = 0.0; m_fHorizontalFOV = 90; m_fNearClip = 1.0; m_fFarClip = 5000; m_fZoom = 1.0f; m_bIsOrthographic = false; m_fScaleHorz = m_fScaleVert = 1; m_nViewWidth = m_nViewHeight = 100; BuildViewMatrix(); BuildProjMatrix(); }
void Camera::SetViewParams(D3DXVECTOR3& pos, D3DXVECTOR3& target, D3DXVECTOR3& up) { // the three code block below make sure that L, R and U are orthogonal to each other D3DXVECTOR3 L = target - pos; D3DXVec3Normalize(&L, &L); D3DXVECTOR3 R; D3DXVec3Cross(&R, &up, &L); D3DXVec3Normalize(&R, &R); D3DXVECTOR3 U; D3DXVec3Cross(&U, &L, &R); D3DXVec3Normalize(&U, &U); m_vEyePoint = pos; m_vRight = R; m_vUp = U; m_vForward = L; BuildViewMatrix(); }
void Camera::Render() { VectorType up, position, lookAt; float yaw, pitch, roll; float rotationMatrix[9]; // Setup the vector that points upwards. up.position.x = 0.0f; up.position.y = 1.0f; up.position.z = 0.0f; // Setup the position of the camera in the world. position.position = m_position; // Setup where the camera is looking by default. lookAt.position.x = 0.0f; lookAt.position.y = 0.0f; lookAt.position.z = 1.0f; // Set the yaw (Y axis), pitch (X axis), and roll (Z axis) rotations in radians. pitch = m_rotation.x * RADIAN; yaw = m_rotation.y * RADIAN; roll = m_rotation.z * RADIAN; // Create the rotation matrix from the yaw, pitch, and roll values. MatrixRotationYawPitchRoll(rotationMatrix, yaw, pitch, roll); // Transform the lookAt and up vector by the rotation matrix so the view is correctly rotated at the origin. TransformCoord(lookAt, rotationMatrix); TransformCoord(up, rotationMatrix); // Translate the rotated camera position to the location of the viewer. lookAt.position.x = position.position.x + lookAt.position.x; lookAt.position.y = position.position.y + lookAt.position.y; lookAt.position.z = position.position.z + lookAt.position.z; // Finally create the view matrix from the three updated vectors. BuildViewMatrix(position, lookAt, up); }
void Camera::Translate(const Vector4& translation) { m_position += translation; m_lookAt += translation; BuildViewMatrix( m_position, m_lookAt, m_up ); }
int FirstPersonCamera::Update(float dt) { //testing code { float speed = 2.0f * dt; if (GetAsyncKeyState('W')) moveBackForward += speed; if (GetAsyncKeyState('S')) moveBackForward -= speed; if (GetAsyncKeyState('A')) moveLeftRight -= speed; if (GetAsyncKeyState('D')) moveLeftRight += speed; if (GetAsyncKeyState(VK_UP)) camPitch += speed; if (GetAsyncKeyState(VK_DOWN)) camPitch -= speed; if (GetAsyncKeyState(VK_LEFT)) camYaw += speed; if (GetAsyncKeyState(VK_RIGHT)) camYaw -= speed; if (GetAsyncKeyState(VK_SPACE)) moveBottomTop += speed; /*GetCursorPos(&mouse_current); if ((mouse_current.x != mouse_last.x) || (mouse_current.y != mouse_last.y)) { camera_.camYaw += -(mouse_current.x- mouse_last.x) * speed * 0.01f; camera_.camPitch += (mouse_current.y- mouse_last.y) * speed * 0.01f; mouse_last = mouse_current; }*/ //if (GetAsyncKeyState('T')) // DefaultCamTarget = dx::XMVectorAdd(DefaultCamTarget,dx::XMVectorSet(0,speed,0,0)); // if (GetAsyncKeyState('G')) // DefaultCamTarget = dx::XMVectorAdd(DefaultCamTarget,dx::XMVectorSet(0,-speed,0,0)); } camRotationMatrix = dx::XMMatrixRotationRollPitchYaw(camPitch, camYaw, 0); camTarget = dx::XMVector3TransformCoord(DefaultForward, camRotationMatrix ); camTarget = dx::XMVector3Normalize(camTarget); /*dx::XMMATRIX RotateYTempMatrix; RotateYTempMatrix = dx::XMMatrixRotationY(camPitch); dx::XMMATRIX RotateXTempMatrix; RotateYTempMatrix = dx::XMMatrixRotationY(camPitch);*/ camRight = dx::XMVector3TransformCoord(DefaultRight, camRotationMatrix); camUp = dx::XMVector3TransformCoord(DefaultUp, camRotationMatrix); camForward = dx::XMVector3TransformCoord(DefaultForward, camRotationMatrix); //camPosition = dx::XMVectorAdd(camPosition,dx::XMVectorMultiply(dx::XMVectorSet(moveBottomTop,moveBottomTop,moveBottomTop,moveBottomTop),camUp)); camPosition = dx::XMVectorAdd(camPosition,dx::XMVectorSet(0,moveBottomTop,0,0)); camPosition = dx::XMVectorAdd(camPosition,dx::XMVectorMultiply(dx::XMVectorSet(moveLeftRight,moveLeftRight,moveLeftRight,moveLeftRight),camRight)); camPosition = dx::XMVectorAdd(camPosition,dx::XMVectorMultiply(dx::XMVectorSet(moveBackForward,moveBackForward,moveBackForward,moveBackForward),camForward)); moveLeftRight = 0.0f; moveBackForward = 0.0f; moveBottomTop = 0.0f; camTarget = dx::XMVectorAdd(camPosition,camTarget); BuildViewMatrix(camPosition,camTarget,camUp); return S_OK; }
void Camera::TranslateTo(const Vector4& location) { m_position = location; BuildViewMatrix( m_position, m_lookAt, m_up ); }