| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <osgAnimation/VertexInfluence> |
|---|
| 16 | #include <osgAnimation/RigGeometry> |
|---|
| 17 | #include <osgAnimation/RigTransformSoftware> |
|---|
| 18 | #include <sstream> |
|---|
| 19 | #include <osg/GL2Extensions> |
|---|
| 20 | |
|---|
| 21 | using namespace osgAnimation; |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | class RigComputeBoundingBoxCallback : public osg::Drawable::ComputeBoundingBoxCallback |
|---|
| 25 | { |
|---|
| 26 | public: |
|---|
| 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 | |
|---|
| 34 | if (rig.getInitialBound().valid()) |
|---|
| 35 | return rig.getInitialBound(); |
|---|
| 36 | |
|---|
| 37 | if (_computed) |
|---|
| 38 | return _boundingBox; |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 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 | |
|---|
| 54 | return _boundingBox; |
|---|
| 55 | } |
|---|
| 56 | protected: |
|---|
| 57 | mutable bool _computed; |
|---|
| 58 | double _factor; |
|---|
| 59 | mutable osg::BoundingBox _boundingBox; |
|---|
| 60 | }; |
|---|
| 61 | |
|---|
| 62 | RigGeometry::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 | |
|---|
| 72 | setComputeBoundingBoxCallback(new RigComputeBoundingBoxCallback); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | RigGeometry::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 | |
|---|
| 84 | |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | const osg::Matrix& RigGeometry::getMatrixFromSkeletonToGeometry() const { return _matrixFromSkeletonToGeometry; } |
|---|
| 89 | const osg::Matrix& RigGeometry::getInvMatrixFromSkeletonToGeometry() const { return _invMatrixFromSkeletonToGeometry;} |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | void RigGeometry::drawImplementation(osg::RenderInfo& renderInfo) const |
|---|
| 93 | { |
|---|
| 94 | osg::Geometry::drawImplementation(renderInfo); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | void 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 | |
|---|
| 115 | void 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 | |
|---|
| 129 | void RigGeometry::update() |
|---|
| 130 | { |
|---|
| 131 | if (!getRigTransformImplementation()) |
|---|
| 132 | { |
|---|
| 133 | _rigTransformImplementation = new RigTransformSoftware; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | RigTransform& implementation = *getRigTransformImplementation(); |
|---|
| 137 | (implementation)(*this); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | void 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 | |
|---|
| 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 | |
|---|
| 199 | const VertexInfluenceSet& RigGeometry::getVertexInfluenceSet() const { return _vertexInfluenceSet;} |
|---|
| 200 | |
|---|
| 201 | const Skeleton* RigGeometry::getSkeleton() const { return _root.get(); } |
|---|
| 202 | Skeleton* RigGeometry::getSkeleton() { return _root.get(); } |
|---|
| 203 | void RigGeometry::setSkeleton(Skeleton* root) { _root = root;} |
|---|
| 204 | RigTransform* RigGeometry::getRigTransformImplementation() { return _rigTransformImplementation.get(); } |
|---|
| 205 | void RigGeometry::setRigTransformImplementation(RigTransform* rig) { _rigTransformImplementation = rig; } |
|---|
| 206 | |
|---|
| 207 | osg::Geometry* RigGeometry::getSourceGeometry() { return _geometry.get(); } |
|---|
| 208 | const osg::Geometry* RigGeometry::getSourceGeometry() const { return _geometry.get(); } |
|---|
| 209 | void RigGeometry::setSourceGeometry(osg::Geometry* geometry) { _geometry = geometry; } |
|---|