root/OpenSceneGraph/trunk/examples/osgtext/osgtext.cpp @ 1643

Revision 1643, 7.5 kB (checked in by robert, 12 years ago)

Further improvements to osgText and the freetype plugin, handling different
font sizes better and plugin removal safely.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under 
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#include <osgUtil/Optimizer>
15#include <osgDB/ReadFile>
16#include <osgDB/Registry>
17#include <osgProducer/Viewer>
18
19#include <osg/Material>
20#include <osg/Geode>
21#include <osg/BlendFunc>
22#include <osg/Depth>
23#include <osg/Projection>
24#include <osg/MatrixTransform>
25
26#include <osgText/Text>
27
28
29osg::Group* createHUDText()
30{
31
32    osg::Geode* geode  = new osg::Geode;
33
34    osgText::Text* text1 = new osgText::Text;
35    text1->setCharacterSize(12.0f);
36    text1->setText("20 GOTO 10");
37    geode->addDrawable(text1);
38
39    osgText::Text* text2 = new osgText::Text;
40    text2->setPosition(osg::Vec3(0.0f,50.0f,0.0f));
41    text2->setCharacterSize(12.0f);
42    text2->setText("10 PRINT \"Hello World\"");
43    geode->addDrawable(text2);
44
45    osgText::Text* text3 = new osgText::Text;
46    text3->setFont(osgText::readFontFile("fonts/arial.ttf"));
47    text3->setPosition(osg::Vec3(100.0f,100.0f,0.0f));
48    text3->setText("This is a test of text AVAV/.|¬!£$%^&*() - fonts/arial.ttf");
49    geode->addDrawable(text3);
50
51    osgText::Text* text4 = new osgText::Text;
52    text4->setFont(osgText::readFontFile("fonts/times.ttf"));
53    text4->setFontSize(128,128);
54    text4->setPosition(osg::Vec3(200.0f,200.0f,0.0f));
55    text4->setLayout(osgText::Text::RIGHT_TO_LEFT);
56    text4->setDrawMode(osgText::Text::TEXT|osgText::Text::ALIGNMENT|osgText::Text::BOUNDINGBOX);
57    text4->setText("This is a test of text AVAV/.|¬!£$%^&*() - fonts/times.ttf");
58    geode->addDrawable(text4);
59
60    osgText::Text* text5 = new osgText::Text;
61    text5->setFont(osgText::readFontFile("fonts/dirtydoz.ttf"));
62    text5->setPosition(osg::Vec3(300.0f,300.0f,0.0f));
63    text5->setColor(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
64    text5->setLayout(osgText::Text::VERTICAL);
65    text5->setDrawMode(osgText::Text::TEXT|osgText::Text::ALIGNMENT|osgText::Text::BOUNDINGBOX);
66    text5->setText("This is a test of text AVAV/.|¬!£$%^&*() - fonts/dirtydoz.ttf");
67    geode->addDrawable(text5);
68
69
70//     osgText::Text::TextString string;
71//     for(int i=0;i<2048;++i)
72//         string.push_back(i);
73//
74//     osgText::Text* text6 = new osgText::Text;
75//     text6->setFont(osgText::readFontFile("/home/robert/Documents/GuopingSun/msmincho.ttc"));
76//     text6->setFontSize(64,64);
77//     text6->setText(string);
78//     text6->setPosition(osg::Vec3(00.0f,400.0f,0.0f));
79//     geode->addDrawable(text6);
80
81   
82
83
84    osg::Group* rootNode = new osg::Group;
85    rootNode->addChild(geode);
86
87    return rootNode;   
88}
89
90osg::Group* create3DText()
91{
92
93    osg::Geode* geode  = new osg::Geode;
94
95    osgText::Text* text1 = new osgText::Text;
96    text1->setFont("fonts/times.ttf");
97    text1->setAxisAlignment(osgText::Text::XY_PLANE);
98    text1->setText("XY_PLANE");
99    geode->addDrawable(text1);
100
101    osgText::Text* text2 = new osgText::Text;
102    text2->setFont("fonts/times.ttf");
103    text2->setPosition(osg::Vec3(0.0f,0.0f,0.0f));
104    text2->setAxisAlignment(osgText::Text::YZ_PLANE);
105    text2->setText("YZ_PLANE");
106    geode->addDrawable(text2);
107
108    osgText::Text* text3 = new osgText::Text;
109    text3->setFont("fonts/times.ttf");
110    text3->setPosition(osg::Vec3(00.0f,00.0f,00.0f));
111    text3->setAxisAlignment(osgText::Text::XZ_PLANE);
112    text3->setText("XZ_PLANE");
113    geode->addDrawable(text3);
114
115    osgText::Text* text4 = new osgText::Text;
116    text4->setFont("fonts/times.ttf");
117    text4->setAxisAlignment(osgText::Text::SCREEN);
118    text4->setPosition(osg::Vec3(00.0f,00.0f,00.0f));
119    text4->setText("SCREEN");
120    geode->addDrawable(text4);
121
122    osg::Group* rootNode = new osg::Group;
123    rootNode->addChild(geode);
124
125    return rootNode;   
126}
127
128int main( int argc, char **argv )
129{
130    // use an ArgumentParser object to manage the program arguments.
131    osg::ArgumentParser arguments(&argc,argv);
132   
133    // set up the usage document, in case we need to print out how to use this program.
134    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getProgramName()+" [options] [filename] ...");
135    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
136   
137
138    // construct the viewer.
139    osgProducer::Viewer viewer(arguments);
140
141    // set up the value with sensible default event handlers.
142    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
143
144    // get details on keyboard and mouse bindings used by the viewer.
145    viewer.getUsage(*arguments.getApplicationUsage());
146
147    // if user request help write it out to cout.
148    if (arguments.read("-h") || arguments.read("--help"))
149    {
150        arguments.getApplicationUsage()->write(cout);
151        return 1;
152    }
153
154    // any option left unread are converted into errors to write out later.
155    arguments.reportRemainingOptionsAsUnrecognized();
156
157    // report any errors if they have occured when parsing the program aguments.
158    if (arguments.errors())
159    {
160        arguments.writeErrorMessages(cout);
161        return 1;
162    }
163
164
165    // read the scene from the list of file specified commandline args.
166    osg::ref_ptr<osg::Node> rootNode = osgDB::readNodeFiles(arguments);
167
168    // prepare scene.
169    {
170
171        if (rootNode.valid())
172        {
173            // optimize the scene graph, remove rendundent nodes and state etc.
174            osgUtil::Optimizer optimizer;
175            optimizer.optimize(rootNode.get());
176        }
177
178        // make sure the root node is group so we can add extra nodes to it.
179        osg::Group* group = dynamic_cast<osg::Group*>(rootNode.get());
180        if (!group)
181        {
182            group = new osg::Group;
183           
184            if (rootNode.valid()) group->addChild(rootNode.get());
185           
186            rootNode = group;
187        }
188
189        {
190            // create the hud.
191            osg::Projection* projection = new osg::Projection;
192            projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024));
193
194            osg::MatrixTransform* modelview_abs = new osg::MatrixTransform;
195            modelview_abs->setReferenceFrame(osg::Transform::RELATIVE_TO_ABSOLUTE);
196            modelview_abs->setMatrix(osg::Matrix::identity());
197
198            modelview_abs->addChild(createHUDText());
199
200            projection->addChild(modelview_abs);
201
202            group->addChild(projection);
203        }
204
205        osg::MatrixTransform* scale = new osg::MatrixTransform;
206        scale->setMatrix(osg::Matrix::scale(1.0f,1.0f,1.0f));
207        scale->addChild(create3DText());
208        group->addChild(scale);
209
210    }
211
212    // set the scene to render
213    viewer.setSceneData(rootNode.get());
214   
215    // create the windows and run the threads.
216    viewer.realize(Producer::CameraGroup::ThreadPerCamera);
217
218    while( !viewer.done() )
219    {
220        // wait for all cull and draw threads to complete.
221        viewer.sync();
222
223        // update the scene by traversing it with the the update visitor which will
224        // call all node update callbacks and animations.
225        viewer.update();
226         
227        // fire off the cull and draw traversals of the scene.
228        viewer.frame();
229       
230    }
231
232    return 0;
233}
Note: See TracBrowser for help on using the browser.