From Kristofer, "The osgSim::ScalarBar has a problem in that if the scalarbar has a color in one end that co-incides with the background color, one cannot see where the scalarbar ends. With this patch, we add a line around the perimiter of the bar to mark it.

Secondly, we thought the text-annotation missed ticks that mark out precisely where on the bar the texts relate to, so we added that as well."
This commit is contained in:
Robert Osfield 2014-01-24 16:19:38 +00:00
parent 8ae57c2797
commit 8a334e724b
2 changed files with 75 additions and 13 deletions

View File

@ -74,7 +74,7 @@ struct MyScalarPrinter: public ScalarBar::ScalarPrinter
};
#endif
osg::Node* createScalarBar()
osg::Node* createScalarBar(bool vertical)
{
#if 1
//ScalarsToColors* stc = new ScalarsToColors(0.0f,1.0f);
@ -90,9 +90,17 @@ osg::Node* createScalarBar()
ColorRange* cr = new ColorRange(0.0f,1.0f,cs);
ScalarBar* sb = new ScalarBar(20, 11, cr, "ScalarBar", ScalarBar::VERTICAL, 0.1f, new MyScalarPrinter);
ScalarBar* sb = new ScalarBar(20, 11, cr,
vertical ? "Vertical" : "Horizontal",
vertical ? ScalarBar::VERTICAL : ScalarBar::HORIZONTAL,
0.1f, new MyScalarPrinter);
sb->setScalarPrinter(new MyScalarPrinter);
if ( !vertical )
{
sb->setPosition( osg::Vec3(0.5f,-0.5f,0));
}
return sb;
#else
ScalarBar *sb = new ScalarBar;
@ -137,7 +145,8 @@ int main(int , char **)
osgViewer::Viewer viewer;
osg::Group* group = new osg::Group;
group->addChild(createScalarBar());
group->addChild(createScalarBar(true));
group->addChild(createScalarBar(false));
group->addChild(createScalarBar_HUD());
// add model to viewer.

View File

@ -1,6 +1,8 @@
#include <osgSim/ScalarBar>
#include <osgText/Text>
#include <osg/Geometry>
#include <osg/Material>
#include <osg/PolygonOffset>
#include <sstream>
using namespace osgSim;
@ -132,7 +134,7 @@ void ScalarBar::createDrawables()
}
else
{
matrix = osg::Matrix::rotate(osg::DegreesToRadians(90.0f),1.0f,0.0f,0.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
@ -175,6 +177,7 @@ void ScalarBar::createDrawables()
cs->push_back(c);
cs->push_back(c);
}
bar->setColorArray(cs.get(), osg::Array::BIND_PER_VERTEX);
// Normal
@ -184,9 +187,13 @@ void ScalarBar::createDrawables()
// The Quad strip that represents the bar
bar->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,vs->size()));
bar->getOrCreateStateSet()->setAttributeAndModes( new osg::PolygonOffset(1,1),
osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON );
addDrawable(bar.get());
#define CHARACTER_OFFSET_FACTOR (0.3f)
// 2. Then the text labels
// =======================
@ -199,7 +206,8 @@ void ScalarBar::createDrawables()
std::vector<osgText::Text*> 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;
float labely = arOffset + characterSize*0.3f;
const float labely = arOffset + characterSize*CHARACTER_OFFSET_FACTOR;
for(i=0; i<_numLabels; ++i)
{
osgText::Text* text = new osgText::Text;
@ -210,17 +218,15 @@ void ScalarBar::createDrawables()
text->setText(_sp->printScalar(_stc->getMin()+(i*labelIncr)));
text->setPosition(osg::Vec3((i*labelxIncr), labely, 0.0f)*matrix);
text->setAlignment(osgText::Text::CENTER_BASE_LINE);
text->setAxisAlignment( (_orientation==HORIZONTAL) ? osgText::Text::XY_PLANE : osgText::Text::XZ_PLANE );
text->setAlignment( (_orientation==HORIZONTAL) ? osgText::Text::CENTER_BASE_LINE : osgText::Text::LEFT_CENTER);
addDrawable(text);
texts[i] = text;
}
// 3. And finally the title
// ========================
// 3. The title
// ============
if(_title != "")
{
@ -231,13 +237,60 @@ void ScalarBar::createDrawables()
text->setCharacterSize(characterSize);
text->setText(_title);
osg::Vec3 titlePos;
if ( _orientation==HORIZONTAL )
{
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 );
}
float titleY = (_numLabels>0) ? labely + characterSize : labely;
// Position the title at the middle of the bar above any labels.
text->setPosition(osg::Vec3((_width/2.0f), titleY, 0.0f)*matrix);
text->setAlignment(osgText::Text::CENTER_BASE_LINE);
text->setAxisAlignment( (_orientation==HORIZONTAL) ? osgText::Text::XY_PLANE : osgText::Text::XZ_PLANE );
text->setPosition(titlePos*matrix);
text->setAlignment(
_orientation==HORIZONTAL ? osgText::Text::CENTER_BASE_LINE : osgText::Text::CENTER_BOTTOM);
addDrawable(text);
}
// 4. The rectangular border and sticks
// ====================================
osg::ref_ptr<osg::Vec3Array> annotVertices = new osg::Vec3Array;
//Border
annotVertices->push_back( osg::Vec3(0.0f,0.0f,0.0f) * matrix );
annotVertices->push_back( osg::Vec3(0.0f,arOffset,0.0f) * matrix );
annotVertices->push_back( osg::Vec3(0.0f,arOffset,0.0f) * matrix );
annotVertices->push_back( osg::Vec3(_width,arOffset,0.0f) * matrix );
annotVertices->push_back( osg::Vec3(_width,arOffset,0.0f) * matrix );
annotVertices->push_back( osg::Vec3(_width,0.0f,0.0f) * matrix );
annotVertices->push_back( osg::Vec3(_width,0.0f,0.0f) * matrix );
annotVertices->push_back( osg::Vec3(0.0f,0.0f,0.0f) * matrix );
//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);
annotVertices->push_back( p1 );
annotVertices->push_back( p2 );
}
osg::ref_ptr<osg::Geometry> annotationGeom = new osg::Geometry();
annotationGeom->addPrimitiveSet( new osg::DrawArrays(GL_LINES,0,annotVertices->size()) );
annotationGeom->setVertexArray( annotVertices.get() );
osg::ref_ptr<osg::Material> annotMaterial = new osg::Material;
annotMaterial->setDiffuse( osg::Material::FRONT, _textProperties._color );
annotationGeom->getOrCreateStateSet()->setAttribute( annotMaterial.get() );
addDrawable( annotationGeom.get() );
}