OpenTwin 0.1
OpenTwin
 
Loading...
Searching...
No Matches
CappingEffect.h
Go to the documentation of this file.
1#pragma once
2
3#include <osgFX/Effect>
4#include <osg/Geometry>
5#include <osg/Stencil>
6#include <osg/Depth>
7#include <osg/Projection>
8#include <osg/PolygonMode>
9
10class CappingTechnique : public osgFX::Technique {
11
12private:
13 osg::ref_ptr<osg::Node> _capPlane;
14
15public:
16 virtual bool validate(osg::State&) const
17 { return true; }
18
19 META_Technique("CappingTechnique","Pimpel capping technique");
22 {
23 // Build the plane to draw with the stencil mask
24 osg::Geometry *geometry = new osg::Geometry();
25 osg::Vec3Array *vertices = new osg::Vec3Array();
26 osg::Vec3Array *normals = new osg::Vec3Array();
27 osg::Vec4Array *colors = new osg::Vec4Array();
28
29 vertices->push_back(osg::Vec3(-10.0, -10.0, 0.1));
30 vertices->push_back(osg::Vec3(-10.0, 10.0, 0.1));
31 vertices->push_back(osg::Vec3(10.0, 10.0, 0.1));
32 vertices->push_back(osg::Vec3(10.0, -10.0, 0.1));
33 normals->push_back(osg::Vec3(0.0, 0.0, 1.0));
34 geometry->setVertexArray(vertices);
35 geometry->setNormalArray(normals);
36 colors->push_back(osg::Vec4(1.0, 0.0, 1.0, 1.0));
37 geometry->setColorArray(colors);
38 geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
39 geometry->setNormalBinding(osg::Geometry::BIND_OVERALL);
40 geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 4));
41
42 osg::Geode *geode = new osg::Geode();
43 geode->addDrawable(geometry);
44 geode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
45
46 _capPlane = geode;
47 return;
48
49 osg::Transform *trans = new osg::Transform();
50 trans->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
51 trans->addChild( geode );
52
53 osg::Projection *proj = new osg::Projection(osg::Matrix::ortho2D(-1,1,-1,1));
54 proj->addChild( trans );
55
56 _capPlane = proj;
57
58 }
59protected:
60 virtual void define_passes()
61 {
62 // pass #0
63 {
64 osg::ref_ptr<osg::StateSet> ss = new osg::StateSet;
65 osg::Stencil *stencil = new osg::Stencil;
66 stencil->setFunction(osg::Stencil::ALWAYS, 0x0, ~0);
67 stencil->setOperation(osg::Stencil::INVERT, osg::Stencil::INVERT, osg::Stencil::INVERT);
68 ss->setAttributeAndModes(stencil, osg::StateAttribute::ON); // | osg::StateAttribute::OVERRIDE);
69 ss->setMode(GL_CULL_FACE,osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);
70 addPass(ss.get());
71 }
72 // pass #1
73 {
74 osg::ref_ptr<osg::StateSet> ss = new osg::StateSet;
75 osg::Stencil *stencil = new osg::Stencil;
76 stencil->setFunction(osg::Stencil::NOTEQUAL, 0x0, ~0);
77 stencil->setOperation(osg::Stencil::ZERO, osg::Stencil::ZERO,osg::Stencil::ZERO);
78 ss->setAttributeAndModes(stencil, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
79 ss->setMode(GL_CLIP_PLANE0, osg::StateAttribute::OFF);
80 osg::Depth *depth = new osg::Depth();
81 depth->setWriteMask(true);
82 ss->setAttributeAndModes( depth, osg::StateAttribute::ON);
83 ss->setMode(GL_CULL_FACE,osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);
84 ss->setAttributeAndModes(new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::FILL));
85 ss->setMode(GL_CLIP_PLANE0, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);
86 addPass(ss.get());
87 }
88 }
89
90 virtual osg::Node *getOverrideChild(int pass){
91 switch(pass) {
92 case 1: // Second pass (pass #1) draws the cap plane
93 return _capPlane;
94 break;
95 default:
96 return NULL;
97 break;
98 }
99 }
100};
101
102class CappingEffect : public osgFX::Effect {
103public:
104 CappingEffect() : osgFX::Effect() {}
105 CappingEffect( const CappingEffect& copy, const osg::CopyOp op=osg::CopyOp::SHALLOW_COPY )
106 : osgFX::Effect(copy, op) {}
107
108 META_Effect( osgFX, CappingEffect, "CappingEffect", "", "" );
109protected:
110 virtual bool define_techniques(){
111 CappingTechnique* technique = new CappingTechnique();
112 addTechnique(technique);
113 return true;
114 }
115};
116
Definition CappingEffect.h:102
CappingEffect(const CappingEffect &copy, const osg::CopyOp op=osg::CopyOp::SHALLOW_COPY)
Definition CappingEffect.h:105
CappingEffect()
Definition CappingEffect.h:104
virtual bool define_techniques()
Definition CappingEffect.h:110
META_Effect(osgFX, CappingEffect, "CappingEffect", "", "")
Definition CappingEffect.h:10
virtual bool validate(osg::State &) const
Definition CappingEffect.h:16
virtual void define_passes()
Definition CappingEffect.h:60
virtual osg::Node * getOverrideChild(int pass)
Definition CappingEffect.h:90
META_Technique("CappingTechnique","Pimpel capping technique")
CappingTechnique()
Constructor.
Definition CappingEffect.h:21