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

Revision 12069, 6.0 kB (checked in by robert, 3 years ago)

Added more flexible testing of Text3D

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 <osgDB/WriteFile>
27#include <osgGA/StateSetManipulator>
28#include <osgUtil/Tessellator>
29#include <osgViewer/Viewer>
30#include <osgViewer/ViewerEventHandlers>
31#include <osg/io_utils>
32
33#include <osgText/Text3D>
34
35#include "TextNode.h"
36
37extern int main_orig(int, char**);
38extern int main_test(int, char**);
39
40
41int main_size(int argc, char** argv)
42{
43    osg::ArgumentParser arguments(&argc, argv);
44
45    osgViewer::Viewer viewer(arguments);
46
47    std::string fontFile("arial.ttf");
48    while(arguments.read("-f",fontFile)) {}
49
50    osg::ref_ptr<osgText::Font> font = osgText::readFontFile(fontFile);
51    if (!font) return 1;
52    OSG_NOTICE<<"Read font "<<fontFile<<" font="<<font.get()<<std::endl;
53
54    osg::Geode* geode = new osg::Geode;
55
56    geode->addDrawable( osg::createTexturedQuadGeometry(osg::Vec3(0.0f,0.0f,0.0f),osg::Vec3(1.0f,0.0,0.0),osg::Vec3(0.0f,0.0,1.0), 0.0, 0.0, 1.0, 1.0) );
57
58    osgText::Text3D* text3d = new osgText::Text3D;
59    text3d->setPosition(osg::Vec3(1.0f,0.0f,0.0f));
60    text3d->setFont(osgText::readFontFile("arial.ttf"));
61    text3d->setCharacterSizeMode(osgText::Text3D::OBJECT_COORDS);
62    text3d->setCharacterSize(1.0f);
63    text3d->setCharacterDepth(0.1f);
64    text3d->setAxisAlignment(osgText::Text3D::XZ_PLANE);
65    text3d->setText("This is a size test");
66
67    geode->addDrawable(text3d);
68
69    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
70    viewer.addEventHandler(new osgViewer::StatsHandler);
71
72    viewer.setSceneData(geode);
73
74    return viewer.run();
75}
76
77
78
79int main(int argc, char** argv)
80{
81    osg::ArgumentParser arguments(&argc, argv);
82
83    if (arguments.read("--test"))
84    {
85        return main_test(argc,argv);
86    }
87    else if (arguments.read("--original") || arguments.read("--orig"))
88    {
89        return main_orig(argc,argv);
90    }
91    else if (arguments.read("--size-test"))
92    {
93        return main_size(argc,argv);
94    }
95
96    osgViewer::Viewer viewer(arguments);
97
98    std::string fontFile("arial.ttf");
99    while(arguments.read("-f",fontFile)) {}
100
101    osg::ref_ptr<osgText::Font> font = osgText::readFontFile(fontFile);
102    if (!font) return 1;
103    OSG_NOTICE<<"Read font "<<fontFile<<" font="<<font.get()<<std::endl;
104
105    std::string word("This is a new test.");
106    while (arguments.read("-w",word)) {}
107
108    osg::ref_ptr<osgText::Style> style = new osgText::Style;
109
110    float thickness = 0.1f;
111    while(arguments.read("--thickness",thickness)) {}
112    style->setThicknessRatio(thickness);
113
114    // set up any bevel if required
115    float r;
116    osg::ref_ptr<osgText::Bevel> bevel;
117    while(arguments.read("--rounded",r)) { bevel = new osgText::Bevel; bevel->roundedBevel2(r); }
118    while(arguments.read("--rounded")) { bevel = new osgText::Bevel; bevel->roundedBevel2(0.25); }
119    while(arguments.read("--flat",r)) { bevel = new osgText::Bevel; bevel->flatBevel(r); }
120    while(arguments.read("--flat")) { bevel = new osgText::Bevel; bevel->flatBevel(0.25); }
121    while(arguments.read("--bevel-thickness",r)) { if (bevel.valid()) bevel->setBevelThickness(r); }
122
123    style->setBevel(bevel.get());
124
125    // set up outline.
126    while(arguments.read("--outline",r)) { style->setOutlineRatio(r); }
127
128    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
129    viewer.addEventHandler(new osgViewer::StatsHandler);
130
131#if 1
132    osg::Geode* geode = new osg::Geode;
133
134    float characterSize = 1.0f;
135    while(arguments.read("--size",characterSize)) {}
136
137    if (arguments.read("--2d"))
138    {
139        osgText::Text* text2D = new osgText::Text;
140        text2D->setFont(font.get());
141        text2D->setCharacterSize(characterSize);
142        text2D->setFontResolution(256,256);
143        text2D->setDrawMode(osgText::Text::TEXT | osgText::Text::BOUNDINGBOX);
144        text2D->setAxisAlignment(osgText::Text::XZ_PLANE);
145        text2D->setText(word);
146        geode->addDrawable(text2D);
147    }
148        if (!arguments.read("--no-3d"))
149    {
150        osgText::Text3D* text3D = new osgText::Text3D;
151        text3D->setFont(font.get());
152        text3D->setStyle(style.get());
153        text3D->setCharacterSize(characterSize);
154        text3D->setDrawMode(osgText::Text3D::TEXT | osgText::Text3D::BOUNDINGBOX);
155        text3D->setAxisAlignment(osgText::Text3D::XZ_PLANE);
156        text3D->setText(word);
157        geode->addDrawable(text3D);
158    }
159   
160
161    if (arguments.read("--size-quad"))
162    {
163        geode->addDrawable( osg::createTexturedQuadGeometry(osg::Vec3(0.0f,characterSize*thickness,0.0f),osg::Vec3(characterSize,0.0,0.0),osg::Vec3(0.0f,0.0,characterSize), 0.0, 0.0, 1.0, 1.0) );
164    }
165   
166    viewer.setSceneData(geode);
167#else
168    osgText::TextNode* text = new osgText::TextNode;
169    text->setFont(font.get());
170    text->setStyle(style.get());
171    text->setTextTechnique(new osgText::TextTechnique);
172    text->setText(word);
173    text->update();
174    viewer.setSceneData(text);
175#endif
176
177    return viewer.run();
178}
Note: See TracBrowser for help on using the browser.