Show
Ignore:
Timestamp:
09/13/04 15:53:45 (10 years ago)
Author:
robert
Message:

Improved the handling of building/writing generated databases, and
the fixed transition distances on the lower levels of geocentric databases.

Files:
1 modified

Legend:

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

    r3340 r3403  
    77#include <osg/PositionAttitudeTransform> 
    88#include <osg/MatrixTransform> 
     9#include <osg/CoordinateSystemNode> 
    910 
     11#include <osgDB/FileUtils> 
    1012#include <osgDB/ReadFile> 
    1113 
    1214#include <osgText/Text> 
    1315 
    14 #include <osgSim/SphereSegment> 
    15  
    16 #include <osgParticle/ExplosionEffect> 
    17 #include <osgParticle/SmokeEffect> 
    18 #include <osgParticle/FireEffect> 
    19 #include <osgParticle/ParticleSystemUpdater> 
     16#include <osgTerrain/DataSet> 
    2017 
    2118#include <osgGA/NodeTrackerManipulator> 
    2219 
    23 // for the grid data.. 
    24 #include "../osghangglide/terrain_coords.h" 
     20class GraphicsContext { 
     21    public: 
     22        GraphicsContext() 
     23        { 
     24            rs = new Producer::RenderSurface; 
     25            rs->setWindowRectangle(0,0,1,1); 
     26            rs->useBorder(false); 
     27            rs->useConfigEventThread(false); 
     28            rs->realize(); 
     29            std::cout<<"Realized window"<<std::endl; 
     30        } 
    2531 
    26 osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime) 
     32        virtual ~GraphicsContext() 
     33        { 
     34        } 
     35         
     36    private: 
     37        Producer::ref_ptr<Producer::RenderSurface> rs; 
     38}; 
     39 
     40osg::Node* createEarth() 
    2741{ 
    28     // set up the animation path  
    29     osg::AnimationPath* animationPath = new osg::AnimationPath; 
    30     animationPath->setLoopMode(osg::AnimationPath::LOOP); 
     42    osg::ref_ptr<osg::Node> scene; 
    3143     
    32     int numSamples = 40; 
    33     float yaw = 0.0f; 
    34     float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f); 
    35     float roll = osg::inDegrees(30.0f); 
     44    { 
     45        std::string filename = osgDB::findDataFile("Images/land_shallow_topo_2048.jpg"); 
     46 
     47        osg::ref_ptr<osgTerrain::DataSet> dataSet = new osgTerrain::DataSet; 
     48 
     49        // register the source imagery 
     50        { 
     51            osgTerrain::DataSet::Source* source = new osgTerrain::DataSet::Source(osgTerrain::DataSet::Source::IMAGE, filename); 
     52 
     53            source->setCoordinateSystemPolicy(osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS); 
     54            source->setCoordinateSystem(osgTerrain::DataSet::coordinateSystemStringToWTK("WGS84")); 
     55 
     56            source->setGeoTransformPolicy(osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS_BUT_SCALE_BY_FILE_RESOLUTION); 
     57            source->setGeoTransformFromRange(-180.0, 180.0, -90.0, 90.0); 
     58 
     59            dataSet->addSource(source); 
     60        } 
     61 
     62        // set up destination database paramters. 
     63        dataSet->setDatabaseType(osgTerrain::DataSet::LOD_DATABASE); 
     64        dataSet->setConvertFromGeographicToGeocentric(true); 
     65        dataSet->setDestinationName("test.osg"); 
     66 
     67        // load the source data and record sizes. 
     68        dataSet->loadSources(); 
     69 
     70        GraphicsContext context; 
     71        dataSet->createDestination(30); 
     72 
     73        if (dataSet->getDatabaseType()==osgTerrain::DataSet::LOD_DATABASE) dataSet->buildDestination(); 
     74        else dataSet->writeDestination(); 
     75         
     76        scene = dataSet->getDestinationRootNode(); 
     77    } 
     78         
     79    return scene.release(); 
    3680     
    37     double time=0.0f; 
    38     double time_delta = looptime/(double)numSamples; 
    39     for(int i=0;i<numSamples;++i) 
    40     { 
    41         osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f)); 
    42         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))); 
    43          
    44         animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation)); 
    45  
    46         yaw += yaw_delta; 
    47         time += time_delta; 
    48  
    49     } 
    50     return animationPath;     
    5181} 
    5282 
    53 osg::Node* createMovingModel(const osg::Vec3& center, float radius) 
    54 { 
    55     float animationLength = 10.0f; 
    56  
    57     osg::AnimationPath* animationPath = createAnimationPath(center,radius,animationLength); 
    58  
    59     osg::Group* model = new osg::Group; 
    60  
    61     osg::Node* glider = osgDB::readNodeFile("glider.osg"); 
    62     if (glider) 
    63     { 
    64         glider->setName("glider"); 
    65  
    66         const osg::BoundingSphere& bs = glider->getBound(); 
    67  
    68         float size = radius/bs.radius()*0.3f; 
    69         osg::MatrixTransform* positioned = new osg::MatrixTransform; 
    70         positioned->setDataVariance(osg::Object::STATIC); 
    71         positioned->setMatrix(osg::Matrix::translate(-bs.center())* 
    72                                      osg::Matrix::scale(size,size,size)* 
    73                                      osg::Matrix::rotate(osg::inDegrees(-90.0f),0.0f,0.0f,1.0f)); 
    74      
    75         positioned->addChild(glider); 
    76      
    77         osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform;     
    78         xform->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON); 
    79         xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,1.0)); 
    80         xform->addChild(positioned); 
    81  
    82         model->addChild(xform); 
    83     } 
    84   
    85     osg::Node* cessna = osgDB::readNodeFile("cessna.osg"); 
    86     if (cessna) 
    87     { 
    88         cessna->setName("cessna"); 
    89      
    90         const osg::BoundingSphere& bs = cessna->getBound(); 
    91  
    92         osgText::Text* text = new osgText::Text; 
    93         float size = radius/bs.radius()*0.3f; 
    94  
    95         text->setPosition(bs.center()); 
    96         text->setText("Cessna"); 
    97         text->setAlignment(osgText::Text::CENTER_CENTER); 
    98         text->setAxisAlignment(osgText::Text::SCREEN); 
    99         text->setCharacterSize(40.0f); 
    100         text->setCharacterSizeMode(osgText::Text::SCREEN_COORDS); 
    101          
    102         osg::Geode* geode = new osg::Geode; 
    103         geode->addDrawable(text); 
    104      
    105         osg::LOD* lod = new osg::LOD; 
    106         lod->setRangeMode(osg::LOD::PIXEL_SIZE_ON_SCREEN); 
    107         lod->addChild(geode,0.0f,100.0f); 
    108         lod->addChild(cessna,100.0f,10000.0f); 
    109  
    110  
    111         osg::MatrixTransform* positioned = new osg::MatrixTransform; 
    112         positioned->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON); 
    113         positioned->setDataVariance(osg::Object::STATIC); 
    114         positioned->setMatrix(osg::Matrix::translate(-bs.center())* 
    115                                      osg::Matrix::scale(size,size,size)* 
    116                                      osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,1.0f)); 
    117      
    118         //positioned->addChild(cessna); 
    119         positioned->addChild(lod); 
    120      
    121         osg::MatrixTransform* xform = new osg::MatrixTransform; 
    122         xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,2.0)); 
    123         xform->addChild(positioned); 
    124  
    125         model->addChild(xform); 
    126     } 
    127      
    128     return model; 
    129 } 
    130  
    131  
    132 osg::Vec3 computeTerrainIntersection(osg::Node* subgraph,float x,float y) 
    133 { 
    134     osgUtil::IntersectVisitor iv; 
    135     osg::ref_ptr<osg::LineSegment> segDown = new osg::LineSegment; 
    136  
    137     const osg::BoundingSphere& bs = subgraph->getBound(); 
    138     float zMax = bs.center().z()+bs.radius(); 
    139     float zMin = bs.center().z()-bs.radius(); 
    140      
    141     segDown->set(osg::Vec3(x,y,zMin),osg::Vec3(x,y,zMax)); 
    142     iv.addLineSegment(segDown.get()); 
    143  
    144     subgraph->accept(iv); 
    145  
    146     if (iv.hits()) 
    147     { 
    148         osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segDown.get()); 
    149         if (!hitList.empty()) 
    150         { 
    151             osg::Vec3 ip = hitList.front().getWorldIntersectPoint(); 
    152             return  ip; 
    153         } 
    154     } 
    155  
    156     return osg::Vec3(x,y,0.0f); 
    157 } 
    158  
    159  
    160 ////////////////////////////////////////////////////////////////////////////// 
    161 // MAIN SCENE GRAPH BUILDING FUNCTION 
    162 ////////////////////////////////////////////////////////////////////////////// 
    163  
    164 void build_world(osg::Group *root) 
    165 { 
    166  
    167     osg::Geode* terrainGeode = new osg::Geode; 
    168     // create terrain 
    169     { 
    170         osg::StateSet* stateset = new osg::StateSet(); 
    171         osg::Image* image = osgDB::readImageFile("Images/lz.rgb"); 
    172         if (image) 
    173         { 
    174             osg::Texture2D* texture = new osg::Texture2D; 
    175             texture->setImage(image); 
    176             stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); 
    177         } 
    178  
    179         terrainGeode->setStateSet( stateset ); 
    180  
    181         float size = 1000; // 10km; 
    182         float scale = size/39.0f; // 10km; 
    183         float z_scale = scale*3.0f; 
    184  
    185         osg::HeightField* grid = new osg::HeightField; 
    186         grid->allocateGrid(38,39); 
    187         grid->setXInterval(scale); 
    188         grid->setYInterval(scale); 
    189  
    190         for(unsigned int r=0;r<39;++r) 
    191         { 
    192             for(unsigned int c=0;c<38;++c) 
    193             { 
    194                 grid->setHeight(c,r,z_scale*vertex[r+c*39][2]); 
    195             } 
    196         } 
    197         terrainGeode->addDrawable(new osg::ShapeDrawable(grid)); 
    198          
    199         root->addChild(terrainGeode); 
    200     }     
    201  
    202     // create sphere segment 
    203     { 
    204         osgSim::SphereSegment* ss = new osgSim::SphereSegment( 
    205                         computeTerrainIntersection(terrainGeode,550.0f,780.0f), // center 
    206                         500.0f, // radius 
    207                         osg::DegreesToRadians(135.0f), 
    208                         osg::DegreesToRadians(245.0f), 
    209                         osg::DegreesToRadians(-10.0f), 
    210                         osg::DegreesToRadians(30.0f), 
    211                         60); 
    212         ss->setAllColors(osg::Vec4(1.0f,1.0f,1.0f,0.5f)); 
    213         ss->setSideColor(osg::Vec4(0.0f,1.0f,1.0f,0.1f)); 
    214  
    215         root->addChild(ss);         
    216     } 
    217  
    218  
    219     // create particle effects 
    220     {     
    221         osg::PositionAttitudeTransform* positionEffects = new osg::PositionAttitudeTransform; 
    222         positionEffects->setPosition(computeTerrainIntersection(terrainGeode,100.0f,100.0f)); 
    223         root->addChild(positionEffects); 
    224  
    225         osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect; 
    226         osgParticle::SmokeEffect* smoke = new osgParticle::SmokeEffect; 
    227         osgParticle::FireEffect* fire = new osgParticle::FireEffect; 
    228  
    229         osg::Geode* geode = new osg::Geode; 
    230         geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),10.0f))); 
    231         positionEffects->addChild(geode); 
    232  
    233         positionEffects->addChild(explosion); 
    234         positionEffects->addChild(smoke); 
    235         positionEffects->addChild(fire); 
    236  
    237         osgParticle::ParticleSystemUpdater *psu = new osgParticle::ParticleSystemUpdater; 
    238  
    239         psu->addParticleSystem(explosion->getParticleSystem()); 
    240         psu->addParticleSystem(smoke->getParticleSystem()); 
    241         psu->addParticleSystem(fire->getParticleSystem()); 
    242  
    243         // add the updater node to the scene graph 
    244         root->addChild(psu); 
    245     } 
    246      
    247      
    248     // create the moving models. 
    249     { 
    250         root->addChild(createMovingModel(osg::Vec3(500.0f,500.0f,500.0f),100.0f)); 
    251     } 
    252 } 
    25383 
    25484class FindNamedNodeVisitor : public osg::NodeVisitor 
     
    274104}; 
    275105 
    276  
    277  
    278 ////////////////////////////////////////////////////////////////////////////// 
    279 // main() 
    280 ////////////////////////////////////////////////////////////////////////////// 
    281106 
    282107 
     
    320145    } 
    321146     
    322     osg::Group *root = new osg::Group; 
    323     build_world(root); 
     147    osg::Node *root = createEarth(); 
     148 
     149    if (!root) return 0; 
    324150    
    325151    // add a viewport to the viewer and attach the scene graph.