| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 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 | |
|---|
| 39 | osg::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 | |
|---|
| 47 | |
|---|
| 48 | osg::ref_ptr<osg::Geode> geode = new osg::Geode(); |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 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; |
|---|
| 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]; |
|---|
| 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; |
|---|
| 67 | osg::ref_ptr<osg::Geometry> geom = new osg::Geometry; |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | int xoff, zoff; |
|---|
| 73 | xoff = (i%2); |
|---|
| 74 | zoff = i>1 ? 1 : 0; |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 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()); |
|---|
| 86 | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 87 | geomss[i] = geom->getOrCreateStateSet(); |
|---|
| 88 | geomss[i]->setMode(GL_LIGHTING, osg::StateAttribute::OFF); |
|---|
| 89 | |
|---|
| 90 | texture[i] = new osg::TextureRectangle; |
|---|
| 91 | texture[i]->setResizeNonPowerOfTwoHint(false); |
|---|
| 92 | texture[i]->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); |
|---|
| 93 | texture[i]->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); |
|---|
| 94 | |
|---|
| 95 | geode->addDrawable(geom.get()); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | texture[0]->setImage(left); |
|---|
| 100 | texture[1]->setImage(right); |
|---|
| 101 | geomss[0]->setTextureAttributeAndModes(0, texture[0].get(), osg::StateAttribute::ON); |
|---|
| 102 | geomss[1]->setTextureAttributeAndModes(0, texture[1].get(), osg::StateAttribute::ON); |
|---|
| 103 | |
|---|
| 104 | topnode->addChild(geode.get()); |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | if (single_pass) { |
|---|
| 108 | StereoPass *stereopass = new StereoPass(texture[0].get(), texture[1].get(), |
|---|
| 109 | width, height, |
|---|
| 110 | min_disp, max_disp, window_size); |
|---|
| 111 | |
|---|
| 112 | topnode->addChild(stereopass->getRoot().get()); |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | geomss[2]->setTextureAttributeAndModes(0, |
|---|
| 116 | stereopass->getOutputTexture().get(), |
|---|
| 117 | osg::StateAttribute::ON); |
|---|
| 118 | } else { |
|---|
| 119 | StereoMultipass *stereomp = new StereoMultipass(texture[0].get(), texture[1].get(), |
|---|
| 120 | width, height, |
|---|
| 121 | min_disp, max_disp, window_size); |
|---|
| 122 | topnode->addChild(stereomp->getRoot().get()); |
|---|
| 123 | |
|---|
| 124 | geomss[2]->setTextureAttributeAndModes(0, |
|---|
| 125 | stereomp->getOutputTexture().get(), |
|---|
| 126 | osg::StateAttribute::ON); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | return topnode; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | int main(int argc, char *argv[]) |
|---|
| 133 | { |
|---|
| 134 | |
|---|
| 135 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | 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."); |
|---|
| 139 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] --left left_image --right right_image --min min_disparity --max max_disparity --window window_size"); |
|---|
| 140 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 141 | arguments.getApplicationUsage()->addCommandLineOption("--left","The left image of the stereo pair to load."); |
|---|
| 142 | arguments.getApplicationUsage()->addCommandLineOption("--right","The right image of the stereo pair to load."); |
|---|
| 143 | arguments.getApplicationUsage()->addCommandLineOption("--min","The minimum disparity to start matching pixels."); |
|---|
| 144 | arguments.getApplicationUsage()->addCommandLineOption("--max","The maximum disparity to stop matching pixels."); |
|---|
| 145 | arguments.getApplicationUsage()->addCommandLineOption("--window","The window size used to match areas around pixels."); |
|---|
| 146 | arguments.getApplicationUsage()->addCommandLineOption("--single","Use a single pass instead on multiple passes."); |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 150 | { |
|---|
| 151 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 152 | return 1; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | std::string leftName(""); |
|---|
| 156 | while(arguments.read("--left", leftName)) {} |
|---|
| 157 | |
|---|
| 158 | std::string rightName(""); |
|---|
| 159 | while(arguments.read("--right", rightName)) {} |
|---|
| 160 | |
|---|
| 161 | unsigned int minDisparity = 0; |
|---|
| 162 | while (arguments.read("--min", minDisparity)) {} |
|---|
| 163 | |
|---|
| 164 | unsigned int maxDisparity = 31; |
|---|
| 165 | while (arguments.read("--max", maxDisparity)) {} |
|---|
| 166 | |
|---|
| 167 | unsigned int windowSize = 5; |
|---|
| 168 | while (arguments.read("--window", windowSize)) {} |
|---|
| 169 | |
|---|
| 170 | bool useSinglePass = false; |
|---|
| 171 | while (arguments.read("--single")) { useSinglePass = true; } |
|---|
| 172 | |
|---|
| 173 | if (leftName == "" || rightName=="") { |
|---|
| 174 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 175 | return 1; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | osg::ref_ptr<osg::Image> leftIm = osgDB::readImageFile(leftName); |
|---|
| 180 | osg::ref_ptr<osg::Image> rightIm = osgDB::readImageFile(rightName); |
|---|
| 181 | |
|---|
| 182 | osg::Node* scene = createScene(leftIm.get(), rightIm.get(), minDisparity, maxDisparity, windowSize, useSinglePass); |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | osgViewer::Viewer viewer; |
|---|
| 186 | viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| 190 | |
|---|
| 191 | viewer.setSceneData(scene); |
|---|
| 192 | |
|---|
| 193 | return viewer.run(); |
|---|
| 194 | } |
|---|