root/OpenSceneGraph/trunk/examples/osgpackeddepthstencil/osgpackeddepthstencil.cpp @ 9204

Revision 9204, 5.4 kB (checked in by robert, 5 years ago)

From Jaromir Vitek, osgpackeddepthstencil example as a test case.

Line 
1#include <osg/GLExtensions>
2#include <osg/Node>
3#include <osg/Geometry>
4#include <osg/Notify>
5#include <osg/Texture2D>
6#include <osg/Stencil>
7#include <osg/ColorMask>
8#include <osg/Geode>
9
10#include <osgViewer/Viewer>
11#include <osgViewer/ViewerEventHandlers>
12
13
14osg::Geode* createMask()
15{
16    osg::Vec3Array *vertices = new osg::Vec3Array;
17    vertices->push_back(osg::Vec3(-0.5, -0.5, 0.0));
18    vertices->push_back(osg::Vec3(0.5, -0.5, 0.0));
19    vertices->push_back(osg::Vec3(0.5, 0.5, 0.0));
20    vertices->push_back(osg::Vec3(-0.5, 0.5, 0.0));
21
22    osg::Geometry *geom = new osg::Geometry;
23    geom->setVertexArray(vertices);
24    geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4));
25
26    osg::Geode *geode = new osg::Geode;
27    geode->addDrawable(geom);
28
29    osg::Stencil* stencil = new osg::Stencil;
30    stencil->setFunction(osg::Stencil::ALWAYS, 1, ~0u);
31    stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::REPLACE);
32
33    osg::StateSet *ss = geode->getOrCreateStateSet();
34    ss->setAttributeAndModes(stencil,
35        osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
36    ss->setAttribute(new osg::ColorMask(false, false, false, false),
37        osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
38
39    return geode;
40}
41
42osg::Geode* createGeometry()
43{
44    osg::Vec3Array* vertices = new osg::Vec3Array;
45    vertices->push_back(osg::Vec3(-1.0, -1.0, 0.0));
46    vertices->push_back(osg::Vec3(1.0, -1.0, 0.0));
47    vertices->push_back(osg::Vec3(1.0, 1.0, 0.0));
48    vertices->push_back(osg::Vec3(-1.0, 1.0, 0.0));
49
50    osg::Geometry* geom = new osg::Geometry;
51    geom->setVertexArray(vertices);
52    geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4));
53
54    osg::Geode* geode = new osg::Geode;
55    geode->addDrawable(geom);
56
57    osg::Stencil* stencil = new osg::Stencil;
58    stencil->setFunction(osg::Stencil::NOTEQUAL, 1, ~0u);
59    stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::KEEP);
60
61    osg::StateSet *ss = geode->getOrCreateStateSet();
62    ss->setAttributeAndModes(stencil,
63        osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
64
65    return geode;
66}
67
68osg::Geode* createTextureQuad(osg::Texture2D *texture)
69{
70    osg::Vec3Array *vertices = new osg::Vec3Array;
71    vertices->push_back(osg::Vec3(-0.8, 0.0, -0.8));
72    vertices->push_back(osg::Vec3(0.8, 0.0, -0.8));
73    vertices->push_back(osg::Vec3(0.8, 0.0, 0.8));
74    vertices->push_back(osg::Vec3(-0.8, 0.0, 0.8));
75
76    osg::Vec2Array *texcoord = new osg::Vec2Array;
77    texcoord->push_back(osg::Vec2(0.0, 0.0));
78    texcoord->push_back(osg::Vec2(1.0, 0.0));
79    texcoord->push_back(osg::Vec2(1.0, 1.0));
80    texcoord->push_back(osg::Vec2(0.0, 1.0));
81
82    osg::Geometry *geom = new osg::Geometry;
83    geom->setVertexArray(vertices);
84    geom->setTexCoordArray(0, texcoord);
85    geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4));
86
87    osg::Geode *geode = new osg::Geode;
88    geode->addDrawable(geom);
89    geode->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
90
91    return geode;
92}
93
94
95int main( int argc, char **argv )
96{
97    // use an ArgumentParser object to manage the program arguments.
98    osg::ArgumentParser arguments(&argc,argv);
99
100    // construct the viewer.
101    osgViewer::Viewer viewer(arguments);
102
103    // add stats
104    viewer.addEventHandler( new osgViewer::StatsHandler() );
105
106    osg::Group* rootNode = new osg::Group;
107    rootNode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
108
109    // creates texture to be rendered
110    osg::Texture2D *texture = new osg::Texture2D;
111    texture->setTextureSize(1024, 1024);
112    texture->setInternalFormat(GL_RGBA);
113    texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR);
114    texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);
115    texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
116    texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
117    texture->setBorderColor(osg::Vec4(0, 0, 0, 0));
118
119    // creates rtt camera
120    osg::ref_ptr<osg::Camera> rttCamera = new osg::Camera;
121    rttCamera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
122    rttCamera->setClearColor(osg::Vec4(0.0, 0.4, 0.5, 0.0));
123    rttCamera->setClearStencil(0);
124    rttCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
125    rttCamera->setViewMatrix(osg::Matrixd::identity());
126    rttCamera->setViewport(0, 0, 1024, 1024);
127    rttCamera->setRenderOrder(osg::Camera::PRE_RENDER);
128    rttCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
129   
130#define USE_PACKED_DEPTH_STENCIL
131
132#ifdef USE_PACKED_DEPTH_STENCIL
133    rttCamera->attach(osg::Camera::PACKED_DEPTH_STENCIL_BUFFER, GL_DEPTH_STENCIL_EXT);
134#else
135    // this doesn't work on NVIDIA/Vista 64bit
136    // FBO status = 0x8cd6 (FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT)
137    rttCamera->attach(osg::Camera::DEPTH_BUFFER, GL_DEPTH_COMPONENT);
138    rttCamera->attach(osg::Camera::STENCIL_BUFFER, GL_STENCIL_INDEX_EXT);
139#endif
140    rttCamera->attach(osg::Camera::COLOR_BUFFER, texture);
141    rttCamera->setCullingMode(osg::Camera::VIEW_FRUSTUM_SIDES_CULLING);
142
143    // creates rtt subtree
144    osg::Group* g0 = new osg::Group;
145    g0->addChild(createMask());
146    g0->addChild(createGeometry());
147    rttCamera->addChild(g0);
148    rootNode->addChild(rttCamera);
149
150    // creates textured quad with result
151    rootNode->addChild(createTextureQuad(texture));
152   
153    // add model to the viewer.
154    viewer.setSceneData( rootNode );
155
156    return viewer.run();
157}
Note: See TracBrowser for help on using the browser.