This file is indexed.

/usr/share/openscenegraph/examples/osgoit/DepthPeeling.cpp is in openscenegraph-examples 3.2.0~rc1-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#include "DepthPeeling.h"

#include <osg/Array>
#include <osg/AlphaFunc>
#include <osg/BlendFunc>
#include <osg/Depth>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Vec3>
#include <osg/MatrixTransform>
#include <osg/Texture2D>
#include <osg/TextureRectangle>
#include <osg/TexGen>
#include <osg/TexEnv>
#include <osg/TexMat>
#include <osg/TexGenNode>

#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>

#include <osg/Math>

#include <limits>
#include <iostream>

const char *DepthPeeling::PeelingShader =
{
    "#version 120\n"
#ifdef USE_TEXTURE_RECTANGLE
    "#extension GL_ARB_texture_rectangle : enable\n"
    "uniform sampler2DRectShadow depthtex;\n"
#else
    "uniform sampler2DShadow depthtex;\n"
#endif
    "uniform bool depthtest;\n"  // depth test enable flag
    "uniform float invWidth;\n"  // 1.0/width (shadow texture size)
    "uniform float invHeight;\n" // 1.0/height (shadow texture size)
    "uniform float offsetX;\n"   // viewport lower left corner (int)
    "uniform float offsetY;\n"   // viewport lower left corner (int)
    "\n"
    "bool depthpeeling()\n"
    "{\n"
    "  if( depthtest ) {\n"
    "    vec3 r0 = vec3((gl_FragCoord.x-offsetX)*invWidth,\n"
    "                   (gl_FragCoord.y-offsetY)*invHeight,\n"
    "                    gl_FragCoord.z);\n"
#ifdef USE_TEXTURE_RECTANGLE
    "    return shadow2DRect(depthtex, r0).r < 0.5;\n"
#else
    "    return shadow2D(depthtex, r0).r < 0.5;\n"
#endif
    "  }\n"
    "  return false;\n"
    "}\n"
};

class PreDrawFBOCallback : public osg::Camera::DrawCallback
{
public:
  PreDrawFBOCallback( osg::FrameBufferObject* fbo, osg::FrameBufferObject* source_fbo, unsigned int width, unsigned int height, osg::Texture *dt, osg::Texture *ct ) :
 _fbo(fbo), _source_fbo(source_fbo), _depthTexture(dt), _colorTexture(ct), _width(width), _height(height) {}

  virtual void operator () (osg::RenderInfo& renderInfo) const
  {
      // switching only the frame buffer attachments is actually faster than switching the framebuffer
#ifdef USE_PACKED_DEPTH_STENCIL
#ifdef USE_TEXTURE_RECTANGLE
     _fbo->setAttachment(osg::Camera::PACKED_DEPTH_STENCIL_BUFFER, osg::FrameBufferAttachment((osg::TextureRectangle*)(_depthTexture.get())));
#else
     _fbo->setAttachment(osg::Camera::PACKED_DEPTH_STENCIL_BUFFER, osg::FrameBufferAttachment((osg::Texture2D*)(_depthTexture.get())));
#endif
#else
#ifdef USE_TEXTURE_RECTANGLE
     _fbo->setAttachment(osg::Camera::DEPTH_BUFFER, osg::FrameBufferAttachment((osg::TextureRectangle*)(_depthTexture.get())));
#else
     _fbo->setAttachment(osg::Camera::DEPTH_BUFFER, osg::FrameBufferAttachment((osg::Texture2D*)(_depthTexture.get())));
#endif
#endif
#ifdef USE_TEXTURE_RECTANGLE
     _fbo->setAttachment(osg::Camera::COLOR_BUFFER0, osg::FrameBufferAttachment((osg::TextureRectangle*)(_colorTexture.get())));
#else
     _fbo->setAttachment(osg::Camera::COLOR_BUFFER0, osg::FrameBufferAttachment((osg::Texture2D*)(_colorTexture.get())));
#endif

     // check if we need to do some depth buffer copying from a source FBO into the current FBO
     if (_source_fbo.get() != NULL)
     {
         osg::FBOExtensions* fbo_ext = osg::FBOExtensions::instance(renderInfo.getContextID(),true);
         bool fbo_supported = fbo_ext && fbo_ext->isSupported();
         if (fbo_supported && fbo_ext->glBlitFramebuffer)
         {
             // blit the depth buffer from the solid geometry fbo into the current transparency fbo
             (_fbo.get())->apply(*renderInfo.getState(), osg::FrameBufferObject::DRAW_FRAMEBUFFER);
             (_source_fbo.get())->apply(*renderInfo.getState(), osg::FrameBufferObject::READ_FRAMEBUFFER);

//             glReadBuffer(GL_COLOR_ATTACHMENT0_EXT); // only needed to blit the color buffer
//             glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT); // only needed to blit the color buffer
             fbo_ext->glBlitFramebuffer(
                 0, 0, static_cast<GLint>(_width), static_cast<GLint>(_height),
                 0, 0, static_cast<GLint>(_width), static_cast<GLint>(_height),
#ifdef USE_PACKED_DEPTH_STENCIL
                 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
#else
                 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
#endif
             (_fbo.get())->apply(*renderInfo.getState(), osg::FrameBufferObject::READ_FRAMEBUFFER);
             (_fbo.get())->apply(*renderInfo.getState(), osg::FrameBufferObject::DRAW_FRAMEBUFFER);
         }
     }
     // switch to this fbo, if it isn't already bound
     (_fbo.get())->apply( *renderInfo.getState() );
  }
protected:
  osg::ref_ptr<osg::FrameBufferObject> _fbo;
  osg::ref_ptr<osg::FrameBufferObject> _source_fbo;
  osg::ref_ptr<osg::Texture> _depthTexture;
  osg::ref_ptr<osg::Texture> _colorTexture;
  unsigned int _width;
  unsigned int _height;
};


class PostDrawFBOCallback : public osg::Camera::DrawCallback
{
public:
  PostDrawFBOCallback(bool restore) : _restore(restore) {}

  virtual void operator () (osg::RenderInfo& renderInfo) const
  {
    // only unbind the fbo if this is the last transparency pass
    if (_restore)
      osg::FBOExtensions::instance( renderInfo.getState()->getContextID(), false )->glBindFramebuffer( GL_FRAMEBUFFER_EXT, 0 );
  }
protected:
  bool _restore;
};


DepthPeeling::CullCallback::CullCallback(unsigned int texUnit, unsigned int texWidth, unsigned int texHeight, unsigned int offsetValue) :
    _texUnit(texUnit),
    _texWidth(texWidth),
    _texHeight(texHeight),
    _offsetValue(offsetValue)
{
}

void DepthPeeling::CullCallback::operator()(osg::Node* node, osg::NodeVisitor* nv)
{
    osgUtil::CullVisitor* cullVisitor = static_cast<osgUtil::CullVisitor*>(nv);
    osgUtil::RenderStage* renderStage = cullVisitor->getCurrentRenderStage();
    const osg::Viewport* viewport = renderStage->getViewport();

    osg::Matrixd m(*cullVisitor->getProjectionMatrix());
    m.postMultTranslate(osg::Vec3d(1, 1, 1));
    m.postMultScale(osg::Vec3d(0.5, 0.5, 0.5));

    // scale the texture coordinates to the viewport
#ifdef USE_TEXTURE_RECTANGLE
    m.postMultScale(osg::Vec3d(viewport->width(), viewport->height(), 1));
#else
#ifndef USE_NON_POWER_OF_TWO_TEXTURE
    m.postMultScale(osg::Vec3d(viewport->width()/double(_texWidth), viewport->height()/double(_texHeight), 1));
#endif
#endif

    if (_texUnit != 0 && _offsetValue)
    {
        // Kind of polygon offset: note this way, we can also offset lines and points.
        // Whereas with the polygon offset we could only handle surface primitives.
        m.postMultTranslate(osg::Vec3d(0, 0, -ldexp(double(_offsetValue), -24)));
    }

    osg::TexMat* texMat = new osg::TexMat(m);
    osg::StateSet* stateSet = new osg::StateSet;
    stateSet->setTextureAttribute(_texUnit, texMat);

    if (_texUnit != 0)
    {
        //
        // GLSL pipeline support
        //

#ifdef USE_TEXTURE_RECTANGLE
        // osg::Uniform::SAMPLER_2D_RECT_SHADOW not yet available in OSG 3.0.1
        // osg::Uniform* depthUniform = new osg::Uniform(osg::Uniform::SAMPLER_2D_RECT_SHADOW, "depthtex");
        // depthUniform->set((int)_texUnit);
        osg::Uniform* depthUniform = new osg::Uniform("depthtex", (int)_texUnit);
        osg::Uniform *invWidthUniform = new osg::Uniform("invWidth", (float)1.0f);
        osg::Uniform *invHeightUniform = new osg::Uniform("invHeight", (float)1.0f);
#else
        osg::Uniform* depthUniform = new osg::Uniform(osg::Uniform::SAMPLER_2D_SHADOW, "depthtex");
        depthUniform->set((int)_texUnit);
        osg::Uniform *invWidthUniform = new osg::Uniform("invWidth", (float)1.0f / _texWidth);
        osg::Uniform *invHeightUniform = new osg::Uniform("invHeight", (float)1.0f / _texHeight);
#endif
        osg::Uniform *offsetXUniform = new osg::Uniform("offsetX", (float)viewport->x());
        osg::Uniform *offsetYUniform = new osg::Uniform("offsetY", (float)viewport->y());

        // uniforms required for any any GLSL implementation in the rendered geometry
        stateSet->addUniform(depthUniform);
        stateSet->addUniform(invWidthUniform);
        stateSet->addUniform(invHeightUniform);
        stateSet->addUniform(offsetXUniform);
        stateSet->addUniform(offsetYUniform);
    }

    cullVisitor->pushStateSet(stateSet);
    traverse(node, nv);
    cullVisitor->popStateSet();
}


osg::Node* DepthPeeling::createQuad(unsigned int layerNumber, unsigned int numTiles)
{
    float tileSpan = 1;
    float tileOffsetX = 0;
    float tileOffsetY = 0;
    if (_showAllLayers) {
        tileSpan /= numTiles;
        tileOffsetX = tileSpan * (layerNumber%numTiles);
        tileOffsetY = 1 - tileSpan * (1 + layerNumber/numTiles);
    }

    osg::Vec3Array* vertices = new osg::Vec3Array;

    vertices->push_back(osg::Vec3f(tileOffsetX           , tileOffsetY            , 0));
    vertices->push_back(osg::Vec3f(tileOffsetX           , tileOffsetY  + tileSpan, 0));
    vertices->push_back(osg::Vec3f(tileOffsetX + tileSpan, tileOffsetY  + tileSpan, 0));
    vertices->push_back(osg::Vec3f(tileOffsetX + tileSpan, tileOffsetY            , 0));

    osg::Vec3Array* colors = new osg::Vec3Array;
    colors->push_back(osg::Vec3(1, 1, 1));

    osg::Vec2Array* texcoords = new osg::Vec2Array;
    texcoords->push_back(osg::Vec2f(0, 0));
    texcoords->push_back(osg::Vec2f(0, 1));
    texcoords->push_back(osg::Vec2f(1, 1));
    texcoords->push_back(osg::Vec2f(1, 0));

    osg::Geometry* geometry = new osg::Geometry;
    geometry->setVertexArray(vertices);
    geometry->setTexCoordArray(0, texcoords);

    geometry->setColorArray(colors, osg::Array::BIND_OVERALL);

    geometry->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4));

    osg::Geode* geode = new osg::Geode;
    geode->addDrawable(geometry);

    return geode;
}

#include <osg/State>

void DepthPeeling::createPeeling()
{
    int numTiles = ceil(sqrt(double(_numPasses)));

    // cleanup any previous scene data
    _root->removeChildren(0, _root->getNumChildren());

    // create depth textures
    _depthTextures.clear();
    _depthTextures.resize(3);
    for (unsigned int i = 0; i < 3; ++i) {
#ifdef USE_TEXTURE_RECTANGLE
        _depthTextures[i] = new osg::TextureRectangle;
#else
        _depthTextures[i] = new osg::Texture2D;
#endif
        _depthTextures[i]->setTextureSize(_texWidth, _texHeight);

        _depthTextures[i]->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);
        _depthTextures[i]->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);
        _depthTextures[i]->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_BORDER);
        _depthTextures[i]->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_BORDER);

#ifdef USE_PACKED_DEPTH_STENCIL
        _depthTextures[i]->setInternalFormat(GL_DEPTH24_STENCIL8_EXT);
        _depthTextures[i]->setSourceFormat(GL_DEPTH_STENCIL_EXT);
        _depthTextures[i]->setSourceType(GL_UNSIGNED_INT_24_8_EXT);
#else
        _depthTextures[i]->setInternalFormat(GL_DEPTH_COMPONENT);
#endif

        _depthTextures[i]->setShadowComparison(true);
        _depthTextures[i]->setShadowAmbient(0.0); // The r value if the test fails
        _depthTextures[i]->setShadowCompareFunc(osg::Texture::GREATER);
        _depthTextures[i]->setShadowTextureMode(osg::Texture::INTENSITY);
    }

    // create the cameras for the individual depth peel layers
    _colorTextures.clear();
    _colorTextures.resize(_numPasses);
    for (unsigned int i = 0; i < _numPasses; ++i) {

        // create textures for the color buffers
#ifdef USE_TEXTURE_RECTANGLE
        osg::ref_ptr<osg::TextureRectangle> colorTexture = new osg::TextureRectangle;
#else
        osg::ref_ptr<osg::Texture2D> colorTexture = new osg::Texture2D;
#endif
        colorTexture->setTextureSize(_texWidth, _texHeight);
        colorTexture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);
        colorTexture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);
        colorTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_BORDER);
        colorTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_BORDER);
        colorTexture->setInternalFormat(GL_RGBA);

        _colorTextures[i] = colorTexture;
    }

    // create some uniform and cull callback objects
    osg::Uniform *depthOff = new osg::Uniform("depthtest", (bool)false);
    osg::Uniform *depthOn  = new osg::Uniform("depthtest", (bool)true);
    CullCallback *ccb      = new CullCallback(_texUnit, _texWidth, _texHeight, _offsetValue);

    // create a node for solid model rendering
    osg::Group *pre_solidNode = new osg::Group;
    pre_solidNode->addChild(_solidscene.get());

    // create a node for non depth peeled transparent rendering (topmost layer)
    osg::Group *transparentNodeNoPeel = new osg::Group;
    transparentNodeNoPeel->addChild(_transparentscene.get());
    transparentNodeNoPeel->getOrCreateStateSet()->addUniform(depthOff);
    transparentNodeNoPeel->getOrCreateStateSet()->setRenderBinDetails(99, "RenderBin", osg::StateSet::OVERRIDE_RENDERBIN_DETAILS);

    // create a node for depth peeled transparent rendering (any layers below).
    osg::TexGenNode* transparentNodePeel = new osg::TexGenNode;
    transparentNodePeel->setReferenceFrame(osg::TexGenNode::ABSOLUTE_RF);
    transparentNodePeel->setTextureUnit(_texUnit);
    transparentNodePeel->getTexGen()->setMode(osg::TexGen::EYE_LINEAR);
    transparentNodePeel->addChild(_transparentscene.get());
    transparentNodePeel->getOrCreateStateSet()->addUniform(depthOn);
    transparentNodePeel->getOrCreateStateSet()->setRenderBinDetails(99, "RenderBin", osg::StateSet::OVERRIDE_RENDERBIN_DETAILS);

    // only render fragments that are not completely transparent
    transparentNodePeel->getOrCreateStateSet()->setAttributeAndModes(new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.01),
                                    osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);

    // generate texcoords for the depth texture, supporting the fixed function pipeline
    transparentNodePeel->getOrCreateStateSet()->setTextureMode(_texUnit, GL_TEXTURE_GEN_S, osg::StateAttribute::ON);
    transparentNodePeel->getOrCreateStateSet()->setTextureMode(_texUnit, GL_TEXTURE_GEN_T, osg::StateAttribute::ON);
    transparentNodePeel->getOrCreateStateSet()->setTextureMode(_texUnit, GL_TEXTURE_GEN_R, osg::StateAttribute::ON);
    transparentNodePeel->getOrCreateStateSet()->setTextureMode(_texUnit, GL_TEXTURE_GEN_Q, osg::StateAttribute::ON);

    // use two FBOs, one for solid geometry - the other one for the transparency passes
    // depth and color attachments will be switched as needed.
    osg::ref_ptr<osg::FrameBufferObject> fbos[2] = {new osg::FrameBufferObject(), new osg::FrameBufferObject()};

    // create the cameras for the individual depth peel layers
    for (unsigned int i = 0; i < _numPasses; ++i) {

        // get the pointers to the required fbo, color and depth textures for each camera instance
        // we perform ping ponging between two depth textures
        osg::FrameBufferObject *fbo0 = (i >= 1) ? fbos[0].get() : NULL;
        osg::FrameBufferObject *fbo  = (i >= 1) ? fbos[1].get() : fbos[0].get();
        osg::Texture *colorTexture     = _colorTextures[i].get();
        osg::Texture *depthTexture     = (i >= 1) ? _depthTextures[1+(i-1)%2].get() : _depthTextures[i].get();
        osg::Texture *prevDepthTexture = (i >= 2) ? _depthTextures[1+(i-2)%2].get() : NULL;

        // all our peeling layer cameras are post render
        osg::Camera* camera = new osg::Camera;
        camera->setDataVariance(osg::Object::DYNAMIC);
        camera->setInheritanceMask(osg::Camera::ALL_VARIABLES);
        camera->setRenderOrder(osg::Camera::POST_RENDER, i);
        camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
        camera->setClearColor(osg::Vec4f(0, 0, 0, 0));
        camera->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR);
        camera->setPreDrawCallback(new PreDrawFBOCallback(fbo, fbo0, _texWidth, _texHeight, depthTexture, colorTexture));
        camera->setPostDrawCallback(new PostDrawFBOCallback(i == _numPasses - 1));
        camera->setDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
        camera->setReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
        camera->setAllowEventFocus(false);

        // the peeled layers are rendered with blending forced off
        // and the depth buffer is directly taken from camera 0 via framebuffer blit
        if (i > 0) {
            camera->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);
            camera->setClearMask(GL_COLOR_BUFFER_BIT);
        } else {
            // camera 0 has to clear both the depth and color buffers
            camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
        }

        // add the correct geometry for each pass.
        // the peeling passes also need read access to prevDepthTexture and a cull callback
        if (0 == i) {        // solid geometry
            camera->addChild(pre_solidNode);
        } else if (1 == i) { // topmost layer peeling pass
            camera->addChild(transparentNodeNoPeel);
        } else {             // behind layers peeling passes
            camera->addChild(transparentNodePeel);
            // set depth (shadow) texture for depth peeling and add a cull callback
            camera->getOrCreateStateSet()->setTextureAttributeAndModes(_texUnit, prevDepthTexture);
            camera->addCullCallback(ccb);
        }
        _root->addChild(camera);
    }

    // create the composite camera that blends the peeled layers into the final scene
    _compositeCamera = new osg::Camera;
    _compositeCamera->setDataVariance(osg::Object::DYNAMIC);
    _compositeCamera->setInheritanceMask(osg::Camera::READ_BUFFER | osg::Camera::DRAW_BUFFER);
    _compositeCamera->setRenderOrder(osg::Camera::POST_RENDER, _numPasses);
    _compositeCamera->setComputeNearFarMode(osg::Camera::COMPUTE_NEAR_FAR_USING_PRIMITIVES);
    _compositeCamera->setClearMask(0);
    _compositeCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
    _compositeCamera->setViewMatrix(osg::Matrix());
    _compositeCamera->setProjectionMatrix(osg::Matrix::ortho2D(0, 1, 0, 1));
    _compositeCamera->setCullCallback(new CullCallback(0, _texWidth, _texHeight, 0));
    osg::StateSet* stateSet = _compositeCamera->getOrCreateStateSet();
    stateSet->setRenderBinDetails(100, "TraversalOrderBin", osg::StateSet::OVERRIDE_RENDERBIN_DETAILS);
    _root->addChild(_compositeCamera.get());

    // solid geometry is blended first, transparency layers are blended in back to front order.
    // this order is achieved by rendering using a TraversalOrderBin (see camera stateset).
    for (unsigned int i = _numPasses; i > 0; --i) {
        osg::Node* geode = createQuad(i%_numPasses, numTiles);
        osg::StateSet *stateSet = geode->getOrCreateStateSet();
        stateSet->setTextureAttributeAndModes(0, _colorTextures[i%_numPasses].get(), osg::StateAttribute::ON);
        stateSet->setAttribute(new osg::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA), osg::StateAttribute::ON);
        stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
        stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
        osg::Depth* depth = new osg::Depth;
        depth->setWriteMask( false );
        stateSet->setAttributeAndModes( depth, osg::StateAttribute::ON );
        stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
        _compositeCamera->addChild(geode);
    }
}

DepthPeeling::DepthPeeling(unsigned int width, unsigned int height) :
    _numPasses(9),
    _texUnit(2),
    _texWidth(width),
    _texHeight(height),
    _showAllLayers(false),
    _offsetValue(8),
    _root(new osg::Group),
    _solidscene(new osg::Group),
    _transparentscene(new osg::Group)
{
    createPeeling();
}

void DepthPeeling::setSolidScene(osg::Node* scene)
{
    _solidscene->removeChildren(0, _solidscene->getNumChildren());
    _solidscene->addChild(scene);
}

void DepthPeeling::setTransparentScene(osg::Node* scene)
{
    _transparentscene->removeChildren(0, _transparentscene->getNumChildren());
    _transparentscene->addChild(scene);
}

osg::Node* DepthPeeling::getRoot()
{
    return _root.get();
}

void DepthPeeling::resize(int width, int height)
{
#ifdef USE_TEXTURE_RECTANGLE
    for (unsigned int i = 0; i < 3; ++i)
        _depthTextures[i]->setTextureSize(width, height);
    for (unsigned int i = 0; i < _colorTextures.size(); ++i)
        _colorTextures[i]->setTextureSize(width, height);
    _texWidth = width;
    _texHeight = height;
#else
#ifndef USE_NON_POWER_OF_TWO_TEXTURE
    width = nextPowerOfTwo(width);
    height = nextPowerOfTwo(height);
#endif
    _depthTextures[0]->setTextureSize(width, height);
    _depthTextures[1]->setTextureSize(width, height);
    for (unsigned int i = 0; i < _colorTextures.size(); ++i)
        _colorTextures[i]->setTextureSize(width, height);
    _texWidth = width;
    _texHeight = height;
#endif
    createPeeling();
}

void DepthPeeling::setNumPasses(unsigned int numPasses)
{
    if (numPasses == _numPasses)
        return;
    if (numPasses == unsigned(-1))
        return;
    _numPasses = numPasses;
    createPeeling();
}
unsigned int DepthPeeling::getNumPasses() const
{
    return _numPasses;
}

void DepthPeeling::setTexUnit(unsigned int texUnit)
{
    if (texUnit == _texUnit)
        return;
    _texUnit = texUnit;
    createPeeling();
}

void DepthPeeling::setShowAllLayers(bool showAllLayers)
{
    if (showAllLayers == _showAllLayers)
        return;
    _showAllLayers = showAllLayers;
    createPeeling();
}
bool DepthPeeling::getShowAllLayers() const
{
    return _showAllLayers;
}

void DepthPeeling::setOffsetValue(unsigned int offsetValue)
{
    if (offsetValue == _offsetValue)
        return;
    _offsetValue = offsetValue;
    createPeeling();
}
unsigned int DepthPeeling::getOffsetValue() const
{
    return _offsetValue;
}


DepthPeeling::EventHandler::EventHandler(DepthPeeling* depthPeeling) :
    _depthPeeling(depthPeeling)
{ }


/** Handle events, return true if handled, false otherwise. */
bool DepthPeeling::EventHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&, osg::Object*, osg::NodeVisitor*)
{
    if (ea.getEventType() == osgGA::GUIEventAdapter::RESIZE) {
        _depthPeeling->resize(ea.getWindowWidth(), ea.getWindowHeight());
        return true;
    }

    if (ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN) {
        switch (ea.getKey()) {
        case 'm':
            _depthPeeling->setNumPasses(_depthPeeling->getNumPasses() + 1);
            return true;
        case 'n':
            _depthPeeling->setNumPasses(_depthPeeling->getNumPasses() - 1);
            return true;
        case 'p':
            _depthPeeling->setOffsetValue(_depthPeeling->getOffsetValue() + 1);
            return true;
        case 'o':
            _depthPeeling->setOffsetValue(_depthPeeling->getOffsetValue() - 1);
            return true;
        case 'l':
            _depthPeeling->setShowAllLayers(!_depthPeeling->getShowAllLayers());
            return true;
        default:
            return false;
        };
    }

    return false;
}