Changeset 7462

Show
Ignore:
Timestamp:
09/24/07 17:24:23
Author:
robert
Message:

From Adrian Egli, further work on PSSM implementation.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • OpenSceneGraph/trunk/examples/osgshadow/osgshadow.cpp

    r7452 r7462  
    500500    arguments.getApplicationUsage()->addCommandLineOption("--mapres", "ParallelSplitShadowMap texture resolution.");//ADEGLI 
    501501    arguments.getApplicationUsage()->addCommandLineOption("--debug-color", "ParallelSplitShadowMap display debugging color (only the first 3 maps are color r=0,g=1,b=2.");//ADEGLI 
    502  
     502    arguments.getApplicationUsage()->addCommandLineOption("--minNearSplit", "ParallelSplitShadowMap shadow map near offset.");//ADEGLI 
     503    arguments.getApplicationUsage()->addCommandLineOption("--maxFarDist", "ParallelSplitShadowMap max far distance to shadow.");//ADEGLI 
     504    arguments.getApplicationUsage()->addCommandLineOption("--NVidea", "ParallelSplitShadowMap set default PolygonOffset for NVidea.");//ADEGLI 
     505    arguments.getApplicationUsage()->addCommandLineOption("--PolyOffset-Factor", "ParallelSplitShadowMap set PolygonOffset factor.");//ADEGLI 
     506    arguments.getApplicationUsage()->addCommandLineOption("--PolyOffset-Unit", "ParallelSplitShadowMap set PolygonOffset unit.");//ADEGLI 
     507    arguments.getApplicationUsage()->addCommandLineOption("--CullFaceFront", "ParallelSplitShadowMap add a cull face: front.");//ADEGLI 
     508 
     509     
    503510    arguments.getApplicationUsage()->addCommandLineOption("-1", "Use test model one."); 
    504511    arguments.getApplicationUsage()->addCommandLineOption("-2", "Use test model two."); 
     
    626633        osg::ref_ptr<osgShadow::ParallelSplitShadowMap> pssm = new osgShadow::ParallelSplitShadowMap(NULL,mapcount); 
    627634 
    628         unsigned int mapres = 1024; 
    629         while (arguments.read("--mapres", mapres)); 
    630         pssm->setTextureResolution(mapres); 
     635        int mapres = 1024; 
     636        while (arguments.read("--mapres", mapres))  
     637            pssm->setTextureResolution(mapres); 
    631638         
    632639        while (arguments.read("--debug-color")) { pssm->setDebugColorOn(); } 
    633640          
     641 
     642        int minNearSplit=0; 
     643        while (arguments.read("--minNearSplit", minNearSplit)) 
     644            if ( minNearSplit > 0 ) { 
     645                pssm->setMinNearDistanceForSplits(minNearSplit); 
     646                std::cout << "ParallelSplitShadowMap : setMinNearDistanceForSplits(" << minNearSplit <<")" << std::endl; 
     647            } 
     648 
     649        int maxfardist = 0; 
     650        while (arguments.read("--maxFarDist", maxfardist)) 
     651            if ( maxfardist > 0 ) { 
     652                pssm->setMaxFarDistance(maxfardist); 
     653                std::cout << "ParallelSplitShadowMap : setMaxFarDistance(" << maxfardist<<")" << std::endl; 
     654            } 
     655 
     656        double polyoffsetfactor = -0.02; 
     657        double polyoffsetunit = 1.0; 
     658        while (arguments.read("--PolyOffset-Factor", polyoffsetfactor)); 
     659        while (arguments.read("--PolyOffset-Unit", polyoffsetunit)); 
     660        pssm->setPolygonOffset(osg::Vec2(polyoffsetfactor,polyoffsetunit)); //ATI Radeon 
     661 
     662        if (arguments.read("--NVidea")){ 
     663            //pssm->setPolygonOffset(osg::Vec2(-0.02,1.0)); //ATI Radeon 
     664            pssm->setPolygonOffset(osg::Vec2(10.0f,20.0f)); //NVidea  
     665        } 
     666 
     667        if ( arguments.read("--CullFaceFront") ) { 
     668            pssm->forceFrontCullFace(); 
     669        } 
    634670 
    635671        shadowedScene->setShadowTechnique(pssm.get()); 
  • OpenSceneGraph/trunk/include/osgShadow/ParallelSplitShadowMap

    r7452 r7462  
    2121 
    2222#include <osgShadow/ShadowTechnique> 
    23   
     23 
    2424namespace osgShadow { 
     25 
    2526class OSGSHADOW_EXPORT ParallelSplitShadowMap :  public ShadowTechnique  
    2627{ 
     
    5758        inline void setTextureResolution(unsigned int resolution) { _resolution = resolution; } 
    5859 
     60        /** Set the max far distance */  
     61        inline void setMaxFarDistance(double farDist) { _setMaxFarDistance = farDist; _isSetMaxFarDistance = true; } 
     62 
     63        /** Force to add a osg::CullFace::FRONT state */ 
     64        inline void forceFrontCullFace() { _useFrontCullFace = true; } 
     65 
     66        /** Set min near distance for splits */  
     67        inline void setMinNearDistanceForSplits(double nd){ _split_min_near_dist=nd; } 
     68 
     69        /** use linear split (default: linear) */ 
     70        inline void useLinearSplit(bool flag) { _linearSplit = flag;} 
     71     
     72     
     73    /**  
     74         light              /  |  
     75           \             /     | 
     76     min near dist    /        | 
     77      for splits   /           | 
     78              \ /    Š         | 
     79            ./ \     Š         | 
     80            |   \    Š         | 
     81            |    x   Š         | 
     82            |        Š         | 
     83            .        |         | 
     84               \       Š         | 
     85                \    Š         | 
     86                    \          | 
     87                        \      | 
     88                              \  | 
     89                               
     90            .<- max far dist. ->. 
     91    */  
    5992    protected : 
    6093 
     
    120153        unsigned int _resolution; 
    121154 
     155        double _setMaxFarDistance; 
     156        bool _isSetMaxFarDistance; 
     157 
     158        bool _useFrontCullFace; 
     159 
     160        double _split_min_near_dist; 
     161         
     162        bool _linearSplit; 
     163 
    122164}; 
    123165} 
  • OpenSceneGraph/trunk/src/osgShadow/ParallelSplitShadowMap.cpp

    r7452 r7462  
    2020 
    2121#include <osgShadow/ParallelSplitShadowMap> 
     22 
    2223#include <osgShadow/ShadowedScene> 
    2324#include <osg/Notify> 
     
    3334#include <osg/Texture1D> 
    3435 
     36 
     37 
    3538using namespace osgShadow; 
     39 
    3640 
    3741 
     
    110114 
    111115 
    112         sstr << "    float c0=0.5;" << std::endl; 
    113         sstr << "    float c1=0.5;" << std::endl; 
    114         sstr << "    float c2=0.5;" << std::endl; 
     116        sstr << "    float c0=0.0;" << std::endl; 
     117        sstr << "    float c1=0.0;" << std::endl; 
     118        sstr << "    float c2=0.0;" << std::endl; 
     119 
     120        sstr << "    float sumTerm=0.0;" << std::endl; 
    115121 
    116122        for (unsigned int i=0;i<_number_of_splits;i++)    { 
    117             if ( i < 3 ) sstr << "    c" << i << "=0.5+0.5*term" << i << ";" << std::endl; 
    118         } 
    119  
    120         sstr << "    vec4 color    = gl_Color*vec4(c0,c1,c2,1.0); "    << std::endl; 
    121  
    122         sstr << "    vec4 texcolor = vec4(1,1,1,1); "    << std::endl; 
     123            if ( i < 3 ) sstr << "    c" << i << "=term" << i << ";" << std::endl; 
     124            sstr << "    sumTerm=sumTerm+term" << i << ";" << std::endl; 
     125        } 
     126 
     127        sstr << "    vec4 color    = gl_Color * ( 1.0 - sumTerm ) + (sumTerm)* gl_Color*vec4(c0,(1.0-c0)*c1,(1.0-c0)*(1.0-c1)*c2,1.0); "    << std::endl; 
     128 
    123129    } else { 
    124130        sstr << "    vec4 color    = gl_Color; "<< std::endl; 
    125         sstr << "    vec4 texcolor = texture2D(baseTexture,gl_TexCoord[0].st); "    << std::endl; 
    126     } 
    127  
    128  
     131    } 
     132 
     133 
     134    sstr << "    vec4 texcolor = texture2D(baseTexture,gl_TexCoord[0].st); "    << std::endl; 
    129135 
    130136 
    131137    sstr << "    float enableBaseTextureFilter = enableBaseTexture*(1.0 - step(texcolor.x+texcolor.y+texcolor.z+texcolor.a,0.0)); "    << std::endl;                                                //18 
    132     sstr << "    vec4 colorTex = color*texcolor;" << std::endl; 
     138    sstr << "   vec4 colorTex = color*texcolor;" << std::endl; 
    133139    sstr << "    gl_FragColor = (color*(1.0-enableBaseTextureFilter) + colorTex*enableBaseTextureFilter)*(1.0-0.30*v); "<< std::endl; 
    134  
    135  
    136140 
    137141 
     
    160164    _debug_color_in_GLSL(false), 
    161165    _user_polgyonOffset_set(false), 
    162     _resolution(TEXTURE_RESOLUTION) 
     166    _resolution(TEXTURE_RESOLUTION), 
     167    _isSetMaxFarDistance(false), 
     168    _useFrontCullFace(false), 
     169    _split_min_near_dist(ZNEAR_MIN_FROM_LIGHT_SOURCE), 
     170    _linearSplit(LINEAR_SPLIT) 
    163171{ 
    164172    _displayTexturesGroupingNode = gr; 
     
    172180    ShadowTechnique(copy,copyop), 
    173181    _textureUnitOffset(copy._textureUnitOffset), 
    174     _number_of_splits(copy._number_of_splits) 
     182    _debug_color_in_GLSL(copy._debug_color_in_GLSL), 
     183    _user_polgyonOffset_set(copy._user_polgyonOffset_set), 
     184    _resolution(copy._resolution), 
     185    _isSetMaxFarDistance(copy._isSetMaxFarDistance), 
     186    _useFrontCullFace(copy._useFrontCullFace), 
     187    _split_min_near_dist(copy._split_min_near_dist), 
     188    _linearSplit(copy._linearSplit), 
     189    _number_of_splits(copy._number_of_splits), 
     190    _polgyonOffset(copy._polgyonOffset), 
     191    _setMaxFarDistance(copy._setMaxFarDistance) 
     192 
    175193{ 
    176194} 
     
    244262                } 
    245263            #endif 
    246             pssmShadowSplitTexture._camera->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR); 
     264            pssmShadowSplitTexture._camera->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR); 
    247265 
    248266            // set viewport 
     
    274292                stateset->setMode(GL_POLYGON_OFFSET_FILL, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); 
    275293            } 
     294 
     295            if ( _useFrontCullFace ) { 
     296                osg::ref_ptr<osg::CullFace> cull_face = new osg::CullFace; 
     297                cull_face->setMode(osg::CullFace::FRONT); 
     298                stateset->setAttribute(cull_face.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); 
     299                stateset->setMode(GL_CULL_FACE, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); 
     300            } 
    276301        } 
    277302        // set up stateset and append texture, texGen ,... 
     
    305330            pssmShadowSplitTexture._stateset->addUniform(shadowTextureSampler); 
    306331 
    307             //TODO: NOT YET SUPPORTED in the current version of the shader  
     332            //TODO: NOT YET SUPPORTED in the current version of the shader 
    308333            //std::stringstream strAB; strAB << "ambientBias" << (pssmShadowSplitTexture._textureUnit-1); 
    309334            //osg::Uniform* ambientBias = new osg::Uniform(strAB.str().c_str(),pssmShadowSplitTexture._ambientBias); 
     
    331356            { 
    332357                // fake texture for baseTexture, add a fake texture 
    333                 // we support by default at least one texture layer  
    334                 // without this fake texture we can not support  
    335                 // textured and not textured scene  
    336  
    337                 // TODO: at the moment the PSSM supports just one texture layer in the GLSL shader, multitexture are  
     358                // we support by default at least one texture layer 
     359                // without this fake texture we can not support 
     360                // textured and not textured scene 
     361 
     362                // TODO: at the moment the PSSM supports just one texture layer in the GLSL shader, multitexture are 
    338363                //       not yet supported ! 
    339364 
     
    642667    pssmShadowSplitTexture._cameraProj.getPerspective(fovy,aspectRatio,camNear,camFar); 
    643668 
     669 
     670 
     671    if ( _isSetMaxFarDistance ) { 
     672        if ( camNear + _setMaxFarDistance < camFar) camFar = camNear + _setMaxFarDistance; 
     673    } 
     674 
    644675    ////////////////////////////////////////////////////////////////////////// 
    645676    /// CALCULATE SPLIT 
     
    647678    double minNear = camNear; 
    648679    double camNearFar_Dist = maxFar - camNear; 
    649     bool linear = LINEAR_SPLIT; 
    650     if ( linear ) { 
     680    if ( _linearSplit ) { 
    651681         camFar  = camNear + (camNearFar_Dist) * ((double)(pssmShadowSplitTexture._splitID+1))/((double)(_number_of_splits)); 
    652682        camNear = camNear + (camNearFar_Dist) * ((double)(pssmShadowSplitTexture._splitID))/((double)(_number_of_splits)); 
     
    687717    ////////////////////////////////////////////////////////////////////////// 
    688718    /// TRANSFORM frustum corners (Optimized for Orthogonal) 
    689      osg::Matrixf projMat; 
    690      projMat.makePerspective(fovy,aspectRatio,camNear,camFar); 
    691  
    692     osg::Matrixf projViewMat = pssmShadowSplitTexture._cameraView*projMat; 
     719 
    693720    osg::Matrixf invProjViewMat; 
     721 
     722    osg::Matrixf projMat; 
     723    projMat.makePerspective(fovy,aspectRatio,camNear,camFar); 
     724    osg::Matrixf projViewMat(pssmShadowSplitTexture._cameraView*projMat); 
    694725    invProjViewMat.invert(projViewMat); 
     726 
     727 
     728 
    695729 
    696730    //transform frustum vertices to world space 
     
    710744// compute directional light inital postion; 
    711745void ParallelSplitShadowMap::calculateLightInitalPosition(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners){ 
    712     pssmShadowSplitTexture._frustumSplitCenter = frustumCorners[0]; 
     746    pssmShadowSplitTexture._frustumSplitCenter = frustumCorners[0]; 
    713747    for(int i=1;i<8;i++) { 
    714748        pssmShadowSplitTexture._frustumSplitCenter +=frustumCorners[i]; 
     
    733767    // set 2.0m distance to the nearest point 
    734768    int count = 0; 
    735     while (zNear <= ZNEAR_MIN_FROM_LIGHT_SOURCE && count++ < 10) { 
     769    while (zNear <= _split_min_near_dist && count++ < 10) { 
    736770        zNear= DBL_MAX; 
    737771        zFar =-DBL_MAX; 
     
    742776         } 
    743777 
    744         if ( zNear <= ZNEAR_MIN_FROM_LIGHT_SOURCE ){ 
    745             osg::Vec3 dUpdate = - pssmShadowSplitTexture._lightDirection*(fabs(zNear)+ZNEAR_MIN_FROM_LIGHT_SOURCE); 
     778        if ( zNear <= _split_min_near_dist ){ 
     779            osg::Vec3 dUpdate = - pssmShadowSplitTexture._lightDirection*(fabs(zNear)+_split_min_near_dist); 
    746780            pssmShadowSplitTexture._lightCameraSource = pssmShadowSplitTexture._lightCameraSource + dUpdate; 
    747781        }