1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-25 15:35:23 +00:00
OpenMW/apps/openmw/mwrender/player.cpp
2012-08-17 15:19:43 +04:00

193 lines
5.1 KiB
C++

#include "player.hpp"
#include <OgreSceneNode.h>
#include <OgreCamera.h>
#include <OgreRay.h>
#include "../mwbase/environment.hpp"
#include "../mwbase/soundmanager.hpp"
#include "../mwworld/ptr.hpp"
#include "../mwworld/refdata.hpp"
namespace MWRender
{
Player::Player (Ogre::Camera *camera, Ogre::SceneNode* node)
: mCamera(camera),
mPlayerNode(node),
mCameraNode(mPlayerNode->createChildSceneNode()),
mFirstPersonView(true),
mVanityMode(false),
mPreviewMode(false)
{
Ogre::SceneNode *pitchNode = mCameraNode->createChildSceneNode();
pitchNode->attachObject(mCamera);
}
bool Player::rotate(const Ogre::Vector3 &rot, bool adjust)
{
rotateCamera(rot, adjust);
mUpdates = 0;
mTimeIdle = 0.f;
if (mVanityMode) {
toggleVanityMode();
}
return !mVanityMode && !mPreviewMode;
}
void Player::rotateCamera(const Ogre::Vector3 &rot, bool adjust)
{
Ogre::SceneNode *pitchNode = mCamera->getParentSceneNode();
Ogre::SceneNode *yawNode = pitchNode->getParentSceneNode();
if (adjust) {
float f =
limitPitchAngle(89.5f, Ogre::Radian(rot.x).valueDegrees());
if (f != 0.0) {
pitchNode->pitch(Ogre::Degree(f));
}
yawNode->yaw(Ogre::Radian(-rot.z));
} else {
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);
}
Ogre::Quaternion xr(radx, Ogre::Vector3::UNIT_X);
Ogre::Quaternion yr(Ogre::Radian(-rot.z), Ogre::Vector3::UNIT_Y);
pitchNode->setOrientation(xr);
yawNode->setOrientation(yr);
}
updateListener();
}
std::string Player::getHandle() const
{
return mPlayerNode->getName();
}
void Player::attachTo(const MWWorld::Ptr &ptr)
{
ptr.getRefData().setBaseNode(mPlayerNode);
}
float Player::limitPitchAngle(float limitAbs, float shift)
{
Ogre::Quaternion orient =
mCamera->getParentSceneNode()->getOrientation();
float pitchAngle =
(2 * Ogre::Degree(Ogre::Math::ASin(orient.x)).valueDegrees());
if (pitchAngle + shift < limitAbs && pitchAngle + shift > -limitAbs) {
return shift;
}
if (pitchAngle > 0) {
float f = limitAbs - pitchAngle - shift;
return (f > 0.f) ? f : 0.f;
} else if (pitchAngle < 0) {
float f = -limitAbs - pitchAngle - shift;
return (f < 0.f) ? f : 0.f;
}
return 0.f;
}
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 (!mVanityMode) {
++mUpdates;
mTimeIdle += duration;
if (mTimeIdle > 30.f) {
toggleVanityMode();
}
}
if (mFirstPersonView && !mVanityMode) {
return;
}
if (mVanityMode) {
Ogre::Vector3 rot(0.f, 0.f, 0.f);
rot.z = Ogre::Degree(3.f * duration).valueRadians();
rotateCamera(rot, true);
}
}
void Player::toggleViewMode()
{
mFirstPersonView = !mFirstPersonView;
if (mFirstPersonView) {
mCamera->setPosition(0.f, 0.f, 0.f);
} else {
mCamera->setPosition(0.f, 0.f, 400.f);
}
}
void Player::toggleVanityMode()
{
mVanityMode = !mVanityMode;
float offset = 300.f;
Ogre::Vector3 rot(0.f, 0.f, 0.f);
if (mVanityMode) {
mYaw = getYawAngle();
mPitch = getPitchAngle();
mOffset = mCamera->getPosition().z;
rot.x = Ogre::Degree(-30.f).valueRadians();
} else {
rot.x = Ogre::Degree(mPitch).valueRadians();
rot.z = Ogre::Degree(mYaw).valueRadians();
offset = mOffset;
}
mCamera->setPosition(0.f, 0.f, offset);
rotateCamera(rot, false);
}
void Player::togglePreviewMode()
{
/// \todo move camera
mPreviewMode = !mPreviewMode;
}
float Player::getPitchAngle()
{
Ogre::Quaternion orient
= mCamera->getParentSceneNode()->getOrientation();
float angle =
(2 * Ogre::Degree(Ogre::Math::ASin(orient.x)).valueDegrees());
return angle;
}
float Player::getYawAngle()
{
Ogre::Quaternion orient
= mCameraNode->getOrientation();
float angle =
(2 * Ogre::Degree(Ogre::Math::ASin(orient.y)).valueDegrees());
if (orient.w < 0) {
angle = -angle;
}
return -angle;
}
}