// Initialize WIN32 & OpenSG and set up the scene
int main(int argc, char **argv)
{
    preloadSharedObject("OSGTBFileIO");

    // 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->connectMouseDragged(boost::bind(mouseDragged, _1, &sceneManager));
        TutorialWindow->connectMouseWheelMoved(boost::bind(mouseWheelMoved, _1, &sceneManager));
        TutorialWindow->connectKeyTyped(boost::bind(keyTyped, _1));

        // create the scene
        NodeUnrecPtr scene = makeTorus(1.0, 2.0, 16, 16);
        sceneManager.setRoot  (scene);

        //Create the Documentation
        SimpleScreenDoc TheSimpleScreenDoc(&sceneManager, TutorialWindow);

        // show the whole scene
        sceneManager.showAll();

        //Load Sound Definitions
        FCFileType::FCPtrStore NewContainers;
        NewContainers = FCFileHandler::the()->read(BoostPath("Data/04SoundData.xml"));

        FCFileType::FCPtrStore::iterator Itor;
        for(Itor = NewContainers.begin() ; Itor != NewContainers.end() ; ++Itor)
        {
            //Get Sounds
            if( (*Itor)->getType().isDerivedFrom(Sound::getClassType()))
            {
                Sounds.push_back(dynamic_pointer_cast<Sound>(*Itor));
                Sounds.back()->connectSoundPlayed  (boost::bind(handleSoundPlayed,   _1));
                Sounds.back()->connectSoundStopped (boost::bind(handleSoundStopped,  _1));
                Sounds.back()->connectSoundPaused  (boost::bind(handleSoundPaused,   _1));
                Sounds.back()->connectSoundUnpaused(boost::bind(handleSoundUnpaused, _1));
                Sounds.back()->connectSoundLooped  (boost::bind(handleSoundLooped,   _1));
            }
            //Get Sound Groups
            if( (*Itor)->getType().isDerivedFrom(SoundGroup::getClassType()))
            {
                SoundGroups.push_back(dynamic_pointer_cast<SoundGroup>(*Itor));
            }
        }

        //Initialize the Sound Manager
        SoundManager::the()->attachUpdateProducer(TutorialWindow);
        SoundManager::the()->setCamera(sceneManager.getCamera());

        Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f);
        Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5);
        TutorialWindow->openWindow(WinPos,
                                   WinSize,
                                   "04 XML Sound Loading Window");

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

    }
    osgExit();
    return 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));
        TutorialWindow->connectKeyTyped(boost::bind(keyTyped, _1, &sceneManager));

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

        Pnt3f PositionReturnValue;

        //Particle System
        ParticleSystemRefPtr ExampleParticleSystem = ParticleSystem::create();
        for(UInt32 i(0) ; i<500 ; ++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(1.0,1.0,1.0), 
                                               -1, 
                                               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();



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

        //Create an DistanceKill
        DistanceKillParticleAffectorRefPtr ExampleDistanceKillParticleAffector = DistanceKillParticleAffector::create();
        ExampleDistanceKillParticleAffector->setKillDistance(1000.0f);
        ExampleDistanceKillParticleAffector->setParticleSystemNode(ParticleNode);
        ExampleDistanceKillParticleAffector->setDistanceFromSource(DistanceKillParticleAffector::DISTANCE_FROM_CAMERA);
        ExampleDistanceKillParticleAffector->setDistanceFromCamera(sceneManager.getCamera());

        ExampleParticleSystem->pushToAffectors(ExampleDistanceKillParticleAffector);

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

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

    }
    osgExit();

    return 0;
}
// Initialize GLUT & OpenSG and set up the scene
int main(int argc, char **argv)
{
    // OSG init
    osgInit(argc,argv);

    {
        // Set up Window
        WindowEventProducerRecPtr TutorialWindow = createNativeWindow();

        //Initialize Window
        TutorialWindow->initWindow();

        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->connectMouseDragged(boost::bind(mouseDragged, _1, &sceneManager));
        TutorialWindow->connectMouseWheelMoved(boost::bind(mouseWheelMoved, _1, &sceneManager));

        BoostPath FilePath("../Animation/Data/Nanobot.dae");
        if(argc >= 2)
        {
            FilePath = BoostPath(argv[1]);
            if(!boost::filesystem::exists(FilePath))
            {
                std::cerr << "Could not load file: "<< FilePath.string()
                          << ", because no such files exists."<< std::endl;
                FilePath = BoostPath("../Animation/Data/Nanobot.dae");
            }
        }

        NodeRefPtr LoadedRoot;
        std::vector<AnimationRecPtr> LoadedAnimations;

        FCFileType::FCPtrStore ObjStore;
        try
        {
            ObjStore = FCFileHandler::the()->read(FilePath);
        }
        catch(std::exception &ex)
        {
            std::cerr << "Failed to load file: " << FilePath.string() << ", error:"
                     << ex.what()                        << std::endl;
            return -1;
        }
        for(FCFileType::FCPtrStore::iterator StorItor(ObjStore.begin());
            StorItor != ObjStore.end();
            ++StorItor)
        {
            //Animations
            if((*StorItor)->getType().isDerivedFrom(Animation::getClassType()))
            {
                LoadedAnimations.push_back(dynamic_pointer_cast<Animation>(*StorItor));
                LoadedAnimations.back()->attachUpdateProducer(TutorialWindow);
                LoadedAnimations.back()->start();
            }
            //Root Node
            if((*StorItor)->getType() == Node::getClassType() &&
                    dynamic_pointer_cast<Node>(*StorItor)->getParent() == NULL)
            {
                LoadedRoot = dynamic_pointer_cast<Node>(*StorItor);
            }
        }

        if(LoadedRoot == NULL)
        {
            LoadedRoot = SceneFileHandler::the()->read(FilePath.string().c_str());
        }

        if(LoadedRoot == NULL)
        {
            LoadedRoot= makeTorus(.5, 2, 32, 32);
        }

        //Make the fog node
        PostShaderStageRecPtr PostShaderStageCore = PostShaderStage::create();
        PostShaderStageCore->clearPasses();
        PostShaderStageCore->addPass("", generateNoEffectProg());


        DirectionalLightRecPtr SceneLightCore = DirectionalLight::create();
        SceneLightCore->setAmbient(Color4f(0.2f, 0.2f, 0.2f, 1.0f));
        SceneLightCore->setDiffuse(Color4f(0.8f, 0.8f, 0.8f, 1.0f));
        SceneLightCore->setSpecular(Color4f(1.0f, 1.0f, 1.0f, 1.0f));

        NodeRefPtr SceneLight = makeNodeFor(SceneLightCore);
        SceneLight->addChild(LoadedRoot);

        NodeRefPtr PostShaderStageNode = makeNodeFor(PostShaderStageCore);
        PostShaderStageNode->addChild(SceneLight);

        //Make Main Scene Node
        NodeRefPtr scene = makeCoredNode<Group>();

        scene->addChild(PostShaderStageNode);

        // tell the manager what to manage
        sceneManager.setRoot  (scene);
        SceneLightCore->setBeacon(sceneManager.getCamera()->getBeacon());

        //Create the Documentation Foreground and add it to the viewport
        SimpleScreenDoc TheSimpleScreenDoc(&sceneManager, TutorialWindow);

        // show the whole scene
        sceneManager.showAll();

        sceneManager.getWindow()->getPort(0)->setTravMask(1);
        RenderOptionsRecPtr ViewportRenderOptions = RenderOptions::create();
        ViewportRenderOptions->setRenderProperties(0x0);
        ViewportRenderOptions->setRenderProperties(RenderPropertiesPool::the()->getFrom1("Default"));
        ViewportRenderOptions->setRenderProperties(0x01);

        sceneManager.getWindow()->getPort(0)->setRenderOptions(ViewportRenderOptions);

        Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f);
        Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5);
        TutorialWindow->openWindow(WinPos,
                                   WinSize,
                                   "Collada Loader");

        TutorialWindow->connectKeyPressed(boost::bind(keyPressed, _1,
                                                      TutorialWindow.get(),
                                                      PostShaderStageCore.get()));

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

    }

    osgExit();

    return 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(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 = 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->attachUpdateProducer(TutorialWindow);

        //Particle System Drawer (Point)
        PointParticleSystemDrawerRecPtr ExamplePointParticleSystemDrawer = PointParticleSystemDrawer::create();

        //Particle System Drawer (line)
        LineParticleSystemDrawerRecPtr ExampleLineParticleSystemDrawer = 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 = RateParticleGenerator::create();

        //Attach the function objects to the Generator
        ExampleGenerator->setPositionDistribution(createPositionDistribution());
        ExampleGenerator->setLifespanDistribution(createLifespanDistribution());
        ExampleGenerator->setGenerationRate(200);

        UniformParticleAffectorRecPtr ExampleUniformAffector = UniformParticleAffector::create();
        ExampleUniformAffector->setMagnitude(20.0); // force which the field exerts on particles (negative = towards the air field's beacon location)
        NodeRefPtr UniformBeacon = 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
        ParticleSystemCoreRecPtr ParticleNodeCore = ParticleSystemCore::create();
        ParticleNodeCore->setSystem(ExampleParticleSystem);
        ParticleNodeCore->setDrawer(ExamplePointParticleSystemDrawer);
        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);

        TutorialWindow->connectKeyTyped(boost::bind(keyTyped, _1,
                                                    &sceneManager,
                                                    ParticleNodeCore.get(),
                                                    ExamplePointParticleSystemDrawer.get(),
                                                    ExampleLineParticleSystemDrawer.get(),
                                                    ExampleUniformAffector.get()));

        sceneManager.setRoot(scene);

        // Show the whole Scene
        sceneManager.showAll();

        sceneManager.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;
}
// Initialize WIN32 & OpenSG and set up the scene
int main(int argc, char **argv)
{
    std::cout << "\n\nKEY COMMANDS:" << std::endl
        << "space   Play/Pause the playing sounds" << std::endl
        << "1       Play Pop Sound" << std::endl
        << "2       Play Click Sound" << std::endl
        << "CTRL-Q  Exit\n\n" << std::endl;

    // 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));
 
        // create the scene
        NodeUnrecPtr scene = makeTorus(1.0, 2.0, 16, 16);

        //Initialize the Sound Manager
        SoundManager::the()->attachUpdateProducer(TutorialWindow);
        SoundManager::the()->setCamera(sceneManager.getCamera());

        //Create Pop Sound
        SoundRecPtr ZapSound = SoundManager::the()->createSound();
        ZapSound->setFile(BoostPath("./Data/zap.wav"));
        ZapSound->setVolume(1.0);
        ZapSound->setStreaming(false);
        ZapSound->setLooping(1);

        //Attach Sound Events
        ZapSound->connectSoundPlayed  (boost::bind(handleSoundPlayed,   _1));
        ZapSound->connectSoundStopped (boost::bind(handleSoundStopped,  _1));
        ZapSound->connectSoundPaused  (boost::bind(handleSoundPaused,   _1));
        ZapSound->connectSoundUnpaused(boost::bind(handleSoundUnpaused, _1));
        ZapSound->connectSoundLooped  (boost::bind(handleSoundLooped,   _1));

        //Create Click Sound
        SoundRecPtr ClickSound = SoundManager::the()->createSound();
        ClickSound->setFile(BoostPath("./Data/click.wav"));
        ClickSound->setVolume(1.0);
        ClickSound->setStreaming(false);
        ClickSound->setLooping(0);

        //Attach Sound Events
        ClickSound->connectSoundPlayed  (boost::bind(handleSoundPlayed,   _1));
        ClickSound->connectSoundStopped (boost::bind(handleSoundStopped,  _1));
        ClickSound->connectSoundPaused  (boost::bind(handleSoundPaused,   _1));
        ClickSound->connectSoundUnpaused(boost::bind(handleSoundUnpaused, _1));
        ClickSound->connectSoundLooped  (boost::bind(handleSoundLooped,   _1));

        TutorialWindow->connectKeyTyped(boost::bind(&KeyTypedHandler::keyTyped,
                                                    _1,
                                                    ZapSound.get(),
                                                    ClickSound.get()));

        // tell the manager what to manage
        sceneManager.setRoot  (scene);

        // show the whole scene
        sceneManager.showAll();


        Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f);
        Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5);
        TutorialWindow->openWindow(WinPos,
                                   WinSize,
                                   "01 DefaultSound Window");

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

    }
    osgExit();
    return 0;
}
int main(int argc, char **argv)
{
    preloadSharedObject("OSGImageFileIO");

    // 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->connectMouseReleased(boost::bind(mouseReleased, _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
        //point 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(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 PSPointMaterial = ChunkMaterial::create();
        PSPointMaterial->addChunk(PSPointChunk);
        PSPointMaterial->addChunk(PSMaterialChunkChunk);
        PSPointMaterial->addChunk(PSBlendChunk);

        //smoke material
        TextureObjChunkRefPtr QuadTextureObjChunk = TextureObjChunk::create();
        ImageRefPtr LoadedImage = ImageFileHandler::the()->read("Data/Smoke.png");    
        QuadTextureObjChunk->setImage(LoadedImage);

        TextureEnvChunkRefPtr QuadTextureEnvChunk = TextureEnvChunk::create();
        QuadTextureEnvChunk->setEnvMode(GL_MODULATE);

        MaterialChunkRefPtr PSMaterialChunk = MaterialChunk::create();
        PSMaterialChunk->setAmbient(Color4f(0.3f,0.3f,0.3f,1.0f));
        PSMaterialChunk->setDiffuse(Color4f(0.7f,0.7f,0.7f,1.0f));
        PSMaterialChunk->setSpecular(Color4f(0.9f,0.9f,0.9f,1.0f));
        PSMaterialChunk->setColorMaterial(GL_AMBIENT_AND_DIFFUSE);

        ChunkMaterialRefPtr PSSmokeMaterial = ChunkMaterial::create();
        PSSmokeMaterial->addChunk(QuadTextureObjChunk);
        PSSmokeMaterial->addChunk(PSMaterialChunk);
        PSSmokeMaterial->addChunk(PSBlendChunk);
        PSSmokeMaterial->addChunk(QuadTextureEnvChunk);


        //Particle System
        //Rocket
        ParticleSystemRecPtr RocketParticleSystem = ParticleSystem::create();
        RocketParticleSystem->attachUpdateProducer(TutorialWindow);

        //smoke
        ParticleSystemRecPtr SmokeParticleSystem = ParticleSystem::create();
        SmokeParticleSystem->attachUpdateProducer(TutorialWindow);
        //Shrapnel
        ParticleSystemRecPtr ShrapnelParticleSystem = ParticleSystem::create();
        ShrapnelParticleSystem->attachUpdateProducer(TutorialWindow);
        //Fireball
        ParticleSystemRecPtr FireballParticleSystem = ParticleSystem::create();
        FireballParticleSystem->attachUpdateProducer(TutorialWindow);



        //Particle System Drawer
        //Rocket does not have a drawer because it is being attached to a special node core
        //Smoke
        QuadParticleSystemDrawerRecPtr SmokeParticleSystemDrawer = QuadParticleSystemDrawer::create();
        //SmokeParticleSystemDrawer->setQuadSizeScaling(Vec2f(0.5f,0.5f));
        //Shrapnel
        PointParticleSystemDrawerRecPtr ExampleShrapnelParticleSystemDrawer = PointParticleSystemDrawer::create();
        ExampleShrapnelParticleSystemDrawer->setForcePerParticleSizing(true);
        //Fireball
        PointParticleSystemDrawerRecPtr ExampleFireballParticleSystemDrawer = PointParticleSystemDrawer::create();
        ExampleFireballParticleSystemDrawer->setForcePerParticleSizing(true);

        //Particle System Node
        //collision node
        //NodeRefPtr EnvironmentNode = makeSphere(2,4.0f);

        Matrix EnvironmentTransformation;
        EnvironmentTransformation.setTranslate(0.0f,0.0f,10.0f);

        TransformRefPtr EnvironmentTransformCore = Transform::create();
        EnvironmentTransformCore->setMatrix(EnvironmentTransformation);

        NodeRefPtr EnvironmentNode = Node::create();
        EnvironmentNode->setCore(EnvironmentTransformCore);
        NodeRefPtr EnvironmentGeoNode = SceneFileHandler::the()->read("Data/house.obj");
        if(EnvironmentGeoNode == NULL)
        {
            EnvironmentGeoNode = makeTorus(.5, 2, 16, 16);
        }
        EnvironmentNode->addChild(EnvironmentGeoNode);

        NodeRefPtr RocketParticlePrototypeNode = SceneFileHandler::the()->read("Data/rocket.obj");
        if(RocketParticlePrototypeNode == NULL)
        {
            RocketParticlePrototypeNode = makeTorus(.2, 0.8, 16, 16);
        }

        NodeParticleSystemCoreRefPtr RocketParticleNodeCore = NodeParticleSystemCore::create();
        RocketParticleNodeCore->setSystem(RocketParticleSystem);
        RocketParticleNodeCore->setPrototypeNode(RocketParticlePrototypeNode);
        RocketParticleNodeCore->setNormalSource(NodeParticleSystemCore::NORMAL_VELOCITY);
        RocketParticleNodeCore->setUpSource(NodeParticleSystemCore::UP_PARTICLE_NORMAL);
        RocketParticleNodeCore->setUp(Vec3f(0.0f,1.0f,0.0f));

        //Geometry Collision Affector
        GeometryCollisionParticleSystemAffectorRefPtr ExampleGeometryCollisionParticleSystemAffector = GeometryCollisionParticleSystemAffector::create();
        ExampleGeometryCollisionParticleSystemAffector->setCollisionNode(EnvironmentNode);

        ExampleGeometryCollisionParticleSystemAffector->connectParticleCollision(boost::bind(particleCollision, _1));

        NodeRefPtr RocketParticleNode = Node::create();
        RocketParticleNode->setCore(RocketParticleNodeCore);

        //Attach the Affector to the Rocket Particle System
        //RocketParticleSystem->pushToAffectors();
        RocketParticleSystem->pushToSystemAffectors(ExampleGeometryCollisionParticleSystemAffector);


        //Smoke
        RateParticleGeneratorRecPtr SmokeGenerator = RateParticleGenerator::create();
        //Attach the function objects to the Generator
        Distribution3DRefPtr SmokePositionDistribution = createSmokePositionDistribution();
        SmokeGenerator->setPositionDistribution(SmokePositionDistribution);
        SmokeGenerator->setLifespanDistribution(createSmokeLifespanDistribution());
        SmokeGenerator->setGenerationRate(50.0);
        SmokeGenerator->setVelocityDistribution(createSmokeVelocityDistribution());
        //Attach the function objects the Affectors
        AgeFadeParticleAffectorRecPtr SmokeAgeFadeParticleAffector = AgeFadeParticleAffector::create();
        SmokeAgeFadeParticleAffector->setFadeInTime(2.0f);
        SmokeAgeFadeParticleAffector->setFadeOutTime(5.0f);
        SmokeAgeFadeParticleAffector->setStartAlpha(0.0f);
        SmokeAgeFadeParticleAffector->setFadeToAlpha(0.2f);
        SmokeAgeFadeParticleAffector->setEndAlpha(0.0f);    

        AgeSizeParticleAffectorRecPtr SmokeAgeSizeParticleAffector = AgeSizeParticleAffector::create();
        //ages
        SmokeAgeSizeParticleAffector->editMFAges()->push_back(0.1);
        SmokeAgeSizeParticleAffector->editMFAges()->push_back(0.2);
        SmokeAgeSizeParticleAffector->editMFAges()->push_back(0.3);
        SmokeAgeSizeParticleAffector->editMFAges()->push_back(0.5);
        SmokeAgeSizeParticleAffector->editMFAges()->push_back(0.7);
        SmokeAgeSizeParticleAffector->editMFAges()->push_back(0.8);
        SmokeAgeSizeParticleAffector->editMFAges()->push_back(1.0);

        //sizes
        SmokeAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(0.5,0.5,0.5));
        SmokeAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(1.0,1.0,1.0));
        SmokeAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(2.0,2.0,2.0));
        SmokeAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(3.0,3.0,3.0));
        SmokeAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(4.0,4.0,4.0));
        SmokeAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(5.0,5.0,5.0));
        SmokeAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(6.5,6.5,6.5));

        ParticleSystemCoreRefPtr SmokeParticleNodeCore = ParticleSystemCore::create();
        SmokeParticleNodeCore->setSystem(SmokeParticleSystem);
        SmokeParticleNodeCore->setDrawer(SmokeParticleSystemDrawer);
        SmokeParticleNodeCore->setMaterial(PSSmokeMaterial);

        NodeRefPtr SmokeParticleNode = Node::create();
        SmokeParticleNode->setCore(SmokeParticleNodeCore);
        //end/////////////////////

        //Shrapnel
        BurstParticleGeneratorRecPtr ShrapnelBurstGenerator = BurstParticleGenerator::create();
        NodeRefPtr ShrapnelParticlePrototypeNode = SceneFileHandler::the()->read("Data/Shrapnel.obj");

        NodeParticleSystemCoreRefPtr ShrapnelParticleNodeCore = NodeParticleSystemCore::create();
        ShrapnelParticleNodeCore->setSystem(ShrapnelParticleSystem);
        ShrapnelParticleNodeCore->setPrototypeNode(ShrapnelParticlePrototypeNode);

        //Attach the function objects to the Generator
        Distribution3DRefPtr ShrapnelPositionDistribution = createShrapnelPositionDistribution();
        ShrapnelBurstGenerator->setPositionDistribution(ShrapnelPositionDistribution);
        ShrapnelBurstGenerator->setLifespanDistribution(createLifespanDistribution());
        ShrapnelBurstGenerator->setBurstAmount(50.0);
        ShrapnelBurstGenerator->setVelocityDistribution(createShrapnelVelocityDistribution());
        ShrapnelBurstGenerator->setAccelerationDistribution(createShrapnelAccelerationDistribution());

        NodeRefPtr ShrapnelParticleNode = Node::create();
        ShrapnelParticleNode->setCore(ShrapnelParticleNodeCore);
        //end/////////////////////

        //fireball
        BurstParticleGeneratorRecPtr FireballGenerator = BurstParticleGenerator::create();
        NodeRefPtr FireballParticlePrototypeNode = SceneFileHandler::the()->read("Data/bubble.obj");

        NodeParticleSystemCoreRefPtr FireballParticleNodeCore = NodeParticleSystemCore::create();
        FireballParticleNodeCore->setSystem(FireballParticleSystem);
        FireballParticleNodeCore->setPrototypeNode(FireballParticlePrototypeNode);
        //Attach the function objects to the Generator
        Distribution3DRefPtr FireballPositionDistribution = createFireballPositionDistribution();
        FireballGenerator->setPositionDistribution(FireballPositionDistribution);
        FireballGenerator->setLifespanDistribution(createFireballLifespanDistribution());
        FireballGenerator->setBurstAmount(100.0);
        FireballGenerator->setVelocityDistribution(createFireballVelocityDistribution());
        FireballGenerator->setAccelerationDistribution(createFireballAccelerationDistribution());
        //Attach the function objects the Affectors
        AgeSizeParticleAffectorRecPtr FireballAgeSizeParticleAffector = AgeSizeParticleAffector::create();
        //ages
        FireballAgeSizeParticleAffector->editMFAges()->push_back(0.1);
        FireballAgeSizeParticleAffector->editMFAges()->push_back(0.2);
        FireballAgeSizeParticleAffector->editMFAges()->push_back(0.3);
        FireballAgeSizeParticleAffector->editMFAges()->push_back(0.5);
        FireballAgeSizeParticleAffector->editMFAges()->push_back(0.7);
        FireballAgeSizeParticleAffector->editMFAges()->push_back(0.8);
        FireballAgeSizeParticleAffector->editMFAges()->push_back(1.0);

        //sizes
        FireballAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(2.0,2.0,2.0));
        FireballAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(2.3,2.3,2.3));
        FireballAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(2.5,2.5,2.5));
        FireballAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(3.0,3.0,3.0));
        FireballAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(4.0,4.0,4.0));
        FireballAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(5.0,5.0,5.0));
        FireballAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(6.5,6.5,6.5));

        NodeRefPtr FireballParticleNode = Node::create();
        FireballParticleNode->setCore(FireballParticleNodeCore);
        //end/////////////////////

        //Attach the Affector to the Smoke Particle System
        SmokeParticleSystem->pushToAffectors(SmokeAgeFadeParticleAffector);
        SmokeParticleSystem->pushToAffectors(SmokeAgeSizeParticleAffector);

        //Attach the Affector to the fireball Particle System
        FireballParticleSystem->pushToAffectors(FireballAgeSizeParticleAffector);

        // Make Main Scene Node 
        NodeRefPtr scene = Node::create();
        scene->setCore(Group::create());
        scene->addChild(RocketParticleNode);
        scene->addChild(SmokeParticleNode);
        scene->addChild(ShrapnelParticleNode);
        scene->addChild(FireballParticleNode);
        scene->addChild(EnvironmentNode);

        RocketParticleSystem->connectParticleKilled(boost::bind(particleKilled, _1,
                                                                ShrapnelParticleSystem.get(),
                                                                ShrapnelBurstGenerator.get(),
                                                                SmokeParticleSystem.get(),
                                                                SmokeGenerator.get(),
                                                                FireballParticleSystem.get(),
                                                                FireballGenerator.get()));

        TutorialWindow->connectMousePressed(boost::bind(mousePressed, _1,
                                                        &sceneManager,
                                                        RocketParticleSystem.get()));

        sceneManager.setRoot(scene);

        //Create the Documentation
        SimpleScreenDoc TheSimpleScreenDoc(&sceneManager, TutorialWindow);

        sceneManager.getNavigator()->set(Pnt3f(0.0,0.0,-10.0), Pnt3f(0.0,0.0,0.0), Vec3f(0.0,1.0,0.0));
        sceneManager.getNavigator()->setMotionFactor(1.0f);
        sceneManager.getCamera()->setNear(0.1f);
        sceneManager.getCamera()->setFar(1000.0f);


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

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

    }
    osgExit();

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


        // Material blend chunk
        BlendChunkRefPtr PSBlendChunk = BlendChunk::create();
        PSBlendChunk->setSrcFactor(GL_SRC_ALPHA);
        PSBlendChunk->setDestFactor(GL_ONE_MINUS_SRC_ALPHA);

        //load up images for PS drawer
        ImageRefPtr rocket = ImageFileHandler::the()->read("Data/rocket.png");
        ImageRefPtr smoke = ImageFileHandler::the()->read("Data/Smokey.png");

        //Texture Chunk
        TextureObjChunkRefPtr PSRocketTexChunk = TextureObjChunk::create();
        PSRocketTexChunk->setImage(rocket);

        TextureEnvChunkRefPtr PSRocketTexEnvChunk = TextureEnvChunk::create();
        PSRocketTexEnvChunk->setEnvMode(GL_MODULATE);

        TextureObjChunkRefPtr SmokeTexChunk = TextureObjChunk::create();
        SmokeTexChunk->setImage(smoke);

        TextureEnvChunkRefPtr SmokeTexEnvChunk = TextureEnvChunk::create();
        SmokeTexEnvChunk->setEnvMode(GL_MODULATE);

        //Particle System Material
        MaterialChunkRefPtr PSMaterialChunkChunk = MaterialChunk::create();
        PSMaterialChunkChunk->setAmbient(Color4f(1.0f,0.5f,0.3f,1.0f));
        PSMaterialChunkChunk->setDiffuse(Color4f(1.0f,0.5f,0.3f,0.6f));
        PSMaterialChunkChunk->setSpecular(Color4f(1.0f,0.5f,0.3f,0.6f));
        PSMaterialChunkChunk->setColorMaterial(GL_AMBIENT_AND_DIFFUSE);

        // Assembling materials
        ChunkMaterialRefPtr PSMaterial = ChunkMaterial::create();
        PSMaterial->addChunk(PSMaterialChunkChunk);
        PSMaterial->addChunk(PSBlendChunk);
        PSMaterial->addChunk(PSRocketTexChunk);

        ChunkMaterialRefPtr TrailMaterial = ChunkMaterial::create();
        TrailMaterial->addChunk(PSMaterialChunkChunk);
        TrailMaterial->addChunk(PSBlendChunk);
        TrailMaterial->addChunk(SmokeTexChunk);

        AgeFadeParticleAffectorRefPtr AgeFadeAffector = AgeFadeParticleAffector::create();
        AgeFadeAffector->setFadeInTime(0.0f);
        AgeFadeAffector->setStartAlpha(1.0f);
        AgeFadeAffector->setEndAlpha(0.0f);
        AgeFadeAffector->setFadeOutTime(0.35f);
        AgeFadeAffector->setFadeToAlpha(1.0f);

        // Creating a particle generator
        RateParticleGeneratorRefPtr ExampleGenerator = RateParticleGenerator::create();
        //Attach the function objects to the Generator
        ExampleGenerator->setPositionDistribution(createPositionDistribution());
        ExampleGenerator->setGenerationRate(3.0);
        ExampleGenerator->setVelocityDistribution(createVelocityDistribution());
        ExampleGenerator->setNormalDistribution(createNormalDistribution());
        ExampleGenerator->setLifespanDistribution(createLifespanDistribution());
        ExampleGenerator->setSizeDistribution(createSizeDistribution());


        //Creating Particle System
        ParticleSystemRecPtr ExampleParticleSystem = ParticleSystem::create();
        ExampleParticleSystem->addParticle(Pnt3f(0,0,-100),Vec3f(0,1,0),Color4f(1,1,1,1),Vec3f(1,1,1),0.1,Vec3f(0,0,0),Vec3f(0,0,0));
        ExampleParticleSystem->addParticle(Pnt3f(0,0,100),Vec3f(0,1,0),Color4f(1,1,1,1),Vec3f(1,1,1),0.1,Vec3f(0,0,0),Vec3f(0,0,0));
        ExampleParticleSystem->setMaxParticles(5); // 5 rockets max to avoid collisions.  they are bad.
        ExampleParticleSystem->pushToAffectors(AgeFadeAffector);
        ExampleParticleSystem->attachUpdateProducer(TutorialWindow);

        //Creating Particle System Drawer
        QuadParticleSystemDrawerRefPtr ExampleParticleSystemDrawer = QuadParticleSystemDrawer::create();
        ExampleParticleSystemDrawer->setNormalAndUpSource(QuadParticleSystemDrawer::NORMAL_VIEW_DIRECTION,
                QuadParticleSystemDrawer::UP_VELOCITY);

        QuadParticleSystemDrawerRefPtr ExampleTrailDrawer = QuadParticleSystemDrawer::create();
        ExampleTrailDrawer->setNormalAndUpSource(QuadParticleSystemDrawer::NORMAL_VIEW_DIRECTION,
                QuadParticleSystemDrawer::UP_PARTICLE_NORMAL);

        // Attaching affector and generator to the particle system
        ExampleParticleSystem->pushToGenerators(ExampleGenerator);

        //Particle System Core, setting its system, drawer, and material
        ParticleSystemCoreRefPtr ParticleNodeCore = ParticleSystemCore::create();
        ParticleNodeCore->setSystem(ExampleParticleSystem);
        ParticleNodeCore->setDrawer(ExampleParticleSystemDrawer);
        ParticleNodeCore->setMaterial(PSMaterial);

        // create Particle System Particle Trail generator
        ParticleSystemParticleTrailGeneratorRecPtr ExamplePSTrailGenerator = ParticleSystemParticleTrailGenerator::create();
        ExamplePSTrailGenerator->setTrailResolution(0.05f);
        ExamplePSTrailGenerator->setTrailLength(1.2);
        ExamplePSTrailGenerator->setTrailLengthMethod(ParticleTrailGenerator::TIME);
        ExamplePSTrailGenerator->setTrailResolutionMethod(ParticleTrailGenerator::TIME_SPACING);
        ExamplePSTrailGenerator->setTrailMaterial(TrailMaterial);
        ExamplePSTrailGenerator->setTrailDrawer(ExampleTrailDrawer);
        ExamplePSTrailGenerator->setSizeDistribution(createTrailSizeDistribution());
        ExamplePSTrailGenerator->setColorDistribution(createColorDistribution());
        ExamplePSTrailGenerator->setNormalDistribution(createNormalDistribution());
        ExamplePSTrailGenerator->setVelocityDistribution(createNormalDistribution());

        // create affectors for particle trails
        GravityParticleAffectorRefPtr GravAffector = GravityParticleAffector::create();
        GravAffector->setBeacon(ExamplePSTrailGenerator);

        AgeFadeParticleAffectorRefPtr TrailAgeFadeAffector = AgeFadeParticleAffector::create();
        TrailAgeFadeAffector->setFadeInTime(0.2f);
        TrailAgeFadeAffector->setStartAlpha(0.0f);
        TrailAgeFadeAffector->setEndAlpha(0.0f);
        TrailAgeFadeAffector->setFadeOutTime(1.0f);
        TrailAgeFadeAffector->setFadeToAlpha(0.6f);

        // now we attach the affector to the particle trail generator's particle system
        ExamplePSTrailGenerator->getParticleSystem()->pushToAffectors(TrailAgeFadeAffector);


        // attach listener for trail generator to the particle system
        ExamplePSTrailGenerator->setSystemToTrail(ExampleParticleSystem);

        //Attach the the update producer to the particle system particle trail generator.
        ExamplePSTrailGenerator->attachUpdateProducer(TutorialWindow);

        // Set up node with the particle system at its core
        NodeRefPtr ParticleNode = Node::create();
        ParticleNode->setCore(ParticleNodeCore);
        ParticleNode->addChild(ExamplePSTrailGenerator);

        // Make Main Scene Node
        NodeRefPtr scene = Node::create();
        scene->setCore(Group::create());
        scene->addChild(ParticleNode);

        sceneManager.setRoot(scene);

        // Show the whole Scene
        sceneManager.showAll();
        sceneManager.getCamera()->setFar(10000.0f);
        sceneManager.getCamera()->setNear(0.1f);
        sceneManager.setStatistics(false);

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

        std::cout << "Controls: " << std::endl
                  << "P: Increase Trail Resolution" << std::endl
                  << "L: Decrease Trail Resolution" << std::endl
                  << "O: Increase Trail Length" << std::endl
                  << "K: Decrease Trail Length" << std::endl
                  << "J: Toggle calculating trail length by num points/time" << std::endl
                  << "Y: Toggle calculating trail point spacing by time/distance" << std::endl
                  << "B: Particle burst" << std::endl;

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

    osgExit();

    return 0;
}
Example #8
0
// Initialize WIN32 & OpenSG and set up the scene
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->connectMouseDragged(boost::bind(mouseDragged, _1, &sceneManager));
        TutorialWindow->connectMouseWheelMoved(boost::bind(mouseWheelMoved, _1, &sceneManager));

        //Sound Emitter Node
        SoundEmitterRecPtr TheEmitter = SoundEmitter::create();
        TheEmitter->attachUpdateProducer(TutorialWindow);

        NodeUnrecPtr TheEmitterNode = Node::create();
        TheEmitterNode->setCore(TheEmitter);

        //Sphere Transformation Node
        Matrix Translate;
        Translate.setTranslate(0.0,0.0,-5.0);
        Matrix Rotation;
        Rotation.setRotate(Quaternion(Vec3f(0.0,1.0,0.0), 0.0));

        Matrix Total(Translate);
        Total.mult(Rotation);

        TransformRecPtr TheSphereTransform = Transform::create();
        TheSphereTransform->setMatrix(Total);

        NodeUnrecPtr SphereTransformNode = Node::create();
        SphereTransformNode->setCore(TheSphereTransform);
        SphereTransformNode->addChild(makeSphere(2, 1.0));
        SphereTransformNode->addChild(TheEmitterNode);

        // create the scene
        NodeUnrecPtr scene = Node::create();
        scene->setCore(Group::create());
        scene->addChild(SphereTransformNode);

        // tell the manager what to manage
        sceneManager.setRoot  (scene);

        CameraUnrecPtr TheCamera = sceneManager.getCamera();
        TheCamera->setNear(0.1);
        TheCamera->setFar(100.0);

        //Initialize the Sound Manager
        SoundManager::the()->attachUpdateProducer(TutorialWindow);
        SoundManager::the()->setCamera(sceneManager.getCamera());

        SoundRecPtr PopSound = SoundManager::the()->createSound();
        PopSound->setFile(BoostPath("./Data/pop.wav"));
        PopSound->setVolume(1.0);
        PopSound->setStreaming(false);
        PopSound->setLooping(-1);
        PopSound->setEnable3D(true);

        PopSound->connectSoundPlayed  (boost::bind(handleSoundPlayed,   _1));
        PopSound->connectSoundStopped (boost::bind(handleSoundStopped,  _1));
        PopSound->connectSoundPaused  (boost::bind(handleSoundPaused,   _1));
        PopSound->connectSoundUnpaused(boost::bind(handleSoundUnpaused, _1));
        PopSound->connectSoundLooped  (boost::bind(handleSoundLooped,   _1));

        //Attach this sound to the emitter node
        TheEmitter->setSound(PopSound);

        TutorialWindow->connectKeyTyped(boost::bind(keyTyped, _1,
                                                    TheEmitter.get()));
        TutorialWindow->connectUpdate(boost::bind(handleUpdate, _1,
                                                  TheSphereTransform.get()));

        //Create the Documentation
        SimpleScreenDoc TheSimpleScreenDoc(&sceneManager, TutorialWindow);

        Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f);
        Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5);
        TutorialWindow->openWindow(WinPos,
                                   WinSize,
                                   "02 Sound3D Window");


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

    }
    osgExit();
    return 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->connectMouseDragged(boost::bind(mouseDragged, _1, &sceneManager));
        TutorialWindow->connectMouseWheelMoved(boost::bind(mouseWheelMoved, _1, &sceneManager));

        //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 = ParticleSystem::create();
        ExampleParticleSystem->addParticle(Pnt3f(0,-100,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,100,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->attachUpdateProducer(TutorialWindow);

        //Particle System Drawer (Point)
        PointParticleSystemDrawerRecPtr ExamplePointParticleSystemDrawer = PointParticleSystemDrawer::create();

        //Particle System Drawer (line)
        LineParticleSystemDrawerRecPtr ExampleLineParticleSystemDrawer = 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 = RateParticleGenerator::create();

        //Attach the function objects to the Generator
        ExampleGenerator->setPositionDistribution(createPositionDistribution());
        ExampleGenerator->setLifespanDistribution(createLifespanDistribution());
        ExampleGenerator->setGenerationRate(60.0);
        ExampleGenerator->setVelocityDistribution(createVelocityDistribution());

        VortexParticleAffectorRecPtr ExampleVortexAffector = VortexParticleAffector::create();
        ExampleVortexAffector->setMagnitude(20.0); 
        ExampleVortexAffector->setVortexAxis(Vec3f(0.0,0.0,1.0)); // field rotates around z axis
        NodeRefPtr VortexBeacon = Node::create();
        ExampleVortexAffector->setBeacon(VortexBeacon); // set to 'emulate' from (0,0,0)
        ExampleVortexAffector->setMaxDistance(-1.0); // particles affected regardless of distance
        ExampleVortexAffector->setAttenuation(0.25); // strength of uniform field dimishes by dist^attenuation


        //Attach the Generator and Affector to the Particle System
        ExampleParticleSystem->pushToGenerators(ExampleGenerator);
        ExampleParticleSystem->pushToAffectors(ExampleVortexAffector);
        ExampleParticleSystem->setMaxParticles(800);


        //Particle System Node
        ParticleSystemCoreRecPtr ParticleNodeCore = ParticleSystemCore::create();
        ParticleNodeCore->setSystem(ExampleParticleSystem);
        ParticleNodeCore->setDrawer(ExamplePointParticleSystemDrawer);
        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);

        TutorialWindow->connectKeyTyped(boost::bind(keyTyped, _1,
                                                    &sceneManager,
                                                    ParticleNodeCore.get(),
                                                    ExamplePointParticleSystemDrawer.get(),
                                                    ExampleLineParticleSystemDrawer.get(),
                                                    ExampleVortexAffector.get()));

        sceneManager.setRoot(scene);

        //Create the Documentation
        SimpleScreenDoc TheSimpleScreenDoc(&sceneManager, TutorialWindow);

        // Show the whole Scene
        sceneManager.showAll();

        sceneManager.getCamera()->setFar(1000.0);

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

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

    }
    osgExit();

    return 0;
}
Example #10
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;
}
// Initialize GLUT & OpenSG and set up the rootNode
int main(int argc, char **argv)
{
    // OSG init
    osgInit(argc,argv);

    {
        // Set up Window
        WindowEventProducerRecPtr TutorialWindow = createNativeWindow();
        TutorialWindow->initWindow();

        SimpleSceneManager sceneManager;
        TutorialWindow->setDisplayCallback(boost::bind(display, &sceneManager));
        TutorialWindow->setReshapeCallback(boost::bind(reshape, _1, &sceneManager));

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

        // Tell the Manager what to manage
        sceneManager.setWindow(TutorialWindow);

        //Make Base Geometry Node
        NodeRecPtr TriGeometryBase = makeTorus(0.5, 1.0, 24, 24);

        //Make Main Scene Node
        NodeRecPtr scene = makeCoredNode<Group>();
        setName(scene, "scene");
        NodeRecPtr rootNode = Node::create();
        setName(rootNode, "rootNode");
        ComponentTransformRecPtr Trans;
        Trans = ComponentTransform::create();
        rootNode->setCore(Trans);

        // add the torus as a child
        rootNode->addChild(scene);

        //Make The Physics Characteristics Node
        PhysicsCharacteristicsDrawableRecPtr PhysDrawable = PhysicsCharacteristicsDrawable::create();
        PhysDrawable->setRoot(rootNode);

        NodeRecPtr PhysDrawableNode = Node::create();
        PhysDrawableNode->setCore(PhysDrawable);
        PhysDrawableNode->setTravMask(TypeTraits<UInt32>::getMin());

        rootNode->addChild(PhysDrawableNode);


        //Setup Physics Scene
        PhysicsWorldRecPtr physicsWorld = PhysicsWorld::create();
        physicsWorld->setWorldContactSurfaceLayer(0.005);
        physicsWorld->setAutoDisableFlag(1);
        physicsWorld->setAutoDisableTime(0.75);
        physicsWorld->setWorldContactMaxCorrectingVel(100.0);
        physicsWorld->setGravity(Vec3f(0.0, 0.0, -9.81));

        PhysicsHashSpaceRecPtr physicsSpace = PhysicsHashSpace::create();

        PhysicsHandlerRecPtr physHandler = PhysicsHandler::create();
        physHandler->setWorld(physicsWorld);
        physHandler->pushToSpaces(physicsSpace);
        physHandler->setUpdateNode(rootNode);

        physHandler->attachUpdateProducer(TutorialWindow);


        /************************************************************************/
        /* create spaces, geoms and bodys                                       */
        /************************************************************************/
        //create a group for our space
        GroupRecPtr spaceGroup;
        NodeRecPtr spaceGroupNode = makeCoredNode<Group>(&spaceGroup);
        //create the ground plane
        GeometryRecPtr plane;
        NodeRecPtr planeNode = makeBox(30.0, 30.0, 1.0, 1, 1, 1);
        plane = dynamic_cast<Geometry*>(planeNode->getCore());
        //and its Material
        SimpleMaterialRecPtr plane_mat = SimpleMaterial::create();
        plane_mat->setAmbient(Color3f(0.7,0.7,0.7));
        plane_mat->setDiffuse(Color3f(0.9,0.6,1.0));

        plane->setMaterial(plane_mat);



        //create Physical Attachments
        PhysicsBoxGeomRecPtr planeGeom = PhysicsBoxGeom::create();
        planeGeom->setLengths(Vec3f(30.0, 30.0, 1.0));
        //add geoms to space for collision
        planeGeom->setSpace(physicsSpace);


        //add Attachments to nodes...
        spaceGroupNode->addAttachment(physicsSpace);
        spaceGroupNode->addAttachment(physHandler);    
        spaceGroupNode->addAttachment(physicsWorld);
        spaceGroupNode->addChild(planeNode);


        planeNode->addAttachment(planeGeom);


        scene->addChild(spaceGroupNode);


        //Create Statistics Foreground
        SimpleStatisticsForegroundRecPtr PhysicsStatForeground = SimpleStatisticsForeground::create();
        PhysicsStatForeground->setSize(25);
        PhysicsStatForeground->setColor(Color4f(0,1,0,0.7));
        PhysicsStatForeground->addElement(WindowEventProducer::statWindowLoopTime, "Draw FPS: %r.3f");
        PhysicsStatForeground->getCollector()->getElem(WindowEventProducer::statWindowLoopTime, true);
        PhysicsStatForeground->addElement(RenderAction::statNGeometries, 
                                         "%d Nodes drawn");
        PhysicsStatForeground->getCollector()->getElem(RenderAction::statNGeometries, true);
        PhysicsStatForeground->addElement(PhysicsHandler::statPhysicsTime, 
                                          "Physics time: %.3f s");
        PhysicsStatForeground->getCollector()->getElem(PhysicsHandler::statPhysicsTime, true);
        PhysicsStatForeground->addElement(PhysicsHandler::statCollisionTime, 
                                          "Collision time: %.3f s");
        PhysicsStatForeground->getCollector()->getElem(PhysicsHandler::statCollisionTime, true);
        PhysicsStatForeground->addElement(PhysicsHandler::statSimulationTime, 
                                          "Simulation time: %.3f s");
        PhysicsStatForeground->getCollector()->getElem(PhysicsHandler::statSimulationTime, true);
        PhysicsStatForeground->addElement(PhysicsHandler::statNCollisions, 
                                          "%d collisions");
        PhysicsStatForeground->getCollector()->getElem(PhysicsHandler::statNCollisions, true);
        PhysicsStatForeground->addElement(PhysicsHandler::statNCollisionTests, 
                                          "%d collision tests");
        PhysicsStatForeground->getCollector()->getElem(PhysicsHandler::statNCollisionTests, true);
        PhysicsStatForeground->addElement(PhysicsHandler::statNPhysicsSteps, 
                                          "%d simulation steps per frame");
        PhysicsStatForeground->getCollector()->getElem(PhysicsHandler::statNPhysicsSteps, true);
        TutorialWindow->connectUpdate(boost::bind(handleStatisticsReset, _1), boost::signals2::at_front);
        StatCollector::setGlobalCollector(PhysicsStatForeground->getCollector());


        SimpleStatisticsForegroundRecPtr RenderStatForeground = SimpleStatisticsForeground::create();
        RenderStatForeground->setSize(25);
        RenderStatForeground->setColor(Color4f(0,1,0,0.7));





        // tell the manager what to manage
        sceneManager.setRoot  (rootNode);

        sceneManager.getWindow()->getPort(0)->addForeground(PhysicsStatForeground);
        sceneManager.getWindow()->getPort(0)->addForeground(RenderStatForeground);

        
        TutorialWindow->connectKeyPressed(boost::bind(keyPressed, _1,
            TriGeometryBase.get(),
            spaceGroupNode.get(),
            PhysDrawableNode.get(),
            physicsWorld.get(),
            physicsSpace.get()));

        // show the whole rootNode
        sceneManager.getNavigator()->set(Pnt3f(20.0,20.0,10.0), Pnt3f(0.0,0.0,0.0), Vec3f(0.0,0.0,1.0));
        sceneManager.getNavigator()->setMotionFactor(1.0f);
        sceneManager.getCamera()->setFar(10000.0f);
        sceneManager.getCamera()->setNear(0.1f);

        Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f);
        Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5);
        TutorialWindow->openWindow(WinPos,
                                   WinSize,
                                   "01SimplePhysics");

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

    osgExit();

    return 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->connectMouseDragged(boost::bind(mouseDragged, _1, &sceneManager));
        TutorialWindow->connectMouseWheelMoved(boost::bind(mouseWheelMoved, _1, &sceneManager));

        //Particle System Material
        PointChunkRefPtr PSPointChunk = PointChunk::create();
        PSPointChunk->setSize(10.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->setEmission(Color4f(0.2f,0.6f,0.5f,0.3f));
        PSMaterialChunkChunk->setColorMaterial(GL_NONE);

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

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

        //Particle System
        ParticleSystemRefPtr ExampleParticleSystem = ParticleSystem::create();
        ExampleParticleSystem->attachUpdateProducer(TutorialWindow);

        //Particle System Drawer
        //Point
        PointParticleSystemDrawerRecPtr ExamplePointParticleSystemDrawer = PointParticleSystemDrawer::create();
        //ExamplePointParticleSystemDrawer->setForcePerParticleSizing(true);

        //Line
        LineParticleSystemDrawerRecPtr ExampleLineParticleSystemDrawer = LineParticleSystemDrawer::create();
        ExampleLineParticleSystemDrawer->setLineDirectionSource(LineParticleSystemDrawer::DIRECTION_NORMAL);//DIRECTION_VELOCITY_CHANGE);
        ExampleLineParticleSystemDrawer->setLineLengthSource(LineParticleSystemDrawer::LENGTH_SIZE_X);
        ExampleLineParticleSystemDrawer->setEndPointFading(Vec2f(1.0,0.0));
        //Quad
        QuadParticleSystemDrawerRecPtr ExampleQuadParticleSystemDrawer = QuadParticleSystemDrawer::create();
        ExampleQuadParticleSystemDrawer->setQuadSizeScaling(Vec2f(0.1,0.1));
        ExampleQuadParticleSystemDrawer->setNormalAndUpSource(QuadParticleSystemDrawer::NORMAL_PARTICLE_NORMAL,QuadParticleSystemDrawer::UP_STATIC);

        RateParticleGeneratorRefPtr ExampleGeneratorTheSequel = RateParticleGenerator::create();

        //Attach the function objects to the Generator
        ExampleGeneratorTheSequel->setPositionDistribution(createPositionDistribution());
        ExampleGeneratorTheSequel->setLifespanDistribution(createLifespanDistribution());
        ExampleGeneratorTheSequel->setGenerationRate(300.0);
        ExampleGeneratorTheSequel->setVelocityDistribution(createVelocityDistribution());


        //Attach the Generator to the Particle System
        //ExampleParticleSystem->pushToGenerators(ExampleGenerator);
        ExampleParticleSystem->setMaxParticles(500);
        ExampleParticleSystem->pushToGenerators(ExampleGeneratorTheSequel);

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

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

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

        //Create the Documentation
        SimpleScreenDoc TheSimpleScreenDoc(&sceneManager, TutorialWindow);

        // Show the whole Scene
        sceneManager.showAll();

        sceneManager.getCamera()->setFar(500.0);

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

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

    }
    osgExit();

    return 0;
}