Attach the camera to the Head node in first-person

This commit is contained in:
Chris Robinson 2013-07-13 16:12:38 -07:00
parent 2d7620e774
commit 5ee889e8b6
4 changed files with 80 additions and 37 deletions

View file

@ -115,7 +115,23 @@ void Animation::setObjectRoot(Ogre::SceneNode *node, const std::string &model, b
Ogre::Skeleton::BoneIterator boneiter = skelinst->getBoneIterator(); Ogre::Skeleton::BoneIterator boneiter = skelinst->getBoneIterator();
while(boneiter.hasMoreElements()) while(boneiter.hasMoreElements())
boneiter.getNext()->setManuallyControlled(true); boneiter.getNext()->setManuallyControlled(true);
// Reattach any objects that have been attached to this one
ObjectAttachMap::iterator iter = mAttachedObjects.begin();
while(iter != mAttachedObjects.end())
{
if(!skelinst->hasBone(iter->second))
mAttachedObjects.erase(iter++);
else
{
mSkelBase->attachObjectToBone(iter->second, iter->first);
iter++;
}
}
} }
else
mAttachedObjects.clear();
for(size_t i = 0;i < mObjectRoot.mControllers.size();i++) for(size_t i = 0;i < mObjectRoot.mControllers.size();i++)
{ {
if(mObjectRoot.mControllers[i].getSource().isNull()) if(mObjectRoot.mControllers[i].getSource().isNull())
@ -741,4 +757,24 @@ bool Animation::isPriorityActive(int priority) const
return false; return false;
} }
Ogre::TagPoint *Animation::attachObjectToBone(const Ogre::String &bonename, Ogre::MovableObject *obj)
{
Ogre::TagPoint *tag = NULL;
Ogre::SkeletonInstance *skel = (mSkelBase ? mSkelBase->getSkeleton() : NULL);
if(skel && skel->hasBone(bonename))
{
tag = mSkelBase->attachObjectToBone(bonename, obj);
mAttachedObjects[obj] = bonename;
}
return tag;
}
void Animation::detachObjectFromBone(Ogre::MovableObject *obj)
{
ObjectAttachMap::iterator iter = mAttachedObjects.find(obj);
if(iter != mAttachedObjects.end())
mAttachedObjects.erase(iter);
mSkelBase->detachObjectFromBone(obj);
}
} }

View file

@ -80,6 +80,8 @@ protected:
}; };
typedef std::map<std::string,AnimState> AnimStateMap; typedef std::map<std::string,AnimState> AnimStateMap;
typedef std::map<Ogre::MovableObject*,std::string> ObjectAttachMap;
MWWorld::Ptr mPtr; MWWorld::Ptr mPtr;
Camera *mCamera; Camera *mCamera;
@ -96,6 +98,8 @@ protected:
Ogre::SharedPtr<AnimationValue> mAnimationValuePtr[sNumGroups]; Ogre::SharedPtr<AnimationValue> mAnimationValuePtr[sNumGroups];
ObjectAttachMap mAttachedObjects;
float mAnimVelocity; float mAnimVelocity;
float mAnimSpeedMult; float mAnimSpeedMult;
@ -216,6 +220,12 @@ public:
{ mCamera = cam; } { mCamera = cam; }
Ogre::Node *getNode(const std::string &name); Ogre::Node *getNode(const std::string &name);
// Attaches the given object to a bone on this object's base skeleton. If the bone doesn't
// exist, the object isn't attached and NULL is returned. The returned TagPoint is only
// valid until the next setObjectRoot call.
Ogre::TagPoint *attachObjectToBone(const Ogre::String &bonename, Ogre::MovableObject *obj);
void detachObjectFromBone(Ogre::MovableObject *obj);
}; };
} }

View file

@ -3,6 +3,7 @@
#include <OgreSceneNode.h> #include <OgreSceneNode.h>
#include <OgreCamera.h> #include <OgreCamera.h>
#include <OgreSceneManager.h> #include <OgreSceneManager.h>
#include <OgreTagPoint.h>
#include "../mwbase/environment.hpp" #include "../mwbase/environment.hpp"
#include "../mwbase/windowmanager.hpp" #include "../mwbase/windowmanager.hpp"
@ -57,10 +58,10 @@ namespace MWRender
Ogre::Quaternion xr(Ogre::Radian(getPitch() + Ogre::Math::HALF_PI), Ogre::Vector3::UNIT_X); Ogre::Quaternion xr(Ogre::Radian(getPitch() + Ogre::Math::HALF_PI), Ogre::Vector3::UNIT_X);
if (!mVanity.enabled && !mPreviewMode) { if (!mVanity.enabled && !mPreviewMode) {
mCameraNode->setOrientation(xr); mCamera->getParentNode()->setOrientation(xr);
} else { } else {
Ogre::Quaternion zr(Ogre::Radian(getYaw()), Ogre::Vector3::NEGATIVE_UNIT_Z); Ogre::Quaternion zr(Ogre::Radian(getYaw()), Ogre::Vector3::NEGATIVE_UNIT_Z);
mCameraNode->setOrientation(zr * xr); mCamera->getParentNode()->setOrientation(zr * xr);
} }
} }
@ -112,14 +113,12 @@ namespace MWRender
void Camera::toggleViewMode() void Camera::toggleViewMode()
{ {
mFirstPersonView = !mFirstPersonView; mFirstPersonView = !mFirstPersonView;
mAnimation->setViewMode(isFirstPerson() ? NpcAnimation::VM_FirstPerson : processViewChange();
NpcAnimation::VM_Normal);
if (mFirstPersonView) { if (mFirstPersonView) {
mCamera->setPosition(0.f, 0.f, 0.f); mCamera->setPosition(0.f, 0.f, 0.f);
setLowHeight(false);
} else { } else {
mCamera->setPosition(0.f, 0.f, mCameraDistance); mCamera->setPosition(0.f, 0.f, mCameraDistance);
setLowHeight(true);
} }
} }
@ -139,21 +138,16 @@ namespace MWRender
return true; return true;
mVanity.enabled = enable; mVanity.enabled = enable;
mAnimation->setViewMode(isFirstPerson() ? NpcAnimation::VM_FirstPerson : processViewChange();
NpcAnimation::VM_Normal);
float offset = mPreviewCam.offset; float offset = mPreviewCam.offset;
Ogre::Vector3 rot(0.f, 0.f, 0.f); Ogre::Vector3 rot(0.f, 0.f, 0.f);
if (mVanity.enabled) { if (mVanity.enabled) {
rot.x = Ogre::Degree(-30.f).valueRadians(); rot.x = Ogre::Degree(-30.f).valueRadians();
mMainCam.offset = mCamera->getPosition().z; mMainCam.offset = mCamera->getPosition().z;
setLowHeight(true);
} else { } else {
rot.x = getPitch(); rot.x = getPitch();
offset = mMainCam.offset; offset = mMainCam.offset;
setLowHeight(!mFirstPersonView);
} }
rot.z = getYaw(); rot.z = getYaw();
@ -169,20 +163,15 @@ namespace MWRender
return; return;
mPreviewMode = enable; mPreviewMode = enable;
mAnimation->setViewMode(isFirstPerson() ? NpcAnimation::VM_FirstPerson : processViewChange();
NpcAnimation::VM_Normal);
float offset = mCamera->getPosition().z; float offset = mCamera->getPosition().z;
if (mPreviewMode) { if (mPreviewMode) {
mMainCam.offset = offset; mMainCam.offset = offset;
offset = mPreviewCam.offset; offset = mPreviewCam.offset;
setLowHeight(true);
} else { } else {
mPreviewCam.offset = offset; mPreviewCam.offset = offset;
offset = mMainCam.offset; offset = mMainCam.offset;
setLowHeight(!mFirstPersonView);
} }
mCamera->setPosition(0.f, 0.f, offset); mCamera->setPosition(0.f, 0.f, offset);
@ -286,27 +275,45 @@ namespace MWRender
{ {
mAnimation->setViewMode(NpcAnimation::VM_Normal); mAnimation->setViewMode(NpcAnimation::VM_Normal);
mAnimation->setCamera(NULL); mAnimation->setCamera(NULL);
mAnimation->detachObjectFromBone(mCamera);
} }
mAnimation = anim; mAnimation = anim;
mAnimation->setViewMode(isFirstPerson() ? NpcAnimation::VM_FirstPerson :
NpcAnimation::VM_Normal);
mAnimation->setCamera(this); mAnimation->setCamera(this);
processViewChange();
} }
void Camera::setHeight(float height) void Camera::processViewChange()
{ {
mHeight = height; mAnimation->detachObjectFromBone(mCamera);
mCameraNode->setPosition(0.f, 0.f, mHeight); mCamera->detachFromParent();
if(isFirstPerson())
{
mAnimation->setViewMode(NpcAnimation::VM_FirstPerson);
Ogre::TagPoint *tag = mAnimation->attachObjectToBone("Head", mCamera);
tag->setInheritOrientation(false);
}
else
{
mAnimation->setViewMode(NpcAnimation::VM_Normal);
mCameraNode->attachObject(mCamera);
}
} }
float Camera::getHeight() float Camera::getHeight()
{ {
return mHeight * mTrackingPtr.getRefData().getBaseNode()->getScale().z; if(mCamera->isParentTagPoint())
{
Ogre::TagPoint *tag = static_cast<Ogre::TagPoint*>(mCamera->getParentNode());
return tag->_getFullLocalTransform().getTrans().z;
}
return mCamera->getParentNode()->getPosition().z;
} }
bool Camera::getPosition(Ogre::Vector3 &player, Ogre::Vector3 &camera) bool Camera::getPosition(Ogre::Vector3 &player, Ogre::Vector3 &camera)
{ {
mCamera->getParentSceneNode ()->needUpdate(true); mCamera->getParentSceneNode()->needUpdate(true);
camera = mCamera->getRealPosition(); camera = mCamera->getRealPosition();
player = mTrackingPtr.getRefData().getBaseNode()->getPosition(); player = mTrackingPtr.getRefData().getBaseNode()->getPosition();
@ -329,15 +336,6 @@ namespace MWRender
mFreeLook = enable; mFreeLook = enable;
} }
void Camera::setLowHeight(bool low)
{
if (low) {
mCameraNode->setPosition(0.f, 0.f, mHeight * 0.85);
} else {
mCameraNode->setPosition(0.f, 0.f, mHeight);
}
}
bool Camera::isVanityOrPreviewModeEnabled() bool Camera::isVanityOrPreviewModeEnabled()
{ {
return mPreviewMode || mVanity.enabled; return mPreviewMode || mVanity.enabled;

View file

@ -46,8 +46,6 @@ namespace MWRender
/// Updates sound manager listener data /// Updates sound manager listener data
void updateListener(); void updateListener();
void setLowHeight(bool low = true);
public: public:
Camera(Ogre::Camera *camera); Camera(Ogre::Camera *camera);
~Camera(); ~Camera();
@ -80,6 +78,8 @@ namespace MWRender
bool isFirstPerson() const bool isFirstPerson() const
{ return !(mVanity.enabled || mPreviewMode || !mFirstPersonView); } { return !(mVanity.enabled || mPreviewMode || !mFirstPersonView); }
void processViewChange();
void update(float duration); void update(float duration);
/// Set camera distance for current mode. Don't work on 1st person view. /// Set camera distance for current mode. Don't work on 1st person view.
@ -93,7 +93,6 @@ namespace MWRender
void setAnimation(NpcAnimation *anim); void setAnimation(NpcAnimation *anim);
void setHeight(float height);
float getHeight(); float getHeight();
/// Stores player and camera world positions in passed arguments /// Stores player and camera world positions in passed arguments