Show
Ignore:
Timestamp:
06/30/04 21:07:05 (10 years ago)
Author:
robert
Message:

Added support for using pixel size control of LOD levels.

Files:
1 modified

Legend:

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

    r3039 r3098  
    66#include <osg/Texture2D> 
    77#include <osg/PositionAttitudeTransform> 
     8#include <osg/MatrixTransform> 
    89 
    910#include <osgDB/ReadFile> 
     11 
     12#include <osgText/Text> 
    1013 
    1114#include <osgSim/SphereSegment> 
     
    1821// for the grid data.. 
    1922#include "../osghangglide/terrain_coords.h" 
     23 
     24osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime) 
     25{ 
     26    // set up the animation path  
     27    osg::AnimationPath* animationPath = new osg::AnimationPath; 
     28    animationPath->setLoopMode(osg::AnimationPath::LOOP); 
     29     
     30    int numSamples = 40; 
     31    float yaw = 0.0f; 
     32    float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f); 
     33    float roll = osg::inDegrees(30.0f); 
     34     
     35    double time=0.0f; 
     36    double time_delta = looptime/(double)numSamples; 
     37    for(int i=0;i<numSamples;++i) 
     38    { 
     39        osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f)); 
     40        osg::Quat rotation(osg::Quat(roll,osg::Vec3(0.0,1.0,0.0))*osg::Quat(-(yaw+osg::inDegrees(90.0f)),osg::Vec3(0.0,0.0,1.0))); 
     41         
     42        animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation)); 
     43 
     44        yaw += yaw_delta; 
     45        time += time_delta; 
     46 
     47    } 
     48    return animationPath;     
     49} 
     50 
     51osg::Node* createMovingModel(const osg::Vec3& center, float radius) 
     52{ 
     53    float animationLength = 10.0f; 
     54 
     55    osg::AnimationPath* animationPath = createAnimationPath(center,radius,animationLength); 
     56 
     57    osg::Group* model = new osg::Group; 
     58 
     59    osg::Node* glider = osgDB::readNodeFile("glider.osg"); 
     60    if (glider) 
     61    { 
     62        const osg::BoundingSphere& bs = glider->getBound(); 
     63 
     64        float size = radius/bs.radius()*0.3f; 
     65        osg::MatrixTransform* positioned = new osg::MatrixTransform; 
     66        positioned->setDataVariance(osg::Object::STATIC); 
     67        positioned->setMatrix(osg::Matrix::translate(-bs.center())* 
     68                                     osg::Matrix::scale(size,size,size)* 
     69                                     osg::Matrix::rotate(osg::inDegrees(-90.0f),0.0f,0.0f,1.0f)); 
     70     
     71        positioned->addChild(glider); 
     72     
     73        osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform;     
     74        xform->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON); 
     75        xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,1.0)); 
     76        xform->addChild(positioned); 
     77 
     78        model->addChild(xform); 
     79    } 
     80  
     81    osg::Node* cessna = osgDB::readNodeFile("cessna.osg"); 
     82    if (cessna) 
     83    { 
     84        const osg::BoundingSphere& bs = cessna->getBound(); 
     85 
     86        osgText::Text* text = new osgText::Text; 
     87        float size = radius/bs.radius()*0.3f; 
     88 
     89        text->setPosition(bs.center()); 
     90        text->setText("Cessna"); 
     91        text->setAlignment(osgText::Text::CENTER_CENTER); 
     92        text->setAxisAlignment(osgText::Text::SCREEN); 
     93        text->setCharacterSize(40.0f); 
     94        text->setCharacterSizeMode(osgText::Text::SCREEN_COORDS); 
     95         
     96        osg::Geode* geode = new osg::Geode; 
     97        geode->addDrawable(text); 
     98     
     99        osg::LOD* lod = new osg::LOD; 
     100        lod->setRangeMode(osg::LOD::PIXEL_SIZE_ON_SCREEN); 
     101        lod->addChild(geode,0.0f,100.0f); 
     102        lod->addChild(cessna,100.0f,10000.0f); 
     103 
     104 
     105        osg::MatrixTransform* positioned = new osg::MatrixTransform; 
     106        positioned->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON); 
     107        positioned->setDataVariance(osg::Object::STATIC); 
     108        positioned->setMatrix(osg::Matrix::translate(-bs.center())* 
     109                                     osg::Matrix::scale(size,size,size)* 
     110                                     osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,1.0f)); 
     111     
     112        //positioned->addChild(cessna); 
     113        positioned->addChild(lod); 
     114     
     115        osg::MatrixTransform* xform = new osg::MatrixTransform; 
     116        xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,2.0)); 
     117        xform->addChild(positioned); 
     118 
     119        model->addChild(xform); 
     120    } 
     121     
     122    return model; 
     123} 
     124 
    20125 
    21126osg::Vec3 computeTerrainIntersection(osg::Node* subgraph,float x,float y) 
     
    133238        root->addChild(psu); 
    134239    } 
     240     
     241     
     242    // create the moving models. 
     243    { 
     244        root->addChild(createMovingModel(osg::Vec3(500.0f,500.0f,500.0f),100.0f)); 
     245    } 
    135246} 
    136247