|
有两张背景是透明的png图片都是512*512
将他们分别当做纹理贴在一个正方形节点上,设置属性使节点变成透明,再加入飞机模型cessna.osg
利用阴影示例的代码开启影子
但是结果不是我们想要的
我们发现影子不在花上的都不能显示,其实想要完成的功能是在一个透明材质上的模型显示出影子 不知道有没有解决方法
代码如下:- /**********************************************************
- *Write by FlySky
- *zzuxp@163.com http://www.OsgChina.org
- **********************************************************/
- #include <osgViewer/Viewer>
- #include <osg/Node>
- #include <osg/Geode>
- #include <osg/Group>
- #include <osg/Camera>
- #include <osg/ShapeDrawable>
- #include <osg/ComputeBoundsVisitor>
- #include <osg/BoundingBox>
- #include <osg/BoundingSphere>
- #include <osg/AnimationPath>
- #include <osgDB/ReadFile>
- #include <osgDB/WriteFile>
- #include <osgShadow/ShadowedScene>
- #include <osgShadow/ShadowVolume>
- #include <osgShadow/ShadowTexture>
- #include <osgShadow/ShadowMap>
- #include <osgShadow/SoftShadowMap>
- #include <osgShadow/ParallelSplitShadowMap>
- #include <osgUtil/Optimizer>
- #include <iostream>
- #include "Tex.h"
- //标识阴影接收对象
- const int ReceivesShadowTraversalMask = 0x1;
- //标识阴影投影对象
- const int CastsShadowTraversalMask = 0x2;
- //创建地形平面
- osg::ref_ptr<osg::Node> CreateShadowReceiverNode()
- {
- osg::ref_ptr<osg::Geode> geode = new osg::Geode;
- //设置纹理属性
- osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet(); osg::ref_ptr<osg::Image> image = osgDB::readImageFile("2.png");
- if (image.get())
- {
- osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
- //texture->setDataVariance(osg::Object::DYNAMIC);
- texture->setImage(image.get());
- stateset->setTextureAttributeAndModes(0,texture.get(),osg::StateAttribute::ON);
- // 启用混合,设置透明.
- stateset->setMode( GL_BLEND, osg::StateAttribute::ON );
- stateset->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
- }
- osg::Geometry* geom = new osg::Geometry;
- osg::Vec3Array* vertices = new osg::Vec3Array;
- vertices->push_back(osg::Vec3(-256,255,-15.1));
- vertices->push_back(osg::Vec3(-256,-256,-15.1));
- vertices->push_back(osg::Vec3(255,-256,-15.1));
- vertices->push_back(osg::Vec3(255,255,-15.1));
- geom->setVertexArray(vertices);
- osg::Vec3Array* normals = new osg::Vec3Array;
- normals->push_back(osg::Vec3(0.0f,0.0f,1.0f));
- geom->setNormalArray(normals);
- geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
- osg::ref_ptr<osg::Vec2Array>vt=new osg::Vec2Array();
- vt->push_back(osg::Vec2(0.0f,0.0f));
- vt->push_back(osg::Vec2(1.0f,0.0f));
- vt->push_back(osg::Vec2(1.0f,1.0f));
- vt->push_back(osg::Vec2(0.0f,1.0f));
- geom->setTexCoordArray(0,vt.get());
- geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4));
- geode->addDrawable(geom);
- geode->setStateSet(stateset.get());
- osg::ref_ptr<osg::Group> group = new osg::Group;
- group->addChild(geode.get());
- return group.get();
- }
- //创建一个光照
- osg::ref_ptr<osg::Node> CreateLight(osg::ref_ptr<osg::Node> model)
- {
- osg::ComputeBoundsVisitor cbbv;
- model->accept(cbbv);
- osg::BoundingBox bb = cbbv.getBoundingBox();
- osg::Vec4 lightpos;
- lightpos.set(bb.center().x(), bb.center().y(), bb.zMax() + bb.radius()*2.0f ,1.0f);
- osg::ref_ptr<osg::LightSource> ls = new osg::LightSource();
- ls->getLight()->setPosition(lightpos);
- ls->getLight()->setAmbient(osg::Vec4(0.2,0.2,0.2,1.0));
- ls->getLight()->setDiffuse(osg::Vec4(0.8,0.8,0.8,1.0));
- return ls.get() ;
- }
- int main()
- {
- osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
- osg::ref_ptr<osg::Group> root = new osg::Group();
- osg::ref_ptr<osg::Node> cessna = osgDB::readNodeFile("cessna.osg");
- cessna->setNodeMask(CastsShadowTraversalMask);
- osg::ref_ptr<osg::Node> shadower = CreateShadowReceiverNode();
- shadower->setNodeMask(ReceivesShadowTraversalMask);
- //创建一个阴影节点,并标识接收对象和投影对象
- osg::ref_ptr<osgShadow::ShadowedScene> shadowedScene = new osgShadow::ShadowedScene();
- shadowedScene->setReceivesShadowTraversalMask(ReceivesShadowTraversalMask);
- shadowedScene->setCastsShadowTraversalMask(CastsShadowTraversalMask);
- //创建阴影纹理
- //osg::ref_ptr<osgShadow::SoftShadowMap> st = new osgShadow::SoftShadowMap();
- 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> node = new osg::Group() ;
- node->addChild(cessna.get()) ;
- node->addChild(shadower.get()) ;
- //添加场景数据并添加光源
- shadowedScene->addChild(CreateLight(node.get()));
- shadowedScene->addChild(node.get());
- root->addChild(shadowedScene.get());
- //root->addChild(node.get());
- //优化场景数据
- osgUtil::Optimizer optimizer ;
- optimizer.optimize(root.get()) ;
- viewer->setSceneData(root.get());
- viewer->realize();
- viewer->run();
- return 0 ;
- }
复制代码 |
|