Пример #1
0
int main(int argc, char **argv)
{
    // OSG init
    osgInit(argc,argv);

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

    TutorialWindow->setDisplayCallback(display);
    TutorialWindow->setReshapeCallback(reshape);

    TutorialKeyListener TheKeyListener;
    TutorialWindow->addKeyListener(&TheKeyListener);
    TutorialMouseListener TheTutorialMouseListener;
    TutorialMouseMotionListener TheTutorialMouseMotionListener;
    TutorialWindow->addMouseListener(&TheTutorialMouseListener);
    TutorialWindow->addMouseMotionListener(&TheTutorialMouseMotionListener);

    // Create the SimpleSceneManager helper
    mgr = new SimpleSceneManager;

    // Tell the Manager what to manage
    mgr->setWindow(TutorialWindow);

    //Particle System Material
    //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/Checker.jpg");    
    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(PSMaterialChunk);
    PSSmokeMaterial->addChunk(QuadTextureEnvChunk);


    //Particle System
    //Rocket
    RocketParticleSystem = OSG::ParticleSystem::create();
    RocketParticleSystem->attachUpdateListener(TutorialWindow);

    TutorialRocketParticleSystemListener TheRocketListener;
    RocketParticleSystem->addParticleSystemListener(&TheRocketListener);

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



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



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

    Matrix EnvironmentTransformation;
    EnvironmentTransformation.setScale(0.1f);

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

    NodeRefPtr EnvironmentNode = Node::create();
    EnvironmentNode->setCore(EnvironmentTransformCore);
    //EnvironmentNode->addChild(SceneFileHandler::the()->read("Data/Chloroplast.osb"));
    EnvironmentNode->addChild(SceneFileHandler::the()->read("Data/house.obj"));

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

    NodeParticleSystemCoreRefPtr RocketParticleNodeCore = OSG::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);

    TutorialParticleCollisionListener TheCollisionListener;
    ExampleGeometryCollisionParticleSystemAffector->addParticleGeometryCollisionListener(&TheCollisionListener);


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

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


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

    SmokeAgeSizeParticleAffector = OSG::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 = OSG::ParticleSystemCore::create();
    SmokeParticleNodeCore->setSystem(SmokeParticleSystem);
    SmokeParticleNodeCore->setDrawer(SmokeParticleSystemDrawer);
    SmokeParticleNodeCore->setMaterial(PSSmokeMaterial);

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

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

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

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

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

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

    NodeParticleSystemCoreRefPtr FireballParticleNodeCore = OSG::NodeParticleSystemCore::create();
    FireballParticleNodeCore->setSystem(FireballParticleSystem);
    FireballParticleNodeCore->setPrototypeNode(FireballParticlePrototypeNode);
    //Attach the function objects to the Generator
    FireballPositionDistribution = createFireballPositionDistribution();
    FireballGenerator->setPositionDistribution(FireballPositionDistribution);
    FireballGenerator->setLifespanDistribution(createFireballLifespanDistribution());
    FireballGenerator->setBurstAmount(100.0);
    FireballGenerator->setVelocityDistribution(createFireballVelocityDistribution());
    FireballGenerator->setAccelerationDistribution(createFireballAccelerationDistribution());
    //Attach the function objects the Affectors
    FireballAgeSizeParticleAffector = OSG::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 = OSG::Node::create();
    FireballParticleNode->setCore(FireballParticleNodeCore);
    //end/////////////////////


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

    mgr->setRoot(scene);

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


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

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

    osgExit();

    return 0;
}
int main(int argc, char **argv)
{
    // OSG init
    osgInit(argc,argv);

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

    TutorialWindow->setDisplayCallback(display);
    TutorialWindow->setReshapeCallback(reshape);

    TutorialKeyListener TheKeyListener;
    TutorialWindow->addKeyListener(&TheKeyListener);
    TutorialMouseListener TheTutorialMouseListener;
    TutorialMouseMotionListener TheTutorialMouseMotionListener;
    TutorialWindow->addMouseListener(&TheTutorialMouseListener);
    TutorialWindow->addMouseMotionListener(&TheTutorialMouseMotionListener);

    // Create the SimpleSceneManager helper
    mgr = new SimpleSceneManager;

    // Tell the Manager what to manage
    mgr->setWindow(TutorialWindow);

    // Material point chunk, so particles are drawn as points
    PointChunkRefPtr PSPointChunk = PointChunk::create();
    PSPointChunk->setSize(5.0f);
    PSPointChunk->setSmooth(true);

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

    LineChunkRefPtr PSLineChunk = LineChunk::create();

    //Texture Chunk
    TextureObjChunkRefPtr PSTexChunk = OSG::TextureObjChunk::create();

    //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(PSPointChunk);
    PSMaterial->addChunk(PSBlendChunk);
    PSMaterial->addChunk(PSLineChunk);
    PSMaterial->addChunk(PSTexChunk);
    PSMaterial->setTransparencyMode(Material::TransparencyForceTransparent);

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

    //Creating Particle System
    ExampleParticleSystem = OSG::ParticleSystem::create();
    // add a couple temp particles so the camera is zoomed out 
    ExampleParticleSystem->addParticle(OSG::Pnt3f(0,0,-100),OSG::Vec3f(0,1,0),OSG::Color4f(1,1,1,1),OSG::Vec3f(1,1,1),0.1,OSG::Vec3f(0,0,0),OSG::Vec3f(0,0,0));
    ExampleParticleSystem->addParticle(OSG::Pnt3f(0,0,100),OSG::Vec3f(0,1,0),OSG::Color4f(1,1,1,1),OSG::Vec3f(1,1,1),0.1,OSG::Vec3f(0,0,0),OSG::Vec3f(0,0,0));
    ExampleParticleSystem->setMaxParticles(200);
    ExampleParticleSystem->attachUpdateListener(TutorialWindow); 

    //Creating Particle System Drawer
    PointParticleSystemDrawerRefPtr ExampleParticleSystemDrawer = OSG::PointParticleSystemDrawer::create();
    ExampleParticleSystemDrawer->setForcePerParticleSizing(true);

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

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

    // Create Trail Generator(s)
    // simple trail generator
    ExampleTrailGenerator = OSG::SimpleParticleTrailGenerator::create();
    ExampleTrailGenerator->setTrailResolution(2.5f);
    ExampleTrailGenerator->setDrawMethod(SimpleParticleTrailGenerator::POINTS);
    ExampleTrailGenerator->setTrailLength(3.12);
    ExampleTrailGenerator->setTrailLengthMethod(ParticleTrailGenerator::TIME);
    ExampleTrailGenerator->setTrailResolutionMethod(ParticleTrailGenerator::DISTANCE_SPACING);
    ExampleTrailGenerator->setTrailMaterial(PSMaterial);

    // attach listener for trail generator to the particle system
    ExampleParticleSystem->addParticleSystemListener(ExampleTrailGenerator->getParticleSystemListener());

    // Set up node with the particle system at its core
    NodeRefPtr ParticleNode = OSG::Node::create();
    ParticleNode->setCore(ParticleNodeCore);
    // add the trail generator to the scene
    ParticleNode->addChild(ExampleTrailGenerator);

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

    mgr->setRoot(scene);

    // Show the whole Scene
    mgr->showAll();
    mgr->getCamera()->setFar(10000.0f);
    mgr->getCamera()->setNear(0.1f);
    mgr->setStatistics(true);

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

    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
        << "I: Toggle drawing trails as points/lines" << 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;
}