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

Revision 1649, 12.0 kB (checked in by robert, 11 years ago)

Updates to Text for Sun Forte compilers.

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