2008-05-07 21:46:24 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <sstream>
|
|
|
|
#include <osg/io_utils>
|
|
|
|
#include <osg/ArgumentParser>
|
|
|
|
#include <osg/Geode>
|
|
|
|
#include <osgViewer/Viewer>
|
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
2010-03-19 01:10:48 +08:00
|
|
|
#include <osgGA/StateSetManipulator>
|
2008-05-07 21:46:24 +08:00
|
|
|
|
|
|
|
void textInfo(osgText::Text* text)
|
|
|
|
{
|
2010-03-19 01:10:48 +08:00
|
|
|
const osgText::Text::TextureGlyphQuadMap& tgqm = text->getTextureGlyphQuadMap();
|
|
|
|
|
|
|
|
const osgText::Text::TextureGlyphQuadMap::const_iterator tgqmi = tgqm.begin();
|
|
|
|
|
|
|
|
const osgText::Text::GlyphQuads& gq = tgqmi->second;
|
|
|
|
|
|
|
|
osgText::String& s = text->getText();
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < s.size(); i++)
|
|
|
|
{
|
2017-02-22 20:07:43 +08:00
|
|
|
osg::Vec2 ul; gq.getCoord(0 + (i * 4), ul); // upperLeft
|
|
|
|
osg::Vec2 ll; gq.getCoord(1 + (i * 4), ll); // lowerLeft
|
|
|
|
osg::Vec2 lr; gq.getCoord(2 + (i * 4), lr); // lowerRight
|
|
|
|
osg::Vec2 ur; gq.getCoord(3 + (i * 4), ur); // upperRight
|
2010-03-19 01:10:48 +08:00
|
|
|
|
|
|
|
osg::notify(osg::NOTICE)
|
|
|
|
<< "'" << static_cast<char>(s[i]) << "':"
|
|
|
|
<< " width(" << lr.x() - ll.x() << ")"
|
|
|
|
<< " height(" << ul.y() - ll.y() << ")" << std::endl << "\t"
|
|
|
|
<< "ul(" << ul << "), "
|
|
|
|
<< "ll(" << ll << "), "
|
|
|
|
<< "lr(" << lr << "), "
|
|
|
|
<< "ur(" << ur << ")"
|
|
|
|
<< std::endl
|
|
|
|
;
|
|
|
|
}
|
2008-05-07 21:46:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
osg::Camera* createOrthoCamera(double width, double height)
|
|
|
|
{
|
2010-03-19 01:10:48 +08:00
|
|
|
osg::Camera* camera = new osg::Camera();
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
camera->getOrCreateStateSet()->setMode(
|
|
|
|
GL_LIGHTING,
|
|
|
|
osg::StateAttribute::PROTECTED | osg::StateAttribute::OFF
|
|
|
|
);
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
osg::Matrix m = osg::Matrix::ortho2D(0.0f, width, 0.0f, height);
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
camera->setProjectionMatrix(m);
|
|
|
|
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
|
|
|
|
camera->setViewMatrix(osg::Matrix::identity());
|
|
|
|
camera->setClearMask(GL_DEPTH_BUFFER_BIT);
|
|
|
|
camera->setRenderOrder(osg::Camera::POST_RENDER);
|
2009-02-08 23:56:35 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
return camera;
|
2008-05-07 21:46:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
osgText::Text* createLabel(const std::string& l, const char* f, unsigned int size)
|
|
|
|
{
|
2010-03-19 01:10:48 +08:00
|
|
|
static osg::Vec3 pos(10.0f, 10.0f, 0.0f);
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
osgText::Text* label = new osgText::Text();
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osgText::Font> font = osgText::readRefFontFile(f);
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
label->setFont(font);
|
|
|
|
label->setCharacterSize(size);
|
|
|
|
label->setFontResolution(size, size);
|
|
|
|
label->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
|
|
|
|
label->setPosition(pos);
|
|
|
|
label->setAlignment(osgText::Text::LEFT_BOTTOM);
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
// It seems to be important we do this last to get best results?
|
|
|
|
label->setText(l);
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
textInfo(label);
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
pos.y() += size + 10.0f;
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
return label;
|
2008-05-07 21:46:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef std::list<unsigned int> Sizes;
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2010-03-19 01:10:48 +08:00
|
|
|
osgViewer::Viewer viewer;
|
|
|
|
osg::ArgumentParser args(&argc, argv);
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
// Make sure we have the minimum args...
|
|
|
|
if(argc <= 2)
|
|
|
|
{
|
|
|
|
osg::notify(osg::FATAL) << "usage: " << args[0] << " fontfile size1 [size2 ...]" << std::endl;
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2008-05-07 21:46:24 +08:00
|
|
|
|
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
|
|
|
|
viewer.addEventHandler(new osgViewer::StatsHandler());
|
|
|
|
viewer.addEventHandler(new osgViewer::WindowSizeHandler());
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
osg::Group* group = new osg::Group();
|
|
|
|
osg::Camera* camera = createOrthoCamera(1280.0f, 1024.0f);
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
// Create the list of desired sizes.
|
|
|
|
Sizes sizes;
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
for(int i = 2; i < argc; i++)
|
|
|
|
{
|
|
|
|
if(!args.isNumber(i)) continue;
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
sizes.push_back(std::atoi(args[i]));
|
|
|
|
}
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
osg::Geode* geode = new osg::Geode();
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
// Add all of our osgText drawables.
|
|
|
|
for(Sizes::const_iterator i = sizes.begin(); i != sizes.end(); i++)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
ss << *i << " 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
geode->addDrawable(createLabel(ss.str(), args[1], *i));
|
|
|
|
}
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
camera->addChild(geode);
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
group->addChild(camera);
|
2008-05-07 21:46:24 +08:00
|
|
|
|
2010-03-19 01:10:48 +08:00
|
|
|
viewer.setSceneData(group);
|
|
|
|
|
|
|
|
return viewer.run();
|
2008-05-07 21:46:24 +08:00
|
|
|
}
|
2009-02-08 23:56:35 +08:00
|
|
|
|