mirror of
https://github.com/OpenMW/openmw.git
synced 2025-06-21 13:11:33 +00:00
Camera refactoring
This commit is contained in:
parent
d5ca091d6e
commit
47cbdcba15
6 changed files with 143 additions and 195 deletions
|
@ -56,21 +56,19 @@ namespace MWRender
|
||||||
mCamera(camera),
|
mCamera(camera),
|
||||||
mAnimation(nullptr),
|
mAnimation(nullptr),
|
||||||
mFirstPersonView(true),
|
mFirstPersonView(true),
|
||||||
mMode(Mode::Normal),
|
mMode(Mode::FirstPerson),
|
||||||
mVanityAllowed(true),
|
mVanityAllowed(true),
|
||||||
mStandingPreviewAllowed(Settings::Manager::getBool("preview if stand still", "Camera")),
|
mStandingPreviewAllowed(Settings::Manager::getBool("preview if stand still", "Camera")),
|
||||||
mDeferredRotationAllowed(Settings::Manager::getBool("deferred preview rotation", "Camera")),
|
mDeferredRotationAllowed(Settings::Manager::getBool("deferred preview rotation", "Camera")),
|
||||||
mNearest(30.f),
|
mNearest(30.f),
|
||||||
mFurthest(800.f),
|
mFurthest(800.f),
|
||||||
mIsNearest(false),
|
mIsNearest(false),
|
||||||
|
mProcessViewChange(false),
|
||||||
mHeight(124.f),
|
mHeight(124.f),
|
||||||
mBaseCameraDistance(Settings::Manager::getFloat("third person camera distance", "Camera")),
|
mBaseCameraDistance(Settings::Manager::getFloat("third person camera distance", "Camera")),
|
||||||
mPitch(0.f),
|
mPitch(0.f),
|
||||||
mYaw(0.f),
|
mYaw(0.f),
|
||||||
mRoll(0.f),
|
mRoll(0.f),
|
||||||
mVanityToggleQueued(false),
|
|
||||||
mVanityToggleQueuedValue(false),
|
|
||||||
mViewModeToggleQueued(false),
|
|
||||||
mCameraDistance(0.f),
|
mCameraDistance(0.f),
|
||||||
mMaxNextCameraDistance(800.f),
|
mMaxNextCameraDistance(800.f),
|
||||||
mFocalPointCurrentOffset(osg::Vec2d()),
|
mFocalPointCurrentOffset(osg::Vec2d()),
|
||||||
|
@ -100,7 +98,7 @@ namespace MWRender
|
||||||
mCamera->removeUpdateCallback(mUpdateCallback);
|
mCamera->removeUpdateCallback(mUpdateCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
osg::Vec3d Camera::getFocalPoint() const
|
osg::Vec3d Camera::getTrackingNodePosition() const
|
||||||
{
|
{
|
||||||
if (!mTrackingNode)
|
if (!mTrackingNode)
|
||||||
return osg::Vec3d();
|
return osg::Vec3d();
|
||||||
|
@ -108,54 +106,37 @@ namespace MWRender
|
||||||
if (nodepaths.empty())
|
if (nodepaths.empty())
|
||||||
return osg::Vec3d();
|
return osg::Vec3d();
|
||||||
osg::Matrix worldMat = osg::computeLocalToWorld(nodepaths[0]);
|
osg::Matrix worldMat = osg::computeLocalToWorld(nodepaths[0]);
|
||||||
|
return worldMat.getTrans();
|
||||||
|
}
|
||||||
|
|
||||||
osg::Vec3d position = worldMat.getTrans();
|
osg::Vec3d Camera::getThirdPersonBasePosition() const
|
||||||
if (isFirstPerson())
|
|
||||||
position.z() += mHeadBobbingOffset;
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
|
osg::Vec3d position = getTrackingNodePosition();
|
||||||
position.z() += mHeight * mHeightScale;
|
position.z() += mHeight * mHeightScale;
|
||||||
|
|
||||||
// We subtract 10.f here and add it within focalPointOffset in order to avoid camera clipping through ceiling.
|
// We subtract 10.f here and add it within focalPointOffset in order to avoid camera clipping through ceiling.
|
||||||
// Needed because character's head can be a bit higher than collision area.
|
// Needed because character's head can be a bit higher than collision area.
|
||||||
position.z() -= 10.f;
|
position.z() -= 10.f;
|
||||||
|
|
||||||
position += getFocalPointOffset() + mFocalPointAdjustment;
|
|
||||||
}
|
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
|
|
||||||
osg::Vec3d Camera::getFocalPointOffset() const
|
osg::Vec3d Camera::getFocalPointOffset() const
|
||||||
{
|
{
|
||||||
osg::Vec3d offset(0, 0, 10.f);
|
osg::Vec3d offset(0, 0, 10.f);
|
||||||
offset.x() += mFocalPointCurrentOffset.x() * cos(getYaw());
|
offset.x() += mFocalPointCurrentOffset.x() * cos(mYaw);
|
||||||
offset.y() += mFocalPointCurrentOffset.x() * sin(getYaw());
|
offset.y() += mFocalPointCurrentOffset.x() * sin(mYaw);
|
||||||
offset.z() += mFocalPointCurrentOffset.y();
|
offset.z() += mFocalPointCurrentOffset.y();
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::getPosition(osg::Vec3d &focal, osg::Vec3d &camera) const
|
|
||||||
{
|
|
||||||
focal = getFocalPoint();
|
|
||||||
osg::Vec3d offset(0,0,0);
|
|
||||||
if (!isFirstPerson())
|
|
||||||
{
|
|
||||||
osg::Quat orient = osg::Quat(getPitch(), osg::Vec3d(1,0,0)) * osg::Quat(getYaw(), osg::Vec3d(0,0,1));
|
|
||||||
offset = orient * osg::Vec3d(0.f, -mCameraDistance, 0.f);
|
|
||||||
}
|
|
||||||
camera = focal + offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Camera::updateCamera(osg::Camera *cam)
|
void Camera::updateCamera(osg::Camera *cam)
|
||||||
{
|
{
|
||||||
osg::Vec3d focal, position;
|
|
||||||
getPosition(focal, position);
|
|
||||||
|
|
||||||
osg::Quat orient = osg::Quat(mRoll, osg::Vec3d(0, 1, 0)) * osg::Quat(mPitch, osg::Vec3d(1, 0, 0)) * osg::Quat(mYaw, osg::Vec3d(0, 0, 1));
|
osg::Quat orient = osg::Quat(mRoll, osg::Vec3d(0, 1, 0)) * osg::Quat(mPitch, osg::Vec3d(1, 0, 0)) * osg::Quat(mYaw, osg::Vec3d(0, 0, 1));
|
||||||
osg::Vec3d forward = orient * osg::Vec3d(0,1,0);
|
osg::Vec3d forward = orient * osg::Vec3d(0,1,0);
|
||||||
osg::Vec3d up = orient * osg::Vec3d(0,0,1);
|
osg::Vec3d up = orient * osg::Vec3d(0,0,1);
|
||||||
|
|
||||||
cam->setViewMatrixAsLookAt(position, position + forward, up);
|
cam->setViewMatrixAsLookAt(mPosition, mPosition + forward, up);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::updateHeadBobbing(float duration) {
|
void Camera::updateHeadBobbing(float duration) {
|
||||||
|
@ -176,42 +157,12 @@ namespace MWRender
|
||||||
mRoll = osg::sign(stepState) * effect * coef * maxRoll; // range from -maxRoll to maxRoll
|
mRoll = osg::sign(stepState) * effect * coef * maxRoll; // range from -maxRoll to maxRoll
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::reset()
|
|
||||||
{
|
|
||||||
togglePreviewMode(false);
|
|
||||||
toggleVanityMode(false);
|
|
||||||
if (!mFirstPersonView)
|
|
||||||
toggleViewMode();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Camera::rotateCamera(float pitch, float yaw, bool adjust)
|
|
||||||
{
|
|
||||||
if (adjust)
|
|
||||||
{
|
|
||||||
pitch += getPitch();
|
|
||||||
yaw += getYaw();
|
|
||||||
}
|
|
||||||
setYaw(yaw);
|
|
||||||
setPitch(pitch);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Camera::update(float duration, bool paused)
|
void Camera::update(float duration, bool paused)
|
||||||
{
|
{
|
||||||
if (mAnimation->upperBodyReady())
|
if (mQueuedMode && mAnimation->upperBodyReady())
|
||||||
{
|
setMode(*mQueuedMode);
|
||||||
// Now process the view changes we queued earlier
|
if (mProcessViewChange)
|
||||||
if (mVanityToggleQueued)
|
processViewChange();
|
||||||
{
|
|
||||||
toggleVanityMode(mVanityToggleQueuedValue);
|
|
||||||
mVanityToggleQueued = false;
|
|
||||||
}
|
|
||||||
if (mViewModeToggleQueued)
|
|
||||||
{
|
|
||||||
togglePreviewMode(false);
|
|
||||||
toggleViewMode();
|
|
||||||
mViewModeToggleQueued = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (paused)
|
if (paused)
|
||||||
return;
|
return;
|
||||||
|
@ -222,9 +173,9 @@ namespace MWRender
|
||||||
&& (mFirstPersonView || mShowCrosshairInThirdPersonMode));
|
&& (mFirstPersonView || mShowCrosshairInThirdPersonMode));
|
||||||
|
|
||||||
if(mMode == Mode::Vanity)
|
if(mMode == Mode::Vanity)
|
||||||
rotateCamera(0.f, osg::DegreesToRadians(3.f * duration), true);
|
setYaw(mYaw + osg::DegreesToRadians(3.f) * duration);
|
||||||
|
|
||||||
if (isFirstPerson() && mHeadBobbingEnabled)
|
if (mMode == Mode::FirstPerson && mHeadBobbingEnabled)
|
||||||
updateHeadBobbing(duration);
|
updateHeadBobbing(duration);
|
||||||
else
|
else
|
||||||
mRoll = mHeadBobbingOffset = 0;
|
mRoll = mHeadBobbingOffset = 0;
|
||||||
|
@ -244,19 +195,24 @@ namespace MWRender
|
||||||
|
|
||||||
void Camera::updatePosition()
|
void Camera::updatePosition()
|
||||||
{
|
{
|
||||||
mFocalPointAdjustment = osg::Vec3d();
|
if (mMode == Mode::Static)
|
||||||
if (isFirstPerson())
|
|
||||||
return;
|
return;
|
||||||
|
if (mMode == Mode::FirstPerson)
|
||||||
|
{
|
||||||
|
mPosition = getTrackingNodePosition();
|
||||||
|
mPosition.z() += mHeadBobbingOffset;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const float cameraObstacleLimit = 5.0f;
|
constexpr float cameraObstacleLimit = 5.0f;
|
||||||
const float focalObstacleLimit = 10.f;
|
constexpr float focalObstacleLimit = 10.f;
|
||||||
const int collisionType = (MWPhysics::CollisionType::CollisionType_Default & ~MWPhysics::CollisionType::CollisionType_Actor);
|
|
||||||
|
|
||||||
const auto* rayCasting = MWBase::Environment::get().getWorld()->getRayCasting();
|
const auto* rayCasting = MWBase::Environment::get().getWorld()->getRayCasting();
|
||||||
|
constexpr int collisionType = (MWPhysics::CollisionType::CollisionType_Default & ~MWPhysics::CollisionType::CollisionType_Actor);
|
||||||
|
|
||||||
// Adjust focal point to prevent clipping.
|
// Adjust focal point to prevent clipping.
|
||||||
osg::Vec3d focal = getFocalPoint();
|
|
||||||
osg::Vec3d focalOffset = getFocalPointOffset();
|
osg::Vec3d focalOffset = getFocalPointOffset();
|
||||||
|
osg::Vec3d focal = getThirdPersonBasePosition() + focalOffset;
|
||||||
float offsetLen = focalOffset.length();
|
float offsetLen = focalOffset.length();
|
||||||
if (offsetLen > 0)
|
if (offsetLen > 0)
|
||||||
{
|
{
|
||||||
|
@ -264,39 +220,73 @@ namespace MWRender
|
||||||
if (result.mHit)
|
if (result.mHit)
|
||||||
{
|
{
|
||||||
double adjustmentCoef = -(result.mHitPos + result.mHitNormal * focalObstacleLimit - focal).length() / offsetLen;
|
double adjustmentCoef = -(result.mHitPos + result.mHitNormal * focalObstacleLimit - focal).length() / offsetLen;
|
||||||
mFocalPointAdjustment = focalOffset * std::max(-1.0, adjustmentCoef);
|
focal += focalOffset * std::max(-1.0, adjustmentCoef);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate camera distance.
|
// Calculate offset from focal point.
|
||||||
mCameraDistance = mBaseCameraDistance + getCameraDistanceCorrection();
|
mCameraDistance = mBaseCameraDistance + getCameraDistanceCorrection();
|
||||||
if (mDynamicCameraDistanceEnabled)
|
if (mDynamicCameraDistanceEnabled)
|
||||||
mCameraDistance = std::min(mCameraDistance, mMaxNextCameraDistance);
|
mCameraDistance = std::min(mCameraDistance, mMaxNextCameraDistance);
|
||||||
osg::Vec3d cameraPos;
|
|
||||||
getPosition(focal, cameraPos);
|
osg::Quat orient = osg::Quat(getPitch(), osg::Vec3d(1,0,0)) * osg::Quat(getYaw(), osg::Vec3d(0,0,1));
|
||||||
MWPhysics::RayCastingResult result = rayCasting->castSphere(focal, cameraPos, cameraObstacleLimit, collisionType);
|
osg::Vec3d offset = orient * osg::Vec3d(0.f, -mCameraDistance, 0.f);
|
||||||
|
MWPhysics::RayCastingResult result = rayCasting->castSphere(focal, focal + offset, cameraObstacleLimit, collisionType);
|
||||||
if (result.mHit)
|
if (result.mHit)
|
||||||
|
{
|
||||||
mCameraDistance = (result.mHitPos + result.mHitNormal * cameraObstacleLimit - focal).length();
|
mCameraDistance = (result.mHitPos + result.mHitNormal * cameraObstacleLimit - focal).length();
|
||||||
|
offset = orient * osg::Vec3d(0.f, -mCameraDistance, 0.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
mPosition = focal + offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::setMode(Mode newMode, bool force)
|
||||||
|
{
|
||||||
|
if (newMode == Mode::StandingPreview)
|
||||||
|
newMode = Mode::ThirdPerson;
|
||||||
|
if (mMode == newMode)
|
||||||
|
return;
|
||||||
|
Mode oldMode = mMode;
|
||||||
|
if (!force && (newMode == Mode::FirstPerson || oldMode == Mode::FirstPerson) && !mAnimation->upperBodyReady())
|
||||||
|
{
|
||||||
|
// Changing the view will stop all playing animations, so if we are playing
|
||||||
|
// anything important, queue the view change for later
|
||||||
|
mQueuedMode = newMode;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
mMode = newMode;
|
||||||
|
mQueuedMode = std::nullopt;
|
||||||
|
if (newMode == Mode::FirstPerson)
|
||||||
|
mFirstPersonView = true;
|
||||||
|
else if (newMode == Mode::ThirdPerson)
|
||||||
|
mFirstPersonView = false;
|
||||||
|
calculateDeferredRotation();
|
||||||
|
if (oldMode == Mode::FirstPerson || newMode == Mode::FirstPerson)
|
||||||
|
{
|
||||||
|
instantTransition();
|
||||||
|
mProcessViewChange = true;
|
||||||
|
}
|
||||||
|
else if (newMode == Mode::ThirdPerson)
|
||||||
|
updateStandingPreviewMode();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::updateStandingPreviewMode()
|
void Camera::updateStandingPreviewMode()
|
||||||
{
|
{
|
||||||
if (!mStandingPreviewAllowed)
|
|
||||||
return;
|
|
||||||
float speed = mTrackingPtr.getClass().getCurrentSpeed(mTrackingPtr);
|
float speed = mTrackingPtr.getClass().getCurrentSpeed(mTrackingPtr);
|
||||||
bool combat = mTrackingPtr.getClass().isActor() &&
|
bool combat = mTrackingPtr.getClass().isActor() &&
|
||||||
mTrackingPtr.getClass().getCreatureStats(mTrackingPtr).getDrawState() != MWMechanics::DrawState_Nothing;
|
mTrackingPtr.getClass().getCreatureStats(mTrackingPtr).getDrawState() != MWMechanics::DrawState_Nothing;
|
||||||
bool standingStill = speed == 0 && !combat && !mFirstPersonView;
|
bool standingStill = speed == 0 && !combat && mStandingPreviewAllowed;
|
||||||
if (!standingStill && mMode == Mode::StandingPreview)
|
if (!standingStill && mMode == Mode::StandingPreview)
|
||||||
{
|
{
|
||||||
mMode = Mode::Normal;
|
mMode = Mode::ThirdPerson;
|
||||||
calculateDeferredRotation();
|
calculateDeferredRotation();
|
||||||
}
|
}
|
||||||
else if (standingStill && mMode == Mode::Normal)
|
else if (standingStill && mMode == Mode::ThirdPerson)
|
||||||
mMode = Mode::StandingPreview;
|
mMode = Mode::StandingPreview;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::setFocalPointTargetOffset(osg::Vec2d v)
|
void Camera::setFocalPointTargetOffset(const osg::Vec2d& v)
|
||||||
{
|
{
|
||||||
mFocalPointTargetOffset = v;
|
mFocalPointTargetOffset = v;
|
||||||
mPreviousTransitionSpeed = mFocalPointTransitionSpeed;
|
mPreviousTransitionSpeed = mFocalPointTransitionSpeed;
|
||||||
|
@ -346,78 +336,35 @@ namespace MWRender
|
||||||
|
|
||||||
void Camera::toggleViewMode(bool force)
|
void Camera::toggleViewMode(bool force)
|
||||||
{
|
{
|
||||||
// Changing the view will stop all playing animations, so if we are playing
|
setMode(mFirstPersonView ? Mode::ThirdPerson : Mode::FirstPerson, force);
|
||||||
// anything important, queue the view change for later
|
|
||||||
if (!mAnimation->upperBodyReady() && !force)
|
|
||||||
{
|
|
||||||
mViewModeToggleQueued = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
mViewModeToggleQueued = false;
|
|
||||||
|
|
||||||
mFirstPersonView = !mFirstPersonView;
|
|
||||||
updateStandingPreviewMode();
|
|
||||||
instantTransition();
|
|
||||||
processViewChange();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::allowVanityMode(bool allow)
|
void Camera::allowVanityMode(bool allow)
|
||||||
{
|
{
|
||||||
if (!allow && mMode == Mode::Vanity)
|
if (!allow && mMode == Mode::Vanity)
|
||||||
{
|
|
||||||
disableDeferredPreviewRotation();
|
|
||||||
toggleVanityMode(false);
|
toggleVanityMode(false);
|
||||||
}
|
|
||||||
mVanityAllowed = allow;
|
mVanityAllowed = allow;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Camera::toggleVanityMode(bool enable)
|
bool Camera::toggleVanityMode(bool enable)
|
||||||
{
|
{
|
||||||
// Changing the view will stop all playing animations, so if we are playing
|
|
||||||
// anything important, queue the view change for later
|
|
||||||
if (mFirstPersonView && !mAnimation->upperBodyReady())
|
|
||||||
{
|
|
||||||
mVanityToggleQueued = true;
|
|
||||||
mVanityToggleQueuedValue = enable;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!mVanityAllowed && enable)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if ((mMode == Mode::Vanity) == enable)
|
|
||||||
return true;
|
|
||||||
mMode = enable ? Mode::Vanity : Mode::Normal;
|
|
||||||
if (!mDeferredRotationAllowed)
|
|
||||||
disableDeferredPreviewRotation();
|
|
||||||
if (!enable)
|
if (!enable)
|
||||||
calculateDeferredRotation();
|
setMode(mFirstPersonView ? Mode::FirstPerson : Mode::ThirdPerson, false);
|
||||||
|
else if (mVanityAllowed)
|
||||||
processViewChange();
|
setMode(Mode::Vanity, false);
|
||||||
return true;
|
return (mMode == Mode::Vanity) == enable;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::togglePreviewMode(bool enable)
|
void Camera::togglePreviewMode(bool enable)
|
||||||
{
|
{
|
||||||
if (mFirstPersonView && !mAnimation->upperBodyReady())
|
if (mFirstPersonView && !mAnimation->upperBodyReady())
|
||||||
return;
|
return;
|
||||||
|
if ((mMode == Mode::Preview) == enable)
|
||||||
if((mMode == Mode::Preview) == enable)
|
|
||||||
return;
|
return;
|
||||||
|
if (enable)
|
||||||
mMode = enable ? Mode::Preview : Mode::Normal;
|
setMode(Mode::Preview);
|
||||||
if (mMode == Mode::Normal)
|
else
|
||||||
updateStandingPreviewMode();
|
setMode(mFirstPersonView ? Mode::FirstPerson : Mode::ThirdPerson);
|
||||||
else if (mFirstPersonView)
|
|
||||||
instantTransition();
|
|
||||||
if (mMode == Mode::Normal)
|
|
||||||
{
|
|
||||||
if (!mDeferredRotationAllowed)
|
|
||||||
disableDeferredPreviewRotation();
|
|
||||||
calculateDeferredRotation();
|
|
||||||
}
|
|
||||||
processViewChange();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::setSneakOffset(float offset)
|
void Camera::setSneakOffset(float offset)
|
||||||
|
@ -439,16 +386,16 @@ namespace MWRender
|
||||||
|
|
||||||
float Camera::getCameraDistance() const
|
float Camera::getCameraDistance() const
|
||||||
{
|
{
|
||||||
if (isFirstPerson())
|
return mMode == Mode::FirstPerson ? 0.f : mCameraDistance;
|
||||||
return 0.f;
|
|
||||||
return mCameraDistance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::adjustCameraDistance(float delta)
|
void Camera::adjustCameraDistance(float delta)
|
||||||
{
|
{
|
||||||
if (!isFirstPerson())
|
if (mMode == Mode::Static)
|
||||||
|
return;
|
||||||
|
if (mMode != Mode::FirstPerson)
|
||||||
{
|
{
|
||||||
if(isNearest() && delta < 0.f && getMode() != Mode::Preview && getMode() != Mode::Vanity)
|
if (mIsNearest && delta < 0.f && mMode != Mode::Preview && mMode != Mode::Vanity)
|
||||||
toggleViewMode();
|
toggleViewMode();
|
||||||
else
|
else
|
||||||
mBaseCameraDistance = std::min(mCameraDistance - getCameraDistanceCorrection(), mBaseCameraDistance) + delta;
|
mBaseCameraDistance = std::min(mCameraDistance - getCameraDistanceCorrection(), mBaseCameraDistance) + delta;
|
||||||
|
@ -480,12 +427,12 @@ namespace MWRender
|
||||||
void Camera::setAnimation(NpcAnimation *anim)
|
void Camera::setAnimation(NpcAnimation *anim)
|
||||||
{
|
{
|
||||||
mAnimation = anim;
|
mAnimation = anim;
|
||||||
processViewChange();
|
mProcessViewChange = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::processViewChange()
|
void Camera::processViewChange()
|
||||||
{
|
{
|
||||||
if(isFirstPerson())
|
if (mMode == Mode::FirstPerson)
|
||||||
{
|
{
|
||||||
mAnimation->setViewMode(NpcAnimation::VM_FirstPerson);
|
mAnimation->setViewMode(NpcAnimation::VM_FirstPerson);
|
||||||
mTrackingNode = mAnimation->getNode("Camera");
|
mTrackingNode = mAnimation->getNode("Camera");
|
||||||
|
@ -503,12 +450,12 @@ namespace MWRender
|
||||||
else
|
else
|
||||||
mHeightScale = 1.f;
|
mHeightScale = 1.f;
|
||||||
}
|
}
|
||||||
rotateCamera(getPitch(), getYaw(), false);
|
mProcessViewChange = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::applyDeferredPreviewRotationToPlayer(float dt)
|
void Camera::applyDeferredPreviewRotationToPlayer(float dt)
|
||||||
{
|
{
|
||||||
if (isVanityOrPreviewModeEnabled() || mTrackingPtr.isEmpty())
|
if (mMode != Mode::ThirdPerson || mTrackingPtr.isEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
osg::Vec3f rot = mDeferredRotation;
|
osg::Vec3f rot = mDeferredRotation;
|
||||||
|
@ -541,6 +488,8 @@ namespace MWRender
|
||||||
|
|
||||||
void Camera::rotateCameraToTrackingPtr()
|
void Camera::rotateCameraToTrackingPtr()
|
||||||
{
|
{
|
||||||
|
if (mMode == Mode::Static)
|
||||||
|
return;
|
||||||
setPitch(-mTrackingPtr.getRefData().getPosition().rot[0] - mDeferredRotation.x());
|
setPitch(-mTrackingPtr.getRefData().getPosition().rot[0] - mDeferredRotation.x());
|
||||||
setYaw(-mTrackingPtr.getRefData().getPosition().rot[2] - mDeferredRotation.z());
|
setYaw(-mTrackingPtr.getRefData().getPosition().rot[2] - mDeferredRotation.z());
|
||||||
}
|
}
|
||||||
|
@ -555,8 +504,13 @@ namespace MWRender
|
||||||
|
|
||||||
void Camera::calculateDeferredRotation()
|
void Camera::calculateDeferredRotation()
|
||||||
{
|
{
|
||||||
|
if (mMode == Mode::Static)
|
||||||
|
{
|
||||||
|
mDeferredRotation = osg::Vec3f();
|
||||||
|
return;
|
||||||
|
}
|
||||||
MWWorld::Ptr ptr = mTrackingPtr;
|
MWWorld::Ptr ptr = mTrackingPtr;
|
||||||
if (isVanityOrPreviewModeEnabled() || ptr.isEmpty())
|
if (mMode == Mode::Preview || mMode == Mode::Vanity || ptr.isEmpty())
|
||||||
return;
|
return;
|
||||||
if (mFirstPersonView)
|
if (mFirstPersonView)
|
||||||
{
|
{
|
||||||
|
@ -566,6 +520,13 @@ namespace MWRender
|
||||||
|
|
||||||
mDeferredRotation.x() = Misc::normalizeAngle(-ptr.getRefData().getPosition().rot[0] - mPitch);
|
mDeferredRotation.x() = Misc::normalizeAngle(-ptr.getRefData().getPosition().rot[0] - mPitch);
|
||||||
mDeferredRotation.z() = Misc::normalizeAngle(-ptr.getRefData().getPosition().rot[2] - mYaw);
|
mDeferredRotation.z() = Misc::normalizeAngle(-ptr.getRefData().getPosition().rot[2] - mYaw);
|
||||||
|
if (!mDeferredRotationAllowed)
|
||||||
|
mDeferredRotationDisabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Camera::isVanityOrPreviewModeEnabled() const
|
||||||
|
{
|
||||||
|
return mMode == Mode::Vanity || mMode == Mode::Preview || mMode == Mode::StandingPreview;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#ifndef GAME_MWRENDER_CAMERA_H
|
#ifndef GAME_MWRENDER_CAMERA_H
|
||||||
#define GAME_MWRENDER_CAMERA_H
|
#define GAME_MWRENDER_CAMERA_H
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include <osg/ref_ptr>
|
#include <osg/ref_ptr>
|
||||||
|
@ -24,7 +25,7 @@ namespace MWRender
|
||||||
class Camera
|
class Camera
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
enum class Mode { Normal, Vanity, Preview, StandingPreview };
|
enum class Mode : int {Static = 0, FirstPerson = 1, ThirdPerson = 2, Vanity = 3, Preview = 4, StandingPreview = 5};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MWWorld::Ptr mTrackingPtr;
|
MWWorld::Ptr mTrackingPtr;
|
||||||
|
@ -35,8 +36,12 @@ namespace MWRender
|
||||||
|
|
||||||
NpcAnimation *mAnimation;
|
NpcAnimation *mAnimation;
|
||||||
|
|
||||||
|
// Always 'true' if mMode == `FirstPerson`. Also it is 'true' in `Vanity` or `Preview` modes if
|
||||||
|
// the camera should return to `FirstPerson` view after it.
|
||||||
bool mFirstPersonView;
|
bool mFirstPersonView;
|
||||||
|
|
||||||
Mode mMode;
|
Mode mMode;
|
||||||
|
std::optional<Mode> mQueuedMode;
|
||||||
bool mVanityAllowed;
|
bool mVanityAllowed;
|
||||||
bool mStandingPreviewAllowed;
|
bool mStandingPreviewAllowed;
|
||||||
bool mDeferredRotationAllowed;
|
bool mDeferredRotationAllowed;
|
||||||
|
@ -45,17 +50,15 @@ namespace MWRender
|
||||||
float mFurthest;
|
float mFurthest;
|
||||||
bool mIsNearest;
|
bool mIsNearest;
|
||||||
|
|
||||||
|
bool mProcessViewChange;
|
||||||
|
|
||||||
float mHeight, mBaseCameraDistance;
|
float mHeight, mBaseCameraDistance;
|
||||||
float mPitch, mYaw, mRoll;
|
float mPitch, mYaw, mRoll;
|
||||||
|
osg::Vec3d mPosition;
|
||||||
bool mVanityToggleQueued;
|
|
||||||
bool mVanityToggleQueuedValue;
|
|
||||||
bool mViewModeToggleQueued;
|
|
||||||
|
|
||||||
float mCameraDistance;
|
float mCameraDistance;
|
||||||
float mMaxNextCameraDistance;
|
float mMaxNextCameraDistance;
|
||||||
|
|
||||||
osg::Vec3d mFocalPointAdjustment;
|
|
||||||
osg::Vec2d mFocalPointCurrentOffset;
|
osg::Vec2d mFocalPointCurrentOffset;
|
||||||
osg::Vec2d mFocalPointTargetOffset;
|
osg::Vec2d mFocalPointTargetOffset;
|
||||||
float mFocalPointTransitionSpeedCoef;
|
float mFocalPointTransitionSpeedCoef;
|
||||||
|
@ -78,6 +81,8 @@ namespace MWRender
|
||||||
float mTotalMovement; // Needed for head bobbing.
|
float mTotalMovement; // Needed for head bobbing.
|
||||||
void updateHeadBobbing(float duration);
|
void updateHeadBobbing(float duration);
|
||||||
|
|
||||||
|
osg::Vec3d getTrackingNodePosition() const;
|
||||||
|
osg::Vec3d getFocalPointOffset() const;
|
||||||
void updateFocalPointOffset(float duration);
|
void updateFocalPointOffset(float duration);
|
||||||
void updatePosition();
|
void updatePosition();
|
||||||
float getCameraDistanceCorrection() const;
|
float getCameraDistanceCorrection() const;
|
||||||
|
@ -99,7 +104,7 @@ namespace MWRender
|
||||||
MWWorld::Ptr getTrackingPtr() const { return mTrackingPtr; }
|
MWWorld::Ptr getTrackingPtr() const { return mTrackingPtr; }
|
||||||
|
|
||||||
void setFocalPointTransitionSpeed(float v) { mFocalPointTransitionSpeedCoef = v; }
|
void setFocalPointTransitionSpeed(float v) { mFocalPointTransitionSpeedCoef = v; }
|
||||||
void setFocalPointTargetOffset(osg::Vec2d v);
|
void setFocalPointTargetOffset(const osg::Vec2d& v);
|
||||||
void instantTransition();
|
void instantTransition();
|
||||||
void enableDynamicCameraDistance(bool v) { mDynamicCameraDistanceEnabled = v; }
|
void enableDynamicCameraDistance(bool v) { mDynamicCameraDistanceEnabled = v; }
|
||||||
void enableCrosshairInThirdPersonMode(bool v) { mShowCrosshairInThirdPersonMode = v; }
|
void enableCrosshairInThirdPersonMode(bool v) { mShowCrosshairInThirdPersonMode = v; }
|
||||||
|
@ -108,11 +113,8 @@ namespace MWRender
|
||||||
void updateCamera(osg::Camera* cam);
|
void updateCamera(osg::Camera* cam);
|
||||||
|
|
||||||
/// Reset to defaults
|
/// Reset to defaults
|
||||||
void reset();
|
void reset() { setMode(Mode::FirstPerson); }
|
||||||
|
|
||||||
/// Set where the camera is looking at. Uses Morrowind (euler) angles
|
|
||||||
/// \param rot Rotation angles in radians
|
|
||||||
void rotateCamera(float pitch, float yaw, bool adjust);
|
|
||||||
void rotateCameraToTrackingPtr();
|
void rotateCameraToTrackingPtr();
|
||||||
|
|
||||||
float getYaw() const { return mYaw; }
|
float getYaw() const { return mYaw; }
|
||||||
|
@ -136,8 +138,6 @@ namespace MWRender
|
||||||
/// \brief Lowers the camera for sneak.
|
/// \brief Lowers the camera for sneak.
|
||||||
void setSneakOffset(float offset);
|
void setSneakOffset(float offset);
|
||||||
|
|
||||||
bool isFirstPerson() const { return mFirstPersonView && mMode == Mode::Normal; }
|
|
||||||
|
|
||||||
void processViewChange();
|
void processViewChange();
|
||||||
|
|
||||||
void update(float duration, bool paused=false);
|
void update(float duration, bool paused=false);
|
||||||
|
@ -149,16 +149,12 @@ namespace MWRender
|
||||||
|
|
||||||
void setAnimation(NpcAnimation *anim);
|
void setAnimation(NpcAnimation *anim);
|
||||||
|
|
||||||
osg::Vec3d getFocalPoint() const;
|
osg::Vec3d getThirdPersonBasePosition() const;
|
||||||
osg::Vec3d getFocalPointOffset() const;
|
const osg::Vec3d& getPosition() const { return mPosition; }
|
||||||
|
|
||||||
/// Stores focal and camera world positions in passed arguments
|
bool isVanityOrPreviewModeEnabled() const;
|
||||||
void getPosition(osg::Vec3d &focal, osg::Vec3d &camera) const;
|
|
||||||
|
|
||||||
bool isVanityOrPreviewModeEnabled() const { return mMode != Mode::Normal; }
|
|
||||||
Mode getMode() const { return mMode; }
|
Mode getMode() const { return mMode; }
|
||||||
|
void setMode(Mode mode, bool force = true);
|
||||||
bool isNearest() const { return mIsNearest; }
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -830,11 +830,7 @@ namespace MWRender
|
||||||
mViewOverShoulderController->update();
|
mViewOverShoulderController->update();
|
||||||
mCamera->update(dt, paused);
|
mCamera->update(dt, paused);
|
||||||
|
|
||||||
osg::Vec3d focal, cameraPos;
|
bool isUnderwater = mWater->isUnderwater(mCamera->getPosition());
|
||||||
mCamera->getPosition(focal, cameraPos);
|
|
||||||
mCurrentCameraPos = cameraPos;
|
|
||||||
|
|
||||||
bool isUnderwater = mWater->isUnderwater(cameraPos);
|
|
||||||
mStateUpdater->setFogStart(mFog->getFogStart(isUnderwater));
|
mStateUpdater->setFogStart(mFog->getFogStart(isUnderwater));
|
||||||
mStateUpdater->setFogEnd(mFog->getFogEnd(isUnderwater));
|
mStateUpdater->setFogEnd(mFog->getFogEnd(isUnderwater));
|
||||||
setFogColor(mFog->getFogColor(isUnderwater));
|
setFogColor(mFog->getFogColor(isUnderwater));
|
||||||
|
|
|
@ -207,7 +207,6 @@ namespace MWRender
|
||||||
|
|
||||||
// camera stuff
|
// camera stuff
|
||||||
Camera* getCamera() { return mCamera.get(); }
|
Camera* getCamera() { return mCamera.get(); }
|
||||||
const osg::Vec3f& getCameraPosition() const { return mCurrentCameraPos; }
|
|
||||||
|
|
||||||
/// temporarily override the field of view with given value.
|
/// temporarily override the field of view with given value.
|
||||||
void overrideFieldOfView(float val);
|
void overrideFieldOfView(float val);
|
||||||
|
@ -285,7 +284,6 @@ namespace MWRender
|
||||||
osg::ref_ptr<SceneUtil::PositionAttitudeTransform> mPlayerNode;
|
osg::ref_ptr<SceneUtil::PositionAttitudeTransform> mPlayerNode;
|
||||||
std::unique_ptr<Camera> mCamera;
|
std::unique_ptr<Camera> mCamera;
|
||||||
std::unique_ptr<ViewOverShoulderController> mViewOverShoulderController;
|
std::unique_ptr<ViewOverShoulderController> mViewOverShoulderController;
|
||||||
osg::Vec3f mCurrentCameraPos;
|
|
||||||
|
|
||||||
osg::ref_ptr<StateUpdater> mStateUpdater;
|
osg::ref_ptr<StateUpdater> mStateUpdater;
|
||||||
osg::ref_ptr<SharedUniformStateUpdater> mSharedUniformStateUpdater;
|
osg::ref_ptr<SharedUniformStateUpdater> mSharedUniformStateUpdater;
|
||||||
|
|
|
@ -33,7 +33,7 @@ namespace MWRender
|
||||||
|
|
||||||
void ViewOverShoulderController::update()
|
void ViewOverShoulderController::update()
|
||||||
{
|
{
|
||||||
if (mCamera->isFirstPerson())
|
if (mCamera->getMode() == Camera::Mode::FirstPerson || mCamera->getMode() == Camera::Mode::Static)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Mode oldMode = mMode;
|
Mode oldMode = mMode;
|
||||||
|
@ -54,7 +54,7 @@ namespace MWRender
|
||||||
if (mCamera->getMode() == Camera::Mode::Vanity)
|
if (mCamera->getMode() == Camera::Mode::Vanity)
|
||||||
// Player doesn't touch controls for a long time. Transition should be very slow.
|
// Player doesn't touch controls for a long time. Transition should be very slow.
|
||||||
mCamera->setFocalPointTransitionSpeed(0.2f);
|
mCamera->setFocalPointTransitionSpeed(0.2f);
|
||||||
else if ((oldMode == Mode::Combat || mMode == Mode::Combat) && mCamera->getMode() == Camera::Mode::Normal)
|
else if ((oldMode == Mode::Combat || mMode == Mode::Combat) && mCamera->getMode() == Camera::Mode::ThirdPerson)
|
||||||
// Transition to/from combat mode and we are not it preview mode. Should be fast.
|
// Transition to/from combat mode and we are not it preview mode. Should be fast.
|
||||||
mCamera->setFocalPointTransitionSpeed(5.f);
|
mCamera->setFocalPointTransitionSpeed(5.f);
|
||||||
else
|
else
|
||||||
|
@ -77,14 +77,14 @@ namespace MWRender
|
||||||
|
|
||||||
void ViewOverShoulderController::trySwitchShoulder()
|
void ViewOverShoulderController::trySwitchShoulder()
|
||||||
{
|
{
|
||||||
if (mCamera->getMode() != Camera::Mode::Normal)
|
if (mCamera->getMode() != Camera::Mode::ThirdPerson)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const float limitToSwitch = 120; // switch to other shoulder if wall is closer than this limit
|
const float limitToSwitch = 120; // switch to other shoulder if wall is closer than this limit
|
||||||
const float limitToSwitchBack = 300; // switch back to default shoulder if there is no walls at this distance
|
const float limitToSwitchBack = 300; // switch back to default shoulder if there is no walls at this distance
|
||||||
|
|
||||||
auto orient = osg::Quat(mCamera->getYaw(), osg::Vec3d(0,0,1));
|
auto orient = osg::Quat(mCamera->getYaw(), osg::Vec3d(0,0,1));
|
||||||
osg::Vec3d playerPos = mCamera->getFocalPoint() - mCamera->getFocalPointOffset();
|
osg::Vec3d playerPos = mCamera->getThirdPersonBasePosition();
|
||||||
|
|
||||||
MWBase::World* world = MWBase::Environment::get().getWorld();
|
MWBase::World* world = MWBase::Environment::get().getWorld();
|
||||||
osg::Vec3d sideOffset = orient * osg::Vec3d(world->getHalfExtents(mCamera->getTrackingPtr()).x() - 1, 0, 0);
|
osg::Vec3d sideOffset = orient * osg::Vec3d(world->getHalfExtents(mCamera->getTrackingPtr()).x() - 1, 0, 0);
|
||||||
|
|
|
@ -595,13 +595,7 @@ namespace MWWorld
|
||||||
|
|
||||||
void World::useDeathCamera()
|
void World::useDeathCamera()
|
||||||
{
|
{
|
||||||
if(mRendering->getCamera()->isVanityOrPreviewModeEnabled() )
|
mRendering->getCamera()->setMode(MWRender::Camera::Mode::ThirdPerson);
|
||||||
{
|
|
||||||
mRendering->getCamera()->togglePreviewMode(false);
|
|
||||||
mRendering->getCamera()->toggleVanityMode(false);
|
|
||||||
}
|
|
||||||
if(mRendering->getCamera()->isFirstPerson())
|
|
||||||
mRendering->getCamera()->toggleViewMode(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MWWorld::Player& World::getPlayer()
|
MWWorld::Player& World::getPlayer()
|
||||||
|
@ -1858,7 +1852,7 @@ namespace MWWorld
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isWerewolf = player.getClass().getNpcStats(player).isWerewolf();
|
bool isWerewolf = player.getClass().getNpcStats(player).isWerewolf();
|
||||||
bool isFirstPerson = mRendering->getCamera()->isFirstPerson();
|
bool isFirstPerson = this->isFirstPerson();
|
||||||
if (isWerewolf && isFirstPerson)
|
if (isWerewolf && isFirstPerson)
|
||||||
{
|
{
|
||||||
float werewolfFov = Fallback::Map::getFloat("General_Werewolf_FOV");
|
float werewolfFov = Fallback::Map::getFloat("General_Werewolf_FOV");
|
||||||
|
@ -1928,11 +1922,12 @@ namespace MWWorld
|
||||||
|
|
||||||
void World::updateSoundListener()
|
void World::updateSoundListener()
|
||||||
{
|
{
|
||||||
|
osg::Vec3f cameraPosition = mRendering->getCamera()->getPosition();
|
||||||
const ESM::Position& refpos = getPlayerPtr().getRefData().getPosition();
|
const ESM::Position& refpos = getPlayerPtr().getRefData().getPosition();
|
||||||
osg::Vec3f listenerPos;
|
osg::Vec3f listenerPos;
|
||||||
|
|
||||||
if (isFirstPerson())
|
if (isFirstPerson())
|
||||||
listenerPos = mRendering->getCameraPosition();
|
listenerPos = cameraPosition;
|
||||||
else
|
else
|
||||||
listenerPos = refpos.asVec3() + osg::Vec3f(0, 0, 1.85f * mPhysics->getHalfExtents(getPlayerPtr()).z());
|
listenerPos = refpos.asVec3() + osg::Vec3f(0, 0, 1.85f * mPhysics->getHalfExtents(getPlayerPtr()).z());
|
||||||
|
|
||||||
|
@ -1943,7 +1938,7 @@ namespace MWWorld
|
||||||
osg::Vec3f forward = listenerOrient * osg::Vec3f(0,1,0);
|
osg::Vec3f forward = listenerOrient * osg::Vec3f(0,1,0);
|
||||||
osg::Vec3f up = listenerOrient * osg::Vec3f(0,0,1);
|
osg::Vec3f up = listenerOrient * osg::Vec3f(0,0,1);
|
||||||
|
|
||||||
bool underwater = isUnderwater(getPlayerPtr().getCell(), mRendering->getCameraPosition());
|
bool underwater = isUnderwater(getPlayerPtr().getCell(), cameraPosition);
|
||||||
|
|
||||||
MWBase::Environment::get().getSoundManager()->setListenerPosDir(listenerPos, forward, up, underwater);
|
MWBase::Environment::get().getSoundManager()->setListenerPosDir(listenerPos, forward, up, underwater);
|
||||||
}
|
}
|
||||||
|
@ -2395,7 +2390,7 @@ namespace MWWorld
|
||||||
|
|
||||||
bool World::isFirstPerson() const
|
bool World::isFirstPerson() const
|
||||||
{
|
{
|
||||||
return mRendering->getCamera()->isFirstPerson();
|
return mRendering->getCamera()->getMode() == MWRender::Camera::Mode::FirstPerson;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool World::isPreviewModeEnabled() const
|
bool World::isPreviewModeEnabled() const
|
||||||
|
@ -2430,10 +2425,12 @@ namespace MWWorld
|
||||||
|
|
||||||
bool World::vanityRotateCamera(float * rot)
|
bool World::vanityRotateCamera(float * rot)
|
||||||
{
|
{
|
||||||
if(!mRendering->getCamera()->isVanityOrPreviewModeEnabled())
|
auto* camera = mRendering->getCamera();
|
||||||
|
if(!camera->isVanityOrPreviewModeEnabled())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
mRendering->getCamera()->rotateCamera(rot[0], rot[2], true);
|
camera->setPitch(camera->getPitch() + rot[0]);
|
||||||
|
camera->setYaw(camera->getYaw() + rot[2]);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue