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