diff --git a/VisualStudio/osgPlugins/osg/dot_osg.dsp b/VisualStudio/osgPlugins/osg/dot_osg.dsp index e3261a4a9..686bb61ac 100755 --- a/VisualStudio/osgPlugins/osg/dot_osg.dsp +++ b/VisualStudio/osgPlugins/osg/dot_osg.dsp @@ -198,6 +198,10 @@ SOURCE=..\..\..\src\osgPlugins\osg\Object.cpp # End Source File # Begin Source File +SOURCE=..\..\..\src\osgPlugins\osg\OccluderNode.cpp +# End Source File +# Begin Source File + SOURCE=..\..\..\src\osgPlugins\osg\Point.cpp # End Source File # Begin Source File diff --git a/include/osg/CollectOccludersVisitor b/include/osg/CollectOccludersVisitor index e5e98c739..1280c3266 100644 --- a/include/osg/CollectOccludersVisitor +++ b/include/osg/CollectOccludersVisitor @@ -30,6 +30,10 @@ class SG_EXPORT CollectOccludersVisitor : public osg::NodeVisitor, public osg::C virtual void apply(osg::LOD& node); virtual void apply(osg::OccluderNode& node); + + void setCreateDrawablesOnOccludeNodes(bool flag) { _createDrawables=flag; } + bool getCreateDrawablesOnOccludeNodes() const { return _createDrawables; } + void setCollectedOcculderList(const ShadowVolumeOccluderList& svol) { _occluderList = svol; } ShadowVolumeOccluderList& getCollectedOccluderList() { return _occluderList; } const ShadowVolumeOccluderList& getCollectedOccluderList() const { return _occluderList; } @@ -43,7 +47,8 @@ class SG_EXPORT CollectOccludersVisitor : public osg::NodeVisitor, public osg::C /** prevent unwanted copy operator.*/ CollectOccludersVisitor& operator = (const CollectOccludersVisitor&) { return *this; } - ShadowVolumeOccluderList _collectedOccluderList; + bool _createDrawables; + ShadowVolumeOccluderList _collectedOccluderList; }; diff --git a/include/osg/ShadowVolumeOccluder b/include/osg/ShadowVolumeOccluder index 14a552e2a..d8210954e 100644 --- a/include/osg/ShadowVolumeOccluder +++ b/include/osg/ShadowVolumeOccluder @@ -35,7 +35,7 @@ class SG_EXPORT ShadowVolumeOccluder /** compute the shadow volume occluder. */ - bool computeOccluder(const NodePath& nodePath,const ConvexPlanerOccluder& occluder,CullStack& cullStack); + bool computeOccluder(const NodePath& nodePath,const ConvexPlanerOccluder& occluder,CullStack& cullStack,bool createDrawables=false); inline void disableResultMasks(); diff --git a/src/Demos/osgoccluder/osgoccluder.cpp b/src/Demos/osgoccluder/osgoccluder.cpp index facd8fa51..ea8a2ba2a 100644 --- a/src/Demos/osgoccluder/osgoccluder.cpp +++ b/src/Demos/osgoccluder/osgoccluder.cpp @@ -82,7 +82,7 @@ osg::Node* createOccludersAroundModel(osg::Node* model) occluderNode->setOccluder(cpo); - // create a drawable for occluder. + // create a drawable for occluder. osg::GeoSet* geoset = osgNew osg::GeoSet; osg::Vec3* coords = osgNew osg::Vec3[occluder.getVertexList().size()]; diff --git a/src/osg/CollectOccludersVisitor.cpp b/src/osg/CollectOccludersVisitor.cpp index f6775d1c0..d782efd69 100644 --- a/src/osg/CollectOccludersVisitor.cpp +++ b/src/osg/CollectOccludersVisitor.cpp @@ -11,6 +11,9 @@ CollectOccludersVisitor::CollectOccludersVisitor() { // overide the default node visitor mode. setTraversalMode(NodeVisitor::TRAVERSE_ACTIVE_CHILDREN); + + _createDrawables = false; + } CollectOccludersVisitor::~CollectOccludersVisitor() @@ -117,11 +120,13 @@ void CollectOccludersVisitor::apply(osg::OccluderNode& node) // planes, all with their normals facing inward towards the volume, // and then transform them back into projection space. ShadowVolumeOccluder svo; - if (svo.computeOccluder(_nodePath, *node.getOccluder(), *this)) + if (svo.computeOccluder(_nodePath, *node.getOccluder(), *this,_createDrawables)) { // need to test occluder against view frustum. // std::cout << " adding in Occluder"< #include +#include +#include +#include + using namespace osg; @@ -108,7 +112,9 @@ void pushToFarPlane(PointList& points) itr!=points.end(); ++itr) { - itr->second.z()=-1.0f; +// std::cout << "itr->second "<< itr->second<< " after "; + itr->second.z() += 1.0f; +// std::cout << itr->second<< std::endl; } } @@ -129,7 +135,59 @@ Plane computeFrontPlane(const PointList& front) return Plane(front[2].second,front[1].second,front[0].second); } -bool ShadowVolumeOccluder::computeOccluder(const NodePath& nodePath,const ConvexPlanerOccluder& occluder,CullStack& cullStack) +Drawable* createOccluderDrawable(const PointList& front, const PointList& back) +{ + // create a drawable for occluder. + osg::GeoSet* geoset = osgNew osg::GeoSet; + + int totalNumber = front.size()+back.size(); + osg::Vec3* coords = osgNew osg::Vec3[front.size()+back.size()]; + osg::Vec3* cptr = coords; + for(PointList::const_iterator fitr=front.begin(); + fitr!=front.end(); + ++fitr) + { + *cptr = fitr->second; + ++cptr; + } + + for(PointList::const_iterator bitr=back.begin(); + bitr!=back.end(); + ++bitr) + { + *cptr = bitr->second; + ++cptr; + } + + geoset->setCoords(coords); + + osg::Vec4* color = osgNew osg::Vec4[1]; + color[0].set(1.0f,1.0f,1.0f,0.5f); + geoset->setColors(color); + geoset->setColorBinding(osg::GeoSet::BIND_OVERALL); + + geoset->setPrimType(osg::GeoSet::POINTS); + geoset->setNumPrims(totalNumber); + + //cout << "totalNumber = "<addDrawable(geoset); + + osg::StateSet* stateset = osgNew osg::StateSet; + stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); + stateset->setMode(GL_BLEND,osg::StateAttribute::ON); + stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); + + geoset->setStateSet(stateset); + + return geoset; +} + + + +bool ShadowVolumeOccluder::computeOccluder(const NodePath& nodePath,const ConvexPlanerOccluder& occluder,CullStack& cullStack,bool createDrawables) { @@ -153,9 +211,25 @@ bool ShadowVolumeOccluder::computeOccluder(const NodePath& nodePath,const Convex Matrix invP; invP.invert(P); +// std::cout<<"P "<0.0f) { -// std::cout << " flipping polytope"<(nodePath.back()); + if (group) + { + + osg::Matrix invMV; + invMV.invert(MV); + + transform(points,invMV); + transform(farPoints,invMV); + +// Vec3 v6=v5*invMV; +// std::cout<<"******************"<addChild(geode); + geode->addDrawable(createOccluderDrawable(points,farPoints)); + } } @@ -208,7 +298,7 @@ bool ShadowVolumeOccluder::computeOccluder(const NodePath& nodePath,const Convex } else { -// std::cout << " occluder clipped out of frustum."< #include #include +#include #include @@ -1080,67 +1081,94 @@ void Viewer::keyboard(unsigned char key, int x, int y) osg::notify(osg::NOTICE) << "Saved screen image to `"<getViewport()); + + if (sceneView->getProjectionMatrix()) cov.pushProjectionMatrix(sceneView->getProjectionMatrix()); + else if (sceneView->getCamera()) cov.pushProjectionMatrix(osgNew Matrix(sceneView->getCamera()->getProjectionMatrix())); + else cov.pushProjectionMatrix(osgNew Matrix()); + + if (sceneView->getModelViewMatrix()) cov.pushModelViewMatrix(sceneView->getModelViewMatrix()); + else if (sceneView->getCamera()) cov.pushModelViewMatrix(osgNew Matrix(sceneView->getCamera()->getModelViewMatrix())); + else cov.pushModelViewMatrix(osgNew Matrix()); + + sceneView->getSceneData()->accept(cov); + + cov.popModelViewMatrix(); + cov.popProjectionMatrix(); + cov.popViewport(); + + + } + break; + case 'i' : case 'r' : - { - osg::notify(osg::NOTICE) << "***** Intersecting **************"<< std::endl; - - osg::Vec3 near_point,far_point; - if (!sceneView->projectWindowXYIntoObject(x,_wh-y,near_point,far_point)) { - osg::notify(osg::NOTICE) << "Failed to calculate intersection ray."<< std::endl; - return; - } + osg::notify(osg::NOTICE) << "***** Intersecting **************"<< std::endl; - osg::ref_ptr lineSegment = osgNew osg::LineSegment; - lineSegment->set(near_point,far_point); - osg::notify(osg::NOTICE) << "start("<start()<<") end("<end()<<")"<< std::endl; - - osgUtil::IntersectVisitor iv; - iv.addLineSegment(lineSegment.get()); - - float startTime = clockSeconds(); - - sceneView->getSceneData()->accept(iv); - - float endTime = clockSeconds(); - - osg::notify(osg::NOTICE) << "Time for interesection = "<<(endTime-startTime)*1000<<"ms"<< std::endl; - - if (iv.hits()) - { - osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(lineSegment.get()); - for(osgUtil::IntersectVisitor::HitList::iterator hitr=hitList.begin(); - hitr!=hitList.end(); - ++hitr) + osg::Vec3 near_point,far_point; + if (!sceneView->projectWindowXYIntoObject(x,_wh-y,near_point,far_point)) { - osg::Vec3 ip = hitr->getLocalIntersectPoint(); - osg::Vec3 in = hitr->getLocalIntersectNormal(); - osg::Geode* geode = hitr->_geode.get(); - osg::notify(osg::NOTICE) << " Itersection Point ("<_matrix.valid()) - { - osg::Vec3 ipEye = hitr->getWorldIntersectPoint(); - osg::Vec3 inEye = hitr->getWorldIntersectNormal(); - inEye.normalize(); - if (geode) osg::notify(osg::NOTICE) << "Geode '"<getName()<< std::endl; - osg::notify(osg::NOTICE) << " Eye Itersection Point ("< lineSegment = osgNew osg::LineSegment; + lineSegment->set(near_point,far_point); + osg::notify(osg::NOTICE) << "start("<start()<<") end("<end()<<")"<< std::endl; + + osgUtil::IntersectVisitor iv; + iv.addLineSegment(lineSegment.get()); + + float startTime = clockSeconds(); + + sceneView->getSceneData()->accept(iv); + + float endTime = clockSeconds(); + + osg::notify(osg::NOTICE) << "Time for interesection = "<<(endTime-startTime)*1000<<"ms"<< std::endl; + + if (iv.hits()) + { + osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(lineSegment.get()); + for(osgUtil::IntersectVisitor::HitList::iterator hitr=hitList.begin(); + hitr!=hitList.end(); + ++hitr) { - // remove geoset.. - osg::GeoSet* gset = hitr->_geoset.get(); - osg::notify(osg::NOTICE) << " geoset ("<removeDrawable(gset)<<")"<< std::endl; + osg::Vec3 ip = hitr->getLocalIntersectPoint(); + osg::Vec3 in = hitr->getLocalIntersectNormal(); + osg::Geode* geode = hitr->_geode.get(); + osg::notify(osg::NOTICE) << " Itersection Point ("<_matrix.valid()) + { + osg::Vec3 ipEye = hitr->getWorldIntersectPoint(); + osg::Vec3 inEye = hitr->getWorldIntersectNormal(); + inEye.normalize(); + if (geode) osg::notify(osg::NOTICE) << "Geode '"<getName()<< std::endl; + osg::notify(osg::NOTICE) << " Eye Itersection Point ("<_geoset.get(); + osg::notify(osg::NOTICE) << " geoset ("<removeDrawable(gset)<<")"<< std::endl; + } + } } + osg::notify(osg::NOTICE) << std::endl << std::endl; } - - osg::notify(osg::NOTICE) << std::endl << std::endl; - } - break; + break; case 27 : diff --git a/src/osgPlugins/osg/Makefile b/src/osgPlugins/osg/Makefile index b72ecf23f..2b7391a7e 100644 --- a/src/osgPlugins/osg/Makefile +++ b/src/osgPlugins/osg/Makefile @@ -28,6 +28,7 @@ CXXFILES =\ Matrix.cpp\ Node.cpp\ Object.cpp\ + OccluderNode.cpp\ Point.cpp\ PolygonMode.cpp\ PolygonOffset.cpp\ diff --git a/src/osgUtil/SceneView.cpp b/src/osgUtil/SceneView.cpp index ebf4ce2b4..96278829b 100644 --- a/src/osgUtil/SceneView.cpp +++ b/src/osgUtil/SceneView.cpp @@ -289,11 +289,11 @@ void SceneView::cull() _cullVisitor->setTraversalMask(_cullMask); cullStage(projection.get(),modelview.get(),_cullVisitor.get(),_rendergraph.get(),_renderStage.get()); -// if (_camera.valid()) -// { -// // clamp the camera to the near/far computed in cull traversal. -// _camera->setNearFar(_cullVisitor->getCalculatedNearPlane(),_cullVisitor->getCalculatedFarPlane()); -// } + if (_camera.valid()) + { + // clamp the camera to the near/far computed in cull traversal. + _camera->setNearFar(_cullVisitor->getCalculatedNearPlane(),_cullVisitor->getCalculatedFarPlane()); + } }