|
楼主 |
发表于 2015-2-27 17:28:16
|
显示全部楼层
老师呀 地形模型挖空一个长方形怎么不行呢。。。。
- #include "stdafx.h"
- #include <osg/Node>
- #include <osg/Geometry>
- #include <osg/Geode>
- #include <osg/Notify>
- #include <osg/MatrixTransform>
- #include <osg/Texture2D>
- #include <osg/BlendFunc>
- #include <osg/Stencil>
- #include <osg/ColorMask>
- #include <osg/Depth>
- #include <osg/ClipNode>
- #include <osgDB/ReadFile>
- #include <osgDB/WriteFile>
- #include <osgUtil/Optimizer>
- #include <osgViewer/Viewer>
- #include <iostream>
- #include <osgUtil/Tessellator>
- #pragma comment(lib,"OpenThreadsd.lib")
- #pragma comment(lib,"osgd.lib")
- #pragma comment(lib,"osgDBd.lib")
- #pragma comment(lib,"osgAPExd.lib")
- #pragma comment(lib,"osgGAd.lib")
- #pragma comment(lib,"osgUtild.lib")
- #pragma comment(lib,"osgTextd.lib")
- #pragma comment(lib,"osgSimd.lib")
- #pragma comment(lib,"osgViewerd.lib")
- osg::ref_ptr<osg::Geode> clipPlane = new osg::Geode;
- osg::ref_ptr<osg::MatrixTransform> TransMt = new osg::MatrixTransform;
- osg::ref_ptr<osg::Group> Group = new osg::Group;
- osg::ref_ptr<osg::Group> Root = new osg::Group;
- osg::Vec3 transPos;
- osg::ref_ptr<osg::Vec3Array> m_VertexV3a = new osg::Vec3Array ;
- void CreateGrooveUp(osg::ref_ptr<osg::Vec3Array> pts,osg::ref_ptr<osg::Node> pNode)
- {
- osg::Vec3Array::iterator itr = pts->begin();
- transPos = (*itr);
- for(;itr!=pts->end();itr++ )
- {
- (*itr) = (*itr) - transPos;
- }
- Group->addChild(TransMt);
- osg::Matrix mat;
- mat.setTrans(transPos);
- TransMt->setMatrix(mat);
- TransMt->addChild(clipPlane);
- osg::ref_ptr<osg::Geometry> top_cap = new osg::Geometry;
- clipPlane->addDrawable(top_cap);
- top_cap->setVertexArray(pts);
- osg::Vec4Array* colors = new osg::Vec4Array;
- colors->push_back(osg::Vec4(1,0,0,1));
- top_cap->setColorArray(colors);
- top_cap->setColorBinding(osg::Geometry::BIND_OVERALL);
- top_cap->addPrimitiveSet( new osg::DrawArrays(
- osg::PrimitiveSet::LINE_LOOP,
- 0,pts->size()));
- osgUtil::Tessellator tess;
- tess.setTessellationType( osgUtil::Tessellator::TESS_TYPE_GEOMETRY );
- tess.setWindingType( osgUtil::Tessellator::TESS_WINDING_POSITIVE );
- tess.retessellatePolygons( *(top_cap.get()) );
- osg::Stencil* stencil = new osg::Stencil;
- stencil->setFunction(osg::Stencil::ALWAYS, 1, ~0u);
- stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::INCR);
- //int ref = stencil->getFunctionRef();
- //unsigned int mask = stencil->getFunctionMask();
- clipPlane->getOrCreateStateSet()->setAttributeAndModes(stencil, osg::StateAttribute::ON);
- clipPlane->getOrCreateStateSet()->setAttributeAndModes(new osg::Depth(osg::Depth::LESS, 0, 1, false), osg::StateAttribute::ON);
- clipPlane->getOrCreateStateSet()->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
- clipPlane->getOrCreateStateSet()->setAttributeAndModes(new osg::ColorMask(false, false, false, false));
- clipPlane->setCullingActive(false);
- clipPlane->getOrCreateStateSet()->setRenderBinDetails(-2, "RenderBin");
- osg::Geode* clearDepthNode = new osg::Geode;
- //clearDepthNode->addDrawable(GL_DEPTH_BUFFER_BIT);
- clearDepthNode->getOrCreateStateSet()->setRenderBinDetails(-1, "RenderBin");
- TransMt->addChild(clearDepthNode);
- stencil = new osg::Stencil;
- stencil->setFunction(osg::Stencil::NOTEQUAL, 1, 1);
- pNode->getOrCreateStateSet()->setAttributeAndModes(stencil, osg::StateAttribute::ON);
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- m_VertexV3a->push_back(osg::Vec3(-10,10,1));
- m_VertexV3a->push_back(osg::Vec3(10,10,1));
- m_VertexV3a->push_back(osg::Vec3(10,-10,1));
- m_VertexV3a->push_back(osg::Vec3(-10,-10,1));
- osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile("subtile.ive");
- CreateGrooveUp(m_VertexV3a,loadedModel);
- Root->addChild(loadedModel);
- Root->addChild(Group);
- osgViewer::Viewer viewer;
- viewer.setUpViewInWindow(100,100,800,800);
- viewer.setSceneData(Root);
- //osg::DisplaySettings::instance()->setMinimumNumStencilBits(8);
- return viewer.run();
- return 0;
- }
复制代码 |
|