void GCamera::Move(XMFLOAT3 direction) { mPosition = GMathVF(XMVector3Transform(GMathFV(mPosition), XMMatrixTranslation(direction.x, direction.y, direction.z))); mTarget = GMathVF(XMVector3Transform(GMathFV(mTarget), XMMatrixTranslation(direction.x, direction.y, direction.z))); mUp = GMathVF(XMVector3Transform(GMathFV(mUp), XMMatrixTranslation(direction.x, direction.y, direction.z))); this->lookatViewMatrix(); }
// Set camera position void GCamera::Position(XMFLOAT3& new_position) { XMFLOAT3 move_vector = GMathVF(GMathFV(new_position) - GMathFV(mPosition)); XMFLOAT3 target = mTarget; this->Move(move_vector); this->Target(target); }
void GCamera::Target(XMFLOAT3 new_target) { if (XMVector3Equal(GMathFV(new_target), GMathFV(mPosition)) || XMVector3Equal(GMathFV(new_target), GMathFV(mTarget))) return; XMFLOAT3 old_look_at_target = GMathVF(GMathFV(mTarget) - GMathFV(mPosition)); XMFLOAT3 new_look_at_target = GMathVF(GMathFV(new_target) - GMathFV(mPosition)); float angle = XMConvertToDegrees(XMVectorGetX( XMVector3AngleBetweenNormals(XMVector3Normalize(GMathFV(old_look_at_target)), XMVector3Normalize(GMathFV(new_look_at_target))))); if (angle != 0.0f && angle != 360.0f && angle != 180.0f) { XMVECTOR axis = XMVector3Cross(GMathFV(old_look_at_target), GMathFV(new_look_at_target)); Rotate(GMathVF(axis), angle); } mTarget = new_target; this->lookatViewMatrix(); }
void GCamera::Update() { XMVECTOR m_moveCommand= GMathFV(XMFLOAT3(0,0,0)); XMVECTOR forward= GMathFV(mLook); XMVECTOR right= GMathFV(mRight); if (m_forward) m_moveCommand += forward; if (m_back) m_moveCommand -= forward; if (m_left) m_moveCommand -= right ; if (m_right) m_moveCommand += right ; XMMATRIX R = XMMatrixRotationAxis(GMathFV(mRight), m_pitch); XMVECTOR vecLookW = XMVector3TransformCoord(GMathFV(mLook), R); XMVECTOR vecUpW = XMVector3TransformCoord(GMathFV(mUp), R); R = XMMatrixRotationY(m_yaw); XMVECTOR vecRightW = XMVector3TransformCoord(GMathFV(mRight), R); vecUpW = XMVector3TransformCoord(vecUpW, R); vecLookW = XMVector3TransformCoord(vecLookW, R); mRight = GMathVF(vecRightW); mUp = GMathVF(vecUpW); mLook = GMathVF(vecLookW); // make sure that 45 degree cases are not faster buildViewMatrix(); mPosition = GMathVF(m_moveCommand*mSpeed + GMathFV(mPosition)); m_pitch = 0; m_yaw = 0; m_forward = false; m_back = false; m_right = false; m_left = false; }
GCamera::GCamera(void) { mPosition = XMFLOAT3(0.0f, 0.0f, -1.0f); mTarget = XMFLOAT3(0.0f, 0.0f, 0.0f); mUp = GMathVF(GMathFV(mPosition) + GMathFV(XMFLOAT3(0, 1, 0))); this->initViewMatrix(); mAngle = 0.0f; mClientWidth = 0.0f; mClientHeight = 0.0f; mNearest = 0.0f; mFarthest = 0.0f; XMStoreFloat4x4(&mView, XMMatrixIdentity()); XMStoreFloat4x4(&mProj, XMMatrixIdentity()); XMStoreFloat4x4(&mOrtho, XMMatrixIdentity()); }
GCamera::GCamera(void) { mPosition = XMFLOAT3(0.0f, 0.0f, -1.0f); mTarget = XMFLOAT3(0.0f, 0.0f, 0.0f); mUp = GMathVF(GMathFV(mPosition) + GMathFV(XMFLOAT3(0, 1, 0))); //this->lookatViewMatrix(); mAngle = 0.5f; mClientWidth = 0.0f; mClientHeight = 0.0f; mNearest = 0.0f; mFarthest = 1000.0f; mSpeed = 0.5f; mRight = XMFLOAT3(1, 0, 0); mUp = XMFLOAT3(0, 1, 0); mLook = XMFLOAT3(0, 0, 1); XMStoreFloat4x4(&mView, XMMatrixIdentity()); XMStoreFloat4x4(&mProj, XMMatrixIdentity()); XMStoreFloat4x4(&mOrtho, XMMatrixIdentity()); buildViewMatrix(); }
void GCamera::Rotate(XMFLOAT3 axis, float degrees) { if (XMVector3Equal(GMathFV(axis), XMVectorZero()) || degrees == 0.0f) return; // rotate vectors XMFLOAT3 look_at_target = GMathVF(GMathFV(mTarget) - GMathFV(mPosition)); XMFLOAT3 look_at_up = GMathVF(GMathFV(mUp) - GMathFV(mPosition)); look_at_target = GMathVF(XMVector3Transform(GMathFV(look_at_target), XMMatrixRotationAxis(GMathFV(axis), XMConvertToRadians(degrees)))); look_at_up = GMathVF(XMVector3Transform(GMathFV(look_at_up), XMMatrixRotationAxis(GMathFV(axis), XMConvertToRadians(degrees)))); // restore vectors's end points mTarget and mUp from new rotated vectors mTarget = GMathVF(GMathFV(mPosition) + GMathFV(look_at_target)); mUp = GMathVF(GMathFV(mPosition) + GMathFV(look_at_up)); this->lookatViewMatrix(); }
void GCamera::buildViewMatrix() { XMVECTOR vecLookW = XMVector3Normalize(GMathFV(mLook)); XMVECTOR vecRightW = XMVector3Normalize(XMVector3Cross(GMathFV(mUp), vecLookW)); XMVECTOR vecUpW = XMVector3Normalize(XMVector3Cross(vecLookW, vecRightW)); XMVECTOR vecPosW = GMathFV(mPosition); float x = -GMathVF(XMVector3Dot(vecPosW, vecRightW)).x; float y = -GMathVF(XMVector3Dot(vecPosW, vecUpW)).x; float z = -GMathVF(XMVector3Dot(vecPosW, vecLookW)).x; mRight = GMathVF(vecRightW); mUp = GMathVF(vecUpW); mLook = GMathVF(vecLookW); mView(0, 0) = mRight.x; mView(1, 0) = mRight.y; mView(2, 0) = mRight.z; mView(3, 0) = x; mView(0, 1) = mUp.x; mView(1, 1) = mUp.y; mView(2, 1) = mUp.z; mView(3, 1) = y; mView(0, 2) = mLook.x; mView(1, 2) = mLook.y; mView(2, 2) = mLook.z; mView(3, 2) = z; mView(0, 3) = 0.0f; mView(1, 3) = 0.0f; mView(2, 3) = 0.0f; mView(3, 3) = 1.0f; }