|
本帖最后由 五十载 于 2015-3-19 14:19 编辑
- #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,"osgGAd.lib")
- #pragma comment(lib,"osgUtild.lib")
- #pragma comment(lib,"osgTextd.lib")
- #pragma comment(lib,"osgSimd.lib")
- #pragma comment(lib,"osgViewerd.lib")
- int main()
- {
- osg::Geometry *lineStripGeometry = new osg::Geometry;
- osg::Vec3Array *lineVertexArray = new osg::Vec3Array;
-
- lineVertexArray->push_back(osg::Vec3(0, 0, 0));
- lineVertexArray->push_back(osg::Vec3(10, 0, 0));
- lineVertexArray->push_back(osg::Vec3(10, 10, 0));
- lineVertexArray->push_back(osg::Vec3(0, 10, 0));
-
- osg::Vec4Array *lineColorArray = new osg::Vec4Array;
- lineColorArray->push_back(osg::Vec4(1.0, 1.0, 1.0, 1.0));
- lineStripGeometry->setVertexArray(lineVertexArray);
- lineStripGeometry->setColorArray(lineColorArray);
- //OSG 3.2之后的版本在setColorArray中设置绑定方式
- lineStripGeometry->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
- lineStripGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON, 0, lineVertexArray->size()));
- lineStripGeometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
- //添加模板测试
- osg::Stencil *lineStripStencil = new osg::Stencil;
- lineStripStencil->setFunction(osg::Stencil::NEVER, 0x0, 0x0);
- lineStripStencil->setOperation(osg::Stencil::INCR, osg::Stencil::INCR, osg::Stencil::INCR);
-
- lineStripGeometry->getOrCreateStateSet()->setAttributeAndModes(lineStripStencil,
- osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
-
- //////////////////////////////////////////////////////////////////////////
- osg::Geometry *quadGeometry = new osg::Geometry;
- osg::Vec3Array *quadVertexArray = new osg::Vec3Array;
- quadVertexArray->push_back(osg::Vec3(0, 0, 0));
- quadVertexArray->push_back(osg::Vec3(25, 0, 0));
- quadVertexArray->push_back(osg::Vec3(25, 25, 0));
- quadVertexArray->push_back(osg::Vec3(0, 25, 0));
- osg::Vec4Array *quadColorArray = new osg::Vec4Array;
- quadColorArray->push_back(osg::Vec4(0.0, 1.0, 1.0, 1.0));
- quadGeometry->setVertexArray(quadVertexArray);
- quadGeometry->setColorArray(quadColorArray);
- quadGeometry->setColorBinding(osg::Geometry::BIND_OVERALL);
- quadGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 4));
- quadGeometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
- osg::Stencil *quadStencil = new osg::Stencil;
- quadStencil->setFunction(osg::Stencil::NOTEQUAL, 0x1, ~0u);
- quadStencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::KEEP);
-
- quadGeometry->getOrCreateStateSet()->setAttributeAndModes(quadStencil,
- osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
- osg::Geode *geode = new osg::Geode;
- geode->addDrawable(lineStripGeometry);
- //geode->addDrawable(quadGeometry);
-
- osg::Node* pNode = new osg::Node;
- pNode = osgDB::readNodeFile("./subtile.ive");
- pNode->getOrCreateStateSet()->setAttributeAndModes(quadStencil,
- osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
- osgViewer::Viewer viewer;
- //viewer.setUpViewInWindow(100,100,800,800);
- osg::Group* pMain = new osg::Group;
- pMain->addChild(geode);
- pMain->addChild(pNode);
- viewer.setSceneData(pMain);
- viewer.getCamera()->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
- viewer.getCamera()->setClearStencil(0);
- osg::DisplaySettings::instance()->setMinimumNumStencilBits(8);
- return viewer.run();
- }
复制代码 |
|