OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RigGeometry.h
Go to the documentation of this file.
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  // The idea is to compute a bounding box with a factor x of the first step we compute the bounding box
29  {
30  public:
31  RigComputeBoundingBoxCallback(double factor = 2.0): _computed(false), _factor(factor) {}
32 
34  osg::Drawable::ComputeBoundingBoxCallback(rhs, copyop),
35  _computed(false),
36  _factor(rhs._factor) {}
37 
39 
40 
41  void reset() { _computed = false; }
42  virtual osg::BoundingBox computeBound(const osg::Drawable& drawable) const;
43  protected:
44  mutable bool _computed;
45  double _factor;
47  };
48 
49 
51  {
52  public:
53 
54  RigGeometry();
55 // RigGeometry(const osg::Geometry& b);
57 
59 
60  void setInfluenceMap(VertexInfluenceMap* vertexInfluenceMap) { _vertexInfluenceMap = vertexInfluenceMap; }
61  const VertexInfluenceMap* getInfluenceMap() const { return _vertexInfluenceMap.get();}
62  VertexInfluenceMap* getInfluenceMap() { return _vertexInfluenceMap.get();}
63 
64  const Skeleton* getSkeleton() const;
65  Skeleton* getSkeleton();
66  // will be used by the update callback to init correctly the rig mesh
67  void setSkeleton(Skeleton*);
68 
69  void setNeedToComputeMatrix(bool state) { _needToComputeMatrix = state;}
70  bool getNeedToComputeMatrix() const { return _needToComputeMatrix;}
71 
72 
73  // this build the internal database about vertex influence and bones
74  void buildVertexInfluenceSet();
75  const VertexInfluenceSet& getVertexInfluenceSet() const;
76 
77  void computeMatrixFromRootSkeleton();
78 
79 
80  // set implementation of rig method
81  void setRigTransformImplementation(RigTransform*);
82  RigTransform* getRigTransformImplementation();
83 
84  virtual void drawImplementation(osg::RenderInfo& renderInfo) const;
85  void update();
86 
87  const osg::Matrix& getMatrixFromSkeletonToGeometry() const;
88  const osg::Matrix& getInvMatrixFromSkeletonToGeometry() const;
89 
90  osg::Geometry* getSourceGeometry();
91  const osg::Geometry* getSourceGeometry() const;
92  void setSourceGeometry(osg::Geometry* geometry);
93 
94  void copyFrom(osg::Geometry& from);
95 
97  {
99  FindNearestParentSkeleton() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_PARENTS) {}
100  void apply(osg::Transform& node)
101  {
102  if (_root.valid())
103  return;
104  _root = dynamic_cast<osgAnimation::Skeleton*>(&node);
105  traverse(node);
106  }
107  };
108 
109  protected:
110 
113 
116 
121 
122 
123  };
124 
125 
127  {
129 
131 
133 
134  virtual void update(osg::NodeVisitor*, osg::Drawable* drw) {
135  RigGeometry* geom = dynamic_cast<RigGeometry*>(drw);
136  if(!geom)
137  return;
138  if(!geom->getSkeleton() && !geom->getParents().empty())
139  {
141  if(geom->getParents().size() > 1)
142  osg::notify(osg::WARN) << "A RigGeometry should not have multi parent ( " << geom->getName() << " )" << std::endl;
143  geom->getParents()[0]->accept(finder);
144 
145  if(!finder._root.valid())
146  {
147  osg::notify(osg::WARN) << "A RigGeometry did not find a parent skeleton for RigGeometry ( " << geom->getName() << " )" << std::endl;
148  return;
149  }
150  geom->buildVertexInfluenceSet();
151  geom->setSkeleton(finder._root.get());
152  }
153 
154  if(!geom->getSkeleton())
155  return;
156 
157  if(geom->getNeedToComputeMatrix())
159 
160  geom->update();
161  }
162  };
163 }
164 
165 #endif
RigComputeBoundingBoxCallback(const RigComputeBoundingBoxCallback &rhs, const osg::CopyOp &copyop)
Definition: RigGeometry.h:33
const Skeleton * getSkeleton() const
const ParentList & getParents() const
Definition: Node.h:180
#define OSGANIMATION_EXPORT
Definition: Export.h:40
osg::Matrix _invMatrixFromSkeletonToGeometry
Definition: RigGeometry.h:118
osg::observer_ptr< Skeleton > _root
Definition: RigGeometry.h:119
UpdateRigGeometry(const UpdateRigGeometry &, const osg::CopyOp &)
Definition: RigGeometry.h:130
bool valid() const
Definition: ref_ptr.h:95
const VertexInfluenceMap * getInfluenceMap() const
Definition: RigGeometry.h:61
void setInfluenceMap(VertexInfluenceMap *vertexInfluenceMap)
Definition: RigGeometry.h:60
osg::ref_ptr< RigTransform > _rigTransformImplementation
Definition: RigGeometry.h:112
RigComputeBoundingBoxCallback(double factor=2.0)
Definition: RigGeometry.h:31
META_Object(osgAnimation, UpdateRigGeometry)
#define META_Object(library, name)
Definition: Object.h:42
VertexInfluenceSet _vertexInfluenceSet
Definition: RigGeometry.h:114
const std::string & getName() const
Definition: Object.h:144
osg::Matrix _matrixFromSkeletonToGeometry
Definition: RigGeometry.h:117
void setSkeleton(Skeleton *)
void setNeedToComputeMatrix(bool state)
Definition: RigGeometry.h:69
virtual void update(osg::NodeVisitor *, osg::Drawable *drw)
Definition: RigGeometry.h:134
Definition: AlphaFunc.h:19
OSG_EXPORT std::ostream & notify(const NotifySeverity severity)
VertexInfluenceMap * getInfluenceMap()
Definition: RigGeometry.h:62
bool getNeedToComputeMatrix() const
Definition: RigGeometry.h:70
osg::ref_ptr< VertexInfluenceMap > _vertexInfluenceMap
Definition: RigGeometry.h:115
osg::ref_ptr< osg::Geometry > _geometry
Definition: RigGeometry.h:111