root/OpenSceneGraph/trunk/examples/osgsidebyside/osgsidebyside.cpp @ 12529

Revision 12529, 10.2 kB (checked in by robert, 4 years ago)

Replaced .osg with .osgt file usage

  • Property svn:eol-style set to native
Line 
1/* OpenSceneGraph example, osgcamera.
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
20#include <osgDB/ReadFile>
21#include <osgDB/WriteFile>
22#include <osgViewer/Viewer>
23#include <osgViewer/ViewerEventHandlers>
24#include <osgGA/TrackballManipulator>
25#include <osgGA/FlightManipulator>
26#include <osgGA/AnimationPathManipulator>
27#include <osgGA/GUIEventHandler>
28#include <osgGA/StateSetManipulator>
29#include <osg/PolygonMode>
30#include <osgUtil/Optimizer>
31#include <osg/Depth>
32#include <iostream>
33#include <osg/Switch>
34#include <osgSim/MultiSwitch>
35#include <osgSim/DOFTransform>
36
37#include <osg/AlphaFunc>
38#include <osg/BlendFunc>
39
40using namespace osg;
41using namespace osgGA;
42
43
44class SwitchDOFVisitor : public osg::NodeVisitor, public osgGA::GUIEventHandler
45{
46public:
47    SwitchDOFVisitor():
48      osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
49    {
50    }
51
52    virtual void apply(Group& node)
53    {
54        osgSim::MultiSwitch* pMSwitch = dynamic_cast<osgSim::MultiSwitch*>(&node);
55       
56        if (pMSwitch)
57        {
58            mSwitches.push_back(pMSwitch);
59        }
60
61        osg::NodeVisitor::apply(node);
62    }
63   
64    virtual void apply(Transform& node)
65    {
66        osgSim::DOFTransform* pDof = dynamic_cast<osgSim::DOFTransform*>(&node);
67       
68        if (pDof)
69        {
70            mDofs.push_back(pDof);
71            pDof->setAnimationOn(true);
72        }
73
74        osg::NodeVisitor::apply(node);
75    }
76
77    void nextSwitch()
78    {
79        for (size_t i=0; i < mSwitches.size(); i++)
80        {
81            if (mSwitches[i]->getSwitchSetList().size() > 1)
82            {
83                // Toggle through switchsets
84                unsigned int nextSwitchSet = mSwitches[i]->getActiveSwitchSet();
85                nextSwitchSet++;
86                if (nextSwitchSet >= mSwitches[i]->getSwitchSetList().size())
87                    nextSwitchSet = 0;
88                mSwitches[i]->setActiveSwitchSet(nextSwitchSet);
89            }
90            else if (mSwitches[i]->getSwitchSetList().size() == 1)
91            {
92                // If we have only one switchset, toggle values within this switchset
93                osgSim::MultiSwitch::ValueList values = mSwitches[i]->getValueList(0);
94                for (size_t j=0; j < values.size(); j++)
95                {
96                    if (values[j])
97                    {
98                        unsigned int nextValue = j+1;
99                        if (nextValue >= values.size())
100                            nextValue = 0;
101                        mSwitches[i]->setSingleChildOn(0, nextValue);
102                    }
103                }
104            }
105        }
106    }
107
108    void multiplyAnimation(float scale)
109    {
110        for (size_t i=0; i < mDofs.size(); i++)
111        {
112            mDofs[i]->setIncrementHPR(mDofs[i]->getIncrementHPR() * scale);
113            mDofs[i]->setIncrementScale(mDofs[i]->getIncrementScale() * scale);
114            mDofs[i]->setIncrementTranslate(mDofs[i]->getIncrementTranslate() * scale);
115        }
116    }
117
118    bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
119    {
120        osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
121        if (!viewer) return false;
122
123        if (ea.getHandled()) return false;
124
125        if(ea.getEventType()==GUIEventAdapter::KEYDOWN)
126        {
127
128            switch( ea.getKey() )
129            {
130                case osgGA::GUIEventAdapter::KEY_Right:
131                    // Toggle next switch
132                    nextSwitch();
133                    return true;
134                    break;
135                case osgGA::GUIEventAdapter::KEY_Up:
136                    // Increase animation speed
137                    multiplyAnimation(2);
138                    return true;
139                    break;
140                case osgGA::GUIEventAdapter::KEY_Down:
141                    // Decrease animation speed
142                    multiplyAnimation(0.5);
143                    return true;
144                    break;
145            }
146        }
147        return false;
148    }
149
150private:
151    std::vector<osgSim::MultiSwitch*> mSwitches;
152    std::vector<osgSim::DOFTransform*> mDofs;
153};
154
155void singleWindowSideBySideCameras(osgViewer::Viewer& viewer)
156{
157    osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
158    if (!wsi)
159    {
160        osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
161        return;
162    }
163   
164    unsigned int width, height;
165    wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
166
167
168
169    // Not fullscreen
170    width /= 2;
171    height /= 2;
172
173    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
174    traits->x = 100;
175    traits->y = 100;
176    traits->width = width;
177    traits->height = height;
178    traits->windowDecoration = true;
179    traits->doubleBuffer = true;
180    traits->sharedContext = 0;
181
182    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
183    if (gc.valid())
184    {
185        osg::notify(osg::INFO)<<"  GraphicsWindow has been created successfully."<<std::endl;
186
187        // need to ensure that the window is cleared make sure that the complete window is set the correct colour
188        // rather than just the parts of the window that are under the camera's viewports
189        gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f));
190        gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
191    }
192    else
193    {
194        osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl;
195    }
196
197
198    osg::Camera* master = viewer.getCamera();
199
200    // get the default settings for the camera
201    double fovy, aspectRatio, zNear, zFar;
202    master->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, zFar);
203
204    // reset this for the actual apsect ratio of out created window
205    double windowAspectRatio = double(width)/double(height);
206    master->setProjectionMatrixAsPerspective(fovy, windowAspectRatio, 1.0, 10000.0);
207
208    master->setName("MasterCam");
209
210    osg::ref_ptr<osg::Camera> camera = new osg::Camera;
211    camera->setCullMask(1);
212    camera->setName("Left");
213    camera->setGraphicsContext(gc.get());
214    camera->setViewport(new osg::Viewport(0, 0, width/2, height));
215    GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
216    camera->setDrawBuffer(buffer);
217    camera->setReadBuffer(buffer);
218    viewer.addSlave(camera.get(), osg::Matrixd::scale(1.0,0.5,1.0), osg::Matrixd());
219
220    camera = new osg::Camera;
221    camera->setCullMask(2);
222    camera->setName("Right");
223    camera->setGraphicsContext(gc.get());
224    camera->setViewport(new osg::Viewport(width/2, 0, width/2, height));
225    buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
226    camera->setDrawBuffer(buffer);
227    camera->setReadBuffer(buffer);
228    viewer.addSlave(camera.get(), osg::Matrixd::scale(1.0,0.5,1.0), osg::Matrixd());
229}
230
231int main( int argc, char **argv )
232{
233    // use an ArgumentParser object to manage the program arguments.
234    osg::ArgumentParser arguments(&argc,argv);
235
236    if (argc<2)
237    {
238        std::cout << argv[0] <<": requires filename argument." << std::endl;
239        return 1;
240    }
241
242    osgViewer::Viewer viewer(arguments);
243   
244    std::string outputfile("output.osgt");
245    while (arguments.read("-o",outputfile)) {}
246
247    while (arguments.read("-s")) { viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); }
248    while (arguments.read("-g")) { viewer.setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext); }
249    while (arguments.read("-d")) { viewer.setThreadingModel(osgViewer::Viewer::DrawThreadPerContext); }
250    while (arguments.read("-c")) { viewer.setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext); }
251   
252    singleWindowSideBySideCameras(viewer);
253
254    viewer.setCameraManipulator( new osgGA::TrackballManipulator() );
255    viewer.addEventHandler(new osgViewer::StatsHandler);
256    viewer.addEventHandler(new osgViewer::ThreadingHandler);
257    viewer.addEventHandler(new osgViewer::WindowSizeHandler());
258    viewer.addEventHandler(new osgViewer::LODScaleHandler());
259    viewer.addEventHandler(new osgGA::StateSetManipulator());
260
261    SwitchDOFVisitor* visit = new SwitchDOFVisitor;
262    viewer.addEventHandler(visit);
263   
264    osg::ref_ptr<osg::Node> loadedModel;
265    // load the scene.
266    loadedModel = osgDB::readNodeFiles(arguments);
267
268    if (!loadedModel)
269    {
270        std::cout << argv[0] <<": No data loaded." << std::endl;
271        return 1;
272    }
273
274    osg::Group* group = new osg::Group;
275   
276    osg::Group* group1 = new osg::Group;
277    group1->addChild(loadedModel.get());
278    group1->setNodeMask(1);
279
280    // Uncomment these lines if you like to compare the loaded model to the resulting model in a merge/diff tool
281    //osgDB::writeNodeFile(*loadedModel.get(), "dummy1.osgt");
282
283    osgDB::writeNodeFile(*loadedModel.get(), outputfile);
284    osg::ref_ptr<osg::Node> convertedModel = osgDB::readNodeFile(outputfile);
285
286    //osgDB::writeNodeFile(*convertedModel.get(), "dummy2.osgt");
287
288    osg::Group* group2 = new osg::Group;
289    group2->addChild(convertedModel.get());
290    group2->setNodeMask(2);
291
292    // Activate DOF animations and collect switches
293    loadedModel->accept(*visit);
294    convertedModel->accept(*visit);
295
296    group->addChild(group1);
297    group->addChild(group2);
298
299    viewer.setSceneData(group);
300
301    viewer.setThreadingModel(osgViewer::Viewer::DrawThreadPerContext);
302    viewer.realize();
303
304    viewer.run();
305
306    return 0;
307}
Note: See TracBrowser for help on using the browser.