| 1 | #include <osgProducer/Viewer> |
|---|
| 2 | |
|---|
| 3 | #include <osg/Group> |
|---|
| 4 | #include <osg/Geode> |
|---|
| 5 | #include <osg/ShapeDrawable> |
|---|
| 6 | #include <osg/Texture2D> |
|---|
| 7 | #include <osg/PositionAttitudeTransform> |
|---|
| 8 | |
|---|
| 9 | #include <osgDB/ReadFile> |
|---|
| 10 | |
|---|
| 11 | #include <osgParticle/ExplosionEffect> |
|---|
| 12 | #include <osgParticle/SmokeEffect> |
|---|
| 13 | #include <osgParticle/FireEffect> |
|---|
| 14 | #include <osgParticle/ParticleSystemUpdater> |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | #include "../osghangglide/terrain_coords.h" |
|---|
| 18 | |
|---|
| 19 | osg::Vec3 computeTerrainIntersection(osg::Node* subgraph,float x,float y) |
|---|
| 20 | { |
|---|
| 21 | osgUtil::IntersectVisitor iv; |
|---|
| 22 | osg::ref_ptr<osg::LineSegment> segDown = new osg::LineSegment; |
|---|
| 23 | |
|---|
| 24 | const osg::BoundingSphere& bs = subgraph->getBound(); |
|---|
| 25 | float zMax = bs.center().z()+bs.radius(); |
|---|
| 26 | float zMin = bs.center().z()-bs.radius(); |
|---|
| 27 | |
|---|
| 28 | segDown->set(osg::Vec3(x,y,zMin),osg::Vec3(x,y,zMax)); |
|---|
| 29 | iv.addLineSegment(segDown.get()); |
|---|
| 30 | |
|---|
| 31 | subgraph->accept(iv); |
|---|
| 32 | |
|---|
| 33 | if (iv.hits()) |
|---|
| 34 | { |
|---|
| 35 | osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segDown.get()); |
|---|
| 36 | if (!hitList.empty()) |
|---|
| 37 | { |
|---|
| 38 | osg::Vec3 ip = hitList.front().getWorldIntersectPoint(); |
|---|
| 39 | return ip; |
|---|
| 40 | } |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | return osg::Vec3(x,y,0.0f); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | void build_world(osg::Group *root) |
|---|
| 52 | { |
|---|
| 53 | |
|---|
| 54 | osg::Geode* terrainGeode = new osg::Geode; |
|---|
| 55 | { |
|---|
| 56 | osg::StateSet* stateset = new osg::StateSet(); |
|---|
| 57 | osg::Image* image = osgDB::readImageFile("Images/lz.rgb"); |
|---|
| 58 | if (image) |
|---|
| 59 | { |
|---|
| 60 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 61 | texture->setImage(image); |
|---|
| 62 | stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | terrainGeode->setStateSet( stateset ); |
|---|
| 66 | |
|---|
| 67 | float size = 1000; |
|---|
| 68 | float scale = size/39.0f; |
|---|
| 69 | float z_scale = scale*3.0f; |
|---|
| 70 | |
|---|
| 71 | osg::HeightField* grid = new osg::HeightField; |
|---|
| 72 | grid->allocateGrid(38,39); |
|---|
| 73 | grid->setXInterval(scale); |
|---|
| 74 | grid->setYInterval(scale); |
|---|
| 75 | |
|---|
| 76 | for(unsigned int r=0;r<39;++r) |
|---|
| 77 | { |
|---|
| 78 | for(unsigned int c=0;c<38;++c) |
|---|
| 79 | { |
|---|
| 80 | grid->setHeight(c,r,z_scale*vertex[r+c*39][2]); |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | terrainGeode->addDrawable(new osg::ShapeDrawable(grid)); |
|---|
| 84 | |
|---|
| 85 | root->addChild(terrainGeode); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | osg::PositionAttitudeTransform* positionEffects = new osg::PositionAttitudeTransform; |
|---|
| 91 | positionEffects->setPosition(computeTerrainIntersection(terrainGeode,100.0f,100.0f)); |
|---|
| 92 | root->addChild(positionEffects); |
|---|
| 93 | |
|---|
| 94 | osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect; |
|---|
| 95 | osgParticle::SmokeEffect* smoke = new osgParticle::SmokeEffect; |
|---|
| 96 | osgParticle::FireEffect* fire = new osgParticle::FireEffect; |
|---|
| 97 | |
|---|
| 98 | osg::Geode* geode = new osg::Geode; |
|---|
| 99 | geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),10.0f))); |
|---|
| 100 | positionEffects->addChild(geode); |
|---|
| 101 | |
|---|
| 102 | positionEffects->addChild(explosion); |
|---|
| 103 | positionEffects->addChild(smoke); |
|---|
| 104 | positionEffects->addChild(fire); |
|---|
| 105 | |
|---|
| 106 | osgParticle::ParticleSystemUpdater *psu = new osgParticle::ParticleSystemUpdater; |
|---|
| 107 | |
|---|
| 108 | psu->addParticleSystem(explosion->getParticleSystem()); |
|---|
| 109 | psu->addParticleSystem(smoke->getParticleSystem()); |
|---|
| 110 | psu->addParticleSystem(fire->getParticleSystem()); |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | root->addChild(psu); |
|---|
| 114 | |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | int main(int argc, char **argv) |
|---|
| 124 | { |
|---|
| 125 | |
|---|
| 126 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of particle systems."); |
|---|
| 130 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] image_file_left_eye image_file_right_eye"); |
|---|
| 131 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | osgProducer::Viewer viewer(arguments); |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 145 | { |
|---|
| 146 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 147 | return 1; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | if (arguments.errors()) |
|---|
| 155 | { |
|---|
| 156 | arguments.writeErrorMessages(std::cout); |
|---|
| 157 | return 1; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | osg::Group *root = new osg::Group; |
|---|
| 161 | build_world(root); |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | viewer.setSceneData(root); |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | viewer.realize(); |
|---|
| 168 | |
|---|
| 169 | while( !viewer.done() ) |
|---|
| 170 | { |
|---|
| 171 | |
|---|
| 172 | viewer.sync(); |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | viewer.update(); |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | viewer.frame(); |
|---|
| 180 | |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | viewer.sync(); |
|---|
| 185 | |
|---|
| 186 | return 0; |
|---|
| 187 | } |
|---|