Beispiel #1
0
//创建移动表针
osg::Node* cOSG::CreateMoveHandler(void)
{
	//得到当前的时间
	SYSTEMTIME st ;
	::GetLocalTime (&st) ;
	//创建秒针,分针,时针
	osg::Node * second = CreateHandler(osg::Vec3 (-10.0,-0.0 , 0.0), osg::Vec3(35,-0.0 ,0.0)) ;
	osg::Node * minute = CreateHandler(osg::Vec3 (-8.0, -0.0, 0.0), osg::Vec3 (25,	-0.0, 0.0)) ;
	osg::Node * hour = CreateHandler(osg::Vec3 (-6.0, -0.0, 0.0), osg::Vec3 (15,	-0.0, 0.0)) ;
	//利用当前系统时间初始化角度
	float animationLength = 60.0F;
	osg::Vec3 center(0.0, 0.0, 0.0);
	float radius = 0.0 ;
	float angle0 , angle1, angle2 ;
	angle0 = st.wSecond *6.0 ;
	angle1 = st.wMinute *6.0 + angle0 * (1/60.0) ;
	angle2 = st.wHour * 30.0 + angle1 * (1/60) ;
	//周期越来越长,秒针周期为60S
	osg::AnimationPath* secondPath =createAnimationPath(center,radius,animationLength, -120.0+angle0) ;//+ angle2);
	animationLength *= 60.0f ;
	//分针为60^2
	osg::AnimationPath* minutePath = createAnimationPath (center, radius, animationLength, -120.0+angle1); //+angle1);
	animationLength *=60.0f ;
	//时针为60^3
	osg::AnimationPath* hourPath = createAnimationPath (center, radius, animationLength, -120.0+angle2) ;//+angle0);
	osg::Group* model = new osg::Group;
	osg::MatrixTransform* positioned1 = new osg::MatrixTransform;
	positioned1->setDataVariance(osg::Object::STATIC);
	positioned1->addChild(second);
	osg::MatrixTransform* positioned2 = new osg::MatrixTransform;
	positioned2->setDataVariance(osg::Object::STATIC);
	positioned2->addChild (minute);
	osg::MatrixTransform* positioned3 = new osg::MatrixTransform;
	positioned3->setDataVariance(osg::Object::STATIC);
	positioned3->addChild (hour);
	osg::PositionAttitudeTransform* xform1 = new osg::PositionAttitudeTransform;
	xform1->setUpdateCallback(new osg::AnimationPathCallback(secondPath,0.0,1.0));
	xform1->addChild(positioned1);
	osg::PositionAttitudeTransform* xform2 = new osg::PositionAttitudeTransform;
	xform2->setUpdateCallback(new osg::AnimationPathCallback(minutePath,0.0,1.0));
	xform2->addChild(positioned2);
	osg::PositionAttitudeTransform* xform3 = new osg::PositionAttitudeTransform;
	xform3->setUpdateCallback(new osg::AnimationPathCallback(hourPath,0.0,1.0));
	xform3->addChild(positioned3);
	model->addChild(xform1);
	model->addChild(xform2);
	model->addChild(xform3);
	return model;
}
Beispiel #2
0
int main( int argc,
          char * argv[] )
{
    osgViewer::Viewer viewer;
    viewer.setUpViewInWindow( 10, 30, 1000, 600 );

	viewer.setCameraManipulator(new CTravelManipulator(&viewer))

    osg::ref_ptr< osg::Group > root = new osg::Group;
    viewer.setSceneData( root.get() );

    btDynamicsWorld * dynamicsWorld = initPhysics();

    root->addChild( createModel( dynamicsWorld ) );//创建一个加入dynamicWorld中的刚体模型

    /* BEGIN: Create environment boxes */
    osg::MatrixTransform * ground;
    btRigidBody * groundBody;

    float thin = .01;
    ground = createOSGBox( osg::Vec3( 800, 800, thin ) );
    root->addChild( ground );//先创建一个osg节点
    groundBody = createBTBox( ground, osg::Vec3( 0, 0, -10 ) );//再根据节点,创建物理性质,刚体
    dynamicsWorld->addRigidBody( groundBody );

    /* BEGIN: Create animated box */
    /* OSG Code */
    osg::MatrixTransform * box = createOSGBox( osg::Vec3( .3, .3, .3 ) );
    osg::AnimationPathCallback * apc = new osg::AnimationPathCallback( createAnimationPath( osg::Vec3( 0, 0, -9.25 ), 9.4, 6 ), 0, 1 );
    box->setUpdateCallback( apc );//创建做圆周运动的盒子,添加路径动画
    root->addChild( box );//osg对象要逐一加入组节点root中

    /* Bullet Code */
    btRigidBody * boxBody = createBTBox( box, osg::Vec3( -9, -3, -9 ) );//再结合盒子,添加刚体性质
    boxBody->setCollisionFlags( boxBody->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT );
    boxBody->setActivationState( DISABLE_DEACTIVATION );
    dynamicsWorld->addRigidBody( boxBody );//属于bullet的对象,要逐一添加进入 btDynamicsWorld * dynamicsWorld对象中

    /* osgBullet Code */
    osgbCollision::RefBulletObject< btRigidBody >* boxRigid =
        new osgbCollision::RefBulletObject< btRigidBody >( boxBody );
    box->setUserData( boxRigid );//利用osgbullet代码,将osg对象和bullet对象结合起来

    osgbDynamics::RigidBodyAnimation * rba = new osgbDynamics::RigidBodyAnimation;
    apc->setNestedCallback( rba );
    /* END: Create animated box */

    double currSimTime = viewer.getFrameStamp()->getSimulationTime();;
    double prevSimTime = viewer.getFrameStamp()->getSimulationTime();
    viewer.realize();
    while( !viewer.done() )
    {
        currSimTime = viewer.getFrameStamp()->getSimulationTime();
        dynamicsWorld->stepSimulation( currSimTime - prevSimTime );
        prevSimTime = currSimTime;
        viewer.frame();
    }

    return( 0 );
}
Beispiel #3
0
    osg::Node* createMovingModel(const osg::Vec3& center, float radius)
    {
        float animationLength = 10.0f;

        osg::AnimationPath* animationPath = createAnimationPath(center,radius,animationLength);

        osg::Group* model = new osg::Group;

        osg::Node* cessna = osgDB::readNodeFile("cessna.osgt");
        if (cessna)
        {
            const osg::BoundingSphere& bs = cessna->getBound();

            float size = radius/bs.radius()*0.3f;
            osg::MatrixTransform* positioned = new osg::MatrixTransform;
            positioned->setDataVariance(osg::Object::STATIC);
            positioned->setMatrix(osg::Matrix::translate(-bs.center())*
                                  osg::Matrix::scale(size,size,size)*
                                  osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,2.0f));

            positioned->addChild(cessna);

            osg::MatrixTransform* xform = new osg::MatrixTransform;
            xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,2.0));
            xform->addChild(positioned);

            model->addChild(xform);
        }

        return model;
    }
osg::Node* createMovingModel(const osg::Vec3& center, float radius)
{
    float animationLength = 10.0f;

    osg::AnimationPath* animationPath = createAnimationPath(center,radius,animationLength);

    osg::Group* model = new osg::Group;

    osg::Node* glider = osgDB::readNodeFile("glider.osg");
    if (glider)
    {
        const osg::BoundingSphere& bs = glider->getBound();

        osg::BoundingSphere::value_type size = radius/bs.radius()*0.3f;
        osg::MatrixTransform* positioned = new osg::MatrixTransform;
        positioned->setDataVariance(osg::Object::STATIC);
        positioned->setMatrix(osg::Matrix::translate(-bs.center())*
                                     osg::Matrix::scale(size,size,size)*
                                     osg::Matrix::rotate(osg::inDegrees(-90.0f),0.0f,0.0f,1.0f));
    
        positioned->addChild(glider);
        
        // Create a sound node
        //osg::ref_ptr<osgAudio::SoundNode> sound_node = createSound("bee.wav");

        // Add the sound node
        //positioned->addChild(sound_node.get());

        osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform;    
        xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,1.0));
        xform->addChild(positioned);

        model->addChild(xform);
    }
 
    osg::Node* cessna = osgDB::readNodeFile("cessna.osg");
    if (cessna)
    {
        const osg::BoundingSphere& bs = cessna->getBound();

        osg::BoundingSphere::value_type size = radius/bs.radius()*0.3f;
        osg::MatrixTransform* positioned = new osg::MatrixTransform;
        positioned->setDataVariance(osg::Object::STATIC);
        positioned->setMatrix(osg::Matrix::translate(-bs.center())*
                                     osg::Matrix::scale(size,size,size)*
                                     osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,1.0f));
    
        positioned->addChild(cessna);
    
        osg::MatrixTransform* xform = new osg::MatrixTransform;
        xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,2.0));
        xform->addChild(positioned);

        model->addChild(xform);
    }
    
    return model;
}
Beispiel #5
0
osg::Node* createMovingModel(const osg::Vec3& center, float radius)
{
    float animationLength = 10.0f;

    osg::AnimationPath* animationPath = createAnimationPath(center,radius,animationLength);

    osg::ref_ptr<osg::Group> model = new osg::Group;

    osg::ref_ptr<osg::Node> glider = osgDB::readRefNodeFile("glider.osgt");
    if (glider)
    {
        const osg::BoundingSphere& bs = glider->getBound();
        float size = radius/bs.radius()*0.15f;

        osg::MatrixTransform* positioned = new osg::MatrixTransform;
        positioned->setDataVariance(osg::Object::STATIC);
        positioned->setMatrix(osg::Matrix::translate(-bs.center())*
                              osg::Matrix::scale(size,size,size)*
                              osg::Matrix::rotate(osg::inDegrees(-90.0f),0.0f,0.0f,1.0f));

        positioned->addChild(glider);

        osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform;
        xform->setDataVariance(osg::Object::DYNAMIC);
        xform->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
        xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,0.5));
        xform->addChild(positioned);

        model->addChild(xform);
    }

    osg::ref_ptr<osg::Node> cessna = osgDB::readRefNodeFile("cessna.osgt");
    if (cessna)
    {
        const osg::BoundingSphere& bs = cessna->getBound();
        float size = radius/bs.radius()*0.15f;

        osg::MatrixTransform* positioned = new osg::MatrixTransform;
        positioned->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
        positioned->setDataVariance(osg::Object::STATIC);
        positioned->setMatrix(osg::Matrix::translate(-bs.center())*
                              osg::Matrix::scale(size,size,size)*
                              osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,1.0f));

        //positioned->addChild(cessna);
        positioned->addChild(cessna);

        osg::MatrixTransform* xform = new osg::MatrixTransform;
        xform->setDataVariance(osg::Object::DYNAMIC);
        xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,1.0));
        xform->addChild(positioned);

        model->addChild(xform);
    }

    return model.release();
}
osg::MatrixTransform* SolarSystem::createRotation( double orbit, double speed )
{
    osg::Vec3 center( 0.0, 0.0, 0.0 );
    float animationLength = 10.0f;
    osg::AnimationPath* animationPath = createAnimationPath( center, orbit, animationLength );

    osg::MatrixTransform* rotation = new osg::MatrixTransform;
    rotation->setUpdateCallback( new osg::AnimationPathCallback( animationPath, 0.0f, speed ) );

    return rotation;
}// end SolarSystem::createEarthRotation
/** 
 * This method will add an animation path to the given node.
 */
void OSGExp::addAnimation(INode* node, TimeValue t, osg::Node* osgNode){
	if (!hasAnimation(node))
		return;
	osg::AnimationPath* animationPath = createAnimationPath(node,t);
	if(animationPath){
		osgNode->setUpdateCallback(new osg::AnimationPathCallback(animationPath));
		osgNode->setDataVariance(osg::Object::DYNAMIC);
//		TSTR string;
//		if (node->GetUserPropString("animation",string))
//			osgNode->setName(string.data());
	}
}
int main( int argc, char** argv )
{
    osg::ref_ptr<osg::MatrixTransform> cessna = new osg::MatrixTransform;
    cessna->addChild( osgDB::readNodeFile("cessna.osg.0,0,90.rot") );
    
    osg::ref_ptr<osg::AnimationPathCallback> apcb = new osg::AnimationPathCallback;
    apcb->setAnimationPath( createAnimationPath(50.0f, 6.0f) );
    cessna->addUpdateCallback( apcb.get() );
    
    osg::ref_ptr<osg::MatrixTransform> dumptruck = new osg::MatrixTransform;
    dumptruck->addChild( osgDB::readNodeFile("dumptruck.osg") );
    dumptruck->setMatrix( osg::Matrix::translate(0.0f, 0.0f, -100.0f) );
    
    osg::ref_ptr<osg::MatrixTransform> models = new osg::MatrixTransform;
    models->addChild( cessna.get() );
    models->addChild( dumptruck.get() );
    models->setMatrix( osg::Matrix::translate(0.0f, 0.0f, 200.0f) );
    
    // World bounding box callback & node
    osg::ref_ptr<BoundingBoxCallback> bbcb = new BoundingBoxCallback;
    bbcb->_nodesToCompute.push_back( cessna.get() );
    bbcb->_nodesToCompute.push_back( dumptruck.get() );
    
    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    geode->addDrawable( new osg::ShapeDrawable(new osg::Box) );
    
    osg::ref_ptr<osg::MatrixTransform> boundingBoxNode = new osg::MatrixTransform;
    boundingBoxNode->addChild( geode.get() );
    boundingBoxNode->addUpdateCallback( bbcb.get() );
    boundingBoxNode->getOrCreateStateSet()->setAttributeAndModes(
        new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE) );
    boundingBoxNode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
    
    // Build the scene
    osg::ref_ptr<osg::Group> root = new osg::Group;
    root->addChild( models.get() );
    root->addChild( osgDB::readNodeFile("lz.osg") );
    root->addChild( boundingBoxNode.get() );
    
    osgViewer::Viewer viewer;
    viewer.setSceneData( root.get() );

    return viewer.run();
}
Beispiel #9
0
int main_spark( int argc, char** argv )
{
    osg::ArgumentParser arguments( &argc, argv );
    
    bool trackingModel = false;
    int effectType = 0;
    
    if ( arguments.read("--simple") ) effectType = 0;
    else if ( arguments.read("--explosion") ) effectType = 1;
    else if ( arguments.read("--fire") ) effectType = 2;
    else if ( arguments.read("--rain") ) effectType = 3;
    else if ( arguments.read("--smoke") ) effectType = 4;
    
    effectType = 3;

    spark::init();

    osg::ref_ptr<SparkDrawable> spark = new SparkDrawable;
	osg::ref_ptr<osg::Geode> geode = new osg::Geode;

	switch ( effectType )
    {
    case 1:  // Explosion
        spark->setBaseSystemCreator( &createExplosion );
        spark->addParticleSystem();
        spark->setSortParticles( true );
        spark->addImage( "explosion", osgDB::readImageFile("data/explosion.bmp"), GL_ALPHA );
        spark->addImage( "flash", osgDB::readImageFile("data/flash.bmp"), GL_RGB );
        spark->addImage( "spark1", osgDB::readImageFile("data/spark1.bmp"), GL_RGB );
        spark->addImage( "spark2", osgDB::readImageFile("data/point.bmp"), GL_ALPHA );
        spark->addImage( "wave", osgDB::readImageFile("data/wave.bmp"), GL_RGBA );
        break;
    case 2:  // Fire
        spark->setBaseSystemCreator( /*&createFire*/&fire_creator(2).createFire );
        spark->addParticleSystem();
        spark->addImage( "fire", osgDB::readImageFile("data/fire2.bmp"), GL_ALPHA );
        spark->addImage( "explosion", osgDB::readImageFile("data/explosion.bmp"), GL_ALPHA );
        break;
    case 3:  // Rain
        spark->setBaseSystemCreator( &createRain, true );  // Must use the proto type directly
        spark->addImage( "waterdrops", osgDB::readImageFile("data/waterdrops.bmp"), GL_ALPHA );
		geode = new osg::Geode;
        break;
    case 4:  // Smoke
        spark->setBaseSystemCreator( &createSmoke );
        spark->addParticleSystem();
        spark->addImage( "smoke", osgDB::readImageFile("data/smoke.png"), GL_RGBA );
        trackingModel = true;
        break;
    default:  // Simple
        spark->setBaseSystemCreator( &createSimpleSystem );
        spark->addParticleSystem();
        spark->addImage( "flare", osgDB::readImageFile("data/flare.bmp"), GL_ALPHA );
        break;
    }
    
    geode->addDrawable( spark.get() );
    geode->getOrCreateStateSet()->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
    geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
    
    osg::ref_ptr<SparkUpdatingHandler> handler = new SparkUpdatingHandler;
    handler->addSpark( spark.get() );
    
    osg::ref_ptr<osg::MatrixTransform> model = new osg::MatrixTransform;
    model->addChild( osgDB::readNodeFile("glider.osg") );
    if ( trackingModel )
    {
        handler->setTrackee( 0, model.get() );
        
        osg::ref_ptr<osg::AnimationPathCallback> apcb = new osg::AnimationPathCallback;
        apcb->setAnimationPath( createAnimationPath(4.0f, 6.0f) );
        model->setUpdateCallback( apcb.get() );
    }
    
    osg::ref_ptr<osg::Group> root = new osg::Group;
    root->addChild( geode.get() );
    root->addChild( model.get() );
    
    osgViewer::Viewer viewer(arguments);
    viewer.getCamera()->setClearColor( osg::Vec4(0.0f, 0.0f, 0.0f, 1.0f) );
    viewer.setSceneData( root.get() );
    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
    viewer.addEventHandler( new osgViewer::StatsHandler );
    viewer.addEventHandler( new osgViewer::WindowSizeHandler );
    viewer.addEventHandler( handler.get() );
    return viewer.run();
}
Beispiel #10
0
int main( int argc, char** argv )
{
    osg::ref_ptr<osg::Node> model = osgDB::readNodeFile( "cow.osg" );
	osg::ref_ptr<osg::Node> model2 = osgDB::readNodeFile( "cessna.osg" );
    
    osg::ref_ptr<osg::Shader> vertShader = new osg::Shader( osg::Shader::VERTEX );
    osg::ref_ptr<osg::Shader> fragShader = new osg::Shader( osg::Shader::FRAGMENT );
	vertShader->loadShaderSourceFromFile("myVert.vert");
	fragShader->loadShaderSourceFromFile("myFrag.frag");
    
    osg::ref_ptr<osg::Program> program = new osg::Program;
    program->addShader( vertShader.get() );
    program->addShader( fragShader.get() );

    osg::ref_ptr<osg::Switch> n_switch = new osg::Switch;
    n_switch->addChild( model.get(), false );
    n_switch->addChild( model2.get(), true );
    n_switch->setUpdateCallback( new SwitchingCallback );

    osg::StateSet* stateset = n_switch->getOrCreateStateSet();
    stateset->setAttributeAndModes( program.get() );
	osg::Uniform *uni_lightness = stateset->getOrCreateUniform( "lightness", osg::Uniform::FLOAT, 1 );
	/*

	//	vagy másképp:

	osg::Uniform* uni_lightness = new osg::Uniform( osg::Uniform::FLOAT, // uniform típusa
													"lightness",		 // neve
													1);					 // elemszáma
	stateset->addUniform( uni_lightness );
	*/

	uni_lightness->setUpdateCallback(new UpdateLightnessCallback() );

	// anim path
    osg::ref_ptr<osg::MatrixTransform> root = new osg::MatrixTransform;
    root->addChild( n_switch.get() );

    osg::ref_ptr<osg::AnimationPathCallback> apcb = new osg::AnimationPathCallback;
    apcb->setAnimationPath( createAnimationPath(10.0f, 6.0f) );
    root->setUpdateCallback( apcb.get() );

	// viewer
	
	osgViewer::Viewer viewer;
	
	viewer.setUpViewInWindow(50, 50, 800, 600 );
    viewer.setSceneData( root.get() );

	viewer.realize();

	viewer.setCameraManipulator( new osgGA::TrackballManipulator );

    osg::State* state = viewer.getCamera()->getGraphicsContext()->getState(); 
    state->setUseModelViewAndProjectionUniforms(true); 
    state->setUseVertexAttributeAliasing(true); 

	while ( !viewer.done() )
	{
		viewer.frame();
	}

    return 0;
}
Beispiel #11
0
int main( int argc,
          char * argv[] )
{
    osg::ArgumentParser arguments( &argc, argv );
    osgViewer::Viewer viewer;
    viewer.setUpViewInWindow( 10, 30, 800, 600 );

    osgGA::TrackballManipulator * tb = new osgGA::TrackballManipulator();

    tb->setHomePosition( osg::Vec3( 5, -12, 12 ),
                        osg::Vec3( -7, 0, -10 ),
                        osg::Vec3( 0, 0, 1 ) );
    viewer.setCameraManipulator( tb );

    osg::ref_ptr< osg::Group > root = new osg::Group;
    viewer.setSceneData( root.get() );

    osgDB::getDataFilePathList().push_back( "C:\\OpenSceneGraph\\Data" );

    btDynamicsWorld * dynamicsWorld = initPhysics();

    root->addChild( createModel( dynamicsWorld ) );

    /* BEGIN: Create environment boxes */
    osg::MatrixTransform * ground;
    btRigidBody * groundBody;

    float thin = .01;
    ground = createOSGBox( osg::Vec3( 10, 10, thin ) );
    root->addChild( ground );
    groundBody = createBTBox( ground, osg::Vec3( 0, 0, -10 ) );
    dynamicsWorld->addRigidBody( groundBody );

    ground = createOSGBox( osg::Vec3( 10, thin, 5 ) );
    root->addChild( ground );
    groundBody = createBTBox( ground, osg::Vec3( 0, 10, -5 ) );
    dynamicsWorld->addRigidBody( groundBody );

    ground = createOSGBox( osg::Vec3( 10, thin, 5 ) );
    root->addChild( ground );
    groundBody = createBTBox( ground, osg::Vec3( 0, -10, -5 ) );
    dynamicsWorld->addRigidBody( groundBody );

    ground = createOSGBox( osg::Vec3( thin, 10, 5 ) );
    root->addChild( ground );
    groundBody = createBTBox( ground, osg::Vec3( 10, 0, -5 ) );
    dynamicsWorld->addRigidBody( groundBody );

    ground = createOSGBox( osg::Vec3( thin, 10, 5 ) );
    root->addChild( ground );
    groundBody = createBTBox( ground, osg::Vec3( -10, 0, -5 ) );
    dynamicsWorld->addRigidBody( groundBody );
    /* END: Create environment boxes */

    /* BEGIN: Create animated box */
    /* OSG Code */
    osg::MatrixTransform * box = createOSGBox( osg::Vec3( .3, .3, .3 ) );
    osg::AnimationPathCallback * apc = new osg::AnimationPathCallback( createAnimationPath( osg::Vec3( 0, 0, -9.25 ), 9.4, 6 ), 0, 1 );
    box->setUpdateCallback( apc );
    root->addChild( box );

    /* Bullet Code */
    btRigidBody * boxBody = createBTBox( box, osg::Vec3( -9, -3, -9 ) );
    boxBody->setCollisionFlags( boxBody->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT );
    boxBody->setActivationState( DISABLE_DEACTIVATION );
    dynamicsWorld->addRigidBody( boxBody );

    /* osgBullet Code */
    osgbCollision::RefBulletObject< btRigidBody >* boxRigid =
        new osgbCollision::RefBulletObject< btRigidBody >( boxBody );
    box->setUserData( boxRigid );

    osgbDynamics::RigidBodyAnimation * rba = new osgbDynamics::RigidBodyAnimation;
    apc->setNestedCallback( rba );
    /* END: Create animated box */

    // bonus geometry
    root->addChild( osgDB::readNodeFiles( arguments ) );

    double currSimTime = viewer.getFrameStamp()->getSimulationTime();;
    double prevSimTime = viewer.getFrameStamp()->getSimulationTime();
    viewer.realize();
    while( !viewer.done() )
    {
        currSimTime = viewer.getFrameStamp()->getSimulationTime();
        dynamicsWorld->stepSimulation( currSimTime - prevSimTime );
        prevSimTime = currSimTime;
        viewer.frame();
    }

    return( 0 );
}
Beispiel #12
0
int main( int argc,
          char * argv[] )
{
    osg::ref_ptr< osg::Group > root = new osg::Group();

    btDynamicsWorld * dynamicsWorld = initPhysics();


    // Create target
    createTarget( root.get(), dynamicsWorld );


    // Make the ground plane
    osg::MatrixTransform * ground = createOSGBox( osg::Vec3( 10, 10, .01 ) );
    root->addChild( ground );
    btRigidBody * groundBody = createBTBox( ground, osg::Vec3( 0, 0, -10 ) );
    dynamicsWorld->addRigidBody( groundBody );


    // Make animated box.
    osg::MatrixTransform * box = createOSGBox( osg::Vec3( .5, .5, .5 ) );
    osg::AnimationPathCallback * apc = new osg::AnimationPathCallback(
        createAnimationPath( osg::Vec3( -6, -10, -9 ),
                             osg::Vec3( 15.5, 18, 0 ), 5 ),
        0, 1 );
    box->setUpdateCallback( apc );
    root->addChild( box );

    //   Animated box, bullet code
    btRigidBody * boxBody = createBTBox( box, osg::Vec3( -9, -3, -9 ) );
    boxBody->setCollisionFlags( boxBody->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT );
    boxBody->setActivationState( DISABLE_DEACTIVATION );
    dynamicsWorld->addRigidBody( boxBody );

    //   Animated box, osgBullet Code
    osgbCollision::RefBulletObject< btRigidBody >* boxRigid =
        new osgbCollision::RefBulletObject< btRigidBody >( boxBody );
    box->setUserData( boxRigid );

    osgbDynamics::RigidBodyAnimation * rba = new osgbDynamics::RigidBodyAnimation;
    apc->setNestedCallback( rba );


    osgViewer::Viewer viewer;
    viewer.setUpViewInWindow( 10, 30, 800, 600 );
    osgGA::TrackballManipulator * tb = new osgGA::TrackballManipulator();
    tb->setHomePosition( osg::Vec3( 20, -24, 3 ),
                        osg::Vec3( 2, 0, -10 ),
                        osg::Vec3( 0, 0, 1 ) );
    viewer.setCameraManipulator( tb );
    viewer.setSceneData( root.get() );

    double currSimTime;
    double prevSimTime = viewer.getFrameStamp()->getSimulationTime();

    viewer.realize();

    while( !viewer.done() )
    {
        currSimTime = viewer.getFrameStamp()->getSimulationTime();
        dynamicsWorld->stepSimulation( currSimTime - prevSimTime );
        prevSimTime = currSimTime;
        viewer.frame();
    }

    return( 0 );
}