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

Revision 1647, 12.4 kB (checked in by robert, 12 years ago)

Updates to the new osgtext example and fix to the friend usage.

  • 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/Geode>
20#include <osg/Projection>
21#include <osg/MatrixTransform>
22
23#include <osgText/Font>
24#include <osgText/Text>
25
26
27osg::Group* createHUDText()
28{
29
30    osgText::Font* font = osgText::readFontFile("fonts/arial.ttf");
31
32    osg::Geode* geode  = new osg::Geode;
33
34    float windowHeight = 1024.0f;
35    float windowWidth = 1280.0f;
36    float margin = 50.0f;
37
38
39    osg::Vec4 layoutColor(1.0f,1.0f,0.0f,1.0f);
40    float layoutCharacterSize = 20.0f;   
41   
42    {
43        osgText::Text* text = new osgText::Text;
44        text->setFont(font);
45        text->setColor(layoutColor);
46        text->setCharacterSize(layoutCharacterSize);
47        text->setPosition(osg::Vec3(margin,windowHeight-margin,0.0f));
48
49        // the default layout is left to right, typically used in languages
50        // originating from europe such as English, French, German, Spanish etc..
51        text->setLayout(osgText::Text::LEFT_TO_RIGHT);
52
53        text->setText("text->setLayout(osgText::Text::LEFT_TO_RIGHT);");
54        geode->addDrawable(text);
55    }
56
57    {
58        osgText::Text* text = new osgText::Text;
59        text->setFont(font);
60        text->setColor(layoutColor);
61        text->setCharacterSize(layoutCharacterSize);
62        text->setPosition(osg::Vec3(windowWidth-margin,windowHeight-margin,0.0f));
63
64        // right to left layouts would be used for hebrew or arabic fonts.
65        text->setLayout(osgText::Text::RIGHT_TO_LEFT);
66
67        text->setText("text->setLayout(osgText::Text::RIGHT_TO_LEFT);");
68        geode->addDrawable(text);
69    }
70
71    {
72        osgText::Text* text = new osgText::Text;
73        text->setFont(font);
74        text->setColor(layoutColor);
75        text->setPosition(osg::Vec3(margin,windowHeight-margin,0.0f));
76        text->setCharacterSize(layoutCharacterSize);
77
78        // vertical font layout would be used for asian fonts.
79        text->setLayout(osgText::Text::VERTICAL);
80
81        text->setText("text->setLayout(osgText::Text::VERTICAL);");
82        geode->addDrawable(text);
83    }
84   
85   
86    osg::Vec4 fontSizeColor(0.0f,1.0f,1.0f,1.0f);
87    float fontSizeCharacterSize = 30;
88   
89    osg::Vec3 cursor = osg::Vec3(margin*2,windowHeight-margin*2,0.0f);
90   
91    {
92        osgText::Text* text = new osgText::Text;
93        text->setFont(font);
94        text->setColor(fontSizeColor);
95        text->setCharacterSize(fontSizeCharacterSize);
96        text->setPosition(cursor);
97       
98        // use text that uses 10 by 10 texels as a target resolution for fonts.
99        text->setFontSize(10,10); // blocky but small texture memory usage
100       
101        text->setText("text->setFontSize(10,10); // blocky but small texture memory usage");
102        geode->addDrawable(text);
103    }
104   
105    cursor.y() -= fontSizeCharacterSize;
106    {
107        osgText::Text* text = new osgText::Text;
108        text->setFont(font);
109        text->setColor(fontSizeColor);
110        text->setCharacterSize(fontSizeCharacterSize);
111        text->setPosition(cursor);
112       
113        // use text that uses 10 by 10 texels as a target resolution for fonts.
114        text->setFontSize(20,20); // smoother but higher texture memory usage (but still quite low).
115       
116        text->setText("text->setFontSize(20,20); // smoother but higher texture memory usage (but still quite low).");
117        geode->addDrawable(text);
118    }
119   
120    cursor.y() -= fontSizeCharacterSize;
121    {
122        osgText::Text* text = new osgText::Text;
123        text->setFont(font);
124        text->setColor(fontSizeColor);
125        text->setCharacterSize(fontSizeCharacterSize);
126        text->setPosition(cursor);
127       
128        // use text that uses 10 by 10 texels as a target resolution for fonts.
129        text->setFontSize(40,40); // even smoother but again higher texture memory usage.
130       
131        text->setText("text->setFontSize(40,40); // even smoother but again higher texture memory usage.");
132        geode->addDrawable(text);
133    }
134
135
136    osg::Vec4 characterSizeColor(1.0f,0.0f,1.0f,1.0f);
137   
138    cursor.y() -= fontSizeCharacterSize*2.0f;
139   
140    {
141        osgText::Text* text = new osgText::Text;
142        text->setFont(font);
143        text->setColor(characterSizeColor);
144        text->setFontSize(15,15);
145        text->setPosition(cursor);
146       
147        // use text that 15 units high.
148        text->setCharacterSize(15); // tiny
149       
150        text->setText("text->setCharacterSize(15.0f); // tiny");
151        geode->addDrawable(text);
152    }
153   
154    cursor.y() -= fontSizeCharacterSize;
155    {
156        osgText::Text* text = new osgText::Text;
157        text->setFont(font);
158        text->setColor(characterSizeColor);
159        text->setFontSize(30,30);
160        text->setPosition(cursor);
161       
162        // use text that uses 10 by 10 texels as a target resolution for fonts.
163        text->setCharacterSize(30.0f); // tiny
164       
165        text->setText("text->setCharacterSize(30.0f); // medium size");
166        geode->addDrawable(text);
167    }
168   
169    cursor.y() -= fontSizeCharacterSize;
170    {
171        osgText::Text* text = new osgText::Text;
172        text->setFont(font);
173        text->setColor(characterSizeColor);
174        text->setFontSize(40,40);
175        text->setPosition(cursor);
176       
177        // use text that uses 10 by 10 texels as a target resolution for fonts.
178        text->setCharacterSize(40.0f); // tiny
179       
180        text->setText("text->setCharacterSize(40.0f); // big, but can go much bigger :-)");
181        geode->addDrawable(text);
182    }
183
184
185
186if (0)
187{
188    osgText::Text* text2 = new osgText::Text;
189    text2->setPosition(osg::Vec3(0.0f,50.0f,0.0f));
190    text2->setCharacterSize(12.0f);
191    text2->setText("10 PRINT \"Hello World\"");
192    geode->addDrawable(text2);
193
194    osgText::Text* text3 = new osgText::Text;
195    text3->setFont(osgText::readFontFile("fonts/arial.ttf"));
196    text3->setPosition(osg::Vec3(100.0f,100.0f,0.0f));
197    text3->setText("This is a test of text AVAV/.|¬!£$%^&*() - fonts/arial.ttf");
198    geode->addDrawable(text3);
199
200    osgText::Text* text4 = new osgText::Text;
201    text4->setFont(osgText::readFontFile("fonts/times.ttf"));
202    text4->setFontSize(128,128);
203    text4->setPosition(osg::Vec3(200.0f,200.0f,0.0f));
204    text4->setLayout(osgText::Text::RIGHT_TO_LEFT);
205    text4->setDrawMode(osgText::Text::TEXT|osgText::Text::ALIGNMENT|osgText::Text::BOUNDINGBOX);
206    text4->setText("This is a test of text AVAV/.|¬!£$%^&*() - fonts/times.ttf");
207    geode->addDrawable(text4);
208
209    osgText::Text* text5 = new osgText::Text;
210    text5->setFont(osgText::readFontFile("fonts/dirtydoz.ttf"));
211    text5->setPosition(osg::Vec3(300.0f,300.0f,0.0f));
212    text5->setColor(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
213    text5->setLayout(osgText::Text::VERTICAL);
214    text5->setDrawMode(osgText::Text::TEXT|osgText::Text::ALIGNMENT|osgText::Text::BOUNDINGBOX);
215    text5->setText("This is a test of text AVAV/.|¬!£$%^&*() - fonts/dirtydoz.ttf");
216    geode->addDrawable(text5);
217}
218
219//     osgText::Text::TextString string;
220//     for(int i=0;i<2048;++i)
221//         string.push_back(i);
222//
223//     osgText::Text* text6 = new osgText::Text;
224//     text6->setFont(osgText::readFontFile("/home/robert/Documents/GuopingSun/msmincho.ttc"));
225//     text6->setFontSize(64,64);
226//     text6->setText(string);
227//     text6->setPosition(osg::Vec3(00.0f,400.0f,0.0f));
228//     geode->addDrawable(text6);
229
230   
231
232
233    osg::Group* rootNode = new osg::Group;
234    rootNode->addChild(geode);
235
236    return rootNode;   
237}
238
239osg::Group* create3DText()
240{
241
242    osg::Geode* geode  = new osg::Geode;
243
244    osgText::Text* text1 = new osgText::Text;
245    text1->setFont("fonts/times.ttf");
246    text1->setAxisAlignment(osgText::Text::XY_PLANE);
247    text1->setText("XY_PLANE");
248    geode->addDrawable(text1);
249
250    osgText::Text* text2 = new osgText::Text;
251    text2->setFont("fonts/times.ttf");
252    text2->setPosition(osg::Vec3(0.0f,0.0f,0.0f));
253    text2->setAxisAlignment(osgText::Text::YZ_PLANE);
254    text2->setText("YZ_PLANE");
255    geode->addDrawable(text2);
256
257    osgText::Text* text3 = new osgText::Text;
258    text3->setFont("fonts/times.ttf");
259    text3->setPosition(osg::Vec3(00.0f,00.0f,00.0f));
260    text3->setAxisAlignment(osgText::Text::XZ_PLANE);
261    text3->setText("XZ_PLANE");
262    geode->addDrawable(text3);
263
264    osgText::Text* text4 = new osgText::Text;
265    text4->setFont("fonts/times.ttf");
266    text4->setAxisAlignment(osgText::Text::SCREEN);
267    text4->setPosition(osg::Vec3(00.0f,00.0f,00.0f));
268    text4->setText("SCREEN");
269    geode->addDrawable(text4);
270
271    osg::Group* rootNode = new osg::Group;
272    rootNode->addChild(geode);
273
274    return rootNode;   
275}
276
277int main( int argc, char **argv )
278{
279    // use an ArgumentParser object to manage the program arguments.
280    osg::ArgumentParser arguments(&argc,argv);
281   
282    // set up the usage document, in case we need to print out how to use this program.
283    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getProgramName()+" [options] [filename] ...");
284    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
285   
286
287    // construct the viewer.
288    osgProducer::Viewer viewer(arguments);
289
290    // set up the value with sensible default event handlers.
291    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
292
293    // get details on keyboard and mouse bindings used by the viewer.
294    viewer.getUsage(*arguments.getApplicationUsage());
295
296    // if user request help write it out to cout.
297    if (arguments.read("-h") || arguments.read("--help"))
298    {
299        arguments.getApplicationUsage()->write(cout);
300        return 1;
301    }
302
303    // any option left unread are converted into errors to write out later.
304    arguments.reportRemainingOptionsAsUnrecognized();
305
306    // report any errors if they have occured when parsing the program aguments.
307    if (arguments.errors())
308    {
309        arguments.writeErrorMessages(cout);
310        return 1;
311    }
312
313
314    // read the scene from the list of file specified commandline args.
315    osg::ref_ptr<osg::Node> rootNode = osgDB::readNodeFiles(arguments);
316
317    // prepare scene.
318    {
319
320        if (rootNode.valid())
321        {
322            // optimize the scene graph, remove rendundent nodes and state etc.
323            osgUtil::Optimizer optimizer;
324            optimizer.optimize(rootNode.get());
325        }
326
327        // make sure the root node is group so we can add extra nodes to it.
328        osg::Group* group = dynamic_cast<osg::Group*>(rootNode.get());
329        if (!group)
330        {
331            group = new osg::Group;
332           
333            if (rootNode.valid()) group->addChild(rootNode.get());
334           
335            rootNode = group;
336        }
337
338        {
339            // create the hud.
340            osg::Projection* projection = new osg::Projection;
341            projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024));
342
343            osg::MatrixTransform* modelview_abs = new osg::MatrixTransform;
344            modelview_abs->setReferenceFrame(osg::Transform::RELATIVE_TO_ABSOLUTE);
345            modelview_abs->setMatrix(osg::Matrix::identity());
346
347            modelview_abs->addChild(createHUDText());
348
349            projection->addChild(modelview_abs);
350
351            group->addChild(projection);
352        }
353
354        osg::MatrixTransform* scale = new osg::MatrixTransform;
355        scale->setMatrix(osg::Matrix::scale(1.0f,1.0f,1.0f));
356        scale->addChild(create3DText());
357        group->addChild(scale);
358
359    }
360
361    // set the scene to render
362    viewer.setSceneData(rootNode.get());
363   
364    // create the windows and run the threads.
365    viewer.realize(Producer::CameraGroup::ThreadPerCamera);
366
367    while( !viewer.done() )
368    {
369        // wait for all cull and draw threads to complete.
370        viewer.sync();
371
372        // update the scene by traversing it with the the update visitor which will
373        // call all node update callbacks and animations.
374        viewer.update();
375         
376        // fire off the cull and draw traversals of the scene.
377        viewer.frame();
378       
379    }
380
381    return 0;
382}
Note: See TracBrowser for help on using the browser.