From 111e3eb6db5580bc110f83bdfd9852b6244872a6 Mon Sep 17 00:00:00 2001 From: scrawl Date: Tue, 21 Apr 2015 18:29:28 +0200 Subject: [PATCH] Slightly improve bounding box for skinned meshes --- components/nifosg/nifloader.cpp | 36 ++++++++++++------------------- components/nifosg/riggeometry.cpp | 6 +----- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/components/nifosg/nifloader.cpp b/components/nifosg/nifloader.cpp index f0bb538f2..eb78a0632 100644 --- a/components/nifosg/nifloader.cpp +++ b/components/nifosg/nifloader.cpp @@ -1196,28 +1196,24 @@ namespace NifOsg } rig->setInfluenceMap(map); - rig->setComputeBoundingBoxCallback(new StaticBoundingBoxCallback(geometry->getBound())); + // Compute the bounding box + osg::BoundingBox boundingBox; + + osg::Matrix worldTrans = getWorldTransform(triShape); + for(size_t i = 0;i < bones.length();i++) + { + osg::BoundingSphere boneSphere (data->bones[i].boundSphereCenter, data->bones[i].boundSphereRadius); + osg::Matrix boneWorldTrans(getWorldTransform(bones[i].getPtr())); + osg::Matrix mat = boneWorldTrans * worldTrans.inverse(worldTrans); + SceneUtil::transformBoundingSphere(mat, boneSphere); + boundingBox.expandBy(boneSphere); + } + rig->setComputeBoundingBoxCallback(new StaticBoundingBoxCallback(boundingBox)); geode->addDrawable(rig); - // World bounding box callback & node - osg::ref_ptr bbcb = new BoundingBoxCallback; - bbcb->mDrawable = rig; - - osg::ref_ptr geode2 = new osg::Geode; - geode2->addDrawable( new osg::ShapeDrawable(new osg::Box) ); - - osg::ref_ptr boundingBoxNode = new osg::MatrixTransform; - boundingBoxNode->addChild( geode2.get() ); - boundingBoxNode->addUpdateCallback( bbcb.get() ); - boundingBoxNode->getOrCreateStateSet()->setAttributeAndModes( - new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE) ); - boundingBoxNode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); - - // Add a copy, we will alternate between the two copies every other frame using the FrameSwitch // This is so we can set the DataVariance as STATIC, giving a huge performance boost - /* rig->setDataVariance(osg::Object::STATIC); osg::Geode* geode2 = static_cast(osg::clone(geode.get(), osg::CopyOp::DEEP_COPY_NODES| osg::CopyOp::DEEP_COPY_DRAWABLES)); @@ -1226,11 +1222,7 @@ namespace NifOsg frameswitch->addChild(geode); frameswitch->addChild(geode2); - trans->addChild(frameswitch); - */ - - parentNode->addChild(geode); - parentNode->addChild(boundingBoxNode); + parentNode->addChild(frameswitch); } diff --git a/components/nifosg/riggeometry.cpp b/components/nifosg/riggeometry.cpp index 8283caa23..37fee1f51 100644 --- a/components/nifosg/riggeometry.cpp +++ b/components/nifosg/riggeometry.cpp @@ -46,10 +46,8 @@ RigGeometry::RigGeometry() RigGeometry::RigGeometry(const RigGeometry ©, const osg::CopyOp ©op) : osg::Geometry(copy, copyop) , mInfluenceMap(copy.mInfluenceMap) - , mSourceGeometry(copy.mSourceGeometry) { - //setVertexArray(dynamic_cast(from.getVertexArray()->clone(osg::CopyOp::DEEP_COPY_ALL))); - //setNormalArray(dynamic_cast(from.getNormalArray()->clone(osg::CopyOp::DEEP_COPY_ALL)), osg::Array::BIND_PER_VERTEX); + setSourceGeometry(copy.mSourceGeometry); } void RigGeometry::setSourceGeometry(osg::ref_ptr sourceGeometry) @@ -110,8 +108,6 @@ bool RigGeometry::initFromParentSkeleton(osg::NodeVisitor* nv) return false; } - // geometryToSkel = nv->getNodePath()... - if (!mInfluenceMap) { std::cerr << "No InfluenceMap set on RigGeometry" << std::endl;