root/OpenSceneGraph/trunk/examples/osgfont/osgfont.cpp @ 12292

Revision 12292, 4.0 kB (checked in by robert, 4 years ago)

Ran svn propset -R svn:eol-style native . on the OpenSceneGraph

  • Property svn:eol-style set to native
RevLine 
[8274]1#include <cstdlib>
2#include <sstream>
3#include <osg/io_utils>
4#include <osg/ArgumentParser>
5#include <osg/Geode>
6#include <osgViewer/Viewer>
7#include <osgViewer/ViewerEventHandlers>
[11250]8#include <osgGA/StateSetManipulator>
[8274]9
10void textInfo(osgText::Text* text)
11{
[11250]12    const osgText::Text::TextureGlyphQuadMap& tgqm = text->getTextureGlyphQuadMap();
[8274]13
[11250]14    const osgText::Text::TextureGlyphQuadMap::const_iterator tgqmi = tgqm.begin();
[9705]15
[11250]16    const osgText::Text::GlyphQuads& gq = tgqmi->second;
[8274]17
[11250]18    osgText::String& s = text->getText();
[8274]19
[11250]20    for(unsigned int i = 0; i < s.size(); i++)
21    {
22        osg::Vec2 ul = gq.getCoords()[0 + (i * 4)]; // upperLeft
23        osg::Vec2 ll = gq.getCoords()[1 + (i * 4)]; // lowerLeft
24        osg::Vec2 lr = gq.getCoords()[2 + (i * 4)]; // lowerRight
25        osg::Vec2 ur = gq.getCoords()[3 + (i * 4)]; // upperRight
[8274]26
[11250]27        /*
28        osg::Vec3 ul = gq.getTransformedCoords(0)[0 + (i * 4)];
29        osg::Vec3 ll = gq.getTransformedCoords(0)[1 + (i * 4)];
30        osg::Vec3 lr = gq.getTransformedCoords(0)[2 + (i * 4)];
31        osg::Vec3 ur = gq.getTransformedCoords(0)[3 + (i * 4)];
32        */
[8274]33
[11250]34        osg::notify(osg::NOTICE)
35            << "'" << static_cast<char>(s[i]) << "':"
36            << " width(" << lr.x() - ll.x() << ")"
37            << " height(" << ul.y() - ll.y() << ")" << std::endl << "\t"
38            << "ul(" << ul << "), "
39            << "ll(" << ll << "), "
40            << "lr(" << lr << "), "
41            << "ur(" << ur << ")"
42            << std::endl
43        ;
44    }
[8274]45}
46
47osg::Camera* createOrthoCamera(double width, double height)
48{
[11250]49    osg::Camera* camera = new osg::Camera();
[8274]50
[11250]51    camera->getOrCreateStateSet()->setMode(
52        GL_LIGHTING,
53        osg::StateAttribute::PROTECTED | osg::StateAttribute::OFF
54    );
[8274]55
[11250]56    osg::Matrix m = osg::Matrix::ortho2D(0.0f, width, 0.0f, height);
[8274]57
[11250]58    camera->setProjectionMatrix(m);
59    camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
60    camera->setViewMatrix(osg::Matrix::identity());
61    camera->setClearMask(GL_DEPTH_BUFFER_BIT);
62    camera->setRenderOrder(osg::Camera::POST_RENDER);
[9705]63
[11250]64    return camera;
[8274]65}
66
67osgText::Text* createLabel(const std::string& l, const char* f, unsigned int size)
68{
[11250]69    static osg::Vec3 pos(10.0f, 10.0f, 0.0f);
[8274]70
[11250]71    osgText::Text* label = new osgText::Text();
72    osgText::Font* font  = osgText::readFontFile(f);
[8274]73
[11250]74    label->setFont(font);
75    label->setCharacterSize(size);
76    label->setFontResolution(size, size);
77    label->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
78    label->setPosition(pos);
79    label->setAlignment(osgText::Text::LEFT_BOTTOM);
[8274]80
[11250]81    // It seems to be important we do this last to get best results?
82    label->setText(l);
[8274]83
[11250]84    textInfo(label);
[8274]85
[11250]86    pos.y() += size + 10.0f;
[8274]87
[11250]88    return label;
[8274]89}
90
91typedef std::list<unsigned int> Sizes;
92
93int main(int argc, char** argv)
94{
[11250]95    osgViewer::Viewer   viewer;
96    osg::ArgumentParser args(&argc, argv);
[8274]97
[11250]98    // Make sure we have the minimum args...
99    if(argc <= 2)
100    {
101        osg::notify(osg::FATAL) << "usage: " << args[0] << " fontfile size1 [size2 ...]" << std::endl;
[8274]102
[11250]103        return 1;
104    }
[8274]105
106
[11250]107    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
108    viewer.addEventHandler(new osgViewer::StatsHandler());
109    viewer.addEventHandler(new osgViewer::WindowSizeHandler());
[8274]110
[11250]111    osg::Group*  group  = new osg::Group();
112    osg::Camera* camera = createOrthoCamera(1280.0f, 1024.0f);
[8274]113
[11250]114    // Create the list of desired sizes.
115    Sizes sizes;
[8274]116
[11250]117    for(int i = 2; i < argc; i++)
118    {
119        if(!args.isNumber(i)) continue;
[8274]120
[11250]121        sizes.push_back(std::atoi(args[i]));
122    }
[8274]123
[11250]124    osg::Geode* geode = new osg::Geode();
[8274]125
[11250]126    // Add all of our osgText drawables.
127    for(Sizes::const_iterator i = sizes.begin(); i != sizes.end(); i++)
128    {
129        std::stringstream ss;
[8274]130
[11250]131        ss << *i << " 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
[8274]132
[11250]133        geode->addDrawable(createLabel(ss.str(), args[1], *i));
134    }
[8274]135
[11250]136    camera->addChild(geode);
[8274]137
[11250]138    group->addChild(camera);
[8274]139
[11250]140    viewer.setSceneData(group);
141
142    return viewer.run();
[8274]143}
Note: See TracBrowser for help on using the browser.