| 1 | #include <osgViewer/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 <osgUtil/SmoothingVisitor> |
|---|
| 12 | #include <osgUtil/IntersectVisitor> |
|---|
| 13 | |
|---|
| 14 | #include <osgDB/ReadFile> |
|---|
| 15 | |
|---|
| 16 | #include <osgText/Text> |
|---|
| 17 | |
|---|
| 18 | #include <osgSim/SphereSegment> |
|---|
| 19 | #include <osgSim/OverlayNode> |
|---|
| 20 | |
|---|
| 21 | #include <osgParticle/ExplosionEffect> |
|---|
| 22 | #include <osgParticle/SmokeEffect> |
|---|
| 23 | #include <osgParticle/FireEffect> |
|---|
| 24 | #include <osgParticle/ParticleSystemUpdater> |
|---|
| 25 | |
|---|
| 26 | #include <osg/io_utils> |
|---|
| 27 | |
|---|
| 28 | #include <iostream> |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | #include "../osghangglide/terrain_coords.h" |
|---|
| 32 | |
|---|
| 33 | osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime) |
|---|
| 34 | { |
|---|
| 35 | |
|---|
| 36 | osg::AnimationPath* animationPath = new osg::AnimationPath; |
|---|
| 37 | animationPath->setLoopMode(osg::AnimationPath::LOOP); |
|---|
| 38 | |
|---|
| 39 | int numSamples = 40; |
|---|
| 40 | float yaw = 0.0f; |
|---|
| 41 | float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f); |
|---|
| 42 | float roll = osg::inDegrees(30.0f); |
|---|
| 43 | |
|---|
| 44 | double time=0.0f; |
|---|
| 45 | double time_delta = looptime/(double)numSamples; |
|---|
| 46 | for(int i=0;i<numSamples;++i) |
|---|
| 47 | { |
|---|
| 48 | osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f)); |
|---|
| 49 | 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))); |
|---|
| 50 | |
|---|
| 51 | animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation)); |
|---|
| 52 | |
|---|
| 53 | yaw += yaw_delta; |
|---|
| 54 | time += time_delta; |
|---|
| 55 | |
|---|
| 56 | } |
|---|
| 57 | return animationPath; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | class IntersectionUpdateCallback : public osg::NodeCallback |
|---|
| 63 | { |
|---|
| 64 | virtual void operator()(osg::Node* , osg::NodeVisitor* nv) |
|---|
| 65 | { |
|---|
| 66 | if (!root_ || !terrain_ || !ss_ || !intersectionGroup_) |
|---|
| 67 | { |
|---|
| 68 | osg::notify(osg::NOTICE)<<"IntersectionUpdateCallback not set up correctly."<<std::endl; |
|---|
| 69 | return; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | frameCount_++; |
|---|
| 74 | if (frameCount_ > 200) |
|---|
| 75 | { |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | osg::Matrixd terrainLocalToWorld; |
|---|
| 79 | osg::MatrixList terrain_worldMatrices = terrain_->getWorldMatrices(root_.get()); |
|---|
| 80 | if (terrain_worldMatrices.empty()) terrainLocalToWorld.makeIdentity(); |
|---|
| 81 | else if (terrain_worldMatrices.size()==1) terrainLocalToWorld = terrain_worldMatrices.front(); |
|---|
| 82 | else |
|---|
| 83 | { |
|---|
| 84 | osg::notify(osg::NOTICE)<<"IntersectionUpdateCallback: warning cannot interestect with multiple terrain instances, just uses first one."<<std::endl; |
|---|
| 85 | terrainLocalToWorld = terrain_worldMatrices.front(); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | osg::Matrixd ssWorldToLocal = osg::computeWorldToLocal(nv->getNodePath()); |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | osg::Matrixd possie = terrainLocalToWorld*ssWorldToLocal; |
|---|
| 93 | |
|---|
| 94 | osgSim::SphereSegment::LineList lines = ss_->computeIntersection(possie, terrain_.get()); |
|---|
| 95 | if (!lines.empty()) |
|---|
| 96 | { |
|---|
| 97 | if (intersectionGroup_.valid()) |
|---|
| 98 | { |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | osg::MatrixTransform* mt = new osg::MatrixTransform; |
|---|
| 102 | mt->setMatrix(osg::computeLocalToWorld(nv->getNodePath())); |
|---|
| 103 | intersectionGroup_->addChild(mt); |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | osg::Geode* geode = new osg::Geode; |
|---|
| 108 | mt->addChild(geode); |
|---|
| 109 | |
|---|
| 110 | geode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 111 | |
|---|
| 112 | for(osgSim::SphereSegment::LineList::iterator itr=lines.begin(); |
|---|
| 113 | itr!=lines.end(); |
|---|
| 114 | ++itr) |
|---|
| 115 | { |
|---|
| 116 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 117 | geode->addDrawable(geom); |
|---|
| 118 | |
|---|
| 119 | osg::Vec3Array* vertices = itr->get(); |
|---|
| 120 | geom->setVertexArray(vertices); |
|---|
| 121 | geom->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP, 0, vertices->getNumElements())); |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | else |
|---|
| 126 | { |
|---|
| 127 | osg::notify(osg::NOTICE)<<"No intersections found"<<std::endl; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | frameCount_ = 0; |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | public: |
|---|
| 135 | osg::observer_ptr<osg::Group> root_; |
|---|
| 136 | osg::observer_ptr<osg::Geode> terrain_; |
|---|
| 137 | osg::observer_ptr<osgSim::SphereSegment> ss_; |
|---|
| 138 | osg::observer_ptr<osg::Group> intersectionGroup_; |
|---|
| 139 | unsigned frameCount_; |
|---|
| 140 | }; |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | osg::Node* createMovingModel(const osg::Vec3& center, float radius, osg::Geode * terrainGeode, osg::Group * root, bool createMovingRadar = false) |
|---|
| 144 | { |
|---|
| 145 | float animationLength = 10.0f; |
|---|
| 146 | |
|---|
| 147 | osg::AnimationPath* animationPath = createAnimationPath(center,radius,animationLength); |
|---|
| 148 | |
|---|
| 149 | osg::Group* model = new osg::Group; |
|---|
| 150 | |
|---|
| 151 | osg::Node* glider = osgDB::readNodeFile("glider.osg"); |
|---|
| 152 | if (glider) |
|---|
| 153 | { |
|---|
| 154 | const osg::BoundingSphere& bs = glider->getBound(); |
|---|
| 155 | |
|---|
| 156 | float size = radius/bs.radius()*0.3f; |
|---|
| 157 | osg::MatrixTransform* positioned = new osg::MatrixTransform; |
|---|
| 158 | positioned->setDataVariance(osg::Object::STATIC); |
|---|
| 159 | positioned->setMatrix(osg::Matrix::translate(-bs.center())* |
|---|
| 160 | osg::Matrix::scale(size,size,size)* |
|---|
| 161 | osg::Matrix::rotate(osg::inDegrees(-90.0f),0.0f,0.0f,1.0f)); |
|---|
| 162 | |
|---|
| 163 | positioned->addChild(glider); |
|---|
| 164 | |
|---|
| 165 | osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform; |
|---|
| 166 | xform->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON); |
|---|
| 167 | xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,1.0)); |
|---|
| 168 | xform->addChild(positioned); |
|---|
| 169 | model->addChild(xform); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | if (createMovingRadar) |
|---|
| 173 | { |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | osg::Group* intersectionGroup = new osg::Group; |
|---|
| 178 | root->addChild(intersectionGroup); |
|---|
| 179 | |
|---|
| 180 | osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform; |
|---|
| 181 | xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,1.0)); |
|---|
| 182 | |
|---|
| 183 | osgSim::SphereSegment * ss = new osgSim::SphereSegment(osg::Vec3d(0.0,0.0,0.0), |
|---|
| 184 | 700.0f, |
|---|
| 185 | osg::DegreesToRadians(135.0f), |
|---|
| 186 | osg::DegreesToRadians(240.0f), |
|---|
| 187 | osg::DegreesToRadians(-60.0f), |
|---|
| 188 | osg::DegreesToRadians(-40.0f), |
|---|
| 189 | 60); |
|---|
| 190 | |
|---|
| 191 | IntersectionUpdateCallback * iuc = new IntersectionUpdateCallback; |
|---|
| 192 | iuc->frameCount_ = 0; |
|---|
| 193 | iuc->root_ = root; |
|---|
| 194 | iuc->terrain_ = terrainGeode; |
|---|
| 195 | iuc->ss_ = ss; |
|---|
| 196 | iuc->intersectionGroup_ = intersectionGroup; |
|---|
| 197 | ss->setUpdateCallback(iuc); |
|---|
| 198 | ss->setAllColors(osg::Vec4(1.0f,1.0f,1.0f,0.5f)); |
|---|
| 199 | ss->setSideColor(osg::Vec4(0.5f,1.0f,1.0f,0.1f)); |
|---|
| 200 | xform->addChild(ss); |
|---|
| 201 | model->addChild(xform); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | osg::Node* cessna = osgDB::readNodeFile("cessna.osg"); |
|---|
| 205 | if (cessna) |
|---|
| 206 | { |
|---|
| 207 | const osg::BoundingSphere& bs = cessna->getBound(); |
|---|
| 208 | |
|---|
| 209 | osgText::Text* text = new osgText::Text; |
|---|
| 210 | float size = radius/bs.radius()*0.3f; |
|---|
| 211 | |
|---|
| 212 | text->setPosition(bs.center()); |
|---|
| 213 | text->setText("Cessna"); |
|---|
| 214 | text->setAlignment(osgText::Text::CENTER_CENTER); |
|---|
| 215 | text->setAxisAlignment(osgText::Text::SCREEN); |
|---|
| 216 | text->setCharacterSize(40.0f); |
|---|
| 217 | text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS); |
|---|
| 218 | |
|---|
| 219 | osg::Geode* geode = new osg::Geode; |
|---|
| 220 | geode->addDrawable(text); |
|---|
| 221 | |
|---|
| 222 | osg::LOD* lod = new osg::LOD; |
|---|
| 223 | lod->setRangeMode(osg::LOD::PIXEL_SIZE_ON_SCREEN); |
|---|
| 224 | lod->setRadius(cessna->getBound().radius()); |
|---|
| 225 | lod->addChild(geode,0.0f,100.0f); |
|---|
| 226 | lod->addChild(cessna,100.0f,10000.0f); |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | osg::MatrixTransform* positioned = new osg::MatrixTransform; |
|---|
| 230 | positioned->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON); |
|---|
| 231 | positioned->setDataVariance(osg::Object::STATIC); |
|---|
| 232 | positioned->setMatrix(osg::Matrix::translate(-bs.center())* |
|---|
| 233 | osg::Matrix::scale(size,size,size)* |
|---|
| 234 | osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,1.0f)); |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | positioned->addChild(lod); |
|---|
| 238 | |
|---|
| 239 | osg::MatrixTransform* xform = new osg::MatrixTransform; |
|---|
| 240 | xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,2.0)); |
|---|
| 241 | xform->addChild(positioned); |
|---|
| 242 | |
|---|
| 243 | model->addChild(xform); |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | return model; |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | osg::Group* createOverlay(const osg::Vec3& center, float radius) |
|---|
| 250 | { |
|---|
| 251 | osg::Group* group = new osg::Group; |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | { |
|---|
| 255 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 256 | |
|---|
| 257 | unsigned int num_rows = 10; |
|---|
| 258 | |
|---|
| 259 | osg::Vec3 left = center+osg::Vec3(-radius,-radius,0.0f); |
|---|
| 260 | osg::Vec3 right = center+osg::Vec3(radius,-radius,0.0f); |
|---|
| 261 | osg::Vec3 delta_row = osg::Vec3(0.0f,2.0f*radius/float(num_rows-1),0.0f); |
|---|
| 262 | |
|---|
| 263 | osg::Vec3 top = center+osg::Vec3(-radius,radius,0.0f); |
|---|
| 264 | osg::Vec3 bottom = center+osg::Vec3(-radius,-radius,0.0f); |
|---|
| 265 | osg::Vec3 delta_column = osg::Vec3(2.0f*radius/float(num_rows-1),0.0f,0.0f); |
|---|
| 266 | |
|---|
| 267 | osg::Vec3Array* vertices = new osg::Vec3Array; |
|---|
| 268 | for(unsigned int i=0; i<num_rows; ++i) |
|---|
| 269 | { |
|---|
| 270 | vertices->push_back(left); |
|---|
| 271 | vertices->push_back(right); |
|---|
| 272 | left += delta_row; |
|---|
| 273 | right += delta_row; |
|---|
| 274 | |
|---|
| 275 | vertices->push_back(top); |
|---|
| 276 | vertices->push_back(bottom); |
|---|
| 277 | top += delta_column; |
|---|
| 278 | bottom += delta_column; |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | geom->setVertexArray(vertices); |
|---|
| 282 | |
|---|
| 283 | osg::Vec4ubArray& color = *(new osg::Vec4ubArray(1)); |
|---|
| 284 | color[0].set(0,0,0,255); |
|---|
| 285 | geom->setColorArray(&color); |
|---|
| 286 | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 287 | |
|---|
| 288 | geom->addPrimitiveSet(new osg::DrawArrays(GL_LINES,0,vertices->getNumElements())); |
|---|
| 289 | |
|---|
| 290 | geom->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 291 | |
|---|
| 292 | osg::Geode* geode = new osg::Geode; |
|---|
| 293 | geode->addDrawable(geom); |
|---|
| 294 | group->addChild(geode); |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | return group; |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | osg::Vec3 computeTerrainIntersection(osg::Node* subgraph,float x,float y) |
|---|
| 301 | { |
|---|
| 302 | osgUtil::IntersectVisitor iv; |
|---|
| 303 | osg::ref_ptr<osg::LineSegment> segDown = new osg::LineSegment; |
|---|
| 304 | |
|---|
| 305 | const osg::BoundingSphere& bs = subgraph->getBound(); |
|---|
| 306 | float zMax = bs.center().z()+bs.radius(); |
|---|
| 307 | float zMin = bs.center().z()-bs.radius(); |
|---|
| 308 | |
|---|
| 309 | segDown->set(osg::Vec3(x,y,zMin),osg::Vec3(x,y,zMax)); |
|---|
| 310 | iv.addLineSegment(segDown.get()); |
|---|
| 311 | |
|---|
| 312 | subgraph->accept(iv); |
|---|
| 313 | |
|---|
| 314 | if (iv.hits()) |
|---|
| 315 | { |
|---|
| 316 | osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(segDown.get()); |
|---|
| 317 | if (!hitList.empty()) |
|---|
| 318 | { |
|---|
| 319 | osg::Vec3 ip = hitList.front().getWorldIntersectPoint(); |
|---|
| 320 | return ip; |
|---|
| 321 | } |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | return osg::Vec3(x,y,0.0f); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | void build_world(osg::Group *root, unsigned int testCase, bool useOverlay, osgSim::OverlayNode::OverlayTechnique technique) |
|---|
| 333 | { |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | osg::ref_ptr<osg::Geode> terrainGeode = 0; |
|---|
| 337 | { |
|---|
| 338 | terrainGeode = new osg::Geode; |
|---|
| 339 | |
|---|
| 340 | osg::StateSet* stateset = new osg::StateSet(); |
|---|
| 341 | osg::Image* image = osgDB::readImageFile("Images/lz.rgb"); |
|---|
| 342 | if (image) |
|---|
| 343 | { |
|---|
| 344 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 345 | texture->setImage(image); |
|---|
| 346 | stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | terrainGeode->setStateSet( stateset ); |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | { |
|---|
| 353 | unsigned int numColumns = 38; |
|---|
| 354 | unsigned int numRows = 39; |
|---|
| 355 | unsigned int r, c; |
|---|
| 356 | |
|---|
| 357 | osg::Vec3 origin(0.0f,0.0f,0.0f); |
|---|
| 358 | osg::Vec3 size(1000.0f,1000.0f,250.0f); |
|---|
| 359 | |
|---|
| 360 | osg::Geometry* geometry = new osg::Geometry; |
|---|
| 361 | |
|---|
| 362 | osg::Vec3Array& v = *(new osg::Vec3Array(numColumns*numRows)); |
|---|
| 363 | osg::Vec2Array& tc = *(new osg::Vec2Array(numColumns*numRows)); |
|---|
| 364 | osg::Vec4ubArray& color = *(new osg::Vec4ubArray(1)); |
|---|
| 365 | |
|---|
| 366 | color[0].set(255,255,255,255); |
|---|
| 367 | |
|---|
| 368 | float rowCoordDelta = size.y()/(float)(numRows-1); |
|---|
| 369 | float columnCoordDelta = size.x()/(float)(numColumns-1); |
|---|
| 370 | |
|---|
| 371 | float rowTexDelta = 1.0f/(float)(numRows-1); |
|---|
| 372 | float columnTexDelta = 1.0f/(float)(numColumns-1); |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | float min_z = FLT_MAX; |
|---|
| 376 | float max_z = -FLT_MAX; |
|---|
| 377 | for(r=0;r<numRows;++r) |
|---|
| 378 | { |
|---|
| 379 | for(c=0;c<numColumns;++c) |
|---|
| 380 | { |
|---|
| 381 | min_z = osg::minimum(min_z,vertex[r+c*numRows][2]); |
|---|
| 382 | max_z = osg::maximum(max_z,vertex[r+c*numRows][2]); |
|---|
| 383 | } |
|---|
| 384 | } |
|---|
| 385 | |
|---|
| 386 | float scale_z = size.z()/(max_z-min_z); |
|---|
| 387 | |
|---|
| 388 | osg::Vec3 pos = origin; |
|---|
| 389 | osg::Vec2 tex(0.0f,0.0f); |
|---|
| 390 | int vi=0; |
|---|
| 391 | for(r=0;r<numRows;++r) |
|---|
| 392 | { |
|---|
| 393 | pos.x() = origin.x(); |
|---|
| 394 | tex.x() = 0.0f; |
|---|
| 395 | for(c=0;c<numColumns;++c) |
|---|
| 396 | { |
|---|
| 397 | v[vi].set(pos.x(),pos.y(),pos.z()+(vertex[r+c*numRows][2]-min_z)*scale_z); |
|---|
| 398 | tc[vi] = tex; |
|---|
| 399 | pos.x()+=columnCoordDelta; |
|---|
| 400 | tex.x()+=columnTexDelta; |
|---|
| 401 | ++vi; |
|---|
| 402 | } |
|---|
| 403 | pos.y() += rowCoordDelta; |
|---|
| 404 | tex.y() += rowTexDelta; |
|---|
| 405 | } |
|---|
| 406 | |
|---|
| 407 | geometry->setVertexArray(&v); |
|---|
| 408 | geometry->setTexCoordArray(0, &tc); |
|---|
| 409 | geometry->setColorArray(&color); |
|---|
| 410 | geometry->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 411 | |
|---|
| 412 | for(r=0;r<numRows-1;++r) |
|---|
| 413 | { |
|---|
| 414 | osg::DrawElementsUShort& drawElements = *(new osg::DrawElementsUShort(GL_QUAD_STRIP,2*numColumns)); |
|---|
| 415 | geometry->addPrimitiveSet(&drawElements); |
|---|
| 416 | int ei=0; |
|---|
| 417 | for(c=0;c<numColumns;++c) |
|---|
| 418 | { |
|---|
| 419 | drawElements[ei++] = (r+1)*numColumns+c; |
|---|
| 420 | drawElements[ei++] = (r)*numColumns+c; |
|---|
| 421 | } |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | osgUtil::SmoothingVisitor smoother; |
|---|
| 425 | smoother.smooth(*geometry); |
|---|
| 426 | |
|---|
| 427 | terrainGeode->addDrawable(geometry); |
|---|
| 428 | } |
|---|
| 429 | |
|---|
| 430 | } |
|---|
| 431 | |
|---|
| 432 | |
|---|
| 433 | |
|---|
| 434 | osg::ref_ptr<osgSim::SphereSegment> ss = 0; |
|---|
| 435 | { |
|---|
| 436 | |
|---|
| 437 | osg::Matrix terrainToSS; |
|---|
| 438 | |
|---|
| 439 | switch(testCase) |
|---|
| 440 | { |
|---|
| 441 | case(0): |
|---|
| 442 | ss = new osgSim::SphereSegment( |
|---|
| 443 | computeTerrainIntersection(terrainGeode.get(),550.0f,780.0f), |
|---|
| 444 | 510.0f, |
|---|
| 445 | osg::DegreesToRadians(135.0f), |
|---|
| 446 | osg::DegreesToRadians(240.0f), |
|---|
| 447 | osg::DegreesToRadians(-10.0f), |
|---|
| 448 | osg::DegreesToRadians(30.0f), |
|---|
| 449 | 60); |
|---|
| 450 | root->addChild(ss.get()); |
|---|
| 451 | break; |
|---|
| 452 | case(1): |
|---|
| 453 | ss = new osgSim::SphereSegment( |
|---|
| 454 | computeTerrainIntersection(terrainGeode.get(),550.0f,780.0f), |
|---|
| 455 | 510.0f, |
|---|
| 456 | osg::DegreesToRadians(45.0f), |
|---|
| 457 | osg::DegreesToRadians(240.0f), |
|---|
| 458 | osg::DegreesToRadians(-10.0f), |
|---|
| 459 | osg::DegreesToRadians(30.0f), |
|---|
| 460 | 60); |
|---|
| 461 | root->addChild(ss.get()); |
|---|
| 462 | break; |
|---|
| 463 | case(2): |
|---|
| 464 | ss = new osgSim::SphereSegment( |
|---|
| 465 | computeTerrainIntersection(terrainGeode.get(),550.0f,780.0f), |
|---|
| 466 | 510.0f, |
|---|
| 467 | osg::DegreesToRadians(5.0f), |
|---|
| 468 | osg::DegreesToRadians(355.0f), |
|---|
| 469 | osg::DegreesToRadians(-10.0f), |
|---|
| 470 | osg::DegreesToRadians(30.0f), |
|---|
| 471 | 60); |
|---|
| 472 | root->addChild(ss.get()); |
|---|
| 473 | break; |
|---|
| 474 | case(3): |
|---|
| 475 | ss = new osgSim::SphereSegment( |
|---|
| 476 | computeTerrainIntersection(terrainGeode.get(),550.0f,780.0f), |
|---|
| 477 | 510.0f, |
|---|
| 478 | osg::DegreesToRadians(0.0f), |
|---|
| 479 | osg::DegreesToRadians(360.0f), |
|---|
| 480 | osg::DegreesToRadians(-10.0f), |
|---|
| 481 | osg::DegreesToRadians(30.0f), |
|---|
| 482 | 60); |
|---|
| 483 | root->addChild(ss.get()); |
|---|
| 484 | break; |
|---|
| 485 | case(4): |
|---|
| 486 | { |
|---|
| 487 | ss = new osgSim::SphereSegment(osg::Vec3d(0.0,0.0,0.0), |
|---|
| 488 | 700.0f, |
|---|
| 489 | osg::DegreesToRadians(135.0f), |
|---|
| 490 | osg::DegreesToRadians(240.0f), |
|---|
| 491 | osg::DegreesToRadians(-60.0f), |
|---|
| 492 | osg::DegreesToRadians(-40.0f), |
|---|
| 493 | 60); |
|---|
| 494 | |
|---|
| 495 | osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform; |
|---|
| 496 | |
|---|
| 497 | mt->setMatrix(osg::Matrix(-0.851781, 0.156428, -0.5, 0, |
|---|
| 498 | -0.180627, -0.983552, -6.93889e-18, 0, |
|---|
| 499 | -0.491776, 0.0903136, 0.866025, 0, |
|---|
| 500 | 598.217, 481.957, 100, 1)); |
|---|
| 501 | mt->addChild(ss.get()); |
|---|
| 502 | |
|---|
| 503 | terrainToSS.invert(mt->getMatrix()); |
|---|
| 504 | |
|---|
| 505 | root->addChild(mt.get()); |
|---|
| 506 | break; |
|---|
| 507 | } |
|---|
| 508 | case(5): |
|---|
| 509 | { |
|---|
| 510 | ss = new osgSim::SphereSegment(osg::Vec3d(0.0,0.0,0.0), |
|---|
| 511 | 700.0f, |
|---|
| 512 | osg::DegreesToRadians(35.0f), |
|---|
| 513 | osg::DegreesToRadians(135.0f), |
|---|
| 514 | osg::DegreesToRadians(-60.0f), |
|---|
| 515 | osg::DegreesToRadians(-40.0f), |
|---|
| 516 | 60); |
|---|
| 517 | |
|---|
| 518 | osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform; |
|---|
| 519 | |
|---|
| 520 | mt->setMatrix(osg::Matrix(-0.851781, 0.156428, -0.5, 0, |
|---|
| 521 | -0.180627, -0.983552, -6.93889e-18, 0, |
|---|
| 522 | -0.491776, 0.0903136, 0.866025, 0, |
|---|
| 523 | 598.217, 481.957, 100, 1)); |
|---|
| 524 | mt->addChild(ss.get()); |
|---|
| 525 | |
|---|
| 526 | terrainToSS.invert(mt->getMatrix()); |
|---|
| 527 | |
|---|
| 528 | root->addChild(mt.get()); |
|---|
| 529 | break; |
|---|
| 530 | } |
|---|
| 531 | case(6): |
|---|
| 532 | { |
|---|
| 533 | ss = new osgSim::SphereSegment(osg::Vec3d(0.0,0.0,0.0), |
|---|
| 534 | 700.0f, |
|---|
| 535 | osg::DegreesToRadians(-45.0f), |
|---|
| 536 | osg::DegreesToRadians(45.0f), |
|---|
| 537 | osg::DegreesToRadians(-60.0f), |
|---|
| 538 | osg::DegreesToRadians(-40.0f), |
|---|
| 539 | 60); |
|---|
| 540 | |
|---|
| 541 | osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform; |
|---|
| 542 | |
|---|
| 543 | mt->setMatrix(osg::Matrix(-0.851781, 0.156428, -0.5, 0, |
|---|
| 544 | -0.180627, -0.983552, -6.93889e-18, 0, |
|---|
| 545 | -0.491776, 0.0903136, 0.866025, 0, |
|---|
| 546 | 598.217, 481.957, 100, 1)); |
|---|
| 547 | mt->addChild(ss.get()); |
|---|
| 548 | |
|---|
| 549 | terrainToSS.invert(mt->getMatrix()); |
|---|
| 550 | |
|---|
| 551 | root->addChild(mt.get()); |
|---|
| 552 | break; |
|---|
| 553 | } |
|---|
| 554 | }; |
|---|
| 555 | |
|---|
| 556 | if (ss.valid()) |
|---|
| 557 | { |
|---|
| 558 | ss->setAllColors(osg::Vec4(1.0f,1.0f,1.0f,0.5f)); |
|---|
| 559 | ss->setSideColor(osg::Vec4(0.0f,1.0f,1.0f,0.1f)); |
|---|
| 560 | |
|---|
| 561 | if (!ss->getParents().empty()) |
|---|
| 562 | { |
|---|
| 563 | ss->getParent(0)->addChild(ss->computeIntersectionSubgraph(terrainToSS, terrainGeode.get())); |
|---|
| 564 | } |
|---|
| 565 | |
|---|
| 566 | } |
|---|
| 567 | } |
|---|
| 568 | |
|---|
| 569 | |
|---|
| 570 | if (useOverlay) |
|---|
| 571 | { |
|---|
| 572 | osgSim::OverlayNode* overlayNode = new osgSim::OverlayNode(technique); |
|---|
| 573 | overlayNode->getOrCreateStateSet()->setTextureAttribute(1, new osg::TexEnv(osg::TexEnv::DECAL)); |
|---|
| 574 | |
|---|
| 575 | const osg::BoundingSphere& bs = terrainGeode->getBound(); |
|---|
| 576 | osg::Group* overlaySubgraph = createOverlay(bs.center(), bs.radius()*0.5f); |
|---|
| 577 | overlaySubgraph->addChild(ss.get()); |
|---|
| 578 | overlayNode->setOverlaySubgraph(overlaySubgraph); |
|---|
| 579 | overlayNode->setOverlayTextureSizeHint(1024); |
|---|
| 580 | overlayNode->setOverlayBaseHeight(0.0); |
|---|
| 581 | overlayNode->addChild(terrainGeode.get()); |
|---|
| 582 | |
|---|
| 583 | root->addChild(overlayNode); |
|---|
| 584 | } |
|---|
| 585 | else |
|---|
| 586 | { |
|---|
| 587 | root->addChild(terrainGeode.get()); |
|---|
| 588 | } |
|---|
| 589 | |
|---|
| 590 | |
|---|
| 591 | { |
|---|
| 592 | osg::Vec3 position = computeTerrainIntersection(terrainGeode.get(),100.0f,100.0f); |
|---|
| 593 | |
|---|
| 594 | osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect(position, 10.0f); |
|---|
| 595 | osgParticle::SmokeEffect* smoke = new osgParticle::SmokeEffect(position, 10.0f); |
|---|
| 596 | osgParticle::FireEffect* fire = new osgParticle::FireEffect(position, 10.0f); |
|---|
| 597 | |
|---|
| 598 | root->addChild(explosion); |
|---|
| 599 | root->addChild(smoke); |
|---|
| 600 | root->addChild(fire); |
|---|
| 601 | } |
|---|
| 602 | |
|---|
| 603 | |
|---|
| 604 | { |
|---|
| 605 | osg::Vec3 position = computeTerrainIntersection(terrainGeode.get(),200.0f,100.0f); |
|---|
| 606 | |
|---|
| 607 | osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect(position, 1.0f); |
|---|
| 608 | osgParticle::SmokeEffect* smoke = new osgParticle::SmokeEffect(position, 1.0f); |
|---|
| 609 | osgParticle::FireEffect* fire = new osgParticle::FireEffect(position, 1.0f); |
|---|
| 610 | |
|---|
| 611 | root->addChild(explosion); |
|---|
| 612 | root->addChild(smoke); |
|---|
| 613 | root->addChild(fire); |
|---|
| 614 | } |
|---|
| 615 | |
|---|
| 616 | |
|---|
| 617 | bool createMovingRadar = true; |
|---|
| 618 | |
|---|
| 619 | |
|---|
| 620 | { |
|---|
| 621 | root->addChild(createMovingModel(osg::Vec3(500.0f,500.0f,500.0f),100.0f, terrainGeode.get(), root, createMovingRadar)); |
|---|
| 622 | } |
|---|
| 623 | } |
|---|
| 624 | |
|---|
| 625 | |
|---|
| 626 | |
|---|
| 627 | |
|---|
| 628 | |
|---|
| 629 | |
|---|
| 630 | |
|---|
| 631 | int main(int argc, char **argv) |
|---|
| 632 | { |
|---|
| 633 | |
|---|
| 634 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 635 | |
|---|
| 636 | |
|---|
| 637 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of particle systems."); |
|---|
| 638 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] image_file_left_eye image_file_right_eye"); |
|---|
| 639 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 640 | |
|---|
| 641 | |
|---|
| 642 | |
|---|
| 643 | osgViewer::Viewer viewer; |
|---|
| 644 | |
|---|
| 645 | |
|---|
| 646 | unsigned int testCase = 0; |
|---|
| 647 | while (arguments.read("-t", testCase)) {} |
|---|
| 648 | |
|---|
| 649 | bool useOverlay = false; |
|---|
| 650 | osgSim::OverlayNode::OverlayTechnique technique = osgSim::OverlayNode::OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY; |
|---|
| 651 | while (arguments.read("--object")) { useOverlay = true; technique = osgSim::OverlayNode::OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY; } |
|---|
| 652 | while (arguments.read("--ortho") || arguments.read("--orthographic")) { useOverlay = true; technique = osgSim::OverlayNode::VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY; } |
|---|
| 653 | while (arguments.read("--persp") || arguments.read("--perspective")) { useOverlay = true; technique = osgSim::OverlayNode::VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY; } |
|---|
| 654 | |
|---|
| 655 | |
|---|
| 656 | |
|---|
| 657 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 658 | { |
|---|
| 659 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 660 | return 1; |
|---|
| 661 | } |
|---|
| 662 | |
|---|
| 663 | |
|---|
| 664 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 665 | |
|---|
| 666 | |
|---|
| 667 | if (arguments.errors()) |
|---|
| 668 | { |
|---|
| 669 | arguments.writeErrorMessages(std::cout); |
|---|
| 670 | return 1; |
|---|
| 671 | } |
|---|
| 672 | |
|---|
| 673 | osg::Group *root = new osg::Group; |
|---|
| 674 | build_world(root, testCase, useOverlay, technique); |
|---|
| 675 | |
|---|
| 676 | |
|---|
| 677 | viewer.setSceneData(root); |
|---|
| 678 | |
|---|
| 679 | return viewer.run(); |
|---|
| 680 | } |
|---|