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

Revision 5757, 7.7 kB (checked in by robert, 7 years ago)

Renamed osg::CameraNode? to osg::Camera, cleaned up osg::View.

Added beginnings of new osgViewer::Scene,View,Viewer,CompositeViewer? and GraphicsWindowProxy? files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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/PolygonOffset>
23#include <osg/MatrixTransform>
24#include <osg/Camera>
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
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->setPosition(position);
60        text->setText("All you need to do is create your text in a subgraph.");
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->setPosition(position);
72        text->setText("Then place an osg::Camera above the subgraph\n"
73                      "to create an orthographic projection.\n");
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("Set the Camera's ReferenceFrame to ABSOLUTE_RF to ensure\n"
85                      "it remains independent from any external model view matrices.");
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 set the Camera's clear mask to just clear the depth buffer.");
97       
98        position += delta;
99    }   
100
101    {
102        osgText::Text* text = new  osgText::Text;
103        geode->addDrawable( text );
104
105        text->setFont(timesFont);
106        text->setPosition(position);
107        text->setText("And finally set the Camera's RenderOrder to POST_RENDER\n"
108                      "to make sure its drawn last.");
109       
110        position += delta;
111    }   
112   
113   
114    {
115        osg::BoundingBox bb;
116        for(unsigned int i=0;i<geode->getNumDrawables();++i)
117        {
118            bb.expandBy(geode->getDrawable(i)->getBound());
119        }
120
121        osg::Geometry* geom = new osg::Geometry;
122
123        osg::Vec3Array* vertices = new osg::Vec3Array;
124        float depth = bb.zMin()-0.1;
125        vertices->push_back(osg::Vec3(bb.xMin(),bb.yMax(),depth));
126        vertices->push_back(osg::Vec3(bb.xMin(),bb.yMin(),depth));
127        vertices->push_back(osg::Vec3(bb.xMax(),bb.yMin(),depth));
128        vertices->push_back(osg::Vec3(bb.xMax(),bb.yMax(),depth));
129        geom->setVertexArray(vertices);
130
131        osg::Vec3Array* normals = new osg::Vec3Array;
132        normals->push_back(osg::Vec3(0.0f,0.0f,1.0f));
133        geom->setNormalArray(normals);
134        geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
135
136        osg::Vec4Array* colors = new osg::Vec4Array;
137        colors->push_back(osg::Vec4(1.0f,1.0,0.8f,0.2f));
138        geom->setColorArray(colors);
139        geom->setColorBinding(osg::Geometry::BIND_OVERALL);
140
141        geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4));
142       
143        osg::StateSet* stateset = geom->getOrCreateStateSet();
144        stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
145        //stateset->setAttribute(new osg::PolygonOffset(1.0f,1.0f),osg::StateAttribute::ON);
146        stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
147
148        geode->addDrawable(geom);
149    }
150
151    osg::Camera* camera = new osg::Camera;
152
153    // set the projection matrix
154    camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1280,0,1024));
155
156    // set the view matrix   
157    camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
158    camera->setViewMatrix(osg::Matrix::identity());
159
160    // only clear the depth buffer
161    camera->setClearMask(GL_DEPTH_BUFFER_BIT);
162
163    // draw subgraph after main camera view.
164    camera->setRenderOrder(osg::Camera::POST_RENDER);
165
166    camera->addChild(geode);
167   
168    return camera;
169
170}
171
172int main( int argc, char **argv )
173{
174
175    // use an ArgumentParser object to manage the program arguments.
176    osg::ArgumentParser arguments(&argc,argv);
177   
178    // set up the usage document, in case we need to print out how to use this program.
179    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates how to do Head Up Displays.");
180    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] [filename] ...");
181    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
182   
183
184    // construct the viewer.
185    osgProducer::Viewer viewer(arguments);
186
187    // set up the value with sensible default event handlers.
188    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
189
190    // get details on keyboard and mouse bindings used by the viewer.
191    viewer.getUsage(*arguments.getApplicationUsage());
192
193    // if user request help write it out to cout.
194    if (arguments.read("-h") || arguments.read("--help"))
195    {
196        arguments.getApplicationUsage()->write(std::cout);
197        return 1;
198    }
199
200    // any option left unread are converted into errors to write out later.
201    arguments.reportRemainingOptionsAsUnrecognized();
202
203    // report any errors if they have occured when parsing the program aguments.
204    if (arguments.errors())
205    {
206        arguments.writeErrorMessages(std::cout);
207        return 1;
208    }
209   
210    // read the scene from the list of file specified commandline args.
211    osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
212
213    osg::ref_ptr<osg::Group> group  = new osg::Group;
214   
215    // add the HUD subgraph.   
216    if (scene.valid()) group->addChild(scene.get());
217    group->addChild(createHUD());
218
219    // set the scene to render
220    viewer.setSceneData(group.get());
221
222    // create the windows and run the threads.
223    viewer.realize();
224
225    while( !viewer.done() )
226    {
227        // wait for all cull and draw threads to complete.
228        viewer.sync();
229
230        // update the scene by traversing it with the the update visitor which will
231        // call all node update callbacks and animations.
232        viewer.update();
233         
234        // fire off the cull and draw traversals of the scene.
235        viewer.frame();
236       
237    }
238   
239    // wait for all cull and draw threads to complete.
240    viewer.sync();
241
242    // run a clean up frame to delete all OpenGL objects.
243    viewer.cleanup_frame();
244
245    // wait for all the clean up frame to complete.
246    viewer.sync();
247   
248    return 0;
249}
Note: See TracBrowser for help on using the browser.