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

Revision 1938, 7.3 kB (checked in by robert, 11 years ago)

Changed the CullVisitor::popProjection() so that it doesn't adjust orthorgraphic
projection matrices.

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