Changeset 4280 for OpenSceneGraph/trunk/examples/osgcatch/osgcatch.cpp
- Timestamp:
- 05/21/05 14:57:28 (8 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/examples/osgcatch/osgcatch.cpp
r4071 r4280 32 32 33 33 typedef std::vector<std::string> FileList; 34 typedef std::map<std::string, osg::ref_ptr<osg::Node> > ObjectMap; 35 36 static ObjectMap s_objectMap; 34 37 35 38 class Character : public osg::Referenced … … 38 41 Character(); 39 42 40 void setCharacter(const std::string& filename, const std::string& name, const osg::Vec3& orgin, const osg::Vec3& width, float positionRatio);43 void setCharacter(const std::string& filename, const std::string& name, const osg::Vec3& orgin, const osg::Vec3& width, const osg::Vec3& catchPos, float positionRatio); 41 44 42 45 void setLives(const std::string& filename, const osg::Vec3& orgin, const osg::Vec3& delta, unsigned int numLives); … … 86 89 87 90 88 void Character::setCharacter(const std::string& filename, const std::string& name, const osg::Vec3& origin, const osg::Vec3& width, float positionRatio)91 void Character::setCharacter(const std::string& filename, const std::string& name, const osg::Vec3& origin, const osg::Vec3& width, const osg::Vec3& catchPos, float positionRatio) 89 92 { 90 93 _origin = origin; … … 118 121 moveTo(positionRatio); 119 122 120 _centerBasket = width* 0.2 + height*0.57+ pos;121 _radiusBasket = width.length()* 0.34;123 _centerBasket = width*catchPos.x() + height*catchPos.y() + pos; 124 _radiusBasket = width.length()*catchPos.z(); 122 125 123 126 } … … 277 280 double _timeToRemove; 278 281 282 static void setUpCatchablesMap(const FileList& fileList); 279 283 280 284 public: … … 347 351 float _viscosityCoefficient; 348 352 float _densityCoefficeint; 353 349 354 350 355 }; … … 357 362 _timeToRemove = -1.0; // do not remove. 358 363 setFluidToAir(); 364 } 365 366 void CatchableObject::setUpCatchablesMap(const FileList& fileList) 367 { 368 for(FileList::const_iterator itr=fileList.begin(); 369 itr!=fileList.end(); 370 ++itr) 371 { 372 const std::string& filename = *itr; 373 osg::Image* image = osgDB::readImageFile(filename); 374 if (image) 375 { 376 osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet(); 377 stateset->setTextureAttributeAndModes(0,new osg::Texture2D(image),osg::StateAttribute::ON); 378 stateset->setMode(GL_BLEND,osg::StateAttribute::ON); 379 stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); 380 381 osg::Vec3 width((float)(image->s())/(float)(image->t()),0.0f,0.0); 382 osg::Vec3 height(0.0f,0.0f,1.0f); 383 osg::Vec3 pos = (width+height)*-0.5f; 384 385 osg::Geometry* geometry = osg::createTexturedQuadGeometry(pos,width,height); 386 geometry->setStateSet(stateset.get()); 387 388 osg::Geode* geode = new osg::Geode; 389 geode->addDrawable(geometry); 390 391 s_objectMap[filename] = geode; 392 } 393 } 359 394 } 360 395 … … 368 403 _mass = 1000.0*Volume; 369 404 370 osg::Image* image = osgDB::readImageFile(filename); 371 if (image) 372 { 373 osg::Vec3 width(characterSize*((float)image->s())/(float)(image->t()),0.0f,0.0); 374 osg::Vec3 height(0.0f,0.0f,characterSize); 375 osg::Vec3 pos = (width+height)*-0.5f; 376 377 osg::Geometry* geometry = osg::createTexturedQuadGeometry(pos,width,height); 378 osg::StateSet* stateset = geometry->getOrCreateStateSet(); 379 stateset->setTextureAttributeAndModes(0,new osg::Texture2D(image),osg::StateAttribute::ON); 380 stateset->setMode(GL_BLEND,osg::StateAttribute::ON); 381 stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); 382 383 osg::Geode* geode = new osg::Geode; 384 geode->addDrawable(geometry); 405 if (s_objectMap.count(filename)!=0) 406 { 407 osg::PositionAttitudeTransform* scaleTransform = new osg::PositionAttitudeTransform; 408 scaleTransform->setScale(osg::Vec3(characterSize,characterSize,characterSize)); 409 scaleTransform->addChild(s_objectMap[filename].get()); 385 410 386 411 _object = new osg::PositionAttitudeTransform; 387 412 _object->setName(name); 388 _object->addChild(geode);389 413 _object->setPosition(center); 390 } 391 414 _object->addChild(scaleTransform); 415 } 416 else 417 { 418 osg::notify(osg::NOTICE)<<"CatchableObject::setObject("<<filename<<") not able to create catchable object."<<std::endl; 419 } 392 420 } 393 421 … … 480 508 481 509 void createNewCatchable(); 510 511 enum Players 512 { 513 PLAYER_GIRL, 514 PLAYER_BOY 515 }; 516 517 void addPlayer(Players player) 518 { 519 osg::Vec3 livesPosition; 520 osg::Vec3 catchesPosition; 521 if (_numberOfPlayers==0) 522 { 523 livesPosition = _originBaseLine+osg::Vec3(0.0f,-0.5f,0.0f); 524 catchesPosition = _originBaseLine+osg::Vec3(200.0f,-0.5f,0.0f); 525 } 526 else 527 { 528 livesPosition = _originBaseLine+osg::Vec3(900.0f,-0.5f,000.0f); 529 catchesPosition = _originBaseLine+osg::Vec3(1100.0f,-0.5f,0.0f); 530 } 531 532 switch(player) 533 { 534 case PLAYER_GIRL: 535 { 536 std::string player_one = "Catch/girl.png"; 537 osg::Vec3 catchPos(0.2, 0.57, 0.34); 538 539 _players[_numberOfPlayers].setCharacter(player_one,"girl", _originBaseLine + osg::Vec3(0.0f,-1.0f,0.0f), _widthBaseLine, catchPos, 0.5f); 540 _players[_numberOfPlayers].setLives(player_one,livesPosition, osg::Vec3(0.0f,0.0f,100.0f),3); 541 _players[_numberOfPlayers].setCatches("Catch/broach.png",catchesPosition, osg::Vec3(0.0f,0.0f,100.0f),10); 542 543 ++_numberOfPlayers; 544 break; 545 } 546 case PLAYER_BOY: 547 { 548 std::string player_two = "Catch/boy.png"; 549 osg::Vec3 catchPos(0.8, 0.57, 0.34); 550 551 _players[_numberOfPlayers].setCharacter(player_two,"boy", _originBaseLine + osg::Vec3(0.0f,-2.0f,0.0f), _widthBaseLine, catchPos, 0.5f); 552 _players[_numberOfPlayers].setLives(player_two,livesPosition, osg::Vec3(0.0f,0.0f,100.0f),3); 553 _players[_numberOfPlayers].setCatches("Catch/broach.png",catchesPosition, osg::Vec3(0.0f,0.0f,100.0f),10); 554 555 ++_numberOfPlayers; 556 break; 557 } 558 } 559 } 560 561 482 562 483 563 protected: … … 499 579 osg::ref_ptr<osg::Group> _group; 500 580 501 Character _player1;502 Character _player 2;581 unsigned int _numberOfPlayers; 582 Character _players[2]; 503 583 504 584 typedef std::list< osg::ref_ptr<CatchableObject> > CatchableObjectList; 505 585 CatchableObjectList _catchableObjects; 586 587 FileList _backgroundFiles; 588 FileList _benignCatachables; 506 589 507 590 bool _leftKeyPressed; … … 522 605 _characterSize = _width.length()*0.2f; 523 606 524 _backgroundImageFile = "Catch/sky1.JPG"; 607 _backgroundImageFile = "Catch/farm.JPG"; 608 609 _numberOfPlayers = 0; 525 610 526 611 _leftKeyPressed=false; 527 612 _rightKeyPressed=false; 613 614 _backgroundFiles.push_back("Catch/farm.JPG"); 615 _backgroundFiles.push_back("Catch/sky1.JPG"); 616 _backgroundFiles.push_back("Catch/sky2.JPG"); 617 _backgroundFiles.push_back("Catch/sky3.JPG"); 618 619 _benignCatachables.push_back("Catch/a.png"); 620 _benignCatachables.push_back("Catch/b.png"); 621 _benignCatachables.push_back("Catch/c.png"); 622 _benignCatachables.push_back("Catch/m.png"); 623 _benignCatachables.push_back("Catch/n.png"); 624 _benignCatachables.push_back("Catch/s.png"); 625 _benignCatachables.push_back("Catch/t.png"); 626 _benignCatachables.push_back("Catch/u.png"); 627 _benignCatachables.push_back("Catch/ball.png"); 628 629 CatchableObject::setUpCatchablesMap(_benignCatachables); 630 631 632 528 633 } 529 634 … … 537 642 if (_leftKeyPressed) 538 643 { 539 _player2.moveLeft();644 if (_numberOfPlayers>=2) _players[1].moveLeft(); 540 645 } 541 646 542 647 if (_rightKeyPressed) 543 648 { 544 _player2.moveRight();649 if (_numberOfPlayers>=2) _players[1].moveRight(); 545 650 } 546 651 … … 558 663 bool removeEntry = false; 559 664 560 if ((*itr)->dangerous())665 for(unsigned int i=0;i<_numberOfPlayers;++i) 561 666 { 562 if ((*itr)-> anyInside(_player1.getLowerLeft(),_player1.getUpperRight()))667 if ((*itr)->dangerous()) 563 668 { 564 _player1.looseLife(); 565 removeEntry = true; 669 if ((*itr)->anyInside(_players[i].getLowerLeft(),_players[i].getUpperRight())) 670 { 671 _players[i].looseLife(); 672 removeEntry = true; 673 } 566 674 } 567 568 if ((*itr)->anyInside(_player2.getLowerLeft(),_player2.getUpperRight())) 675 else 569 676 { 570 _player2.looseLife(); 571 removeEntry = true; 677 if ((*itr)->centerInside(_players[i].getCurrentCenterOfBasket(),_players[i].getCurrentRadiusOfBasket())) 678 { 679 _players[i].addCatch(); 680 removeEntry = true; 681 } 572 682 } 573 683 } 574 else575 {576 if ((*itr)->centerInside(_player1.getCurrentCenterOfBasket(),_player1.getCurrentRadiusOfBasket()))577 {578 _player1.addCatch();579 removeEntry = true;580 }581 582 if ((*itr)->centerInside(_player2.getCurrentCenterOfBasket(),_player2.getCurrentRadiusOfBasket()))583 {584 _player2.addCatch();585 removeEntry = true;586 }587 }588 589 684 590 685 if (!(*itr)->anyInside(_origin, _origin+_width+_height) || … … 632 727 return true; 633 728 } 634 else if (ea.getKey()=='1')635 {636 _player1.looseLife();637 _player1.addCatch();638 return true;639 }640 else if (ea.getKey()=='2')641 {642 _player2.looseLife();643 _player2.addCatch();644 return true;645 }646 729 else if (ea.getKey()==' ') 647 730 { 648 _player1.reset(); 649 _player2.reset(); 731 for(unsigned int i=0;i<_numberOfPlayers;++i) 732 { 733 _players[i].reset(); 734 } 650 735 return true; 651 736 } … … 669 754 float px = (ea.getXnormalized()+1.0f)*0.5f; 670 755 671 _player1.moveTo(px);756 if (_numberOfPlayers>=1) _players[0].moveTo(px); 672 757 673 758 return true; … … 698 783 _group = new osg::Group; 699 784 700 std::string player_one = "Catch/girl.png"; 701 std::string player_two = "Catch/boy.png"; 702 703 _player1.setCharacter(player_one,"girl", _originBaseLine + osg::Vec3(0.0f,-1.0f,0.0f), _widthBaseLine, 0.4f); 704 _player1.setLives(player_one,_originBaseLine+osg::Vec3(0.0f,-0.5f,0.0f), osg::Vec3(0.0f,0.0f,100.0f),3); 705 _player1.setCatches("Catch/broach.png",_originBaseLine+osg::Vec3(200.0f,-0.5f,0.0f), osg::Vec3(0.0f,0.0f,100.0f),10); 706 _group->addChild(_player1._character.get()); 707 _group->addChild(_player1._livesSwitch.get()); 708 _group->addChild(_player1._catchSwitch.get()); 709 710 _player2.setCharacter(player_two,"boy", _originBaseLine + osg::Vec3(0.0f,-2.0f,0.0f), _widthBaseLine, 0.4f); 711 _player2.setLives(player_two,_originBaseLine+osg::Vec3(900.0f,-0.5f,000.0f), osg::Vec3(0.0f,0.0f,100.0f),3); 712 _player2.setCatches("Catch/broach.png",_originBaseLine+osg::Vec3(1100.0f,-0.5f,0.0f), osg::Vec3(0.0f,0.0f,100.0f),10); 713 _group->addChild(_player2._character.get()); 714 _group->addChild(_player2._livesSwitch.get()); 715 _group->addChild(_player2._catchSwitch.get()); 716 785 if (_numberOfPlayers==0) 786 { 787 addPlayer(PLAYER_GIRL); 788 } 789 790 for(unsigned int i=0;i<_numberOfPlayers;++i) 791 { 792 _group->addChild(_players[i]._character.get()); 793 _group->addChild(_players[i]._livesSwitch.get()); 794 _group->addChild(_players[i]._catchSwitch.get()); 795 } 717 796 718 797 createNewCatchable(); … … 743 822 void GameEventHandler::createNewCatchable() 744 823 { 824 if (_benignCatachables.empty()) return; 825 826 unsigned int catachableIndex = (unsigned int)((float)_benignCatachables.size()*(float)rand()/(float)RAND_MAX); 827 if (catachableIndex>=_benignCatachables.size()) catachableIndex = _benignCatachables.size()-1; 828 829 const std::string& filename = _benignCatachables[catachableIndex]; 830 745 831 float ratio = ((float)rand() / (float)RAND_MAX); 746 832 float size = 100.0f*((float)rand() / (float)RAND_MAX); … … 752 838 osg::Vec3 velocity(-cosf(angle)*speed,0.0f,-sinf(angle)*speed); 753 839 //std::cout<<"angle = "<<angle<<" velocity="<<velocity<<std::endl; 754 catchableObject->setObject( "Catch/a.png","boy",position,size,velocity);840 catchableObject->setObject(filename,"boy",position,size,velocity); 755 841 _catchableObjects.push_back(catchableObject); 756 842 … … 793 879 794 880 881 while (arguments.read("--boy")) seh->addPlayer(GameEventHandler::PLAYER_BOY); 882 while (arguments.read("--girl")) seh->addPlayer(GameEventHandler::PLAYER_GIRL); 883 884 885 795 886 // get details on keyboard and mouse bindings used by the viewer. 796 887 viewer.getUsage(*arguments.getApplicationUsage());
