TA的每日心情 | 开心 2020-3-20 17:50 |
---|
签到天数: 1 天 [LV.1]初来乍到
|
本帖最后由 liyihongcug 于 2020-3-19 10:30 编辑
BS里可以和CS(ShadowCaster)里对照。CS需要setLight
并且把caster->getShadowCastingGroup()->addChild
SkyNode *s_sky = osgEarth::Util::SkyNode::create(m_pMapNode);
caster = new osgEarth::Util::ShadowCaster();
s_sky->attach( m_pViewer, 0 );
if ( m_pMapNode->getNumParents() > 0 )
{
qDebug()<<"insert group";
osgEarth::insertGroup(s_sky, m_pMapNode->getParent(0));
}
else
{
qDebug()<<"insert not group";
s_sky->addChild( m_pMapNode );
m_pRoot->addChild(s_sky);
}
caster->setLight( m_pViewer->getLight() );
caster->getShadowCastingGroup()->addChild( m_pMapNode->getModelLayerGroup() );
if ( m_pMapNode->getNumParents() > 0 )
{
insertGroup(caster, m_pMapNode->getParent(0));
}
else
{
caster->addChild(m_pMapNode);
m_pRoot->addChild(caster);
}
m_pMapNode->addChild(m_pSceneData);
if(theGlobalVar->getGraphicsType() == GT_USE3D)
{
ref_ptr model = osgDB::readNodeFile("./Resources/Models/2.3ds");
osgEarth::GeoPoint point(SpatialReference::create("wgs84"), 103.659487, 31.037032, 698, ALTMODE_ABSOLUTE);
osg::Matrix m;
point.createLocalToWorld(m);
ref_ptr mt = new osg::MatrixTransform(m);
mt->addChild(model.get());
caster->getShadowCastingGroup()->addChild(mt.get());
m_pRoot->addChild(mt.get());
//Registry::shaderGenerator().run(model.get());
}
要点追溯:osg 阴影实现
渲染的时候只要标记出 阴影投影对象 和 阴影接收对象 就行了,其他的交给引擎就可以了。
实现步骤:
创建ShadowedScene对象。
标明 投射者和被投射者。
选择一种阴影渲染技术,如 ShadowMap,ShadowVolume,ShadowTexture,SoftShadowMap等。
标明哪个物体是投射着哪个是被投射者。
之后将两者及光照添加到ShadowedScene中进行渲染
第一种 shadowMap
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
osg::ref_ptr<osg::Group> root = new osg::Group();
//创建一个阴影节点,并标识接收对象和投影对象
osg::ref_ptr<osgShadow::ShadowedScene> shadowedScene = new osgShadow::ShadowedScene();
shadowedScene->setReceivesShadowTraversalMask(ReceivesShadowTraversalMask);
shadowedScene->setCastsShadowTraversalMask(CastsShadowTraversalMask);
//创建阴影纹理
osg::ref_ptr<osgShadow::ShadowMap> st = new osgShadow::ShadowMap();
//osg::ref_ptr<osgShadow::ShadowTexture> st = new osgShadow::ShadowTexture();
//关联阴影纹理
shadowedScene->setShadowTechnique(st);
//添加光照
osg::ref_ptr<osg::Group> lightRoot = new osg::Group;
createLight(lightRoot);
shadowedScene->addChild(lightRoot.get());
//物体
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->addDrawable( createHouseWall() );
//旋转物体
osg::ref_ptr<osg:ositionAttitudeTransform> pat = new osg::PositionAttitudeTransform;
pat->addChild( geode );
pat->setUpdateCallback( new RotateCallback );
shadowedScene->addChild(pat);
shadowedScene->addChild(createGround());
root->addChild(shadowedScene);
osgUtil::Optimizer optimizer ;
optimizer.optimize(root.get()) ;
viewer->setUpViewInWindow(20,20,400,400);
viewer->setSceneData(root.get());
viewer->setCameraManipulator(new osgGA::TrackballManipulator);
return viewer->run();
}
第三种ShadowTexture
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
osg::ref_ptr<osg::Group> root = new osg::Group();
//创建一个阴影节点,并标识接收对象和投影对象
osg::ref_ptr<osgShadow::ShadowedScene> shadowedScene = new osgShadow::ShadowedScene();
shadowedScene->setReceivesShadowTraversalMask(ReceivesShadowTraversalMask);
shadowedScene->setCastsShadowTraversalMask(CastsShadowTraversalMask);
//创建阴影纹理
osg::ref_ptr<osgShadow::ShadowTexture> st = new osgShadow::ShadowTexture();
//关联阴影纹理
shadowedScene->setShadowTechnique(st);
osg::ref_ptr<osg::Node> node = new osg::Node();
node = createModel();
//添加场景数据并添加光源
shadowedScene->addChild(createLight(node.get()));
shadowedScene->addChild(node.get());
root->addChild(shadowedScene.get());
//优化场景数据
osgUtil::Optimizer optimizer;
optimizer.optimize(root.get());
viewer->setSceneData(root.get());
viewer->realize();
viewer->run();
return 0; |
|