1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-15 21:19:57 +00:00
openmw-tes3mp/apps/openmw/mwrender/camera.cpp

411 lines
10 KiB
C++
Raw Normal View History

2013-04-29 12:50:40 +00:00
#include "camera.hpp"
2015-06-14 21:13:26 +00:00
#include <osg/Camera>
#include <components/sceneutil/positionattitudetransform.hpp>
#include "../mwbase/environment.hpp"
#include "../mwbase/windowmanager.hpp"
#include "../mwworld/ptr.hpp"
#include "../mwworld/refdata.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;
};
}
namespace MWRender
{
2015-05-21 21:54:39 +00:00
Camera::Camera (osg::Camera* camera)
: mHeightScale(1.f),
mCamera(camera),
mAnimation(NULL),
mFirstPersonView(true),
2012-08-13 22:36:18 +00:00
mPreviewMode(false),
mFreeLook(true),
mNearest(30.f),
2013-07-29 14:45:35 +00:00
mFurthest(800.f),
mIsNearest(false),
mHeight(124.f),
2015-05-21 21:54:39 +00:00
mMaxCameraDistance(192.f),
mVanityToggleQueued(false),
mVanityToggleQueuedValue(false),
2015-05-21 21:54:39 +00:00
mViewModeToggleQueued(false),
mCameraDistance(0.f)
{
2012-08-14 10:37:48 +00:00
mVanity.enabled = false;
mVanity.allowed = true;
mPreviewCam.pitch = 0.f;
2012-08-13 22:36:18 +00:00
mPreviewCam.yaw = 0.f;
mPreviewCam.offset = 400.f;
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
mCameraDistance = mMaxCameraDistance;
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
}
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
}
2015-06-01 13:34:46 +00:00
osg::Vec3d Camera::getFocalPoint()
{
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();
osg::NodePathList nodepaths = trackNode->getParentalNodePaths();
if (nodepaths.empty())
2015-06-01 13:34:46 +00:00
return osg::Vec3d();
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())
position.z() += mHeight * mHeightScale;
2015-06-01 13:34:46 +00:00
return position;
}
void Camera::updateCamera(osg::Camera *cam)
{
if (mTrackingPtr.isEmpty())
return;
osg::Vec3d position = getFocalPoint();
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));
2015-05-21 21:54:39 +00:00
osg::Vec3d offset = orient * osg::Vec3d(0, isFirstPerson() ? 0 : -mCameraDistance, 0);
2015-05-21 21:54:39 +00:00
position += offset;
2015-05-31 21:09:37 +00:00
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);
}
2015-05-21 21:54:39 +00:00
void Camera::reset()
{
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)
{
2015-05-21 21:54:39 +00:00
pitch += getPitch();
yaw += getYaw();
}
2015-05-21 21:54:39 +00:00
setYaw(yaw);
setPitch(pitch);
}
2015-05-21 21:54:39 +00:00
void Camera::attachTo(const MWWorld::Ptr &ptr)
{
2015-05-21 21:54:39 +00:00
mTrackingPtr = ptr;
2012-08-12 11:50:37 +00:00
}
void Camera::update(float duration, bool paused)
2012-08-12 11:50:37 +00:00
{
if (mAnimation->upperBodyReady())
{
// Now process the view changes we queued earlier
if (mVanityToggleQueued)
{
toggleVanityMode(mVanityToggleQueuedValue);
mVanityToggleQueued = false;
}
if (mViewModeToggleQueued)
{
togglePreviewMode(false);
toggleViewMode();
mViewModeToggleQueued = false;
}
}
if (paused)
return;
2012-10-01 07:24:44 +00:00
2012-08-27 17:18:55 +00:00
// only show the crosshair in game mode and in first person mode.
MWBase::WindowManager *wm = MWBase::Environment::get().getWindowManager();
wm->showCrosshair(!wm->isGuiMode() && (mFirstPersonView && !mVanity.enabled && !mPreviewMode));
2012-08-27 17:18:55 +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
}
}
void Camera::toggleViewMode(bool force)
2012-08-12 11:50:37 +00:00
{
// Changing the view will stop all playing animations, so if we are playing
// anything important, queue the view change for later
if (!mAnimation->upperBodyReady() && !force)
{
mViewModeToggleQueued = true;
return;
}
else
mViewModeToggleQueued = false;
2012-08-12 11:50:37 +00:00
mFirstPersonView = !mFirstPersonView;
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
{
// 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;
}
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
processViewChange();
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;
}
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
{
if (mFirstPersonView && !mAnimation->upperBodyReady())
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;
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;
}
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
void Camera::setSneakOffset(float offset)
{
mAnimation->setFirstPersonOffset(osg::Vec3f(0,0,-offset));
}
2013-04-29 12:50:40 +00:00
float Camera::getYaw()
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
}
2013-04-29 12:50:40 +00:00
float Camera::getPitch()
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)
angle = limit;
2013-04-27 08:24:36 +00:00
else if(angle < -limit)
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
float Camera::getCameraDistance() const
{
if (isFirstPerson())
return 0.f;
2015-05-21 21:54:39 +00:00
return mCameraDistance;
}
2013-04-29 12:50:40 +00:00
void Camera::setCameraDistance(float dist, bool adjust, bool override)
2012-08-14 10:37:48 +00:00
{
2013-04-27 08:24:36 +00:00
if(mFirstPersonView && !mPreviewMode && !mVanity.enabled)
return;
2013-04-27 08:24:36 +00:00
mIsNearest = false;
2015-05-21 21:54:39 +00:00
if (adjust)
dist += mCameraDistance;
if (dist >= mFurthest) {
dist = mFurthest;
} else if (!override && dist < 10.f) {
2015-05-28 01:47:53 +00:00
dist = 10.f;
2015-05-21 21:54:39 +00:00
} else if (override && dist <= mNearest) {
2015-05-28 01:47:53 +00:00
dist = mNearest;
mIsNearest = true;
}
2015-05-21 21:54:39 +00:00
mCameraDistance = dist;
2012-08-14 10:37:48 +00:00
if (override) {
if (mVanity.enabled || mPreviewMode) {
2015-05-21 21:54:39 +00:00
mPreviewCam.offset = mCameraDistance;
} else if (!mFirstPersonView) {
2015-05-21 21:54:39 +00:00
mMaxCameraDistance = mCameraDistance;
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()
{
if (mVanity.enabled || mPreviewMode) {
mCameraDistance = mPreviewCam.offset;
} else if (!mFirstPersonView) {
mCameraDistance = mMaxCameraDistance;
}
}
2013-04-29 12:50:40 +00:00
void Camera::setAnimation(NpcAnimation *anim)
2012-08-15 11:17:35 +00:00
{
mAnimation = anim;
processViewChange();
2012-08-15 11:17:35 +00:00
}
void Camera::processViewChange()
2012-08-14 16:33:29 +00:00
{
if(isFirstPerson())
{
mAnimation->setViewMode(NpcAnimation::VM_FirstPerson);
2015-05-30 23:07:43 +00:00
mTrackingNode = mAnimation->getNode("Camera");
if (!mTrackingNode)
mTrackingNode = mAnimation->getNode("Head");
mHeightScale = 1.f;
}
else
{
mAnimation->setViewMode(NpcAnimation::VM_Normal);
SceneUtil::PositionAttitudeTransform* transform = mTrackingPtr.getRefData().getBaseNode();
mTrackingNode = transform;
if (transform)
2015-11-11 16:23:47 +00:00
mHeightScale = transform->getScale().z();
else
mHeightScale = 1.f;
}
2015-05-21 21:54:39 +00:00
rotateCamera(getPitch(), getYaw(), false);
2012-08-14 16:33:29 +00:00
}
2015-06-01 13:34:46 +00:00
void Camera::getPosition(osg::Vec3f &focal, osg::Vec3f &camera)
{
2015-06-01 13:34:46 +00:00
focal = getFocalPoint();
2015-06-01 13:34:46 +00:00
osg::Quat orient = osg::Quat(getPitch(), osg::Vec3d(1,0,0)) * osg::Quat(getYaw(), osg::Vec3d(0,0,1));
osg::Vec3d offset = orient * osg::Vec3d(0, isFirstPerson() ? 0 : -mCameraDistance, 0);
2015-06-01 13:34:46 +00:00
camera = focal + offset;
}
2012-08-17 08:08:31 +00:00
2013-04-29 12:50:40 +00:00
void Camera::togglePlayerLooking(bool enable)
{
mFreeLook = enable;
}
2013-04-29 12:50:40 +00:00
bool Camera::isVanityOrPreviewModeEnabled()
{
return mPreviewMode || mVanity.enabled;
}
bool Camera::isNearest()
{
return mIsNearest;
}
}