Show
Ignore:
Timestamp:
09/23/08 11:59:23 (6 years ago)
Author:
robert
Message:

From Wojciech Lweandowski, added support for noew LispSM shadow technique (use --lispsm on CmdLine?) and added IslandScene? database (use -4 on CmdLine?).

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgshadow/osgshadow.cpp

    r8379 r8920  
    4242#include <osgShadow/SoftShadowMap> 
    4343#include <osgShadow/ParallelSplitShadowMap> 
     44#include <osgShadow/LightSpacePerspectiveShadowMap> 
    4445 
    4546#include <osgDB/ReadFile> 
     
    4849#include <iostream> 
    4950 
     51 
    5052// for the grid data.. 
    51 #include "../osghangglide/terrain_coords.h" 
    52  
    53 const int ReceivesShadowTraversalMask = 0x1; 
    54 const int CastsShadowTraversalMask = 0x2; 
     53#include "terrain_coords.h" 
     54// for the model number four - island scene 
     55#include "IslandScene.h" 
     56 
     57 
     58static int ReceivesShadowTraversalMask = 0x1; 
     59static int CastsShadowTraversalMask = 0x2; 
    5560 
    5661namespace ModelOne 
     
    472477        return ModelTwo::createModel(arguments); 
    473478    } 
     479    else if (arguments.read("-4")) 
     480    { 
     481        return ModelFour::createModel(arguments); 
     482    } 
    474483    else /*if (arguments.read("-3"))*/ 
    475484    { 
     
    491500    arguments.getApplicationUsage()->addCommandLineOption("--directionalLight", "Use a direction light."); 
    492501    arguments.getApplicationUsage()->addCommandLineOption("--noUpdate", "Disable the updating the of light source."); 
     502 
     503    arguments.getApplicationUsage()->addCommandLineOption("--castsShadowMask", "Override default castsShadowMask (default - 0x2)"); 
     504    arguments.getApplicationUsage()->addCommandLineOption("--receivesShadowMask", "Override default receivesShadowMask (default - 0x1)"); 
     505 
    493506    arguments.getApplicationUsage()->addCommandLineOption("--base", "Add a base geometry to test shadows."); 
    494507    arguments.getApplicationUsage()->addCommandLineOption("--sv", "Select ShadowVolume implementation."); 
    495508    arguments.getApplicationUsage()->addCommandLineOption("--ssm", "Select SoftShadowMap implementation."); 
    496509    arguments.getApplicationUsage()->addCommandLineOption("--sm", "Select ShadowMap implementation."); 
    497 //    arguments.getApplicationUsage()->addCommandLineOption("--pssm", "Select ParallelSplitShadowMap implementation."); 
     510 
    498511    arguments.getApplicationUsage()->addCommandLineOption("--pssm", "Select ParallelSplitShadowMap implementation.");//ADEGLI 
    499512    arguments.getApplicationUsage()->addCommandLineOption("--mapcount", "ParallelSplitShadowMap texture count.");//ADEGLI 
     
    506519    arguments.getApplicationUsage()->addCommandLineOption("--PolyOffset-Unit", "ParallelSplitShadowMap set PolygonOffset unit.");//ADEGLI 
    507520 
     521    arguments.getApplicationUsage()->addCommandLineOption("--lispsm", "Select LightSpacePerspectiveShadowMap implementation."); 
     522    arguments.getApplicationUsage()->addCommandLineOption("--ViewBounds", "LiSPSM optimize shadow for view frustum (weakest option)"); 
     523    arguments.getApplicationUsage()->addCommandLineOption("--CullBounds", "LiSPSM optimize shadow for bounds of culled objects in view frustum (better option)."); 
     524    arguments.getApplicationUsage()->addCommandLineOption("--DrawBounds", "LiSPSM optimize shadow for bounds of predrawn pixels in view frustum (best & default)."); 
     525    arguments.getApplicationUsage()->addCommandLineOption("--mapres", "LiSPSM texture resolution."); 
     526    arguments.getApplicationUsage()->addCommandLineOption("--maxFarDist", "LiSPSM max far distance to shadow."); 
     527    arguments.getApplicationUsage()->addCommandLineOption("--moveVCamFactor", "LiSPSM move the virtual frustum behind the real camera, (also back ground object can cast shadow)."); 
     528    arguments.getApplicationUsage()->addCommandLineOption("--minLightMargin", "LiSPSM the same as --moveVCamFactor."); 
    508529 
    509530    arguments.getApplicationUsage()->addCommandLineOption("-1", "Use test model one."); 
    510531    arguments.getApplicationUsage()->addCommandLineOption("-2", "Use test model two."); 
    511     arguments.getApplicationUsage()->addCommandLineOption("-3", "Use test model three."); 
     532    arguments.getApplicationUsage()->addCommandLineOption("-3", "Use test model three (default)."); 
     533    arguments.getApplicationUsage()->addCommandLineOption("-4", "Use test model four - island scene."); 
    512534    arguments.getApplicationUsage()->addCommandLineOption("--two-sided", "Use two-sided stencil extension for shadow volumes."); 
    513535    arguments.getApplicationUsage()->addCommandLineOption("--two-pass", "Use two-pass stencil for shadow volumes."); 
     
    526548    while (arguments.read("--positionalLight")) postionalLight = true; 
    527549    while (arguments.read("--directionalLight")) postionalLight = false; 
     550 
     551    while (arguments.read("--castsShadowMask", CastsShadowTraversalMask )); 
     552    while (arguments.read("--receivesShadowMask", ReceivesShadowTraversalMask )); 
    528553 
    529554    bool updateLightPosition = true; 
     
    639664        shadowedScene->setShadowTechnique(sm.get()); 
    640665    } 
     666    else if ( arguments.read("--lispsm") ) 
     667    { 
     668        osg::ref_ptr<osgShadow::MinimalShadowMap> sm = NULL; 
     669 
     670        if( arguments.read( "--ViewBounds" ) ) 
     671            sm = new osgShadow::LightSpacePerspectiveShadowMapVB; 
     672        else if( arguments.read( "--CullBounds" ) ) 
     673            sm = new osgShadow::LightSpacePerspectiveShadowMapCB; 
     674        else // if( arguments.read( "--DrawBounds" ) ) // default 
     675            sm = new osgShadow::LightSpacePerspectiveShadowMapDB; 
     676 
     677        shadowedScene->setShadowTechnique( sm.get() ); 
     678 
     679        if( sm.valid() )  
     680        { 
     681            while( arguments.read("--debugHUD") )            
     682                sm->setDebugDraw( true ); 
     683 
     684            float minLightMargin = 10.f; 
     685            float maxFarPlane = 0; 
     686            unsigned int texSize = 1024; 
     687            unsigned int baseTexUnit = 0; 
     688            unsigned int shadowTexUnit = 1; 
     689 
     690            while ( arguments.read("--moveVCamFactor", minLightMargin ) ); 
     691            while ( arguments.read("--minLightMargin", minLightMargin ) ); 
     692            while ( arguments.read("--maxFarDist", maxFarPlane ) ); 
     693            while ( arguments.read("--mapres", texSize )); 
     694            while ( arguments.read("--baseTextureUnit", baseTexUnit) ); 
     695            while ( arguments.read("--shadowTextureUnit", shadowTexUnit) ); 
     696 
     697            sm->setMinLightMargin( minLightMargin ); 
     698            sm->setMaxFarPlane( maxFarPlane ); 
     699            sm->setTextureSize( osg::Vec2s( texSize, texSize ) ); 
     700            sm->setShadowTextureCoordIndex( shadowTexUnit ); 
     701            sm->setShadowTextureUnit( shadowTexUnit ); 
     702            sm->setBaseTextureCoordIndex( baseTexUnit ); 
     703            sm->setBaseTextureUnit( baseTexUnit ); 
     704        }  
     705    } 
    641706    else /* if (arguments.read("--sm")) */ 
    642707    { 
     
    647712        while (arguments.read("--mapres", mapres)) 
    648713            sm->setTextureSize(osg::Vec2s(mapres,mapres)); 
    649    } 
     714    } 
    650715 
    651716    osg::ref_ptr<osg::Node> model = osgDB::readNodeFiles(arguments); 
     
    710775 
    711776        //set the LightSource, only for checking, there is only 1 light in the scene 
    712         dynamic_cast<osgShadow::ShadowMap*>(shadowedScene->getShadowTechnique())->setLight(ls.get()); 
     777        osgShadow::ShadowMap* shadowMap = dynamic_cast<osgShadow::ShadowMap*>(shadowedScene->getShadowTechnique()); 
     778        if( shadowMap ) shadowMap->setLight(ls.get()); 
    713779    } 
    714780 
     
    741807 
    742808        osgShadow::ShadowMap* sm = dynamic_cast<osgShadow::ShadowMap*>(shadowedScene->getShadowTechnique()); 
    743         osg::ref_ptr<osg::Camera> hudCamera = sm->makeDebugHUD(); 
    744  
    745         // set up cameras to rendering on the first window available. 
    746         hudCamera->setGraphicsContext(windows[0]); 
    747         hudCamera->setViewport(0,0,windows[0]->getTraits()->width, windows[0]->getTraits()->height); 
    748  
    749         viewer.addSlave(hudCamera.get(), false); 
     809        if( sm ) {  
     810            osg::ref_ptr<osg::Camera> hudCamera = sm->makeDebugHUD(); 
     811 
     812            // set up cameras to rendering on the first window available. 
     813            hudCamera->setGraphicsContext(windows[0]); 
     814            hudCamera->setViewport(0,0,windows[0]->getTraits()->width, windows[0]->getTraits()->height); 
     815 
     816            viewer.addSlave(hudCamera.get(), false); 
     817        } 
    750818    } 
    751819 
     
    755823    while (!viewer.done()) 
    756824    { 
     825        { 
     826            osgShadow::MinimalShadowMap * msm = dynamic_cast<osgShadow::MinimalShadowMap*>( shadowedScene->getShadowTechnique() ); 
     827    
     828            if( msm ) { 
     829 
     830                // If scene decorated by CoordinateSystemNode try to find localToWorld  
     831                // and set modellingSpaceToWorld matrix to optimize scene bounds computation 
     832 
     833                osg::NodePath np = viewer.getCoordinateSystemNodePath(); 
     834                if( !np.empty() ) { 
     835                    osg::CoordinateSystemNode * csn =  
     836                        dynamic_cast<osg::CoordinateSystemNode *>( np.back() ); 
     837 
     838                    if( csn ) { 
     839                        osg::Vec3d pos =  
     840                            viewer.getCameraManipulator()->getMatrix().getTrans(); 
     841 
     842                        msm->setModellingSpaceToWorldTransform 
     843                            ( csn->computeLocalCoordinateFrame( pos ) ); 
     844                    } 
     845                } 
     846            }         
     847        } 
     848 
    757849        if (updateLightPosition) 
    758850        { 
    759851            float t = viewer.getFrameStamp()->getSimulationTime(); 
     852 
    760853            if (postionalLight) 
    761854            {