查看: 718|回复: 0

水牛模型身上融合一个osg::Stencil长方形轮廓怎么没效果?

[复制链接]

该用户从未签到

发表于 2014-9-28 16:04:38 | 显示全部楼层 |阅读模式
  1. #include <osgViewer/Viewer>
  2. #include <osgDB/ReadFile>
  3. #include <osg/geode>
  4. #include <osg/LineWidth>
  5. #include <osgUtil/Tessellator>
  6. #include <osg/Stencil>
  7. #include <osg//ComputeBoundsVisitor>
  8. #include <osgapex/cleardrawable>
  9. #include <osg/Depth>
  10. #include <osg/MatrixTransform>

  11. #pragma comment(lib,"OpenThreadsd.lib")
  12. #pragma comment(lib,"osgd.lib")
  13. #pragma comment(lib,"osgDBd.lib")
  14. #pragma comment(lib,"osgAPExd.lib")
  15. #pragma comment(lib,"osgGAd.lib")
  16. #pragma comment(lib,"osgUtild.lib")
  17. #pragma comment(lib,"osgTextd.lib")
  18. #pragma comment(lib,"osgSimd.lib")
  19. #pragma comment(lib,"osgViewerd.lib")

  20. int _tmain(int argc, _TCHAR* argv[])
  21. {
  22.         osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
  23.         osg::ref_ptr<osg::Group> g_Root = new osg::Group;
  24.         osg::ref_ptr<osg::Node> pNode = osgDB::readNodeFile("cow.osg");
  25.         osg::ref_ptr<osg::Group> g_depth = new osg::Group;
  26.         osg::ref_ptr<osg::MatrixTransform> setMat = new osg::MatrixTransform;
  27.         setMat->setMatrix(osg::Matrixd::scale(16,1,16));
  28.         setMat->addChild(pNode);

  29.         osg::ref_ptr<osg::Vec3Array> V3a;
  30.         V3a = new osg::Vec3Array;
  31.         V3a->push_back(osg::Vec3(-0.1f,0.1f,-0.1f));
  32.         V3a->push_back(osg::Vec3(0.1f,0.1f,-0.1f));
  33.         V3a->push_back(osg::Vec3(0.1f,0.1f,0.1f));
  34.         V3a->push_back(osg::Vec3(-0.1f,0.1f,0.1f));
  35.        
  36.         //pNode
  37.         osg::ref_ptr<osg::Geode> ClipPlane = new osg::Geode;
  38.         osg::ref_ptr<osg::Geometry> top_cap = new osg::Geometry;
  39.         ClipPlane->addDrawable(top_cap);
  40.         top_cap->setVertexArray(V3a);

  41.         osg::Vec4Array* colors = new osg::Vec4Array;
  42.         colors->push_back(osg::Vec4(1,0,0,1));
  43.         top_cap->setColorArray(colors);
  44.         top_cap->setColorBinding(osg::Geometry::BIND_OVERALL);

  45.         top_cap->addPrimitiveSet( new osg::DrawArrays(
  46.                 osg::PrimitiveSet::LINE_LOOP,
  47.                 0,V3a->size()));

  48.         osgUtil::Tessellator tess;
  49.         tess.setTessellationType( osgUtil::Tessellator::TESS_TYPE_GEOMETRY );
  50.         tess.setWindingType( osgUtil::Tessellator::TESS_WINDING_POSITIVE );
  51.         tess.retessellatePolygons(*(top_cap.get()));

  52.         osg::Stencil* stencil = new osg::Stencil;
  53.         stencil->setFunction(osg::Stencil::ALWAYS, 1, ~0u);
  54.         stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::INCR);
  55.         int ref = stencil->getFunctionRef();
  56.         unsigned int mask = stencil->getFunctionMask();
  57.         ClipPlane->getOrCreateStateSet()->setAttributeAndModes(stencil, osg::StateAttribute::ON);
  58.         ClipPlane->getOrCreateStateSet()->setAttributeAndModes(new osg::Depth(osg::Depth::LESS, 0, 1, false), osg::StateAttribute::ON);
  59.         ClipPlane->getOrCreateStateSet()->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
  60.         ClipPlane->getOrCreateStateSet()->setAttributeAndModes(new osg::ColorMask(false, false, false, false));
  61.         ClipPlane->setCullingActive(false);
  62.         ClipPlane->getOrCreateStateSet()->setRenderBinDetails(-2, "RenderBin");
  63.         g_depth->addChild(ClipPlane);

  64.         osg::Geode* clearDepthNode = new osg::Geode;
  65.         clearDepthNode->addDrawable(new osgAPEx::ClearDrawable(GL_DEPTH_BUFFER_BIT));
  66.         clearDepthNode->getOrCreateStateSet()->setRenderBinDetails(-1, "RenderBin");
  67.         g_depth->addChild(clearDepthNode);

  68.         stencil = new osg::Stencil;
  69.         stencil->setFunction(osg::Stencil::NOTEQUAL, 1, 1);
  70.         pNode->getOrCreateStateSet()->setAttributeAndModes(stencil, osg::StateAttribute::ON);   

  71.         g_Root->addChild(g_depth);
  72.         g_Root->addChild(setMat);
  73.         viewer->setSceneData(g_Root);
  74.         viewer->run();
  75.         return 0;
  76. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

联系我们

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