root/OpenSceneGraph/trunk/examples/osgstereoimage/osgstereoimage.cpp @ 1728

Revision 1728, 4.6 kB (checked in by robert, 12 years ago)

Updates to osgProducer and osgstereoimage for stereo support.

Changed the default distance from the scene of the home position of the
trackball and flight manipulators.

  • 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 <osgProducer/Viewer>
15#include <osgDB/ReadFile>
16#include <osgUtil/Optimizer>
17
18#include <osg/Geode>
19#include <osg/Notify>
20#include <osg/MatrixTransform>
21
22
23int main( int argc, char **argv )
24{
25
26    // use an ArgumentParser object to manage the program arguments.
27    osg::ArgumentParser arguments(&argc,argv);
28   
29    // set up the usage document, in case we need to print out how to use this program.
30    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getProgramName()+" [options] image_file_left_eye image_file_right_eye");
31    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
32   
33
34    // construct the viewer.
35    osgProducer::Viewer viewer(arguments);
36
37    // set up the value with sensible default event handlers.
38    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
39
40    // get details on keyboard and mouse bindings used by the viewer.
41    viewer.getUsage(*arguments.getApplicationUsage());
42
43    // if user request help write it out to cout.
44    if (arguments.read("-h") || arguments.read("--help"))
45    {
46        arguments.getApplicationUsage()->write(std::cout);
47        return 1;
48    }
49
50    // any option left unread are converted into errors to write out later.
51    arguments.reportRemainingOptionsAsUnrecognized();
52
53    // report any errors if they have occured when parsing the program aguments.
54    if (arguments.errors())
55    {
56        arguments.writeErrorMessages(std::cout);
57        return 1;
58    }
59
60    // extract the filenames from the arguments list.
61    std::string fileLeft,fileRight;
62    for(int pos=1;pos<arguments.argc();++pos)
63    {
64        if (arguments.isString(pos))
65        {
66            if (fileLeft.empty()) fileLeft = arguments[pos];
67            else if (fileRight.empty()) fileRight = arguments[pos];
68        }
69    }
70
71
72    if (fileLeft.empty() || fileRight.empty())
73    {
74        std::cout << arguments.getProgramName() <<": Please specify two image files required for stereo imaging."<<std::endl;
75        return 1;
76    }
77   
78    osg::Image* imageLeft = osgDB::readImageFile(fileLeft);
79    osg::Image* imageRight = osgDB::readImageFile(fileRight);
80   
81    if (!imageLeft || !imageRight)
82    {   
83        std::cout << arguments.getProgramName() <<": Unable to load two image files required for stereo imaging."<<std::endl;
84        return 1;
85    }
86   
87    float average_s = (imageLeft->s()+imageRight->s())*0.5f;
88    float average_t = (imageLeft->t()+imageRight->t())*0.5f;
89
90    osg::Geode* geodeLeft = osg::createGeodeForImage(imageLeft,average_s,average_t);
91    geodeLeft->setNodeMask(0x01);
92
93    osg::Geode* geodeRight = osg::createGeodeForImage(imageRight,average_s,average_t);
94    geodeRight->setNodeMask(0x02);
95
96    osg::ref_ptr<osg::Group> rootNode = new osg::Group;
97    rootNode->addChild(geodeLeft);
98    rootNode->addChild(geodeRight);
99
100    // set the scene to render
101    viewer.setSceneData(rootNode.get());
102
103    // set up the use of stereo by default.
104    osg::DisplaySettings* ds = viewer.getDisplaySettings();
105    if (!ds) ds = osg::DisplaySettings::instance();
106    if (ds) ds->setStereo(true);
107
108    // create the windows and run the threads.
109    viewer.realize(Producer::CameraGroup::ThreadPerCamera);
110
111    // set all the sceneview's up so that their left and right add cull masks are set up.
112    for(osgProducer::OsgCameraGroup::SceneHandlerList::iterator itr=viewer.getSceneHandlerList().begin();
113        itr!=viewer.getSceneHandlerList().end();
114        ++itr)
115    {
116        osgUtil::SceneView* sceneview = itr->get();
117        sceneview->setCullMask(0xffffffff);
118        sceneview->setCullMaskLeft(0x00000001);
119        sceneview->setCullMaskRight(0x00000002);
120    }
121
122
123    while( !viewer.done() )
124    {
125        // wait for all cull and draw threads to complete.
126        viewer.sync();
127
128        // update the scene by traversing it with the the update visitor which will
129        // call all node update callbacks and animations.
130        viewer.update();
131         
132        // fire off the cull and draw traversals of the scene.
133        viewer.frame();
134       
135    }
136   
137    return 0;
138}
Note: See TracBrowser for help on using the browser.