|
|
|
@ -4,104 +4,359 @@
|
|
|
|
|
#include <OgreCamera.h>
|
|
|
|
|
|
|
|
|
|
#include "../mwbase/environment.hpp"
|
|
|
|
|
#include "../mwbase/windowmanager.hpp"
|
|
|
|
|
#include "../mwbase/soundmanager.hpp"
|
|
|
|
|
|
|
|
|
|
#include "../mwworld/ptr.hpp"
|
|
|
|
|
#include "../mwworld/refdata.hpp"
|
|
|
|
|
|
|
|
|
|
#include "npcanimation.hpp"
|
|
|
|
|
|
|
|
|
|
namespace MWRender
|
|
|
|
|
{
|
|
|
|
|
Player::Player (Ogre::Camera *camera, Ogre::SceneNode* node)
|
|
|
|
|
: mCamera (camera),
|
|
|
|
|
mNode (node),
|
|
|
|
|
: mCamera(camera),
|
|
|
|
|
mPlayerNode(node),
|
|
|
|
|
mCameraNode(mPlayerNode->createChildSceneNode()),
|
|
|
|
|
mFirstPersonView(true),
|
|
|
|
|
mVanityModeEnabled(false)
|
|
|
|
|
{}
|
|
|
|
|
mPreviewMode(false),
|
|
|
|
|
mFreeLook(true),
|
|
|
|
|
mHeight(128.f),
|
|
|
|
|
mCameraDistance(300.f),
|
|
|
|
|
mDistanceAdjusted(false)
|
|
|
|
|
{
|
|
|
|
|
mVanity.enabled = false;
|
|
|
|
|
mVanity.allowed = true;
|
|
|
|
|
mVanity.forced = false;
|
|
|
|
|
|
|
|
|
|
bool Player::setRotation(const Ogre::Vector3 &rot)
|
|
|
|
|
mCameraNode->attachObject(mCamera);
|
|
|
|
|
mCameraNode->setPosition(0.f, 0.f, mHeight);
|
|
|
|
|
|
|
|
|
|
mPreviewCam.yaw = 0.f;
|
|
|
|
|
mPreviewCam.offset = 400.f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Player::rotate(const Ogre::Vector3 &rot, bool adjust)
|
|
|
|
|
{
|
|
|
|
|
Ogre::SceneNode *sceneNode = mNode;
|
|
|
|
|
Ogre::Node* yawNode = sceneNode->getChildIterator().getNext();
|
|
|
|
|
Ogre::Node* pitchNode = yawNode->getChildIterator().getNext();
|
|
|
|
|
if (mVanity.enabled) {
|
|
|
|
|
toggleVanityMode(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ogre::Vector3 trueRot = rot;
|
|
|
|
|
|
|
|
|
|
/// \note rotate player on forced vanity
|
|
|
|
|
if (mVanity.forced) {
|
|
|
|
|
if (mFreeLook) {
|
|
|
|
|
float diff = (adjust) ? rot.z : mMainCam.yaw - rot.z;
|
|
|
|
|
|
|
|
|
|
// we are only interested in X and Y rotation
|
|
|
|
|
mVanity.enabled = false;
|
|
|
|
|
rotateCamera(rot, adjust);
|
|
|
|
|
mVanity.enabled = true;
|
|
|
|
|
|
|
|
|
|
// Rotate around X axis
|
|
|
|
|
Ogre::Radian radx(rot.x);
|
|
|
|
|
if (radx.valueDegrees() > 89.5f) {
|
|
|
|
|
radx = Ogre::Degree(89.5f);
|
|
|
|
|
} else if (radx.valueDegrees() < -89.5f) {
|
|
|
|
|
radx = Ogre::Degree(-89.5f);
|
|
|
|
|
compensateYaw(diff);
|
|
|
|
|
}
|
|
|
|
|
trueRot.z = 0.f;
|
|
|
|
|
}
|
|
|
|
|
Ogre::Quaternion xr(radx, Ogre::Vector3::UNIT_X);
|
|
|
|
|
|
|
|
|
|
// Rotate around Y axis
|
|
|
|
|
Ogre::Quaternion yr(Ogre::Radian(-rot.z), Ogre::Vector3::UNIT_Y);
|
|
|
|
|
if (mFreeLook || mVanity.enabled || mPreviewMode) {
|
|
|
|
|
rotateCamera(trueRot, adjust);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pitchNode->setOrientation(xr);
|
|
|
|
|
yawNode->setOrientation(yr);
|
|
|
|
|
/// \note if vanity mode is forced by TVM then rotate player
|
|
|
|
|
return (!mVanity.enabled && !mPreviewMode) || mVanity.forced;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Player::rotateCamera(const Ogre::Vector3 &rot, bool adjust)
|
|
|
|
|
{
|
|
|
|
|
if (adjust) {
|
|
|
|
|
setYaw(getYaw() + rot.z);
|
|
|
|
|
setPitch(getPitch() + rot.x);
|
|
|
|
|
} else {
|
|
|
|
|
setYaw(rot.z);
|
|
|
|
|
setPitch(rot.x);
|
|
|
|
|
}
|
|
|
|
|
Ogre::Quaternion xr(
|
|
|
|
|
Ogre::Radian(getPitch() + Ogre::Math::HALF_PI),
|
|
|
|
|
Ogre::Vector3::UNIT_X
|
|
|
|
|
);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
updateListener();
|
|
|
|
|
|
|
|
|
|
return !mVanityModeEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string Player::getHandle() const
|
|
|
|
|
{
|
|
|
|
|
return mNode->getName();
|
|
|
|
|
return mPlayerNode->getName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Player::attachTo(const MWWorld::Ptr &ptr)
|
|
|
|
|
{
|
|
|
|
|
ptr.getRefData().setBaseNode(mNode);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Player::update(float duration)
|
|
|
|
|
{
|
|
|
|
|
if (mAnimation) {
|
|
|
|
|
mAnimation->runAnimation(duration);
|
|
|
|
|
}
|
|
|
|
|
if (mFirstPersonView && !mVanity.enabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (mVanity.enabled) {
|
|
|
|
|
Ogre::Vector3 rot(0.f, 0.f, 0.f);
|
|
|
|
|
rot.z = Ogre::Degree(3.f * duration).valueRadians();
|
|
|
|
|
rotateCamera(rot, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Player::adjustRotation(const Ogre::Vector3 &rot)
|
|
|
|
|
void Player::toggleViewMode()
|
|
|
|
|
{
|
|
|
|
|
Ogre::SceneNode *pitchNode = mCamera->getParentSceneNode();
|
|
|
|
|
Ogre::SceneNode *yawNode = pitchNode->getParentSceneNode();
|
|
|
|
|
mFirstPersonView = !mFirstPersonView;
|
|
|
|
|
if (mFirstPersonView) {
|
|
|
|
|
mCamera->setPosition(0.f, 0.f, 0.f);
|
|
|
|
|
setLowHeight(false);
|
|
|
|
|
} else {
|
|
|
|
|
mCamera->setPosition(0.f, 0.f, mCameraDistance);
|
|
|
|
|
setLowHeight(true);
|
|
|
|
|
}
|
|
|
|
|
mPlayerNode->setVisible(!mFirstPersonView, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Player::allowVanityMode(bool allow)
|
|
|
|
|
{
|
|
|
|
|
if (!allow && mVanity.enabled && !mVanity.forced) {
|
|
|
|
|
toggleVanityMode(false);
|
|
|
|
|
}
|
|
|
|
|
mVanity.allowed = allow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float f = controlFlip(Ogre::Radian(rot.x).valueDegrees());
|
|
|
|
|
if (f != 0.0) {
|
|
|
|
|
pitchNode->pitch(Ogre::Degree(f));
|
|
|
|
|
bool Player::toggleVanityMode(bool enable, bool force)
|
|
|
|
|
{
|
|
|
|
|
if ((mVanity.forced && !force) ||
|
|
|
|
|
(!mVanity.allowed && (force || enable)))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
} else if (mVanity.enabled == enable) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
yawNode->yaw(Ogre::Radian(-rot.z));
|
|
|
|
|
mVanity.enabled = enable;
|
|
|
|
|
mVanity.forced = force && enable;
|
|
|
|
|
|
|
|
|
|
updateListener();
|
|
|
|
|
float offset = mPreviewCam.offset;
|
|
|
|
|
Ogre::Vector3 rot(0.f, 0.f, 0.f);
|
|
|
|
|
if (mVanity.enabled) {
|
|
|
|
|
rot.x = Ogre::Degree(-30.f).valueRadians();
|
|
|
|
|
mMainCam.offset = mCamera->getPosition().z;
|
|
|
|
|
|
|
|
|
|
mPlayerNode->setVisible(true, false);
|
|
|
|
|
setLowHeight(true);
|
|
|
|
|
} else {
|
|
|
|
|
rot.x = getPitch();
|
|
|
|
|
offset = mMainCam.offset;
|
|
|
|
|
|
|
|
|
|
return !mVanityModeEnabled;
|
|
|
|
|
mPlayerNode->setVisible(!mFirstPersonView, false);
|
|
|
|
|
setLowHeight(!mFirstPersonView);
|
|
|
|
|
}
|
|
|
|
|
rot.z = getYaw();
|
|
|
|
|
mCamera->setPosition(0.f, 0.f, offset);
|
|
|
|
|
rotateCamera(rot, false);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float Player::controlFlip(float shift)
|
|
|
|
|
void Player::togglePreviewMode(bool enable)
|
|
|
|
|
{
|
|
|
|
|
Ogre::SceneNode *pitchNode = mCamera->getParentSceneNode();
|
|
|
|
|
Ogre::Quaternion orient = pitchNode->getOrientation();
|
|
|
|
|
if (mPreviewMode == enable) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mPreviewMode = enable;
|
|
|
|
|
float offset = mCamera->getPosition().z;
|
|
|
|
|
if (mPreviewMode) {
|
|
|
|
|
mMainCam.offset = offset;
|
|
|
|
|
offset = mPreviewCam.offset;
|
|
|
|
|
|
|
|
|
|
float pitchAngle =
|
|
|
|
|
(2 * Ogre::Degree(Ogre::Math::ASin(orient.x)).valueDegrees());
|
|
|
|
|
mPlayerNode->setVisible(true, false);
|
|
|
|
|
setLowHeight(true);
|
|
|
|
|
} else {
|
|
|
|
|
mPreviewCam.offset = offset;
|
|
|
|
|
offset = mMainCam.offset;
|
|
|
|
|
|
|
|
|
|
if (pitchAngle + shift < 89.5f && pitchAngle + shift > -89.5f) {
|
|
|
|
|
return shift;
|
|
|
|
|
mPlayerNode->setVisible(!mFirstPersonView, false);
|
|
|
|
|
setLowHeight(!mFirstPersonView);
|
|
|
|
|
}
|
|
|
|
|
if (pitchAngle > 0) {
|
|
|
|
|
float f = 89.5f - pitchAngle - shift;
|
|
|
|
|
return (f > 0.f) ? f : 0.f;
|
|
|
|
|
} else if (pitchAngle < 0) {
|
|
|
|
|
float f = -89.5 - pitchAngle - shift;
|
|
|
|
|
return (f < 0.f) ? f : 0.f;
|
|
|
|
|
mCamera->setPosition(0.f, 0.f, offset);
|
|
|
|
|
rotateCamera(Ogre::Vector3(getPitch(), 0.f, getYaw()), false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float Player::getYaw()
|
|
|
|
|
{
|
|
|
|
|
if (mVanity.enabled || mPreviewMode) {
|
|
|
|
|
return mPreviewCam.yaw;
|
|
|
|
|
}
|
|
|
|
|
return 0.f;
|
|
|
|
|
return mMainCam.yaw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Player::updateListener()
|
|
|
|
|
void Player::setYaw(float angle)
|
|
|
|
|
{
|
|
|
|
|
Ogre::Vector3 pos = mCamera->getRealPosition();
|
|
|
|
|
Ogre::Vector3 dir = mCamera->getRealDirection();
|
|
|
|
|
if (angle > Ogre::Math::PI) {
|
|
|
|
|
angle -= Ogre::Math::TWO_PI;
|
|
|
|
|
} else if (angle < -Ogre::Math::PI) {
|
|
|
|
|
angle += Ogre::Math::TWO_PI;
|
|
|
|
|
}
|
|
|
|
|
if (mVanity.enabled || mPreviewMode) {
|
|
|
|
|
mPreviewCam.yaw = angle;
|
|
|
|
|
} else {
|
|
|
|
|
mMainCam.yaw = angle;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ogre::Real xch;
|
|
|
|
|
xch = pos.y, pos.y = -pos.z, pos.z = xch;
|
|
|
|
|
xch = dir.y, dir.y = -dir.z, dir.z = xch;
|
|
|
|
|
float Player::getPitch()
|
|
|
|
|
{
|
|
|
|
|
if (mVanity.enabled || mPreviewMode) {
|
|
|
|
|
return mPreviewCam.pitch;
|
|
|
|
|
}
|
|
|
|
|
return mMainCam.pitch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MWBase::Environment::get().getSoundManager()->setListenerPosDir(pos, dir);
|
|
|
|
|
void Player::setPitch(float angle)
|
|
|
|
|
{
|
|
|
|
|
float limit = Ogre::Math::HALF_PI;
|
|
|
|
|
if (mVanity.forced || mPreviewMode) {
|
|
|
|
|
limit /= 2;
|
|
|
|
|
}
|
|
|
|
|
if (angle > limit) {
|
|
|
|
|
angle = limit - 0.01;
|
|
|
|
|
} else if (angle < -limit) {
|
|
|
|
|
angle = -limit + 0.01;
|
|
|
|
|
}
|
|
|
|
|
if (mVanity.enabled || mPreviewMode) {
|
|
|
|
|
mPreviewCam.pitch = angle;
|
|
|
|
|
} else {
|
|
|
|
|
mMainCam.pitch = angle;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Player::setCameraDistance(float dist, bool adjust, bool override)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
if (override) {
|
|
|
|
|
if (mVanity.enabled || mPreviewMode) {
|
|
|
|
|
mPreviewCam.offset = v.z;
|
|
|
|
|
} else if (!mFirstPersonView) {
|
|
|
|
|
mCameraDistance = v.z;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
mDistanceAdjusted = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Player::setAnimation(NpcAnimation *anim)
|
|
|
|
|
{
|
|
|
|
|
mAnimation = anim;
|
|
|
|
|
mPlayerNode->setVisible(!mFirstPersonView, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ogre::Vector3 Player::getPosition()
|
|
|
|
|
{
|
|
|
|
|
return mPlayerNode->getPosition();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Player::getSightAngles(float &pitch, float &yaw)
|
|
|
|
|
{
|
|
|
|
|
pitch = mMainCam.pitch;
|
|
|
|
|
yaw = mMainCam.yaw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|