From Roland Smeenk & Cedric Pinson,
"Summary of changes: From Roland -Added MorphGeometry -Bone Bindmatrix is only calculated if needed -osgAnimation plugin now supports all available channel types (before only linear vec3 or quat channels) -osgAnimation plugin now supports MorphGeometry -osgAnimation plugin now supports animation and channel weights, animation playmode, duration and starttime -removed osgAnimationManager.cpp from CMakeList From Cedric -fixed the last_update field (it was only updated at the first update) in BasicAnimationManager.cpp - Refactore some part of MorphGeometry minor changes - Add osganimationmorph as example "
This commit is contained in:
@@ -37,7 +37,7 @@ void AnimationManagerBase::normalizeTargets()
|
||||
|
||||
void AnimationManagerBase::operator()(osg::Node* node, osg::NodeVisitor* nv)
|
||||
{
|
||||
if (nv && nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR)
|
||||
if (nv && nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR)
|
||||
{
|
||||
if (needToLink())
|
||||
{
|
||||
|
||||
@@ -47,14 +47,14 @@ void BasicAnimationManager::stopAll()
|
||||
void BasicAnimationManager::playAnimation(Animation* pAnimation, int priority, float weight)
|
||||
{
|
||||
if (!findAnimation(pAnimation))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ( isPlaying(pAnimation) )
|
||||
stopAnimation(pAnimation);
|
||||
|
||||
_animationsPlaying[priority].push_back(pAnimation);
|
||||
// for debug
|
||||
//std::cout << "player Animation " << pAnimation->getName() << " at " << _lastUpdate << std::endl;
|
||||
pAnimation->setStartTime(_lastUpdate);
|
||||
pAnimation->setWeight(weight);
|
||||
}
|
||||
@@ -62,7 +62,7 @@ void BasicAnimationManager::playAnimation(Animation* pAnimation, int priority, f
|
||||
bool BasicAnimationManager::stopAnimation(Animation* pAnimation)
|
||||
{
|
||||
// search though the layer and remove animation
|
||||
for( AnimationLayers::iterator iterAnim = _animationsPlaying.begin(); iterAnim != _animationsPlaying.end(); ++iterAnim )
|
||||
for( AnimationLayers::iterator iterAnim = _animationsPlaying.begin(); iterAnim != _animationsPlaying.end(); ++iterAnim )
|
||||
{
|
||||
AnimationList& list = iterAnim->second;
|
||||
for (AnimationList::iterator it = list.begin(); it != list.end(); it++)
|
||||
@@ -79,8 +79,7 @@ bool BasicAnimationManager::stopAnimation(Animation* pAnimation)
|
||||
|
||||
void BasicAnimationManager::update (double time)
|
||||
{
|
||||
if (!_lastUpdate)
|
||||
_lastUpdate = time;
|
||||
_lastUpdate = time; // keep time of last update
|
||||
|
||||
// could filtered with an active flag
|
||||
for (TargetSet::iterator it = _targets.begin(); it != _targets.end(); it++)
|
||||
@@ -94,8 +93,16 @@ void BasicAnimationManager::update (double time)
|
||||
AnimationList& list = iterAnim->second;
|
||||
for (unsigned int i = 0; i < list.size(); i++)
|
||||
{
|
||||
if (! list[i]->update(time))
|
||||
if (! list[i]->update(time))
|
||||
{
|
||||
// debug
|
||||
// std::cout << list[i]->getName() << " finished at " << time << std::endl;
|
||||
toremove.push_back(i);
|
||||
} else
|
||||
{
|
||||
// debug
|
||||
//std::cout << list[i]->getName() << " updated" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// remove finished animation
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
#include <osgAnimation/Bone>
|
||||
#include <osgAnimation/Skeleton>
|
||||
|
||||
osgAnimation::Bone::UpdateBone::UpdateBone(const osgAnimation::Bone::UpdateBone& apc,const osg::CopyOp& copyop):
|
||||
osgAnimation::Bone::UpdateBone::UpdateBone(const osgAnimation::Bone::UpdateBone& apc,const osg::CopyOp& copyop) :
|
||||
osgAnimation::AnimationUpdateCallback(apc, copyop),
|
||||
_position(apc._position),
|
||||
_quaternion(apc._quaternion),
|
||||
@@ -25,11 +25,12 @@ 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),
|
||||
osgAnimation::Bone::Bone(const Bone& b, const osg::CopyOp& copyop) :
|
||||
osg::Transform(b,copyop),
|
||||
_position(b._position),
|
||||
_rotation(b._rotation),
|
||||
_scale(b._scale)
|
||||
_scale(b._scale),
|
||||
_needToRecomputeBindMatrix(true)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -56,24 +57,7 @@ void osgAnimation::Bone::computeBindMatrix()
|
||||
_needToRecomputeBindMatrix = false;
|
||||
if (!parent)
|
||||
{
|
||||
#if 0
|
||||
// no more parent means, we get the skeleton
|
||||
if (getParents().empty()) {
|
||||
osg::notify(osg::WARN) << "Warning " << className() <<"::computeBindMatrix you should not have this message, it means you miss to attach this bone(" << getName() <<") to a Skeleton node" << std::endl;
|
||||
return;
|
||||
} else if (getParents().size() > 1) {
|
||||
osg::notify(osg::WARN) << "Warning " << className() <<"::computeBindMatrix you have more than one parent in a skeleton structure (" << getName() <<") unknown behaviour" << std::endl;
|
||||
return;
|
||||
}
|
||||
osgAnimation::Skeleton* skel = dynamic_cast<osgAnimation::Skeleton*>(getParents()[0]);
|
||||
if (!skel) {
|
||||
osg::notify(osg::WARN) << "Warning " << className() <<"::computeBindMatrix you should not have this message, it means you miss to attach this bone(" << getName() <<") to a Skeleton node" << std::endl;
|
||||
return;
|
||||
}
|
||||
_invBindInSkeletonSpace = osg::Matrix::inverse(skel->getMatrix()) * _invBindInSkeletonSpace;
|
||||
#else
|
||||
osg::notify(osg::WARN) << "Warning " << className() <<"::computeBindMatrix you should not have this message, it means you miss to attach this bone(" << getName() <<") to a Skeleton node" << std::endl;
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
_invBindInSkeletonSpace = parent->getInvBindMatrixInSkeletonSpace() * _invBindInSkeletonSpace;
|
||||
|
||||
@@ -10,47 +10,49 @@ 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}/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
|
||||
${HEADER_PATH}/BasicAnimationManager
|
||||
${HEADER_PATH}/Bone
|
||||
${HEADER_PATH}/Channel
|
||||
${HEADER_PATH}/CubicBezier
|
||||
${HEADER_PATH}/EaseMotion
|
||||
${HEADER_PATH}/Export
|
||||
${HEADER_PATH}/Interpolator
|
||||
${HEADER_PATH}/Keyframe
|
||||
${HEADER_PATH}/LinkVisitor
|
||||
${HEADER_PATH}/MorphGeometry
|
||||
${HEADER_PATH}/RigGeometry
|
||||
${HEADER_PATH}/Sampler
|
||||
${HEADER_PATH}/Skeleton
|
||||
${HEADER_PATH}/Skinning
|
||||
${HEADER_PATH}/Target
|
||||
${HEADER_PATH}/Timeline
|
||||
${HEADER_PATH}/TimelineAnimationManager
|
||||
${HEADER_PATH}/UpdateCallback
|
||||
${HEADER_PATH}/Vec3Packed
|
||||
${HEADER_PATH}/VertexInfluence
|
||||
)
|
||||
|
||||
|
||||
ADD_LIBRARY(${LIB_NAME}
|
||||
${OPENSCENEGRAPH_USER_DEFINED_DYNAMIC_OR_STATIC}
|
||||
${LIB_PUBLIC_HEADERS}
|
||||
Channel.cpp
|
||||
Target.cpp
|
||||
Animation.cpp
|
||||
Bone.cpp
|
||||
RigGeometry.cpp
|
||||
AnimationManagerBase.cpp
|
||||
AnimationManager.cpp
|
||||
BasicAnimationManager.cpp
|
||||
TimelineAnimationManager.cpp
|
||||
AnimationManagerBase.cpp
|
||||
Bone.cpp
|
||||
Channel.cpp
|
||||
MorphGeometry.cpp
|
||||
RigGeometry.cpp
|
||||
Skeleton.cpp
|
||||
VertexInfluence.cpp
|
||||
UpdateCallback.cpp
|
||||
Target.cpp
|
||||
TimelineAnimationManager.cpp
|
||||
Timeline.cpp
|
||||
UpdateCallback.cpp
|
||||
VertexInfluence.cpp
|
||||
${OPENSCENEGRAPH_VERSIONINFO_RC}
|
||||
)
|
||||
|
||||
|
||||
277
src/osgAnimation/MorphGeometry.cpp
Normal file
277
src/osgAnimation/MorphGeometry.cpp
Normal file
@@ -0,0 +1,277 @@
|
||||
/* -*-c++-*-
|
||||
* Copyright (C) 2008 Cedric Pinson <mornifle@plopbyte.net>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
* Authors:
|
||||
*
|
||||
* Roland Smeenk
|
||||
* Cedric Pinson <mornifle@plopbyte.net>
|
||||
*
|
||||
*/
|
||||
#include <osg/Geode>
|
||||
#include <osgAnimation/MorphGeometry>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
using namespace osgAnimation;
|
||||
|
||||
MorphGeometry::MorphGeometry() :
|
||||
_dirty(false),
|
||||
_method(NORMALIZED),
|
||||
_morphNormals(true)
|
||||
{
|
||||
setUseDisplayList(false);
|
||||
setUpdateCallback(new UpdateVertex);
|
||||
setDataVariance(osg::Object::DYNAMIC);
|
||||
setUseVertexBufferObjects(true);
|
||||
}
|
||||
|
||||
MorphGeometry::MorphGeometry(const osg::Geometry& b) :
|
||||
osg::Geometry(b, osg::CopyOp::DEEP_COPY_ARRAYS),
|
||||
_dirty(false),
|
||||
_method(NORMALIZED),
|
||||
_morphNormals(true)
|
||||
{
|
||||
setUseDisplayList(false);
|
||||
setUpdateCallback(new UpdateVertex);
|
||||
setDataVariance(osg::Object::DYNAMIC);
|
||||
setUseVertexBufferObjects(true);
|
||||
if (b.getInternalOptimizedGeometry())
|
||||
computeInternalOptimizedGeometry();
|
||||
}
|
||||
|
||||
MorphGeometry::MorphGeometry(const MorphGeometry& b, const osg::CopyOp& copyop) :
|
||||
osg::Geometry(b,copyop),
|
||||
_dirty(b._dirty),
|
||||
_method(b._method),
|
||||
_morphTargets(b._morphTargets),
|
||||
_positionSource(b._positionSource),
|
||||
_normalSource(b._normalSource),
|
||||
_morphNormals(b._morphNormals)
|
||||
{
|
||||
setUseDisplayList(false);
|
||||
setUseVertexBufferObjects(true);
|
||||
if (b.getInternalOptimizedGeometry())
|
||||
computeInternalOptimizedGeometry();
|
||||
}
|
||||
|
||||
void MorphGeometry::transformSoftwareMethod()
|
||||
{
|
||||
if (_dirty)
|
||||
{
|
||||
// See if we have an internal optimized geometry
|
||||
osg::Geometry* morphGeometry = this;
|
||||
if (_internalOptimizedGeometry.valid())
|
||||
morphGeometry = _internalOptimizedGeometry;
|
||||
|
||||
osg::Vec3Array* pos = dynamic_cast<osg::Vec3Array*>(morphGeometry->getVertexArray());
|
||||
if (pos && _positionSource.size() != pos->size())
|
||||
{
|
||||
_positionSource = std::vector<osg::Vec3>(pos->begin(),pos->end());
|
||||
pos->setDataVariance(osg::Object::DYNAMIC);
|
||||
}
|
||||
|
||||
osg::Vec3Array* normal = dynamic_cast<osg::Vec3Array*>(morphGeometry->getNormalArray());
|
||||
if (normal && _normalSource.size() != normal->size())
|
||||
{
|
||||
_normalSource = std::vector<osg::Vec3>(normal->begin(),normal->end());
|
||||
normal->setDataVariance(osg::Object::DYNAMIC);
|
||||
}
|
||||
|
||||
|
||||
if (!_positionSource.empty())
|
||||
{
|
||||
bool initialized = false;
|
||||
if (_method == NORMALIZED)
|
||||
{
|
||||
// base * 1 - (sum of weights) + sum of (weight * target)
|
||||
float baseWeight = 0;
|
||||
for (unsigned int i=0; i < _morphTargets.size(); i++)
|
||||
{
|
||||
baseWeight += _morphTargets[i].getWeight();
|
||||
}
|
||||
baseWeight = 1 - baseWeight;
|
||||
|
||||
if (baseWeight != 0)
|
||||
{
|
||||
initialized = true;
|
||||
for (unsigned int i=0; i < pos->size(); i++)
|
||||
{
|
||||
(*pos)[i] = _positionSource[i] * baseWeight;
|
||||
}
|
||||
if (_morphNormals)
|
||||
{
|
||||
for (unsigned int i=0; i < normal->size(); i++)
|
||||
{
|
||||
(*normal)[i] = _normalSource[i] * baseWeight;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else //if (_method == RELATIVE)
|
||||
{
|
||||
// base + sum of (weight * target)
|
||||
initialized = true;
|
||||
for (unsigned int i=0; i < pos->size(); i++)
|
||||
{
|
||||
(*pos)[i] = _positionSource[i];
|
||||
}
|
||||
if (_morphNormals)
|
||||
{
|
||||
for (unsigned int i=0; i < normal->size(); i++)
|
||||
{
|
||||
(*normal)[i] = _normalSource[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (unsigned int i=0; i < _morphTargets.size(); i++)
|
||||
{
|
||||
if (_morphTargets[i].getWeight() > 0)
|
||||
{
|
||||
// See if any the targets use the internal optimized geometry
|
||||
osg::Geometry* targetGeometry = _morphTargets[i].getGeometry()->getInternalOptimizedGeometry();
|
||||
if (!targetGeometry)
|
||||
targetGeometry = _morphTargets[i].getGeometry();
|
||||
|
||||
osg::Vec3Array* targetPos = dynamic_cast<osg::Vec3Array*>(targetGeometry->getVertexArray());
|
||||
osg::Vec3Array* targetNormals = dynamic_cast<osg::Vec3Array*>(targetGeometry->getNormalArray());
|
||||
|
||||
if (initialized)
|
||||
{
|
||||
// If vertices are initialized, add the morphtargets
|
||||
for (unsigned int j=0; j < pos->size(); j++)
|
||||
{
|
||||
(*pos)[j] += (*targetPos)[j] * _morphTargets[i].getWeight();
|
||||
}
|
||||
|
||||
if (_morphNormals)
|
||||
{
|
||||
for (unsigned int j=0; j < normal->size(); j++)
|
||||
{
|
||||
(*normal)[j] += (*targetNormals)[j] * _morphTargets[i].getWeight();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// If not initialized, initialize with this morph target
|
||||
initialized = true;
|
||||
for (unsigned int j=0; j < pos->size(); j++)
|
||||
{
|
||||
(*pos)[j] = (*targetPos)[j] * _morphTargets[i].getWeight();
|
||||
}
|
||||
|
||||
if (_morphNormals)
|
||||
{
|
||||
for (unsigned int j=0; j < normal->size(); j++)
|
||||
{
|
||||
(*normal)[j] = (*targetNormals)[j] * _morphTargets[i].getWeight();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pos->dirty();
|
||||
if (_morphNormals)
|
||||
{
|
||||
for (unsigned int j=0; j < normal->size(); j++)
|
||||
{
|
||||
(*normal)[j].normalize();
|
||||
}
|
||||
normal->dirty();
|
||||
}
|
||||
}
|
||||
|
||||
dirtyBound();
|
||||
_dirty = false;
|
||||
}
|
||||
}
|
||||
|
||||
UpdateMorph::UpdateMorph(const UpdateMorph& apc,const osg::CopyOp& copyop) : AnimationUpdateCallback(apc, copyop)
|
||||
{
|
||||
}
|
||||
|
||||
UpdateMorph::UpdateMorph(const std::string& name) : AnimationUpdateCallback(name)
|
||||
{
|
||||
}
|
||||
|
||||
/** Callback method called by the NodeVisitor when visiting a node.*/
|
||||
void UpdateMorph::operator()(osg::Node* node, osg::NodeVisitor* nv)
|
||||
{
|
||||
if (nv && nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR)
|
||||
{
|
||||
osg::Geode* geode = dynamic_cast<osg::Geode*>(node);
|
||||
if (geode)
|
||||
{
|
||||
unsigned int numDrawables = geode->getNumDrawables();
|
||||
for (unsigned int i = 0; i != numDrawables; ++i)
|
||||
{
|
||||
osgAnimation::MorphGeometry* morph = dynamic_cast<osgAnimation::MorphGeometry*>(geode->getDrawable(i));
|
||||
if (morph)
|
||||
{
|
||||
// Update morph weights
|
||||
std::map<int, osg::ref_ptr<osgAnimation::FloatTarget> >::iterator iter = _weightTargets.begin();
|
||||
while (iter != _weightTargets.end())
|
||||
{
|
||||
if (iter->second->getValue() >= 0)
|
||||
{
|
||||
morph->setWeight(iter->first, iter->second->getValue());
|
||||
}
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
traverse(node,nv);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool UpdateMorph::needLink() const
|
||||
{
|
||||
// the idea is to return true if nothing is linked
|
||||
return (_weightTargets.size() == 0);
|
||||
}
|
||||
|
||||
bool UpdateMorph::link(osgAnimation::Channel* channel)
|
||||
{
|
||||
// Typically morph geometries only have the weights for morph targets animated
|
||||
|
||||
// Expect a weight value
|
||||
// TODO Should we make this more generic to handle other things than single values?
|
||||
int weightIndex = atoi(channel->getName().c_str());
|
||||
|
||||
if (weightIndex >= 0)
|
||||
{
|
||||
osgAnimation::FloatLinearChannel* fc = dynamic_cast<osgAnimation::FloatLinearChannel*>(channel);
|
||||
if (fc)
|
||||
{
|
||||
osgAnimation::FloatTarget* ft = new osgAnimation::FloatTarget;
|
||||
_weightTargets[weightIndex] = ft;
|
||||
ft->setValue(-1);
|
||||
fc->setTarget(ft);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Channel " << channel->getName() << " does not contain a valid symbolic name for this class" << std::endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -27,7 +27,9 @@ struct computeBindMatrixVisitor : public osg::NodeVisitor
|
||||
Bone* bone = dynamic_cast<Bone*>(&node);
|
||||
if (!bone)
|
||||
return;
|
||||
bone->computeBindMatrix();
|
||||
if (bone->needToComputeBindMatrix())
|
||||
bone->computeBindMatrix();
|
||||
|
||||
traverse(node);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user