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

Revision 9705, 3.5 kB (checked in by robert, 6 years ago)

From Paul Melis, "While trying out the osgbrowser example (where I had forgotten to update
LD_LIBRARY_PATH so the XUL libs would be found) I noticed that although
the gecko plugin was found it could not be loaded. But this did not
trigger any visible warning/error message (at least not without INFO
notify level). Would you mind if we change the notify level for a
dlerror() to WARNING? This will also make it more explicit for the case
when a plugin isn't actually found, which seems to come up a lot for
novice users (e.g. no freetype on win32, so no freetype plugin, etc).
Also, the current error message is misleading ("Warning: Could not FIND
plugin to ...") because the it's not always a case of not finding the
plugin. I slightly enhanced the situation of not finding a plugin versus
finding it but not being able to load it.

Here's also a few fixes to some of the examples:
- osgfont: make usage help line more in line with the actual behaviour
- osgcompositeviewer: complain when no model file was provided
- osgmovie: don't include quicktime-dependent feature on Linux
- osgocclussionquery: comment addition (as I was surprised that lines
were being drawn in a function called createRandomTriangles())"

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