From f7e6f0092c4b1df2495605019a092dc8d113cc6f Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 28 Jan 2014 11:01:28 +0000 Subject: [PATCH] From Kristofer Tingdahl, "the vertical bar is upside down, and hence not as the documentation says it should be. This is corrected with this patch" From Robert Osfield, changed the example so that the vertical and horizon scalar bars are rotated to the XZ plane so you can see them with the default viewer's camera orientation. Tweaked the positioning of title text of vertic scalar bar to avoid overlap of text. --- examples/osgscalarbar/osgscalarbar.cpp | 12 +++++++++--- src/osgSim/ScalarBar.cpp | 19 ++++++++++--------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/examples/osgscalarbar/osgscalarbar.cpp b/examples/osgscalarbar/osgscalarbar.cpp index 618c93286..9f3e6ba96 100644 --- a/examples/osgscalarbar/osgscalarbar.cpp +++ b/examples/osgscalarbar/osgscalarbar.cpp @@ -98,7 +98,7 @@ osg::Node* createScalarBar(bool vertical) if ( !vertical ) { - sb->setPosition( osg::Vec3(0.5f,-0.5f,0)); + sb->setPosition( osg::Vec3(0.5f,0.5f,0)); } return sb; @@ -145,10 +145,16 @@ int main(int , char **) osgViewer::Viewer viewer; osg::Group* group = new osg::Group; - group->addChild(createScalarBar(true)); - group->addChild(createScalarBar(false)); + group->addChild(createScalarBar_HUD()); + // rotate the scalar from XY plane to XZ so we see them viewing it with the default camera manipulators that look along the Y axis, with Z up. + osg::MatrixTransform* transform = new osg::MatrixTransform; + group->addChild(transform); + transform->setMatrix(osg::Matrix::rotate(osg::inDegrees(90.0),1.0,0.0,0.0)); + transform->addChild(createScalarBar(true)); + transform->addChild(createScalarBar(false)); + // add model to viewer. viewer.setSceneData( group ); diff --git a/src/osgSim/ScalarBar.cpp b/src/osgSim/ScalarBar.cpp index e2d3c9019..b8afc99ee 100644 --- a/src/osgSim/ScalarBar.cpp +++ b/src/osgSim/ScalarBar.cpp @@ -134,7 +134,7 @@ void ScalarBar::createDrawables() } else { - matrix = osg::Matrix::rotate(osg::DegreesToRadians(90.0f),0.0f,0.0f,-1.0f) * osg::Matrix::translate(_position); + matrix = osg::Matrix::rotate(osg::DegreesToRadians(90.0f),0.0f,0.0f,1.0f) * osg::Matrix::translate(_position); } // 1. First the bar @@ -206,7 +206,10 @@ void ScalarBar::createDrawables() std::vector texts(_numLabels); // We'll need to collect pointers to these for later float labelIncr = (_numLabels>0) ? (_stc->getMax()-_stc->getMin())/(_numLabels-1) : 0.0f; float labelxIncr = (_numLabels>0) ? (_width)/(_numLabels-1) : 0.0f; - const float labely = arOffset + characterSize*CHARACTER_OFFSET_FACTOR; + const float labelStickStartY = _orientation==HORIZONTAL ? arOffset : 0; + const float labelY = labelStickStartY + + (_orientation==HORIZONTAL ? characterSize : -characterSize) * CHARACTER_OFFSET_FACTOR; + for(i=0; i<_numLabels; ++i) { @@ -217,7 +220,7 @@ void ScalarBar::createDrawables() text->setCharacterSize(characterSize); text->setText(_sp->printScalar(_stc->getMin()+(i*labelIncr))); - text->setPosition(osg::Vec3((i*labelxIncr), labely, 0.0f)*matrix); + text->setPosition(osg::Vec3((i*labelxIncr), labelY, 0.0f)*matrix); text->setAlignment( (_orientation==HORIZONTAL) ? osgText::Text::CENTER_BASE_LINE : osgText::Text::LEFT_CENTER); addDrawable(text); @@ -240,16 +243,14 @@ void ScalarBar::createDrawables() osg::Vec3 titlePos; if ( _orientation==HORIZONTAL ) { - const float titleY = (_numLabels>0) ? labely + characterSize : labely; + const float titleY = (_numLabels>0) ? labelY + characterSize : labelY; titlePos = osg::Vec3((_width/2.0f), titleY, 0.0f); } else { - titlePos = osg::Vec3( 0, arOffset/2, 0 ); + titlePos = osg::Vec3(_width+characterSize*0.5, arOffset*0.5, 0 ); } - float titleY = (_numLabels>0) ? labely + characterSize : labely; - // Position the title at the middle of the bar above any labels. text->setPosition(titlePos*matrix); text->setAlignment( @@ -279,8 +280,8 @@ void ScalarBar::createDrawables() //Sticks for(i=0; i<_numLabels; ++i) { - const osg::Vec3 p1(osg::Vec3((i*labelxIncr), arOffset, 0.0f)*matrix); - const osg::Vec3 p2(osg::Vec3((i*labelxIncr), labely, 0.0f)*matrix); + const osg::Vec3 p1(osg::Vec3((i*labelxIncr), labelStickStartY, 0.0f)*matrix); + const osg::Vec3 p2(osg::Vec3((i*labelxIncr), labelY, 0.0f)*matrix); annotVertices->push_back( p1 ); annotVertices->push_back( p2 ); }