int main(int argc, char **argv) { // OSG init osgInit(argc,argv); // Set up Window TutorialWindow = createNativeWindow(); TutorialWindow->initWindow(); TutorialWindow->setDisplayCallback(display); TutorialWindow->setReshapeCallback(reshape); TutorialKeyListener TheKeyListener; TutorialWindow->addKeyListener(&TheKeyListener); TutorialMouseListener TheTutorialMouseListener; TutorialMouseMotionListener TheTutorialMouseMotionListener; TutorialWindow->addMouseListener(&TheTutorialMouseListener); TutorialWindow->addMouseMotionListener(&TheTutorialMouseMotionListener); // Create the SimpleSceneManager helper mgr = new SimpleSceneManager; // Tell the Manager what to manage mgr->setWindow(TutorialWindow); //Particle System Material PointChunkRefPtr PSPointChunk = PointChunk::create(); PSPointChunk->setSize(5.0f); PSPointChunk->setSmooth(true); BlendChunkRefPtr PSBlendChunk = BlendChunk::create(); PSBlendChunk->setSrcFactor(GL_SRC_ALPHA); PSBlendChunk->setDestFactor(GL_ONE_MINUS_SRC_ALPHA); MaterialChunkRefPtr PSMaterialChunkChunk = MaterialChunk::create(); PSMaterialChunkChunk->setAmbient(Color4f(1.0f,1.0f,1.0f,1.0f)); PSMaterialChunkChunk->setDiffuse(Color4f(0.7f,0.7f,0.7f,1.0f)); PSMaterialChunkChunk->setSpecular(Color4f(0.9f,0.9f,0.9f,1.0f)); PSMaterialChunkChunk->setColorMaterial(GL_AMBIENT_AND_DIFFUSE); ChunkMaterialRefPtr PSMaterial = ChunkMaterial::create(); PSMaterial->addChunk(PSPointChunk); PSMaterial->addChunk(PSMaterialChunkChunk); PSMaterial->addChunk(PSBlendChunk); //Particle System ParticleSystemRefPtr ExampleParticleSystem = OSG::ParticleSystem::create(); ExampleParticleSystem->addParticle(Pnt3f(0,0,0), Vec3f(0.0,0.0f,1.0f), Color4f(1.0,1.0,1.0,1.0), Vec3f(1.0,1.0,1.0), 0.1, Vec3f(0.0f,0.0f,0.0f), //Velocity Vec3f(0.0f,0.0f,0.0f) ); ExampleParticleSystem->addParticle(Pnt3f(50,0,0), Vec3f(0.0,0.0f,1.0f), Color4f(1.0,1.0,1.0,1.0), Vec3f(1.0,1.0,1.0), 0.1, Vec3f(0.0f,0.0f,0.0f), //Velocity Vec3f(0.0f,0.0f,0.0f) ); ExampleParticleSystem->attachUpdateListener(TutorialWindow); //Particle System Drawer (Point) ExamplePointParticleSystemDrawer = OSG::PointParticleSystemDrawer::create(); //Particle System Drawer (line) ExampleLineParticleSystemDrawer = OSG::LineParticleSystemDrawer::create(); ExampleLineParticleSystemDrawer->setLineDirectionSource(LineParticleSystemDrawer::DIRECTION_VELOCITY); ExampleLineParticleSystemDrawer->setLineLengthSource(LineParticleSystemDrawer::LENGTH_SIZE_X); ExampleLineParticleSystemDrawer->setLineLength(0.5f); ExampleLineParticleSystemDrawer->setEndPointFading(Vec2f(1.0f,0.0f)); //Create a Rate Particle Generator RateParticleGeneratorRefPtr ExampleGenerator = OSG::RateParticleGenerator::create(); //Attach the function objects to the Generator ExampleGenerator->setPositionDistribution(createPositionDistribution()); ExampleGenerator->setLifespanDistribution(createLifespanDistribution()); ExampleGenerator->setVelocityDistribution(createVelocityDistribution()); ExampleGenerator->setGenerationRate(2.0); ExampleConserveVelocityAffector = OSG::ConserveVelocityParticleAffector::create(); ExampleConserveVelocityAffector->setConserve(0.0); // all velocity conserved initially. Use keys 3 and 4 to change this value while running. //Attach the Generator and Affector to the Particle System ExampleParticleSystem->pushToGenerators(ExampleGenerator); ExampleParticleSystem->pushToAffectors(ExampleConserveVelocityAffector); ExampleParticleSystem->setMaxParticles(500); //Particle System Node ParticleNodeCore = OSG::ParticleSystemCore::create(); ParticleNodeCore->setSystem(ExampleParticleSystem); ParticleNodeCore->setDrawer(ExamplePointParticleSystemDrawer); ParticleNodeCore->setMaterial(PSMaterial); NodeRefPtr ParticleNode = OSG::Node::create(); ParticleNode->setCore(ParticleNodeCore); // Make Main Scene Node and add the Torus NodeRefPtr scene = OSG::Node::create(); scene->setCore(OSG::Group::create()); scene->addChild(ParticleNode); mgr->setRoot(scene); // Show the whole Scene mgr->showAll(); mgr->getCamera()->setFar(1000.0); std::cout << "Conserve Velocity Particle Affector Tutorial Controls:\n" << "1: Use point drawer\n" << "2: Use line drawer\n" << "3: Decrease velocity conserved.\n" << "4: Increase velocity conserved.\n" << "Ctrl + Q: Exit Tutorial"; //Open Window Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f); Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5); TutorialWindow->openWindow(WinPos, WinSize, "28ConserveVelocityParticleAffector"); //Enter main Loop TutorialWindow->mainLoop(); osgExit(); return 0; }
int main(int argc, char **argv) { // OSG init osgInit(argc,argv); // Set up Window TutorialWindow = createNativeWindow(); TutorialWindow->initWindow(); TutorialWindow->setDisplayCallback(display); TutorialWindow->setReshapeCallback(reshape); TutorialKeyListener TheKeyListener; TutorialWindow->addKeyListener(&TheKeyListener); TutorialMouseListener TheTutorialMouseListener; TutorialMouseMotionListener TheTutorialMouseMotionListener; TutorialWindow->addMouseListener(&TheTutorialMouseListener); TutorialWindow->addMouseMotionListener(&TheTutorialMouseMotionListener); // Create the SimpleSceneManager helper mgr = new SimpleSceneManager; // Tell the Manager what to manage mgr->setWindow(TutorialWindow); //Particle System Material PointChunkRefPtr PSPointChunk = PointChunk::create(); PSPointChunk->setSize(5.0f); PSPointChunk->setSmooth(true); BlendChunkRefPtr PSBlendChunk = BlendChunk::create(); PSBlendChunk->setSrcFactor(GL_SRC_ALPHA); PSBlendChunk->setDestFactor(GL_ONE_MINUS_SRC_ALPHA); MaterialChunkRefPtr PSMaterialChunkChunk = MaterialChunk::create(); PSMaterialChunkChunk->setAmbient(Color4f(1.0f,1.0f,1.0f,1.0f)); PSMaterialChunkChunk->setDiffuse(Color4f(0.7f,0.7f,0.7f,1.0f)); PSMaterialChunkChunk->setSpecular(Color4f(0.9f,0.9f,0.9f,1.0f)); PSMaterialChunkChunk->setColorMaterial(GL_NONE); ChunkMaterialRefPtr PSMaterial = ChunkMaterial::create(); PSMaterial->addChunk(PSPointChunk); PSMaterial->addChunk(PSMaterialChunkChunk); PSMaterial->addChunk(PSBlendChunk); //Particle System ParticleSystemRefPtr ExampleParticleSystem = OSG::ParticleSystem::create(); ExampleParticleSystem->addParticle(Pnt3f(0,25,0), Vec3f(0.0,0.0f,1.0f), Color4f(1.0,1.0,1.0,1.0), Vec3f(1.0,1.0,1.0), 0.1, Vec3f(0.0f,0.0f,0.0f), //Velocity Vec3f(0.0f,0.0f,0.0f) ); ExampleParticleSystem->addParticle(Pnt3f(0,-25,0), Vec3f(0.0,0.0f,1.0f), Color4f(1.0,1.0,1.0,1.0), Vec3f(1.0,1.0,1.0), 0.1, Vec3f(0.0f,0.0f,0.0f), //Velocity Vec3f(0.0f,0.0f,0.0f) ); ExampleParticleSystem->attachUpdateListener(TutorialWindow); //Particle System Drawer (Point) ExamplePointParticleSystemDrawer = OSG::PointParticleSystemDrawer::create(); //Particle System Drawer (line) ExampleLineParticleSystemDrawer = OSG::LineParticleSystemDrawer::create(); ExampleLineParticleSystemDrawer->setLineDirectionSource(LineParticleSystemDrawer::DIRECTION_VELOCITY); ExampleLineParticleSystemDrawer->setLineLengthSource(LineParticleSystemDrawer::LENGTH_SIZE_X); ExampleLineParticleSystemDrawer->setLineLength(2.0f); ExampleLineParticleSystemDrawer->setEndPointFading(Vec2f(0.0f,1.0f)); //Create a Rate Particle Generator RateParticleGeneratorRefPtr ExampleGenerator = OSG::RateParticleGenerator::create(); //Attach the function objects to the Generator ExampleGenerator->setPositionDistribution(createPositionDistribution()); ExampleGenerator->setLifespanDistribution(createLifespanDistribution()); ExampleGenerator->setGenerationRate(200); ExampleUniformAffector = OSG::UniformParticleAffector::create(); ExampleUniformAffector->setMagnitude(20.0); // force which the field exerts on particles (negative = towards the air field's beacon location) NodeRefPtr UniformBeacon = OSG::Node::create(); ExampleUniformAffector->setBeacon(UniformBeacon); // set to 'emulate' from (0,0,0) ExampleUniformAffector->setDirection(Vec3f(1.0,0.0,0.0)); // direction which field is exerted ExampleUniformAffector->setMaxDistance(-1.0); // particles affected regardless of distance from ExampleUniformAffector->setAttenuation(0.0); // strength of uniform field dimishes by dist^attenuation, in this case it is constant regardless of distance ExampleUniformAffector->setParticleMass(10.0); //Attach the Generator and Affector to the Particle System ExampleParticleSystem->pushToGenerators(ExampleGenerator); ExampleParticleSystem->pushToAffectors(ExampleUniformAffector); ExampleParticleSystem->setMaxParticles(500); //Particle System Node ParticleNodeCore = OSG::ParticleSystemCore::create(); ParticleNodeCore->setSystem(ExampleParticleSystem); ParticleNodeCore->setDrawer(ExamplePointParticleSystemDrawer); ParticleNodeCore->setMaterial(PSMaterial); NodeRefPtr ParticleNode = OSG::Node::create(); ParticleNode->setCore(ParticleNodeCore); // Make Main Scene Node and add the Torus NodeRefPtr scene = OSG::Node::create(); scene->setCore(OSG::Group::create()); scene->addChild(ParticleNode); mgr->setRoot(scene); // Show the whole Scene mgr->showAll(); mgr->getCamera()->setFar(1000.0); std::cout << "Uniform Particle Affector Tutorial Controls:\n" << "1: Use point drawer\n" << "2: Use line drawer\n" << "W,A,S,D: Change direction of field\n" << "Ctrl + Q: Exit Tutorial"; //Open Window Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f); Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5); TutorialWindow->openWindow(WinPos, WinSize, "19UniformFieldParticleAffector"); //Enter main Loop TutorialWindow->mainLoop(); osgExit(); return 0; }