From Cedric Pinson and Jeremey Moles, Changes to OpenSceneGraph-osgWidget-dev branch.

Notes from Robert Osfield, Merged changes to OpenSceneGraph-osgWidget-dev r9367 (prior to my botched attempt at merged svn/trunk into the branch).
This commit is contained in:
Robert Osfield
2008-12-16 20:29:00 +00:00
parent 3313327ab4
commit 60fc821764
36 changed files with 1218 additions and 836 deletions

View File

@@ -18,15 +18,53 @@
using namespace osgAnimation;
AnimationManagerBase::~AnimationManagerBase() {}
AnimationManagerBase::AnimationManagerBase()
{
setUpdateCallback(new UpdateCallback);
_needToLink = false;
}
void AnimationManagerBase::clearTargets()
{
for (TargetSet::iterator it = _targets.begin(); it != _targets.end(); it++)
(*it).get()->reset();
}
void AnimationManagerBase::normalizeTargets()
{
for (TargetSet::iterator it = _targets.begin(); it != _targets.end(); it++)
(*it).get()->normalize();
}
void AnimationManagerBase::operator()(osg::Node* node, osg::NodeVisitor* nv)
{
if (nv && nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR)
{
if (needToLink())
{
/** manager need to link, it means that an animation has been added
so we need to relink all item animated with all animations.
We apply the linker visitor on the manager node to affect
all its children.
But it should not be done here, it should be done in the
update of AnimationManager
*/
link(node);
}
const osg::FrameStamp* fs = nv->getFrameStamp();
update(fs->getSimulationTime());
}
traverse(node,nv);
}
AnimationManagerBase::AnimationManagerBase(const AnimationManagerBase& b, const osg::CopyOp& copyop) : osg::NodeCallback(b,copyop)
{
_animations = b._animations;
_targets = b._targets;
_needToLink = b._needToLink;
}
void AnimationManagerBase::buildTargetReference()
{
_targets.clear();
@@ -52,20 +90,10 @@ bool AnimationManagerBase::needToLink() const { return _needToLink; }
void AnimationManagerBase::link()
void AnimationManagerBase::link(osg::Node* subgraph)
{
LinkVisitor linker(_animations);
accept(linker);
subgraph->accept(linker);
_needToLink = false;
buildTargetReference();
}
osgAnimation::AnimationMap AnimationManagerBase::getAnimationMap() const
{
osgAnimation::AnimationMap map;
for (AnimationList::const_iterator it = _animations.begin(); it != _animations.end(); it++)
map[(*it)->getName()] = *it;
return map;
}

View File

@@ -0,0 +1,138 @@
/* -*-c++-*-
* Copyright (C) 2008 Cedric Pinson <mornifle@plopbyte.net>
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osgAnimation/BasicAnimationManager>
#include <osgAnimation/LinkVisitor>
#include <osgAnimation/Assert>
using namespace osgAnimation;
BasicAnimationManager::~BasicAnimationManager() {}
void BasicAnimationManager::stopAll()
{
// loop over all playing animation
for( AnimationLayers::iterator iterAnim = _animationsPlaying.begin(); iterAnim != _animationsPlaying.end(); ++iterAnim )
{
AnimationList& list = iterAnim->second;
for (AnimationList::iterator it = list.begin(); it != list.end(); it++)
(*it)->resetTargets();
}
_animationsPlaying.clear();
}
BasicAnimationManager::BasicAnimationManager()
{
_lastUpdate = 0;
}
void BasicAnimationManager::playAnimation(Animation* pAnimation, int priority, float weight)
{
bool r = findAnimation(pAnimation);
OSGANIMATION_ASSERT(r && "This animation is not registered");
if ( isPlaying(pAnimation) )
stopAnimation(pAnimation);
_animationsPlaying[priority].push_back(pAnimation);
pAnimation->setStartTime(_lastUpdate);
pAnimation->setWeight(weight);
}
bool BasicAnimationManager::stopAnimation(Animation* pAnimation)
{
// search though the layer and remove animation
for( AnimationLayers::iterator iterAnim = _animationsPlaying.begin(); iterAnim != _animationsPlaying.end(); ++iterAnim )
{
AnimationList& list = iterAnim->second;
for (AnimationList::iterator it = list.begin(); it != list.end(); it++)
if( (*it) == pAnimation )
{
(*it)->resetTargets();
list.erase(it);
return true;
}
}
return false;
}
void BasicAnimationManager::update (double time)
{
if (!_lastUpdate)
_lastUpdate = time;
// could filtered with an active flag
for (TargetSet::iterator it = _targets.begin(); it != _targets.end(); it++)
(*it).get()->reset();
// update from high priority to low priority
for( AnimationLayers::reverse_iterator iterAnim = _animationsPlaying.rbegin(); iterAnim != _animationsPlaying.rend(); ++iterAnim )
{
// update all animation
std::vector<int> toremove;
AnimationList& list = iterAnim->second;
for (unsigned int i = 0; i < list.size(); i++)
{
if (! list[i]->update(time))
toremove.push_back(i);
}
// remove finished animation
while (!toremove.empty())
{
list.erase(list.begin() + toremove.back());
toremove.pop_back();
}
}
for (TargetSet::iterator it = _targets.begin(); it != _targets.end(); it++)
(*it).get()->normalize();
}
bool BasicAnimationManager::findAnimation(Animation* pAnimation)
{
for( AnimationList::const_iterator iterAnim = _animations.begin(); iterAnim != _animations.end(); ++iterAnim )
{
if ( (*iterAnim) == pAnimation )
return true;
}
return false;
}
bool BasicAnimationManager::isPlaying(Animation* pAnimation)
{
for( AnimationLayers::iterator iterAnim = _animationsPlaying.begin(); iterAnim != _animationsPlaying.end(); ++iterAnim )
{
AnimationList& list = iterAnim->second;
for (AnimationList::iterator it = list.begin(); it != list.end(); it++)
if ( (*it) == pAnimation )
return true;
}
return false;
}
bool BasicAnimationManager::isPlaying(const std::string& name)
{
// loop over all playing animation
for( AnimationLayers::iterator iterAnim = _animationsPlaying.begin(); iterAnim != _animationsPlaying.end(); ++iterAnim )
{
AnimationList& list = iterAnim->second;
for (AnimationList::iterator it = list.begin(); it != list.end(); it++)
if ( (*it)->getName() == name )
return true;
}
return false;
}

View File

@@ -24,6 +24,31 @@ osgAnimation::Bone::UpdateBone::UpdateBone(const osgAnimation::Bone::UpdateBone&
{
}
osgAnimation::Bone::Bone(const Bone& b, const osg::CopyOp& copyop)
: osg::Transform(b,copyop),
_position(b._position),
_rotation(b._rotation),
_scale(b._scale)
{
}
osgAnimation::Bone::Bone(const std::string& name)
{
if (!name.empty())
setName(name);
_needToRecomputeBindMatrix = false;
}
void osgAnimation::Bone::setDefaultUpdateCallback(const std::string& name)
{
std::string cbName = name;
if (cbName.empty())
cbName = getName();
setUpdateCallback(new UpdateBone(cbName));
}
void osgAnimation::Bone::computeBindMatrix()
{
_invBindInSkeletonSpace = osg::Matrix::inverse(_bindInBoneSpace);

View File

@@ -10,26 +10,27 @@ SET(LIB_NAME osgAnimation)
SET(HEADER_PATH ${OpenSceneGraph_SOURCE_DIR}/include/${LIB_NAME})
SET(LIB_PUBLIC_HEADERS
${HEADER_PATH}/Export
${HEADER_PATH}/Bone
${HEADER_PATH}/Skeleton
${HEADER_PATH}/Channel
${HEADER_PATH}/Sampler
${HEADER_PATH}/Interpolator
${HEADER_PATH}/Target
${HEADER_PATH}/Animation
${HEADER_PATH}/Keyframe
${HEADER_PATH}/Skinning
${HEADER_PATH}/CubicBezier
${HEADER_PATH}/Vec3Packed
${HEADER_PATH}/AnimationManager
${HEADER_PATH}/AnimationManagerBase
${HEADER_PATH}/UpdateCallback
${HEADER_PATH}/LinkVisitor
${HEADER_PATH}/VertexInfluence
${HEADER_PATH}/EaseMotion
${HEADER_PATH}/Assert
${HEADER_PATH}/Timeline
${HEADER_PATH}/Export
${HEADER_PATH}/Bone
${HEADER_PATH}/Skeleton
${HEADER_PATH}/Channel
${HEADER_PATH}/Sampler
${HEADER_PATH}/Interpolator
${HEADER_PATH}/Target
${HEADER_PATH}/Animation
${HEADER_PATH}/Keyframe
${HEADER_PATH}/Skinning
${HEADER_PATH}/CubicBezier
${HEADER_PATH}/Vec3Packed
${HEADER_PATH}/BasicAnimationManager
${HEADER_PATH}/TimelineAnimationManager
${HEADER_PATH}/AnimationManagerBase
${HEADER_PATH}/UpdateCallback
${HEADER_PATH}/LinkVisitor
${HEADER_PATH}/VertexInfluence
${HEADER_PATH}/EaseMotion
${HEADER_PATH}/Assert
${HEADER_PATH}/Timeline
)
@@ -41,7 +42,8 @@ ADD_LIBRARY(${LIB_NAME}
Animation.cpp
Bone.cpp
RigGeometry.cpp
AnimationManager.cpp
BasicAnimationManager.cpp
TimelineAnimationManager.cpp
AnimationManagerBase.cpp
Skeleton.cpp
VertexInfluence.cpp

View File

@@ -22,14 +22,45 @@
*/
#include <osgAnimation/RigGeometry>
void osgAnimation::RigGeometry::buildTransformer(Skeleton* root)
using namespace osgAnimation;
RigGeometry::RigGeometry()
{
setUseDisplayList(false);
setUpdateCallback(new UpdateVertex);
setDataVariance(osg::Object::DYNAMIC);
_needToComputeMatrix = true;
_matrixFromSkeletonToGeometry = _invMatrixFromSkeletonToGeometry = osg::Matrix::identity();
}
RigGeometry::RigGeometry(const osg::Geometry& b) : osg::Geometry(b, osg::CopyOp::SHALLOW_COPY)
{
setUseDisplayList(false);
setUpdateCallback(new UpdateVertex);
setDataVariance(osg::Object::DYNAMIC);
_needToComputeMatrix = true;
_matrixFromSkeletonToGeometry = _invMatrixFromSkeletonToGeometry = osg::Matrix::identity();
}
RigGeometry::RigGeometry(const RigGeometry& b, const osg::CopyOp& copyop) :
osg::Geometry(b,copyop),
_positionSource(b._positionSource),
_normalSource(b._normalSource),
_vertexInfluenceSet(b._vertexInfluenceSet),
_vertexInfluenceMap(b._vertexInfluenceMap),
_transformVertexes(b._transformVertexes),
_needToComputeMatrix(b._needToComputeMatrix)
{
}
void RigGeometry::buildTransformer(Skeleton* root)
{
Bone::BoneMap bm = root->getBoneMap();
_transformVertexes.init(bm, _vertexInfluenceSet.getUniqVertexSetToBoneSetList());
_root = root;
}
void osgAnimation::RigGeometry::buildVertexSet()
void RigGeometry::buildVertexSet()
{
if (!_vertexInfluenceMap.valid())
{
@@ -47,7 +78,7 @@ void osgAnimation::RigGeometry::buildVertexSet()
std::cout << "uniq groups " << _vertexInfluenceSet.getUniqVertexSetToBoneSetList().size() << " for " << getName() << std::endl;
}
void osgAnimation::RigGeometry::computeMatrixFromRootSkeleton()
void RigGeometry::computeMatrixFromRootSkeleton()
{
if (!_root.valid())
{
@@ -60,8 +91,11 @@ void osgAnimation::RigGeometry::computeMatrixFromRootSkeleton()
_needToComputeMatrix = false;
}
void osgAnimation::RigGeometry::transformSoftwareMethod()
void RigGeometry::transformSoftwareMethod()
{
setUseDisplayList(false);
setUseVertexBufferObjects(true);
// std::cout << getName() << " _matrixFromSkeletonToGeometry" << _matrixFromSkeletonToGeometry << std::endl;
osg::Vec3Array* pos = dynamic_cast<osg::Vec3Array*>(getVertexArray());
if (pos && _positionSource.size() != pos->size())
@@ -86,8 +120,10 @@ void osgAnimation::RigGeometry::transformSoftwareMethod()
_transformVertexes.compute<osg::Vec3>(_matrixFromSkeletonToGeometry, _invMatrixFromSkeletonToGeometry, &_normalSource.front(), &normal->front());
normal->dirty();
}
if (getUseDisplayList())
dirtyDisplayList();
dirtyBound();
}
const osgAnimation::Skeleton* osgAnimation::RigGeometry::getSkeleton() const { return _root.get(); }
osgAnimation::Skeleton* osgAnimation::RigGeometry::getSkeleton() { return _root.get(); }
const osgAnimation::Skeleton* RigGeometry::getSkeleton() const { return _root.get(); }
osgAnimation::Skeleton* RigGeometry::getSkeleton() { return _root.get(); }

View File

@@ -52,15 +52,15 @@ struct updateMatrixVisitor : public osg::NodeVisitor
}
if (parent)
bone->_boneInSkeletonSpace = bone->getMatrixInBoneSpace() * bone->getBoneParent()->getMatrixInSkeletonSpace();
bone->setBoneInSkeletonSpace(bone->getMatrixInBoneSpace() * bone->getBoneParent()->getMatrixInSkeletonSpace());
else
bone->_boneInSkeletonSpace = bone->getMatrixInBoneSpace();// * _skeleton;
bone->setBoneInSkeletonSpace(bone->getMatrixInBoneSpace());
traverse(node);
}
};
void Skeleton::UpdateCallback::operator()(osg::Node* node, osg::NodeVisitor* nv)
void Skeleton::UpdateSkeleton::operator()(osg::Node* node, osg::NodeVisitor* nv)
{
if (nv && nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR)
{
@@ -72,23 +72,18 @@ void Skeleton::UpdateCallback::operator()(osg::Node* node, osg::NodeVisitor* nv)
// process. It's important to update Bone before other things because the update
// of RigGeometry need it
updateMatrixVisitor visitor;
#if 0
visitor._skeleton = b->getMatrix();
int numChildren = b->getNumChildren();
for (int i = 0; i < numChildren; i++)
{
b->getChild(i)->accept(visitor);
}
#else
b->accept(visitor);
#endif
}
}
traverse(node,nv);
}
Skeleton::Skeleton()
Skeleton::Skeleton()
{
setUpdateCallback(new UpdateCallback());
}
void Skeleton::setDefaultUpdateCallback()
{
setUpdateCallback(new Skeleton::UpdateSkeleton );
}

View File

@@ -16,34 +16,32 @@
using namespace osgAnimation;
// temporary
// the problem comes that the AnimationManagerBase should only a group
// and it's data should be in an update callback
struct TimelineAdaptator : public Timeline
{
osg::ref_ptr<AnimationManagerTimeline> _manager;
TimelineAdaptator(AnimationManagerTimeline* manager) : _manager(manager) {}
void evaluate(unsigned int frame)
{
_manager->clearTargets();
Timeline::evaluate(frame);
_manager->normalizeTargets();
}
};
AnimationManagerTimeline::AnimationManagerTimeline()
Timeline::Timeline()
{
_timeline = new TimelineAdaptator(this);
_lastUpdate = 0;
_currentFrame = 0;
_fps = 25;
_speed = 1.0;
_state = Stop;
_initFirstFrame = false;
_previousFrameEvaluated = 0;
_evaluating = 0;
_numberFrame = -1; // something like infinity
setName("Timeline");
}
AnimationManagerTimeline::AnimationManagerTimeline(const AnimationManagerBase& manager) : AnimationManagerBase(manager)
Timeline::Timeline(const Timeline& nc,const osg::CopyOp& op) : osg::Object(nc, op),
_actions(nc._actions)
{
_timeline = new TimelineAdaptator(this);
_lastUpdate = 0;
_currentFrame = 0;
_fps = 25;
_speed = 1.0;
_state = Stop;
_initFirstFrame = false;
_previousFrameEvaluated = 0;
_evaluating = 0;
_numberFrame = -1; // something like infinity
setName("Timeline");
}
void AnimationManagerTimeline::update(double time)
{
_timeline->update(time);
}

View File

@@ -0,0 +1,40 @@
/* -*-c++-*-
* Copyright (C) 2008 Cedric Pinson <mornifle@plopbyte.net>
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osgAnimation/Timeline>
#include <osgAnimation/TimelineAnimationManager>
using namespace osgAnimation;
TimelineAnimationManager::TimelineAnimationManager()
{
_timeline = new Timeline;
}
TimelineAnimationManager::TimelineAnimationManager(const AnimationManagerBase& manager) : AnimationManagerBase(manager)
{
_timeline = new Timeline;
}
TimelineAnimationManager::TimelineAnimationManager(const TimelineAnimationManager& nc,const osg::CopyOp& co) : AnimationManagerBase(nc, co)
{
_timeline = new Timeline(*nc.getTimeline());
}
void TimelineAnimationManager::update(double time)
{
clearTargets();
_timeline->update(time);
normalizeTargets();
}

View File

@@ -54,9 +54,9 @@ void AnimationUpdateCallback::updateLink()
Maybe this function should be on the manager side like
_manager->linkItem(Bone);
*/
AnimationMap map = _manager->getAnimationMap();
for (AnimationMap::iterator it = map.begin(); it != map.end(); it++)
link(it->second.get());
const AnimationList& animationList = _manager->getAnimationList();
for (AnimationList::const_iterator it = animationList.begin(); it != animationList.end(); it++)
link(it->get());
_manager->buildTargetReference();
}
}