1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-24 09:53:51 +00:00
openmw-tes3mp/apps/openmw/mwrender/player.cpp

314 lines
8.2 KiB
C++
Raw Normal View History

#include "player.hpp"
#include <OgreSceneNode.h>
#include <OgreCamera.h>
2012-08-12 11:50:37 +00:00
#include <OgreRay.h>
#include "../mwbase/environment.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()),
2012-08-13 22:36:18 +00:00
mVanityNode(mPlayerNode->createChildSceneNode()),
mFirstPersonView(true),
2012-08-13 22:36:18 +00:00
mPreviewMode(false),
2012-08-14 16:33:29 +00:00
mHeight(128.f),
2012-08-14 10:37:48 +00:00
mCameraDistance(400.f)
{
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);
mPlayerNode->setVisible(false);
2012-08-13 22:36:18 +00:00
mPreviewCam.yaw = 0.f;
2012-08-14 10:37:48 +00:00
mPreviewCam.offset = 600.f;
2012-08-12 11:50:37 +00:00
}
bool Player::rotate(const Ogre::Vector3 &rot, bool adjust)
{
2012-08-12 14:35:35 +00:00
mUpdates = 0;
mTimeIdle = 0.f;
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) {
moveCameraNode(mPlayerNode);
mVanity.enabled = false;
rotateCamera(rot, adjust);
moveCameraNode(mVanityNode);
mVanity.enabled = true;
trueRot.z = 0.f;
}
rotateCamera(trueRot, adjust);
/// \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
Ogre::SceneNode *pitchNode = mCamera->getParentSceneNode();
Ogre::SceneNode *yawNode = pitchNode->getParentSceneNode();
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
);
Ogre::Quaternion zr(Ogre::Radian(getYaw()), Ogre::Vector3::UNIT_Z);
pitchNode->setOrientation(xr);
yawNode->setOrientation(zr);
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
Ogre::Vector3 pos = mPlayerNode->getPosition();
2012-08-14 10:37:48 +00:00
if (!mVanity.enabled) {
2012-08-12 14:35:35 +00:00
++mUpdates;
mTimeIdle += duration;
if (mTimeIdle > 30.f) {
2012-08-13 22:36:18 +00:00
toggleVanityMode(true);
2012-08-12 14:35:35 +00:00
}
}
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);
2012-08-14 10:37:48 +00:00
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);
2012-08-14 16:33:29 +00:00
mCameraNode->setPosition(0.f, 0.f, 128.f);
mPlayerNode->setVisible(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);
2012-08-14 16:33:29 +00:00
mCameraNode->setPosition(0.f, 0.f, 104.f);
mPlayerNode->setVisible(true);
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
2012-08-13 04:37:32 +00:00
float offset = 300.f;
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) {
mPlayerNode->setVisible(true);
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;
2012-08-14 10:37:48 +00:00
moveCameraNode(mVanityNode);
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-14 10:37:48 +00:00
moveCameraNode(mPlayerNode);
2012-08-12 14:35:35 +00:00
}
if (offset == 0.f) {
mPlayerNode->setVisible(false);
}
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) {
mPlayerNode->setVisible(true);
2012-08-14 10:37:48 +00:00
mMainCam.offset = offset;
offset = mPreviewCam.offset;
moveCameraNode(mVanityNode);
} else {
mPreviewCam.offset = offset;
offset = mMainCam.offset;
moveCameraNode(mPlayerNode);
}
if (offset == 0.f) {
mPlayerNode->setVisible(false);
}
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::moveCameraNode(Ogre::SceneNode *node)
{
mCameraNode->getParentSceneNode()->removeChild(mCameraNode);
node->addChild(mCameraNode);
}
void Player::setCameraDistance(float dist, bool adjust)
{
/// \note non-Morrowind feature: allow to change camera distance
/// int 3d-person mode
/// \todo review and simplify condition if possible
if (mPreviewMode ||
mVanity.forced ||
(!mVanity.enabled && !mFirstPersonView))
{
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 < 100.f) {
v.z = 100.f;
}
mCamera->setPosition(v);
if (!mVanity.enabled && !mFirstPersonView) {
mCameraDistance = v.z;
}
}
}
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;
}
}