|
楼主 |
发表于 2008-11-29 19:54:21
|
显示全部楼层
源代码如下
// 初始化组
shadowedScene = new osgShadow::ShadowedScene;
shadowedScene->setReceivesShadowTraversalMask(ReceivesShadowTraversalMask);
shadowedScene->setCastsShadowTraversalMask(CastsShadowTraversalMask);
//自己定义的类,用来处理地形,飞机和雷达
CAnimate1* a1 = new CAnimate1();
osgSim::OverlayNode::OverlayTechnique technique = osgSim::OverlayNode::OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY;
//载入地形模型
osg::Node *temp = new osg::Node;
temp = osgDB::readNodeFile("e:\\RambleSystem\\pegout.osga") ;
center0 = temp->getBound().center();
//temp1是地形结点
osg::Node *temp1 = a1->build_world(temp,0,true, technique);
//temp2包括了飞机和雷达
osg::Node *temp2 = a1->createMovingModel(center0,100.0f,temp1,mRoot.get(),true);
//设置地形为阴影接收者
osg::Node* shadowed = temp1;
shadowed->setNodeMask(ReceivesShadowTraversalMask);
//设置飞机和雷达为阴影发送者
osg::Node* shadower = temp2;
shadower->setNodeMask(CastsShadowTraversalMask);
//设置阴影技法为ShadowMap,没有问题,效果见图1
osg::ref_ptr<osgShadow::ShadowTexture> sm = new osgShadow::ShadowTexture;
shadowedScene->setShadowTechnique(sm.get());
//假如设置阴影技法为ShadowTexture,有问题,效果见图2
osg::ref_ptr<osgShadow::ShadowTexture> sm = new osgShadow::ShadowTexture;
shadowedScene->setShadowTechnique(sm.get());
shadowedScene->addChild(shadowed);
shadowedScene->addChild(shadower);
//以下设置光源
osg::ComputeBoundsVisitor cbbv;
temp1->accept(cbbv);
osg::BoundingBox bb = cbbv.getBoundingBox();
osg::Vec4 lightpos;
bool postionalLight = true;
if (postionalLight)
{ lightpos.set(bb.center().x(),bb.center().y(),bb.zMax() + bb.radius()*2.0f ,1.0f);
} else
{ lightpos.set(0.5f,0.25f,0.8f,0.0f); }
osg::ref_ptr<osg:ightSource> ls = new osg::LightSource;
ls->getLight()->setPosition(lightpos);
shadowedScene->addChild(ls.get());
}
[ 本帖最后由 口口广大 于 2008-11-29 19:58 编辑 ] |
|