Camera::Camera(int width,int height) { m_View = matrix::m4identity(); m_Proj = matrix::m4identity(); mViewProj = matrix::m4identity(); m_Speed = 10.0f; m_LastMousePosition = Vector2f(0,0); m_ViewPos = Vector3f(0,0,-5); m_UpVec = Vector3f(0,1,0); m_LookAt = Vector3f(0,0,1); m_RightVec = Vector3f(1,0,0); float w = static_cast<float>(width); float h = static_cast<float>(height); float aspect = w / h; setLens(0.25f*PI, aspect, 1.0f, 1000.0f); m_OrthoProj = matrix::mat4OrthoLH(w,h,0.0f,1.0f); // checked m_OrthoView = matrix::m4identity();//matrix::mat4LookAtLH(m_ViewPos,m_LookAt,m_UpVec); // checked m_Ortho = false; buildView(); }
void Camera::lookAt(D3DXVECTOR3& pos, D3DXVECTOR3& target, D3DXVECTOR3& up) { 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); mPosW = pos; mRightW = R; mUpW = U; mLookW = L; buildView(); mViewProj = mView * mProj; }
void Camera::lookAt(D3DXVECTOR3& pos, D3DXVECTOR3& target, D3DXVECTOR3& up) { 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_posW = pos; m_rightW = R; m_upW = U; m_lookW = L; buildView(); buildFrustumPlanes(); m_viewProj = m_view * m_proj; }
void FpsCamera::move(Direction d) { Vec3 dir(0, 0, 0); if (d & Forward) { dir += m_view; } if (d & Backward) { dir -= m_view; } if (d & Rightward) { dir += m_right; } if (d & Leftward) { dir -= m_right; } D3DXVec3Normalize(&dir, &dir); m_pos += (dir * m_delta); buildView(); }
void Window::updateView() { double amount = perSecond(m_speed * (m_keysPressed.contains(Qt::Key_Shift) ? 5 : 1)); // if (m_keysPressed.contains(Qt::Key_W)) { // QVector3D vec(m_view(2, 0), m_view(2, 1), m_view(2, 2)); // m_camera.pos += vec * amount; // } // if (m_keysPressed.contains(Qt::Key_S)) { // QVector3D vec(m_view(2, 0), m_view(2, 1), m_view(2, 2)); // qDebug()<<vec<<amount<<m_view; // m_camera.pos -= vec * amount; // } // if (m_keysPressed.contains(Qt::Key_A)) { // QVector3D vec(m_view(0, 0), m_view(0, 1), m_view(0, 2)); // m_camera.pos += vec * amount; // } // if (m_keysPressed.contains(Qt::Key_D)) { // QVector3D vec(m_view(0, 0), m_view(0, 1), m_view(0, 2)); // m_camera.pos -= vec * amount; // } // if (m_keysPressed.contains(Qt::Key_R)) { // QVector3D vec(m_view(1, 0), m_view(1, 1), m_view(1, 2)); // // QVector3D vec(0, 0, 1); // m_camera.pos -= vec * amount; // } // if (m_keysPressed.contains(Qt::Key_F)) { // // QVector3D vec(0, 0, 1); // QVector3D vec(m_view(1, 0), m_view(1, 1), m_view(1, 2)); // m_camera.pos += vec * amount; // } QQuaternion quat; if (m_keysPressed.contains(Qt::Key_W)) { QVector3D vec(m_view(0, 0), m_view(0, 1), m_view(0, 2)); quat *= QQuaternion::fromAxisAndAngle(vec, amount); m_needsUpdate = true; } if (m_keysPressed.contains(Qt::Key_S)) { QVector3D vec(m_view(0, 0), m_view(0, 1), m_view(0, 2)); quat *= QQuaternion::fromAxisAndAngle(vec, -amount); m_needsUpdate = true; } if (m_keysPressed.contains(Qt::Key_A)) { QVector3D vec(m_view(1, 0), m_view(1, 1), m_view(1, 2)); quat *= QQuaternion::fromAxisAndAngle(vec, amount); m_needsUpdate = true; } if (m_keysPressed.contains(Qt::Key_D)) { QVector3D vec(m_view(1, 0), m_view(1, 1), m_view(1, 2)); quat *= QQuaternion::fromAxisAndAngle(vec, -amount); m_needsUpdate = true; } if (m_keysPressed.contains(Qt::Key_Q)) { QVector3D vec(m_view(2, 0), m_view(2, 1), m_view(2, 2)); quat *= QQuaternion::fromAxisAndAngle(vec, -amount); m_needsUpdate = true; } if (m_keysPressed.contains(Qt::Key_E)) { QVector3D vec(m_view(2, 0), m_view(2, 1), m_view(2, 2)); quat *= QQuaternion::fromAxisAndAngle(vec, amount); m_needsUpdate = true; } m_camera.rotation *= quat; m_camera.rotation.normalize(); buildView(); }
void Camera::update(float dt, float offsetHeight) { // 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 (g_input->keyDown(DIK_W) && g_input->keyDown(DIK_LSHIFT)) dir += m_lookW; if (g_input->keyDown(DIK_S) && g_input->keyDown(DIK_LSHIFT)) dir -= m_lookW; if (g_input->keyDown(DIK_D) && g_input->keyDown(DIK_LSHIFT)) dir += m_rightW; if (g_input->keyDown(DIK_A) && g_input->keyDown(DIK_LSHIFT)) dir -= m_rightW; // Move at m_speed along net direction. D3DXVec3Normalize(&dir, &dir); D3DXVECTOR3 newPos = m_posW + dir * m_fLinearSpeed * dt; // if (terrain != 0) // { // // New position might not be on terrain, so project the // // point onto the terrain. // newPos.y = terrain->getHeight(newPos.x, newPos.z) + offsetHeight; // // // Now the difference of the new position and old (current) // // position approximates a tangent vector on the terrain. // D3DXVECTOR3 tangent = newPos - m_posW; // D3DXVec3Normalize(&tangent, &tangent); // // // Now move camera along tangent vector. // m_posW += tangent*m_speed*dt; // // // After update, there may be errors in the camera height since our // // tangent is only an approximation. So force camera to correct height, // // and offset by the specified amount so that camera does not sit // // exactly on terrain, but instead, slightly above it. // m_posW.y = terrain->getHeight(m_posW.x, m_posW.z) + offsetHeight; // } // else // { m_posW = newPos; // } // We rotate at a fixed speed. float pitch = 0.0f; float yAngle = 0.0f; if (g_input->keyDown(DIK_LCONTROL) || g_input->keyDown(DIK_RCONTROL)) { pitch = g_input->mouseMoveY() / m_fRotSpeed; yAngle = g_input->mouseMoveX() / m_fRotSpeed; } // Rotate camera's look and up vectors around the camera's right vector. D3DXMATRIX R; D3DXMatrixRotationAxis(&R, &m_rightW, pitch); D3DXVec3TransformCoord(&m_lookW, &m_lookW, &R); D3DXVec3TransformCoord(&m_upW, &m_upW, &R); // Rotate camera axes about the world's y-axis. D3DXMatrixRotationY(&R, yAngle); D3DXVec3TransformCoord(&m_rightW, &m_rightW, &R); D3DXVec3TransformCoord(&m_upW, &m_upW, &R); D3DXVec3TransformCoord(&m_lookW, &m_lookW, &R); // Rebuild the view matrix to reflect changes. buildView(); buildFrustumPlanes(); m_viewProj = m_view * m_proj; }
void Camera::setPitch(float angle) { mat4 R = matrix::mat4Rotation(m_RightVec,angle); m_UpVec = R * m_UpVec; m_LookAt = R * m_LookAt; buildView(); }
void Camera::restore() { m_Ortho = false; buildView(); }
void Camera::strafe(float unit) { Vector3f tmp = m_RightVec * unit; m_ViewPos = m_ViewPos + tmp; buildView(); }
void Camera::move(float unit) { Vector3f tmp = m_LookAt * unit; m_ViewPos = m_ViewPos + tmp; buildView(); }
void Camera::lookAt(const Vector3f& lookAt) { m_LookAt = lookAt; buildView(); }
void Camera::setPosition(const Vector3f& pos) { m_ViewPos = pos; buildView(); }
void Camera::setLens(float fov, float aspect, float nearZ, float farZ) { m_Proj = matrix::mat4PerspectiveFovLH(fov, aspect, nearZ, farZ); // checked buildView(); }