查看: 1210|回复: 2

osg::Stencil渲染无效

[复制链接]

该用户从未签到

发表于 2014-11-11 21:56:07 | 显示全部楼层 |阅读模式
本帖最后由 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);
Stencil.png

该用户从未签到

发表于 2014-12-6 17:12:33 | 显示全部楼层
你给搞反了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表