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

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

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

  • Property svn:eol-style set to native
Line 
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>
8#include <osgGA/StateSetManipulator>
9
10void textInfo(osgText::Text* text)
11{
12    const osgText::Text::TextureGlyphQuadMap& tgqm = text->getTextureGlyphQuadMap();
13
14    const osgText::Text::TextureGlyphQuadMap::const_iterator tgqmi = tgqm.begin();
15
16    const osgText::Text::GlyphQuads& gq = tgqmi->second;
17
18    osgText::String& s = text->getText();
19
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
26
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        */
33
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    }
45}
46
47osg::Camera* createOrthoCamera(double width, double height)
48{
49    osg::Camera* camera = new osg::Camera();
50
51    camera->getOrCreateStateSet()->setMode(
52        GL_LIGHTING,
53        osg::StateAttribute::PROTECTED | osg::StateAttribute::OFF
54    );
55
56    osg::Matrix m = osg::Matrix::ortho2D(0.0f, width, 0.0f, height);
57
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);
63
64    return camera;
65}
66
67osgText::Text* createLabel(const std::string& l, const char* f, unsigned int size)
68{
69    static osg::Vec3 pos(10.0f, 10.0f, 0.0f);
70
71    osgText::Text* label = new osgText::Text();
72    osgText::Font* font  = osgText::readFontFile(f);
73
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);
80
81    // It seems to be important we do this last to get best results?
82    label->setText(l);
83
84    textInfo(label);
85
86    pos.y() += size + 10.0f;
87
88    return label;
89}
90
91typedef std::list<unsigned int> Sizes;
92
93int main(int argc, char** argv)
94{
95    osgViewer::Viewer   viewer;
96    osg::ArgumentParser args(&argc, argv);
97
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;
102
103        return 1;
104    }
105
106
107    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
108    viewer.addEventHandler(new osgViewer::StatsHandler());
109    viewer.addEventHandler(new osgViewer::WindowSizeHandler());
110
111    osg::Group*  group  = new osg::Group();
112    osg::Camera* camera = createOrthoCamera(1280.0f, 1024.0f);
113
114    // Create the list of desired sizes.
115    Sizes sizes;
116
117    for(int i = 2; i < argc; i++)
118    {
119        if(!args.isNumber(i)) continue;
120
121        sizes.push_back(std::atoi(args[i]));
122    }
123
124    osg::Geode* geode = new osg::Geode();
125
126    // Add all of our osgText drawables.
127    for(Sizes::const_iterator i = sizes.begin(); i != sizes.end(); i++)
128    {
129        std::stringstream ss;
130
131        ss << *i << " 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
132
133        geode->addDrawable(createLabel(ss.str(), args[1], *i));
134    }
135
136    camera->addChild(geode);
137
138    group->addChild(camera);
139
140    viewer.setSceneData(group);
141
142    return viewer.run();
143}
Note: See TracBrowser for help on using the browser.