osg示例osgplanets分析

int main( int argc, char **argv )
{
   

   
    osgViewer::Viewer viewer;

    osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
    viewer.setCameraManipulator( keyswitchManipulator.get() );

    SolarSystem solarSystem;
    

    solarSystem.rotateSpeedCorrection();
    solarSystem.RorbitCorrection();
    solarSystem.radiusCorrection();

    std::string writeFileName;
   


    osgGA::NodeTrackerManipulator::TrackerMode trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_ROTATION;
    std::string mode;
   
    osgGA::NodeTrackerManipulator::RotationMode rotationMode = osgGA::NodeTrackerManipulator::TRACKBALL;
   

    osg::Group* root = new osg::Group;

    osg::ClearNode* clearNode = new osg::ClearNode;
    clearNode->setClearColor(osg::Vec4(0.0f,0.0f,0.0f,1.0f));
    root->addChild(clearNode);

    osg::Group* sunLight = solarSystem.createSunLight();
    root->addChild(sunLight);

    // create the sun
    osg::Node* solarSun = solarSystem.createPlanet( solarSystem._radiusSun, "Sun", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f), solarSystem._mapSun );
    osg::StateSet* sunStateSet = solarSun->getOrCreateStateSet();
    osg::Material* material = new osg::Material;
    material->setEmission( osg::Material::FRONT_AND_BACK, osg::Vec4( 1.0f, 1.0f, 0.0f, 0.0f ) );
    sunStateSet->setAttributeAndModes( material, osg::StateAttribute::ON );

    osg::Billboard* sunBillboard = new osg::Billboard();
    sunBillboard->setMode(osg::Billboard::POINT_ROT_EYE);
    sunBillboard->addDrawable(
        createSquare(osg::Vec3(-150.0f,0.0f,-150.0f),osg::Vec3(300.0f,0.0f,0.0f),osg::Vec3(0.0f,0.0f,300.0f),createBillboardImage( osg::Vec4( 1.0, 1.0, 0, 1.0f), 64, 1.0) ),
        osg::Vec3(0.0f,0.0f,0.0f));

    sunLight->addChild( sunBillboard );


    // stick sun right under root, no transformations for the sun
    sunLight->addChild( solarSun );

    // create light source in the sun

/*
*********************************************
**  earthMoonGroup and Transformations
*********************************************
*/
    // create earth and moon
    osg::Node* earth = solarSystem.createPlanet( solarSystem._radiusEarth, "Earth", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f), solarSystem._mapEarth, solarSystem._mapEarthNight );
    osg::Node* moon = solarSystem.createPlanet( solarSystem._radiusMoon, "Moon", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f), solarSystem._mapMoon );

    // create transformations for the earthMoonGroup
    osg::MatrixTransform* aroundSunRotationEarthMoonGroup = solarSystem.createRotation( solarSystem._RorbitEarth, solarSystem._rotateSpeedEarthAndMoon );
//    osg::MatrixTransform* earthMoonGroupPosition = solarSystem.createTranslationAndTilt( solarSystem._RorbitEarth, solarSystem._tiltEarth );
    osg::MatrixTransform* earthMoonGroupPosition = solarSystem.createTranslationAndTilt( solarSystem._RorbitEarth, 0.0 );


    //Group with earth and moon under it
    osg::Group* earthMoonGroup = new osg::Group;

    //transformation to rotate the earth around itself
    osg::MatrixTransform* earthAroundItselfRotation = solarSystem.createRotation ( 0.0, solarSystem._rotateSpeedEarth );

    //transformations for the moon
    osg::MatrixTransform* moonAroundEarthRotation = solarSystem.createRotation( solarSystem._RorbitMoon, solarSystem._rotateSpeedMoon );
    osg::MatrixTransform* moonTranslation = solarSystem.createTranslationAndTilt( solarSystem._RorbitMoon, 0.0 );


    moonTranslation->addChild( moon );
    moonAroundEarthRotation->addChild( moonTranslation );
    earthMoonGroup->addChild( moonAroundEarthRotation );

    earthAroundItselfRotation->addChild( earth );
    earthMoonGroup->addChild( earthAroundItselfRotation );

    earthMoonGroupPosition->addChild( earthMoonGroup );

    aroundSunRotationEarthMoonGroup->addChild( earthMoonGroupPosition );

    sunLight->addChild( aroundSunRotationEarthMoonGroup );
/*
*********************************************
**  end earthMoonGroup and Transformations
*********************************************
*/

/*
*********************************************
**  Mercury and Transformations
*********************************************
*/
    osg::Node* mercury = solarSystem.createPlanet( solarSystem._radiusMercury, "Mercury", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ), solarSystem._mapMercury, "" );

    osg::MatrixTransform* aroundSunRotationMercury = solarSystem.createRotation( solarSystem._RorbitMercury, solarSystem._rotateSpeedMercury );
    osg::MatrixTransform* mercuryPosition = solarSystem.createTranslationAndTilt( solarSystem._RorbitMercury, 0.0f );

    mercuryPosition->addChild( mercury );
    aroundSunRotationMercury->addChild( mercuryPosition );

    sunLight->addChild( aroundSunRotationMercury );
/*
*********************************************
**  end Mercury and Transformations
*********************************************
*/

/*
*********************************************
**  Venus and Transformations
*********************************************
*/
    osg::Node* venus = solarSystem.createPlanet( solarSystem._radiusVenus, "Venus", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ), solarSystem._mapVenus, "" );

    osg::MatrixTransform* aroundSunRotationVenus = solarSystem.createRotation( solarSystem._RorbitVenus, solarSystem._rotateSpeedVenus );
    osg::MatrixTransform* venusPosition = solarSystem.createTranslationAndTilt( solarSystem._RorbitVenus, 0.0f );

    venusPosition->addChild( venus );
    aroundSunRotationVenus->addChild( venusPosition );

    sunLight->addChild( aroundSunRotationVenus );
/*
*********************************************
**  end Venus and Transformations
*********************************************
*/

/*
*********************************************
**  Mars and Transformations
*********************************************
*/
    osg::Node* mars = solarSystem.createPlanet( solarSystem._radiusMars, "Mars", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ), solarSystem._mapMars, "" );

    osg::MatrixTransform* aroundSunRotationMars = solarSystem.createRotation( solarSystem._RorbitMars, solarSystem._rotateSpeedMars );
    osg::MatrixTransform* marsPosition = solarSystem.createTranslationAndTilt( solarSystem._RorbitMars, 0.0f );

    marsPosition->addChild( mars );
    aroundSunRotationMars->addChild( marsPosition );

    sunLight->addChild( aroundSunRotationMars );
/*
*********************************************
**  end Mars and Transformations
*********************************************
*/

/*
*********************************************
**  Jupiter and Transformations
*********************************************
*/
    osg::Node* jupiter = solarSystem.createPlanet( solarSystem._radiusJupiter, "Jupiter", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ), solarSystem._mapJupiter, "" );

    osg::MatrixTransform* aroundSunRotationJupiter = solarSystem.createRotation( solarSystem._RorbitJupiter, solarSystem._rotateSpeedJupiter );
    osg::MatrixTransform* jupiterPosition = solarSystem.createTranslationAndTilt( solarSystem._RorbitJupiter, 0.0f );

    jupiterPosition->addChild( jupiter );
    aroundSunRotationJupiter->addChild( jupiterPosition );

    sunLight->addChild( aroundSunRotationJupiter );
/*
*********************************************
**  end Jupiter and Transformations
*********************************************
*/

/*
    // add space, but don't light it, as its not illuminated by our sun
    osg::Node* space = solarSystem.createSpace( "Space", solarSystem._mapSpace );
    space->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
    root->addChild( space );
*/

    if (!writeFileName.empty())
    {
        osgDB::writeNodeFile(*root, writeFileName);
        std::cout<<"Written solar system to \""<<writeFileName<<"\""<<std::endl;
        return 0;
    }


    // run optimization over the scene graph
    osgUtil::Optimizer optimzer;
    optimzer.optimize( root );

    // set the scene to render
    viewer.setSceneData( root );


    // set up tracker manipulators, once for each astral body
    {
        FindNamedNodeVisitor fnnv("Moon");
        root->accept(fnnv);

        if (!fnnv._foundNodes.empty())
        {
            // set up the node tracker.
            osgGA::NodeTrackerManipulator* tm = new osgGA::NodeTrackerManipulator;
            tm->setTrackerMode( trackerMode );
            tm->setRotationMode( rotationMode );
            tm->setTrackNode( fnnv._foundNodes.front().get() );

            unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
            keyswitchManipulator->addMatrixManipulator( 'm', "moon", tm );
            keyswitchManipulator->selectMatrixManipulator( num );
        }
    }

    {
        FindNamedNodeVisitor fnnv("Earth");
        root->accept(fnnv);

        if (!fnnv._foundNodes.empty())
        {
            // set up the node tracker.
            osgGA::NodeTrackerManipulator* tm = new osgGA::NodeTrackerManipulator;
            tm->setTrackerMode( trackerMode );
            tm->setRotationMode( rotationMode );
            tm->setTrackNode( fnnv._foundNodes.front().get() );

            unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
            keyswitchManipulator->addMatrixManipulator( 'e', "earth", tm);
            keyswitchManipulator->selectMatrixManipulator( num );
        }
    }

    {
        FindNamedNodeVisitor fnnv("Sun");
        root->accept(fnnv);

        if (!fnnv._foundNodes.empty())
        {
            // set up the node tracker.
            osgGA::NodeTrackerManipulator* tm = new osgGA::NodeTrackerManipulator;
            tm->setTrackerMode( trackerMode );
            tm->setRotationMode( rotationMode );
            tm->setTrackNode( fnnv._foundNodes.front().get() );

            unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
            keyswitchManipulator->addMatrixManipulator( 's', "sun", tm);
            keyswitchManipulator->selectMatrixManipulator( num );
        }
    }

    return viewer.run();

}// end main

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章