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

Revision 1622, 4.7 kB (checked in by robert, 11 years ago)

Ported osgstereoimage to use osgProducer, added it to the examples directory.

Added a osgProducer::CameraGroup::getSceneHanderList() methods.

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