root/OpenSceneGraph/trunk/src/osgAnimation/RigGeometry.cpp @ 11009

Revision 11009, 7.7 kB (checked in by robert, 4 years ago)

From Cedric Pinson, "Here a list of changes:
Bone now inherit from MatrixTransform?. It simplify a lot the update of
Bone matrix. It helps to have the bone system more generic. eg it's now
possible to have animation data with precomputed bind matrix. The other
benefit, is now the collada plugin will be able to use osgAnimation to
display skinned mesh. Michael Plating did a great work to improve this
aspect, he is working on the collada plugin and should be able to submit
a new version soon.
The RigGeometry? has been refactored so now it works when you save and
reload RigGeometry? because the source is not touched anymore. The
benefit with this update is that it should be now possible to use a
MorphGeometry? as source for a RigGeometry?.

The bad news is that the format has changed, so i have rebuild osg-data
related to osgAnimation data, updated the blender exporter to export to
the new format.
The fbx plugin could be touched about this commit, i dont compile it so
i can't give more information about it.
The bvh plugin has been updated by Wang rui so this one is fixed with
the new code of osgAnimation.
The examples has been updated to work with the new code too...

The example osg-data/example.osg should be remove, it's an old example
that does not work.

For people using blender the blender exporter up to date is here:
http://hg.plopbyte.net/osgexport2/
it will be merge to http://hg.plopbyte.net/osgexport/ as soon as the
modification will be push in the trunk.
"

RevLine 
[9093]1/*  -*-c++-*-
[10693]2 *  Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
[9093]3 *
[10693]4 * This library is open source and may be redistributed and/or modified under 
5 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
6 * (at your option) any later version.  The full license is in LICENSE file
7 * included with this distribution, and on the openscenegraph.org website.
[9093]8 *
[10693]9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * OpenSceneGraph Public License for more details.
[9093]13 */
[10693]14
[11009]15#include <osgAnimation/VertexInfluence>
[9093]16#include <osgAnimation/RigGeometry>
[10693]17#include <osgAnimation/RigTransformSoftware>
18#include <sstream>
19#include <osg/GL2Extensions>
[9093]20
[9370]21using namespace osgAnimation;
22
[10877]23// The idea is to compute a bounding box with a factor x of the first step we compute the bounding box
24class RigComputeBoundingBoxCallback : public osg::Drawable::ComputeBoundingBoxCallback
25{
26public:
27    RigComputeBoundingBoxCallback(double factor = 2.0) : _computed(false), _factor(factor) {}
28    void reset() { _computed = false; }
29    virtual osg::BoundingBox computeBound(const osg::Drawable& drawable) const
30    {
31        const osgAnimation::RigGeometry& rig = dynamic_cast<const osgAnimation::RigGeometry&>(drawable);
32
33        // if a valid inital bounding box is set we use it without asking more
34        if (rig.getInitialBound().valid())
35            return rig.getInitialBound();
36
37        if (_computed)
38            return _boundingBox;
39
40        // if the computing of bb is invalid (like no geometry inside)
41        // then dont tag the bounding box as computed
42        osg::BoundingBox bb = rig.computeBound();
43        if (!bb.valid())
44            return bb;
45
46
47        _boundingBox.expandBy(bb);
48        osg::Vec3 center = _boundingBox.center();
49        osg::Vec3 vec = (_boundingBox._max-center)*_factor;
50        _boundingBox.expandBy(center + vec);
51        _boundingBox.expandBy(center - vec);
52        _computed = true;
53//        osg::notify(osg::NOTICE) << "build the bounding box for RigGeometry " << rig.getName() << " " << _boundingBox._min << " " << _boundingBox._max << std::endl;
54        return _boundingBox;
55    }
56protected:
57    mutable bool _computed;
58    double _factor;
59    mutable osg::BoundingBox _boundingBox;
60};
61
[9370]62RigGeometry::RigGeometry()
[9093]63{
[10693]64    _supportsDisplayList = false;
65    setUseVertexBufferObjects(true);
[9370]66    setUpdateCallback(new UpdateVertex);
67    setDataVariance(osg::Object::DYNAMIC);
68    _needToComputeMatrix = true;
69    _matrixFromSkeletonToGeometry = _invMatrixFromSkeletonToGeometry = osg::Matrix::identity();
[10693]70
71    // disable the computation of boundingbox for the rig mesh
[10877]72    setComputeBoundingBoxCallback(new RigComputeBoundingBoxCallback);
[9370]73}
74
[10693]75
76RigGeometry::RigGeometry(const RigGeometry& b, const osg::CopyOp& copyop) :
[9370]77    osg::Geometry(b,copyop),
[11009]78    _geometry(b._geometry),
[9370]79    _vertexInfluenceSet(b._vertexInfluenceSet),
80    _vertexInfluenceMap(b._vertexInfluenceMap),
[10693]81    _needToComputeMatrix(b._needToComputeMatrix)
[9370]82{
[10693]83    // we dont copy the RigImplementation yet. because the RigImplementation need to be initialized in a valid graph, with a skeleton ...
84    // dont know yet what to do with a clone of a RigGeometry
[9370]85}
86
[10693]87
88const osg::Matrix& RigGeometry::getMatrixFromSkeletonToGeometry() const { return _matrixFromSkeletonToGeometry; }
89const osg::Matrix& RigGeometry::getInvMatrixFromSkeletonToGeometry() const { return _invMatrixFromSkeletonToGeometry;}
90
91
92void RigGeometry::drawImplementation(osg::RenderInfo& renderInfo) const
[9370]93{
[10693]94    osg::Geometry::drawImplementation(renderInfo);
[9093]95}
96
[10693]97void RigGeometry::buildVertexInfluenceSet()
[9093]98{
[10693]99    if (!_vertexInfluenceMap.valid())
[9093]100    {
[10693]101        osg::notify(osg::WARN) << "buildVertexInfluenceSet can't be called without VertexInfluence already set to the RigGeometry ( " << getName() << " ) " << std::endl;
[9093]102        return;
103    }
104    _vertexInfluenceSet.clear();
105    for (osgAnimation::VertexInfluenceMap::iterator it = _vertexInfluenceMap->begin();
106         it != _vertexInfluenceMap->end();
[11009]107         ++it)
[9093]108        _vertexInfluenceSet.addVertexInfluence(it->second);
109
110    _vertexInfluenceSet.buildVertex2BoneList();
111    _vertexInfluenceSet.buildUniqVertexSetToBoneSetList();
[10693]112    osg::notify(osg::NOTICE) << "uniq groups " << _vertexInfluenceSet.getUniqVertexSetToBoneSetList().size() << " for " << getName() << std::endl;
[9093]113}
114
[10693]115void RigGeometry::computeMatrixFromRootSkeleton()
[9093]116{
[10693]117    if (!_root.valid())
[9093]118    {
119        osg::notify(osg::WARN) << "Warning " << className() <<"::computeMatrixFromRootSkeleton if you have this message it means you miss to call buildTransformer(Skeleton* root), or your RigGeometry (" << getName() <<") is not attached to a Skeleton subgraph" << std::endl;
120        return;
121    }
122    osg::MatrixList mtxList = getParent(0)->getWorldMatrices(_root.get());
[11009]123    osg::Matrix notRoot = _root->getMatrix();
124    _matrixFromSkeletonToGeometry = mtxList[0] * osg::Matrix::inverse(notRoot);
[9093]125    _invMatrixFromSkeletonToGeometry = osg::Matrix::inverse(_matrixFromSkeletonToGeometry);
126    _needToComputeMatrix = false;
127}
128
[10693]129void RigGeometry::update()
[9093]130{
[10693]131    if (!getRigTransformImplementation())
[9093]132    {
[10693]133        _rigTransformImplementation = new RigTransformSoftware;
[9093]134    }
135
[11009]136    RigTransform& implementation = *getRigTransformImplementation();
137    (implementation)(*this);
[9093]138}
139
[11009]140void RigGeometry::copyFrom(osg::Geometry& from)
141{
142    bool copyToSelf = (this==&from);
143
144    osg::Geometry& target = *this;
145
146    if (!copyToSelf) target.setStateSet(from.getStateSet());
147
148    // copy over primitive sets.
149    if (!copyToSelf) target.getPrimitiveSetList() = from.getPrimitiveSetList();
150
151    if (from.getVertexArray())
152    {
153        if (!copyToSelf) target.setVertexArray(from.getVertexArray());
154    }
155
156    target.setNormalBinding(from.getNormalBinding());
157    if (from.getNormalArray())
158    {
159        if (!copyToSelf) target.setNormalArray(from.getNormalArray());
160    }
161
162    target.setColorBinding(from.getColorBinding());
163    if (from.getColorArray())
164    {
165        if (!copyToSelf) target.setColorArray(from.getColorArray());
166    }
167
168    target.setSecondaryColorBinding(from.getSecondaryColorBinding());
169    if (from.getSecondaryColorArray())
170    {
171        if (!copyToSelf) target.setSecondaryColorArray(from.getSecondaryColorArray());
172    }
173
174    target.setFogCoordBinding(from.getFogCoordBinding());
175    if (from.getFogCoordArray())
176    {
177        if (!copyToSelf) target.setFogCoordArray(from.getFogCoordArray());
178    }
179
180    for(unsigned int ti=0;ti<from.getNumTexCoordArrays();++ti)
181    {
182        if (from.getTexCoordArray(ti))
183        {
184            if (!copyToSelf) target.setTexCoordArray(ti,from.getTexCoordArray(ti));
185        }
186    }
187   
188    ArrayDataList& arrayList = from.getVertexAttribArrayList();
189    for(unsigned int vi=0;vi< arrayList.size();++vi)
190    {
191        ArrayData& arrayData = arrayList[vi];
192        if (arrayData.array.valid())
193        {
194            if (!copyToSelf) target.setVertexAttribData(vi,arrayData);
195        }
196    }
197}
198
[10693]199const VertexInfluenceSet& RigGeometry::getVertexInfluenceSet() const { return _vertexInfluenceSet;}
200
201const Skeleton* RigGeometry::getSkeleton() const { return _root.get(); }
202Skeleton* RigGeometry::getSkeleton() { return _root.get(); }
203void RigGeometry::setSkeleton(Skeleton* root) { _root = root;}
204RigTransform* RigGeometry::getRigTransformImplementation() { return _rigTransformImplementation.get(); }
205void RigGeometry::setRigTransformImplementation(RigTransform* rig) { _rigTransformImplementation = rig; }
[11009]206
207osg::Geometry* RigGeometry::getSourceGeometry() { return _geometry.get(); }
208const osg::Geometry* RigGeometry::getSourceGeometry() const { return _geometry.get(); }
209void RigGeometry::setSourceGeometry(osg::Geometry* geometry) { _geometry = geometry; }
Note: See TracBrowser for help on using the browser.