From bc0fb12d0a665192d95ae8c5b2517801595ac98a Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 19 Feb 2007 14:21:01 +0000 Subject: [PATCH] Added option to use osgShadow::ShadowMap technique --- examples/osgdepthshadow/GNUmakefile | 2 +- examples/osgdepthshadow/osgdepthshadow.cpp | 71 ++++++++++++++----- .../osgshadowtexture/osgshadowtexture.cpp | 25 +++++++ 3 files changed, 79 insertions(+), 19 deletions(-) diff --git a/examples/osgdepthshadow/GNUmakefile b/examples/osgdepthshadow/GNUmakefile index 022c7cffe..e7ef61032 100644 --- a/examples/osgdepthshadow/GNUmakefile +++ b/examples/osgdepthshadow/GNUmakefile @@ -4,7 +4,7 @@ include $(TOPDIR)/Make/makedefs CXXFILES =\ osgdepthshadow.cpp\ -LIBS += -losgViewer -losgText -losgGA -losgDB -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS) +LIBS += -losgViewer -losgShadow -losgText -losgGA -losgDB -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS) INSTFILES = \ $(CXXFILES)\ diff --git a/examples/osgdepthshadow/osgdepthshadow.cpp b/examples/osgdepthshadow/osgdepthshadow.cpp index 29087f7f3..3d1eadca8 100644 --- a/examples/osgdepthshadow/osgdepthshadow.cpp +++ b/examples/osgdepthshadow/osgdepthshadow.cpp @@ -21,6 +21,9 @@ #include +#include +#include + #include using namespace osg; @@ -436,33 +439,65 @@ int main(int argc, char** argv) return 1; } - ref_ptr scene = new MatrixTransform; - scene->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(125.0),1.0,0.0,0.0)); - - ref_ptr shadowed_scene = _create_scene(); - if (!shadowed_scene.valid()) return 1; - - ref_ptr light_transform = _create_lights(); - if (!light_transform.valid()) return 1; - - ref_ptr shadowedScene; - - if (withBaseTexture) + if (arguments.read("--sm")) { - shadowed_scene->getOrCreateStateSet()->setTextureAttributeAndModes( 0, new osg::Texture2D(osgDB::readImageFile("Images/lz.rgb")), osg::StateAttribute::ON); - shadowedScene = createShadowedScene(shadowed_scene.get(),light_transform.get(),1); + osgShadow::ShadowMap* sm = new osgShadow::ShadowMap; + sm->setTextureUnit( withBaseTexture ? 1 : 0 ); + + osg::ref_ptr shadowedScene = new osgShadow::ShadowedScene(sm); + + + ref_ptr created_scene = _create_scene(); + if (!created_scene.valid()) return 1; + + shadowedScene->addChild(_create_scene().get()); + + + ref_ptr scene = new MatrixTransform; + scene->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(125.0),1.0,0.0,0.0)); + + scene->addChild(_create_lights().get()); + scene->addChild(shadowedScene.get()); + + if (withBaseTexture) + { + scene->getOrCreateStateSet()->setTextureAttributeAndModes( 0, new osg::Texture2D(osgDB::readImageFile("Images/lz.rgb")), osg::StateAttribute::ON); + } + + viewer.setSceneData(scene.get()); + } else { - shadowedScene = createShadowedScene(shadowed_scene.get(),light_transform.get(),0); + ref_ptr scene = new MatrixTransform; + scene->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(125.0),1.0,0.0,0.0)); + + ref_ptr created_scene = _create_scene(); + if (!created_scene.valid()) return 1; + + ref_ptr light_transform = _create_lights(); + if (!light_transform.valid()) return 1; + + ref_ptr shadowedScene; + + if (withBaseTexture) + { + scene->getOrCreateStateSet()->setTextureAttributeAndModes( 0, new osg::Texture2D(osgDB::readImageFile("Images/lz.rgb")), osg::StateAttribute::ON); + shadowedScene = createShadowedScene(created_scene.get(),light_transform.get(),1); + } + else + { + shadowedScene = createShadowedScene(created_scene.get(),light_transform.get(),0); + } + + scene->addChild(shadowedScene.get()); + + viewer.setSceneData(scene.get()); } // viewer.setUpViewOnSingleScreen(); - scene->addChild(shadowedScene.get()); - - viewer.setSceneData(scene.get()); return viewer.run(); } diff --git a/examples/osgshadowtexture/osgshadowtexture.cpp b/examples/osgshadowtexture/osgshadowtexture.cpp index f04ae0825..a0f480ca9 100644 --- a/examples/osgshadowtexture/osgshadowtexture.cpp +++ b/examples/osgshadowtexture/osgshadowtexture.cpp @@ -16,6 +16,7 @@ #include #include #include +#include // include the call which creates the shadowed subgraph. #include "CreateShadowedScene.h" @@ -204,6 +205,30 @@ osg::Node* createModel(osg::ArgumentParser& arguments) return shadowedScene; + } + else if (arguments.read("--sm")) + { + + osgShadow::ShadowedScene* shadowedScene = new osgShadow::ShadowedScene; + + osg::ref_ptr shadowMap = new osgShadow::ShadowMap; + shadowedScene->setShadowTechnique(shadowMap.get()); + + osg::ref_ptr ls = new osg::LightSource; + ls->getLight()->setPosition(osg::Vec4(lightPosition,1.0)); + + shadowedScene->setRecievesShadowTraversalMask(0x1); + shadowed->setNodeMask(shadowedScene->getRecievesShadowTraversalMask()); + + shadowedScene->setCastsShadowTraversalMask(0x2); + shadower->setNodeMask(shadowedScene->getCastsShadowTraversalMask()); + + shadowedScene->addChild(shadower); + shadowedScene->addChild(shadowed); + shadowedScene->addChild(ls.get()); + + return shadowedScene; + } else {