1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-10 15:39:02 +00:00
OpenMW/apps/openmw/mwrender/player.cpp

363 lines
9.5 KiB
C++
Raw Normal View History

#include "player.hpp"
#include <OgreSceneNode.h>
#include <OgreCamera.h>
#include "../mwbase/environment.hpp"
#include "../mwbase/windowmanager.hpp"
#include "../mwbase/soundmanager.hpp"
#include "../mwworld/ptr.hpp"
#include "../mwworld/refdata.hpp"
2012-08-14 16:33:29 +00:00
#include "npcanimation.hpp"
namespace MWRender
{
Player::Player (Ogre::Camera *camera, Ogre::SceneNode* node)
2012-08-12 11:50:37 +00:00
: mCamera(camera),
mPlayerNode(node),
mCameraNode(mPlayerNode->createChildSceneNode()),
mFirstPersonView(true),
2012-08-13 22:36:18 +00:00
mPreviewMode(false),
mFreeLook(true),
2012-08-14 16:33:29 +00:00
mHeight(128.f),
mCameraDistance(300.f),
mDistanceAdjusted(false)
{
2012-08-14 10:37:48 +00:00
mVanity.enabled = false;
mVanity.allowed = true;
mVanity.forced = false;
2012-08-13 22:36:18 +00:00
mCameraNode->attachObject(mCamera);
mCameraNode->setPosition(0.f, 0.f, mHeight);
mPreviewCam.yaw = 0.f;
mPreviewCam.offset = 400.f;
2012-08-12 11:50:37 +00:00
}
bool Player::rotate(const Ogre::Vector3 &rot, bool adjust)
{
2012-08-14 10:37:48 +00:00
if (mVanity.enabled) {
2012-08-13 22:36:18 +00:00
toggleVanityMode(false);
2012-08-12 11:50:37 +00:00
}
2012-08-14 10:37:48 +00:00
Ogre::Vector3 trueRot = rot;
/// \note rotate player on forced vanity
if (mVanity.forced) {
if (mFreeLook) {
float diff = (adjust) ? rot.z : mMainCam.yaw - rot.z;
2012-08-14 10:37:48 +00:00
mVanity.enabled = false;
rotateCamera(rot, adjust);
mVanity.enabled = true;
2012-08-17 08:08:31 +00:00
compensateYaw(diff);
}
2012-08-14 10:37:48 +00:00
trueRot.z = 0.f;
}
if (mFreeLook || mVanity.enabled || mPreviewMode) {
rotateCamera(trueRot, adjust);
}
2012-08-14 10:37:48 +00:00
/// \note if vanity mode is forced by TVM then rotate player
return (!mVanity.enabled && !mPreviewMode) || mVanity.forced;
}
2012-08-13 04:37:32 +00:00
void Player::rotateCamera(const Ogre::Vector3 &rot, bool adjust)
{
2012-08-12 11:50:37 +00:00
if (adjust) {
2012-08-13 22:36:18 +00:00
setYaw(getYaw() + rot.z);
setPitch(getPitch() + rot.x);
2012-08-12 11:50:37 +00:00
} else {
2012-08-13 22:36:18 +00:00
setYaw(rot.z);
setPitch(rot.x);
2012-08-12 11:50:37 +00:00
}
2012-08-13 22:36:18 +00:00
Ogre::Quaternion xr(
Ogre::Radian(getPitch() + Ogre::Math::HALF_PI),
Ogre::Vector3::UNIT_X
);
2012-08-17 08:08:31 +00:00
Ogre::Quaternion zr(
Ogre::Radian(getYaw()),
Ogre::Vector3::NEGATIVE_UNIT_Z
);
if (!mVanity.enabled && !mPreviewMode) {
mPlayerNode->setOrientation(zr);
mCameraNode->setOrientation(xr);
} else {
mCameraNode->setOrientation(zr * xr);
}
2012-08-13 04:37:32 +00:00
updateListener();
}
2012-08-12 11:50:37 +00:00
std::string Player::getHandle() const
{
2012-08-12 11:50:37 +00:00
return mPlayerNode->getName();
}
void Player::attachTo(const MWWorld::Ptr &ptr)
{
ptr.getRefData().setBaseNode(mPlayerNode);
}
void Player::updateListener()
{
Ogre::Vector3 pos = mCamera->getRealPosition();
Ogre::Vector3 dir = mCamera->getRealDirection();
Ogre::Real xch;
xch = pos.y, pos.y = -pos.z, pos.z = xch;
xch = dir.y, dir.y = -dir.z, dir.z = xch;
MWBase::Environment::get().getSoundManager()->setListenerPosDir(pos, dir);
}
2012-08-12 11:50:37 +00:00
void Player::update(float duration)
{
2012-08-14 16:33:29 +00:00
if (mAnimation) {
mAnimation->runAnimation(duration);
}
2012-08-14 10:37:48 +00:00
if (mFirstPersonView && !mVanity.enabled) {
2012-08-12 11:50:37 +00:00
return;
}
2012-08-14 10:37:48 +00:00
if (mVanity.enabled) {
2012-08-12 14:35:35 +00:00
Ogre::Vector3 rot(0.f, 0.f, 0.f);
rot.z = Ogre::Degree(3.f * duration).valueRadians();
2012-08-13 04:37:32 +00:00
rotateCamera(rot, true);
2012-08-12 11:50:37 +00:00
}
}
void Player::toggleViewMode()
{
mFirstPersonView = !mFirstPersonView;
if (mFirstPersonView) {
2012-08-13 04:37:32 +00:00
mCamera->setPosition(0.f, 0.f, 0.f);
setLowHeight(false);
2012-08-12 11:50:37 +00:00
} else {
2012-08-14 10:37:48 +00:00
mCamera->setPosition(0.f, 0.f, mCameraDistance);
setLowHeight(true);
2012-08-12 11:50:37 +00:00
}
2012-08-15 11:17:35 +00:00
mPlayerNode->setVisible(!mFirstPersonView, false);
2012-08-12 11:50:37 +00:00
}
2012-08-14 10:37:48 +00:00
void Player::allowVanityMode(bool allow)
{
if (!allow && mVanity.enabled && !mVanity.forced) {
toggleVanityMode(false);
}
mVanity.allowed = allow;
}
2012-08-12 11:50:37 +00:00
2012-08-14 10:37:48 +00:00
bool Player::toggleVanityMode(bool enable, bool force)
2012-08-12 11:50:37 +00:00
{
2012-08-14 10:37:48 +00:00
if ((mVanity.forced && !force) ||
(!mVanity.allowed && (force || enable)))
{
return false;
} else if (mVanity.enabled == enable) {
return true;
2012-08-13 22:36:18 +00:00
}
2012-08-14 10:37:48 +00:00
mVanity.enabled = enable;
mVanity.forced = force && enable;
2012-08-12 14:35:35 +00:00
float offset = mPreviewCam.offset;
2012-08-12 14:35:35 +00:00
Ogre::Vector3 rot(0.f, 0.f, 0.f);
2012-08-14 10:37:48 +00:00
if (mVanity.enabled) {
2012-08-12 14:35:35 +00:00
rot.x = Ogre::Degree(-30.f).valueRadians();
2012-08-13 22:36:18 +00:00
mMainCam.offset = mCamera->getPosition().z;
mPlayerNode->setVisible(true, false);
setLowHeight(true);
2012-08-12 14:35:35 +00:00
} else {
2012-08-13 22:36:18 +00:00
rot.x = getPitch();
offset = mMainCam.offset;
2012-08-15 11:17:35 +00:00
mPlayerNode->setVisible(!mFirstPersonView, false);
setLowHeight(!mFirstPersonView);
}
2012-08-13 22:36:18 +00:00
rot.z = getYaw();
2012-08-13 04:37:32 +00:00
mCamera->setPosition(0.f, 0.f, offset);
rotateCamera(rot, false);
2012-08-14 10:37:48 +00:00
return true;
2012-08-12 11:50:37 +00:00
}
2012-08-13 22:36:18 +00:00
void Player::togglePreviewMode(bool enable)
2012-08-12 11:50:37 +00:00
{
2012-08-13 22:36:18 +00:00
if (mPreviewMode == enable) {
return;
}
mPreviewMode = enable;
2012-08-14 10:37:48 +00:00
float offset = mCamera->getPosition().z;
if (mPreviewMode) {
mMainCam.offset = offset;
offset = mPreviewCam.offset;
mPlayerNode->setVisible(true, false);
setLowHeight(true);
2012-08-14 10:37:48 +00:00
} else {
mPreviewCam.offset = offset;
offset = mMainCam.offset;
2012-08-15 11:17:35 +00:00
mPlayerNode->setVisible(!mFirstPersonView, false);
setLowHeight(!mFirstPersonView);
}
mCamera->setPosition(0.f, 0.f, offset);
2012-08-14 10:37:48 +00:00
rotateCamera(Ogre::Vector3(getPitch(), 0.f, getYaw()), false);
2012-08-12 11:50:37 +00:00
}
2012-08-12 14:35:35 +00:00
2012-08-13 22:36:18 +00:00
float Player::getYaw()
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.yaw;
}
return mMainCam.yaw;
}
2012-08-12 14:35:35 +00:00
2012-08-13 22:36:18 +00:00
void Player::setYaw(float angle)
{
if (angle > Ogre::Math::PI) {
angle -= Ogre::Math::TWO_PI;
} else if (angle < -Ogre::Math::PI) {
angle += Ogre::Math::TWO_PI;
}
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
}
2012-08-13 22:36:18 +00:00
float Player::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
2012-08-13 22:36:18 +00:00
void Player::setPitch(float angle)
2012-08-14 10:37:48 +00:00
{
float limit = Ogre::Math::HALF_PI;
if (mVanity.forced || mPreviewMode) {
limit /= 2;
2012-08-13 22:36:18 +00:00
}
2012-08-14 10:37:48 +00:00
if (angle > limit) {
angle = limit - 0.01;
} else if (angle < -limit) {
angle = -limit + 0.01;
}
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
void Player::setCameraDistance(float dist, bool adjust, bool override)
2012-08-14 10:37:48 +00:00
{
if (mFirstPersonView && !mPreviewMode && !mVanity.enabled) {
return;
}
Ogre::Vector3 v(0.f, 0.f, dist);
if (adjust) {
v += mCamera->getPosition();
}
if (v.z > 800.f) {
v.z = 800.f;
} else if (v.z < 10.f) {
v.z = 10.f;
}
mCamera->setPosition(v);
2012-08-14 10:37:48 +00:00
if (override) {
if (mVanity.enabled || mPreviewMode) {
mPreviewCam.offset = v.z;
} else if (!mFirstPersonView) {
2012-08-14 10:37:48 +00:00
mCameraDistance = v.z;
}
} else {
mDistanceAdjusted = true;
2012-08-14 10:37:48 +00:00
}
}
2012-08-14 16:33:29 +00:00
void Player::setCameraDistance()
{
if (mDistanceAdjusted) {
if (mVanity.enabled || mPreviewMode) {
mCamera->setPosition(0, 0, mPreviewCam.offset);
} else if (!mFirstPersonView) {
mCamera->setPosition(0, 0, mCameraDistance);
}
}
mDistanceAdjusted = false;
}
2012-08-15 11:17:35 +00:00
void Player::setAnimation(NpcAnimation *anim)
{
mAnimation = anim;
mPlayerNode->setVisible(!mFirstPersonView, false);
}
2012-08-14 16:33:29 +00:00
void Player::setHeight(float height)
{
mHeight = height;
mCameraNode->setPosition(0.f, 0.f, mHeight);
}
float Player::getHeight()
{
return mHeight * mPlayerNode->getScale().z;
}
bool Player::getPosition(Ogre::Vector3 &player, Ogre::Vector3 &camera)
{
float xch;
camera = mCamera->getRealPosition();
xch = camera.z, camera.z = camera.y, camera.y = -xch;
player = mPlayerNode->getPosition();
return mFirstPersonView && !mVanity.enabled && !mPreviewMode;
2012-08-14 16:33:29 +00:00
}
Ogre::Vector3 Player::getPosition()
{
return mPlayerNode->getPosition();
}
void Player::getSightAngles(float &pitch, float &yaw)
{
pitch = mMainCam.pitch;
yaw = mMainCam.yaw;
}
2012-08-17 08:08:31 +00:00
void Player::compensateYaw(float diff)
{
mPreviewCam.yaw -= diff;
Ogre::Quaternion zr(
Ogre::Radian(mPreviewCam.yaw),
Ogre::Vector3::NEGATIVE_UNIT_Z
);
Ogre::Quaternion xr(
Ogre::Radian(mPreviewCam.pitch),
Ogre::Vector3::UNIT_X);
mCameraNode->setOrientation(zr * xr);
}
void Player::togglePlayerLooking(bool enable)
{
mFreeLook = enable;
}
void Player::setLowHeight(bool low)
{
if (low) {
mCameraNode->setPosition(0.f, 0.f, mHeight * 0.85);
} else {
mCameraNode->setPosition(0.f, 0.f, mHeight);
}
}
}