root/OpenSceneGraph/trunk/examples/osgtext3D/osgtext3D.cpp @ 11727

Revision 11727, 5.2 kB (checked in by robert, 4 years ago)

Cleaned up main and introduced --samples <num>, --flat, --smooth command line controls.

Add StatsHandler? to viewer to enable review of different settings on number vertices/triangles.

Line 
1/* OpenSceneGraph example, osgtext.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osg/ArgumentParser>
20#include <osg/Geode>
21#include <osg/Geometry>
22#include <osg/CullFace>
23#include <osg/TriangleIndexFunctor>
24#include <osg/PositionAttitudeTransform>
25#include <osgUtil/SmoothingVisitor>
26#include <osgText/Font3D>
27#include <osgDB/WriteFile>
28#include <osgGA/StateSetManipulator>
29#include <osgUtil/Tessellator>
30#include <osgViewer/Viewer>
31#include <osgViewer/ViewerEventHandlers>
32#include <osg/io_utils>
33
34#include "GlyphGeometry.h"
35
36extern int main_orig(int, char**);
37extern int main_test(int, char**);
38
39int main(int argc, char** argv)
40{
41    osg::ArgumentParser arguments(&argc, argv);
42
43    if (arguments.read("--test"))
44    {
45        return main_test(argc,argv);
46    }
47    else if (arguments.read("--original") || arguments.read("--orig"))
48    {
49        return main_orig(argc,argv);
50    }
51
52    std::string fontFile("arial.ttf");
53    while(arguments.read("-f",fontFile)) {}
54
55    std::string word("This is a simple test");
56
57    while(arguments.read("--ascii"))
58    {
59        word.clear();
60        for(unsigned int c=' '; c<=127;++c)
61        {
62            word.push_back(c);
63        }
64    }
65
66    while(arguments.read("-w",word)) {}
67
68    osg::ref_ptr<osgText::Font3D> font = osgText::readFont3DFile(fontFile);
69    if (!font) return 1;
70    OSG_NOTICE<<"Read font "<<fontFile<<" font="<<font.get()<<std::endl;
71
72    bool useTessellator = true;
73    while(arguments.read("-t") || arguments.read("--tessellate")) { useTessellator = true; }
74    while(arguments.read("--no-tessellate")) { useTessellator = false; }
75
76    float thickness = 5.0;
77    while(arguments.read("--thickness",thickness)) {}
78
79    float width = 20.0;
80    while(arguments.read("--width",width)) {}
81
82    float creaseAngle = 30.0f;
83    while(arguments.read("--crease-angle",creaseAngle)) {}
84
85    OSG_NOTICE<<"creaseAngle="<<creaseAngle<<std::endl;
86
87    osgText::BevelProfile profile;
88    float ratio = 0.5;
89    while(arguments.read("--rounded",ratio)) { profile.roundedBevel(ratio); }
90    while(arguments.read("--rounded2",ratio)) { profile.roundedBevel2(ratio); }
91    while(arguments.read("--flat",ratio)) { profile.flatBevel(ratio); }
92
93    bool outline = false;
94    while(arguments.read("--outline")) { outline = true; }
95    while(arguments.read("--no-outline")) { outline = false; }
96
97    bool smooth = true;
98    while(arguments.read("--flat")) { smooth = false; }
99    while(arguments.read("--smooth")) { smooth = false; }
100
101    unsigned int numSamples = 10;
102    while(arguments.read("--samples", numSamples)) {}
103    font->setNumberCurveSamples(numSamples);
104
105    profile.print(std::cout);
106
107
108    osg::ref_ptr<osg::Group> group = new osg::Group;
109    osg::Vec3 position;
110
111    for(unsigned int i=0; i<word.size(); ++i)
112    {
113        osg::ref_ptr<osgText::Font3D::Glyph3D> glyph = font->getGlyph(word[i]);
114        if (!glyph) return 1;
115
116        osg::ref_ptr<osg::PositionAttitudeTransform> transform = new osg::PositionAttitudeTransform;
117        transform->setPosition(position);
118        transform->setAttitude(osg::Quat(osg::inDegrees(90.0),osg::Vec3d(1.0,0.0,0.0)));
119
120        position.x() += glyph->getHorizontalWidth();
121
122        osg::ref_ptr<osg::Geode> geode = new osg::Geode;
123
124        osg::ref_ptr<osg::Geometry> glyphGeometry = osgText::computeGlyphGeometry(glyph.get(), thickness, width);
125        osg::ref_ptr<osg::Geometry> textGeometry = osgText::computeTextGeometry(glyphGeometry.get(), profile, width);
126        osg::ref_ptr<osg::Geometry> shellGeometry = outline ? osgText::computeShellGeometry(glyphGeometry.get(), profile, width) : 0;
127        if (textGeometry.valid()) geode->addDrawable(textGeometry.get());
128        if (shellGeometry.valid()) geode->addDrawable(shellGeometry.get());
129
130        // create the normals
131        if (smooth && textGeometry.valid())
132        {
133            osgUtil::SmoothingVisitor::smooth(*textGeometry, osg::DegreesToRadians(creaseAngle));
134        }
135
136        transform->addChild(geode.get());
137
138        group->addChild(transform.get());
139    }
140
141    std::string filename;
142    if (arguments.read("-o", filename)) osgDB::writeNodeFile(*group, filename);
143
144    osgViewer::Viewer viewer(arguments);
145    viewer.setSceneData(group.get());
146    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
147    viewer.addEventHandler(new osgViewer::StatsHandler);
148    return viewer.run();
149}
Note: See TracBrowser for help on using the browser.