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

Revision 6941, 8.9 kB (checked in by robert, 7 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osghud.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osgUtil/Optimizer>
20#include <osgDB/ReadFile>
21
22#include <osgViewer/Viewer>
23#include <osgViewer/CompositeViewer>
24
25#include <osgGA/TrackballManipulator>
26
27#include <osg/Material>
28#include <osg/Geode>
29#include <osg/BlendFunc>
30#include <osg/Depth>
31#include <osg/PolygonOffset>
32#include <osg/MatrixTransform>
33#include <osg/Camera>
34
35#include <osgText/Text>
36
37
38osg::Camera* createHUD()
39{
40    // create a camera to set up the projection and model view matrices, and the subgraph to drawn in the HUD
41    osg::Camera* camera = new osg::Camera;
42
43    // set the projection matrix
44    camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1280,0,1024));
45
46    // set the view matrix   
47    camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
48    camera->setViewMatrix(osg::Matrix::identity());
49
50    // only clear the depth buffer
51    camera->setClearMask(GL_DEPTH_BUFFER_BIT);
52
53    // draw subgraph after main camera view.
54    camera->setRenderOrder(osg::Camera::POST_RENDER);
55
56    // we don't want the camera to grab event focus from the viewers main camera(s).
57    camera->setAllowEventFocus(false);
58   
59
60
61    // add to this camera a subgraph to render
62    {
63
64        osg::Geode* geode = new osg::Geode();
65
66        std::string timesFont("fonts/arial.ttf");
67
68        // turn lighting off for the text and disable depth test to ensure its always ontop.
69        osg::StateSet* stateset = geode->getOrCreateStateSet();
70        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
71
72        osg::Vec3 position(150.0f,800.0f,0.0f);
73        osg::Vec3 delta(0.0f,-120.0f,0.0f);
74
75        {
76            osgText::Text* text = new  osgText::Text;
77            geode->addDrawable( text );
78
79            text->setFont(timesFont);
80            text->setPosition(position);
81            text->setText("Head Up Displays are simple :-)");
82
83            position += delta;
84        }   
85
86
87        {
88            osgText::Text* text = new  osgText::Text;
89            geode->addDrawable( text );
90
91            text->setFont(timesFont);
92            text->setPosition(position);
93            text->setText("All you need to do is create your text in a subgraph.");
94
95            position += delta;
96        }   
97
98
99        {
100            osgText::Text* text = new  osgText::Text;
101            geode->addDrawable( text );
102
103            text->setFont(timesFont);
104            text->setPosition(position);
105            text->setText("Then place an osg::Camera above the subgraph\n"
106                          "to create an orthographic projection.\n");
107
108            position += delta;
109        }
110
111        {
112            osgText::Text* text = new  osgText::Text;
113            geode->addDrawable( text );
114
115            text->setFont(timesFont);
116            text->setPosition(position);
117            text->setText("Set the Camera's ReferenceFrame to ABSOLUTE_RF to ensure\n"
118                          "it remains independent from any external model view matrices.");
119
120            position += delta;
121        }
122
123        {
124            osgText::Text* text = new  osgText::Text;
125            geode->addDrawable( text );
126
127            text->setFont(timesFont);
128            text->setPosition(position);
129            text->setText("And set the Camera's clear mask to just clear the depth buffer.");
130
131            position += delta;
132        }   
133
134        {
135            osgText::Text* text = new  osgText::Text;
136            geode->addDrawable( text );
137
138            text->setFont(timesFont);
139            text->setPosition(position);
140            text->setText("And finally set the Camera's RenderOrder to POST_RENDER\n"
141                          "to make sure its drawn last.");
142
143            position += delta;
144        }   
145
146
147        {
148            osg::BoundingBox bb;
149            for(unsigned int i=0;i<geode->getNumDrawables();++i)
150            {
151                bb.expandBy(geode->getDrawable(i)->getBound());
152            }
153
154            osg::Geometry* geom = new osg::Geometry;
155
156            osg::Vec3Array* vertices = new osg::Vec3Array;
157            float depth = bb.zMin()-0.1;
158            vertices->push_back(osg::Vec3(bb.xMin(),bb.yMax(),depth));
159            vertices->push_back(osg::Vec3(bb.xMin(),bb.yMin(),depth));
160            vertices->push_back(osg::Vec3(bb.xMax(),bb.yMin(),depth));
161            vertices->push_back(osg::Vec3(bb.xMax(),bb.yMax(),depth));
162            geom->setVertexArray(vertices);
163
164            osg::Vec3Array* normals = new osg::Vec3Array;
165            normals->push_back(osg::Vec3(0.0f,0.0f,1.0f));
166            geom->setNormalArray(normals);
167            geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
168
169            osg::Vec4Array* colors = new osg::Vec4Array;
170            colors->push_back(osg::Vec4(1.0f,1.0,0.8f,0.2f));
171            geom->setColorArray(colors);
172            geom->setColorBinding(osg::Geometry::BIND_OVERALL);
173
174            geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4));
175
176            osg::StateSet* stateset = geom->getOrCreateStateSet();
177            stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
178            //stateset->setAttribute(new osg::PolygonOffset(1.0f,1.0f),osg::StateAttribute::ON);
179            stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
180
181            geode->addDrawable(geom);
182        }
183
184        camera->addChild(geode);
185    }
186
187    return camera;
188}
189
190int main( int argc, char **argv )
191{
192    // use an ArgumentParser object to manage the program arguments.
193    osg::ArgumentParser arguments(&argc,argv);
194
195
196    // read the scene from the list of file specified commandline args.
197    osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
198   
199    // if not loaded assume no arguments passed in, try use default mode instead.
200    if (!scene) scene = osgDB::readNodeFile("dumptruck.osg");
201   
202   
203    if (!scene)
204    {
205        osg::notify(osg::NOTICE)<<"No model loaded"<<std::endl;
206        return 1;
207    }
208
209
210    if (arguments.read("--Viewer"))
211    {
212        // construct the viewer.
213        osgViewer::Viewer viewer;
214
215        // create a HUD as slave camera attached to the master view.
216       
217        viewer.setUpViewAcrossAllScreens();
218
219        osgViewer::Viewer::Windows windows;
220        viewer.getWindows(windows);
221       
222        if (windows.empty()) return 1;
223       
224        osg::Camera* hudCamera = createHUD();
225       
226        // set up cameras to rendering on the first window available.
227        hudCamera->setGraphicsContext(windows[0]);
228        hudCamera->setViewport(0,0,windows[0]->getTraits()->width, windows[0]->getTraits()->height);
229
230        viewer.addSlave(hudCamera, false);
231
232        // set the scene to render
233        viewer.setSceneData(scene.get());
234
235        return viewer.run();
236
237    }
238    if (arguments.read("--CompositeViewer"))
239    {
240        // construct the viewer.
241        osgViewer::CompositeViewer viewer;
242
243        // create the main 3D view
244        osgViewer::View* view = new osgViewer::View;
245        viewer.addView(view);
246
247        view->setSceneData(scene.get());
248        view->setUpViewAcrossAllScreens();;
249        view->setCameraManipulator(new osgGA::TrackballManipulator);
250
251        // now create the HUD camera's view
252       
253        osgViewer::Viewer::Windows windows;
254        viewer.getWindows(windows);
255       
256        if (windows.empty()) return 1;
257       
258        osg::Camera* hudCamera = createHUD();
259       
260        // set up cameras to rendering on the first window available.
261        hudCamera->setGraphicsContext(windows[0]);
262        hudCamera->setViewport(0,0,windows[0]->getTraits()->width, windows[0]->getTraits()->height);
263       
264        osgViewer::View* hudView = new osgViewer::View;
265        hudView->setCamera(hudCamera);
266       
267        viewer.addView(hudView);
268
269        return viewer.run();
270
271    }
272    else
273    {
274        // construct the viewer.
275        osgViewer::Viewer viewer;
276
277        osg::ref_ptr<osg::Group> group  = new osg::Group;
278
279        // add the HUD subgraph.   
280        if (scene.valid()) group->addChild(scene.get());
281        group->addChild(createHUD());
282
283        // set the scene to render
284        viewer.setSceneData(group.get());
285
286        return viewer.run();
287    }
288   
289}
Note: See TracBrowser for help on using the browser.