root/OpenSceneGraph/trunk/examples/osgstereomatch/osgstereomatch.cpp @ 13574

Revision 13574, 7.5 kB (checked in by robert, 4 days ago)

Improved support for controlling the ShadingModel? via the VolumeSettings? object

  • Property svn:eol-style set to native
Line 
1/* OpenSceneGraph example, osgstereomatch.
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 <osg/Vec3>
20#include <osg/Vec4>
21#include <osg/Quat>
22#include <osg/Matrix>
23#include <osg/ShapeDrawable>
24#include <osg/Geometry>
25#include <osg/Geode>
26#include <osg/TextureRectangle>
27
28#include <osgDB/FileUtils>
29#include <osgDB/ReadFile>
30
31#include <osgViewer/Viewer>
32#include <osgViewer/ViewerEventHandlers>
33
34#include <iostream>
35
36#include "StereoPass.h"
37#include "StereoMultipass.h"
38
39osg::Node* createScene(osg::Image *left, osg::Image *right, unsigned int min_disp, unsigned int max_disp, unsigned int window_size, bool single_pass)
40{
41    int width = left->s();
42    int height = left->t();
43
44    osg::Group* topnode = new osg::Group;
45
46    // create four quads so we can display up to four images
47
48    osg::ref_ptr<osg::Geode> geode = new osg::Geode();
49
50    // each geom will contain a quad
51    osg::ref_ptr<osg::DrawArrays> da = new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4);
52
53    osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
54    colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
55
56    osg::ref_ptr<osg::Vec2Array> tcoords = new osg::Vec2Array; // texture coords
57    tcoords->push_back(osg::Vec2(0, 0));
58    tcoords->push_back(osg::Vec2(width, 0));
59    tcoords->push_back(osg::Vec2(width, height));
60    tcoords->push_back(osg::Vec2(0, height));
61
62    osg::ref_ptr<osg::StateSet> geomss[4]; // stateset where we can attach textures
63    osg::ref_ptr<osg::TextureRectangle> texture[4];
64
65    for (int i=0;i<4;i++) {
66        osg::ref_ptr<osg::Vec3Array> vcoords = new osg::Vec3Array; // vertex coords
67        osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
68
69        // tile the quads on the screen
70        // 2 3
71        // 0 1
72        int xoff, zoff;
73        xoff = (i%2);
74        zoff = i>1 ? 1 : 0;
75
76        // initial viewer camera looks along y
77        vcoords->push_back(osg::Vec3d(0+(xoff * width), 0, 0+(zoff * height)));
78        vcoords->push_back(osg::Vec3d(width+(xoff * width), 0, 0+(zoff * height)));
79        vcoords->push_back(osg::Vec3d(width+(xoff * width), 0, height+(zoff * height)));
80        vcoords->push_back(osg::Vec3d(0+(xoff * width), 0, height+(zoff * height)));
81
82        geom->setVertexArray(vcoords.get());
83        geom->setTexCoordArray(0,tcoords.get());
84        geom->addPrimitiveSet(da.get());
85        geom->setColorArray(colors.get(), osg::Array::BIND_OVERALL);
86        geomss[i] = geom->getOrCreateStateSet();
87        geomss[i]->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
88
89        texture[i] = new osg::TextureRectangle;
90        texture[i]->setResizeNonPowerOfTwoHint(false);
91        texture[i]->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
92        texture[i]->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
93
94        geode->addDrawable(geom.get());
95    }
96
97    // attach the input images to the bottom textures of the view
98    texture[0]->setImage(left);
99    texture[1]->setImage(right);
100    geomss[0]->setTextureAttributeAndModes(0, texture[0].get(), osg::StateAttribute::ON);
101    geomss[1]->setTextureAttributeAndModes(0, texture[1].get(), osg::StateAttribute::ON);
102
103    topnode->addChild(geode.get());
104
105    // create the processing passes
106    if (single_pass) {
107        StereoPass *stereopass = new StereoPass(texture[0].get(), texture[1].get(),
108                                                width, height,
109                                                min_disp, max_disp, window_size);
110
111        topnode->addChild(stereopass->getRoot().get());
112
113        // attach the output of the processing to the top left geom
114        geomss[2]->setTextureAttributeAndModes(0,
115                                               stereopass->getOutputTexture().get(),
116                                               osg::StateAttribute::ON);
117    } else {
118        StereoMultipass *stereomp = new StereoMultipass(texture[0].get(), texture[1].get(),
119                                                width, height,
120                                                min_disp, max_disp, window_size);
121        topnode->addChild(stereomp->getRoot().get());
122        // attach the output of the processing to the top left geom
123        geomss[2]->setTextureAttributeAndModes(0,
124                                               stereomp->getOutputTexture().get(),
125                                               osg::StateAttribute::ON);
126    }
127
128    return topnode;
129}
130
131int main(int argc, char *argv[])
132{
133    // use an ArgumentParser object to manage the program arguments.
134    osg::ArgumentParser arguments(&argc,argv);
135
136    // set up the usage document, in case we need to print out how to use this program.
137    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates a stereo matching algorithm. It uses multiple render targets and multiple passes with texture ping-pong.");
138    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] --left left_image --right right_image --min min_disparity --max max_disparity --window window_size");
139    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
140    arguments.getApplicationUsage()->addCommandLineOption("--left","The left image of the stereo pair to load.");
141    arguments.getApplicationUsage()->addCommandLineOption("--right","The right image of the stereo pair to load.");
142    arguments.getApplicationUsage()->addCommandLineOption("--min","The minimum disparity to start matching pixels.");
143    arguments.getApplicationUsage()->addCommandLineOption("--max","The maximum disparity to stop matching pixels.");
144    arguments.getApplicationUsage()->addCommandLineOption("--window","The window size used to match areas around pixels.");
145    arguments.getApplicationUsage()->addCommandLineOption("--single","Use a single pass instead on multiple passes.");
146
147    // if user request help write it out to cout.
148    if (arguments.read("-h") || arguments.read("--help"))
149    {
150        arguments.getApplicationUsage()->write(std::cout);
151        return 1;
152    }
153
154    std::string leftName("");
155    while(arguments.read("--left", leftName)) {}
156
157    std::string rightName("");
158    while(arguments.read("--right", rightName)) {}
159
160    unsigned int minDisparity = 0;
161    while (arguments.read("--min", minDisparity)) {}
162
163    unsigned int maxDisparity = 31;
164    while (arguments.read("--max", maxDisparity)) {}
165
166    unsigned int windowSize = 5;
167    while (arguments.read("--window", windowSize)) {}
168
169    bool useSinglePass = false;
170    while (arguments.read("--single")) { useSinglePass = true; }
171
172    if (leftName == "" || rightName=="") {
173        arguments.getApplicationUsage()->write(std::cout);
174        return 1;
175    }
176
177    // load the images
178    osg::ref_ptr<osg::Image> leftIm = osgDB::readImageFile(leftName);
179    osg::ref_ptr<osg::Image> rightIm = osgDB::readImageFile(rightName);
180
181    osg::Node* scene = createScene(leftIm.get(), rightIm.get(), minDisparity, maxDisparity, windowSize, useSinglePass);
182
183    // construct the viewer.
184    osgViewer::Viewer viewer;
185    viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
186
187    // add the stats handler
188    viewer.addEventHandler(new osgViewer::StatsHandler);
189
190    viewer.setSceneData(scene);
191
192    return viewer.run();
193}
Note: See TracBrowser for help on using the browser.