|
楼主 |
发表于 2011-5-31 15:15:15
|
显示全部楼层
#include <osg/AnimationPath>
#include <osg/GraphicsContext>
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osgShadow/ShadowedScene>
#include <osgShadow/ShadowMap>
#include <osgViewer/Viewer>
osg::AnimationPath* createAnimationPath( float radius,float time)
{
osg::ref_ptr<osg::AnimationPath> path = new osg::AnimationPath;
path->setLoopMode( osg::AnimationPath::NO_LOOPING);
unsigned int numSamples = 320;
float delta_yaw = 3.0f * osg:I / ((float)numSamples - 1.0f);
float delta_time = time / (float)numSamples;
for ( unsigned int i=0; i<numSamples; i++ )
{
if (i<10)
{
osg::Vec3 pos( 0.0f,0.0f, i*10.0f-100.0f);
path->insert( delta_time * (float)i,osg::AnimationPath::ControlPoint(pos));
}
else
{
float yaw = delta_yaw * (float)(i-10);
osg::Vec3 pos( cosf(yaw)*radius-300.0f,0.0f ,sinf(yaw)*radius );
osg:uat rot( -yaw, osg::Y_AXIS );
path->insert( delta_time * (float)i,
osg::AnimationPath::ControlPoint(pos, rot));
}
}
return path.release();
}
unsigned int rcvShadowMask = 0x1;
unsigned int castShadowMask = 0x2;
int main(int argc, char** argv)
{
osg::ref_ptr<osg::GraphicsContext::Traits> traits =new osg::GraphicsContext::Traits;
traits->x = 50;
traits->y = 50;
traits->width = 800;
traits->height = 600;
traits->windowDecoration = true;
traits->doubleBuffer = true;
traits->samples = 4;
osg::ref_ptr<osg::GraphicsContext> gc =osg::GraphicsContext::createGraphicsContext( traits.get() );
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
camera->setGraphicsContext( gc );
camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height) );
camera->setClearMask( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
camera->setClearColor( osg::Vec4f(0.2f, 0.2f, 0.4f, 1.0f) );
camera->setProjectionMatrixAsPerspective(30.0f,(double)traits->width/(double)traits->height,1.0,1000.0 );
osg::ref_ptr<osg::MatrixTransform> groundNode =new osg::MatrixTransform;
groundNode->addChild( osgDB::readNodeFile("lz.osg") );
groundNode->setMatrix( osg::Matrix::translate(0.0f, 0.0f,-200.0f));
groundNode->setNodeMask( rcvShadowMask );
osg::ref_ptr<osg::MatrixTransform> cessnaNode =new osg::MatrixTransform;
cessnaNode->addChild( osgDB::readNodeFile("SZ5.flt.0,0,90.rot"));
cessnaNode->setNodeMask( castShadowMask );
osg::ref_ptr<osg::AnimationPathCallback> apcb =new osg::AnimationPathCallback;
apcb->setAnimationPath( createAnimationPath(300.0f,100.0f) );
cessnaNode->setUpdateCallback( apcb.get() );
osg::ref_ptr<osg::MatrixTransform> truckNode =new osg::MatrixTransform;
osg::ref_ptr<osg:ightSource> source = new osg::LightSource;
source->getLight()->setPosition( osg::Vec4(4.0, 4.0, 10.0,0.0) );
source->getLight()->setAmbient( osg::Vec4(0.2, 0.2, 0.2, 1.0));
source->getLight()->setDiffuse( osg::Vec4(0.8, 0.8, 0.8, 1.0));
osg::ref_ptr<osgShadow::ShadowMap> sm = new osgShadow::ShadowMap;
sm->setLight( source.get() );
sm->setTextureSize( osg::Vec2s(1024, 1024) );
sm->setTextureUnit( 1 );
osg::ref_ptr<osgShadow::ShadowedScene> root =new osgShadow::ShadowedScene;
root->setShadowTechnique( sm.get() );
root->setReceivesShadowTraversalMask( rcvShadowMask );
root->setCastsShadowTraversalMask( castShadowMask );
root->addChild( groundNode.get() );
root->addChild( cessnaNode.get() );
root->addChild( source.get() );
osgViewer::Viewer viewer;
viewer.setCamera( camera.get() );
viewer.setSceneData( root.get() );
return viewer.run();
} |
|