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

Revision 11731, 5.9 kB (checked in by robert, 4 years ago)

Refactored osgText::Font so that it now supports both 2D and 3D glyphs.

Added TextNode?.h and TextNode?.cpp to examples/osgtext3D in prep for introducing the new node to osgText library

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#include "TextNode.h"
36
37extern int main_orig(int, char**);
38extern int main_test(int, char**);
39
40int main_experimental(osg::ArgumentParser& arguments)
41{
42    std::string fontFile("arial.ttf");
43    while(arguments.read("-f",fontFile)) {}
44
45    std::string word("This is a simple test");
46
47    while(arguments.read("--ascii"))
48    {
49        word.clear();
50        for(unsigned int c=' '; c<=127;++c)
51        {
52            word.push_back(c);
53        }
54    }
55
56    while(arguments.read("-w",word)) {}
57
58    osg::ref_ptr<osgText::Font> font = osgText::readFontFile(fontFile);
59    if (!font) return 1;
60    OSG_NOTICE<<"Read font "<<fontFile<<" font="<<font.get()<<std::endl;
61
62    bool useTessellator = true;
63    while(arguments.read("-t") || arguments.read("--tessellate")) { useTessellator = true; }
64    while(arguments.read("--no-tessellate")) { useTessellator = false; }
65
66    float thickness = 5.0;
67    while(arguments.read("--thickness",thickness)) {}
68
69    float width = 20.0;
70    while(arguments.read("--width",width)) {}
71
72    float creaseAngle = 30.0f;
73    while(arguments.read("--crease-angle",creaseAngle)) {}
74
75    OSG_NOTICE<<"creaseAngle="<<creaseAngle<<std::endl;
76
77    osgText::BevelProfile profile;
78    float ratio = 0.5;
79    while(arguments.read("--rounded",ratio)) { profile.roundedBevel(ratio); }
80    while(arguments.read("--rounded2",ratio)) { profile.roundedBevel2(ratio); }
81    while(arguments.read("--flat",ratio)) { profile.flatBevel(ratio); }
82
83    bool outline = false;
84    while(arguments.read("--outline")) { outline = true; }
85    while(arguments.read("--no-outline")) { outline = false; }
86
87    bool smooth = true;
88    while(arguments.read("--flat-shaded")) { smooth = false; }
89    while(arguments.read("--smooth")) { smooth = false; }
90
91    unsigned int numSamples = 10;
92    while(arguments.read("--samples", numSamples)) {}
93    font->setNumberCurveSamples(numSamples);
94
95    profile.print(std::cout);
96
97
98    osg::ref_ptr<osg::Group> group = new osg::Group;
99    osg::Vec3 position;
100
101    for(unsigned int i=0; i<word.size(); ++i)
102    {
103        osg::ref_ptr<osgText::Glyph3D> glyph = font->getGlyph3D(word[i]);
104        if (!glyph) return 1;
105
106        osg::ref_ptr<osg::PositionAttitudeTransform> transform = new osg::PositionAttitudeTransform;
107        transform->setPosition(position);
108        transform->setAttitude(osg::Quat(osg::inDegrees(90.0),osg::Vec3d(1.0,0.0,0.0)));
109
110        position.x() += glyph->getHorizontalWidth();
111
112        osg::ref_ptr<osg::Geode> geode = new osg::Geode;
113
114        osg::ref_ptr<osg::Geometry> glyphGeometry = osgText::computeGlyphGeometry(glyph.get(), thickness, width);
115        osg::ref_ptr<osg::Geometry> textGeometry = osgText::computeTextGeometry(glyphGeometry.get(), profile, width);
116        osg::ref_ptr<osg::Geometry> shellGeometry = outline ? osgText::computeShellGeometry(glyphGeometry.get(), profile, width) : 0;
117        if (textGeometry.valid()) geode->addDrawable(textGeometry.get());
118        if (shellGeometry.valid()) geode->addDrawable(shellGeometry.get());
119
120        // create the normals
121        if (smooth && textGeometry.valid())
122        {
123            osgUtil::SmoothingVisitor::smooth(*textGeometry, osg::DegreesToRadians(creaseAngle));
124        }
125
126        transform->addChild(geode.get());
127
128        group->addChild(transform.get());
129    }
130
131    std::string filename;
132    if (arguments.read("-o", filename)) osgDB::writeNodeFile(*group, filename);
133
134    osgViewer::Viewer viewer(arguments);
135    viewer.setSceneData(group.get());
136    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
137    viewer.addEventHandler(new osgViewer::StatsHandler);
138    return viewer.run();
139}
140
141int main(int argc, char** argv)
142{
143    osg::ArgumentParser arguments(&argc, argv);
144
145    if (arguments.read("--test"))
146    {
147        return main_test(argc,argv);
148    }
149    else if (arguments.read("--original") || arguments.read("--orig"))
150    {
151        return main_orig(argc,argv);
152    }
153    else if (arguments.read("--exp"))
154    {
155        return main_experimental(arguments);
156    }
157
158    osgViewer::Viewer viewer(arguments);
159
160    std::string fontFile("arial.ttf");
161    while(arguments.read("-f",fontFile)) {}
162
163    osg::ref_ptr<osgText::Font> font = osgText::readFontFile(fontFile);
164    if (!font) return 1;
165    OSG_NOTICE<<"Read font "<<fontFile<<" font="<<font.get()<<std::endl;
166
167    std::string word("This is a new test.");
168    while (arguments.read("-w",word)) {}
169
170    osgText::TextNode* text = new osgText::TextNode;
171    text->setText(word);
172    text->setTextTechnique(new osgText::TextTechnique);
173    text->update();
174
175    viewer.setSceneData(text);
176
177    return viewer.run();
178}
Note: See TracBrowser for help on using the browser.