root/OpenSceneGraph/trunk/examples/osghud/osghud.cpp @ 1927

Revision 1927, 5.8 kB (checked in by robert, 11 years ago)

Added support for automatic scaling of text to screen coords. Optimized
the text implementation to provide better speed, especially by using the
alignement to screen option.

Deprecated the Text::setFontSize(,) method, which is now being replaced
by setFontResolution(,)

Fixed typos in Texture*.cpp.

Removed old deprecated methods from osg headers.

  • 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 <osgProducer/Viewer>
17
18#include <osg/Material>
19#include <osg/Geode>
20#include <osg/BlendFunc>
21#include <osg/Depth>
22#include <osg/Projection>
23#include <osg/MatrixTransform>
24
25#include <osgText/Text>
26
27
28osg::Node* createHUD()
29{
30    osg::Geode* geode = new osg::Geode();
31   
32    std::string timesFont("fonts/times.ttf");
33
34    // turn lighting off for the text and disable depth test to ensure its always ontop.
35    osg::StateSet* stateset = geode->getOrCreateStateSet();
36    stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
37    stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
38
39    osg::Vec3 position(150.0f,800.0f,0.0f);
40    osg::Vec3 delta(0.0f,-120.0f,0.0f);
41
42    {
43        osgText::Text* text = new  osgText::Text;
44        geode->addDrawable( text );
45
46        text->setFont(timesFont);
47        text->setPosition(position);
48        text->setText("Head Up Displays are simple :-)");
49       
50        position += delta;
51    }   
52
53
54    {
55        osgText::Text* text = new  osgText::Text;
56        geode->addDrawable( text );
57
58        text->setFont(timesFont);
59        text->setAutoScaleToScreen(true);
60        text->setPosition(position);
61        text->setText("All you need to do is create your text in a subgraph.");
62       
63        position += delta;
64    }   
65
66
67    {
68        osgText::Text* text = new  osgText::Text;
69        geode->addDrawable( text );
70
71        text->setFont(timesFont);
72        text->setPosition(position);
73        text->setText("Disable depth test in this subgraph to ensure its always ontop.");
74       
75        position += delta;
76    }   
77
78    {
79        osgText::Text* text = new  osgText::Text;
80        geode->addDrawable( text );
81
82        text->setFont(timesFont);
83        text->setPosition(position);
84        text->setText("Then place an osg::Projection node above the subgraph\nto create an orthographic projection.");
85       
86        position += delta;
87    }   
88
89    {
90        osgText::Text* text = new  osgText::Text;
91        geode->addDrawable( text );
92
93        text->setFont(timesFont);
94        text->setPosition(position);
95        text->setText("And add an osg::ModelViewMatrix set to ABSOLUTE to ensure\nit remains independent from any external model view matrices.");
96       
97        position += delta;
98    }   
99
100    // create the hud.
101    osg::MatrixTransform* modelview_abs = new osg::MatrixTransform;
102    modelview_abs->setReferenceFrame(osg::Transform::RELATIVE_TO_ABSOLUTE);
103    modelview_abs->setMatrix(osg::Matrix::identity());
104    modelview_abs->addChild(geode);
105
106    osg::Projection* projection = new osg::Projection;
107    projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024));
108    projection->addChild(modelview_abs);
109
110    return projection;
111
112}
113
114int main( int argc, char **argv )
115{
116
117    // use an ArgumentParser object to manage the program arguments.
118    osg::ArgumentParser arguments(&argc,argv);
119   
120    // set up the usage document, in case we need to print out how to use this program.
121    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates how to do Head Up Displays.");
122    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] [filename] ...");
123    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
124   
125
126    // construct the viewer.
127    osgProducer::Viewer viewer(arguments);
128
129    // set up the value with sensible default event handlers.
130    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
131
132    // get details on keyboard and mouse bindings used by the viewer.
133    viewer.getUsage(*arguments.getApplicationUsage());
134
135    // if user request help write it out to cout.
136    if (arguments.read("-h") || arguments.read("--help"))
137    {
138        arguments.getApplicationUsage()->write(std::cout);
139        return 1;
140    }
141
142    // any option left unread are converted into errors to write out later.
143    arguments.reportRemainingOptionsAsUnrecognized();
144
145    // report any errors if they have occured when parsing the program aguments.
146    if (arguments.errors())
147    {
148        arguments.writeErrorMessages(std::cout);
149        return 1;
150    }
151   
152    // read the scene from the list of file specified commandline args.
153    osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
154
155    osg::ref_ptr<osg::Group> group = dynamic_cast<osg::Group*>(scene.get());
156    if (!group)
157    {
158        group = new osg::Group;
159        group->addChild(scene.get());
160    }
161
162    // add the HUD subgraph.   
163    group->addChild(createHUD());
164
165    // set the scene to render
166    viewer.setSceneData(group.get());
167
168    // create the windows and run the threads.
169    viewer.realize();
170
171    while( !viewer.done() )
172    {
173        // wait for all cull and draw threads to complete.
174        viewer.sync();
175
176        // update the scene by traversing it with the the update visitor which will
177        // call all node update callbacks and animations.
178        viewer.update();
179         
180        // fire off the cull and draw traversals of the scene.
181        viewer.frame();
182       
183    }
184   
185    // wait for all cull and draw threads to complete before exit.
186    viewer.sync();
187   
188    return 0;
189}
Note: See TracBrowser for help on using the browser.