|
本帖最后由 abc-osg 于 2014-11-11 21:57 编辑
geode1 为一个20*20的正方形,geode2为10*10的小方形,位于geode1中间,想把geode2的区域设置为全透明,实现有问题,下面代码有问题,高手帮助来看下,使哪儿设置错了.
osg::ref_ptr<osg::Geode> geode1 = new osg::Geode();
osg::ref_ptr<osg::Geode> geode2 = new osg::Geode();
osg::ref_ptr<osg::Geometry> geom1 = new osg::Geometry();
osg::ref_ptr<osg::Geometry> geom2 = new osg::Geometry();
osg::ref_ptr<osg::Vec3Array> v1= new osg::Vec3Array();
osg::ref_ptr<osg::Vec3Array> v2= new osg::Vec3Array();
//创建纹理坐标
osg::ref_ptr<osg::Vec2Array> vt= new osg::Vec2Array();
//创建颜色数组
osg::ref_ptr<osg::Vec4Array> vc = new osg::Vec4Array();
osg::ref_ptr<osg::Vec4Array> vc1 = new osg::Vec4Array();
//创建法线数组
osg::ref_ptr<osg::Vec3Array> nc = new osg::Vec3Array();
v1->push_back(osg::Vec3(0.0f,0.0f,0.0f)) ;
v1->push_back(osg::Vec3(20.0f,0.0f,0.0f)) ;
v1->push_back(osg::Vec3(20.0f,20.0f,0.0f)) ;
v1->push_back(osg::Vec3(0.0f,20.0f,0.0f)) ;
v2->push_back(osg::Vec3(5.0f,5.0f,0.0f)) ;
v2->push_back(osg::Vec3(15.0f,5.0f,0.0f)) ;
v2->push_back(osg::Vec3(15.0f,15.0f,0.0f)) ;
v2->push_back(osg::Vec3(5.0f,15.0f,0.0f)) ;
vc->push_back(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
vc1->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
nc->push_back(osg::Vec3(0.0f,0.0f,1.0f));
//设置顶点数据
geom1->setVertexArray(v1.get());
//设置颜色数组
geom1->setColorArray(vc.get());
//设置颜色的绑定方式为单个顶点
geom1->setColorBinding(osg::Geometry::BIND_OVERALL);
//设置法线数组
geom1->setNormalArray(nc.get());
//设置法线的绑定方式为全部顶点
geom2->setNormalBinding(osg::Geometry::BIND_OVERALL);
//添加图元,绘图基元为四边形
geom1->addPrimitiveSet(new osg:: DrawArrays(osg:: PrimitiveSet:: QUADS,0,v1->size()));
geom2->setVertexArray(v2.get());
//设置颜色数组
geom2->setColorArray(vc1.get());
//设置颜色的绑定方式为单个顶点
geom2->setColorBinding(osg::Geometry::BIND_OVERALL);
//设置法线数组
geom2->setNormalArray(nc.get());
//设置法线的绑定方式为全部顶点
geom2->setNormalBinding(osg::Geometry::BIND_OVERALL);
//添加图元,绘图基元为四边形
geom2->addPrimitiveSet(new osg:: DrawArrays(osg:: PrimitiveSet:: QUADS,0,v2->size()));
osg::Stencil *stencil2 = new osg::Stencil;
stencil2->setFunction(osg::Stencil::ALWAYS, 1, ~0u);//osg::Stencil::NEVER, 0x0, 0x0); //
stencil2->setOperation(osg::Stencil::REPLACE, osg::Stencil::REPLACE, osg::Stencil::REPLACE);
//osg::Stencil::INCR, osg::Stencil::INCR, osg::Stencil::INCR);
osg::StateSet *stateset2 = new osg::StateSet;
stateset2->setAttributeAndModes(stencil2, osg::StateAttribute::ON);
stateset2->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
//stateset2->setAttribute(new osg::ColorMask(false, false, false, false),
// osg::StateAttribute:: ON | osg::StateAttribute:: OVERRIDE);
osg::ref_ptr<osg::Material> mt=new osg::Material;
mt->setTransparency(osg::Material::FRONT_AND_BACK,1.0);
stateset2->setMode(GL_BLEND,osg::StateAttribute::ON);
stateset2->setAttributeAndModes(mt,osg:: StateAttribute:: OVERRIDE|osg:: StateAttribute:: ON);
stateset2->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
osg::StateSet *stateset1 = new osg::StateSet;
osg::Stencil *stencil1 = new osg::Stencil;
stencil1->setFunction(osg:: Stencil::EQUAL, 1,1);//osg::Stencil::NOTEQUAL, 0x1, 0x1);//
stencil1->setOperation(osg:: Stencil::KEEP, osg:: Stencil::KEEP, osg:: Stencil::KEEP);
stateset1->setAttributeAndModes(stencil1, osg::StateAttribute::ON);
stateset1->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
geode1->addDrawable(geom1.get());
geode2->addDrawable(geom2.get());
geode1->setStateSet(stateset1);
geode2->setStateSet(stateset2);
mRoot->setDataVariance(osg::Object:: DYNAMIC);
mRoot->addChild(geode1);
mRoot->addChild(geode2);
jumpToNode(geode1); |
-
|