From e6052ef4b4895a9fee350877a912503e8ae7a616 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 11 Nov 2016 17:16:40 +0000 Subject: [PATCH] Added Dragger::applyAppropriateFrontFace(StateSet*) to make it easy to handle inverted matrices by toggling the FrontFace to sure the correct face is visible. --- examples/osgvolume/osgvolume.cpp | 2 +- include/osgManipulator/Dragger | 8 +++++++- src/osgManipulator/Dragger.cpp | 22 ++++++++++++++++++++++ src/osgVolume/Locator.cpp | 2 +- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/examples/osgvolume/osgvolume.cpp b/examples/osgvolume/osgvolume.cpp index 29efa4e3f..e0df891b9 100644 --- a/examples/osgvolume/osgvolume.cpp +++ b/examples/osgvolume/osgvolume.cpp @@ -1186,7 +1186,7 @@ int main( int argc, char **argv ) dragger->setActivationModKeyMask(osgGA::GUIEventAdapter::MODKEY_SHIFT); dragger->addDraggerCallback(new DraggerVolumeTileCallback(tile.get(), tile->getLocator())); dragger->setMatrix(osg::Matrix::translate(0.5,0.5,0.5)*tile->getLocator()->getTransform()); - + dragger->applyAppropriateFrontFace(dragger->getOrCreateStateSet()); group->addChild(dragger.get()); diff --git a/include/osgManipulator/Dragger b/include/osgManipulator/Dragger index 14cc5dede..80aadaeee 100644 --- a/include/osgManipulator/Dragger +++ b/include/osgManipulator/Dragger @@ -276,7 +276,13 @@ class OSGMANIPULATOR_EXPORT Dragger : public osg::MatrixTransform virtual void setIntersectionMask(osg::Node::NodeMask intersectionMask) { _intersectionMask = intersectionMask; } osg::Node::NodeMask getIntersectionMask() const { return _intersectionMask; } - protected: + /** Return true if the axis of the Locator are inverted requiring the faces of any cubes used from rendering to be flipped to ensure the correct front/back face is used.*/ + bool inverted() const; + + /** apply the appropriate FrontFace setting to provided StateSet to ensure that the rendering of hull of the volume is the correct orientation.*/ + void applyAppropriateFrontFace(osg::StateSet* ss) const; + +protected: Dragger(); Dragger(const Dragger& rhs, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY); diff --git a/src/osgManipulator/Dragger.cpp b/src/osgManipulator/Dragger.cpp index 98193cf2d..339d6d5d9 100644 --- a/src/osgManipulator/Dragger.cpp +++ b/src/osgManipulator/Dragger.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -200,6 +201,27 @@ Dragger::~Dragger() { } +bool Dragger::inverted() const +{ + osg::Vec3d xAxis(_matrix(0,0), _matrix(1,0), _matrix(2,0)); + osg::Vec3d yAxis(_matrix(0,1), _matrix(1,1), _matrix(2,1)); + osg::Vec3d zAxis(_matrix(0,2), _matrix(1,2), _matrix(2,2)); + double volume = (xAxis^yAxis)*zAxis; + return volume<0.0; +} + +void Dragger::applyAppropriateFrontFace(osg::StateSet* ss) const +{ + osg::StateAttribute* sa = ss->getAttribute(osg::StateAttribute::FRONTFACE); + osg::FrontFace* ff = dynamic_cast(sa); + if (!ff) + { + ff = new osg::FrontFace; + ss->setAttribute(ff); + } + ff->setMode( inverted() ? osg::FrontFace::CLOCKWISE : osg::FrontFace::COUNTER_CLOCKWISE); +} + void Dragger::setHandleEvents(bool flag) { if (_handleEvents == flag) return; diff --git a/src/osgVolume/Locator.cpp b/src/osgVolume/Locator.cpp index 2de869d16..982fbad80 100644 --- a/src/osgVolume/Locator.cpp +++ b/src/osgVolume/Locator.cpp @@ -229,7 +229,7 @@ bool Locator::inverted() const osg::Vec3d xAxis(_transform(0,0), _transform(1,0), _transform(2,0)); osg::Vec3d yAxis(_transform(0,1), _transform(1,1), _transform(2,1)); osg::Vec3d zAxis(_transform(0,2), _transform(1,2), _transform(2,2)); - float volume = (xAxis^yAxis)*zAxis; + double volume = (xAxis^yAxis)*zAxis; return volume<0.0; }