int main(int argc, char **argv) { // OSG init osgInit(argc,argv); { // Set up Window WindowEventProducerRecPtr TutorialWindow = createNativeWindow(); TutorialWindow->initWindow(); // Create the SimpleSceneManager helper SimpleSceneManager sceneManager; TutorialWindow->setDisplayCallback(boost::bind(display, &sceneManager)); TutorialWindow->setReshapeCallback(boost::bind(reshape, _1, &sceneManager)); // Tell the Manager what to manage sceneManager.setWindow(TutorialWindow); //Attach to events TutorialWindow->connectMousePressed(boost::bind(mousePressed, _1, &sceneManager)); TutorialWindow->connectMouseReleased(boost::bind(mouseReleased, _1, &sceneManager)); TutorialWindow->connectMouseMoved(boost::bind(mouseMoved, _1, &sceneManager)); TutorialWindow->connectMouseDragged(boost::bind(mouseDragged, _1, &sceneManager)); TutorialWindow->connectMouseWheelMoved(boost::bind(mouseWheelMoved, _1, &sceneManager)); TutorialWindow->connectKeyTyped(boost::bind(keyTyped, _1, &sceneManager)); //Particle System Material PointChunkRefPtr PSPointChunk = PointChunk::create(); PSPointChunk->setSize(20.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(0.3f,0.3f,0.3f,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); Distribution3DRefPtr PositionDistribution = createPositionDistribution(); Distribution1DRefPtr LifespanDistribution = createLifespanDistribution(); Pnt3f PositionReturnValue; Time LifespanReturnValue = -1; //Particle System ParticleSystemRefPtr ExampleParticleSystem = ParticleSystem::create(); for(UInt32 i(0) ; i<200 ; ++i)//controls how many particles are created { if(PositionDistribution != NULL) { PositionReturnValue = Pnt3f(PositionDistribution->generate()); } if(LifespanDistribution != NULL) { LifespanReturnValue = LifespanDistribution->generate(); } ExampleParticleSystem->addParticle( PositionReturnValue, Vec3f(0.0f,0.0f,1.0f), Color4f(1.0,0.0,0.0,1.0), Vec3f(1.0,1.0,1.0), LifespanReturnValue, Vec3f(0.0f,0.0f,0.0f), //Velocity Vec3f(0.0f,0.0f,0.0f) //acceleration ); } ExampleParticleSystem->attachUpdateProducer(TutorialWindow); //Particle System Drawer PointParticleSystemDrawerRefPtr ExampleParticleSystemDrawer = PointParticleSystemDrawer::create(); //Create an CollectiveGravityParticleSystemAffector CollectiveGravityParticleSystemAffectorRefPtr ExampleCollectiveGravityParticleSystemAffector = CollectiveGravityParticleSystemAffector::create(); ExampleCollectiveGravityParticleSystemAffector->setParticleMass(1000000.0f); ExampleParticleSystem->pushToSystemAffectors(ExampleCollectiveGravityParticleSystemAffector); //Particle System Node ParticleSystemCoreRefPtr ParticleNodeCore = ParticleSystemCore::create(); ParticleNodeCore->setSystem(ExampleParticleSystem); ParticleNodeCore->setDrawer(ExampleParticleSystemDrawer); ParticleNodeCore->setMaterial(PSMaterial); NodeRefPtr ParticleNode = Node::create(); ParticleNode->setCore(ParticleNodeCore); // Make Main Scene Node and add the Torus NodeRefPtr scene = Node::create(); scene->setCore(Group::create()); scene->addChild(ParticleNode); sceneManager.setRoot(scene); // Show the whole Scene sceneManager.showAll(); //Open Window Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f); Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5); TutorialWindow->openWindow(WinPos, WinSize, "10GravityParticleSystemAffector"); //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); Distribution3DRefPtr PositionDistribution = createPositionDistribution(); Distribution1DRefPtr LifespanDistribution = createLifespanDistribution(); Pnt3f PositionReturnValue; Time LifespanReturnValue = -1; //Particle System ParticleSystemRefPtr ExampleParticleSystem = OSG::ParticleSystem::create(); for(UInt32 i(0) ; i<200 ; ++i)//controls how many particles are created { if(PositionDistribution != NULL) { PositionReturnValue = Pnt3f(PositionDistribution->generate()); } if(LifespanDistribution != NULL) { LifespanReturnValue = LifespanDistribution->generate(); } ExampleParticleSystem->addParticle( PositionReturnValue, Vec3f(1.0f,0.0f,0.0f), Color4f(1.0,0.0,0.0,1.0), Vec3f(1.0,1.0,1.0), LifespanReturnValue, Vec3f(0.0f,0.0f,0.0f), //Velocity Vec3f(0.0f,0.0f,0.0f) //acceleration ); } ExampleParticleSystem->attachUpdateListener(TutorialWindow); //NodeRefPtr ParticlePrototypeNode = makeTorus(1.0,4.0,16,16); NodeRefPtr ParticlePrototypeNode = SceneFileHandler::the()->read("Data/rocket.obj"); if(ParticlePrototypeNode == NULL) { ParticlePrototypeNode = makeTorus(.5, 2, 16, 16); } //Particle System Node NodeParticleSystemCoreRefPtr NodeParticleNodeCore = OSG::NodeParticleSystemCore::create(); NodeParticleNodeCore->setSystem(ExampleParticleSystem); NodeParticleNodeCore->setPrototypeNode(ParticlePrototypeNode); NodeParticleNodeCore->setNormalSource(NodeParticleSystemCore::NORMAL_VELOCITY); NodeParticleNodeCore->setUpSource(NodeParticleSystemCore::UP_STATIC); NodeParticleNodeCore->setUp(Vec3f(0.0f,1.0f,0.0f)); NodeRefPtr ParticleNode = OSG::Node::create(); ParticleNode->setCore(NodeParticleNodeCore); //Create an CollectiveGravityParticleSystemAffector CollectiveGravityParticleSystemAffectorRefPtr ExampleCollectiveGravityParticleSystemAffector = OSG::CollectiveGravityParticleSystemAffector::create(); ExampleCollectiveGravityParticleSystemAffector->setParticleMass(10000000000.0f); ExampleParticleSystem->pushToSystemAffectors(ExampleCollectiveGravityParticleSystemAffector); // 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(); //Open Window Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f); Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5); TutorialWindow->openWindow(WinPos, WinSize, "12NodeParticleDrawer"); //Enter main Loop TutorialWindow->mainLoop(); osgExit(); return 0; }