|
- #include <osgViewer/Viewer>
- #include <osgDB/ReadFile>
- #include <osg/geode>
- #include <osg/LineWidth>
- #include <osgUtil/Tessellator>
- #include <osg/Stencil>
- #include <osg//ComputeBoundsVisitor>
- #include <osgapex/cleardrawable>
- #include <osg/Depth>
- #include <osg/MatrixTransform>
- #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")
- int _tmain(int argc, _TCHAR* argv[])
- {
- osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
- osg::ref_ptr<osg::Group> g_Root = new osg::Group;
- osg::ref_ptr<osg::Node> pNode = osgDB::readNodeFile("cow.osg");
- osg::ref_ptr<osg::Group> g_depth = new osg::Group;
- osg::ref_ptr<osg::MatrixTransform> setMat = new osg::MatrixTransform;
- setMat->setMatrix(osg::Matrixd::scale(16,1,16));
- setMat->addChild(pNode);
- osg::ref_ptr<osg::Vec3Array> V3a;
- V3a = new osg::Vec3Array;
- V3a->push_back(osg::Vec3(-0.1f,0.1f,-0.1f));
- V3a->push_back(osg::Vec3(0.1f,0.1f,-0.1f));
- V3a->push_back(osg::Vec3(0.1f,0.1f,0.1f));
- V3a->push_back(osg::Vec3(-0.1f,0.1f,0.1f));
-
- //pNode
- osg::ref_ptr<osg::Geode> ClipPlane = new osg::Geode;
- osg::ref_ptr<osg::Geometry> top_cap = new osg::Geometry;
- ClipPlane->addDrawable(top_cap);
- top_cap->setVertexArray(V3a);
- 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,V3a->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");
- g_depth->addChild(ClipPlane);
- osg::Geode* clearDepthNode = new osg::Geode;
- clearDepthNode->addDrawable(new osgAPEx::ClearDrawable(GL_DEPTH_BUFFER_BIT));
- clearDepthNode->getOrCreateStateSet()->setRenderBinDetails(-1, "RenderBin");
- g_depth->addChild(clearDepthNode);
- stencil = new osg::Stencil;
- stencil->setFunction(osg::Stencil::NOTEQUAL, 1, 1);
- pNode->getOrCreateStateSet()->setAttributeAndModes(stencil, osg::StateAttribute::ON);
- g_Root->addChild(g_depth);
- g_Root->addChild(setMat);
- viewer->setSceneData(g_Root);
- viewer->run();
- return 0;
- }
复制代码 |
|