| 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 | #include <osg/Geometry> |
|---|
| 10 | |
|---|
| 11 | #include <osgDB/ReadFile> |
|---|
| 12 | |
|---|
| 13 | #include <osgText/Text> |
|---|
| 14 | |
|---|
| 15 | #include <osgSim/SphereSegment> |
|---|
| 16 | #include <osgSim/OverlayNode> |
|---|
| 17 | |
|---|
| 18 | #include <osgParticle/ExplosionEffect> |
|---|
| 19 | #include <osgParticle/SmokeEffect> |
|---|
| 20 | #include <osgParticle/FireEffect> |
|---|
| 21 | #include <osgParticle/ParticleSystemUpdater> |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | #include "../osghangglide/terrain_coords.h" |
|---|
| 25 | |
|---|
| 26 | osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime) |
|---|
| 27 | { |
|---|
| 28 | |
|---|
| 29 | osg::AnimationPath* animationPath = new osg::AnimationPath; |
|---|
| 30 | animationPath->setLoopMode(osg::AnimationPath::LOOP); |
|---|
| 31 | |
|---|
| 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); |
|---|
| 36 | |
|---|
| 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; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 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 | const osg::BoundingSphere& bs = glider->getBound(); |
|---|
| 65 | |
|---|
| 66 | float size = radius/bs.radius()*0.3f; |
|---|
| 67 | osg::MatrixTransform* positioned = new osg::MatrixTransform; |
|---|
| 68 | positioned->setDataVariance(osg::Object::STATIC); |
|---|
| 69 | positioned->setMatrix(osg::Matrix::translate(-bs.center())* |
|---|
| 70 | osg::Matrix::scale(size,size,size)* |
|---|
| 71 | osg::Matrix::rotate(osg::inDegrees(-90.0f),0.0f,0.0f,1.0f)); |
|---|
| 72 | |
|---|
| 73 | positioned->addChild(glider); |
|---|
| 74 | |
|---|
| 75 | osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform; |
|---|
| 76 | xform->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON); |
|---|
| 77 | xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,1.0)); |
|---|
| 78 | xform->addChild(positioned); |
|---|
| 79 | |
|---|
| 80 | model->addChild(xform); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | osg::Node* cessna = osgDB::readNodeFile("cessna.osg"); |
|---|
| 84 | if (cessna) |
|---|
| 85 | { |
|---|
| 86 | const osg::BoundingSphere& bs = cessna->getBound(); |
|---|
| 87 | |
|---|
| 88 | osgText::Text* text = new osgText::Text; |
|---|
| 89 | float size = radius/bs.radius()*0.3f; |
|---|
| 90 | |
|---|
| 91 | text->setPosition(bs.center()); |
|---|
| 92 | text->setText("Cessna"); |
|---|
| 93 | text->setAlignment(osgText::Text::CENTER_CENTER); |
|---|
| 94 | text->setAxisAlignment(osgText::Text::SCREEN); |
|---|
| 95 | text->setCharacterSize(40.0f); |
|---|
| 96 | text->setCharacterSizeMode(osgText::Text::SCREEN_COORDS); |
|---|
| 97 | |
|---|
| 98 | osg::Geode* geode = new osg::Geode; |
|---|
| 99 | geode->addDrawable(text); |
|---|
| 100 | |
|---|
| 101 | osg::LOD* lod = new osg::LOD; |
|---|
| 102 | lod->setRangeMode(osg::LOD::PIXEL_SIZE_ON_SCREEN); |
|---|
| 103 | lod->addChild(geode,0.0f,100.0f); |
|---|
| 104 | lod->addChild(cessna,100.0f,10000.0f); |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | osg::MatrixTransform* positioned = new osg::MatrixTransform; |
|---|
| 108 | positioned->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON); |
|---|
| 109 | positioned->setDataVariance(osg::Object::STATIC); |
|---|
| 110 | positioned->setMatrix(osg::Matrix::translate(-bs.center())* |
|---|
| 111 | osg::Matrix::scale(size,size,size)* |
|---|
| 112 | osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,1.0f)); |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | positioned->addChild(lod); |
|---|
| 116 | |
|---|
| 117 | osg::MatrixTransform* xform = new osg::MatrixTransform; |
|---|
| 118 | xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,2.0)); |
|---|
| 119 | xform->addChild(positioned); |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | { |
|---|
| 123 | osg::PositionAttitudeTransform* positionEffects = new osg::PositionAttitudeTransform; |
|---|
| 124 | positionEffects->setPosition(osg::Vec3(0.0f,0.0f,0.0f)); |
|---|
| 125 | xform->addChild(positionEffects); |
|---|
| 126 | |
|---|
| 127 | osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect; |
|---|
| 128 | osgParticle::SmokeEffect* smoke = new osgParticle::SmokeEffect; |
|---|
| 129 | osgParticle::FireEffect* fire = new osgParticle::FireEffect; |
|---|
| 130 | |
|---|
| 131 | positionEffects->addChild(explosion); |
|---|
| 132 | positionEffects->addChild(smoke); |
|---|
| 133 | positionEffects->addChild(fire); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | model->addChild(xform); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | return model; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | osg::Group* createOverlay(const osg::Vec3& center, float radius) |
|---|
| 143 | { |
|---|
| 144 | osg::Group* group = new osg::Group; |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | { |
|---|
| 148 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 149 | |
|---|
| 150 | unsigned int num_rows = 10; |
|---|
| 151 | |
|---|
| 152 | osg::Vec3 left = center+osg::Vec3(-radius,-radius,0.0f); |
|---|
| 153 | osg::Vec3 right = center+osg::Vec3(radius,-radius,0.0f); |
|---|
| 154 | osg::Vec3 delta_row = osg::Vec3(0.0f,2.0f*radius/float(num_rows-1),0.0f); |
|---|
| 155 | |
|---|
| 156 | osg::Vec3 top = center+osg::Vec3(-radius,radius,0.0f); |
|---|
| 157 | osg::Vec3 bottom = center+osg::Vec3(-radius,-radius,0.0f); |
|---|
| 158 | osg::Vec3 delta_column = osg::Vec3(2.0f*radius/float(num_rows-1),0.0f,0.0f); |
|---|
| 159 | |
|---|
| 160 | osg::Vec3Array* vertices = new osg::Vec3Array; |
|---|
| 161 | for(unsigned int i=0; i<num_rows; ++i) |
|---|
| 162 | { |
|---|
| 163 | vertices->push_back(left); |
|---|
| 164 | vertices->push_back(right); |
|---|
| 165 | left += delta_row; |
|---|
| 166 | right += delta_row; |
|---|
| 167 | |
|---|
| 168 | vertices->push_back(top); |
|---|
| 169 | vertices->push_back(bottom); |
|---|
| 170 | top += delta_column; |
|---|
| 171 | bottom += delta_column; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | geom->setVertexArray(vertices); |
|---|
| 175 | geom->addPrimitiveSet(new osg::DrawArrays(GL_LINES,0,vertices->getNumElements())); |
|---|
| 176 | |
|---|
| 177 | osg::Geode* geode = new osg::Geode; |
|---|
| 178 | geode->addDrawable(geom); |
|---|
| 179 | group->addChild(geode); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | return group; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | osg::Vec3 computeTerrainIntersection(osg::Node* subgraph,float x,float y) |
|---|
| 186 | { |
|---|
| 187 | osgUtil::IntersectVisitor iv; |
|---|
| 188 | osg::ref_ptr<osg::LineSegment> segDown = new osg::LineSegment; |
|---|
| 189 | |
|---|
| 190 | const osg::BoundingSphere& bs = subgraph->getBound(); |
|---|
| 191 | float zMax = bs.center().z()+bs.radius(); |
|---|
| 192 | float zMin = bs.center().z()-bs.radius(); |
|---|
| 193 | |
|---|
| 194 | segDown->set(osg::Vec3(x,y,zMin),osg::Vec3(x,y,zMax)); |
|---|
| 195 | iv.addLineSegment(segDown.get()); |
|---|
| 196 | |
|---|
| 197 | subgraph->accept(iv); |
|---|
| 198 | |
|---|
| 199 | if (iv.hits()) |
|---|
| 200 | { |
|---|
| 201 | osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segDown.get()); |
|---|
| 202 | if (!hitList.empty()) |
|---|
| 203 | { |
|---|
| 204 | osg::Vec3 ip = hitList.front().getWorldIntersectPoint(); |
|---|
| 205 | return ip; |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | return osg::Vec3(x,y,0.0f); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | void build_world(osg::Group *root) |
|---|
| 218 | { |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | osg::ref_ptr<osg::Geode> terrainGeode = 0; |
|---|
| 222 | { |
|---|
| 223 | terrainGeode = new osg::Geode; |
|---|
| 224 | |
|---|
| 225 | osg::StateSet* stateset = new osg::StateSet(); |
|---|
| 226 | osg::Image* image = osgDB::readImageFile("Images/lz.rgb"); |
|---|
| 227 | if (image) |
|---|
| 228 | { |
|---|
| 229 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 230 | texture->setImage(image); |
|---|
| 231 | stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | terrainGeode->setStateSet( stateset ); |
|---|
| 235 | |
|---|
| 236 | float size = 1000; |
|---|
| 237 | float scale = size/39.0f; |
|---|
| 238 | float z_scale = scale*3.0f; |
|---|
| 239 | |
|---|
| 240 | osg::HeightField* grid = new osg::HeightField; |
|---|
| 241 | grid->allocate(38,39); |
|---|
| 242 | grid->setXInterval(scale); |
|---|
| 243 | grid->setYInterval(scale); |
|---|
| 244 | |
|---|
| 245 | for(unsigned int r=0;r<39;++r) |
|---|
| 246 | { |
|---|
| 247 | for(unsigned int c=0;c<38;++c) |
|---|
| 248 | { |
|---|
| 249 | grid->setHeight(c,r,z_scale*vertex[r+c*39][2]); |
|---|
| 250 | } |
|---|
| 251 | } |
|---|
| 252 | terrainGeode->addDrawable(new osg::ShapeDrawable(grid)); |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | osg::ref_ptr<osgSim::SphereSegment> ss = 0; |
|---|
| 260 | { |
|---|
| 261 | ss = new osgSim::SphereSegment( |
|---|
| 262 | computeTerrainIntersection(terrainGeode.get(),550.0f,780.0f), |
|---|
| 263 | 500.0f, |
|---|
| 264 | osg::DegreesToRadians(135.0f), |
|---|
| 265 | osg::DegreesToRadians(245.0f), |
|---|
| 266 | osg::DegreesToRadians(-10.0f), |
|---|
| 267 | osg::DegreesToRadians(30.0f), |
|---|
| 268 | 60); |
|---|
| 269 | ss->setAllColors(osg::Vec4(1.0f,1.0f,1.0f,0.5f)); |
|---|
| 270 | ss->setSideColor(osg::Vec4(0.0f,1.0f,1.0f,0.1f)); |
|---|
| 271 | |
|---|
| 272 | root->addChild(ss.get()); |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | osgSim::SphereSegment::LineList lines = ss->computeIntersection(terrainGeode.get(), osg::Matrixd::identity()); |
|---|
| 276 | if (!lines.empty()) |
|---|
| 277 | { |
|---|
| 278 | osg::notify(osg::NOTICE)<<"We've found intersections!!!!"<<std::endl; |
|---|
| 279 | } |
|---|
| 280 | else |
|---|
| 281 | { |
|---|
| 282 | osg::notify(osg::NOTICE)<<"No intersections found"<<std::endl; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | #if 1 |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | osgSim::OverlayNode* overlayNode = new osgSim::OverlayNode; |
|---|
| 289 | |
|---|
| 290 | const osg::BoundingSphere& bs = terrainGeode->getBound(); |
|---|
| 291 | osg::Group* overlaySubgraph = createOverlay(bs.center(), bs.radius()*0.5f); |
|---|
| 292 | overlaySubgraph->addChild(ss.get()); |
|---|
| 293 | overlayNode->setOverlaySubgraph(overlaySubgraph); |
|---|
| 294 | overlayNode->setOverlayTextureSizeHint(2048); |
|---|
| 295 | overlayNode->addChild(terrainGeode.get()); |
|---|
| 296 | |
|---|
| 297 | root->addChild(overlayNode); |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | #else |
|---|
| 301 | root->addChild(terrainGeode); |
|---|
| 302 | #endif |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | { |
|---|
| 307 | osg::Vec3 position = computeTerrainIntersection(terrainGeode.get(),100.0f,100.0f); |
|---|
| 308 | |
|---|
| 309 | osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect(position, 10.0f); |
|---|
| 310 | osgParticle::SmokeEffect* smoke = new osgParticle::SmokeEffect(position, 10.0f); |
|---|
| 311 | osgParticle::FireEffect* fire = new osgParticle::FireEffect(position, 10.0f); |
|---|
| 312 | |
|---|
| 313 | root->addChild(explosion); |
|---|
| 314 | root->addChild(smoke); |
|---|
| 315 | root->addChild(fire); |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | { |
|---|
| 320 | osg::Vec3 position = computeTerrainIntersection(terrainGeode.get(),200.0f,100.0f); |
|---|
| 321 | |
|---|
| 322 | osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect(position, 1.0f); |
|---|
| 323 | osgParticle::SmokeEffect* smoke = new osgParticle::SmokeEffect(position, 1.0f); |
|---|
| 324 | osgParticle::FireEffect* fire = new osgParticle::FireEffect(position, 1.0f); |
|---|
| 325 | |
|---|
| 326 | root->addChild(explosion); |
|---|
| 327 | root->addChild(smoke); |
|---|
| 328 | root->addChild(fire); |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | { |
|---|
| 333 | root->addChild(createMovingModel(osg::Vec3(500.0f,500.0f,500.0f),100.0f)); |
|---|
| 334 | } |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | int main(int argc, char **argv) |
|---|
| 344 | { |
|---|
| 345 | |
|---|
| 346 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of particle systems."); |
|---|
| 350 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] image_file_left_eye image_file_right_eye"); |
|---|
| 351 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | osgProducer::Viewer viewer(arguments); |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 359 | |
|---|
| 360 | |
|---|
| 361 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 365 | { |
|---|
| 366 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 367 | return 1; |
|---|
| 368 | } |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | if (arguments.errors()) |
|---|
| 375 | { |
|---|
| 376 | arguments.writeErrorMessages(std::cout); |
|---|
| 377 | return 1; |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | osg::Group *root = new osg::Group; |
|---|
| 381 | build_world(root); |
|---|
| 382 | |
|---|
| 383 | |
|---|
| 384 | viewer.setSceneData(root); |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | viewer.realize(); |
|---|
| 388 | |
|---|
| 389 | while( !viewer.done() ) |
|---|
| 390 | { |
|---|
| 391 | |
|---|
| 392 | viewer.sync(); |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | |
|---|
| 396 | viewer.update(); |
|---|
| 397 | |
|---|
| 398 | |
|---|
| 399 | viewer.frame(); |
|---|
| 400 | |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | |
|---|
| 404 | viewer.sync(); |
|---|
| 405 | |
|---|
| 406 | return 0; |
|---|
| 407 | } |
|---|