From 5c4bcd4aace670cc5685ad6b4e03131d8852949b Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 10 Mar 2003 11:59:41 +0000 Subject: [PATCH] Moved the RenderLeaf implementatation across to always applying matrices before state, which fixes problem that can occur with state that needs the current model view or projection matrices. Changes osgreflect so that it uses ClipNode rather than clip plane, thereby fixing the problems which occur when the above is applied. Create a new osgProducer version of the original GLUT based version. --- Make/makedirdefs | 1 + examples/osgviewer/osgviewer.cpp | 12 +-- src/Demos/osgreflect/osgreflect.cpp | 159 +++++++++++++--------------- src/osgUtil/RenderLeaf.cpp | 21 +--- 4 files changed, 79 insertions(+), 114 deletions(-) diff --git a/Make/makedirdefs b/Make/makedirdefs index 7b14153e5..35d19f444 100644 --- a/Make/makedirdefs +++ b/Make/makedirdefs @@ -83,6 +83,7 @@ EXAMPLE_DIRS = \ osgconv\ osghud\ osgtext\ + osgreflect\ osgstereoimage\ diff --git a/examples/osgviewer/osgviewer.cpp b/examples/osgviewer/osgviewer.cpp index a89a354d7..d5c140aa6 100644 --- a/examples/osgviewer/osgviewer.cpp +++ b/examples/osgviewer/osgviewer.cpp @@ -1,14 +1,12 @@ /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield * - * This library is open source and may be redistributed and/or modified under - * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or - * (at your option) any later version. The full license is in LICENSE file - * included with this distribution, and on the openscenegraph.org website. + * This application is open source and may be redistributed and/or modified + * freely and without restriction, both in commericial and non commericial applications, + * as long as this copyright notice is maintained. * - * This library is distributed in the hope that it will be useful, + * This application is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * OpenSceneGraph Public License for more details. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ #include diff --git a/src/Demos/osgreflect/osgreflect.cpp b/src/Demos/osgreflect/osgreflect.cpp index 8afdfb968..10b078289 100644 --- a/src/Demos/osgreflect/osgreflect.cpp +++ b/src/Demos/osgreflect/osgreflect.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include @@ -97,82 +97,12 @@ osg::Drawable* createMirrorSurface(float xMin,float xMax,float yMin,float yMax,f return geom; } -void write_usage(std::ostream& out,const std::string& name) +osg::Node* createMirroredScene(osg::Node* model) { - out << std::endl; - out <<"usage:"<< std::endl; - out <<" "< commandLine; - for(int i=1;iaddChild(loadedModel); // calculate where to place the mirror according to the // loaded models bounding sphere. - const osg::BoundingSphere& bs = loadedModelTransform->getBound(); + const osg::BoundingSphere& bs = model->getBound(); float width_factor = 1.5; float height_factor = 0.3; @@ -252,7 +182,7 @@ int main( int argc, char **argv ) osg::Group* groupBin2 = new osg::Group(); groupBin2->setStateSet(statesetBin2); - groupBin2->addChild(loadedModelTransform); + groupBin2->addChild(model); rootNode->addChild(groupBin2); } @@ -302,9 +232,7 @@ int main( int argc, char **argv ) // to prevert an 'inside' out view of the reflected model. // set up the stencil ops so that only operator on this mirrors stencil value. - osg::Stencil* stencil = new osg::Stencil; - stencil->setFunction(osg::Stencil::EQUAL,1,~0); - stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::KEEP); + // this clip plane removes any of the scene which when mirror would // poke through the mirror. However, this clip plane should really @@ -313,21 +241,30 @@ int main( int argc, char **argv ) clipplane->setClipPlane(osg::Vec4(0.0f,0.0f,-1.0f,z)); clipplane->setClipPlaneNum(0); - osg::StateSet* dstate = new osg::StateSet; + osg::ClipNode* clipNode = new osg::ClipNode; + clipNode->addClipPlane(clipplane); + + + osg::StateSet* dstate = clipNode->getOrCreateStateSet(); dstate->setRenderBinDetails(4,"RenderBin"); dstate->setMode(GL_CULL_FACE,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF); - dstate->setAttributeAndModes(stencil,osg::StateAttribute::ON); - dstate->setAttributeAndModes(clipplane,osg::StateAttribute::ON); - osg::MatrixTransform* dcs = new osg::MatrixTransform; - dcs->setStateSet(dstate); - dcs->preMult(osg::Matrix::translate(0.0f,0.0f,-z)* + osg::Stencil* stencil = new osg::Stencil; + stencil->setFunction(osg::Stencil::EQUAL,1,~0); + stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::KEEP); + dstate->setAttributeAndModes(stencil,osg::StateAttribute::ON); + + osg::MatrixTransform* reverseMatrix = new osg::MatrixTransform; + reverseMatrix->setStateSet(dstate); + reverseMatrix->preMult(osg::Matrix::translate(0.0f,0.0f,-z)* osg::Matrix::scale(1.0f,1.0f,-1.0f)* osg::Matrix::translate(0.0f,0.0f,z)); - dcs->addChild(loadedModelTransform); + reverseMatrix->addChild(model); - rootNode->addChild(dcs); + clipNode->addChild(reverseMatrix); + + rootNode->addChild(clipNode); } @@ -363,12 +300,60 @@ int main( int argc, char **argv ) rootNode->addChild(geode); } + + return rootNode; +} + +int main( int argc, char **argv ) +{ + + // initialize the GLUT + glutInit( &argc, argv ); + + if (argc<2) + { + //write_usage(osg::notify(osg::NOTICE),argv[0]); + return 0; + } + + // create the commandline args. + std::vector commandLine; + for(int i=1;iaddChild(loadedModel); + + osg::NodeCallback* nc = new osgUtil::TransformCallback(loadedModelTransform->getBound().center(),osg::Vec3(0.0f,0.0f,1.0f),osg::inDegrees(45.0f)); + loadedModelTransform->setUpdateCallback(nc); + + osg::Node* rootNode = createMirroredScene(loadedModelTransform); // add model to the viewer. viewer.addViewport( rootNode ); - osg::NodeCallback* nc = new osgUtil::TransformCallback(loadedModelTransform->getBound().center(),osg::Vec3(0.0f,0.0f,1.0f),osg::inDegrees(45.0f)); - loadedModelTransform->setUpdateCallback(nc); // register trackball, flight and drive. viewer.registerCameraManipulator(new osgGA::TrackballManipulator); diff --git a/src/osgUtil/RenderLeaf.cpp b/src/osgUtil/RenderLeaf.cpp index 6da29605a..47db78e62 100644 --- a/src/osgUtil/RenderLeaf.cpp +++ b/src/osgUtil/RenderLeaf.cpp @@ -16,22 +16,16 @@ using namespace osg; using namespace osgUtil; -// comment if you are are working with vertex programs, -// but it does break osgreflect demo and perhaps others, so keep an eye -// out artifacts. -// #define APPLY_MATRICES_BEFORE_STATE - void RenderLeaf::render(State& state,RenderLeaf* previous) { if (previous) { -#ifdef APPLY_MATRICES_BEFORE_STATE // apply matrices if required. state.applyProjectionMatrix(_projection.get()); state.applyModelViewMatrix(_modelview.get()); -#endif + // apply state if required. RenderGraph* prev_rg = previous->_parent; RenderGraph* prev_rg_parent = prev_rg->_parent; @@ -52,34 +46,21 @@ void RenderLeaf::render(State& state,RenderLeaf* previous) } -#ifndef APPLY_MATRICES_BEFORE_STATE - // apply matrices if required. - state.applyProjectionMatrix(_projection.get()); - state.applyModelViewMatrix(_modelview.get()); -#endif // draw the drawable _drawable->draw(state); } else { -#ifdef APPLY_MATRICES_BEFORE_STATE // apply matrices if required. state.applyProjectionMatrix(_projection.get()); state.applyModelViewMatrix(_modelview.get()); -#endif // apply state if required. RenderGraph::moveRenderGraph(state,NULL,_parent->_parent); state.apply(_parent->_stateset); -#ifndef APPLY_MATRICES_BEFORE_STATE - // apply matrices if required. - state.applyProjectionMatrix(_projection.get()); - state.applyModelViewMatrix(_modelview.get()); -#endif - // draw the drawable _drawable->draw(state); }