- Timestamp:
- 12/20/03 15:25:18 (10 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/examples/osgphotoalbum/osgphotoalbum.cpp
r2539 r2566 12 12 */ 13 13 14 #include <osgProducer/Viewer>15 #include <osgDB/ReadFile>16 #include <osgDB/WriteFile>17 #include <osgDB/ImageOptions>18 19 #include <osgUtil/Optimizer>20 21 14 #include <osg/Geode> 22 15 #include <osg/Notify> … … 26 19 #include <osg/Texture2D> 27 20 #include <osg/PolygonOffset> 21 #include <osg/CullFace> 22 23 #include <osgUtil/Optimizer> 24 25 #include <osgDB/ReadFile> 26 #include <osgDB/WriteFile> 27 #include <osgDB/ImageOptions> 28 28 29 29 #include <osgText/Text> 30 31 #include <osgProducer/Viewer> 30 32 31 33 #include <sstream> … … 47 49 _maximumHeight(0.0f,0.0f,1.0f), 48 50 _numPointsAcross(10), 49 _numPointsUp(10) {} 50 51 DataReference(const std::string& fileName, unsigned int res, float width, float height): 51 _numPointsUp(10), 52 _backPage(false) {} 53 54 DataReference(const std::string& fileName, unsigned int res, float width, float height,bool backPage): 52 55 _fileName(fileName), 53 56 _resolutionX(res), … … 57 60 _maximumHeight(0.0f,0.0f,height), 58 61 _numPointsAcross(10), 59 _numPointsUp(10) {} 62 _numPointsUp(10), 63 _backPage(backPage) {} 60 64 61 65 DataReference(const DataReference& rhs): … … 67 71 _maximumHeight(rhs._maximumHeight), 68 72 _numPointsAcross(rhs._numPointsAcross), 69 _numPointsUp(rhs._numPointsUp) {} 73 _numPointsUp(rhs._numPointsUp), 74 _backPage(rhs._backPage) {} 70 75 71 76 std::string _fileName; … … 77 82 unsigned int _numPointsAcross; 78 83 unsigned int _numPointsUp; 84 bool _backPage; 79 85 }; 80 86 … … 82 88 DataReferenceMap _dataReferences; 83 89 84 std::string insertReference(const std::string& fileName, unsigned int res, float width, float height )90 std::string insertReference(const std::string& fileName, unsigned int res, float width, float height, bool backPage) 85 91 { 86 92 std::stringstream ostr; … … 88 94 89 95 std::string myReference = ostr.str(); 90 _dataReferences[myReference] = DataReference(fileName,res,width,height );96 _dataReferences[myReference] = DataReference(fileName,res,width,height,backPage); 91 97 return myReference; 92 98 } … … 94 100 95 101 96 virtual ReadResult readNode(const std::string& fileName, const Options* opt)102 virtual ReadResult readNode(const std::string& fileName, const Options*) 97 103 { 98 104 std::cout<<"Trying to read paged image "<<fileName<<std::endl; … … 148 154 } 149 155 156 photoWidth*=0.95; 157 photoHeight*=0.95; 158 150 159 osg::Vec3 halfWidthVector(dr._maximumWidth*(photoWidth*0.5f/maxWidth)); 151 160 osg::Vec3 halfHeightVector(dr._maximumHeight*(photoHeight*0.5f/maxHeight)); … … 168 177 169 178 osg::Vec3Array* coords = new osg::Vec3Array(4); 170 (*coords)[0] = dr._center - halfWidthVector + halfHeightVector; 171 (*coords)[1] = dr._center - halfWidthVector - halfHeightVector; 172 (*coords)[2] = dr._center + halfWidthVector - halfHeightVector; 173 (*coords)[3] = dr._center + halfWidthVector + halfHeightVector; 179 180 if (!dr._backPage) 181 { 182 (*coords)[0] = dr._center - halfWidthVector + halfHeightVector; 183 (*coords)[1] = dr._center - halfWidthVector - halfHeightVector; 184 (*coords)[2] = dr._center + halfWidthVector - halfHeightVector; 185 (*coords)[3] = dr._center + halfWidthVector + halfHeightVector; 186 } 187 else 188 { 189 (*coords)[3] = dr._center - halfWidthVector + halfHeightVector; 190 (*coords)[2] = dr._center - halfWidthVector - halfHeightVector; 191 (*coords)[1] = dr._center + halfWidthVector - halfHeightVector; 192 (*coords)[0] = dr._center + halfWidthVector + halfHeightVector; 193 } 174 194 geom->setVertexArray(coords); 175 195 … … 217 237 218 238 219 static Page* createPage(Album* album, unsigned int pageNo, const std::string& f ilename, float width, float height)220 { 221 osg::ref_ptr<Page> page = new Page(album, pageNo, f ilename, width, height);239 static Page* createPage(Album* album, unsigned int pageNo, const std::string& frontFileName, const std::string& backFileName, float width, float height) 240 { 241 osg::ref_ptr<Page> page = new Page(album, pageNo, frontFileName, backFileName, width, height); 222 242 if (page.valid()) return page.release(); 223 243 else return 0; … … 243 263 bool rotating() const { return _targetRotation!=_rotation; } 244 264 245 void setPageVisible(bool visible) { _switch->setSingleChildOn(visible?1:0); } 265 void setPageVisible(bool frontVisible,bool backVisible) 266 { 267 _switch->setValue(0,!frontVisible && !backVisible); 268 _switch->setValue(1,frontVisible); 269 _switch->setValue(2,backVisible); 270 } 246 271 247 272 osg::Switch* getSwitch() { return _switch.get(); } … … 284 309 protected: 285 310 286 Page(Album* album, unsigned int pageNo, const std::string& f ilename, float width, float height);311 Page(Album* album, unsigned int pageNo, const std::string& frontFileName, const std::string& backFileName, float width, float height); 287 312 288 313 float _rotation; … … 340 365 341 366 342 Page::Page(Album* album, unsigned int pageNo, const std::string& f ilename, float width, float height)367 Page::Page(Album* album, unsigned int pageNo, const std::string& frontFileName, const std::string& backFileName, float width, float height) 343 368 { 344 369 // set up transform parts. … … 369 394 _switch->addChild(non_visible_page); 370 395 { 371 // just an empty group for the time being... will need to create geometry soon.372 396 osg::Geometry* geom = new osg::Geometry; 373 397 geom->setStateSet(album->getBackgroundStateSet()); … … 379 403 (*coords)[3].set(width,0.0,height); 380 404 geom->setVertexArray(coords); 381 382 osg::Vec3Array* normals = new osg::Vec3Array(1); 383 (*normals)[0].set(0.0f,-1.0f,0.0f); 405 406 407 osg::Vec3Array* normals = new osg::Vec3Array(4); 408 (*normals)[0].set(-1.0f,0.0f,0.0f); 409 (*normals)[1].set(0.0f,0.0f,-1.0f); 410 (*normals)[2].set(1.0f,0.0f,0.0f); 411 (*normals)[3].set(0.0f,0.0f,1.0f); 384 412 geom->setNormalArray(normals); 385 geom->setNormalBinding(osg::Geometry::BIND_ OVERALL);413 geom->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE); 386 414 387 415 osg::Vec2Array* tcoords = new osg::Vec2Array(4); … … 397 425 geom->setColorBinding(osg::Geometry::BIND_OVERALL); 398 426 399 geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,0,4)); 427 osg::UByteArray* vindices = new osg::UByteArray(8); 428 (*vindices)[0]=0; 429 (*vindices)[1]=1; 430 (*vindices)[2]=1; 431 (*vindices)[3]=2; 432 (*vindices)[4]=2; 433 (*vindices)[5]=3; 434 (*vindices)[6]=3; 435 (*vindices)[7]=0; 436 437 geom->setVertexIndices(vindices); 438 geom->setTexCoordIndices(0,vindices); 439 440 geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,8)); 400 441 401 442 // set up the geode. … … 409 450 410 451 // set up visible page. 411 osg::Group* visible_page = new osg::Group; 412 _switch->addChild(visible_page); 413 414 { 415 416 452 osg::Group* front_page = new osg::Group; 453 _switch->addChild(front_page); 454 455 { 456 417 457 osg::Geometry* geom = new osg::Geometry; 418 458 geom->setStateSet(album->getBackgroundStateSet()); … … 449 489 450 490 451 visible_page->addChild(geode); 452 } 453 491 front_page->addChild(geode); 492 } 493 494 if (!frontFileName.empty()) 454 495 { 455 496 float cut_off_distance = 8.0f; … … 465 506 text->setAxisAlignment(osgText::Text::XZ_PLANE); 466 507 text->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); 467 text->setText(std::string("Loading ")+f ilename);508 text->setText(std::string("Loading ")+frontFileName); 468 509 469 510 osg::Geode* geode = new osg::Geode; … … 479 520 480 521 pagedlod->setRange(1,cut_off_distance,max_visible_distance); 481 pagedlod->setFileName(1,rw->insertReference(f ilename,256,width,height));522 pagedlod->setFileName(1,rw->insertReference(frontFileName,256,width,height,false)); 482 523 483 524 pagedlod->setRange(2,0.0f,cut_off_distance); 484 pagedlod->setFileName(2,rw->insertReference(filename,1024,width,height)); 485 486 visible_page->addChild(pagedlod); 487 } 488 525 pagedlod->setFileName(2,rw->insertReference(frontFileName,1024,width,height,false)); 526 527 front_page->addChild(pagedlod); 528 } 529 530 531 // set up back of page. 532 osg::Group* back_page = new osg::Group; 533 _switch->addChild(back_page); 534 535 { 536 537 osg::Geometry* geom = new osg::Geometry; 538 geom->setStateSet(album->getBackgroundStateSet()); 539 540 osg::Vec3Array* coords = new osg::Vec3Array(4); 541 (*coords)[0].set(width,0.0,height); 542 (*coords)[1].set(width,0.0,0); 543 (*coords)[2].set(0.0f,0.0,0); 544 (*coords)[3].set(0.0f,0.0,height); 545 geom->setVertexArray(coords); 546 547 osg::Vec3Array* normals = new osg::Vec3Array(1); 548 (*normals)[0].set(0.0f,1.0f,0.0f); 549 geom->setNormalArray(normals); 550 geom->setNormalBinding(osg::Geometry::BIND_OVERALL); 551 552 osg::Vec2Array* tcoords = new osg::Vec2Array(4); 553 (*tcoords)[0].set(1.0f,1.0f); 554 (*tcoords)[1].set(1.0f,0.0f); 555 (*tcoords)[2].set(0.0f,0.0f); 556 (*tcoords)[3].set(0.0f,1.0f); 557 geom->setTexCoordArray(0,tcoords); 558 559 osg::Vec4Array* colours = new osg::Vec4Array(1); 560 (*colours)[0].set(1.0f,1.0f,1.0,1.0f); 561 geom->setColorArray(colours); 562 geom->setColorBinding(osg::Geometry::BIND_OVERALL); 563 564 geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4)); 565 566 // set up the geode. 567 osg::Geode* geode = new osg::Geode; 568 geode->addDrawable(geom); 569 570 571 back_page->addChild(geode); 572 } 573 574 if (!backFileName.empty()) 575 { 576 float cut_off_distance = 8.0f; 577 float max_visible_distance = 300.0f; 578 579 osg::Vec3 center(width*0.5f,0.0f,height*0.5f); 580 581 osgText::Text* text = new osgText::Text; 582 text->setFont("fonts/arial.ttf"); 583 text->setPosition(center); 584 text->setCharacterSize(height/20.0f); 585 text->setAlignment(osgText::Text::CENTER_CENTER); 586 text->setAxisAlignment(osgText::Text::REVERSED_XZ_PLANE); 587 text->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); 588 text->setText(std::string("Loading ")+backFileName); 589 590 osg::Geode* geode = new osg::Geode; 591 geode->addDrawable(text); 592 593 osg::PagedLOD* pagedlod = new osg::PagedLOD; 594 pagedlod->setCenter(center); 595 pagedlod->setRadius(1.6f); 596 pagedlod->setNumChildrenThatCannotBeExpired(2); 597 598 pagedlod->setRange(0,max_visible_distance,1e7); 599 pagedlod->addChild(geode); 600 601 pagedlod->setRange(1,cut_off_distance,max_visible_distance); 602 pagedlod->setFileName(1,rw->insertReference(backFileName,256,width,height,true)); 603 604 pagedlod->setRange(2,0.0f,cut_off_distance); 605 pagedlod->setFileName(2,rw->insertReference(backFileName,1024,width,height,true)); 606 607 back_page->addChild(pagedlod); 608 } 609 489 610 addChild(_switch.get()); 490 611 } … … 535 656 536 657 _group = new osg::Group; 658 _group->getOrCreateStateSet()->setAttributeAndModes(new osg::CullFace,osg::StateAttribute::ON); 537 659 538 660 _backgroundStateSet = new osg::StateSet; … … 541 663 // load the images. 542 664 unsigned int i; 543 for(i=0;i<fileList.size();++i) 544 { 545 Page* page = Page::createPage(this,_pages.size(),fileList[i], width, height); 665 for(i=0;i<fileList.size();i+=2) 666 { 667 Page* page = i+1<fileList.size()? 668 Page::createPage(this,_pages.size(),fileList[i],fileList[i+1], width, height): 669 Page::createPage(this,_pages.size(),fileList[i],"", width, height); 546 670 if (page) 547 671 { … … 572 696 _pages[i]->rotateTo(osg::PI,timeToRotateBy); 573 697 } 574 _pages[pageNo]->setPageVisible(true);575 698 _currentPageNo = pageNo; 576 699 … … 583 706 _pages[i]->rotateTo(0,timeToRotateBy); 584 707 } 585 _pages[pageNo]->setPageVisible(true);586 708 _currentPageNo = pageNo; 587 709 … … 596 718 for(unsigned int i=0;i<_pages.size();++i) 597 719 { 598 _pages[i]->setPageVisible(_pages[i]->rotating()); 599 } 600 601 //_pages[0]->setPageVisible(true); 602 //if (_currentPageNo>=1) _pages[_currentPageNo-1]->setPageVisible(true); 603 _pages[_currentPageNo]->setPageVisible(true); 604 //if (_currentPageNo<_pages.size()-1) _pages[_currentPageNo+1]->setPageVisible(true); 605 //_pages[_pages.size()-1]->setPageVisible(true); 720 bool front_visible = _pages[i]->rotating() || 721 (i>0?_pages[i-1]->rotating():false) || 722 i==_currentPageNo || 723 i==0; 724 725 bool back_visible = _pages[i]->rotating() || 726 ((i+1)<_pages.size()?_pages[i+1]->rotating():false) || 727 i==_currentPageNo-1 || 728 i==_pages.size()-1; 729 730 _pages[i]->setPageVisible(front_visible,back_visible); 731 } 732 606 733 } 607 734 … … 782 909 { 783 910 Producer::Camera* cam = viewer.getCameraConfig()->getCamera(i); 784 Producer::RenderSurface* rs = cam->getRenderSurface();911 //Producer::RenderSurface* rs = cam->getRenderSurface(); 785 912 //rs->useCursor(false); 786 913 fovx = cam->getLensHorizontalFov();
