Permalink
Browse files

From Andreas Henne, "in my application I use the TrackballDragger, th…

…e ScaleAxisDragger and the TranslateAxisDragger. Unfortunately these draggers are very thin and they do not provide methods to change their visual appearance. Another problem that I noticed is that lighting on the cones and boxes of the TranslateAxisDragger and ScaleAxisDragger is incorrect when the draggers are scaled due to not normalized normals. This small patch adresses these problems, providing methods to make the draggers thicker. I have attached a zip archive containing the corresponding files and also a modified osgManipulator example that makes use of the modifications. I don't want to retain any copyright."
  • Loading branch information...
1 parent 2e2cb59 commit b74be79c5fd2a11b57e2483a33a032fc7f08db41 @osg-training-guest osg-training-guest committed Oct 18, 2013
@@ -106,6 +106,8 @@ osgManipulator::Dragger* createDragger(const std::string& name)
{
osgManipulator::TrackballDragger* d = new osgManipulator::TrackballDragger();
d->setupDefaultGeometry();
+ d->setAxisLineWidth(5.0f);
+ d->setPickCylinderHeight(0.1f);
dragger = d;
}
else if ("Translate1DDragger" == name)
@@ -124,6 +126,9 @@ osgManipulator::Dragger* createDragger(const std::string& name)
{
osgManipulator::TranslateAxisDragger* d = new osgManipulator::TranslateAxisDragger();
d->setupDefaultGeometry();
+ d->setAxisLineWidth(5.0f);
+ d->setPickCylinderRadius(0.05f);
+ d->setConeHeight(0.2f);
dragger = d;
}
else if ("TranslatePlaneDragger" == name)
@@ -16,6 +16,9 @@
#define OSGMANIPULATOR_SCALEAXISDRAGGER 1
#include <osgManipulator/Scale1DDragger>
+#include <osg/ShapeDrawable>
+#include <osg/Geometry>
+#include <osg/LineWidth>
namespace osgManipulator {
@@ -33,13 +36,32 @@ class OSGMANIPULATOR_EXPORT ScaleAxisDragger : public CompositeDragger
/** Setup default geometry for dragger. */
void setupDefaultGeometry();
+ /** Sets the width of the axis lines in pixels. */
+ void setAxisLineWidth(float linePixelWidth);
+
+ /** Retrieves the width of the axis lines in pixels. */
+ float getAxisLineWidth() const { return _axisLineWidth; }
+
+ /** Sets the size of the boxes. */
+ void setBoxSize(float size);
+
+ /** Retrieves the size of the boxes. */
+ float getBoxSize() const { return _boxSize; }
+
protected:
virtual ~ScaleAxisDragger();
osg::ref_ptr< Scale1DDragger > _xDragger;
osg::ref_ptr< Scale1DDragger > _yDragger;
osg::ref_ptr< Scale1DDragger > _zDragger;
+
+ float _boxSize;
+ float _axisLineWidth;
+
+ osg::ref_ptr<osg::Geode> _lineGeode;
+ osg::ref_ptr<osg::LineWidth> _lineWidth;
+ osg::ref_ptr<osg::Box> _box;
};
@@ -17,6 +17,9 @@
#include <osgManipulator/RotateCylinderDragger>
#include <osgManipulator/RotateSphereDragger>
+#include <osg/ShapeDrawable>
+#include <osg/Geometry>
+#include <osg/LineWidth>
namespace osgManipulator {
@@ -34,6 +37,18 @@ class OSGMANIPULATOR_EXPORT TrackballDragger : public CompositeDragger
/** Setup default geometry for dragger. */
void setupDefaultGeometry();
+ /** Sets the width of the axis lines in pixels. */
+ void setAxisLineWidth(float linePixelWidth);
+
+ /** Retrieves the width of the axis lines in pixels. */
+ float getAxisLineWidth() const { return _axisLineWidth; }
+
+ /** Sets the height of the cylinders representing the axis lines for picking. */
+ void setPickCylinderHeight(float pickCylinderHeight);
+
+ /** Retrieves the height of the cylinders representing the axis lines for picking. */
+ float getPickCylinderHeight() const { return _pickCylinderHeight; }
+
protected:
virtual ~TrackballDragger();
@@ -42,6 +57,13 @@ class OSGMANIPULATOR_EXPORT TrackballDragger : public CompositeDragger
osg::ref_ptr<RotateCylinderDragger> _yDragger;
osg::ref_ptr<RotateCylinderDragger> _zDragger;
osg::ref_ptr<RotateSphereDragger> _xyzDragger;
+
+ float _axisLineWidth;
+ float _pickCylinderHeight;
+
+ osg::ref_ptr<osg::Geode> _geode;
+ osg::ref_ptr<osg::Cylinder> _cylinder;
+ osg::ref_ptr<osg::LineWidth> _lineWidth;
};
@@ -16,6 +16,9 @@
#define OSGMANIPULATOR_TRANSLATEAXISDRAGGER 1
#include <osgManipulator/Translate1DDragger>
+#include <osg/ShapeDrawable>
+#include <osg/Geometry>
+#include <osg/LineWidth>
namespace osgManipulator {
@@ -33,13 +36,40 @@ class OSGMANIPULATOR_EXPORT TranslateAxisDragger : public CompositeDragger
/** Setup default geometry for dragger. */
void setupDefaultGeometry();
+ /** Sets the width of the axis lines in pixels. */
+ void setAxisLineWidth(float linePixelWidth);
+
+ /** Retrieves the width of the axis lines in pixels. */
+ float getAxisLineWidth() const { return _axisLineWidth; }
+
+ /** Sets the radius of the cylinders representing the axis lines for picking. */
+ void setPickCylinderRadius(float pickCylinderRadius);
+
+ /** Retrieves the radius of the cylinders representing the axis lines for picking. */
+ float getPickCylinderRadius() const { return _pickCylinderRadius; }
+
+ /** Sets the height of the cones. */
+ void setConeHeight(float radius);
+
+ /** Retrieves the height of the cones. */
+ float getConeHeight() const { return _coneHeight; }
+
protected:
virtual ~TranslateAxisDragger();
osg::ref_ptr< Translate1DDragger > _xDragger;
osg::ref_ptr< Translate1DDragger > _yDragger;
osg::ref_ptr< Translate1DDragger > _zDragger;
+
+ float _coneHeight;
+ float _axisLineWidth;
+ float _pickCylinderRadius;
+
+ osg::ref_ptr<osg::Geode> _lineGeode;
+ osg::ref_ptr<osg::Cylinder> _cylinder;
+ osg::ref_ptr<osg::LineWidth> _lineWidth;
+ osg::ref_ptr<osg::Cone> _cone;
};
@@ -35,6 +35,9 @@ ScaleAxisDragger::ScaleAxisDragger()
addChild(_zDragger.get());
addDragger(_zDragger.get());
+ _axisLineWidth = 2.0f;
+ _boxSize = 0.05f;
+
setParentDragger(getParentDragger());
}
@@ -45,7 +48,7 @@ ScaleAxisDragger::~ScaleAxisDragger()
void ScaleAxisDragger::setupDefaultGeometry()
{
// Create a line.
- osg::Geode* lineGeode = new osg::Geode;
+ _lineGeode = new osg::Geode;
{
osg::Geometry* geometry = new osg::Geometry();
@@ -56,26 +59,32 @@ void ScaleAxisDragger::setupDefaultGeometry()
geometry->setVertexArray(vertices);
geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,2));
- lineGeode->addDrawable(geometry);
+ _lineGeode->addDrawable(geometry);
}
// Turn of lighting for line and set line width.
{
- osg::LineWidth* linewidth = new osg::LineWidth();
- linewidth->setWidth(2.0f);
- lineGeode->getOrCreateStateSet()->setAttributeAndModes(linewidth, osg::StateAttribute::ON);
- lineGeode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
+ _lineWidth = new osg::LineWidth();
+ _lineWidth->setWidth(_axisLineWidth);
+ _lineGeode->getOrCreateStateSet()->setAttributeAndModes(_lineWidth, osg::StateAttribute::ON);
+ _lineGeode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
}
// Add line to all the individual 1D draggers.
- _xDragger->addChild(lineGeode);
- _yDragger->addChild(lineGeode);
- _zDragger->addChild(lineGeode);
+ _xDragger->addChild(_lineGeode);
+ _yDragger->addChild(_lineGeode);
+ _zDragger->addChild(_lineGeode);
osg::Geode* geode = new osg::Geode;
// Create a box.
- geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(1.0f,0.0f,0.0f), 0.05)));
+ _box = new osg::Box(osg::Vec3(1.0f,0.0f,0.0f), _boxSize);
+ geode->addDrawable(new osg::ShapeDrawable(_box));
+
+ // This ensures correct lighting for scaled draggers.
+#if !defined(OSG_GLES2_AVAILABLE)
+ geode->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
+#endif
// Add geode to all 1D draggers.
_xDragger->addChild(geode);
@@ -99,3 +108,17 @@ void ScaleAxisDragger::setupDefaultGeometry()
_yDragger->setColor(osg::Vec4(0.0f,1.0f,0.0f,1.0f));
_zDragger->setColor(osg::Vec4(0.0f,0.0f,1.0f,1.0f));
}
+
+void ScaleAxisDragger::setAxisLineWidth(float linePixelWidth)
+{
+ _axisLineWidth = linePixelWidth;
+ if (_lineWidth.valid())
+ _lineWidth->setWidth(linePixelWidth);
+}
+
+void ScaleAxisDragger::setBoxSize(float size)
+{
+ _boxSize = size;
+ if (_box.valid())
+ _box->setHalfLengths(osg::Vec3(size * 0.5f, size * 0.5f, size * 0.5f));
+}
@@ -28,26 +28,26 @@ using namespace osgManipulator;
namespace
{
-osg::Geometry* createCircleGeometry(float radius, unsigned int numSegments)
-{
- const float angleDelta = 2.0f*osg::PI/(float)numSegments;
- const float r = radius;
- float angle = 0.0f;
- osg::Vec3Array* vertexArray = new osg::Vec3Array(numSegments);
- osg::Vec3Array* normalArray = new osg::Vec3Array(numSegments);
- for(unsigned int i = 0; i < numSegments; ++i,angle+=angleDelta)
+ osg::Geometry* createCircleGeometry(float radius, unsigned int numSegments)
{
- float c = cosf(angle);
- float s = sinf(angle);
- (*vertexArray)[i].set(c*r,s*r,0.0f);
- (*normalArray)[i].set(c,s,0.0f);
+ const float angleDelta = 2.0f*osg::PI/(float)numSegments;
+ const float r = radius;
+ float angle = 0.0f;
+ osg::Vec3Array* vertexArray = new osg::Vec3Array(numSegments);
+ osg::Vec3Array* normalArray = new osg::Vec3Array(numSegments);
+ for(unsigned int i = 0; i < numSegments; ++i,angle+=angleDelta)
+ {
+ float c = cosf(angle);
+ float s = sinf(angle);
+ (*vertexArray)[i].set(c*r,s*r,0.0f);
+ (*normalArray)[i].set(c,s,0.0f);
+ }
+ osg::Geometry* geometry = new osg::Geometry();
+ geometry->setVertexArray(vertexArray);
+ geometry->setNormalArray(normalArray, osg::Array::BIND_PER_VERTEX);
+ geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,0,vertexArray->size()));
+ return geometry;
}
- osg::Geometry* geometry = new osg::Geometry();
- geometry->setVertexArray(vertexArray);
- geometry->setNormalArray(normalArray, osg::Array::BIND_PER_VERTEX);
- geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,0,vertexArray->size()));
- return geometry;
-}
}
@@ -102,6 +102,9 @@ TrackballDragger::TrackballDragger(bool useAutoTransform)
addDragger(_xyzDragger.get());
}
+ _axisLineWidth = 2.0f;
+ _pickCylinderHeight = 0.15f;
+
setParentDragger(getParentDragger());
}
@@ -111,38 +114,39 @@ TrackballDragger::~TrackballDragger()
void TrackballDragger::setupDefaultGeometry()
{
- osg::Geode* geode = new osg::Geode;
+ _geode = new osg::Geode;
{
osg::TessellationHints* hints = new osg::TessellationHints;
hints->setCreateTop(false);
hints->setCreateBottom(false);
hints->setCreateBackFace(false);
- osg::Cylinder* cylinder = new osg::Cylinder;
- cylinder->setHeight(0.15f);
- osg::ShapeDrawable* cylinderDrawable = new osg::ShapeDrawable(cylinder,hints);
- geode->addDrawable(cylinderDrawable);
+ _cylinder = new osg::Cylinder;
+ _cylinder->setHeight(_pickCylinderHeight);
+ osg::ShapeDrawable* cylinderDrawable = new osg::ShapeDrawable(_cylinder, hints);
+ _geode->addDrawable(cylinderDrawable);
setDrawableToAlwaysCull(*cylinderDrawable);
- geode->addDrawable(createCircleGeometry(1.0f, 100));
+ _geode->addDrawable(createCircleGeometry(1.0f, 100));
}
// Draw in line mode.
{
osg::PolygonMode* polymode = new osg::PolygonMode;
polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
- geode->getOrCreateStateSet()->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
- geode->getOrCreateStateSet()->setAttributeAndModes(new osg::LineWidth(2.0f),osg::StateAttribute::ON);
+ _geode->getOrCreateStateSet()->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
+ _lineWidth = new osg::LineWidth(_axisLineWidth);
+ _geode->getOrCreateStateSet()->setAttributeAndModes(_lineWidth, osg::StateAttribute::ON);
- #if !defined(OSG_GLES2_AVAILABLE)
- geode->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
- #endif
+#if !defined(OSG_GLES2_AVAILABLE)
+ _geode->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
+#endif
}
// Add line to all the individual 1D draggers.
- _xDragger->addChild(geode);
- _yDragger->addChild(geode);
- _zDragger->addChild(geode);
+ _xDragger->addChild(_geode);
+ _yDragger->addChild(_geode);
+ _zDragger->addChild(_geode);
// Rotate X-axis dragger appropriately.
@@ -172,3 +176,16 @@ void TrackballDragger::setupDefaultGeometry()
_xyzDragger->addChild(sphereGeode);
}
}
+
+void TrackballDragger::setAxisLineWidth(float linePixelWidth)
+{
+ _axisLineWidth = linePixelWidth;
+ if (_lineWidth.valid())
+ _lineWidth->setWidth(linePixelWidth);
+}
+void TrackballDragger::setPickCylinderHeight(float pickCylinderHeight)
+{
+ _pickCylinderHeight = pickCylinderHeight;
+ if (_cylinder.valid())
+ _cylinder->setHeight(pickCylinderHeight);
+}
Oops, something went wrong.

0 comments on commit b74be79

Please sign in to comment.