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