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

Revision 1832, 5.6 kB (checked in by robert, 11 years ago)

Changed the Viewer::realize() calls across to not using the threading paramter
leaving it up to the Viewer to specify the mode (which by default is MultiThreaded?).
Added a check for the presence of osgParticle systems so that threading is
disabled in this case.

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