Skip to content
This repository
Browse code

Added support for controlling the extents of the volume rendering by …

…nesting the hull underneath VolumeTile. Currently only supported by the new osgVolume::MultipassTechnique
  • Loading branch information...
commit 894fbe90c47bbadd083b1158c876aff1b08fa990 1 parent 374e5e5
Robert OSFIELD authored
7 include/osgVolume/MultipassTechnique
@@ -33,7 +33,9 @@ class OSGVOLUME_EXPORT MultipassTechnique : public VolumeTechnique
33 33
34 34 virtual void update(osgUtil::UpdateVisitor* nv);
35 35
36   - virtual void cull(osgUtil::CullVisitor* nv);
  36 + virtual void backfaceSubgraphCullTraversal(osgUtil::CullVisitor* cv);
  37 +
  38 + virtual void cull(osgUtil::CullVisitor* cv);
37 39
38 40 /** Clean scene graph from any terrain technique specific nodes.*/
39 41 virtual void cleanSceneGraph();
@@ -53,6 +55,7 @@ class OSGVOLUME_EXPORT MultipassTechnique : public VolumeTechnique
53 55 ModelViewMatrixMap _modelViewMatrixMap;
54 56
55 57 osg::ref_ptr<osg::StateSet> _whenMovingStateSet;
  58 + osg::ref_ptr<osg::StateSet> _volumeRenderStateSet;
56 59
57 60 osg::StateSet* createStateSet(osg::StateSet* statesetPrototype, osg::Program* programPrototype, osg::Shader* shaderToAdd1=0, osg::Shader* shaderToAdd2=0);
58 61
@@ -69,6 +72,8 @@ class OSGVOLUME_EXPORT MultipassTechnique : public VolumeTechnique
69 72
70 73 typedef std::map<int, osg::ref_ptr<osg::StateSet> > StateSetMap;
71 74 StateSetMap _stateSetMap;
  75 +
  76 + osg::ref_ptr<osg::StateSet> _frontFaceStateSet;
72 77 };
73 78
74 79 }
17 include/osgVolume/VolumeScene
@@ -36,12 +36,6 @@ class OSGVOLUME_EXPORT VolumeScene : public osg::Group
36 36
37 37 virtual void traverse(osg::NodeVisitor& nv);
38 38
39   - void tileVisited(osg::NodeVisitor* nv, osgVolume::VolumeTile* tile);
40   -
41   - protected:
42   -
43   - virtual ~VolumeScene();
44   -
45 39 struct TileData : public osg::Referenced
46 40 {
47 41 TileData() : active(false) {}
@@ -54,8 +48,17 @@ class OSGVOLUME_EXPORT VolumeScene : public osg::Group
54 48
55 49 osg::ref_ptr<osg::Texture2D> depthTexture;
56 50 osg::ref_ptr<osg::Camera> rttCamera;
57   - osg::ref_ptr<osg::Camera> stateset;
  51 + osg::ref_ptr<osg::StateSet> stateset;
  52 + osg::ref_ptr<osg::Uniform> texgenUniform;
58 53 };
  54 +
  55 + TileData* tileVisited(osgUtil::CullVisitor* cv, osgVolume::VolumeTile* tile);
  56 + TileData* getTileData(osgUtil::CullVisitor* cv, osgVolume::VolumeTile* tile);
  57 +
  58 + protected:
  59 +
  60 + virtual ~VolumeScene();
  61 +
59 62 typedef std::map< VolumeTile*, osg::ref_ptr<TileData> > Tiles;
60 63
61 64 class ViewData : public osg::Referenced
166 src/osgVolume/MultipassTechnique.cpp
@@ -295,15 +295,17 @@ void MultipassTechnique::init()
295 295 OSG_NOTICE<<"MultipassTechnique::init() : geometryMatrix = "<<geometryMatrix<<std::endl;
296 296 OSG_NOTICE<<"MultipassTechnique::init() : imageMatrix = "<<imageMatrix<<std::endl;
297 297
298   - osg::ref_ptr<osg::StateSet> stateset = _transform->getOrCreateStateSet();
  298 + osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet;
  299 + _volumeRenderStateSet = stateset;
299 300
300 301 unsigned int texgenTextureUnit = 0;
301   - unsigned int volumeTextureUnit = 2;
  302 + unsigned int volumeTextureUnit = 3;
302 303
303 304 // set up uniforms
304 305 {
305 306 stateset->addUniform(new osg::Uniform("colorTexture",0));
306 307 stateset->addUniform(new osg::Uniform("depthTexture",1));
  308 + stateset->addUniform(new osg::Uniform("frontFaceDepthTexture",2));
307 309
308 310 stateset->setMode(GL_ALPHA_TEST,osg::StateAttribute::ON);
309 311
@@ -355,7 +357,7 @@ void MultipassTechnique::init()
355 357 }
356 358 }
357 359
358   - stateset->setTextureAttributeAndModes(texgenTextureUnit, texgen.get(), osg::StateAttribute::ON);
  360 + stateset->setTextureAttributeAndModes(texgenTextureUnit, texgen.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
359 361 }
360 362
361 363
@@ -407,7 +409,7 @@ void MultipassTechnique::init()
407 409 }
408 410 texture3D->setImage(image_3d);
409 411
410   - stateset->setTextureAttributeAndModes(volumeTextureUnit, texture3D, osg::StateAttribute::ON);
  412 + stateset->setTextureAttributeAndModes(volumeTextureUnit, texture3D, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
411 413
412 414 osg::ref_ptr<osg::Uniform> baseTextureSampler = new osg::Uniform("volumeTexture", int(volumeTextureUnit));
413 415 stateset->addUniform(baseTextureSampler.get());
@@ -446,7 +448,7 @@ void MultipassTechnique::init()
446 448
447 449 unsigned int transferFunctionTextureUnit = volumeTextureUnit+1;
448 450
449   - stateset->setTextureAttributeAndModes(transferFunctionTextureUnit, tf_texture.get(), osg::StateAttribute::ON);
  451 + stateset->setTextureAttributeAndModes(transferFunctionTextureUnit, tf_texture.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
450 452 stateset->addUniform(new osg::Uniform("tfTexture",int(transferFunctionTextureUnit)));
451 453 stateset->addUniform(new osg::Uniform("tfOffset",tfOffset));
452 454 stateset->addUniform(new osg::Uniform("tfScale",tfScale));
@@ -487,12 +489,13 @@ void MultipassTechnique::init()
487 489 #endif
488 490
489 491
490   - osg::ref_ptr<osg::Shader> back_main_fragmentShader = osgDB::readRefShaderFile(osg::Shader::FRAGMENT, "shaders/volume_multipass_back.frag");
  492 + osg::ref_ptr<osg::Shader> back_main_fragmentShader = osgDB::readRefShaderFile(osg::Shader::FRAGMENT, "shaders/volume_multipass_back_with_front_depthtexture.frag");;
  493 + //osg::ref_ptr<osg::Shader> back_main_fragmentShader = osgDB::readRefShaderFile(osg::Shader::FRAGMENT, "shaders/volume_multipass_back.frag");
491 494 #if 0
492 495 if (!back_main_fragmentShader)
493 496 {
494   - #include "Shaders/volume_multipass_back_frag.cpp"
495   - back_main_fragmentShader = new osg::Shader(osg::Shader::VERTEX, volume_multipass_back_frag));
  497 + #include "Shaders/shaders/volume_multipass_back_with_front_depthtexture_frag.cpp"
  498 + back_main_fragmentShader = new osg::Shader(osg::Shader::VERTEX, volume_multipass_back_with_front_depthtexture_frag));
496 499 }
497 500 #endif
498 501
@@ -502,7 +505,7 @@ void MultipassTechnique::init()
502 505 osg::ref_ptr<osg::StateSet> front_stateset_prototype = new osg::StateSet;
503 506 osg::ref_ptr<osg::Program> front_program_prototype = new osg::Program;
504 507 {
505   - front_stateset_prototype->setAttributeAndModes(front_CullFace.get(), osg::StateAttribute::ON);
  508 + front_stateset_prototype->setAttributeAndModes(front_CullFace.get(), osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
506 509
507 510 front_program_prototype->addShader(main_vertexShader.get());
508 511 front_program_prototype->addShader(front_main_fragmentShader.get());
@@ -512,13 +515,28 @@ void MultipassTechnique::init()
512 515 osg::ref_ptr<osg::StateSet> back_stateset_prototype = new osg::StateSet;
513 516 osg::ref_ptr<osg::Program> back_program_prototype = new osg::Program;
514 517 {
515   - back_stateset_prototype->setAttributeAndModes(back_CullFace.get(), osg::StateAttribute::ON);
  518 + back_stateset_prototype->setAttributeAndModes(back_CullFace.get(), osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
516 519
517 520 back_program_prototype->addShader(main_vertexShader.get());
518 521 back_program_prototype->addShader(back_main_fragmentShader.get());
519 522 back_program_prototype->addShader(computeRayColorShader.get());
520 523 }
521 524
  525 +
  526 + // set up the rendering of the front face
  527 + {
  528 + _frontFaceStateSet = new osg::StateSet;
  529 +
  530 + // cull only the bac faces so we write only the front
  531 + _frontFaceStateSet->setAttributeAndModes(front_CullFace.get(), osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
  532 +
  533 + // set up the front falce
  534 + osg::ref_ptr<osg::Program> program = new osg::Program;
  535 + program->addShader(main_vertexShader.get());
  536 + _frontFaceStateSet->setAttribute(program.get(), osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
  537 + }
  538 +
  539 +
522 540 // STANDARD_SHADERS
523 541 {
524 542 // STANDARD_SHADERS without TransferFunction
@@ -690,7 +708,6 @@ void MultipassTechnique::init()
690 708 if (cpv._sampleRatioWhenMovingProperty.valid())
691 709 {
692 710 _whenMovingStateSet = new osg::StateSet;
693   - //_whenMovingStateSet->setTextureAttributeAndModes(volumeTextureUnit, new osg::TexEnvFilter(1.0));
694 711 _whenMovingStateSet->addUniform(cpv._sampleRatioWhenMovingProperty->getUniform(), osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
695 712 }
696 713
@@ -701,14 +718,89 @@ void MultipassTechnique::update(osgUtil::UpdateVisitor* /*uv*/)
701 718 // OSG_NOTICE<<"MultipassTechnique:update(osgUtil::UpdateVisitor* nv):"<<std::endl;
702 719 }
703 720
  721 +void MultipassTechnique::backfaceSubgraphCullTraversal(osgUtil::CullVisitor* cv)
  722 +{
  723 + if (!cv) return;
  724 +
  725 + cv->pushStateSet(_frontFaceStateSet.get());
  726 +
  727 + if (getVolumeTile()->getNumChildren()>0)
  728 + {
  729 + getVolumeTile()->osg::Group::traverse(*cv);
  730 + }
  731 + else
  732 + {
  733 + _transform->accept(*cv);
  734 + }
  735 +
  736 + cv->popStateSet();
  737 +}
  738 +
  739 +class RTTBackfaceCameraCullCallback : public osg::NodeCallback
  740 +{
  741 + public:
  742 +
  743 + RTTBackfaceCameraCullCallback(VolumeScene::TileData* tileData, MultipassTechnique* mt):
  744 + _tileData(tileData),
  745 + _mt(mt) {}
  746 +
  747 + virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
  748 + {
  749 + osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
  750 +
  751 + cv->pushProjectionMatrix(_tileData->projectionMatrix);
  752 +
  753 + _mt->backfaceSubgraphCullTraversal(cv);
  754 +
  755 + cv->popProjectionMatrix();
  756 + }
  757 +
  758 + protected:
  759 +
  760 + virtual ~RTTBackfaceCameraCullCallback() {}
  761 +
  762 + osg::observer_ptr<osgVolume::VolumeScene::TileData> _tileData;
  763 + osg::observer_ptr<osgVolume::MultipassTechnique> _mt;
  764 +};
  765 +
704 766 void MultipassTechnique::cull(osgUtil::CullVisitor* cv)
705 767 {
706 768 std::string traversalPass;
707   - bool postTraversal = cv->getUserValue("VolumeSceneTraversal", traversalPass) && traversalPass=="Post";
  769 + bool rttTraversal = cv->getUserValue("VolumeSceneTraversal", traversalPass) && traversalPass=="RenderToTexture";
708 770
709 771 // OSG_NOTICE<<"MultipassTechnique::cull() traversalPass="<<traversalPass<<std::endl;
  772 + osgVolume::VolumeScene* vs = 0;
  773 + osg::NodePath& nodePath = cv->getNodePath();
  774 + for(osg::NodePath::reverse_iterator itr = nodePath.rbegin();
  775 + (itr != nodePath.rend()) && (vs == 0);
  776 + ++itr)
  777 + {
  778 + vs = dynamic_cast<osgVolume::VolumeScene*>(*itr);
  779 + }
710 780
711   - if (postTraversal)
  781 + if (rttTraversal)
  782 + {
  783 + if (vs)
  784 + {
  785 + VolumeScene::TileData* tileData = vs->tileVisited(cv, getVolumeTile());
  786 + if (tileData->rttCamera.valid())
  787 + {
  788 + if (!(tileData->rttCamera->getCullCallback()))
  789 + {
  790 + tileData->rttCamera->setCullCallback(new RTTBackfaceCameraCullCallback(tileData, this));
  791 + }
  792 +
  793 + // traverse RTT Camera
  794 + tileData->rttCamera->accept(*cv);
  795 + }
  796 +
  797 + osg::BoundingBox bb;
  798 + if (_transform.valid()) bb.expandBy(_transform->getBound());
  799 + if (getVolumeTile()->getNumChildren()>0) bb.expandBy(getVolumeTile()->getBound());
  800 + cv->updateCalculatedNearFar(*(cv->getModelViewMatrix()),bb);
  801 + }
  802 + }
  803 + else
712 804 {
713 805
714 806 int shaderMask = 0;
@@ -740,14 +832,27 @@ void MultipassTechnique::cull(osgUtil::CullVisitor* cv)
740 832 }
741 833 }
742 834
743   - int shaderMaskFront = shaderMask | FRONT_SHADERS;
  835 + //int shaderMaskFront = shaderMask | FRONT_SHADERS;
744 836 int shaderMaskBack = shaderMask | BACK_SHADERS;
745 837
746 838 // OSG_NOTICE<<"shaderMaskFront "<<shaderMaskFront<<std::endl;
747 839 // OSG_NOTICE<<"shaderMaskBack "<<shaderMaskBack<<std::endl;
748 840
  841 + if (vs)
  842 + {
  843 + VolumeScene::TileData* tileData = vs->getTileData(cv, getVolumeTile());
  844 + if (tileData)
  845 + {
  846 + Locator* layerLocator = _volumeTile->getLayer()->getLocator();
  847 + osg::Matrix imv = layerLocator->getTransform() * (*(cv->getModelViewMatrix()));
  848 + osg::Matrix inverse_imv;
  849 + inverse_imv.invert(imv);
  850 + tileData->texgenUniform->set(osg::Matrixf(inverse_imv));
  851 + }
  852 + }
749 853
750   - osg::ref_ptr<osg::StateSet> front_stateset = _stateSetMap[shaderMaskFront];
  854 +
  855 + //osg::ref_ptr<osg::StateSet> front_stateset = _stateSetMap[shaderMaskFront];
751 856 osg::ref_ptr<osg::StateSet> back_stateset = _stateSetMap[shaderMaskBack];
752 857 osg::ref_ptr<osg::StateSet> moving_stateset = (_whenMovingStateSet.valid() && isMoving(cv)) ? _whenMovingStateSet : 0;
753 858
@@ -757,6 +862,7 @@ void MultipassTechnique::cull(osgUtil::CullVisitor* cv)
757 862 cv->pushStateSet(moving_stateset.get());
758 863 }
759 864
  865 +#if 0
760 866 if (front_stateset.valid())
761 867 {
762 868 // OSG_NOTICE<<"Have front stateset"<<std::endl;
@@ -764,12 +870,23 @@ void MultipassTechnique::cull(osgUtil::CullVisitor* cv)
764 870 _transform->accept(*cv);
765 871 cv->popStateSet();
766 872 }
767   -
  873 +#endif
768 874 if (back_stateset.valid())
769 875 {
770 876 // OSG_NOTICE<<"Have back stateset"<<std::endl;
771 877 cv->pushStateSet(back_stateset.get());
772   - _transform->accept(*cv);
  878 + cv->pushStateSet(_volumeRenderStateSet.get());
  879 +
  880 + if (getVolumeTile()->getNumChildren()>0)
  881 + {
  882 + getVolumeTile()->osg::Group::traverse(*cv);
  883 + }
  884 + else
  885 + {
  886 + _transform->accept(*cv);
  887 + }
  888 +
  889 + cv->popStateSet();
773 890 cv->popStateSet();
774 891 }
775 892
@@ -779,21 +896,6 @@ void MultipassTechnique::cull(osgUtil::CullVisitor* cv)
779 896 cv->popStateSet();
780 897 }
781 898 }
782   - else
783   - {
784   - osg::NodePath& nodePath = cv->getNodePath();
785   - for(osg::NodePath::reverse_iterator itr = nodePath.rbegin();
786   - itr != nodePath.rend();
787   - ++itr)
788   - {
789   - osgVolume::VolumeScene* vs = dynamic_cast<osgVolume::VolumeScene*>(*itr);
790   - if (vs)
791   - {
792   - vs->tileVisited(cv, getVolumeTile());
793   - break;
794   - }
795   - }
796   - }
797 899 }
798 900
799 901 void MultipassTechnique::cleanSceneGraph()
589 src/osgVolume/VolumeScene.cpp
@@ -45,7 +45,6 @@ class RTTCameraCullCallback : public osg::NodeCallback
45 45 virtual ~RTTCameraCullCallback() {}
46 46
47 47 osgVolume::VolumeScene* _volumeScene;
48   -
49 48 };
50 49
51 50
@@ -81,356 +80,442 @@ VolumeScene::~VolumeScene()
81 80 {
82 81 }
83 82
84   -void VolumeScene::tileVisited(osg::NodeVisitor* nv, osgVolume::VolumeTile* tile)
  83 +VolumeScene::TileData* VolumeScene::tileVisited(osgUtil::CullVisitor* cv, osgVolume::VolumeTile* tile)
85 84 {
86   - osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
87   - if (cv)
  85 + osg::ref_ptr<ViewData> viewData;
  86 +
  87 + {
  88 + OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_viewDataMapMutex);
  89 + viewData = _viewDataMap[cv];
  90 + }
  91 +
  92 + //osg::BoundingBox bb(0.0f,0.0f,0.0f,1.0f,1.0f,1.0f);
  93 + //cv->updateCalculatedNearFar(*(cv->getModelViewMatrix()),bb);
  94 +
  95 + if (viewData.valid())
88 96 {
89   - osg::ref_ptr<ViewData> viewData;
  97 + int textureWidth = 512;
  98 + int textureHeight = 512;
90 99
  100 + osg::Viewport* viewport = cv->getCurrentRenderStage()->getViewport();
  101 + if (viewport)
91 102 {
92   - OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_viewDataMapMutex);
93   - viewData = _viewDataMap[cv];
  103 + textureWidth = viewport->width();
  104 + textureHeight = viewport->height();
  105 + }
  106 +
  107 + osg::ref_ptr<TileData>& tileData = viewData->_tiles[tile];
  108 + if (!tileData)
  109 + {
  110 + tileData = new TileData;
  111 +
  112 + tileData->depthTexture = new osg::Texture2D;
  113 + tileData->depthTexture->setTextureSize(textureWidth, textureHeight);
  114 + tileData->depthTexture->setInternalFormat(GL_DEPTH_COMPONENT);
  115 + tileData->depthTexture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
  116 + tileData->depthTexture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
  117 + tileData->depthTexture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP_TO_BORDER);
  118 + tileData->depthTexture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP_TO_BORDER);
  119 + tileData->depthTexture->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
  120 +
  121 + tileData->rttCamera = new osg::Camera;
  122 + tileData->rttCamera->attach(osg::Camera::DEPTH_BUFFER, tileData->depthTexture.get());
  123 + tileData->rttCamera->setViewport(0,0,textureWidth,textureHeight);
  124 +
  125 + // clear the depth and colour bufferson each clear.
  126 + tileData->rttCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
  127 +
  128 + // set the camera to render before the main camera.
  129 + tileData->rttCamera->setRenderOrder(osg::Camera::PRE_RENDER);
  130 +
  131 + // tell the camera to use OpenGL frame buffer object where supported.
  132 + tileData->rttCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
  133 +
  134 + tileData->rttCamera->setReferenceFrame(osg::Transform::RELATIVE_RF);
  135 + tileData->rttCamera->setProjectionMatrix(osg::Matrixd::identity());
  136 + tileData->rttCamera->setViewMatrix(osg::Matrixd::identity());
  137 +
  138 + tileData->stateset = new osg::StateSet;
  139 + tileData->stateset->setTextureAttribute(2, tileData->depthTexture.get());
  140 +
  141 + tileData->texgenUniform = new osg::Uniform("texgen2",osg::Matrixf());
  142 + tileData->stateset->addUniform(tileData->texgenUniform);
94 143 }
95 144
96   - if (viewData.valid())
  145 + tileData->active = true;
  146 + tileData->nodePath = cv->getNodePath();
  147 + tileData->projectionMatrix = cv->getProjectionMatrix();
  148 + tileData->modelviewMatrix = cv->getModelViewMatrix();
  149 +
  150 +
  151 + if (tileData->depthTexture.valid())
97 152 {
98   - osg::ref_ptr<TileData>& tileData = viewData->_tiles[tile];
99   - if (!tileData) tileData = new TileData;
100   - tileData->active = true;
101   - tileData->nodePath = cv->getNodePath();
102   - tileData->projectionMatrix = cv->getProjectionMatrix();
103   - tileData->modelviewMatrix = cv->getModelViewMatrix();
  153 + if (tileData->depthTexture->getTextureWidth()!=textureWidth || tileData->depthTexture->getTextureHeight()!=textureHeight)
  154 + {
  155 + OSG_NOTICE<<"Need to change texture size to "<<textureHeight<<", "<<textureWidth<<std::endl;
  156 + tileData->depthTexture->setTextureSize(textureWidth, textureHeight);
  157 + tileData->rttCamera->setViewport(0, 0, textureWidth, textureHeight);
  158 + if (tileData->rttCamera->getRenderingCache())
  159 + {
  160 + tileData->rttCamera->getRenderingCache()->releaseGLObjects(0);
  161 + }
  162 + }
104 163 }
105 164
106   - osg::BoundingBox bb(0.0f,0.0f,0.0f,1.0f,1.0f,1.0f);
107   - cv->updateCalculatedNearFar(*(cv->getModelViewMatrix()),bb);
  165 + return tileData.get();
108 166 }
  167 + return 0;
109 168 }
110 169
  170 +VolumeScene::TileData* VolumeScene::getTileData(osgUtil::CullVisitor* cv, osgVolume::VolumeTile* tile)
  171 +{
  172 + osg::ref_ptr<ViewData> viewData;
  173 + {
  174 + OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_viewDataMapMutex);
  175 + viewData = _viewDataMap[cv];
  176 + }
  177 +
  178 + if (!viewData) return 0;
  179 +
  180 + Tiles::iterator itr = viewData->_tiles.find(tile);
  181 + return (itr != viewData->_tiles.end()) ? itr->second.get() : 0;
  182 +}
111 183
112 184 void VolumeScene::traverse(osg::NodeVisitor& nv)
113 185 {
114 186 osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(&nv);
115   - if (cv)
  187 + if (!cv)
116 188 {
117   - osg::ref_ptr<ViewData> viewData;
118   - bool initializeViewData = false;
119   - {
120   - OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_viewDataMapMutex);
121   - if (!_viewDataMap[cv])
122   - {
123   - _viewDataMap[cv] = new ViewData;
124   - initializeViewData = true;
125   - }
  189 + Group::traverse(nv);
  190 + return;
  191 + }
126 192
127   - viewData = _viewDataMap[cv];
128   - }
  193 + // Save current cull settings
  194 + osg::CullSettings saved_cull_settings(*cv);
129 195
130   - if (initializeViewData)
  196 + // cv->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
  197 +
  198 + osg::ref_ptr<ViewData> viewData;
  199 + bool initializeViewData = false;
  200 + {
  201 + OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_viewDataMapMutex);
  202 + if (!_viewDataMap[cv])
131 203 {
132   - OSG_NOTICE<<"Creating ViewData"<<std::endl;
  204 + _viewDataMap[cv] = new ViewData;
  205 + initializeViewData = true;
  206 + }
133 207
134   - unsigned textureWidth = 512;
135   - unsigned textureHeight = 512;
  208 + viewData = _viewDataMap[cv];
  209 + }
136 210
137   - osg::Viewport* viewport = cv->getCurrentRenderStage()->getViewport();
138   - if (viewport)
139   - {
140   - textureWidth = viewport->width();
141   - textureHeight = viewport->height();
142   - }
  211 + if (initializeViewData)
  212 + {
  213 + OSG_NOTICE<<"Creating ViewData"<<std::endl;
143 214
144   - // set up depth texture
145   - viewData->_depthTexture = new osg::Texture2D;
  215 + unsigned textureWidth = 512;
  216 + unsigned textureHeight = 512;
146 217
147   - viewData->_depthTexture->setTextureSize(textureWidth, textureHeight);
148   - viewData->_depthTexture->setInternalFormat(GL_DEPTH_COMPONENT);
149   - viewData->_depthTexture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
150   - viewData->_depthTexture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
  218 + osg::Viewport* viewport = cv->getCurrentRenderStage()->getViewport();
  219 + if (viewport)
  220 + {
  221 + textureWidth = viewport->width();
  222 + textureHeight = viewport->height();
  223 + }
151 224
152   - viewData->_depthTexture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP_TO_BORDER);
153   - viewData->_depthTexture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP_TO_BORDER);
154   - viewData->_depthTexture->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
  225 + // set up depth texture
  226 + viewData->_depthTexture = new osg::Texture2D;
155 227
156   - // set up color texture
157   - viewData->_colorTexture = new osg::Texture2D;
  228 + viewData->_depthTexture->setTextureSize(textureWidth, textureHeight);
  229 + viewData->_depthTexture->setInternalFormat(GL_DEPTH_COMPONENT);
  230 + viewData->_depthTexture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
  231 + viewData->_depthTexture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
158 232
159   - viewData->_colorTexture->setTextureSize(textureWidth, textureHeight);
160   - viewData->_colorTexture->setInternalFormat(GL_RGBA);
161   - viewData->_colorTexture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
162   - viewData->_colorTexture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
  233 + viewData->_depthTexture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP_TO_BORDER);
  234 + viewData->_depthTexture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP_TO_BORDER);
  235 + viewData->_depthTexture->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
163 236
164   - viewData->_colorTexture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP_TO_EDGE);
165   - viewData->_colorTexture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP_TO_EDGE);
  237 + // set up color texture
  238 + viewData->_colorTexture = new osg::Texture2D;
166 239
  240 + viewData->_colorTexture->setTextureSize(textureWidth, textureHeight);
  241 + viewData->_colorTexture->setInternalFormat(GL_RGBA);
  242 + viewData->_colorTexture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
  243 + viewData->_colorTexture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
167 244
168   - // set up the RTT Camera to capture the main scene to a color and depth texture that can be used in post processing
169   - viewData->_rttCamera = new osg::Camera;
170   - viewData->_rttCamera->attach(osg::Camera::DEPTH_BUFFER, viewData->_depthTexture.get());
171   - viewData->_rttCamera->attach(osg::Camera::COLOR_BUFFER, viewData->_colorTexture.get());
172   - viewData->_rttCamera->setCullCallback(new RTTCameraCullCallback(this));
173   - viewData->_rttCamera->setViewport(0,0,textureWidth,textureHeight);
  245 + viewData->_colorTexture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP_TO_EDGE);
  246 + viewData->_colorTexture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP_TO_EDGE);
174 247
175   - // clear the depth and colour bufferson each clear.
176   - viewData->_rttCamera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
177 248
178   - // set the camera to render before the main camera.
179   - viewData->_rttCamera->setRenderOrder(osg::Camera::PRE_RENDER);
  249 + // set up the RTT Camera to capture the main scene to a color and depth texture that can be used in post processing
  250 + viewData->_rttCamera = new osg::Camera;
  251 + viewData->_rttCamera->attach(osg::Camera::DEPTH_BUFFER, viewData->_depthTexture.get());
  252 + viewData->_rttCamera->attach(osg::Camera::COLOR_BUFFER, viewData->_colorTexture.get());
  253 + viewData->_rttCamera->setCullCallback(new RTTCameraCullCallback(this));
  254 + viewData->_rttCamera->setViewport(0,0,textureWidth,textureHeight);
180 255
181   - // tell the camera to use OpenGL frame buffer object where supported.
182   - viewData->_rttCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
  256 + // clear the depth and colour bufferson each clear.
  257 + viewData->_rttCamera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
183 258
  259 + // set the camera to render before the main camera.
  260 + viewData->_rttCamera->setRenderOrder(osg::Camera::PRE_RENDER);
184 261
185   - viewData->_rttCamera->setReferenceFrame(osg::Transform::RELATIVE_RF);
186   - viewData->_rttCamera->setProjectionMatrix(osg::Matrixd::identity());
187   - viewData->_rttCamera->setViewMatrix(osg::Matrixd::identity());
  262 + // tell the camera to use OpenGL frame buffer object where supported.
  263 + viewData->_rttCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
188 264
189   - // create mesh for rendering the RTT textures onto the screen
190   - osg::ref_ptr<osg::Geode> geode = new osg::Geode;
191   - geode->setCullingActive(false);
192 265
193   - viewData->_backdropSubgraph = geode;
194   - //geode->addDrawable(osg::createTexturedQuadGeometry(osg::Vec3(-1.0f,-1.0f,-1.0f),osg::Vec3(2.0f,0.0f,-1.0f),osg::Vec3(0.0f,2.0f,-1.0f)));
  266 + viewData->_rttCamera->setReferenceFrame(osg::Transform::RELATIVE_RF);
  267 + viewData->_rttCamera->setProjectionMatrix(osg::Matrixd::identity());
  268 + viewData->_rttCamera->setViewMatrix(osg::Matrixd::identity());
195 269
196   - viewData->_geometry = new osg::Geometry;
197   - geode->addDrawable(viewData->_geometry.get());
  270 + // create mesh for rendering the RTT textures onto the screen
  271 + osg::ref_ptr<osg::Geode> geode = new osg::Geode;
  272 + geode->setCullingActive(false);
198 273
199   - viewData->_geometry->setUseDisplayList(false);
200   - viewData->_geometry->setUseVertexBufferObjects(false);
  274 + viewData->_backdropSubgraph = geode;
  275 + //geode->addDrawable(osg::createTexturedQuadGeometry(osg::Vec3(-1.0f,-1.0f,-1.0f),osg::Vec3(2.0f,0.0f,-1.0f),osg::Vec3(0.0f,2.0f,-1.0f)));
201 276
202   - viewData->_vertices = new osg::Vec3Array(4);
203   - viewData->_geometry->setVertexArray(viewData->_vertices.get());
  277 + viewData->_geometry = new osg::Geometry;
  278 + geode->addDrawable(viewData->_geometry.get());
204 279
205   - osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array(1);
206   - (*colors)[0].set(1.0f,1.0f,1.0f,1.0f);
207   - viewData->_geometry->setColorArray(colors.get(), osg::Array::BIND_OVERALL);
  280 + viewData->_geometry->setUseDisplayList(false);
  281 + viewData->_geometry->setUseVertexBufferObjects(false);
208 282
209   - osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array(4);
210   - (*texcoords)[0].set(0.0f,1.0f);
211   - (*texcoords)[1].set(0.0f,0.0f);
212   - (*texcoords)[2].set(1.0f,1.0f);
213   - (*texcoords)[3].set(1.0f,0.0f);
214   - viewData->_geometry->setTexCoordArray(0, texcoords.get(), osg::Array::BIND_PER_VERTEX);
  283 + viewData->_vertices = new osg::Vec3Array(4);
  284 + viewData->_geometry->setVertexArray(viewData->_vertices.get());
215 285
216   - viewData->_geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_STRIP,0,4));
  286 + osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array(1);
  287 + (*colors)[0].set(1.0f,1.0f,1.0f,1.0f);
  288 + viewData->_geometry->setColorArray(colors.get(), osg::Array::BIND_OVERALL);
217 289
  290 + osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array(4);
  291 + (*texcoords)[0].set(0.0f,1.0f);
  292 + (*texcoords)[1].set(0.0f,0.0f);
  293 + (*texcoords)[2].set(1.0f,1.0f);
  294 + (*texcoords)[3].set(1.0f,0.0f);
  295 + viewData->_geometry->setTexCoordArray(0, texcoords.get(), osg::Array::BIND_PER_VERTEX);
218 296
219   - osg::ref_ptr<osg::StateSet> stateset = viewData->_geometry->getOrCreateStateSet();
  297 + viewData->_geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_STRIP,0,4));
220 298
221   - stateset->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
222   - stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
223   - stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
224   - stateset->setRenderBinDetails(10,"DepthSortedBin");
225 299
226   - osg::ref_ptr<osg::Program> program = new osg::Program;
227   - stateset->setAttribute(program);
  300 + osg::ref_ptr<osg::StateSet> stateset = viewData->_geometry->getOrCreateStateSet();
228 301
229   - // get vertex shaders from source
230   - osg::ref_ptr<osg::Shader> vertexShader = osgDB::readRefShaderFile(osg::Shader::VERTEX, "shaders/volume_color_depth.vert");
231   - if (vertexShader.valid())
232   - {
233   - program->addShader(vertexShader.get());
234   - }
  302 + stateset->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
  303 + stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
  304 + stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
  305 + stateset->setRenderBinDetails(10,"DepthSortedBin");
  306 +
  307 + osg::ref_ptr<osg::Program> program = new osg::Program;
  308 + stateset->setAttribute(program);
  309 +
  310 + // get vertex shaders from source
  311 + osg::ref_ptr<osg::Shader> vertexShader = osgDB::readRefShaderFile(osg::Shader::VERTEX, "shaders/volume_color_depth.vert");
  312 + if (vertexShader.valid())
  313 + {
  314 + program->addShader(vertexShader.get());
  315 + }
235 316 #if 0
236   - else
237   - {
238   - #include "Shaders/volume_color_depth_vert.cpp"
239   - program->addShader(new osg::Shader(osg::Shader::VERTEX, volume_color_depth_vert));
240   - }
  317 + else
  318 + {
  319 + #include "Shaders/volume_color_depth_vert.cpp"
  320 + program->addShader(new osg::Shader(osg::Shader::VERTEX, volume_color_depth_vert));
  321 + }
241 322 #endif
242   - // get fragment shaders from source
243   - osg::ref_ptr<osg::Shader> fragmentShader = osgDB::readRefShaderFile(osg::Shader::FRAGMENT, "shaders/volume_color_depth.frag");
244   - if (fragmentShader.valid())
245   - {
246   - program->addShader(fragmentShader.get());
247   - }
  323 + // get fragment shaders from source
  324 + osg::ref_ptr<osg::Shader> fragmentShader = osgDB::readRefShaderFile(osg::Shader::FRAGMENT, "shaders/volume_color_depth.frag");
  325 + if (fragmentShader.valid())
  326 + {
  327 + program->addShader(fragmentShader.get());
  328 + }
248 329 #if 0
249   - else
250   - {
251   - #include "Shaders/volume_color_depth_frag.cpp"
252   - program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_color_depth_frag));
253   - }
  330 + else
  331 + {
  332 + #include "Shaders/volume_color_depth_frag.cpp"
  333 + program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_color_depth_frag));
  334 + }
254 335 #endif
255   - viewData->_stateset = new osg::StateSet;
256   - viewData->_stateset->addUniform(new osg::Uniform("colorTexture",0));
257   - viewData->_stateset->addUniform(new osg::Uniform("depthTexture",1));
  336 + viewData->_stateset = new osg::StateSet;
  337 + viewData->_stateset->addUniform(new osg::Uniform("colorTexture",0));
  338 + viewData->_stateset->addUniform(new osg::Uniform("depthTexture",1));
258 339
259   - viewData->_stateset->setTextureAttributeAndModes(0, viewData->_colorTexture.get(), osg::StateAttribute::ON);
260   - viewData->_stateset->setTextureAttributeAndModes(1, viewData->_depthTexture.get(), osg::StateAttribute::ON);
  340 + viewData->_stateset->setTextureAttributeAndModes(0, viewData->_colorTexture.get(), osg::StateAttribute::ON);
  341 + viewData->_stateset->setTextureAttributeAndModes(1, viewData->_depthTexture.get(), osg::StateAttribute::ON);
261 342
262   - viewData->_viewportDimensionsUniform = new osg::Uniform("viewportDimensions",osg::Vec4(0.0,0.0,1280.0,1024.0));
263   - viewData->_stateset->addUniform(viewData->_viewportDimensionsUniform.get());
  343 + viewData->_viewportDimensionsUniform = new osg::Uniform("viewportDimensions",osg::Vec4(0.0,0.0,1280.0,1024.0));
  344 + viewData->_stateset->addUniform(viewData->_viewportDimensionsUniform.get());
264 345
265   - geode->setStateSet(viewData->_stateset.get());
  346 + geode->setStateSet(viewData->_stateset.get());
266 347
267   - }
268   - else
269   - {
270   - // OSG_NOTICE<<"Reusing ViewData"<<std::endl;
  348 + }
  349 + else
  350 + {
  351 + // OSG_NOTICE<<"Reusing ViewData"<<std::endl;
271 352
272   - }
  353 + }
273 354
274   - osg::Matrix projectionMatrix = *(cv->getProjectionMatrix());
275   - osg::Matrix modelviewMatrix = *(cv->getModelViewMatrix());
  355 + osg::Matrix projectionMatrix = *(cv->getProjectionMatrix());
  356 + osg::Matrix modelviewMatrix = *(cv->getModelViewMatrix());
276 357
277 358
278   - // new frame so need to clear last frames log of VolumeTiles
279   - viewData->clearTiles();
  359 + // new frame so need to clear last frames log of VolumeTiles
  360 + viewData->clearTiles();
280 361
281   - osg::Viewport* viewport = cv->getCurrentRenderStage()->getViewport();
282   - if (viewport)
283   - {
284   - viewData->_viewportDimensionsUniform->set(osg::Vec4(viewport->x(), viewport->y(), viewport->width(),viewport->height()));
  362 + osg::Viewport* viewport = cv->getCurrentRenderStage()->getViewport();
  363 + if (viewport)
  364 + {
  365 + viewData->_viewportDimensionsUniform->set(osg::Vec4(viewport->x(), viewport->y(), viewport->width(),viewport->height()));
285 366
286   - if (viewport->width() != viewData->_colorTexture->getTextureWidth() ||
287   - viewport->height() != viewData->_colorTexture->getTextureHeight())
  367 + if (viewport->width() != viewData->_colorTexture->getTextureWidth() ||
  368 + viewport->height() != viewData->_colorTexture->getTextureHeight())
  369 + {
  370 + OSG_NOTICE<<"Need to change texture size to "<<viewport->width()<<", "<< viewport->height()<<std::endl;
  371 + viewData->_colorTexture->setTextureSize(viewport->width(), viewport->height());
  372 + viewData->_colorTexture->dirtyTextureObject();
  373 + viewData->_depthTexture->setTextureSize(viewport->width(), viewport->height());
  374 + viewData->_depthTexture->dirtyTextureObject();
  375 + viewData->_rttCamera->setViewport(0, 0, viewport->width(), viewport->height());
  376 + if (viewData->_rttCamera->getRenderingCache())
288 377 {
289   - OSG_NOTICE<<"Need to change texture size to "<<viewport->width()<<", "<< viewport->height()<<std::endl;
290   - viewData->_colorTexture->setTextureSize(viewport->width(), viewport->height());
291   - viewData->_colorTexture->dirtyTextureObject();
292   - viewData->_depthTexture->setTextureSize(viewport->width(), viewport->height());
293   - viewData->_depthTexture->dirtyTextureObject();
294   - viewData->_rttCamera->setViewport(0, 0, viewport->width(), viewport->height());
295   - if (viewData->_rttCamera->getRenderingCache())
296   - {
297   - viewData->_rttCamera->getRenderingCache()->releaseGLObjects(0);
298   - }
  378 + viewData->_rttCamera->getRenderingCache()->releaseGLObjects(0);
299 379 }
300 380 }
  381 + }
301 382
302   - cv->setUserValue("VolumeSceneTraversal",std::string("RenderToTexture"));
  383 + cv->setUserValue("VolumeSceneTraversal",std::string("RenderToTexture"));
303 384
304   - osg::Camera* parentCamera = cv->getCurrentCamera();
305   - viewData->_rttCamera->setClearColor(parentCamera->getClearColor());
  385 + osg::Camera* parentCamera = cv->getCurrentCamera();
  386 + viewData->_rttCamera->setClearColor(parentCamera->getClearColor());
306 387
307   - //OSG_NOTICE<<"Ready to traverse RTT Camera"<<std::endl;
308   - //OSG_NOTICE<<" RTT Camera ProjectionMatrix Before "<<viewData->_rttCamera->getProjectionMatrix()<<std::endl;
309   - viewData->_rttCamera->accept(nv);
  388 + //OSG_NOTICE<<"Ready to traverse RTT Camera"<<std::endl;
  389 + //OSG_NOTICE<<" RTT Camera ProjectionMatrix Before "<<viewData->_rttCamera->getProjectionMatrix()<<std::endl;
  390 + viewData->_rttCamera->accept(nv);
310 391
311   - //OSG_NOTICE<<" RTT Camera ProjectionMatrix After "<<viewData->_rttCamera->getProjectionMatrix()<<std::endl;
312   - //OSG_NOTICE<<" cv ProjectionMatrix After "<<*(cv->getProjectionMatrix())<<std::endl;
  392 + //OSG_NOTICE<<" RTT Camera ProjectionMatrix After "<<viewData->_rttCamera->getProjectionMatrix()<<std::endl;
  393 + //OSG_NOTICE<<" cv ProjectionMatrix After "<<*(cv->getProjectionMatrix())<<std::endl;
313 394
314   - //OSG_NOTICE<<" after RTT near ="<<cv->getCalculatedNearPlane()<<std::endl;
315   - //OSG_NOTICE<<" after RTT far ="<<cv->getCalculatedFarPlane()<<std::endl;
  395 + //OSG_NOTICE<<" after RTT near ="<<cv->getCalculatedNearPlane()<<std::endl;
  396 + //OSG_NOTICE<<" after RTT far ="<<cv->getCalculatedFarPlane()<<std::endl;
316 397
317   - //OSG_NOTICE<<"tileVisited()"<<viewData->_tiles.size()<<std::endl;
  398 + //OSG_NOTICE<<"tileVisited()"<<viewData->_tiles.size()<<std::endl;
318 399
319 400
320   - typedef osgUtil::CullVisitor::value_type NearFarValueType;
321   - NearFarValueType calculatedNearPlane = std::numeric_limits<NearFarValueType>::max();
322   - NearFarValueType calculatedFarPlane = -std::numeric_limits<NearFarValueType>::max();
323   - if (viewData->_rttCamera->getUserValue("CalculatedNearPlane",calculatedNearPlane) &&
324   - viewData->_rttCamera->getUserValue("CalculatedFarPlane",calculatedFarPlane))
325   - {
326   - calculatedNearPlane *= 0.5;
327   - calculatedFarPlane *= 2.0;
  401 + typedef osgUtil::CullVisitor::value_type NearFarValueType;
  402 + NearFarValueType calculatedNearPlane = std::numeric_limits<NearFarValueType>::max();
  403 + NearFarValueType calculatedFarPlane = -std::numeric_limits<NearFarValueType>::max();
  404 + if (viewData->_rttCamera->getUserValue("CalculatedNearPlane",calculatedNearPlane) &&
  405 + viewData->_rttCamera->getUserValue("CalculatedFarPlane",calculatedFarPlane))
  406 + {
  407 + calculatedNearPlane *= 0.5;
  408 + calculatedFarPlane *= 2.0;
328 409
329   - //OSG_NOTICE<<"Got from RTTCamera CalculatedNearPlane="<<calculatedNearPlane<<std::endl;
330   - //OSG_NOTICE<<"Got from RTTCamera CalculatedFarPlane="<<calculatedFarPlane<<std::endl;
331   - if (calculatedNearPlane < cv->getCalculatedNearPlane()) cv->setCalculatedNearPlane(calculatedNearPlane);
332   - if (calculatedFarPlane > cv->getCalculatedFarPlane()) cv->setCalculatedFarPlane(calculatedFarPlane);
333   - }
  410 + //OSG_NOTICE<<"Got from RTTCamera CalculatedNearPlane="<<calculatedNearPlane<<std::endl;
  411 + //OSG_NOTICE<<"Got from RTTCamera CalculatedFarPlane="<<calculatedFarPlane<<std::endl;
  412 + if (calculatedNearPlane < cv->getCalculatedNearPlane()) cv->setCalculatedNearPlane(calculatedNearPlane);
  413 + if (calculatedFarPlane > cv->getCalculatedFarPlane()) cv->setCalculatedFarPlane(calculatedFarPlane);
  414 + }
334 415
335   - if (calculatedFarPlane>calculatedNearPlane)
336   - {
337   - cv->clampProjectionMatrix(projectionMatrix, calculatedNearPlane, calculatedFarPlane);
338   - }
  416 + if (calculatedFarPlane>calculatedNearPlane)
  417 + {
  418 + cv->clampProjectionMatrix(projectionMatrix, calculatedNearPlane, calculatedFarPlane);
  419 + }
339 420
340   - osg::Matrix inv_projectionModelViewMatrix;
341   - inv_projectionModelViewMatrix.invert(modelviewMatrix*projectionMatrix);
  421 + osg::Matrix inv_projectionModelViewMatrix;
  422 + inv_projectionModelViewMatrix.invert(modelviewMatrix*projectionMatrix);
342 423
343   - double depth = 1.0;
344   - osg::Vec3d v00 = osg::Vec3d(-1.0,-1.0,depth)*inv_projectionModelViewMatrix;
345   - osg::Vec3d v01 = osg::Vec3d(-1.0,1.0,depth)*inv_projectionModelViewMatrix;
346   - osg::Vec3d v10 = osg::Vec3d(1.0,-1.0,depth)*inv_projectionModelViewMatrix;
347   - osg::Vec3d v11 = osg::Vec3d(1.0,1.0,depth)*inv_projectionModelViewMatrix;
  424 + double depth = 1.0;
  425 + osg::Vec3d v00 = osg::Vec3d(-1.0,-1.0,depth)*inv_projectionModelViewMatrix;
  426 + osg::Vec3d v01 = osg::Vec3d(-1.0,1.0,depth)*inv_projectionModelViewMatrix;
  427 + osg::Vec3d v10 = osg::Vec3d(1.0,-1.0,depth)*inv_projectionModelViewMatrix;
  428 + osg::Vec3d v11 = osg::Vec3d(1.0,1.0,depth)*inv_projectionModelViewMatrix;
348 429
349   - // OSG_NOTICE<<"v00= "<<v00<<std::endl;
350   - // OSG_NOTICE<<"v01= "<<v01<<std::endl;
351   - // OSG_NOTICE<<"v10= "<<v10<<std::endl;
352   - // OSG_NOTICE<<"v11= "<<v11<<std::endl;
  430 + // OSG_NOTICE<<"v00= "<<v00<<std::endl;
  431 + // OSG_NOTICE<<"v01= "<<v01<<std::endl;
  432 + // OSG_NOTICE<<"v10= "<<v10<<std::endl;
  433 + // OSG_NOTICE<<"v11= "<<v11<<std::endl;
353 434
354   - (*(viewData->_vertices))[0] = v01;
355   - (*(viewData->_vertices))[1] = v00;
356   - (*(viewData->_vertices))[2] = v11;
357   - (*(viewData->_vertices))[3] = v10;
358   - viewData->_geometry->dirtyBound();
  435 + (*(viewData->_vertices))[0] = v01;
  436 + (*(viewData->_vertices))[1] = v00;
  437 + (*(viewData->_vertices))[2] = v11;
  438 + (*(viewData->_vertices))[3] = v10;
  439 + viewData->_geometry->dirtyBound();
359 440
360   - //OSG_NOTICE<<" new after RTT near ="<<cv->getCalculatedNearPlane()<<std::endl;
361   - //OSG_NOTICE<<" new after RTT far ="<<cv->getCalculatedFarPlane()<<std::endl;
  441 + //OSG_NOTICE<<" new after RTT near ="<<cv->getCalculatedNearPlane()<<std::endl;
  442 + //OSG_NOTICE<<" new after RTT far ="<<cv->getCalculatedFarPlane()<<std::endl;
362 443
363   - viewData->_backdropSubgraph->accept(*cv);
  444 + viewData->_backdropSubgraph->accept(*cv);
364 445
365   - osg::NodePath nodePathPriorToTraversingSubgraph = cv->getNodePath();
366   - cv->setUserValue("VolumeSceneTraversal",std::string("Post"));
  446 + osg::NodePath nodePathPriorToTraversingSubgraph = cv->getNodePath();
  447 + cv->setUserValue("VolumeSceneTraversal",std::string("Post"));
367 448
368   - // for each tile that needs post rendering we need to add it into current RenderStage.
369   - Tiles& tiles = viewData->_tiles;
370   - for(Tiles::iterator itr = tiles.begin();
371   - itr != tiles.end();
372   - ++itr)
  449 + // for each tile that needs post rendering we need to add it into current RenderStage.
  450 + Tiles& tiles = viewData->_tiles;
  451 + for(Tiles::iterator itr = tiles.begin();
  452 + itr != tiles.end();
  453 + ++itr)
  454 + {
  455 + TileData* tileData = itr->second.get();
  456 + if (!tileData || !(tileData->active))
373 457 {
374   - TileData* tileData = itr->second.get();
375   - if (!tileData || !(tileData->active))
376   - {
377   - OSG_NOTICE<<"Skipping TileData that is inactive : "<<tileData<<std::endl;
378   - continue;
379   - }
380   - OSG_NOTICE<<"Handling TileData that is active : "<<tileData<<std::endl;
381   -
  458 + OSG_INFO<<"Skipping TileData that is inactive : "<<tileData<<std::endl;
  459 + continue;
  460 + }
382 461
383   - unsigned int numStateSetPushed = 0;
  462 + unsigned int numStateSetPushed = 0;
384 463
385   - // OSG_NOTICE<<"VolumeTile to add "<<tileData->projectionMatrix.get()<<", "<<tileData->modelviewMatrix.get()<<std::endl;
  464 + // OSG_NOTICE<<"VolumeTile to add "<<tileData->projectionMatrix.get()<<", "<<tileData->modelviewMatrix.get()<<std::endl;
386 465
387 466
388   - osg::NodePath& nodePath = tileData->nodePath;
  467 + osg::NodePath& nodePath = tileData->nodePath;
389 468
390   - cv->getNodePath() = nodePath;
391   - cv->pushProjectionMatrix(tileData->projectionMatrix.get());
392   - cv->pushModelViewMatrix(tileData->modelviewMatrix.get(), osg::Transform::ABSOLUTE_RF_INHERIT_VIEWPOINT);
  469 + cv->getNodePath() = nodePath;
  470 + cv->pushProjectionMatrix(tileData->projectionMatrix.get());
  471 + cv->pushModelViewMatrix(tileData->modelviewMatrix.get(), osg::Transform::ABSOLUTE_RF_INHERIT_VIEWPOINT);
393 472
394 473
395   - cv->pushStateSet(viewData->_stateset.get());
396   - ++numStateSetPushed;
  474 + cv->pushStateSet(viewData->_stateset.get());
  475 + ++numStateSetPushed;
397 476
  477 + cv->pushStateSet(tileData->stateset.get());
  478 + ++numStateSetPushed;
398 479
399   - osg::NodePath::iterator np_itr = nodePath.begin();
  480 + osg::NodePath::iterator np_itr = nodePath.begin();
400 481
401   - // skip over all nodes above VolumeScene as this will have already been traverse by CullVisitor
402   - while(np_itr!=nodePath.end() && (*np_itr)!=viewData->_rttCamera.get()) { ++np_itr; }
403   - if (np_itr!=nodePath.end()) ++np_itr;
  482 + // skip over all nodes above VolumeScene as this will have already been traverse by CullVisitor
  483 + while(np_itr!=nodePath.end() && (*np_itr)!=viewData->_rttCamera.get()) { ++np_itr; }
  484 + if (np_itr!=nodePath.end()) ++np_itr;
404 485
405   - // push the stateset on the nodes between this VolumeScene and the VolumeTile
406   - for(osg::NodePath::iterator ss_itr = np_itr;
407   - ss_itr != nodePath.end();
408   - ++ss_itr)
409   - {
410   - if ((*ss_itr)->getStateSet())
411   - {
412   - numStateSetPushed++;
413   - cv->pushStateSet((*ss_itr)->getStateSet());
414   - // OSG_NOTICE<<" pushing StateSet"<<std::endl;
415   - }
416   - }
417   - cv->traverse(*(tileData->nodePath.back()));
418   -
419   - // pop the StateSet's
420   - for(unsigned int i=0; i<numStateSetPushed; ++i)
  486 + // push the stateset on the nodes between this VolumeScene and the VolumeTile
  487 + for(osg::NodePath::iterator ss_itr = np_itr;
  488 + ss_itr != nodePath.end();
  489 + ++ss_itr)
  490 + {
  491 + if ((*ss_itr)->getStateSet())
421 492 {
422   - cv->popStateSet();
423   - // OSG_NOTICE<<" popping StateSet"<<std::endl;
  493 + numStateSetPushed++;
  494 + cv->pushStateSet((*ss_itr)->getStateSet());
  495 + // OSG_NOTICE<<" pushing StateSet"<<std::endl;
424 496 }
  497 + }
  498 + cv->traverse(*(tileData->nodePath.back()));
425 499
426   - cv->popModelViewMatrix();
427   - cv->popProjectionMatrix();
  500 + // pop the StateSet's
  501 + for(unsigned int i=0; i<numStateSetPushed; ++i)
  502 + {
  503 + cv->popStateSet();
  504 + // OSG_NOTICE<<" popping StateSet"<<std::endl;
428 505 }
429 506
430   - cv->getNodePath() = nodePathPriorToTraversingSubgraph;
431   - }
432   - else
433   - {
434   - Group::traverse(nv);
  507 + cv->popModelViewMatrix();
  508 + cv->popProjectionMatrix();
435 509 }
  510 +
  511 + // need to synchronize projection matrices:
  512 + // current CV projection matrix
  513 + // main scene RTT Camera projection matrix
  514 + // each tile RTT Camera
  515 + // each tile final render.
  516 +
  517 + cv->getNodePath() = nodePathPriorToTraversingSubgraph;
  518 +
  519 + // restore the previous cull settings
  520 + cv->setCullSettings(saved_cull_settings);
436 521 }

0 comments on commit 894fbe9

Please sign in to comment.
Something went wrong with that request. Please try again.