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;
}
// Initialize WIN32 & OpenSG and set up the scene
int main(int argc, char **argv)
{
    // OSG init
    osgInit(argc,argv);

    {
        // create the scene
        NodeRecPtr scene = makeTorus(.5, 2, 16, 16);

        //Create a Window object
        WindowEventProducerRefPtr TheWindow = createNativeWindow();

        //Apply window settings
        TheWindow->setUseCallbackForDraw(true);
        TheWindow->setUseCallbackForReshape(true);
        //TheWindow->setFullscreen(true);
        
        
        
        //Initialize Window
        TheWindow->initWindow();

        commitChanges();
        
        // create the SimpleSceneManager helper
        SimpleSceneManager sceneManager;
        TheWindow->setDisplayCallback(boost::bind(display, &sceneManager));
        TheWindow->setReshapeCallback(boost::bind(reshape, _1, &sceneManager));

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

        //Attach to the Mouse Events
        TheWindow->connectMouseClicked(boost::bind(mouseClicked, _1));
        TheWindow->connectMousePressed(boost::bind(mousePressed, _1, &sceneManager));
        TheWindow->connectMouseReleased(boost::bind(mouseReleased, _1, &sceneManager));
        TheWindow->connectMouseMoved(boost::bind(mouseMoved, _1, &sceneManager));
        TheWindow->connectMouseDragged(boost::bind(mouseDragged, _1, &sceneManager));
        TheWindow->connectMouseEntered(boost::bind(mouseEntered, _1));
        TheWindow->connectMouseExited(boost::bind(mouseExited, _1));
        
        TheWindow->connectMouseWheelMoved(boost::bind(mouseWheelMoved, _1, &sceneManager));

        //Attach to the Key Events
        TheWindow->connectKeyPressed(boost::bind(keyPressed, _1));
        TheWindow->connectKeyReleased(boost::bind(keyReleased, _1));
        TheWindow->connectKeyTyped(boost::bind(keyTyped, _1));

        //Attach to the Window Events
        TheWindow->connectWindowOpened(boost::bind(windowOpened, _1));
        TheWindow->connectWindowClosing(boost::bind(windowClosing, _1));
        TheWindow->connectWindowClosed(boost::bind(windowClosed, _1));
        TheWindow->connectWindowIconified(boost::bind(windowIconified, _1));
        TheWindow->connectWindowDeiconified(boost::bind(windowDeiconified, _1));
        TheWindow->connectWindowActivated(boost::bind(windowActivated, _1));
        TheWindow->connectWindowDeactivated(boost::bind(windowDeactivated, _1));
        TheWindow->connectWindowEntered(boost::bind(windowEntered, _1));
        TheWindow->connectWindowExited(boost::bind(windowExited, _1));

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

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

        // show statistics
        sceneManager.setStatistics(true);


        //Open Window
        Vec2f WinSize(TheWindow->getDesktopSize() * 0.85f);
        Pnt2f WinPos((TheWindow->getDesktopSize() - WinSize) *0.5);
        TheWindow->openWindow(WinPos,
                            WinSize,
                            "01 Native Window");

        //Enter main loop
        TheWindow->mainLoop();
    }

    osgExit();
    return 0;
}