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);

    BlendChunkRefPtr PSBlendChunk = BlendChunk::create();
    PSBlendChunk->setSrcFactor(GL_SRC_ALPHA);
    PSBlendChunk->setDestFactor(GL_ONE_MINUS_SRC_ALPHA);

    //Particle System Material
    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(PSMaterialChunkChunk);
    PSMaterial->addChunk(PSBlendChunk);

    Distribution3DRefPtr PositionDistribution = createPositionDistribution();

    Pnt3f PositionReturnValue;

    //Particle System
    ParticleSystemRefPtr ExampleParticleSystem = OSG::ParticleSystem::create();
    for(UInt32 i(0) ; i<800 ; ++i)//controls how many particles are created
    {
        if(PositionDistribution != NULL)
        {
            PositionReturnValue = Pnt3f(PositionDistribution->generate());
        }

        ExampleParticleSystem->addParticle(
                                           PositionReturnValue,
                                           Vec3f(0.0f,0.0f,1.0f),
                                           Color4f(1.0,0.0,0.0,1.0), 
                                           Vec3f(10.0,10.0,10.0), 
                                           -1, 
                                           Vec3f(0.0f,0.0f,0.0f), //Velocity
                                           Vec3f(0.0f,0.0f,0.0f)	//acceleration
                                          );
    }
    ExampleParticleSystem->attachUpdateListener(TutorialWindow);

    //Particle System Drawer
    DiscParticleSystemDrawerRefPtr ExampleParticleSystemDrawer = OSG::DiscParticleSystemDrawer::create();
    ExampleParticleSystemDrawer->setSegments(16);
    ExampleParticleSystemDrawer->setCenterAlpha(1.0);
    ExampleParticleSystemDrawer->setEdgeAlpha(0.0);

    //Particle System Node
    ParticleSystemCoreRefPtr ParticleNodeCore = OSG::ParticleSystemCore::create();
    ParticleNodeCore->setSystem(ExampleParticleSystem);
    ParticleNodeCore->setDrawer(ExampleParticleSystemDrawer);
    ParticleNodeCore->setMaterial(PSMaterial);

    NodeRefPtr ParticleNode = OSG::Node::create();
    ParticleNode->setCore(ParticleNodeCore);


    //AttractionNode
    TransformRefPtr AttractionCore = OSG::Transform::create();
    Matrix AttractTransform;
    AttractTransform.setTranslate(0.0f, 0.0,0.0);

    AttractionCore->setMatrix(AttractTransform);

    NodeRefPtr AttractionNode = OSG::Node::create();
    AttractionNode->setCore(AttractionCore);

    // Make Main Scene Node and add the Torus
    NodeRefPtr scene = OSG::Node::create();
    scene->setCore(OSG::Group::create());
    scene->addChild(ParticleNode);
    scene->addChild(AttractionNode);

    mgr->setRoot(scene);

    // Show the whole Scene
    //mgr->showAll();
    mgr->getNavigator()->set(Pnt3f(0.0,0.0,500.0), Pnt3f(0.0,0.0,0.0), Vec3f(0.0,1.0,0.0));
    mgr->getNavigator()->setMotionFactor(1.0f);
    mgr->getCamera()->setNear(0.1f);
    mgr->getCamera()->setFar(1000.0f);

    DistanceAttractRepelParticleAffectorRefPtr ExampleDistanceAttractRepelParticleAffector = OSG::DistanceAttractRepelParticleAffector::create();

    ExampleDistanceAttractRepelParticleAffector->setMinDistance(0.0);
    ExampleDistanceAttractRepelParticleAffector->setMaxDistance(1000.0);
    ExampleDistanceAttractRepelParticleAffector->setQuadratic(0.0);
    ExampleDistanceAttractRepelParticleAffector->setLinear(100.0);
    ExampleDistanceAttractRepelParticleAffector->setConstant(0.0);
    ExampleDistanceAttractRepelParticleAffector->setParticleSystemNode(ParticleNode);
    ExampleDistanceAttractRepelParticleAffector->setDistanceFromSource(DistanceAttractRepelParticleAffector::DISTANCE_FROM_NODE);
    ExampleDistanceAttractRepelParticleAffector->setDistanceFromNode(AttractionNode);


    ExampleParticleSystem->pushToAffectors(ExampleDistanceAttractRepelParticleAffector);

    //Open Window
    Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f);
    Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5);
    TutorialWindow->openWindow(WinPos,
                               WinSize,
                               "11DistanceAttractRepelParticleAffector");

    //Enter main Loop
    TutorialWindow->mainLoop();

    osgExit();

    return 0;
}
Esempio n. 2
0
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));

        //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.2f,0.6f,0.5f,0.3f));
        PSMaterialChunkChunk->setDiffuse(Color4f(0.2f,0.9f,0.1f,0.3f));
        PSMaterialChunkChunk->setSpecular(Color4f(0.5f,0.4f,0.2f,0.6f));
        PSMaterialChunkChunk->setColorMaterial(GL_AMBIENT_AND_DIFFUSE);

        //enable depth test
        DepthChunkRefPtr PSDepthChunk = DepthChunk::create();

        ChunkMaterialRefPtr PSMaterial = ChunkMaterial::create();
        PSMaterial->addChunk(PSPointChunk);
        PSMaterial->addChunk(PSMaterialChunkChunk);
        PSMaterial->addChunk(PSBlendChunk);
        PSMaterial->addChunk(PSDepthChunk);

        LineChunkRefPtr PSLineChunk = LineChunk::create();
        ChunkMaterialRefPtr TestMaterial = ChunkMaterial::create();
        //TestMaterial->addChunk(PointChunk::create());
        //TestMaterial->addChunk(LineChunk::create());
        TestMaterial->addChunk(PSMaterialChunkChunk);
        PolygonChunkRefPtr ThePolygonChunk = PolygonChunk::create();
        BlendChunkRefPtr TheBlendChunk = BlendChunk::create();
        DepthChunkRefPtr TheDepthChunk = DepthChunk::create();
        TestMaterial->addChunk(ThePolygonChunk);
        TestMaterial->addChunk(TheBlendChunk);
        TestMaterial->addChunk(TheDepthChunk);

        //Particle System
        ParticleSystemRecPtr ExampleParticleSystem = ParticleSystem::create();


        ExampleParticleSystem->attachUpdateProducer(TutorialWindow);

        ExampleParticleSystem->addParticle(Pnt3f(-40.0,0.0,0.0),
                                           Vec3f(0.0,1.0,0.0),
                                           Color4f(1.0,1.0,1.0,1.0),
                                           Vec3f(1.0,1.0,1.0),
                                           -1,
                                           Vec3f(0.0,0.0,0.0),
                                           Vec3f(0.0,0.0,0.0));

        ExampleParticleSystem->addParticle(Pnt3f(40.0,0.0,0.0),
                                           Vec3f(0.0,1.0,0.0),
                                           Color4f(1.0,1.0,1.0,1.0),
                                           Vec3f(1.0,1.0,1.0),
                                           -1,
                                           Vec3f(0.0,0.0,0.0),
                                           Vec3f(0.0,0.0,0.0));

        PointParticleSystemDrawerRecPtr ExamplePointParticleSystemDrawer = PointParticleSystemDrawer::create();
        ExamplePointParticleSystemDrawer->setForcePerParticleSizing(false);

        Matrix ExampleMatrix;
        ExampleMatrix.setTransform(Vec3f(10.0,10.0,10.0));

        TransformRefPtr ExampleXform = Transform::create();
        ExampleXform->setMatrix(ExampleMatrix);


        NodeRefPtr ExampleNode = Node::create();
        ExampleNode->setCore(ExampleXform);

        RateParticleGeneratorRecPtr ExampleGenerator = RateParticleGenerator::create();
        //		ExampleGenerator->setEmitInWorldSpace(true);
        ExampleGenerator->setBeacon(ExampleNode);
        ExampleGenerator->setGenerationRate(5.0);
        ExampleGenerator->setPositionDistribution(createPositionDistribution());
        ExampleGenerator->setLifespanDistribution(createLifespanDistribution());

        NewtonParticleAffectorRefPtr ExampleAffector = NewtonParticleAffector::create();
        ExampleAffector->setBeacon(ExampleNode);
        ExampleAffector->setMaxDistance(-1.0);

        ConditionalParticleAffectorRecPtr ExampleConditionalAffector = ConditionalParticleAffector::create();
        ExampleConditionalAffector->setConditionalAttribute("active");
        ExampleConditionalAffector->setConditionalOperator(4); //greater than
        ExampleConditionalAffector->setConditionalValue(0); // testing if the value associated with "test" = 1
        ExampleConditionalAffector->pushToAffectors(ExampleAffector);

        DistanceAttractRepelParticleAffectorRefPtr ExampleAttractRepelAffector = DistanceAttractRepelParticleAffector::create();
        ExampleAttractRepelAffector->setDistanceFromSource(DistanceParticleAffector::DISTANCE_FROM_NODE);

        //Attach the Generators and affectors to the Particle System
        ExampleParticleSystem->setBeacon(ExampleNode);
        //ExampleParticleSystem->pushToGenerators(ExampleGenerator);
        ExampleParticleSystem->pushToAffectors(ExampleConditionalAffector);
        // ExampleParticleSystem->pushToAffectors(ExampleAttractRepelAffector);
        ExampleParticleSystem->setMaxParticles(1000);
        ExampleParticleSystem->setDynamic(true);

        //Particle System Core
        ParticleSystemCoreRecPtr ParticleNodeCore = ParticleSystemCore::create();
        ParticleNodeCore->setSystem(ExampleParticleSystem);
        ParticleNodeCore->setDrawer(ExamplePointParticleSystemDrawer);
        ParticleNodeCore->setMaterial(PSMaterial);
        ParticleNodeCore->setSortingMode(ParticleSystemCore::BACK_TO_FRONT);

        NodeRefPtr PSNode = Node::create();
        PSNode->setCore(ParticleNodeCore);

        // Make Main Scene Node and add the Torus
        NodeRefPtr scene = Node::create();
        scene->setCore(Group::create());
        scene->addChild(PSNode);


        TutorialWindow->connectKeyTyped(boost::bind(keyTyped, _1, &sceneManager,
                                        ParticleNodeCore.get(),
                                        ExamplePointParticleSystemDrawer.get(),
                                        ExampleParticleSystem.get(),
                                        ExampleConditionalAffector.get()));
        sceneManager.setRoot(scene);

        // Show the whole Scene
        sceneManager.showAll();
        sceneManager.getCamera()->setFar(1000.0);
        sceneManager.getCamera()->setNear(0.10);

        FCFileType::FCPtrStore Containers;
        Containers.insert(scene);

        //Open Window
        Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f);
        Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5);
        TutorialWindow->openWindow(WinPos,
                                   WinSize,
                                   "16FullTest");

        //Enter main Loop
        TutorialWindow->mainLoop();

    }
    osgExit();

    return 0;
}