Show
Ignore:
Timestamp:
09/30/03 17:50:46 (11 years ago)
Author:
robert
Message:

Added text hud instructions.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgforest/osgforest.cpp

    r2327 r2328  
     1#include <osg/AlphaFunc> 
     2#include <osg/Billboard> 
     3#include <osg/BlendFunc> 
     4#include <osg/Depth> 
    15#include <osg/Geode> 
     6#include <osg/Geometry> 
     7#include <osg/Material> 
     8#include <osg/Math> 
     9#include <osg/MatrixTransform> 
     10#include <osg/PolygonOffset> 
     11#include <osg/Projection> 
    212#include <osg/ShapeDrawable> 
    3 #include <osg/Material> 
     13#include <osg/StateSet> 
     14#include <osg/Switch> 
    415#include <osg/Texture2D> 
    5 #include <osg/Billboard> 
    6 #include <osg/AlphaFunc> 
    7 #include <osg/BlendFunc> 
    8 #include <osg/StateSet> 
    9 #include <osg/Geometry> 
    10 #include <osg/MatrixTransform> 
    11 #include <osg/Switch> 
    12  
    13 #include <osgProducer/Viewer> 
    1416 
    1517#include <osgDB/ReadFile> 
     18 
    1619#include <osgUtil/IntersectVisitor> 
    1720#include <osgUtil/SmoothingVisitor> 
    1821 
    19 #include <osg/Math> 
     22#include <osgText/Text> 
     23 
     24#include <osgProducer/Viewer> 
    2025 
    2126// for the grid data.. 
     
    103108 
    104109    osg::Node* createTransformGraph(Cell* cell,osg::StateSet* stateset); 
    105  
    106     osg::Node* createScene(); 
     110     
     111    osg::Node* createHUDWithText(const std::string& text); 
     112 
     113    osg::Node* createScene(unsigned int numTreesToCreates); 
    107114     
    108115    void advanceToNextTechnique(int delta=1) 
     
    110117        if (_techniqueSwitch.valid()) 
    111118        { 
    112             _currentTechnique = (_currentTechnique + delta)%_techniqueSwitch->getNumChildren(); 
     119            _currentTechnique += delta; 
     120            if (_currentTechnique<0) 
     121                _currentTechnique = _techniqueSwitch->getNumChildren()-1; 
     122            if (_currentTechnique>=(int)_techniqueSwitch->getNumChildren()) 
     123                _currentTechnique = 0; 
    113124            _techniqueSwitch->setSingleChildOn(_currentTechnique); 
    114125        } 
     
    702713} 
    703714 
    704  
    705 osg::Node* ForestTechniqueManager::createScene() 
     715osg::Node* ForestTechniqueManager::createHUDWithText(const std::string& str) 
     716{ 
     717    osg::Geode* geode = new osg::Geode(); 
     718     
     719    std::string timesFont("fonts/arial.ttf"); 
     720 
     721    // turn lighting off for the text and disable depth test to ensure its always ontop. 
     722    osg::StateSet* stateset = geode->getOrCreateStateSet(); 
     723    stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); 
     724 
     725    // or disable depth test, and make sure that the hud is drawn after everything  
     726    // else so that it always appears ontop. 
     727    stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); 
     728    stateset->setRenderBinDetails(11,"RenderBin"); 
     729 
     730    osg::Vec3 position(150.0f,800.0f,0.0f); 
     731    osg::Vec3 delta(0.0f,-120.0f,0.0f); 
     732 
     733    { 
     734        osgText::Text* text = new  osgText::Text; 
     735        geode->addDrawable( text ); 
     736 
     737        text->setFont(timesFont); 
     738        text->setPosition(position); 
     739        text->setText(str); 
     740         
     741        position += delta; 
     742    }     
     743 
     744    
     745    // create the hud. 
     746    osg::MatrixTransform* modelview_abs = new osg::MatrixTransform; 
     747    modelview_abs->setReferenceFrame(osg::Transform::RELATIVE_TO_ABSOLUTE); 
     748    modelview_abs->setMatrix(osg::Matrix::identity()); 
     749    modelview_abs->addChild(geode); 
     750 
     751    osg::Projection* projection = new osg::Projection; 
     752    projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024)); 
     753    projection->addChild(modelview_abs); 
     754 
     755    return projection; 
     756} 
     757 
     758osg::Node* ForestTechniqueManager::createScene(unsigned int numTreesToCreates) 
    706759{ 
    707760    osg::Vec3 origin(0.0f,0.0f,0.0f); 
    708761    osg::Vec3 size(1000.0f,1000.0f,200.0f); 
    709     unsigned int numTreesToCreates = 10000; 
    710762 
    711763    std::cout<<"Creating terrain..."; 
     
    747799    _techniqueSwitch = new osg::Switch; 
    748800     
    749     std::cout<<"Creating billboard based forest..."; 
    750    _techniqueSwitch->addChild(createBillboardGraph(cell.get(),dstate)); 
    751     std::cout<<"done."<<std::endl; 
    752  
    753     std::cout<<"Creating double quad based forest..."; 
    754    _techniqueSwitch->addChild(createXGraph(cell.get(),dstate)); 
    755     std::cout<<"done."<<std::endl; 
    756  
    757     std::cout<<"Creating transform based forest..."; 
    758    _techniqueSwitch->addChild(createTransformGraph(cell.get(),dstate)); 
    759     std::cout<<"done."<<std::endl; 
     801    { 
     802        std::cout<<"Creating billboard based forest..."; 
     803        osg::Group* group = new osg::Group; 
     804        group->addChild(createBillboardGraph(cell.get(),dstate)); 
     805        group->addChild(createHUDWithText("Using osg::Billboard's to create a forest\n\nPress left cursor key to select osg::MatrixTransform based forest\nPress right cursor key to select double quad based forest")); 
     806        _techniqueSwitch->addChild(group); 
     807        std::cout<<"done."<<std::endl; 
     808    } 
     809     
     810    { 
     811        std::cout<<"Creating billboard based forest..."; 
     812        osg::Group* group = new osg::Group; 
     813        group->addChild(createXGraph(cell.get(),dstate)); 
     814        group->addChild(createHUDWithText("Using double quads to create a forest\n\nPress left cursor key to select osg::Billboard based forest\nPress right cursor key to select osg::MatrixTransform based forest\n")); 
     815        _techniqueSwitch->addChild(group); 
     816        std::cout<<"done."<<std::endl; 
     817    } 
     818 
     819    { 
     820        std::cout<<"Creating billboard based forest..."; 
     821        osg::Group* group = new osg::Group; 
     822        group->addChild(createTransformGraph(cell.get(),dstate)); 
     823        group->addChild(createHUDWithText("Using osg::MatrixTransform's to create a forest\n\nPress left cursor key to select double quad based forest\nPress right cursor key to select osg::Billboard based forest")); 
     824        _techniqueSwitch->addChild(group); 
     825        std::cout<<"done."<<std::endl; 
     826    } 
    760827     
    761828    _currentTechnique = 0; 
     
    781848    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); 
    782849    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); 
     850    arguments.getApplicationUsage()->addCommandLineOption("--trees <number>","Set the number of trees to create"); 
    783851    
    784852    // construct the viewer. 
    785853    osgProducer::Viewer viewer(arguments); 
    786854 
     855    float numTreesToCreates = 10000; 
     856    arguments.read("--trees",numTreesToCreates); 
     857 
    787858    // set up the value with sensible default event handlers. 
    788859    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); 
     
    812883    } 
    813884     
    814     osg::Node* node = ttm->createScene(); 
     885    osg::Node* node = ttm->createScene((unsigned int)numTreesToCreates); 
    815886 
    816887    // add model to viewer.