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

Revision 13041, 7.6 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/*  -*-c++-*-
2 *  Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
3 *
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.
8 *
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.
13 */
14
15#include <osgAnimation/VertexInfluence>
16#include <osgAnimation/RigGeometry>
17#include <osgAnimation/RigTransformSoftware>
18#include <sstream>
19#include <osg/GL2Extensions>
20
21using namespace osgAnimation;
22
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_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
62RigGeometry::RigGeometry()
63{
64    _supportsDisplayList = false;
65    setUseVertexBufferObjects(true);
66    setUpdateCallback(new UpdateVertex);
67    setDataVariance(osg::Object::DYNAMIC);
68    _needToComputeMatrix = true;
69    _matrixFromSkeletonToGeometry = _invMatrixFromSkeletonToGeometry = osg::Matrix::identity();
70
71    // disable the computation of boundingbox for the rig mesh
72    setComputeBoundingBoxCallback(new RigComputeBoundingBoxCallback);
73}
74
75
76RigGeometry::RigGeometry(const RigGeometry& b, const osg::CopyOp& copyop) :
77    osg::Geometry(b,copyop),
78    _geometry(b._geometry),
79    _vertexInfluenceSet(b._vertexInfluenceSet),
80    _vertexInfluenceMap(b._vertexInfluenceMap),
81    _needToComputeMatrix(b._needToComputeMatrix)
82{
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
85}
86
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
93{
94    osg::Geometry::drawImplementation(renderInfo);
95}
96
97void RigGeometry::buildVertexInfluenceSet()
98{
99    if (!_vertexInfluenceMap.valid())
100    {
101        OSG_WARN << "buildVertexInfluenceSet can't be called without VertexInfluence already set to the RigGeometry ( " << getName() << " ) " << std::endl;
102        return;
103    }
104    _vertexInfluenceSet.clear();
105    for (osgAnimation::VertexInfluenceMap::iterator it = _vertexInfluenceMap->begin();
106         it != _vertexInfluenceMap->end();
107         ++it)
108        _vertexInfluenceSet.addVertexInfluence(it->second);
109
110    _vertexInfluenceSet.buildVertex2BoneList();
111    _vertexInfluenceSet.buildUniqVertexSetToBoneSetList();
112    OSG_NOTICE << "uniq groups " << _vertexInfluenceSet.getUniqVertexSetToBoneSetList().size() << " for " << getName() << std::endl;
113}
114
115void RigGeometry::computeMatrixFromRootSkeleton()
116{
117    if (!_root.valid())
118    {
119        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());
123    osg::Matrix notRoot = _root->getMatrix();
124    _matrixFromSkeletonToGeometry = mtxList[0] * osg::Matrix::inverse(notRoot);
125    _invMatrixFromSkeletonToGeometry = osg::Matrix::inverse(_matrixFromSkeletonToGeometry);
126    _needToComputeMatrix = false;
127}
128
129void RigGeometry::update()
130{
131    if (!getRigTransformImplementation())
132    {
133        _rigTransformImplementation = new RigTransformSoftware;
134    }
135
136    RigTransform& implementation = *getRigTransformImplementation();
137    (implementation)(*this);
138}
139
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
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; }
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.