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

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