root/OpenSceneGraph/trunk/examples/osgcompositeviewer/osgcompositeviewer.cpp @ 13376

Revision 13376, 10.4 kB (checked in by robert, 7 hours ago)

Introduced new scheme for setting up which version of OpenGL/OpenGL ES the OSG is compiled for.

To select standard OpenGL 1/2 build with full backwards and forwards comtability use:

./configure
make

OR

./configure -DOPENGL_PROFILE=GL2

To select OpenGL 3 core profile build using GL3/gl3.h header:

./configure -DOPENGL_PROFILE=GL3

To select OpenGL Arb core profile build using GL/glcorearb.h header:

./configure -DOPENGL_PROFILE=GLCORE

To select OpenGL ES 1.1 profile use:

./configure -DOPENGL_PROFILE=GLES1

To select OpenGL ES 2 profile use:

./configure -DOPENGL_PROFILE=GLES2

Using OPENGL_PROFILE will select all the appropriate features required so no other settings in cmake will need to be adjusted.
The new configuration options are stored in the include/osg/OpenGL header that deprecates the old include/osg/GL header.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[6941]1/* OpenSceneGraph example, osgcompositeviewer.
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.
[4333]17*/
18
[9705]19#include <iostream>
20
[4333]21#include <osgUtil/Optimizer>
22#include <osgDB/ReadFile>
23
24#include <osg/Material>
25#include <osg/Geode>
26#include <osg/BlendFunc>
27#include <osg/Depth>
28#include <osg/Projection>
29#include <osg/PolygonOffset>
30#include <osg/MatrixTransform>
[5757]31#include <osg/Camera>
[4395]32#include <osg/FrontFace>
[4333]33
34#include <osgText/Text>
35
[5991]36#include <osgGA/TrackballManipulator>
37#include <osgGA/FlightManipulator>
[5992]38#include <osgGA/StateSetManipulator>
[7505]39#include <osgViewer/ViewerEventHandlers>
[4333]40
[5991]41#include <osgViewer/CompositeViewer>
[4333]42
[5998]43#include <osgFX/Scribe>
[5992]44
[5998]45#include <osg/io_utils>
46
47// class to handle events with a pick
48class PickHandler : public osgGA::GUIEventHandler {
[9564]49public:
[5998]50
51    PickHandler():
52        _mx(0.0f),
53        _my(0.0f) {}
[9564]54
[5998]55    ~PickHandler() {}
[9564]56
[13376]57    bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
[5998]58    {
59        osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
60        if (!view) return false;
[9564]61
[5998]62        switch(ea.getEventType())
63        {
64            case(osgGA::GUIEventAdapter::PUSH):
65            {
66                _mx = ea.getX();
67                _my = ea.getY();
68                break;
69            }
70            case(osgGA::GUIEventAdapter::RELEASE):
71            {
72                if (_mx==ea.getX() && _my==ea.getY())
73                {
[13376]74                    pick(view, ea);
[5998]75                }
76                break;
77            }
78            default:
79                break;
80        }
81        return false;
82    }
[9564]83
[13376]84    void pick(osgViewer::View* view, const osgGA::GUIEventAdapter& event)
[5998]85    {
86        osg::Node* node = 0;
87        osg::Group* parent = 0;
88
89        osgUtil::LineSegmentIntersector::Intersections intersections;
[13376]90        if (view->computeIntersections(event, intersections))
[5998]91        {
92            osgUtil::LineSegmentIntersector::Intersection intersection = *intersections.begin();
93            osg::NodePath& nodePath = intersection.nodePath;
94            node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
95            parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
[9564]96        }
[5998]97
98        // now we try to decorate the hit node by the osgFX::Scribe to show that its been "picked"
99        if (parent && node)
100        {
101            osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent);
102            if (!parentAsScribe)
103            {
104                // node not already picked, so highlight it with an osgFX::Scribe
105                osgFX::Scribe* scribe = new osgFX::Scribe();
106                scribe->addChild(node);
107                parent->replaceChild(node,scribe);
108            }
109            else
110            {
111                // node already picked so we want to remove scribe to unpick it.
112                osg::Node::ParentList parentList = parentAsScribe->getParents();
113                for(osg::Node::ParentList::iterator itr=parentList.begin();
114                    itr!=parentList.end();
115                    ++itr)
116                {
117                    (*itr)->replaceChild(parentAsScribe,node);
118                }
119            }
120        }
121
122    }
[9564]123
[5998]124    float _mx, _my;
125
126};
127
128
[4333]129int main( int argc, char **argv )
130{
131
132    // use an ArgumentParser object to manage the program arguments.
133    osg::ArgumentParser arguments(&argc,argv);
[9564]134
[4333]135    // read the scene from the list of file specified commandline args.
136    osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
137
[9705]138    if (!scene)
139    {
140        std::cout << argv[0] << ": requires filename argument." << std::endl;
141        return 1;
142    }
[5991]143
144    // construct the viewer.
[7536]145    osgViewer::CompositeViewer viewer(arguments);
[7505]146
[5991]147    if (arguments.read("-1"))
[5998]148    {
149        {
150            osgViewer::View* view = new osgViewer::View;
[9564]151            view->setName("Single view");
[12529]152            view->setSceneData(osgDB::readNodeFile("fountain.osgt"));
[9564]153
[7516]154            view->addEventHandler( new osgViewer::StatsHandler );
[9564]155
[5998]156            view->setUpViewAcrossAllScreens();
157            view->setCameraManipulator(new osgGA::TrackballManipulator);
158            viewer.addView(view);
159        }
160    }
161
162    if (arguments.read("-2"))
163    {
164
165        // view one
166        {
167            osgViewer::View* view = new osgViewer::View;
[9565]168            view->setName("View one");
[5998]169            viewer.addView(view);
170
171            view->setUpViewOnSingleScreen(0);
172            view->setSceneData(scene.get());
173            view->setCameraManipulator(new osgGA::TrackballManipulator);
174
175            // add the state manipulator
176            osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator;
177            statesetManipulator->setStateSet(view->getCamera()->getOrCreateStateSet());
178
179            view->addEventHandler( statesetManipulator.get() );
180        }
[9564]181
[5998]182        // view two
183        {
184            osgViewer::View* view = new osgViewer::View;
[9565]185            view->setName("View two");
[5998]186            viewer.addView(view);
187
188            view->setUpViewOnSingleScreen(1);
189            view->setSceneData(scene.get());
190            view->setCameraManipulator(new osgGA::TrackballManipulator);
[9564]191
[7516]192            view->addEventHandler( new osgViewer::StatsHandler );
[7505]193
[9564]194
[5998]195            // add the handler for doing the picking
196            view->addEventHandler(new PickHandler());
197        }
198    }
199
[9564]200
[5998]201    if (arguments.read("-3") || viewer.getNumViews()==0)
[9564]202    {
[5992]203
[5991]204        osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
[9564]205        if (!wsi)
[5991]206        {
207            osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
208            return 1;
209        }
[4333]210
[5991]211        unsigned int width, height;
212        wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
[4660]213
[5991]214        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
[5998]215        traits->x = 100;
216        traits->y = 100;
217        traits->width = 1000;
218        traits->height = 800;
[5991]219        traits->windowDecoration = true;
220        traits->doubleBuffer = true;
221        traits->sharedContext = 0;
[4660]222
[5991]223        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
224        if (gc.valid())
225        {
226            osg::notify(osg::INFO)<<"  GraphicsWindow has been created successfully."<<std::endl;
[4333]227
[5991]228            // need to ensure that the window is cleared make sure that the complete window is set the correct colour
229            // rather than just the parts of the window that are under the camera's viewports
230            gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f));
231            gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
232        }
233        else
234        {
235            osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl;
236        }
237
[5992]238        // view one
239        {
240            osgViewer::View* view = new osgViewer::View;
[9565]241            view->setName("View one");
[5998]242            viewer.addView(view);
243
[5992]244            view->setSceneData(scene.get());
[9565]245            view->getCamera()->setName("Cam one");
[5998]246            view->getCamera()->setViewport(new osg::Viewport(0,0, traits->width/2, traits->height/2));
[5992]247            view->getCamera()->setGraphicsContext(gc.get());
248            view->setCameraManipulator(new osgGA::TrackballManipulator);
[5991]249
[5992]250            // add the state manipulator
251            osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator;
252            statesetManipulator->setStateSet(view->getCamera()->getOrCreateStateSet());
253
254            view->addEventHandler( statesetManipulator.get() );
[9564]255
[7515]256            view->addEventHandler( new osgViewer::StatsHandler );
257            view->addEventHandler( new osgViewer::HelpHandler );
[7516]258            view->addEventHandler( new osgViewer::WindowSizeHandler );
259            view->addEventHandler( new osgViewer::ThreadingHandler );
260            view->addEventHandler( new osgViewer::RecordCameraPathHandler );
[5992]261        }
262
263        // view two
264        {
265            osgViewer::View* view = new osgViewer::View;
[9565]266            view->setName("View two");
[5998]267            viewer.addView(view);
268
[5992]269            view->setSceneData(scene.get());
[9565]270            view->getCamera()->setName("Cam two");
[5998]271            view->getCamera()->setViewport(new osg::Viewport(traits->width/2,0, traits->width/2, traits->height/2));
[5992]272            view->getCamera()->setGraphicsContext(gc.get());
273            view->setCameraManipulator(new osgGA::TrackballManipulator);
[9564]274
[5998]275            // add the handler for doing the picking
276            view->addEventHandler(new PickHandler());
[9564]277
[5992]278        }
279
[5999]280        // view three
[5992]281        {
282            osgViewer::View* view = new osgViewer::View;
[9565]283            view->setName("View three");
[5992]284            viewer.addView(view);
285
[12529]286            view->setSceneData(osgDB::readNodeFile("cessnafire.osgt"));
[5998]287
[9565]288            view->getCamera()->setName("Cam three");
[5998]289            view->getCamera()->setProjectionMatrixAsPerspective(30.0, double(traits->width) / double(traits->height/2), 1.0, 1000.0);
290            view->getCamera()->setViewport(new osg::Viewport(0, traits->height/2, traits->width, traits->height/2));
[5992]291            view->getCamera()->setGraphicsContext(gc.get());
292            view->setCameraManipulator(new osgGA::TrackballManipulator);
293        }
294
295    }
[7515]296
[9564]297
[5991]298    while (arguments.read("-s")) { viewer.setThreadingModel(osgViewer::CompositeViewer::SingleThreaded); }
[7536]299    while (arguments.read("-g")) { viewer.setThreadingModel(osgViewer::CompositeViewer::CullDrawThreadPerContext); }
300    while (arguments.read("-c")) { viewer.setThreadingModel(osgViewer::CompositeViewer::CullThreadPerCameraDrawThreadPerContext); }
[9564]301
[5999]302     // run the viewer's main frame loop
303     return viewer.run();
[4333]304}
Note: See TracBrowser for help on using the browser.