2013-04-29 12:50:40 +00:00
|
|
|
#include "camera.hpp"
|
2011-01-08 14:11:37 +00:00
|
|
|
|
2015-06-14 21:13:26 +00:00
|
|
|
#include <osg/Camera>
|
2012-08-08 20:15:52 +00:00
|
|
|
|
2015-11-20 20:57:04 +00:00
|
|
|
#include <components/sceneutil/positionattitudetransform.hpp>
|
2020-06-23 18:05:22 +00:00
|
|
|
#include <components/settings/settings.hpp>
|
2015-11-20 20:57:04 +00:00
|
|
|
|
2012-08-09 13:01:03 +00:00
|
|
|
#include "../mwbase/environment.hpp"
|
2012-08-17 14:55:21 +00:00
|
|
|
#include "../mwbase/windowmanager.hpp"
|
2020-06-19 20:03:11 +00:00
|
|
|
#include "../mwbase/world.hpp"
|
2012-08-09 13:01:03 +00:00
|
|
|
|
2020-06-21 23:54:08 +00:00
|
|
|
#include "../mwworld/class.hpp"
|
2012-08-08 20:15:52 +00:00
|
|
|
#include "../mwworld/ptr.hpp"
|
|
|
|
#include "../mwworld/refdata.hpp"
|
2012-07-03 13:32:38 +00:00
|
|
|
|
2020-06-21 23:54:08 +00:00
|
|
|
#include "../mwmechanics/drawstate.hpp"
|
|
|
|
#include "../mwmechanics/npcstats.hpp"
|
|
|
|
|
2012-08-14 16:33:29 +00:00
|
|
|
#include "npcanimation.hpp"
|
|
|
|
|
2015-05-21 21:54:39 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2015-09-24 13:21:42 +00:00
|
|
|
class UpdateRenderCameraCallback : public osg::NodeCallback
|
2015-05-21 21:54:39 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-09-24 13:21:42 +00:00
|
|
|
UpdateRenderCameraCallback(MWRender::Camera* cam)
|
2015-05-21 21:54:39 +00:00
|
|
|
: mCamera(cam)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
|
|
|
|
{
|
|
|
|
osg::Camera* cam = static_cast<osg::Camera*>(node);
|
|
|
|
|
|
|
|
// traverse first to update animations, in case the camera is attached to an animated node
|
|
|
|
traverse(node, nv);
|
|
|
|
|
|
|
|
mCamera->updateCamera(cam);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
MWRender::Camera* mCamera;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-01-08 14:11:37 +00:00
|
|
|
namespace MWRender
|
|
|
|
{
|
2015-05-21 21:54:39 +00:00
|
|
|
|
|
|
|
Camera::Camera (osg::Camera* camera)
|
2015-11-09 16:30:11 +00:00
|
|
|
: mHeightScale(1.f),
|
|
|
|
mCamera(camera),
|
2018-10-09 06:21:12 +00:00
|
|
|
mAnimation(nullptr),
|
2012-08-08 20:15:52 +00:00
|
|
|
mFirstPersonView(true),
|
2012-08-13 22:36:18 +00:00
|
|
|
mPreviewMode(false),
|
2013-07-29 14:43:16 +00:00
|
|
|
mNearest(30.f),
|
2013-07-29 14:45:35 +00:00
|
|
|
mFurthest(800.f),
|
|
|
|
mIsNearest(false),
|
2014-10-01 15:45:20 +00:00
|
|
|
mHeight(124.f),
|
2020-06-23 18:05:22 +00:00
|
|
|
mBaseCameraDistance(Settings::Manager::getFloat("third person camera distance", "Camera")),
|
2013-12-26 23:36:06 +00:00
|
|
|
mVanityToggleQueued(false),
|
2016-06-09 23:39:37 +00:00
|
|
|
mVanityToggleQueuedValue(false),
|
2015-05-21 21:54:39 +00:00
|
|
|
mViewModeToggleQueued(false),
|
2020-06-21 23:54:08 +00:00
|
|
|
mCameraDistance(0.f),
|
|
|
|
mThirdPersonMode(ThirdPersonViewMode::Standard),
|
2020-06-24 18:07:41 +00:00
|
|
|
mOverShoulderOffset(osg::Vec2f(30.0f, -10.0f)),
|
2020-06-19 20:03:11 +00:00
|
|
|
mDefaultShoulderIsRight(true),
|
|
|
|
mThirdPersionOffsetType(ThirdPersonOffsetType::RightShoulder),
|
|
|
|
mFocalPointCurrentOffset(osg::Vec2d()),
|
|
|
|
mFocalPointTransitionSpeed(1.f)
|
2012-05-30 13:52:39 +00:00
|
|
|
{
|
2012-08-14 10:37:48 +00:00
|
|
|
mVanity.enabled = false;
|
|
|
|
mVanity.allowed = true;
|
|
|
|
|
2013-12-30 20:47:06 +00:00
|
|
|
mPreviewCam.pitch = 0.f;
|
2012-08-13 22:36:18 +00:00
|
|
|
mPreviewCam.yaw = 0.f;
|
2012-08-16 09:15:38 +00:00
|
|
|
mPreviewCam.offset = 400.f;
|
2013-12-30 20:47:06 +00:00
|
|
|
mMainCam.pitch = 0.f;
|
2013-07-31 16:46:32 +00:00
|
|
|
mMainCam.yaw = 0.f;
|
|
|
|
mMainCam.offset = 400.f;
|
2015-05-21 21:54:39 +00:00
|
|
|
|
2020-06-21 23:54:08 +00:00
|
|
|
mCameraDistance = mBaseCameraDistance;
|
2016-06-09 23:05:43 +00:00
|
|
|
|
2015-09-24 13:21:42 +00:00
|
|
|
mUpdateCallback = new UpdateRenderCameraCallback(this);
|
2015-05-21 21:54:39 +00:00
|
|
|
mCamera->addUpdateCallback(mUpdateCallback);
|
2012-08-12 11:50:37 +00:00
|
|
|
}
|
2012-09-13 17:03:31 +00:00
|
|
|
|
2013-04-29 12:50:40 +00:00
|
|
|
Camera::~Camera()
|
2012-09-13 17:03:31 +00:00
|
|
|
{
|
2015-05-21 21:54:39 +00:00
|
|
|
mCamera->removeUpdateCallback(mUpdateCallback);
|
2012-09-13 17:03:31 +00:00
|
|
|
}
|
2012-07-03 13:32:38 +00:00
|
|
|
|
2015-05-21 21:54:39 +00:00
|
|
|
MWWorld::Ptr Camera::getTrackingPtr() const
|
2013-05-17 20:53:43 +00:00
|
|
|
{
|
2015-05-21 21:54:39 +00:00
|
|
|
return mTrackingPtr;
|
2013-05-17 20:53:43 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:54:08 +00:00
|
|
|
osg::Vec3d Camera::getFocalPoint() const
|
2012-07-03 13:32:38 +00:00
|
|
|
{
|
2015-05-21 21:54:39 +00:00
|
|
|
const osg::Node* trackNode = mTrackingNode;
|
|
|
|
if (!trackNode)
|
2015-06-01 13:34:46 +00:00
|
|
|
return osg::Vec3d();
|
2016-02-22 17:58:19 +00:00
|
|
|
osg::NodePathList nodepaths = trackNode->getParentalNodePaths();
|
|
|
|
if (nodepaths.empty())
|
2015-06-01 13:34:46 +00:00
|
|
|
return osg::Vec3d();
|
2016-02-22 17:58:19 +00:00
|
|
|
osg::Matrix worldMat = osg::computeLocalToWorld(nodepaths[0]);
|
2013-04-27 08:24:36 +00:00
|
|
|
|
2015-05-31 21:09:37 +00:00
|
|
|
osg::Vec3d position = worldMat.getTrans();
|
2015-05-21 21:54:39 +00:00
|
|
|
if (!isFirstPerson())
|
2020-06-21 23:54:08 +00:00
|
|
|
{
|
2015-11-09 16:30:11 +00:00
|
|
|
position.z() += mHeight * mHeightScale;
|
2020-06-21 23:54:08 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
position.z() -= 10.f;
|
|
|
|
|
|
|
|
position += getFocalPointOffset() + mFocalPointAdjustment;
|
|
|
|
}
|
2015-06-01 13:34:46 +00:00
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
2020-06-21 23:54:08 +00:00
|
|
|
osg::Vec3d Camera::getFocalPointOffset() const
|
|
|
|
{
|
|
|
|
osg::Vec3d offset(0, 0, 10.f);
|
|
|
|
if (mThirdPersonMode == ThirdPersonViewMode::OverShoulder && !mPreviewMode && !mVanity.enabled)
|
|
|
|
{
|
2020-06-19 20:03:11 +00:00
|
|
|
offset.x() += mFocalPointCurrentOffset.x() * cos(getYaw());
|
|
|
|
offset.y() += mFocalPointCurrentOffset.x() * sin(getYaw());
|
|
|
|
offset.z() += mFocalPointCurrentOffset.y();
|
2020-06-21 23:54:08 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-06-01 13:34:46 +00:00
|
|
|
void Camera::updateCamera(osg::Camera *cam)
|
|
|
|
{
|
|
|
|
if (mTrackingPtr.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2020-06-21 23:54:08 +00:00
|
|
|
osg::Vec3d focal, position;
|
|
|
|
getPosition(focal, position);
|
2014-10-01 16:55:35 +00:00
|
|
|
|
2015-05-31 21:09:37 +00:00
|
|
|
osg::Quat orient = osg::Quat(getPitch(), osg::Vec3d(1,0,0)) * osg::Quat(getYaw(), osg::Vec3d(0,0,1));
|
|
|
|
osg::Vec3d forward = orient * osg::Vec3d(0,1,0);
|
|
|
|
osg::Vec3d up = orient * osg::Vec3d(0,0,1);
|
2015-05-21 21:54:39 +00:00
|
|
|
|
|
|
|
cam->setViewMatrixAsLookAt(position, position + forward, up);
|
2012-08-08 20:15:52 +00:00
|
|
|
}
|
2012-08-09 06:24:18 +00:00
|
|
|
|
2015-05-21 21:54:39 +00:00
|
|
|
void Camera::reset()
|
2012-08-09 06:24:18 +00:00
|
|
|
{
|
2015-05-21 21:54:39 +00:00
|
|
|
togglePreviewMode(false);
|
|
|
|
toggleVanityMode(false);
|
|
|
|
if (!mFirstPersonView)
|
|
|
|
toggleViewMode();
|
2012-08-12 11:50:37 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 21:54:39 +00:00
|
|
|
void Camera::rotateCamera(float pitch, float yaw, bool adjust)
|
2012-08-12 11:50:37 +00:00
|
|
|
{
|
2015-05-21 21:54:39 +00:00
|
|
|
if (adjust)
|
2013-04-29 06:44:44 +00:00
|
|
|
{
|
2015-05-21 21:54:39 +00:00
|
|
|
pitch += getPitch();
|
|
|
|
yaw += getYaw();
|
2013-04-29 06:44:44 +00:00
|
|
|
}
|
2015-05-21 21:54:39 +00:00
|
|
|
setYaw(yaw);
|
|
|
|
setPitch(pitch);
|
2014-10-01 16:55:35 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 21:54:39 +00:00
|
|
|
void Camera::attachTo(const MWWorld::Ptr &ptr)
|
2014-10-01 16:55:35 +00:00
|
|
|
{
|
2015-05-21 21:54:39 +00:00
|
|
|
mTrackingPtr = ptr;
|
2012-08-12 11:50:37 +00:00
|
|
|
}
|
|
|
|
|
2013-07-30 21:24:18 +00:00
|
|
|
void Camera::update(float duration, bool paused)
|
2012-08-12 11:50:37 +00:00
|
|
|
{
|
2014-08-27 22:41:52 +00:00
|
|
|
if (mAnimation->upperBodyReady())
|
2013-12-26 23:36:06 +00:00
|
|
|
{
|
|
|
|
// Now process the view changes we queued earlier
|
|
|
|
if (mVanityToggleQueued)
|
|
|
|
{
|
2016-06-09 23:39:37 +00:00
|
|
|
toggleVanityMode(mVanityToggleQueuedValue);
|
2013-12-26 23:36:06 +00:00
|
|
|
mVanityToggleQueued = false;
|
|
|
|
}
|
|
|
|
if (mViewModeToggleQueued)
|
|
|
|
{
|
|
|
|
|
|
|
|
togglePreviewMode(false);
|
|
|
|
toggleViewMode();
|
|
|
|
mViewModeToggleQueued = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-30 21:24:18 +00:00
|
|
|
if (paused)
|
|
|
|
return;
|
2012-10-01 07:24:44 +00:00
|
|
|
|
2020-06-21 23:54:08 +00:00
|
|
|
// only show the crosshair in game mode
|
2013-04-29 06:44:44 +00:00
|
|
|
MWBase::WindowManager *wm = MWBase::Environment::get().getWindowManager();
|
2020-06-21 23:54:08 +00:00
|
|
|
wm->showCrosshair(!wm->isGuiMode() && !mVanity.enabled && !mPreviewMode
|
|
|
|
&& (mFirstPersonView || mThirdPersonMode != ThirdPersonViewMode::Standard));
|
2012-08-27 17:18:55 +00:00
|
|
|
|
2013-04-29 06:44:44 +00:00
|
|
|
if(mVanity.enabled)
|
|
|
|
{
|
2015-05-21 21:54:39 +00:00
|
|
|
rotateCamera(0.f, osg::DegreesToRadians(3.f * duration), true);
|
2012-08-12 11:50:37 +00:00
|
|
|
}
|
2020-06-21 23:54:08 +00:00
|
|
|
|
2020-06-19 20:03:11 +00:00
|
|
|
updateFocalPointOffset(duration);
|
2020-06-21 23:54:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 18:07:41 +00:00
|
|
|
void Camera::setOverShoulderOffset(float horizontal, float vertical)
|
|
|
|
{
|
2020-06-19 20:03:11 +00:00
|
|
|
mOverShoulderOffset = osg::Vec2f(std::abs(horizontal), vertical);
|
|
|
|
mDefaultShoulderIsRight = horizontal >= 0;
|
2020-06-24 18:07:41 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 20:03:11 +00:00
|
|
|
void Camera::switchToLeftShoulder()
|
2020-06-21 23:54:08 +00:00
|
|
|
{
|
2020-06-19 20:03:11 +00:00
|
|
|
if (mThirdPersionOffsetType == ThirdPersonOffsetType::RightShoulder)
|
|
|
|
mThirdPersionOffsetType = ThirdPersonOffsetType::LeftShoulder;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::switchToRightShoulder()
|
|
|
|
{
|
|
|
|
if (mThirdPersionOffsetType == ThirdPersonOffsetType::LeftShoulder)
|
|
|
|
mThirdPersionOffsetType = ThirdPersonOffsetType::RightShoulder;
|
|
|
|
}
|
2020-06-21 23:54:08 +00:00
|
|
|
|
2020-06-19 20:03:11 +00:00
|
|
|
void Camera::switchToDefaultShoulder()
|
|
|
|
{
|
|
|
|
if (mThirdPersionOffsetType == ThirdPersonOffsetType::LeftShoulder || mThirdPersionOffsetType == ThirdPersonOffsetType::RightShoulder)
|
|
|
|
mThirdPersionOffsetType = mDefaultShoulderIsRight ? ThirdPersonOffsetType::RightShoulder : ThirdPersonOffsetType::LeftShoulder;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::updateFocalPointOffset(float duration)
|
|
|
|
{
|
|
|
|
if (mThirdPersonMode == ThirdPersonViewMode::Standard)
|
|
|
|
return; // In Standard mode there is no focal point offset.
|
|
|
|
|
|
|
|
ThirdPersonOffsetType newOffsetType = mThirdPersionOffsetType;
|
|
|
|
if (mTrackingPtr.getClass().isActor() && mTrackingPtr.getClass().getCreatureStats(mTrackingPtr).getDrawState() != MWMechanics::DrawState_Nothing)
|
|
|
|
newOffsetType = ThirdPersonOffsetType::Combat;
|
|
|
|
else if (MWBase::Environment::get().getWorld()->isSwimming(mTrackingPtr))
|
|
|
|
newOffsetType = ThirdPersonOffsetType::Swimming;
|
|
|
|
else if (mThirdPersionOffsetType == ThirdPersonOffsetType::Combat || mThirdPersionOffsetType == ThirdPersonOffsetType::Swimming)
|
|
|
|
newOffsetType = mDefaultShoulderIsRight ? ThirdPersonOffsetType::RightShoulder : ThirdPersonOffsetType::LeftShoulder;
|
|
|
|
if (newOffsetType != mThirdPersionOffsetType)
|
|
|
|
{
|
|
|
|
if (newOffsetType == ThirdPersonOffsetType::Combat || mThirdPersionOffsetType == ThirdPersonOffsetType::Combat)
|
|
|
|
mFocalPointTransitionSpeed = 5;
|
|
|
|
else
|
|
|
|
mFocalPointTransitionSpeed = 1;
|
|
|
|
mThirdPersionOffsetType = newOffsetType;
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::Vec2d focalPointTargetOffset;
|
|
|
|
switch (mThirdPersionOffsetType)
|
|
|
|
{
|
|
|
|
case ThirdPersonOffsetType::RightShoulder:
|
|
|
|
focalPointTargetOffset = mOverShoulderOffset;
|
|
|
|
break;
|
|
|
|
case ThirdPersonOffsetType::LeftShoulder:
|
2020-06-19 22:46:26 +00:00
|
|
|
focalPointTargetOffset = mOverShoulderOffset;
|
2020-06-19 20:03:11 +00:00
|
|
|
focalPointTargetOffset.x() *= -1;
|
|
|
|
break;
|
|
|
|
case ThirdPersonOffsetType::Combat:
|
|
|
|
case ThirdPersonOffsetType::Swimming:
|
|
|
|
default:
|
|
|
|
focalPointTargetOffset = osg::Vec2d(0, 15);
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::Vec2d delta = focalPointTargetOffset - mFocalPointCurrentOffset;
|
|
|
|
if (delta.length2() > 0)
|
|
|
|
{
|
|
|
|
float coef = duration * (1.0 + 5.0 / delta.length()) * mFocalPointTransitionSpeed;
|
|
|
|
mFocalPointCurrentOffset += delta * std::min(coef, 1.0f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mFocalPointTransitionSpeed = 1.f;
|
2012-08-12 11:50:37 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 15:21:02 +00:00
|
|
|
void Camera::toggleViewMode(bool force)
|
2012-08-12 11:50:37 +00:00
|
|
|
{
|
2013-12-26 23:36:06 +00:00
|
|
|
// Changing the view will stop all playing animations, so if we are playing
|
|
|
|
// anything important, queue the view change for later
|
2014-08-27 22:41:52 +00:00
|
|
|
if (!mAnimation->upperBodyReady() && !force)
|
2013-12-26 23:36:06 +00:00
|
|
|
{
|
|
|
|
mViewModeToggleQueued = true;
|
|
|
|
return;
|
|
|
|
}
|
2014-07-03 17:37:12 +00:00
|
|
|
else
|
|
|
|
mViewModeToggleQueued = false;
|
2013-12-26 23:36:06 +00:00
|
|
|
|
2020-06-22 00:03:38 +00:00
|
|
|
if (mTrackingPtr.getClass().isActor())
|
|
|
|
mTrackingPtr.getClass().getCreatureStats(mTrackingPtr).setSideMovementAngle(0);
|
|
|
|
|
2012-08-12 11:50:37 +00:00
|
|
|
mFirstPersonView = !mFirstPersonView;
|
2013-07-13 23:12:38 +00:00
|
|
|
processViewChange();
|
2012-08-12 11:50:37 +00:00
|
|
|
}
|
2012-08-14 10:37:48 +00:00
|
|
|
|
2013-04-29 12:50:40 +00:00
|
|
|
void Camera::allowVanityMode(bool allow)
|
2012-08-14 10:37:48 +00:00
|
|
|
{
|
2013-04-27 08:24:36 +00:00
|
|
|
if (!allow && mVanity.enabled)
|
2012-08-14 10:37:48 +00:00
|
|
|
toggleVanityMode(false);
|
|
|
|
mVanity.allowed = allow;
|
|
|
|
}
|
2012-08-12 11:50:37 +00:00
|
|
|
|
2013-04-29 12:50:40 +00:00
|
|
|
bool Camera::toggleVanityMode(bool enable)
|
2012-08-12 11:50:37 +00:00
|
|
|
{
|
2013-12-26 23:36:06 +00:00
|
|
|
// Changing the view will stop all playing animations, so if we are playing
|
|
|
|
// anything important, queue the view change for later
|
2016-06-09 23:44:32 +00:00
|
|
|
if (mFirstPersonView && !mAnimation->upperBodyReady())
|
2013-12-26 23:36:06 +00:00
|
|
|
{
|
|
|
|
mVanityToggleQueued = true;
|
2016-06-09 23:39:37 +00:00
|
|
|
mVanityToggleQueuedValue = enable;
|
2013-12-26 23:36:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-27 08:24:36 +00:00
|
|
|
if(!mVanity.allowed && enable)
|
2012-08-14 10:37:48 +00:00
|
|
|
return false;
|
2013-04-27 08:24:36 +00:00
|
|
|
|
|
|
|
if(mVanity.enabled == enable)
|
2012-08-14 10:37:48 +00:00
|
|
|
return true;
|
|
|
|
mVanity.enabled = enable;
|
2012-08-12 14:35:35 +00:00
|
|
|
|
2013-07-13 23:12:38 +00:00
|
|
|
processViewChange();
|
2013-04-09 22:10:14 +00:00
|
|
|
|
2012-08-16 09:15:38 +00:00
|
|
|
float offset = mPreviewCam.offset;
|
2015-05-21 21:54:39 +00:00
|
|
|
|
2012-08-14 10:37:48 +00:00
|
|
|
if (mVanity.enabled) {
|
2015-05-21 21:54:39 +00:00
|
|
|
setPitch(osg::DegreesToRadians(-30.f));
|
|
|
|
mMainCam.offset = mCameraDistance;
|
2012-08-12 14:35:35 +00:00
|
|
|
} else {
|
2012-08-13 22:36:18 +00:00
|
|
|
offset = mMainCam.offset;
|
2012-08-14 18:39:42 +00:00
|
|
|
}
|
2013-04-27 08:24:36 +00:00
|
|
|
|
2015-05-21 21:54:39 +00:00
|
|
|
mCameraDistance = offset;
|
2012-08-14 10:37:48 +00:00
|
|
|
|
|
|
|
return true;
|
2012-08-12 11:50:37 +00:00
|
|
|
}
|
|
|
|
|
2013-04-29 12:50:40 +00:00
|
|
|
void Camera::togglePreviewMode(bool enable)
|
2012-08-12 11:50:37 +00:00
|
|
|
{
|
2014-08-27 22:41:52 +00:00
|
|
|
if (mFirstPersonView && !mAnimation->upperBodyReady())
|
2013-12-26 23:36:06 +00:00
|
|
|
return;
|
|
|
|
|
2013-04-27 08:24:36 +00:00
|
|
|
if(mPreviewMode == enable)
|
2012-08-13 22:36:18 +00:00
|
|
|
return;
|
2013-04-27 08:24:36 +00:00
|
|
|
|
2012-08-13 22:36:18 +00:00
|
|
|
mPreviewMode = enable;
|
2013-07-13 23:12:38 +00:00
|
|
|
processViewChange();
|
2013-04-27 08:24:36 +00:00
|
|
|
|
2015-05-21 21:54:39 +00:00
|
|
|
float offset = mCameraDistance;
|
2012-08-14 10:37:48 +00:00
|
|
|
if (mPreviewMode) {
|
|
|
|
mMainCam.offset = offset;
|
|
|
|
offset = mPreviewCam.offset;
|
|
|
|
} else {
|
|
|
|
mPreviewCam.offset = offset;
|
|
|
|
offset = mMainCam.offset;
|
2012-08-14 18:39:42 +00:00
|
|
|
}
|
2013-04-27 08:24:36 +00:00
|
|
|
|
2015-05-21 21:54:39 +00:00
|
|
|
mCameraDistance = offset;
|
2012-08-12 11:50:37 +00:00
|
|
|
}
|
2012-08-12 14:35:35 +00:00
|
|
|
|
2014-01-10 21:39:01 +00:00
|
|
|
void Camera::setSneakOffset(float offset)
|
2013-10-02 09:16:52 +00:00
|
|
|
{
|
2015-05-31 00:26:31 +00:00
|
|
|
mAnimation->setFirstPersonOffset(osg::Vec3f(0,0,-offset));
|
2013-10-02 09:16:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:54:08 +00:00
|
|
|
float Camera::getYaw() const
|
2012-08-12 14:35:35 +00:00
|
|
|
{
|
2013-04-27 08:24:36 +00:00
|
|
|
if(mVanity.enabled || mPreviewMode)
|
2012-08-13 22:36:18 +00:00
|
|
|
return mPreviewCam.yaw;
|
|
|
|
return mMainCam.yaw;
|
|
|
|
}
|
2012-08-12 14:35:35 +00:00
|
|
|
|
2013-04-29 12:50:40 +00:00
|
|
|
void Camera::setYaw(float angle)
|
2012-08-13 22:36:18 +00:00
|
|
|
{
|
2015-05-21 21:54:39 +00:00
|
|
|
if (angle > osg::PI) {
|
|
|
|
angle -= osg::PI*2;
|
|
|
|
} else if (angle < -osg::PI) {
|
|
|
|
angle += osg::PI*2;
|
2012-08-13 22:36:18 +00:00
|
|
|
}
|
2012-08-14 10:37:48 +00:00
|
|
|
if (mVanity.enabled || mPreviewMode) {
|
2012-08-13 22:36:18 +00:00
|
|
|
mPreviewCam.yaw = angle;
|
|
|
|
} else {
|
|
|
|
mMainCam.yaw = angle;
|
|
|
|
}
|
2012-08-12 14:35:35 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:54:08 +00:00
|
|
|
float Camera::getPitch() const
|
2012-08-12 14:35:35 +00:00
|
|
|
{
|
2012-08-14 10:37:48 +00:00
|
|
|
if (mVanity.enabled || mPreviewMode) {
|
2012-08-13 22:36:18 +00:00
|
|
|
return mPreviewCam.pitch;
|
|
|
|
}
|
|
|
|
return mMainCam.pitch;
|
|
|
|
}
|
2012-08-12 14:35:35 +00:00
|
|
|
|
2013-04-29 12:50:40 +00:00
|
|
|
void Camera::setPitch(float angle)
|
2012-08-14 10:37:48 +00:00
|
|
|
{
|
2013-04-27 08:24:36 +00:00
|
|
|
const float epsilon = 0.000001f;
|
2015-05-21 21:54:39 +00:00
|
|
|
float limit = osg::PI_2 - epsilon;
|
2013-04-27 08:24:36 +00:00
|
|
|
if(mPreviewMode)
|
|
|
|
limit /= 2;
|
|
|
|
|
|
|
|
if(angle > limit)
|
2013-04-07 13:52:43 +00:00
|
|
|
angle = limit;
|
2013-04-27 08:24:36 +00:00
|
|
|
else if(angle < -limit)
|
2013-04-07 13:52:43 +00:00
|
|
|
angle = -limit;
|
2013-04-27 08:24:36 +00:00
|
|
|
|
2012-08-14 10:37:48 +00:00
|
|
|
if (mVanity.enabled || mPreviewMode) {
|
2012-08-13 22:36:18 +00:00
|
|
|
mPreviewCam.pitch = angle;
|
|
|
|
} else {
|
|
|
|
mMainCam.pitch = angle;
|
2012-08-12 14:35:35 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-14 10:37:48 +00:00
|
|
|
|
2014-01-01 22:59:17 +00:00
|
|
|
float Camera::getCameraDistance() const
|
|
|
|
{
|
2016-06-09 23:05:43 +00:00
|
|
|
if (isFirstPerson())
|
|
|
|
return 0.f;
|
2015-05-21 21:54:39 +00:00
|
|
|
return mCameraDistance;
|
2014-01-01 22:59:17 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 18:05:22 +00:00
|
|
|
void Camera::updateBaseCameraDistance(float dist, bool adjust)
|
2012-08-14 10:37:48 +00:00
|
|
|
{
|
2013-04-27 08:24:36 +00:00
|
|
|
if(mFirstPersonView && !mPreviewMode && !mVanity.enabled)
|
2012-08-16 09:15:38 +00:00
|
|
|
return;
|
2013-04-27 08:24:36 +00:00
|
|
|
|
2013-07-29 14:43:16 +00:00
|
|
|
mIsNearest = false;
|
|
|
|
|
2015-05-21 21:54:39 +00:00
|
|
|
if (adjust)
|
2020-06-21 23:54:08 +00:00
|
|
|
{
|
|
|
|
if (mVanity.enabled || mPreviewMode)
|
|
|
|
dist += mCameraDistance;
|
|
|
|
else
|
|
|
|
dist += std::min(mCameraDistance - getCameraDistanceCorrection(), mBaseCameraDistance);
|
|
|
|
}
|
2015-05-21 21:54:39 +00:00
|
|
|
|
2020-06-21 23:54:08 +00:00
|
|
|
|
|
|
|
if (dist >= mFurthest)
|
2015-05-21 21:54:39 +00:00
|
|
|
dist = mFurthest;
|
2020-06-21 23:54:08 +00:00
|
|
|
else if (dist <= mNearest)
|
|
|
|
{
|
2015-05-28 01:47:53 +00:00
|
|
|
dist = mNearest;
|
2013-07-29 14:43:16 +00:00
|
|
|
mIsNearest = true;
|
2012-08-16 09:15:38 +00:00
|
|
|
}
|
2020-06-21 23:54:08 +00:00
|
|
|
|
|
|
|
if (mVanity.enabled || mPreviewMode)
|
|
|
|
mPreviewCam.offset = dist;
|
|
|
|
else if (!mFirstPersonView)
|
2020-06-23 18:05:22 +00:00
|
|
|
{
|
2020-06-21 23:54:08 +00:00
|
|
|
mBaseCameraDistance = dist;
|
2020-06-23 18:05:22 +00:00
|
|
|
Settings::Manager::setFloat("third person camera distance", "Camera", dist);
|
|
|
|
}
|
2020-06-21 23:54:08 +00:00
|
|
|
setCameraDistance();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::setCameraDistance(float dist, bool adjust)
|
|
|
|
{
|
|
|
|
if(mFirstPersonView && !mPreviewMode && !mVanity.enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (adjust) dist += mCameraDistance;
|
|
|
|
|
|
|
|
if (dist >= mFurthest)
|
|
|
|
dist = mFurthest;
|
|
|
|
else if (dist < 10.f)
|
|
|
|
dist = 10.f;
|
2015-05-21 21:54:39 +00:00
|
|
|
mCameraDistance = dist;
|
2020-06-21 23:54:08 +00:00
|
|
|
}
|
2012-08-14 10:37:48 +00:00
|
|
|
|
2020-06-21 23:54:08 +00:00
|
|
|
float Camera::getCameraDistanceCorrection() const
|
|
|
|
{
|
|
|
|
return mThirdPersonMode != ThirdPersonViewMode::Standard ? std::max(-getPitch(), 0.f) * 50.f : 0;
|
2012-08-14 10:37:48 +00:00
|
|
|
}
|
2012-08-14 16:33:29 +00:00
|
|
|
|
2013-04-29 12:50:40 +00:00
|
|
|
void Camera::setCameraDistance()
|
2012-08-16 09:15:38 +00:00
|
|
|
{
|
2020-06-21 23:54:08 +00:00
|
|
|
if (mVanity.enabled || mPreviewMode)
|
2016-07-17 17:44:55 +00:00
|
|
|
mCameraDistance = mPreviewCam.offset;
|
2020-06-21 23:54:08 +00:00
|
|
|
else if (!mFirstPersonView)
|
|
|
|
mCameraDistance = mBaseCameraDistance + getCameraDistanceCorrection();
|
|
|
|
mFocalPointAdjustment = osg::Vec3d();
|
2012-08-16 09:15:38 +00:00
|
|
|
}
|
|
|
|
|
2013-04-29 12:50:40 +00:00
|
|
|
void Camera::setAnimation(NpcAnimation *anim)
|
2012-08-15 11:17:35 +00:00
|
|
|
{
|
|
|
|
mAnimation = anim;
|
2013-07-13 23:12:38 +00:00
|
|
|
|
|
|
|
processViewChange();
|
2012-08-15 11:17:35 +00:00
|
|
|
}
|
|
|
|
|
2013-07-13 23:12:38 +00:00
|
|
|
void Camera::processViewChange()
|
2012-08-14 16:33:29 +00:00
|
|
|
{
|
2013-07-13 23:12:38 +00:00
|
|
|
if(isFirstPerson())
|
|
|
|
{
|
|
|
|
mAnimation->setViewMode(NpcAnimation::VM_FirstPerson);
|
2015-05-30 23:07:43 +00:00
|
|
|
mTrackingNode = mAnimation->getNode("Camera");
|
2015-06-01 02:41:03 +00:00
|
|
|
if (!mTrackingNode)
|
|
|
|
mTrackingNode = mAnimation->getNode("Head");
|
2015-11-09 16:30:11 +00:00
|
|
|
mHeightScale = 1.f;
|
2013-07-13 23:12:38 +00:00
|
|
|
}
|
2014-10-01 16:55:35 +00:00
|
|
|
else
|
2013-07-13 23:12:38 +00:00
|
|
|
{
|
|
|
|
mAnimation->setViewMode(NpcAnimation::VM_Normal);
|
2015-11-20 20:57:04 +00:00
|
|
|
SceneUtil::PositionAttitudeTransform* transform = mTrackingPtr.getRefData().getBaseNode();
|
2015-11-09 16:30:11 +00:00
|
|
|
mTrackingNode = transform;
|
|
|
|
if (transform)
|
2015-11-11 16:23:47 +00:00
|
|
|
mHeightScale = transform->getScale().z();
|
2015-11-09 16:30:11 +00:00
|
|
|
else
|
|
|
|
mHeightScale = 1.f;
|
2013-07-13 23:12:38 +00:00
|
|
|
}
|
2015-05-21 21:54:39 +00:00
|
|
|
rotateCamera(getPitch(), getYaw(), false);
|
2012-08-14 16:33:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:54:08 +00:00
|
|
|
bool Camera::isVanityOrPreviewModeEnabled() const
|
2013-03-07 23:12:56 +00:00
|
|
|
{
|
|
|
|
return mPreviewMode || mVanity.enabled;
|
|
|
|
}
|
2013-07-29 14:43:16 +00:00
|
|
|
|
2020-06-21 23:54:08 +00:00
|
|
|
bool Camera::isNearest() const
|
2013-07-29 14:43:16 +00:00
|
|
|
{
|
|
|
|
return mIsNearest;
|
|
|
|
}
|
2011-01-08 14:11:37 +00:00
|
|
|
}
|