Hi Robert,

Oops, I just forgot to attach my common used functions. Please use the
new source file attached. :-)

Thanks,

Wang Rui

2011/9/5 Robert Osfield <[email protected]>:
> Hi Rui,
>
>
> I tried compiling your example but got the compile error:
>
>   14:27: fatal error: CommonFunctions: No such file or directory
>
> It looks to me like I need some additional files.
>
> Robert.
>
/* -*-c++-*- OpenSceneGraph Cookbook
 * Chapter 6 Recipe 2
 * Author: Wang Rui <wangray84 at gmail dot com>
*/

#include <osg/AnimationPath>
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osgShadow/ShadowedScene>
#include <osgShadow/ViewDependentShadowMap>
#include <osgGA/TrackballManipulator>
#include <osgViewer/ViewerEventHandlers>
#include <osgViewer/Viewer>

osg::AnimationPathCallback* createAnimationPathCallback( float radius, float time )
{
    osg::ref_ptr<osg::AnimationPath> path = new osg::AnimationPath;
    path->setLoopMode( osg::AnimationPath::LOOP );

    unsigned int numSamples = 32;
    float delta_yaw = 2.0f * osg::PI/((float)numSamples - 1.0f);
    float delta_time = time / (float)numSamples;
    for ( unsigned int i=0; i<numSamples; ++i )
    {
        float yaw = delta_yaw * (float)i;
        osg::Vec3 pos( sinf(yaw)*radius, cosf(yaw)*radius, 0.0f );
        osg::Quat rot( -yaw, osg::Z_AXIS );
        path->insert( delta_time * (float)i, osg::AnimationPath::ControlPoint(pos, rot) );
    }

    osg::ref_ptr<osg::AnimationPathCallback> apcb = new osg::AnimationPathCallback;
    apcb->setAnimationPath( path.get() );
    return apcb.release();    
}

int main( int argc, char** argv )
{
    unsigned int rcvShadowMask = 0x1;
    unsigned int castShadowMask = 0x2;
    
    // Set the ground (only receives shadow)
    osg::ref_ptr<osg::MatrixTransform> groundNode = new osg::MatrixTransform;
    groundNode->addChild( osgDB::readNodeFile("lz.osg") );
    groundNode->setMatrix( osg::Matrix::translate(200.0f, 200.0f,-200.0f) );
    groundNode->setNodeMask( rcvShadowMask );
    
    // Set the cessna (only casts shadow)
    osg::ref_ptr<osg::MatrixTransform> cessnaNode = new osg::MatrixTransform;
    cessnaNode->addChild( osgDB::readNodeFile("cessna.osg.0,0,90.rot") );
    cessnaNode->addUpdateCallback( createAnimationPathCallback(50.0f, 6.0f) );
    cessnaNode->setNodeMask( castShadowMask );
    
    // Set shadow node
    osg::ref_ptr<osgShadow::ViewDependentShadowMap> vdsm = new osgShadow::ViewDependentShadowMap;
    vdsm->setShadowMapProjectionHint( osgShadow::ViewDependentShadowMap::ORTHOGRAPHIC_SHADOW_MAP );
    vdsm->setBaseShadowTextureUnit( 1 );
    
    osg::ref_ptr<osgShadow::ShadowedScene> shadowRoot = new osgShadow::ShadowedScene;
    shadowRoot->setShadowTechnique( vdsm.get() );
    shadowRoot->setReceivesShadowTraversalMask( rcvShadowMask );
    shadowRoot->setCastsShadowTraversalMask( castShadowMask );
    
    shadowRoot->addChild( groundNode.get() );
    for ( unsigned int i=0; i<10; ++i )
    {
        for ( unsigned int j=0; j<10; ++j )
        {
            osg::ref_ptr<osg::MatrixTransform> cessnaInstance = new osg::MatrixTransform;
            cessnaInstance->setMatrix( osg::Matrix::translate((float)i*50.0f, (float)j*50.0f, 0.0f) );
            cessnaInstance->addChild( cessnaNode.get() );
            shadowRoot->addChild( cessnaInstance.get() );
        }
    }
    
    const osg::BoundingSphere& bs = groundNode->getBound();
    osg::ref_ptr<osgGA::TrackballManipulator> trackball = new osgGA::TrackballManipulator;
    trackball->setHomePosition( bs.center()+osg::Vec3(0.0f, 0.0f, bs.radius()*0.4f), bs.center(), osg::Y_AXIS );
    
    osgViewer::Viewer viewer;
    viewer.setCameraManipulator( trackball.get() );
    viewer.setSceneData( shadowRoot.get() );
    viewer.addEventHandler( new osgViewer::StatsHandler );
    return viewer.run();
}
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to