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

Revision 4386, 7.2 kB (checked in by robert, 9 years ago)

Cleaned up osghud and osgprerender examples

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