查看: 1101|回复: 0

osg模型模板测试挖空...怎么不行呢?

[复制链接]

该用户从未签到

发表于 2015-3-17 22:55:04 | 显示全部楼层 |阅读模式
本帖最后由 五十载 于 2015-3-19 14:19 编辑

  1. #include "stdafx.h"
  2. #include <osg/Node>
  3. #include <osg/Geometry>
  4. #include <osg/Geode>
  5. #include <osg/Notify>
  6. #include <osg/MatrixTransform>
  7. #include <osg/Texture2D>
  8. #include <osg/BlendFunc>
  9. #include <osg/Stencil>
  10. #include <osg/ColorMask>
  11. #include <osg/Depth>
  12. #include <osg/ClipNode>
  13. #include <osgDB/ReadFile>
  14. #include <osgDB/WriteFile>
  15. #include <osgUtil/Optimizer>
  16. #include <osgViewer/Viewer>
  17. #include <iostream>
  18. #include <osgUtil/Tessellator>
  19. #pragma comment(lib,"OpenThreadsd.lib")
  20. #pragma comment(lib,"osgd.lib")
  21. #pragma comment(lib,"osgDBd.lib")
  22. #pragma comment(lib,"osgGAd.lib")
  23. #pragma comment(lib,"osgUtild.lib")
  24. #pragma comment(lib,"osgTextd.lib")
  25. #pragma comment(lib,"osgSimd.lib")
  26. #pragma comment(lib,"osgViewerd.lib")




  27. int main()
  28. {
  29.         osg::Geometry *lineStripGeometry = new osg::Geometry;  
  30.         osg::Vec3Array *lineVertexArray = new osg::Vec3Array;  
  31.   
  32.         lineVertexArray->push_back(osg::Vec3(0, 0, 0));
  33.         lineVertexArray->push_back(osg::Vec3(10, 0, 0));
  34.         lineVertexArray->push_back(osg::Vec3(10, 10, 0));
  35.         lineVertexArray->push_back(osg::Vec3(0, 10, 0));
  36.          
  37.         osg::Vec4Array *lineColorArray = new osg::Vec4Array;  
  38.         lineColorArray->push_back(osg::Vec4(1.0, 1.0, 1.0, 1.0));  
  39.         lineStripGeometry->setVertexArray(lineVertexArray);  
  40.         lineStripGeometry->setColorArray(lineColorArray);  
  41.         //OSG 3.2之后的版本在setColorArray中设置绑定方式  
  42.         lineStripGeometry->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);  
  43.         lineStripGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON, 0, lineVertexArray->size()));  
  44.         lineStripGeometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);  

  45.         //添加模板测试  
  46.         osg::Stencil *lineStripStencil = new osg::Stencil;  
  47.         lineStripStencil->setFunction(osg::Stencil::NEVER, 0x0, 0x0);  
  48.         lineStripStencil->setOperation(osg::Stencil::INCR, osg::Stencil::INCR, osg::Stencil::INCR);  
  49.        
  50.         lineStripGeometry->getOrCreateStateSet()->setAttributeAndModes(lineStripStencil,
  51.                 osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
  52.        
  53.         //////////////////////////////////////////////////////////////////////////  

  54.         osg::Geometry *quadGeometry = new osg::Geometry;  

  55.         osg::Vec3Array *quadVertexArray = new osg::Vec3Array;  
  56.         quadVertexArray->push_back(osg::Vec3(0, 0, 0));  
  57.         quadVertexArray->push_back(osg::Vec3(25, 0, 0));  
  58.         quadVertexArray->push_back(osg::Vec3(25, 25, 0));  
  59.         quadVertexArray->push_back(osg::Vec3(0, 25, 0));  

  60.         osg::Vec4Array *quadColorArray = new osg::Vec4Array;  
  61.         quadColorArray->push_back(osg::Vec4(0.0, 1.0, 1.0, 1.0));  

  62.         quadGeometry->setVertexArray(quadVertexArray);  
  63.         quadGeometry->setColorArray(quadColorArray);  
  64.         quadGeometry->setColorBinding(osg::Geometry::BIND_OVERALL);  
  65.         quadGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 4));  
  66.         quadGeometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);  

  67.         osg::Stencil *quadStencil = new osg::Stencil;  
  68.         quadStencil->setFunction(osg::Stencil::NOTEQUAL, 0x1, ~0u);  
  69.         quadStencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::KEEP);  
  70.        
  71.         quadGeometry->getOrCreateStateSet()->setAttributeAndModes(quadStencil,
  72.                 osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);

  73.         osg::Geode *geode = new osg::Geode;  
  74.         geode->addDrawable(lineStripGeometry);
  75.         //geode->addDrawable(quadGeometry);
  76.        

  77.         osg::Node* pNode = new osg::Node;
  78.         pNode = osgDB::readNodeFile("./subtile.ive");

  79.         pNode->getOrCreateStateSet()->setAttributeAndModes(quadStencil,
  80.                 osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);

  81.         osgViewer::Viewer viewer;
  82.         //viewer.setUpViewInWindow(100,100,800,800);
  83.         osg::Group* pMain = new osg::Group;
  84.         pMain->addChild(geode);
  85.         pMain->addChild(pNode);
  86.         viewer.setSceneData(pMain);

  87.         viewer.getCamera()->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  88.         viewer.getCamera()->setClearStencil(0);

  89.         osg::DisplaySettings::instance()->setMinimumNumStencilBits(8);
  90.         return viewer.run();
  91. }

复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

联系我们

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