| 21 | | |
| | 22 | #include <osg/Switch> |
| | 23 | #include <osg/TexMat> |
| | 24 | #include <osg/Texture2D> |
| | 25 | |
| | 26 | |
| | 27 | |
| | 28 | typedef std::vector<std::string> FileList; |
| | 29 | |
| | 30 | class SlideEventHandler : public osgGA::GUIEventHandler, public osg::NodeCallback |
| | 31 | { |
| | 32 | public: |
| | 33 | |
| | 34 | SlideEventHandler(); |
| | 35 | |
| | 36 | META_Object(osgStereImageApp,SlideEventHandler); |
| | 37 | |
| | 38 | |
| | 39 | void set(osg::Switch* sw, osg::TexMat* texmat,float timePerSlide, bool autoSteppingActive); |
| | 40 | |
| | 41 | virtual void accept(osgGA::GUIEventHandlerVisitor& v) { v.visit(*this); } |
| | 42 | |
| | 43 | virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&); |
| | 44 | |
| | 45 | virtual void getUsage(osg::ApplicationUsage& usage) const; |
| | 46 | |
| | 47 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv); |
| | 48 | |
| | 49 | void nextSlide(); |
| | 50 | |
| | 51 | void previousSlide(); |
| | 52 | |
| | 53 | void scaleImage(float s); |
| | 54 | |
| | 55 | void rotateImage(float rx,float ry); |
| | 56 | |
| | 57 | |
| | 58 | protected: |
| | 59 | |
| | 60 | ~SlideEventHandler() {} |
| | 61 | SlideEventHandler(const SlideEventHandler&,const osg::CopyOp&) {} |
| | 62 | |
| | 63 | osg::ref_ptr<osg::Switch> _switch; |
| | 64 | osg::ref_ptr<osg::TexMat> _texmat; |
| | 65 | bool _firstTraversal; |
| | 66 | unsigned int _activeSlide; |
| | 67 | double _previousTime; |
| | 68 | double _timePerSlide; |
| | 69 | bool _autoSteppingActive; |
| | 70 | |
| | 71 | }; |
| | 72 | |
| | 73 | SlideEventHandler::SlideEventHandler(): |
| | 74 | _switch(0), |
| | 75 | _texmat(0), |
| | 76 | _firstTraversal(true), |
| | 77 | _activeSlide(0), |
| | 78 | _previousTime(-1.0f), |
| | 79 | _timePerSlide(5.0), |
| | 80 | _autoSteppingActive(false) |
| | 81 | { |
| | 82 | } |
| | 83 | |
| | 84 | void SlideEventHandler::set(osg::Switch* sw, osg::TexMat* texmat,float timePerSlide, bool autoSteppingActive) |
| | 85 | { |
| | 86 | _switch = sw; |
| | 87 | _switch->setUpdateCallback(this); |
| | 88 | |
| | 89 | _texmat = texmat; |
| | 90 | |
| | 91 | _timePerSlide = timePerSlide; |
| | 92 | _autoSteppingActive = autoSteppingActive; |
| | 93 | } |
| | 94 | |
| | 95 | bool SlideEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&) |
| | 96 | { |
| | 97 | switch(ea.getEventType()) |
| | 98 | { |
| | 99 | case(osgGA::GUIEventAdapter::KEYDOWN): |
| | 100 | { |
| | 101 | if (ea.getKey()=='a') |
| | 102 | { |
| | 103 | _autoSteppingActive = !_autoSteppingActive; |
| | 104 | _previousTime = ea.time(); |
| | 105 | return true; |
| | 106 | } |
| | 107 | else if (ea.getKey()=='n') |
| | 108 | { |
| | 109 | nextSlide(); |
| | 110 | return true; |
| | 111 | } |
| | 112 | else if (ea.getKey()=='p') |
| | 113 | { |
| | 114 | previousSlide(); |
| | 115 | return true; |
| | 116 | } |
| | 117 | else if (ea.getKey()=='z') |
| | 118 | { |
| | 119 | scaleImage(0.99f); |
| | 120 | return true; |
| | 121 | } |
| | 122 | else if (ea.getKey()=='x') |
| | 123 | { |
| | 124 | scaleImage(1.01f); |
| | 125 | return true; |
| | 126 | } |
| | 127 | else if (ea.getKey()==' ') |
| | 128 | { |
| | 129 | if (_texmat.valid()) _texmat->setMatrix(osg::Matrix::identity()); |
| | 130 | return true; |
| | 131 | } |
| | 132 | return false; |
| | 133 | } |
| | 134 | case(osgGA::GUIEventAdapter::DRAG): |
| | 135 | case(osgGA::GUIEventAdapter::MOVE): |
| | 136 | { |
| | 137 | static int px = ea.getX(); |
| | 138 | static int py = ea.getY(); |
| | 139 | |
| | 140 | int dx = ea.getX()-px; |
| | 141 | int dy = ea.getY()-py; |
| | 142 | |
| | 143 | px = ea.getX(); |
| | 144 | py = ea.getY(); |
| | 145 | |
| | 146 | rotateImage((float)dx/(float)(ea.getXmax()-ea.getXmin()),(float)dy/(float)(ea.getYmax()-ea.getYmin())); |
| | 147 | |
| | 148 | return true; |
| | 149 | } |
| | 150 | |
| | 151 | default: |
| | 152 | return false; |
| | 153 | } |
| | 154 | } |
| | 155 | |
| | 156 | void SlideEventHandler::getUsage(osg::ApplicationUsage& usage) const |
| | 157 | { |
| | 158 | usage.addKeyboardMouseBinding("Space","Reset the image position to center"); |
| | 159 | usage.addKeyboardMouseBinding("a","Toggle on/off the automatic advancement for image to image"); |
| | 160 | usage.addKeyboardMouseBinding("n","Advance to next image"); |
| | 161 | usage.addKeyboardMouseBinding("p","Move to previous image"); |
| | 162 | usage.addKeyboardMouseBinding("z","Zoom into the image"); |
| | 163 | usage.addKeyboardMouseBinding("x","Zoom out of the image"); |
| | 164 | } |
| | 165 | |
| | 166 | void SlideEventHandler::operator()(osg::Node* node, osg::NodeVisitor* nv) |
| | 167 | { |
| | 168 | if (_autoSteppingActive && nv->getFrameStamp()) |
| | 169 | { |
| | 170 | double time = nv->getFrameStamp()->getReferenceTime(); |
| | 171 | |
| | 172 | if (_firstTraversal) |
| | 173 | { |
| | 174 | _firstTraversal = false; |
| | 175 | _previousTime = time; |
| | 176 | } |
| | 177 | else if (time-_previousTime>_timePerSlide) |
| | 178 | { |
| | 179 | _previousTime = time; |
| | 180 | |
| | 181 | nextSlide(); |
| | 182 | } |
| | 183 | |
| | 184 | } |
| | 185 | |
| | 186 | traverse(node,nv); |
| | 187 | } |
| | 188 | |
| | 189 | void SlideEventHandler::nextSlide() |
| | 190 | { |
| | 191 | if (_switch->getNumChildren()==0) return; |
| | 192 | |
| | 193 | ++_activeSlide; |
| | 194 | if (_activeSlide>=_switch->getNumChildren()) _activeSlide = 0; |
| | 195 | |
| | 196 | _switch->setSingleChildOn(_activeSlide); |
| | 197 | } |
| | 198 | |
| | 199 | void SlideEventHandler::previousSlide() |
| | 200 | { |
| | 201 | if (_switch->getNumChildren()==0) return; |
| | 202 | |
| | 203 | if (_activeSlide==0) _activeSlide = _switch->getNumChildren()-1; |
| | 204 | else --_activeSlide; |
| | 205 | |
| | 206 | _switch->setSingleChildOn(_activeSlide); |
| | 207 | } |
| | 208 | |
| | 209 | void SlideEventHandler::scaleImage(float s) |
| | 210 | { |
| | 211 | if (_texmat.valid()) |
| | 212 | { |
| | 213 | _texmat->setMatrix(_texmat->getMatrix()*osg::Matrix::translate(-0.5f,-0.5f,0.0f)*osg::Matrix::scale(s,s,1.0f)*osg::Matrix::translate(0.5f,0.5f,0.0f)); |
| | 214 | } |
| | 215 | } |
| | 216 | |
| | 217 | void SlideEventHandler::rotateImage(float rx,float ry) |
| | 218 | { |
| | 219 | if (_texmat.valid()) |
| | 220 | { |
| | 221 | const float scale = 0.5f; |
| | 222 | _texmat->setMatrix(_texmat->getMatrix()*osg::Matrix::translate(-rx*scale,ry*scale,0.0f)); |
| | 223 | } |
| | 224 | } |
| | 225 | |
| | 226 | osg::Geode* createSectorForImage(osg::Image* image,float s,float t, float radius, float height, float length) |
| | 227 | { |
| | 228 | |
| | 229 | int numSegments = 20; |
| | 230 | float Theta = length/radius; |
| | 231 | float dTheta = Theta/(float)(numSegments-1); |
| | 232 | |
| | 233 | float ThetaZero = height*s/(t*radius); |
| | 234 | |
| | 235 | // set up the texture. |
| | 236 | osg::Texture2D* texture = new osg::Texture2D; |
| | 237 | texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR); |
| | 238 | texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR); |
| | 239 | texture->setImage(image); |
| | 240 | |
| | 241 | // set up the drawstate. |
| | 242 | osg::StateSet* dstate = new osg::StateSet; |
| | 243 | dstate->setMode(GL_CULL_FACE,osg::StateAttribute::OFF); |
| | 244 | dstate->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
| | 245 | dstate->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON); |
| | 246 | |
| | 247 | // set up the geoset. |
| | 248 | osg::Geometry* geom = new osg::Geometry; |
| | 249 | geom->setStateSet(dstate); |
| | 250 | |
| | 251 | osg::Vec3Array* coords = new osg::Vec3Array(); |
| | 252 | osg::Vec2Array* tcoords = new osg::Vec2Array(); |
| | 253 | |
| | 254 | int i; |
| | 255 | float angle = -Theta/2.0f; |
| | 256 | for(i=0; |
| | 257 | i<numSegments; |
| | 258 | ++i, angle+=dTheta) |
| | 259 | { |
| | 260 | coords->push_back(osg::Vec3(sinf(angle)*radius,cosf(angle)*radius,height*0.5f)); // top |
| | 261 | coords->push_back(osg::Vec3(sinf(angle)*radius,cosf(angle)*radius,-height*0.5f)); // bottom. |
| | 262 | |
| | 263 | tcoords->push_back(osg::Vec2(angle/ThetaZero+0.5f,1.0f)); // top |
| | 264 | tcoords->push_back(osg::Vec2(angle/ThetaZero+0.5f,0.0f)); // bottom. |
| | 265 | |
| | 266 | } |
| | 267 | |
| | 268 | osg::Vec4Array* colors = new osg::Vec4Array(); |
| | 269 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
| | 270 | |
| | 271 | osg::DrawArrays* elements = new osg::DrawArrays(osg::PrimitiveSet::QUAD_STRIP,0,coords->size()); |
| | 272 | |
| | 273 | |
| | 274 | |
| | 275 | geom->setVertexArray(coords); |
| | 276 | geom->setTexCoordArray(0,tcoords); |
| | 277 | geom->setColorArray(colors); |
| | 278 | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
| | 279 | |
| | 280 | geom->addPrimitiveSet(elements); |
| | 281 | |
| | 282 | // set up the geode. |
| | 283 | osg::Geode* geode = new osg::Geode; |
| | 284 | geode->addDrawable(geom); |
| | 285 | |
| | 286 | return geode; |
| | 287 | |
| | 288 | } |
| | 289 | |
| | 290 | // create a switch containing a set of child each containing a |
| | 291 | // stereo image pair. |
| | 292 | osg::Switch* createScene(const FileList& fileList, float radius, float height, float length) |
| | 293 | { |
| | 294 | osg::Switch* sw = new osg::Switch; |
| | 295 | |
| | 296 | // load the images. |
| | 297 | for(unsigned int i=0;i+1<fileList.size();i+=2) |
| | 298 | { |
| | 299 | osg::ref_ptr<osg::Image> imageLeft = osgDB::readImageFile(fileList[i]); |
| | 300 | osg::ref_ptr<osg::Image> imageRight = osgDB::readImageFile(fileList[i+1]); |
| | 301 | if (imageLeft.valid() && imageRight.valid()) |
| | 302 | { |
| | 303 | float average_s = (imageLeft->s()+imageRight->s())*0.5f; |
| | 304 | float average_t = (imageLeft->t()+imageRight->t())*0.5f; |
| | 305 | |
| | 306 | osg::Geode* geodeLeft = createSectorForImage(imageLeft.get(),average_s,average_t, radius, height, length); |
| | 307 | geodeLeft->setNodeMask(0x01); |
| | 308 | |
| | 309 | osg::Geode* geodeRight = createSectorForImage(imageRight.get(),average_s,average_t, radius, height, length); |
| | 310 | geodeRight->setNodeMask(0x02); |
| | 311 | |
| | 312 | osg::ref_ptr<osg::Group> imageGroup = new osg::Group; |
| | 313 | |
| | 314 | imageGroup->addChild(geodeLeft); |
| | 315 | imageGroup->addChild(geodeRight); |
| | 316 | |
| | 317 | sw->addChild(imageGroup.get()); |
| | 318 | } |
| | 319 | else |
| | 320 | { |
| | 321 | std::cout << "Warning: Unable to load both image files, '"<<fileList[i]<<"' & '"<<fileList[i+1]<<"', required for stereo imaging."<<std::endl; |
| | 322 | } |
| | 323 | } |
| | 324 | |
| | 325 | |
| | 326 | if (sw->getNumChildren()>0) |
| | 327 | { |
| | 328 | // select first child. |
| | 329 | sw->setSingleChildOn(0); |
| | 330 | } |
| | 331 | |
| | 332 | return sw; |
| | 333 | } |