| [9093] | 1 | /* -*-c++-*- |
|---|
| [10693] | 2 | * Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net> |
|---|
| [9093] | 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 | #ifndef OSGANIMATION_RIGGEOMETRY_H |
|---|
| 16 | #define OSGANIMATION_RIGGEOMETRY_H |
|---|
| 17 | |
|---|
| [9094] | 18 | #include <osgAnimation/Export> |
|---|
| [9093] | 19 | #include <osgAnimation/Skeleton> |
|---|
| [10693] | 20 | #include <osgAnimation/RigTransform> |
|---|
| [9093] | 21 | #include <osg/Geometry> |
|---|
| 22 | |
|---|
| 23 | namespace osgAnimation |
|---|
| 24 | { |
|---|
| 25 | |
|---|
| [9094] | 26 | class OSGANIMATION_EXPORT RigGeometry : public osg::Geometry |
|---|
| [9093] | 27 | { |
|---|
| 28 | public: |
|---|
| 29 | |
|---|
| [9737] | 30 | RigGeometry(); |
|---|
| 31 | RigGeometry(const osg::Geometry& b); |
|---|
| 32 | RigGeometry(const RigGeometry& b, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY); |
|---|
| 33 | |
|---|
| 34 | META_Object(osgAnimation, RigGeometry); |
|---|
| 35 | |
|---|
| [10693] | 36 | void setInfluenceMap(VertexInfluenceMap* vertexInfluenceMap) { _vertexInfluenceMap = vertexInfluenceMap; } |
|---|
| 37 | const VertexInfluenceMap* getInfluenceMap() const { return _vertexInfluenceMap.get();} |
|---|
| 38 | VertexInfluenceMap* getInfluenceMap() { return _vertexInfluenceMap.get();} |
|---|
| [9737] | 39 | |
|---|
| 40 | const Skeleton* getSkeleton() const; |
|---|
| 41 | Skeleton* getSkeleton(); |
|---|
| [10693] | 42 | // will be used by the update callback to init correctly the rig mesh |
|---|
| 43 | void setSkeleton(Skeleton*); |
|---|
| [9737] | 44 | |
|---|
| 45 | void setNeedToComputeMatrix(bool state) { _needToComputeMatrix = state;} |
|---|
| 46 | bool getNeedToComputeMatrix() const { return _needToComputeMatrix;} |
|---|
| 47 | |
|---|
| [10693] | 48 | |
|---|
| 49 | // this build the internal database about vertex influence and bones |
|---|
| 50 | void buildVertexInfluenceSet(); |
|---|
| 51 | const VertexInfluenceSet& getVertexInfluenceSet() const; |
|---|
| 52 | |
|---|
| [9737] | 53 | void computeMatrixFromRootSkeleton(); |
|---|
| 54 | |
|---|
| 55 | |
|---|
| [10693] | 56 | // set implementation of rig method |
|---|
| 57 | void setRigTransformImplementation(RigTransform*); |
|---|
| 58 | RigTransform* getRigTransformImplementation(); |
|---|
| [9737] | 59 | |
|---|
| [10693] | 60 | virtual void drawImplementation(osg::RenderInfo& renderInfo) const; |
|---|
| 61 | void update(); |
|---|
| 62 | |
|---|
| 63 | const osg::Matrix& getMatrixFromSkeletonToGeometry() const; |
|---|
| 64 | const osg::Matrix& getInvMatrixFromSkeletonToGeometry() const; |
|---|
| 65 | |
|---|
| [9737] | 66 | protected: |
|---|
| 67 | |
|---|
| [10693] | 68 | osg::ref_ptr<RigTransform> _rigTransformImplementation; |
|---|
| [9737] | 69 | |
|---|
| [10693] | 70 | VertexInfluenceSet _vertexInfluenceSet; |
|---|
| 71 | osg::ref_ptr<VertexInfluenceMap> _vertexInfluenceMap; |
|---|
| [9737] | 72 | |
|---|
| 73 | osg::Matrix _matrixFromSkeletonToGeometry; |
|---|
| 74 | osg::Matrix _invMatrixFromSkeletonToGeometry; |
|---|
| 75 | osg::observer_ptr<Skeleton> _root; |
|---|
| 76 | bool _needToComputeMatrix; |
|---|
| 77 | |
|---|
| [9093] | 78 | struct FindNearestParentSkeleton : public osg::NodeVisitor |
|---|
| 79 | { |
|---|
| [10693] | 80 | osg::ref_ptr<Skeleton> _root; |
|---|
| [9093] | 81 | FindNearestParentSkeleton() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_PARENTS) {} |
|---|
| 82 | void apply(osg::Transform& node) |
|---|
| 83 | { |
|---|
| 84 | if (_root.valid()) |
|---|
| 85 | return; |
|---|
| 86 | _root = dynamic_cast<osgAnimation::Skeleton*>(&node); |
|---|
| 87 | traverse(node); |
|---|
| 88 | } |
|---|
| 89 | }; |
|---|
| 90 | |
|---|
| 91 | |
|---|
| [9370] | 92 | struct UpdateVertex : public osg::Drawable::UpdateCallback |
|---|
| [9093] | 93 | { |
|---|
| [9370] | 94 | virtual void update(osg::NodeVisitor*, osg::Drawable* drw) |
|---|
| [9093] | 95 | { |
|---|
| 96 | RigGeometry* geom = dynamic_cast<RigGeometry*>(drw); |
|---|
| 97 | if (!geom) |
|---|
| 98 | return; |
|---|
| [10344] | 99 | if (!geom->getSkeleton() && !geom->getParents().empty()) |
|---|
| [9093] | 100 | { |
|---|
| 101 | FindNearestParentSkeleton finder; |
|---|
| 102 | if (geom->getParents().size() > 1) |
|---|
| 103 | osg::notify(osg::WARN) << "A RigGeometry should not have multi parent ( " << geom->getName() << " )" << std::endl; |
|---|
| 104 | geom->getParents()[0]->accept(finder); |
|---|
| 105 | |
|---|
| 106 | if (!finder._root.valid()) |
|---|
| 107 | return; |
|---|
| [10693] | 108 | geom->buildVertexInfluenceSet(); |
|---|
| 109 | geom->setSkeleton(finder._root.get()); |
|---|
| [9093] | 110 | } |
|---|
| 111 | |
|---|
| 112 | if (!geom->getSkeleton()) |
|---|
| 113 | return; |
|---|
| 114 | |
|---|
| 115 | if (geom->getNeedToComputeMatrix()) |
|---|
| 116 | geom->computeMatrixFromRootSkeleton(); |
|---|
| 117 | |
|---|
| [10693] | 118 | geom->update(); |
|---|
| [9093] | 119 | } |
|---|
| 120 | }; |
|---|
| [9737] | 121 | }; |
|---|
| [9093] | 122 | |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | #endif |
|---|