| 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 | #include <osg/MatrixTransform> |
|---|
| 9 | |
|---|
| 10 | #include <osgDB/ReadFile> |
|---|
| 11 | |
|---|
| 12 | #include <osgText/Text> |
|---|
| 13 | |
|---|
| 14 | #include <osgSim/SphereSegment> |
|---|
| 15 | |
|---|
| 16 | #include <osgParticle/ExplosionEffect> |
|---|
| 17 | #include <osgParticle/SmokeEffect> |
|---|
| 18 | #include <osgParticle/FireEffect> |
|---|
| 19 | #include <osgParticle/ParticleSystemUpdater> |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | #include "../osghangglide/terrain_coords.h" |
|---|
| 23 | |
|---|
| 24 | osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime) |
|---|
| 25 | { |
|---|
| 26 | |
|---|
| 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 | |
|---|
| 51 | osg::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 | |
|---|
| 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 | |
|---|
| 120 | { |
|---|
| 121 | osg::PositionAttitudeTransform* positionEffects = new osg::PositionAttitudeTransform; |
|---|
| 122 | positionEffects->setPosition(osg::Vec3(0.0f,0.0f,0.0f)); |
|---|
| 123 | xform->addChild(positionEffects); |
|---|
| 124 | |
|---|
| 125 | osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect; |
|---|
| 126 | osgParticle::SmokeEffect* smoke = new osgParticle::SmokeEffect; |
|---|
| 127 | osgParticle::FireEffect* fire = new osgParticle::FireEffect; |
|---|
| 128 | |
|---|
| 129 | positionEffects->addChild(explosion); |
|---|
| 130 | positionEffects->addChild(smoke); |
|---|
| 131 | positionEffects->addChild(fire); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | model->addChild(xform); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | return model; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | osg::Vec3 computeTerrainIntersection(osg::Node* subgraph,float x,float y) |
|---|
| 142 | { |
|---|
| 143 | osgUtil::IntersectVisitor iv; |
|---|
| 144 | osg::ref_ptr<osg::LineSegment> segDown = new osg::LineSegment; |
|---|
| 145 | |
|---|
| 146 | const osg::BoundingSphere& bs = subgraph->getBound(); |
|---|
| 147 | float zMax = bs.center().z()+bs.radius(); |
|---|
| 148 | float zMin = bs.center().z()-bs.radius(); |
|---|
| 149 | |
|---|
| 150 | segDown->set(osg::Vec3(x,y,zMin),osg::Vec3(x,y,zMax)); |
|---|
| 151 | iv.addLineSegment(segDown.get()); |
|---|
| 152 | |
|---|
| 153 | subgraph->accept(iv); |
|---|
| 154 | |
|---|
| 155 | if (iv.hits()) |
|---|
| 156 | { |
|---|
| 157 | osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segDown.get()); |
|---|
| 158 | if (!hitList.empty()) |
|---|
| 159 | { |
|---|
| 160 | osg::Vec3 ip = hitList.front().getWorldIntersectPoint(); |
|---|
| 161 | return ip; |
|---|
| 162 | } |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | return osg::Vec3(x,y,0.0f); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | void build_world(osg::Group *root) |
|---|
| 174 | { |
|---|
| 175 | |
|---|
| 176 | osg::Geode* terrainGeode = new osg::Geode; |
|---|
| 177 | |
|---|
| 178 | { |
|---|
| 179 | osg::StateSet* stateset = new osg::StateSet(); |
|---|
| 180 | osg::Image* image = osgDB::readImageFile("Images/lz.rgb"); |
|---|
| 181 | if (image) |
|---|
| 182 | { |
|---|
| 183 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 184 | texture->setImage(image); |
|---|
| 185 | stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | terrainGeode->setStateSet( stateset ); |
|---|
| 189 | |
|---|
| 190 | float size = 1000; |
|---|
| 191 | float scale = size/39.0f; |
|---|
| 192 | float z_scale = scale*3.0f; |
|---|
| 193 | |
|---|
| 194 | osg::HeightField* grid = new osg::HeightField; |
|---|
| 195 | grid->allocateGrid(38,39); |
|---|
| 196 | grid->setXInterval(scale); |
|---|
| 197 | grid->setYInterval(scale); |
|---|
| 198 | |
|---|
| 199 | for(unsigned int r=0;r<39;++r) |
|---|
| 200 | { |
|---|
| 201 | for(unsigned int c=0;c<38;++c) |
|---|
| 202 | { |
|---|
| 203 | grid->setHeight(c,r,z_scale*vertex[r+c*39][2]); |
|---|
| 204 | } |
|---|
| 205 | } |
|---|
| 206 | terrainGeode->addDrawable(new osg::ShapeDrawable(grid)); |
|---|
| 207 | |
|---|
| 208 | root->addChild(terrainGeode); |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | { |
|---|
| 213 | osgSim::SphereSegment* ss = new osgSim::SphereSegment( |
|---|
| 214 | computeTerrainIntersection(terrainGeode,550.0f,780.0f), |
|---|
| 215 | 500.0f, |
|---|
| 216 | osg::DegreesToRadians(135.0f), |
|---|
| 217 | osg::DegreesToRadians(245.0f), |
|---|
| 218 | osg::DegreesToRadians(-10.0f), |
|---|
| 219 | osg::DegreesToRadians(30.0f), |
|---|
| 220 | 60); |
|---|
| 221 | ss->setAllColors(osg::Vec4(1.0f,1.0f,1.0f,0.5f)); |
|---|
| 222 | ss->setSideColor(osg::Vec4(0.0f,1.0f,1.0f,0.1f)); |
|---|
| 223 | |
|---|
| 224 | root->addChild(ss); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | { |
|---|
| 230 | osg::PositionAttitudeTransform* positionEffects = new osg::PositionAttitudeTransform; |
|---|
| 231 | positionEffects->setPosition(computeTerrainIntersection(terrainGeode,100.0f,100.0f)); |
|---|
| 232 | root->addChild(positionEffects); |
|---|
| 233 | |
|---|
| 234 | osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect; |
|---|
| 235 | osgParticle::SmokeEffect* smoke = new osgParticle::SmokeEffect; |
|---|
| 236 | osgParticle::FireEffect* fire = new osgParticle::FireEffect; |
|---|
| 237 | |
|---|
| 238 | positionEffects->addChild(explosion); |
|---|
| 239 | positionEffects->addChild(smoke); |
|---|
| 240 | positionEffects->addChild(fire); |
|---|
| 241 | |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | { |
|---|
| 247 | root->addChild(createMovingModel(osg::Vec3(500.0f,500.0f,500.0f),100.0f)); |
|---|
| 248 | } |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | int main(int argc, char **argv) |
|---|
| 258 | { |
|---|
| 259 | |
|---|
| 260 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of particle systems."); |
|---|
| 264 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] image_file_left_eye image_file_right_eye"); |
|---|
| 265 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | osgProducer::Viewer viewer(arguments); |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 279 | { |
|---|
| 280 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 281 | return 1; |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | if (arguments.errors()) |
|---|
| 289 | { |
|---|
| 290 | arguments.writeErrorMessages(std::cout); |
|---|
| 291 | return 1; |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | osg::Group *root = new osg::Group; |
|---|
| 295 | build_world(root); |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | viewer.setSceneData(root); |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | viewer.realize(); |
|---|
| 302 | |
|---|
| 303 | while( !viewer.done() ) |
|---|
| 304 | { |
|---|
| 305 | |
|---|
| 306 | viewer.sync(); |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | viewer.update(); |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | viewer.frame(); |
|---|
| 314 | |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | viewer.sync(); |
|---|
| 319 | |
|---|
| 320 | return 0; |
|---|
| 321 | } |
|---|