Added TextSettings struct to manage values used to set up the text. with the addition of following command line parameters:

--outline // enable outlne
   --shadow // enable shadow
   --offset ratio // set the backdrop offset
   --text-color r g b a // set the text body color
   --bd-color r g b a // set the shadow/outline color
   --bg-color r g b a // window background color
   -o filename // write create subgraph to disk using specified filename
This commit is contained in:
Robert Osfield 2017-08-29 17:19:26 +01:00
parent 37487b0c0b
commit 5566a025b5
2 changed files with 75 additions and 8 deletions

View File

@ -4,6 +4,7 @@
#include <osg/ArgumentParser> #include <osg/ArgumentParser>
#include <osg/Geode> #include <osg/Geode>
#include <osg/MatrixTransform> #include <osg/MatrixTransform>
#include <osgDB/WriteFile>
#include <osgViewer/Viewer> #include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers> #include <osgViewer/ViewerEventHandlers>
#include <osgGA/StateSetManipulator> #include <osgGA/StateSetManipulator>
@ -57,30 +58,74 @@ osg::Camera* createOrthoCamera(double width, double height)
return camera; return camera;
} }
osgText::Text* createLabel(const std::string& l, const std::string& fontfile, unsigned int size) struct TextSettings
{
TextSettings():
fontFilename("fonts/arial.ttf"),
textColor(1.0f, 1.0f, 1.0f, 1.0f),
backdropType(osgText::Text::NONE),
backdropOffset(0.04f, 0.04f),
backdropColor(0.0f, 0.0f, 0.0f, 1.0f)
{
}
void read(osg::ArgumentParser& arguments)
{
if (arguments.read("--outline")) backdropType = osgText::Text::OUTLINE;
if (arguments.read("--shadow")) backdropType = osgText::Text::DROP_SHADOW_BOTTOM_RIGHT;
float offset;
if (arguments.read("--offset", offset)) backdropOffset.set(offset, offset);
if (arguments.read("--text-color", textColor.r(), textColor.g(), textColor.b(), textColor.a())) {}
if (arguments.read("--bd-color", backdropColor.r(), backdropColor.g(), backdropColor.b(), backdropColor.a())) {}
}
void setText(osgText::Text& text)
{
OSG_NOTICE<<"Settings::setText()"<<std::endl;
text.setFont(fontFilename);
text.setColor(textColor);
text.setBackdropType(backdropType);
text.setBackdropOffset(backdropOffset.x(), backdropOffset.y());
text.setBackdropColor(backdropColor);
}
std::string fontFilename;
osg::Vec4 textColor;
osgText::Text::BackdropType backdropType;
osg::Vec2 backdropOffset;
osg::Vec4 backdropColor;
};
osgText::Text* createLabel(const std::string& l, TextSettings& settings, unsigned int size)
{ {
static osg::Vec3 pos(10.0f, 10.0f, 0.0f); static osg::Vec3 pos(10.0f, 10.0f, 0.0f);
osgText::Text* label = new osgText::Text(); osgText::Text* label = new osgText::Text();
osg::ref_ptr<osgText::Font> font = osgText::readRefFontFile(fontfile); osg::ref_ptr<osgText::Font> font = osgText::readRefFontFile(settings.fontFilename);
settings.setText(*label);
label->setFont(font);
label->setCharacterSize(size); label->setCharacterSize(size);
label->setFontResolution(size, size); label->setFontResolution(size, size);
label->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
label->setPosition(pos); label->setPosition(pos);
label->setAlignment(osgText::Text::LEFT_BOTTOM); label->setAlignment(osgText::Text::LEFT_BOTTOM);
// It seems to be important we do this last to get best results? // It seems to be important we do this last to get best results?
label->setText(l); label->setText(l);
textInfo(label);
// textInfo(label);
pos.y() += size + 10.0f; pos.y() += size + 10.0f;
return label; return label;
} }
typedef std::list<unsigned int> Sizes; typedef std::list<unsigned int> Sizes;
int main(int argc, char** argv) int main(int argc, char** argv)
@ -101,6 +146,11 @@ int main(int argc, char** argv)
viewer.addEventHandler(new osgViewer::StatsHandler()); viewer.addEventHandler(new osgViewer::StatsHandler());
viewer.addEventHandler(new osgViewer::WindowSizeHandler()); viewer.addEventHandler(new osgViewer::WindowSizeHandler());
osg::Vec4d backgroudColor = viewer.getCamera()->getClearColor();
if (args.read("--bg-color", backgroudColor.r(), backgroudColor.g(), backgroudColor.b(), backgroudColor.a()))
{
viewer.getCamera()->setClearColor(backgroudColor);
}
osg::ref_ptr<osg::Group> root = new osg::Group; osg::ref_ptr<osg::Group> root = new osg::Group;
@ -119,9 +169,10 @@ int main(int argc, char** argv)
root = transform; root = transform;
} }
std::string fontfile("arial.ttf"); TextSettings settings;
settings.fontFilename = argv[1];
settings.read(args);
fontfile = argv[1];
// Create the list of desired sizes. // Create the list of desired sizes.
Sizes sizes; Sizes sizes;
@ -142,11 +193,18 @@ int main(int argc, char** argv)
ss << *i << " 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"; ss << *i << " 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
geode->addDrawable(createLabel(ss.str(), fontfile, *i)); geode->addDrawable(createLabel(ss.str(), settings, *i));
} }
root->addChild(geode); root->addChild(geode);
std::string filename;
if (args.read("-o", filename))
{
osgDB::writeNodeFile(*root, filename);
return 0;
}
viewer.setSceneData(root.get()); viewer.setSceneData(root.get());
return viewer.run(); return viewer.run();

View File

@ -1131,6 +1131,8 @@ void Text::drawImplementationSinglePass(osg::State& state, const osg::Vec4& colo
#if 1 #if 1
if(_backdropType != NONE) if(_backdropType != NONE)
{ {
OSG_NOTICE<<"Text::drawImplementationSinglePass() Drawing backdrop"<<std::endl;
unsigned int backdrop_index; unsigned int backdrop_index;
unsigned int max_backdrop_index; unsigned int max_backdrop_index;
if(_backdropType == OUTLINE) if(_backdropType == OUTLINE)
@ -1144,13 +1146,20 @@ void Text::drawImplementationSinglePass(osg::State& state, const osg::Vec4& colo
max_backdrop_index = backdrop_index+1; max_backdrop_index = backdrop_index+1;
} }
OSG_NOTICE<<" 1 backdrop_index="<<backdrop_index<<", max_backdrop_index="<<max_backdrop_index<<std::endl;
if (max_backdrop_index>glyphquad._primitives.size()) max_backdrop_index=glyphquad._primitives.size(); if (max_backdrop_index>glyphquad._primitives.size()) max_backdrop_index=glyphquad._primitives.size();
OSG_NOTICE<<" 2 backdrop_index="<<backdrop_index<<", max_backdrop_index="<<max_backdrop_index<<std::endl;
state.disableColorPointer(); state.disableColorPointer();
state.Color(_backdropColor.r(),_backdropColor.g(),_backdropColor.b(),_backdropColor.a()); state.Color(_backdropColor.r(),_backdropColor.g(),_backdropColor.b(),_backdropColor.a());
OSG_NOTICE<<" _backdropColor("<<_backdropColor<<")"<<std::endl;
for( ; backdrop_index < max_backdrop_index; backdrop_index++) for( ; backdrop_index < max_backdrop_index; backdrop_index++)
{ {
OSG_NOTICE<<" draw backdrop "<<backdrop_index<<std::endl;
glyphquad._primitives[backdrop_index]->draw(state, usingVertexBufferObjects); glyphquad._primitives[backdrop_index]->draw(state, usingVertexBufferObjects);
} }
} }