/usr/share/openscenegraph/examples/osgparticleeffects/osgparticleeffects.cpp is in openscenegraph-examples 3.0.1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | /* OpenSceneGraph example, osgparticleeffects.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <osgViewer/Viewer>
#include <osg/Group>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/Texture2D>
#include <osg/PositionAttitudeTransform>
#include <osg/MatrixTransform>
#include <osg/io_utils>
#include <osgUtil/Optimizer>
#include <osgDB/ReadFile>
#include <osgText/Text>
#include <osgParticle/ExplosionEffect>
#include <osgParticle/ExplosionDebrisEffect>
#include <osgParticle/SmokeEffect>
#include <osgParticle/SmokeTrailEffect>
#include <osgParticle/FireEffect>
// for the grid data..
#include "../osghangglide/terrain_coords.h"
osg::Vec3 wind(1.0f,0.0f,0.0f);
osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime)
{
// set up the animation path
osg::AnimationPath* animationPath = new osg::AnimationPath;
animationPath->setLoopMode(osg::AnimationPath::LOOP);
int numSamples = 40;
float yaw = 0.0f;
float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f);
float roll = osg::inDegrees(30.0f);
double time=0.0f;
double time_delta = looptime/(double)numSamples;
for(int i=0;i<numSamples;++i)
{
osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f));
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)));
animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation));
yaw += yaw_delta;
time += time_delta;
}
return animationPath;
}
osg::Node* createMovingModel(const osg::Vec3& center, float radius)
{
float animationLength = 10.0f;
osg::AnimationPath* animationPath = createAnimationPath(center,radius,animationLength);
osg::Group* model = new osg::Group;
osg::Node* glider = osgDB::readNodeFile("glider.osgt");
if (glider)
{
const osg::BoundingSphere& bs = glider->getBound();
float size = radius/bs.radius()*0.15f;
osg::MatrixTransform* positioned = new osg::MatrixTransform;
positioned->setDataVariance(osg::Object::STATIC);
positioned->setMatrix(osg::Matrix::translate(-bs.center())*
osg::Matrix::scale(size,size,size)*
osg::Matrix::rotate(osg::inDegrees(-90.0f),0.0f,0.0f,1.0f));
positioned->addChild(glider);
osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform;
xform->setDataVariance(osg::Object::DYNAMIC);
xform->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,0.5));
xform->addChild(positioned);
model->addChild(xform);
}
osg::Node* cessna = osgDB::readNodeFile("cessna.osgt");
if (cessna)
{
const osg::BoundingSphere& bs = cessna->getBound();
float size = radius/bs.radius()*0.15f;
osg::MatrixTransform* positioned = new osg::MatrixTransform;
positioned->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
positioned->setDataVariance(osg::Object::STATIC);
positioned->setMatrix(osg::Matrix::translate(-bs.center())*
osg::Matrix::scale(size,size,size)*
osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,1.0f));
//positioned->addChild(cessna);
positioned->addChild(cessna);
osg::MatrixTransform* xform = new osg::MatrixTransform;
xform->setDataVariance(osg::Object::DYNAMIC);
xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,1.0));
xform->addChild(positioned);
model->addChild(xform);
}
return model;
}
osg::Vec3 computeTerrainIntersection(osg::Node* subgraph,float x,float y)
{
const osg::BoundingSphere& bs = subgraph->getBound();
float zMax = bs.center().z()+bs.radius();
float zMin = bs.center().z()-bs.radius();
osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector =
new osgUtil::LineSegmentIntersector(osg::Vec3(x,y,zMin),osg::Vec3(x,y,zMax));
osgUtil::IntersectionVisitor iv(intersector.get());
subgraph->accept(iv);
if (intersector->containsIntersections())
{
return intersector->getFirstIntersection().getWorldIntersectPoint();
}
return osg::Vec3(x,y,0.0f);
}
//////////////////////////////////////////////////////////////////////////////
// MAIN SCENE GRAPH BUILDING FUNCTION
//////////////////////////////////////////////////////////////////////////////
void build_world(osg::Group *root)
{
osg::Geode* terrainGeode = new osg::Geode;
// create terrain
{
osg::StateSet* stateset = new osg::StateSet();
osg::Image* image = osgDB::readImageFile("Images/lz.rgb");
if (image)
{
osg::Texture2D* texture = new osg::Texture2D;
texture->setImage(image);
stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
}
terrainGeode->setStateSet( stateset );
float size = 1000; // 10km;
float scale = size/39.0f; // 10km;
float z_scale = scale*3.0f;
osg::HeightField* grid = new osg::HeightField;
grid->allocate(38,39);
grid->setXInterval(scale);
grid->setYInterval(scale);
for(unsigned int r=0;r<39;++r)
{
for(unsigned int c=0;c<38;++c)
{
grid->setHeight(c,r,z_scale*vertex[r+c*39][2]);
}
}
terrainGeode->addDrawable(new osg::ShapeDrawable(grid));
root->addChild(terrainGeode);
}
// create particle effects
{
osg::Vec3 position = computeTerrainIntersection(terrainGeode,100.0f,100.0f);
osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect(position, 10.0f);
osgParticle::ExplosionDebrisEffect* explosionDebri = new osgParticle::ExplosionDebrisEffect(position, 10.0f);
osgParticle::SmokeEffect* smoke = new osgParticle::SmokeEffect(position, 10.0f);
osgParticle::FireEffect* fire = new osgParticle::FireEffect(position, 10.0f);
explosion->setWind(wind);
explosionDebri->setWind(wind);
smoke->setWind(wind);
fire->setWind(wind);
root->addChild(explosion);
root->addChild(explosionDebri);
root->addChild(smoke);
root->addChild(fire);
}
// create particle effects
{
osg::Vec3 position = computeTerrainIntersection(terrainGeode,200.0f,100.0f);
osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect(position, 1.0f);
osgParticle::ExplosionDebrisEffect* explosionDebri = new osgParticle::ExplosionDebrisEffect(position, 1.0f);
osgParticle::SmokeEffect* smoke = new osgParticle::SmokeEffect(position, 1.0f);
osgParticle::FireEffect* fire = new osgParticle::FireEffect(position, 1.0f);
explosion->setWind(wind);
explosionDebri->setWind(wind);
smoke->setWind(wind);
fire->setWind(wind);
root->addChild(explosion);
root->addChild(explosionDebri);
root->addChild(smoke);
root->addChild(fire);
}
// create the moving models.
{
root->addChild(createMovingModel(osg::Vec3(500.0f,500.0f,500.0f),300.0f));
}
}
// class to handle events with a pick
class PickHandler : public osgGA::GUIEventHandler {
public:
PickHandler() {}
bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
{
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::PUSH):
{
osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
pick(viewer,ea);
}
return false;
default:
return false;
}
}
void pick(osgViewer::Viewer* viewer, const osgGA::GUIEventAdapter& ea)
{
osg::Group* root = dynamic_cast<osg::Group*>(viewer->getSceneData());
if (!root) return;
osgUtil::LineSegmentIntersector::Intersections intersections;
if (viewer->computeIntersections(ea.getX(),ea.getY(),intersections))
{
const osgUtil::LineSegmentIntersector::Intersection& hit = *intersections.begin();
bool handleMovingModels = false;
const osg::NodePath& nodePath = hit.nodePath;
for(osg::NodePath::const_iterator nitr=nodePath.begin();
nitr!=nodePath.end();
++nitr)
{
const osg::Transform* transform = dynamic_cast<const osg::Transform*>(*nitr);
if (transform)
{
if (transform->getDataVariance()==osg::Object::DYNAMIC) handleMovingModels=true;
}
}
osg::Vec3 position = handleMovingModels ? hit.getLocalIntersectPoint() : hit.getWorldIntersectPoint();
float scale = 10.0f * ((float)rand() / (float)RAND_MAX);
float intensity = 1.0f;
osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect(position, scale, intensity);
osgParticle::ExplosionDebrisEffect* explosionDebri = new osgParticle::ExplosionDebrisEffect(position, scale, intensity);
osgParticle::FireEffect* fire = new osgParticle::FireEffect(position, scale, intensity);
osgParticle::ParticleEffect* smoke = 0;
if (handleMovingModels)
smoke = new osgParticle::SmokeTrailEffect(position, scale, intensity);
else
smoke = new osgParticle::SmokeEffect(position, scale, intensity);
explosion->setWind(wind);
explosionDebri->setWind(wind);
smoke->setWind(wind);
fire->setWind(wind);
osg::Group* effectsGroup = new osg::Group;
effectsGroup->addChild(explosion);
effectsGroup->addChild(explosionDebri);
effectsGroup->addChild(smoke);
effectsGroup->addChild(fire);
if (handleMovingModels)
{
// insert particle effects alongside the hit node, therefore able to track that nodes movement,
// however, this does require us to insert the ParticleSystem itself into the root of the scene graph
// separately from the the main particle effects group which contains the emitters and programs.
// the follow code block implements this, note the path for handling particle effects which arn't attached to
// moving models is easy - just a single line of code!
// tell the effects not to attach to the particle system locally for rendering, as we'll handle add it into the
// scene graph ourselves.
explosion->setUseLocalParticleSystem(false);
explosionDebri->setUseLocalParticleSystem(false);
smoke->setUseLocalParticleSystem(false);
fire->setUseLocalParticleSystem(false);
// find a place to insert the particle effects group alongside the hit node.
// there are two possible ways that this can be done, either insert it into
// a pre-existing group along side the hit node, or if no pre existing group
// is found then this needs to be inserted above the hit node, and then the
// particle effect can be inserted into this.
osg::ref_ptr<osg::Node> hitNode = hit.nodePath.back();
osg::Node::ParentList parents = hitNode->getParents();
osg::Group* insertGroup = 0;
unsigned int numGroupsFound = 0;
for(osg::Node::ParentList::iterator itr=parents.begin();
itr!=parents.end();
++itr)
{
if (typeid(*(*itr))==typeid(osg::Group))
{
++numGroupsFound;
insertGroup = *itr;
}
}
if (numGroupsFound==parents.size() && numGroupsFound==1 && insertGroup)
{
osg::notify(osg::INFO)<<"PickHandler::pick(,) hit node's parent is a single osg::Group so we can simple the insert the particle effects group here."<<std::endl;
// just reuse the existing group.
insertGroup->addChild(effectsGroup);
}
else
{
osg::notify(osg::INFO)<<"PickHandler::pick(,) hit node doesn't have an appropriate osg::Group node to insert particle effects into, inserting a new osg::Group."<<std::endl;
insertGroup = new osg::Group;
for(osg::Node::ParentList::iterator itr=parents.begin();
itr!=parents.end();
++itr)
{
(*itr)->replaceChild(hit.nodePath.back(),insertGroup);
}
insertGroup->addChild(hitNode.get());
insertGroup->addChild(effectsGroup);
}
// finally insert the particle systems into a Geode and attach to the root of the scene graph so the particle system
// can be rendered.
osg::Geode* geode = new osg::Geode;
geode->addDrawable(explosion->getParticleSystem());
geode->addDrawable(explosionDebri->getParticleSystem());
geode->addDrawable(smoke->getParticleSystem());
geode->addDrawable(fire->getParticleSystem());
root->addChild(geode);
}
else
{
// when we don't have moving models we can simple insert the particle effect into the root of the scene graph
osg::notify(osg::INFO)<<"PickHandler::pick(,) adding particle effects to root node."<<std::endl;
root->addChild(effectsGroup);
}
#if 0
osg::Geode* geode = new osg::Geode;
geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(position,scale)));
group->addChild(geode);
#endif
}
}
protected:
virtual ~PickHandler() {}
};
// function used in debugging
void insertParticle(osg::Group* root, const osg::Vec3& center, float radius)
{
bool handleMovingModels = false;
osg::Vec3 position = center +
osg::Vec3( radius * (((float)rand() / (float)RAND_MAX)-0.5)*2.0,
radius * (((float)rand() / (float)RAND_MAX)-0.5)*2.0,
0.0f);
float scale = 10.0f * ((float)rand() / (float)RAND_MAX);
float intensity = 1.0f;
osgParticle::ExplosionEffect* explosion = new osgParticle::ExplosionEffect(position, scale, intensity);
osgParticle::ExplosionDebrisEffect* explosionDebri = new osgParticle::ExplosionDebrisEffect(position, scale, intensity);
osgParticle::FireEffect* fire = new osgParticle::FireEffect(position, scale, intensity);
osgParticle::ParticleEffect* smoke = 0;
if (handleMovingModels)
smoke = new osgParticle::SmokeTrailEffect(position, scale, intensity);
else
smoke = new osgParticle::SmokeEffect(position, scale, intensity);
explosion->setWind(wind);
explosionDebri->setWind(wind);
smoke->setWind(wind);
fire->setWind(wind);
osg::Group* effectsGroup = new osg::Group;
effectsGroup->addChild(explosion);
effectsGroup->addChild(explosionDebri);
effectsGroup->addChild(smoke);
effectsGroup->addChild(fire);
root->addChild(effectsGroup);
}
//////////////////////////////////////////////////////////////////////////////
// main()
//////////////////////////////////////////////////////////////////////////////
int main(int, char **)
{
// construct the viewer.
osgViewer::Viewer viewer;
// register the pick handler
viewer.addEventHandler(new PickHandler());
osg::Group *root = new osg::Group;
build_world(root);
osgUtil::Optimizer optimizer;
optimizer.optimize(root);
// add a viewport to the viewer and attach the scene graph.
viewer.setSceneData(root);
return viewer.run();
}
|