| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osg/Notify> |
|---|
| 14 | #include <osg/MatrixTransform> |
|---|
| 15 | #include <osg/Switch> |
|---|
| 16 | #include <osg/PolygonOffset> |
|---|
| 17 | #include <osg/CullFace> |
|---|
| 18 | |
|---|
| 19 | #include <osgUtil/Optimizer> |
|---|
| 20 | |
|---|
| 21 | #include <osgDB/FileNameUtils> |
|---|
| 22 | |
|---|
| 23 | #include <osgText/Text> |
|---|
| 24 | |
|---|
| 25 | #include <osgViewer/Viewer> |
|---|
| 26 | |
|---|
| 27 | #include "ImageReaderWriter.h" |
|---|
| 28 | |
|---|
| 29 | #include <iostream> |
|---|
| 30 | |
|---|
| 31 | using namespace osg; |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | osgDB::RegisterReaderWriterProxy<ImageReaderWriter> g_ImageReaderWriter; |
|---|
| 37 | |
|---|
| 38 | class Album; |
|---|
| 39 | |
|---|
| 40 | class Page : public osg::Transform |
|---|
| 41 | { |
|---|
| 42 | public: |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | static Page* createPage(Album* album, unsigned int pageNo, const std::string& frontFileName, const std::string& backFileName, float width, float height) |
|---|
| 46 | { |
|---|
| 47 | osg::ref_ptr<Page> page = new Page(album, pageNo, frontFileName, backFileName, width, height); |
|---|
| 48 | if (page.valid()) return page.release(); |
|---|
| 49 | else return 0; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | virtual void traverse(osg::NodeVisitor& nv); |
|---|
| 53 | |
|---|
| 54 | void setRotation(float angle) |
|---|
| 55 | { |
|---|
| 56 | _rotation = angle; |
|---|
| 57 | _targetRotation = angle; |
|---|
| 58 | dirtyBound(); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | float getRotation() const { return _rotation; } |
|---|
| 62 | |
|---|
| 63 | void rotateTo(float angle, float timeToRotateBy) |
|---|
| 64 | { |
|---|
| 65 | _targetRotation = angle; |
|---|
| 66 | _targetTime = timeToRotateBy; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | bool rotating() const { return _targetRotation!=_rotation; } |
|---|
| 70 | |
|---|
| 71 | void setPageVisible(bool frontVisible,bool backVisible) |
|---|
| 72 | { |
|---|
| 73 | _switch->setValue(0,!frontVisible && !backVisible); |
|---|
| 74 | _switch->setValue(1,frontVisible); |
|---|
| 75 | _switch->setValue(2,backVisible); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | osg::Switch* getSwitch() { return _switch.get(); } |
|---|
| 79 | const osg::Switch* getSwitch() const { return _switch.get(); } |
|---|
| 80 | |
|---|
| 81 | public: |
|---|
| 82 | |
|---|
| 83 | virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix,osg::NodeVisitor*) const |
|---|
| 84 | { |
|---|
| 85 | if (_referenceFrame==RELATIVE_RF) |
|---|
| 86 | { |
|---|
| 87 | matrix.preMult(getMatrix()); |
|---|
| 88 | } |
|---|
| 89 | else |
|---|
| 90 | { |
|---|
| 91 | matrix = getMatrix(); |
|---|
| 92 | } |
|---|
| 93 | return true; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix,osg::NodeVisitor*) const |
|---|
| 98 | { |
|---|
| 99 | const osg::Matrix& inverse = getInverseMatrix(); |
|---|
| 100 | |
|---|
| 101 | if (_referenceFrame==RELATIVE_RF) |
|---|
| 102 | { |
|---|
| 103 | matrix.postMult(inverse); |
|---|
| 104 | } |
|---|
| 105 | else |
|---|
| 106 | { |
|---|
| 107 | matrix = inverse; |
|---|
| 108 | } |
|---|
| 109 | return true; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | osg::Matrix getMatrix() const { return _pageOffset*osg::Matrix::rotate(-_rotation,0.0f,0.0f,1.0f); } |
|---|
| 113 | osg::Matrix getInverseMatrix() const { return osg::Matrix::inverse(getMatrix()); } |
|---|
| 114 | |
|---|
| 115 | protected: |
|---|
| 116 | |
|---|
| 117 | Page(Album* album, unsigned int pageNo, const std::string& frontFileName, const std::string& backFileName, float width, float height); |
|---|
| 118 | |
|---|
| 119 | float _rotation; |
|---|
| 120 | osg::Matrix _pageOffset; |
|---|
| 121 | |
|---|
| 122 | float _targetRotation; |
|---|
| 123 | float _targetTime; |
|---|
| 124 | float _lastTimeTraverse; |
|---|
| 125 | |
|---|
| 126 | osg::ref_ptr<osg::Switch> _switch; |
|---|
| 127 | |
|---|
| 128 | }; |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | class Album : public osg::Referenced |
|---|
| 132 | { |
|---|
| 133 | public: |
|---|
| 134 | |
|---|
| 135 | Album(osg::ArgumentParser& ap, float width, float height); |
|---|
| 136 | |
|---|
| 137 | osg::Group* getScene() { return _group.get(); } |
|---|
| 138 | |
|---|
| 139 | const osg::Group* getScene() const { return _group.get(); } |
|---|
| 140 | |
|---|
| 141 | osg::Matrix getPageOffset(unsigned int pageNo) const; |
|---|
| 142 | |
|---|
| 143 | bool nextPage(float timeToRotateBy) { return gotoPage(_currentPageNo+1,timeToRotateBy); } |
|---|
| 144 | |
|---|
| 145 | bool previousPage(float timeToRotateBy) { return _currentPageNo>=1?gotoPage(_currentPageNo-1,timeToRotateBy):false; } |
|---|
| 146 | |
|---|
| 147 | bool gotoPage(unsigned int pageNo, float timeToRotateBy); |
|---|
| 148 | |
|---|
| 149 | osg::StateSet* getBackgroundStateSet() { return _backgroundStateSet.get(); } |
|---|
| 150 | |
|---|
| 151 | void setVisibility(); |
|---|
| 152 | |
|---|
| 153 | protected: |
|---|
| 154 | |
|---|
| 155 | typedef std::vector< osg::ref_ptr<Page> > PageList; |
|---|
| 156 | |
|---|
| 157 | osg::ref_ptr<osg::Group> _group; |
|---|
| 158 | PageList _pages; |
|---|
| 159 | |
|---|
| 160 | osg::ref_ptr<osg::StateSet> _backgroundStateSet; |
|---|
| 161 | |
|---|
| 162 | unsigned int _currentPageNo; |
|---|
| 163 | float _radiusOfRings; |
|---|
| 164 | float _startAngleOfPages; |
|---|
| 165 | float _deltaAngleBetweenPages; |
|---|
| 166 | |
|---|
| 167 | }; |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | Page::Page(Album* album, unsigned int pageNo, const std::string& frontFileName, const std::string& backFileName, float width, float height) |
|---|
| 174 | { |
|---|
| 175 | |
|---|
| 176 | _rotation = 0; |
|---|
| 177 | _targetRotation = 0; |
|---|
| 178 | _targetTime = 0; |
|---|
| 179 | _lastTimeTraverse = 0; |
|---|
| 180 | |
|---|
| 181 | _pageOffset = album->getPageOffset(pageNo); |
|---|
| 182 | |
|---|
| 183 | setNumChildrenRequiringUpdateTraversal(1); |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | osgDB::ReaderWriter* readerWriter = osgDB::Registry::instance()->getReaderWriterForExtension("gdal"); |
|---|
| 188 | if (!readerWriter) |
|---|
| 189 | { |
|---|
| 190 | std::cout<<"Error: GDAL plugin not available, cannot preceed with database creation"<<std::endl; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | _switch = new osg::Switch; |
|---|
| 194 | |
|---|
| 195 | ImageReaderWriter* rw = g_ImageReaderWriter.get(); |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | osg::Group* non_visible_page = new osg::Group; |
|---|
| 200 | _switch->addChild(non_visible_page); |
|---|
| 201 | { |
|---|
| 202 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 203 | geom->setStateSet(album->getBackgroundStateSet()); |
|---|
| 204 | |
|---|
| 205 | osg::Vec3Array* coords = new osg::Vec3Array(4); |
|---|
| 206 | (*coords)[0].set(0.0f,0.0,height); |
|---|
| 207 | (*coords)[1].set(0.0f,0.0,0); |
|---|
| 208 | (*coords)[2].set(width,0.0,0); |
|---|
| 209 | (*coords)[3].set(width,0.0,height); |
|---|
| 210 | geom->setVertexArray(coords); |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | osg::Vec3Array* normals = new osg::Vec3Array(4); |
|---|
| 214 | (*normals)[0].set(-1.0f,0.0f,0.0f); |
|---|
| 215 | (*normals)[1].set(0.0f,0.0f,-1.0f); |
|---|
| 216 | (*normals)[2].set(1.0f,0.0f,0.0f); |
|---|
| 217 | (*normals)[3].set(0.0f,0.0f,1.0f); |
|---|
| 218 | geom->setNormalArray(normals); |
|---|
| 219 | geom->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE); |
|---|
| 220 | |
|---|
| 221 | osg::Vec2Array* tcoords = new osg::Vec2Array(4); |
|---|
| 222 | (*tcoords)[0].set(0.0f,1.0f); |
|---|
| 223 | (*tcoords)[1].set(0.0f,0.0f); |
|---|
| 224 | (*tcoords)[2].set(1.0f,0.0f); |
|---|
| 225 | (*tcoords)[3].set(1.0f,1.0f); |
|---|
| 226 | geom->setTexCoordArray(0,tcoords); |
|---|
| 227 | |
|---|
| 228 | osg::Vec4Array* colours = new osg::Vec4Array(1); |
|---|
| 229 | (*colours)[0].set(1.0f,1.0f,1.0,1.0f); |
|---|
| 230 | geom->setColorArray(colours); |
|---|
| 231 | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 232 | |
|---|
| 233 | osg::UByteArray* vindices = new osg::UByteArray(8); |
|---|
| 234 | (*vindices)[0]=0; |
|---|
| 235 | (*vindices)[1]=1; |
|---|
| 236 | (*vindices)[2]=1; |
|---|
| 237 | (*vindices)[3]=2; |
|---|
| 238 | (*vindices)[4]=2; |
|---|
| 239 | (*vindices)[5]=3; |
|---|
| 240 | (*vindices)[6]=3; |
|---|
| 241 | (*vindices)[7]=0; |
|---|
| 242 | |
|---|
| 243 | geom->setVertexIndices(vindices); |
|---|
| 244 | geom->setTexCoordIndices(0,vindices); |
|---|
| 245 | |
|---|
| 246 | geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,8)); |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | osg::Geode* geode = new osg::Geode; |
|---|
| 250 | geode->addDrawable(geom); |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | non_visible_page->addChild(geode); |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | osg::Group* front_page = new osg::Group; |
|---|
| 259 | _switch->addChild(front_page); |
|---|
| 260 | |
|---|
| 261 | { |
|---|
| 262 | |
|---|
| 263 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 264 | geom->setStateSet(album->getBackgroundStateSet()); |
|---|
| 265 | |
|---|
| 266 | osg::Vec3Array* coords = new osg::Vec3Array(4); |
|---|
| 267 | (*coords)[0].set(0.0f,0.0,height); |
|---|
| 268 | (*coords)[1].set(0.0f,0.0,0); |
|---|
| 269 | (*coords)[2].set(width,0.0,0); |
|---|
| 270 | (*coords)[3].set(width,0.0,height); |
|---|
| 271 | geom->setVertexArray(coords); |
|---|
| 272 | |
|---|
| 273 | osg::Vec3Array* normals = new osg::Vec3Array(1); |
|---|
| 274 | (*normals)[0].set(0.0f,-1.0f,0.0f); |
|---|
| 275 | geom->setNormalArray(normals); |
|---|
| 276 | geom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 277 | |
|---|
| 278 | osg::Vec2Array* tcoords = new osg::Vec2Array(4); |
|---|
| 279 | (*tcoords)[0].set(0.0f,1.0f); |
|---|
| 280 | (*tcoords)[1].set(0.0f,0.0f); |
|---|
| 281 | (*tcoords)[2].set(1.0f,0.0f); |
|---|
| 282 | (*tcoords)[3].set(1.0f,1.0f); |
|---|
| 283 | geom->setTexCoordArray(0,tcoords); |
|---|
| 284 | |
|---|
| 285 | osg::Vec4Array* colours = new osg::Vec4Array(1); |
|---|
| 286 | (*colours)[0].set(1.0f,1.0f,1.0,1.0f); |
|---|
| 287 | geom->setColorArray(colours); |
|---|
| 288 | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 289 | |
|---|
| 290 | geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4)); |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | osg::Geode* geode = new osg::Geode; |
|---|
| 294 | geode->addDrawable(geom); |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | front_page->addChild(geode); |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | if (!frontFileName.empty()) |
|---|
| 301 | { |
|---|
| 302 | float cut_off_distance = 8.0f; |
|---|
| 303 | float max_visible_distance = 300.0f; |
|---|
| 304 | |
|---|
| 305 | osg::Vec3 center(width*0.5f,0.0f,height*0.5f); |
|---|
| 306 | |
|---|
| 307 | osgText::Text* text = new osgText::Text; |
|---|
| 308 | text->setFont("fonts/arial.ttf"); |
|---|
| 309 | text->setPosition(center); |
|---|
| 310 | text->setCharacterSize(height/20.0f); |
|---|
| 311 | text->setAlignment(osgText::Text::CENTER_CENTER); |
|---|
| 312 | text->setAxisAlignment(osgText::Text::XZ_PLANE); |
|---|
| 313 | text->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); |
|---|
| 314 | text->setText(std::string("Loading ")+frontFileName); |
|---|
| 315 | |
|---|
| 316 | osg::Geode* geode = new osg::Geode; |
|---|
| 317 | geode->addDrawable(text); |
|---|
| 318 | |
|---|
| 319 | osg::PagedLOD* pagedlod = new osg::PagedLOD; |
|---|
| 320 | pagedlod->setCenter(center); |
|---|
| 321 | pagedlod->setRadius(1.6f); |
|---|
| 322 | pagedlod->setNumChildrenThatCannotBeExpired(2); |
|---|
| 323 | |
|---|
| 324 | pagedlod->setRange(0,max_visible_distance,1e7); |
|---|
| 325 | pagedlod->addChild(geode); |
|---|
| 326 | |
|---|
| 327 | pagedlod->setRange(1,cut_off_distance,max_visible_distance); |
|---|
| 328 | pagedlod->setFileName(1,rw->insertReference(frontFileName,256,width,height,false)); |
|---|
| 329 | |
|---|
| 330 | pagedlod->setRange(2,0.0f,cut_off_distance); |
|---|
| 331 | pagedlod->setFileName(2,rw->insertReference(frontFileName,1024,width,height,false)); |
|---|
| 332 | |
|---|
| 333 | front_page->addChild(pagedlod); |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | osg::Group* back_page = new osg::Group; |
|---|
| 339 | _switch->addChild(back_page); |
|---|
| 340 | |
|---|
| 341 | { |
|---|
| 342 | |
|---|
| 343 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 344 | geom->setStateSet(album->getBackgroundStateSet()); |
|---|
| 345 | |
|---|
| 346 | osg::Vec3Array* coords = new osg::Vec3Array(4); |
|---|
| 347 | (*coords)[0].set(width,0.0,height); |
|---|
| 348 | (*coords)[1].set(width,0.0,0); |
|---|
| 349 | (*coords)[2].set(0.0f,0.0,0); |
|---|
| 350 | (*coords)[3].set(0.0f,0.0,height); |
|---|
| 351 | geom->setVertexArray(coords); |
|---|
| 352 | |
|---|
| 353 | osg::Vec3Array* normals = new osg::Vec3Array(1); |
|---|
| 354 | (*normals)[0].set(0.0f,1.0f,0.0f); |
|---|
| 355 | geom->setNormalArray(normals); |
|---|
| 356 | geom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 357 | |
|---|
| 358 | osg::Vec2Array* tcoords = new osg::Vec2Array(4); |
|---|
| 359 | (*tcoords)[0].set(1.0f,1.0f); |
|---|
| 360 | (*tcoords)[1].set(1.0f,0.0f); |
|---|
| 361 | (*tcoords)[2].set(0.0f,0.0f); |
|---|
| 362 | (*tcoords)[3].set(0.0f,1.0f); |
|---|
| 363 | geom->setTexCoordArray(0,tcoords); |
|---|
| 364 | |
|---|
| 365 | osg::Vec4Array* colours = new osg::Vec4Array(1); |
|---|
| 366 | (*colours)[0].set(1.0f,1.0f,1.0,1.0f); |
|---|
| 367 | geom->setColorArray(colours); |
|---|
| 368 | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 369 | |
|---|
| 370 | geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4)); |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | osg::Geode* geode = new osg::Geode; |
|---|
| 374 | geode->addDrawable(geom); |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | back_page->addChild(geode); |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | if (!backFileName.empty()) |
|---|
| 381 | { |
|---|
| 382 | float cut_off_distance = 8.0f; |
|---|
| 383 | float max_visible_distance = 300.0f; |
|---|
| 384 | |
|---|
| 385 | osg::Vec3 center(width*0.5f,0.0f,height*0.5f); |
|---|
| 386 | |
|---|
| 387 | osgText::Text* text = new osgText::Text; |
|---|
| 388 | text->setFont("fonts/arial.ttf"); |
|---|
| 389 | text->setPosition(center); |
|---|
| 390 | text->setCharacterSize(height/20.0f); |
|---|
| 391 | text->setAlignment(osgText::Text::CENTER_CENTER); |
|---|
| 392 | text->setAxisAlignment(osgText::Text::REVERSED_XZ_PLANE); |
|---|
| 393 | text->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); |
|---|
| 394 | text->setText(std::string("Loading ")+backFileName); |
|---|
| 395 | |
|---|
| 396 | osg::Geode* geode = new osg::Geode; |
|---|
| 397 | geode->addDrawable(text); |
|---|
| 398 | |
|---|
| 399 | osg::PagedLOD* pagedlod = new osg::PagedLOD; |
|---|
| 400 | pagedlod->setCenter(center); |
|---|
| 401 | pagedlod->setRadius(1.6f); |
|---|
| 402 | pagedlod->setNumChildrenThatCannotBeExpired(2); |
|---|
| 403 | |
|---|
| 404 | pagedlod->setRange(0,max_visible_distance,1e7); |
|---|
| 405 | pagedlod->addChild(geode); |
|---|
| 406 | |
|---|
| 407 | pagedlod->setRange(1,cut_off_distance,max_visible_distance); |
|---|
| 408 | pagedlod->setFileName(1,rw->insertReference(backFileName,256,width,height,true)); |
|---|
| 409 | |
|---|
| 410 | pagedlod->setRange(2,0.0f,cut_off_distance); |
|---|
| 411 | pagedlod->setFileName(2,rw->insertReference(backFileName,1024,width,height,true)); |
|---|
| 412 | |
|---|
| 413 | back_page->addChild(pagedlod); |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | addChild(_switch.get()); |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | void Page::traverse(osg::NodeVisitor& nv) |
|---|
| 420 | { |
|---|
| 421 | |
|---|
| 422 | if (nv.getVisitorType()==osg::NodeVisitor::UPDATE_VISITOR) |
|---|
| 423 | { |
|---|
| 424 | const osg::FrameStamp* framestamp = nv.getFrameStamp(); |
|---|
| 425 | if (framestamp) |
|---|
| 426 | { |
|---|
| 427 | double t = framestamp->getReferenceTime(); |
|---|
| 428 | |
|---|
| 429 | if (_rotation!=_targetRotation) |
|---|
| 430 | { |
|---|
| 431 | if (t>=_targetTime) _rotation = _targetRotation; |
|---|
| 432 | else _rotation += (_targetRotation-_rotation)*(t-_lastTimeTraverse)/(_targetTime-_lastTimeTraverse); |
|---|
| 433 | |
|---|
| 434 | dirtyBound(); |
|---|
| 435 | } |
|---|
| 436 | |
|---|
| 437 | _lastTimeTraverse = t; |
|---|
| 438 | |
|---|
| 439 | } |
|---|
| 440 | } |
|---|
| 441 | Transform::traverse(nv); |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | |
|---|
| 445 | |
|---|
| 446 | |
|---|
| 447 | Album::Album(osg::ArgumentParser& arguments, float width, float height) |
|---|
| 448 | { |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | typedef std::vector<std::string> FileList; |
|---|
| 452 | FileList fileList; |
|---|
| 453 | |
|---|
| 454 | for(int pos=1;pos<arguments.argc();++pos) |
|---|
| 455 | { |
|---|
| 456 | if (arguments.isString(pos)) |
|---|
| 457 | { |
|---|
| 458 | std::string filename(arguments[pos]); |
|---|
| 459 | if (osgDB::getLowerCaseFileExtension(filename)=="album") |
|---|
| 460 | { |
|---|
| 461 | PhotoArchive* photoArchive = PhotoArchive::open(filename); |
|---|
| 462 | if (photoArchive) |
|---|
| 463 | { |
|---|
| 464 | g_ImageReaderWriter.get()->addPhotoArchive(photoArchive); |
|---|
| 465 | photoArchive->getImageFileNameList(fileList); |
|---|
| 466 | } |
|---|
| 467 | |
|---|
| 468 | } |
|---|
| 469 | else |
|---|
| 470 | { |
|---|
| 471 | fileList.push_back(arguments[pos]); |
|---|
| 472 | } |
|---|
| 473 | } |
|---|
| 474 | } |
|---|
| 475 | |
|---|
| 476 | _radiusOfRings = 0.02; |
|---|
| 477 | _startAngleOfPages = 0.0f; |
|---|
| 478 | _deltaAngleBetweenPages = osg::PI/(float)fileList.size(); |
|---|
| 479 | |
|---|
| 480 | _group = new osg::Group; |
|---|
| 481 | _group->getOrCreateStateSet()->setAttributeAndModes(new osg::CullFace,osg::StateAttribute::ON); |
|---|
| 482 | |
|---|
| 483 | _backgroundStateSet = new osg::StateSet; |
|---|
| 484 | _backgroundStateSet->setAttributeAndModes(new osg::PolygonOffset(1.0f,1.0f),osg::StateAttribute::ON); |
|---|
| 485 | |
|---|
| 486 | |
|---|
| 487 | unsigned int i; |
|---|
| 488 | for(i=0;i<fileList.size();i+=2) |
|---|
| 489 | { |
|---|
| 490 | Page* page = i+1<fileList.size()? |
|---|
| 491 | Page::createPage(this,_pages.size(),fileList[i],fileList[i+1], width, height): |
|---|
| 492 | Page::createPage(this,_pages.size(),fileList[i],"", width, height); |
|---|
| 493 | if (page) |
|---|
| 494 | { |
|---|
| 495 | _pages.push_back(page); |
|---|
| 496 | _group->addChild(page); |
|---|
| 497 | } |
|---|
| 498 | } |
|---|
| 499 | |
|---|
| 500 | setVisibility(); |
|---|
| 501 | |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | osg::Matrix Album::getPageOffset(unsigned int pageNo) const |
|---|
| 505 | { |
|---|
| 506 | float angleForPage = _startAngleOfPages+_deltaAngleBetweenPages*(float)pageNo; |
|---|
| 507 | osg::Vec3 delta(_radiusOfRings*sinf(angleForPage),-_radiusOfRings*cosf(angleForPage),0.0f); |
|---|
| 508 | return osg::Matrix::translate(delta); |
|---|
| 509 | } |
|---|
| 510 | |
|---|
| 511 | bool Album::gotoPage(unsigned int pageNo, float timeToRotateBy) |
|---|
| 512 | { |
|---|
| 513 | if (pageNo>=_pages.size()) return false; |
|---|
| 514 | |
|---|
| 515 | if (pageNo>_currentPageNo) |
|---|
| 516 | { |
|---|
| 517 | for(unsigned int i=_currentPageNo;i<pageNo;++i) |
|---|
| 518 | { |
|---|
| 519 | _pages[i]->rotateTo(osg::PI,timeToRotateBy); |
|---|
| 520 | } |
|---|
| 521 | _currentPageNo = pageNo; |
|---|
| 522 | |
|---|
| 523 | return true; |
|---|
| 524 | } |
|---|
| 525 | else if (pageNo<_currentPageNo) |
|---|
| 526 | { |
|---|
| 527 | for(unsigned int i=pageNo;i<_currentPageNo;++i) |
|---|
| 528 | { |
|---|
| 529 | _pages[i]->rotateTo(0,timeToRotateBy); |
|---|
| 530 | } |
|---|
| 531 | _currentPageNo = pageNo; |
|---|
| 532 | |
|---|
| 533 | return true; |
|---|
| 534 | } |
|---|
| 535 | |
|---|
| 536 | return false; |
|---|
| 537 | } |
|---|
| 538 | |
|---|
| 539 | void Album::setVisibility() |
|---|
| 540 | { |
|---|
| 541 | for(unsigned int i=0;i<_pages.size();++i) |
|---|
| 542 | { |
|---|
| 543 | bool front_visible = _pages[i]->rotating() || |
|---|
| 544 | (i>0?_pages[i-1]->rotating():false) || |
|---|
| 545 | i==_currentPageNo || |
|---|
| 546 | i==0; |
|---|
| 547 | |
|---|
| 548 | bool back_visible = _pages[i]->rotating() || |
|---|
| 549 | ((i+1)<_pages.size()?_pages[i+1]->rotating():false) || |
|---|
| 550 | i==_currentPageNo-1 || |
|---|
| 551 | i==_pages.size()-1; |
|---|
| 552 | |
|---|
| 553 | _pages[i]->setPageVisible(front_visible,back_visible); |
|---|
| 554 | } |
|---|
| 555 | |
|---|
| 556 | } |
|---|
| 557 | |
|---|
| 558 | |
|---|
| 559 | |
|---|
| 560 | |
|---|
| 561 | |
|---|
| 562 | class SlideEventHandler : public osgGA::GUIEventHandler |
|---|
| 563 | { |
|---|
| 564 | public: |
|---|
| 565 | |
|---|
| 566 | SlideEventHandler(); |
|---|
| 567 | |
|---|
| 568 | META_Object(osgStereImageApp,SlideEventHandler); |
|---|
| 569 | |
|---|
| 570 | void set(Album* album, float timePerSlide, bool autoSteppingActive); |
|---|
| 571 | |
|---|
| 572 | virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&); |
|---|
| 573 | |
|---|
| 574 | virtual void getUsage(osg::ApplicationUsage& usage) const; |
|---|
| 575 | |
|---|
| 576 | protected: |
|---|
| 577 | |
|---|
| 578 | ~SlideEventHandler() {} |
|---|
| 579 | SlideEventHandler(const SlideEventHandler&,const osg::CopyOp&) {} |
|---|
| 580 | |
|---|
| 581 | osg::ref_ptr<Album> _album; |
|---|
| 582 | bool _firstTraversal; |
|---|
| 583 | double _previousTime; |
|---|
| 584 | double _timePerSlide; |
|---|
| 585 | bool _autoSteppingActive; |
|---|
| 586 | }; |
|---|
| 587 | |
|---|
| 588 | SlideEventHandler::SlideEventHandler(): |
|---|
| 589 | _album(0), |
|---|
| 590 | _firstTraversal(true), |
|---|
| 591 | _previousTime(-1.0f), |
|---|
| 592 | _timePerSlide(5.0), |
|---|
| 593 | _autoSteppingActive(false) |
|---|
| 594 | { |
|---|
| 595 | } |
|---|
| 596 | |
|---|
| 597 | void SlideEventHandler::set(Album* album, float timePerSlide, bool autoSteppingActive) |
|---|
| 598 | { |
|---|
| 599 | _album = album; |
|---|
| 600 | |
|---|
| 601 | _timePerSlide = timePerSlide; |
|---|
| 602 | _autoSteppingActive = autoSteppingActive; |
|---|
| 603 | |
|---|
| 604 | } |
|---|
| 605 | |
|---|
| 606 | bool SlideEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&) |
|---|
| 607 | { |
|---|
| 608 | switch(ea.getEventType()) |
|---|
| 609 | { |
|---|
| 610 | case(osgGA::GUIEventAdapter::KEYDOWN): |
|---|
| 611 | { |
|---|
| 612 | if (ea.getKey()=='a') |
|---|
| 613 | { |
|---|
| 614 | _autoSteppingActive = !_autoSteppingActive; |
|---|
| 615 | _previousTime = ea.getTime(); |
|---|
| 616 | return true; |
|---|
| 617 | } |
|---|
| 618 | else if (ea.getKey()=='n') |
|---|
| 619 | { |
|---|
| 620 | _album->nextPage(ea.getTime()+1.0f); |
|---|
| 621 | return true; |
|---|
| 622 | } |
|---|
| 623 | else if (ea.getKey()=='p') |
|---|
| 624 | { |
|---|
| 625 | _album->previousPage(ea.getTime()+1.0f); |
|---|
| 626 | return true; |
|---|
| 627 | } |
|---|
| 628 | return false; |
|---|
| 629 | } |
|---|
| 630 | case(osgGA::GUIEventAdapter::FRAME): |
|---|
| 631 | { |
|---|
| 632 | if (_autoSteppingActive) |
|---|
| 633 | { |
|---|
| 634 | if (_firstTraversal) |
|---|
| 635 | { |
|---|
| 636 | _firstTraversal = false; |
|---|
| 637 | _previousTime = ea.getTime(); |
|---|
| 638 | } |
|---|
| 639 | else if (ea.getTime()-_previousTime>_timePerSlide) |
|---|
| 640 | { |
|---|
| 641 | _previousTime = ea.getTime(); |
|---|
| 642 | |
|---|
| 643 | _album->nextPage(ea.getTime()+1.0f); |
|---|
| 644 | } |
|---|
| 645 | } |
|---|
| 646 | |
|---|
| 647 | _album->setVisibility(); |
|---|
| 648 | |
|---|
| 649 | } |
|---|
| 650 | |
|---|
| 651 | default: |
|---|
| 652 | return false; |
|---|
| 653 | } |
|---|
| 654 | } |
|---|
| 655 | |
|---|
| 656 | void SlideEventHandler::getUsage(osg::ApplicationUsage& usage) const |
|---|
| 657 | { |
|---|
| 658 | usage.addKeyboardMouseBinding("Space","Reset the image position to center"); |
|---|
| 659 | usage.addKeyboardMouseBinding("a","Toggle on/off the automatic advancement for image to image"); |
|---|
| 660 | usage.addKeyboardMouseBinding("n","Advance to next image"); |
|---|
| 661 | usage.addKeyboardMouseBinding("p","Move to previous image"); |
|---|
| 662 | } |
|---|
| 663 | |
|---|
| 664 | int main( int argc, char **argv ) |
|---|
| 665 | { |
|---|
| 666 | |
|---|
| 667 | |
|---|
| 668 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 669 | |
|---|
| 670 | |
|---|
| 671 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use node masks to create stereo images."); |
|---|
| 672 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] image_file [image_file]"); |
|---|
| 673 | arguments.getApplicationUsage()->addCommandLineOption("-d <float>","Time delay in sceonds between the display of successive image pairs when in auto advance mode."); |
|---|
| 674 | arguments.getApplicationUsage()->addCommandLineOption("-a","Enter auto advance of image pairs on start up."); |
|---|
| 675 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 676 | arguments.getApplicationUsage()->addCommandLineOption("--create <filename>","Create an photo archive of specified files"); |
|---|
| 677 | |
|---|
| 678 | |
|---|
| 679 | |
|---|
| 680 | osgViewer::Viewer viewer; |
|---|
| 681 | |
|---|
| 682 | |
|---|
| 683 | SlideEventHandler* seh = new SlideEventHandler(); |
|---|
| 684 | viewer.addEventHandler(seh); |
|---|
| 685 | |
|---|
| 686 | |
|---|
| 687 | float timeDelayBetweenSlides = 5.0f; |
|---|
| 688 | while (arguments.read("-d",timeDelayBetweenSlides)) {} |
|---|
| 689 | |
|---|
| 690 | bool autoSteppingActive = false; |
|---|
| 691 | while (arguments.read("-a")) autoSteppingActive = true; |
|---|
| 692 | |
|---|
| 693 | |
|---|
| 694 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 695 | { |
|---|
| 696 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 697 | return 1; |
|---|
| 698 | } |
|---|
| 699 | |
|---|
| 700 | std::string archiveName; |
|---|
| 701 | while (arguments.read("--create",archiveName)) {} |
|---|
| 702 | |
|---|
| 703 | |
|---|
| 704 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 705 | |
|---|
| 706 | |
|---|
| 707 | if (arguments.errors()) |
|---|
| 708 | { |
|---|
| 709 | arguments.writeErrorMessages(std::cout); |
|---|
| 710 | return 1; |
|---|
| 711 | } |
|---|
| 712 | |
|---|
| 713 | if (arguments.argc()<=1) |
|---|
| 714 | { |
|---|
| 715 | arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); |
|---|
| 716 | return 1; |
|---|
| 717 | } |
|---|
| 718 | |
|---|
| 719 | |
|---|
| 720 | if (!archiveName.empty()) |
|---|
| 721 | { |
|---|
| 722 | |
|---|
| 723 | PhotoArchive::FileNameList fileNameList; |
|---|
| 724 | for(int i=1;i<arguments.argc();++i) |
|---|
| 725 | { |
|---|
| 726 | if (arguments.isString(i)) fileNameList.push_back(std::string(arguments[i])); |
|---|
| 727 | } |
|---|
| 728 | |
|---|
| 729 | PhotoArchive::buildArchive(archiveName,fileNameList); |
|---|
| 730 | |
|---|
| 731 | return 0; |
|---|
| 732 | } |
|---|
| 733 | |
|---|
| 734 | |
|---|
| 735 | |
|---|
| 736 | |
|---|
| 737 | double fovy, aspectRatio, zNear, zFar; |
|---|
| 738 | viewer.getCamera()->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, zFar); |
|---|
| 739 | |
|---|
| 740 | fovy = osg::DegreesToRadians(fovy); |
|---|
| 741 | double fovx = atan(tan(fovy*0.5)*aspectRatio)*2.0; |
|---|
| 742 | |
|---|
| 743 | float radius = 1.0f; |
|---|
| 744 | float width = 2*radius*tan(fovx*0.5f); |
|---|
| 745 | float height = 2*radius*tan(fovy*0.5f); |
|---|
| 746 | |
|---|
| 747 | osg::ref_ptr<Album> album = new Album(arguments,width,height); |
|---|
| 748 | |
|---|
| 749 | |
|---|
| 750 | osg::ref_ptr<osg::Group> rootNode = album->getScene(); |
|---|
| 751 | |
|---|
| 752 | if (!rootNode) return 0; |
|---|
| 753 | |
|---|
| 754 | |
|---|
| 755 | |
|---|
| 756 | |
|---|
| 757 | |
|---|
| 758 | viewer.setSceneData(album->getScene()); |
|---|
| 759 | |
|---|
| 760 | |
|---|
| 761 | seh->set(album.get(),timeDelayBetweenSlides,autoSteppingActive); |
|---|
| 762 | |
|---|
| 763 | viewer.realize(); |
|---|
| 764 | |
|---|
| 765 | |
|---|
| 766 | osgViewer::Viewer::Windows windows; |
|---|
| 767 | viewer.getWindows(windows); |
|---|
| 768 | for(osgViewer::Viewer::Windows::iterator itr = windows.begin(); |
|---|
| 769 | itr != windows.end(); |
|---|
| 770 | ++itr) |
|---|
| 771 | { |
|---|
| 772 | (*itr)->useCursor(false); |
|---|
| 773 | } |
|---|
| 774 | |
|---|
| 775 | |
|---|
| 776 | return viewer.run(); |
|---|
| 777 | } |
|---|