2003-03-15 04:35:45 +08:00
|
|
|
#include <osg/Notify>
|
|
|
|
#include <osg/MatrixTransform>
|
|
|
|
#include <osg/ShapeDrawable>
|
|
|
|
#include <osg/PositionAttitudeTransform>
|
|
|
|
#include <osg/Geometry>
|
|
|
|
#include <osg/Texture2D>
|
|
|
|
#include <osg/Geode>
|
|
|
|
|
|
|
|
#include <osgUtil/Optimizer>
|
|
|
|
|
|
|
|
#include <osgDB/Registry>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
|
2007-01-11 05:05:26 +08:00
|
|
|
#include <osgViewer/Viewer>
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2007-02-16 06:28:32 +08:00
|
|
|
#include <osgShadow/ShadowedScene>
|
|
|
|
#include <osgShadow/ShadowVolume>
|
2007-02-16 18:19:15 +08:00
|
|
|
#include <osgShadow/ShadowTexture>
|
2007-02-19 22:21:01 +08:00
|
|
|
#include <osgShadow/ShadowMap>
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
// include the call which creates the shadowed subgraph.
|
|
|
|
#include "CreateShadowedScene.h"
|
|
|
|
|
|
|
|
|
|
|
|
// for the grid data..
|
|
|
|
#include "../osghangglide/terrain_coords.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime)
|
|
|
|
{
|
|
|
|
// set up the animation path
|
|
|
|
osg::AnimationPath* animationPath = new osg::AnimationPath;
|
|
|
|
animationPath->setLoopMode(osg::AnimationPath::LOOP);
|
|
|
|
|
|
|
|
int numSamples = 40;
|
|
|
|
float yaw = 0.0f;
|
|
|
|
float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f);
|
|
|
|
float roll = osg::inDegrees(30.0f);
|
|
|
|
|
|
|
|
double time=0.0f;
|
|
|
|
double time_delta = looptime/(double)numSamples;
|
|
|
|
for(int i=0;i<numSamples;++i)
|
|
|
|
{
|
|
|
|
osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f));
|
|
|
|
osg::Quat rotation(osg::Quat(roll,osg::Vec3(0.0,1.0,0.0))*osg::Quat(-(yaw+osg::inDegrees(90.0f)),osg::Vec3(0.0,0.0,1.0)));
|
|
|
|
|
|
|
|
animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation));
|
|
|
|
|
|
|
|
yaw += yaw_delta;
|
|
|
|
time += time_delta;
|
|
|
|
|
|
|
|
}
|
|
|
|
return animationPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::Node* createBase(const osg::Vec3& center,float radius)
|
|
|
|
{
|
|
|
|
|
|
|
|
osg::Geode* geode = new osg::Geode;
|
|
|
|
|
|
|
|
// set up the texture of the base.
|
|
|
|
osg::StateSet* stateset = new osg::StateSet();
|
|
|
|
osg::Image* image = osgDB::readImageFile("Images/lz.rgb");
|
|
|
|
if (image)
|
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
osg::Texture2D* texture = new osg::Texture2D;
|
|
|
|
texture->setImage(image);
|
|
|
|
stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
geode->setStateSet( stateset );
|
|
|
|
|
|
|
|
|
2003-10-28 00:07:21 +08:00
|
|
|
osg::HeightField* grid = new osg::HeightField;
|
2005-05-09 21:09:07 +08:00
|
|
|
grid->allocate(38,39);
|
2003-03-15 04:35:45 +08:00
|
|
|
grid->setOrigin(center+osg::Vec3(-radius,-radius,0.0f));
|
|
|
|
grid->setXInterval(radius*2.0f/(float)(38-1));
|
|
|
|
grid->setYInterval(radius*2.0f/(float)(39-1));
|
|
|
|
|
|
|
|
float minHeight = FLT_MAX;
|
|
|
|
float maxHeight = -FLT_MAX;
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int r;
|
|
|
|
for(r=0;r<39;++r)
|
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
for(unsigned int c=0;c<38;++c)
|
|
|
|
{
|
2003-03-15 04:35:45 +08:00
|
|
|
float h = vertex[r+c*39][2];
|
|
|
|
if (h>maxHeight) maxHeight=h;
|
|
|
|
if (h<minHeight) minHeight=h;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float hieghtScale = radius*0.5f/(maxHeight-minHeight);
|
|
|
|
float hieghtOffset = -(minHeight+maxHeight)*0.5f;
|
|
|
|
|
|
|
|
for(r=0;r<39;++r)
|
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
for(unsigned int c=0;c<38;++c)
|
|
|
|
{
|
2003-03-15 04:35:45 +08:00
|
|
|
float h = vertex[r+c*39][2];
|
2005-11-18 04:22:55 +08:00
|
|
|
grid->setHeight(c,r,(h+hieghtOffset)*hieghtScale);
|
|
|
|
}
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
geode->addDrawable(new osg::ShapeDrawable(grid));
|
|
|
|
|
|
|
|
osg::Group* group = new osg::Group;
|
|
|
|
group->addChild(geode);
|
|
|
|
|
|
|
|
return group;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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.osg");
|
|
|
|
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;
|
2007-02-16 14:39:23 +08:00
|
|
|
xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,2.0));
|
2003-03-15 04:35:45 +08:00
|
|
|
xform->addChild(positioned);
|
|
|
|
|
|
|
|
model->addChild(xform);
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-02-16 06:28:32 +08:00
|
|
|
osg::Node* createModel(osg::ArgumentParser& arguments)
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
|
|
|
osg::Vec3 center(0.0f,0.0f,0.0f);
|
|
|
|
float radius = 100.0f;
|
|
|
|
osg::Vec3 lightPosition(center+osg::Vec3(0.0f,0.0f,radius));
|
|
|
|
|
|
|
|
// the shadower model
|
|
|
|
osg::Node* shadower = createMovingModel(center,radius*0.5f);
|
|
|
|
|
|
|
|
// the shadowed model
|
|
|
|
osg::Node* shadowed = createBase(center-osg::Vec3(0.0f,0.0f,radius*0.25),radius);
|
2007-02-16 06:28:32 +08:00
|
|
|
|
2007-02-16 14:39:23 +08:00
|
|
|
if (arguments.read("--sv"))
|
2007-02-16 06:28:32 +08:00
|
|
|
{
|
2007-02-16 14:39:23 +08:00
|
|
|
// hint to tell viewer to request stencil buffer when setting up windows
|
|
|
|
osg::DisplaySettings::instance()->setMinimumNumStencilBits(8);
|
|
|
|
|
2007-02-16 06:28:32 +08:00
|
|
|
osgShadow::ShadowedScene* shadowedScene = new osgShadow::ShadowedScene;
|
|
|
|
|
|
|
|
osg::ref_ptr<osgShadow::ShadowVolume> shadowVolume = new osgShadow::ShadowVolume;
|
|
|
|
shadowedScene->setShadowTechnique(shadowVolume.get());
|
|
|
|
shadowVolume->setDynamicShadowVolumes(true);
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::LightSource> ls = new osg::LightSource;
|
2007-02-16 14:39:23 +08:00
|
|
|
ls->getLight()->setPosition(osg::Vec4(lightPosition,1.0));
|
2007-02-16 06:28:32 +08:00
|
|
|
|
|
|
|
shadowedScene->addChild(shadower);
|
|
|
|
shadowedScene->addChild(shadowed);
|
|
|
|
shadowedScene->addChild(ls.get());
|
|
|
|
|
|
|
|
return shadowedScene;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2007-02-16 18:19:15 +08:00
|
|
|
}
|
|
|
|
else if (arguments.read("--st"))
|
|
|
|
{
|
|
|
|
|
|
|
|
osgShadow::ShadowedScene* shadowedScene = new osgShadow::ShadowedScene;
|
|
|
|
|
|
|
|
osg::ref_ptr<osgShadow::ShadowTexture> shadowTexture = new osgShadow::ShadowTexture;
|
|
|
|
shadowedScene->setShadowTechnique(shadowTexture.get());
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::LightSource> ls = new osg::LightSource;
|
|
|
|
ls->getLight()->setPosition(osg::Vec4(lightPosition,1.0));
|
|
|
|
|
|
|
|
shadowedScene->setRecievesShadowTraversalMask(0x1);
|
2007-02-19 22:21:01 +08:00
|
|
|
shadowed->setNodeMask(shadowedScene->getRecievesShadowTraversalMask());
|
|
|
|
|
|
|
|
shadowedScene->setCastsShadowTraversalMask(0x2);
|
|
|
|
shadower->setNodeMask(shadowedScene->getCastsShadowTraversalMask());
|
|
|
|
|
|
|
|
shadowedScene->addChild(shadower);
|
|
|
|
shadowedScene->addChild(shadowed);
|
|
|
|
shadowedScene->addChild(ls.get());
|
|
|
|
|
|
|
|
return shadowedScene;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (arguments.read("--sm"))
|
|
|
|
{
|
|
|
|
|
|
|
|
osgShadow::ShadowedScene* shadowedScene = new osgShadow::ShadowedScene;
|
|
|
|
|
|
|
|
osg::ref_ptr<osgShadow::ShadowMap> shadowMap = new osgShadow::ShadowMap;
|
|
|
|
shadowedScene->setShadowTechnique(shadowMap.get());
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::LightSource> ls = new osg::LightSource;
|
|
|
|
ls->getLight()->setPosition(osg::Vec4(lightPosition,1.0));
|
|
|
|
|
|
|
|
shadowedScene->setRecievesShadowTraversalMask(0x1);
|
2007-02-16 18:19:15 +08:00
|
|
|
shadowed->setNodeMask(shadowedScene->getRecievesShadowTraversalMask());
|
|
|
|
|
|
|
|
shadowedScene->setCastsShadowTraversalMask(0x2);
|
|
|
|
shadower->setNodeMask(shadowedScene->getCastsShadowTraversalMask());
|
|
|
|
|
|
|
|
shadowedScene->addChild(shadower);
|
|
|
|
shadowedScene->addChild(shadowed);
|
|
|
|
shadowedScene->addChild(ls.get());
|
|
|
|
|
|
|
|
return shadowedScene;
|
|
|
|
|
2007-02-16 06:28:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// combine the models together to create one which has the shadower and the shadowed with the required callback.
|
|
|
|
osg::Group* root = createShadowedScene(shadower,shadowed,lightPosition,radius/100.0f,1);
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-16 06:28:32 +08:00
|
|
|
int main(int argc, char ** argv)
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
2007-02-16 06:28:32 +08:00
|
|
|
osg::ArgumentParser arguments(&argc,argv);
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// construct the viewer.
|
2007-01-11 05:05:26 +08:00
|
|
|
osgViewer::Viewer viewer;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2007-01-11 05:05:26 +08:00
|
|
|
// pass the model to the viewer.
|
2007-02-16 06:28:32 +08:00
|
|
|
viewer.setSceneData( createModel(arguments) );
|
|
|
|
|
|
|
|
viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
|
2003-03-25 18:05:09 +08:00
|
|
|
|
2007-01-11 05:05:26 +08:00
|
|
|
return viewer.run();
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|