Show
Ignore:
Timestamp:
01/18/07 23:32:18 (8 years ago)
Author:
robert
Message:

Added osg::StencilTwoSided? state attribute to wrap up the stencil_two_sided extension.

Added path in osgShadow work for using StencilTwoSided? to accelerate shadow volume rendering.

Files:
1 modified

Legend:

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

    r5966 r6010  
    66#include <osg/Camera> 
    77#include <osg/Stencil> 
     8#include <osg/StencilTwoSided> 
    89#include <osg/CullFace> 
    910#include <osg/Geometry> 
    1011 
    1112#include <osgGA/TrackballManipulator> 
     13#include <osgGA/AnimationPathManipulator> 
    1214 
    1315#include <osgViewer/Viewer> 
     
    1618 
    1719#include <osgDB/ReadFile> 
     20#include <osgDB/WriteFile> 
    1821 
    1922#include <iostream> 
     
    4851    } 
    4952         
     53    void getBase(osg::Polytope& polytope, float margin=0.1) const 
     54    { 
     55        float delta = _bb.radius()*margin; 
     56        polytope.add( osg::Plane(0.0, 0.0, 1.0, -(_bb.zMin()-delta)) ); 
     57    } 
     58     
    5059    void apply(osg::Node& node) 
    5160    { 
     
    153162    bool doShadow = true; 
    154163    while (arguments.read("--noShadow")) doShadow = false; 
     164     
     165    osgShadow::ShadowVolumeGeometry::DrawMode drawMode = osgShadow::ShadowVolumeGeometry::STENCIL_TWO_PASS; 
     166    while (arguments.read("--two-sided")) drawMode = osgShadow::ShadowVolumeGeometry::STENCIL_TWO_SIDED; 
     167     
     168 
     169    std::string pathfile; 
     170    while (arguments.read("-p",pathfile)) 
     171    { 
     172        osg::ref_ptr<osgGA::AnimationPathManipulator> apm = new osgGA::AnimationPathManipulator(pathfile); 
     173        if (apm->valid()) viewer.setCameraManipulator(apm.get()); 
     174    } 
     175 
     176    if (!viewer.getCameraManipulator()) viewer.setCameraManipulator( new osgGA::TrackballManipulator() ); 
    155177 
    156178    // any option left unread are converted into errors to write out later. 
     
    205227    osg::ref_ptr<osgShadow::OccluderGeometry> occluder = new osgShadow::OccluderGeometry; 
    206228    occluder->computeOccluderGeometry(model.get()); 
    207     cbbv.getPolytope(occluder->getBoundingPolytope(),0.001); 
     229//    cbbv.getPolytope(occluder->getBoundingPolytope(),0.001); 
     230    cbbv.getBase(occluder->getBoundingPolytope(),0.001); 
    208231 
    209232    if (addOccluderToScene) 
     
    216239    osg::ref_ptr<osgShadow::ShadowVolumeGeometry> shadowVolume = new osgShadow::ShadowVolumeGeometry; 
    217240 
    218     shadowVolume->setUseDisplayList(!updateLightPosition); 
     241    // shadowVolume->setUseDisplayList(!updateLightPosition); 
     242    shadowVolume->setUseDisplayList(false); 
    219243 
    220244    osg::Vec4 lightpos; 
     
    252276        osg::Vec4 zero_colour(0.0,0.0,0.0,1.0); 
    253277     
    254         // first group 
     278        // first group, render the depth buffer + ambient light contribution 
    255279        { 
    256280 
     
    292316                osg::ref_ptr<osg::Geode> geode = new osg::Geode; 
    293317                occluder->computeShadowVolumeGeometry(lightpos, *shadowVolume); 
    294                 geode->addDrawable(shadowVolume.get()); 
    295  
    296                 // switch off the writing to the color bit planes. 
    297                 osg::ColorMask* colourMask = new osg::ColorMask; 
    298                 colourMask->setMask(false,false,false,false); 
    299  
    300                 osg::Stencil* stencil = new osg::Stencil; 
    301                 stencil->setFunction(osg::Stencil::ALWAYS,0,~0u); 
    302                 stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::INCR); 
    303  
    304                 osg::StateSet* ss_sv1 = geode->getOrCreateStateSet(); 
    305                 ss_sv1->setRenderBinDetails(0, "RenderBin"); 
    306                 ss_sv1->setAttributeAndModes(stencil,osg::StateAttribute::ON); 
    307                 ss_sv1->setAttribute(colourMask); 
     318                shadowVolume->setDrawMode(drawMode); 
    308319                 
    309                 ss_sv1->setAttributeAndModes(new osg::CullFace(osg::CullFace::BACK), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); 
    310  
    311                 camera->addChild(geode.get()); 
     320 
     321                if (drawMode == osgShadow::ShadowVolumeGeometry::STENCIL_TWO_SIDED) 
     322                { 
     323                    osg::notify(osg::NOTICE)<<"STENCIL_TWO_SIDED seleteced"<<std::endl; 
     324 
     325                    osg::StencilTwoSided* stencil = new osg::StencilTwoSided; 
     326                    stencil->setFunction(osg::StencilTwoSided::BACK, osg::StencilTwoSided::ALWAYS,0,~0u); 
     327                    stencil->setOperation(osg::StencilTwoSided::BACK, osg::StencilTwoSided::KEEP, osg::StencilTwoSided::KEEP, osg::StencilTwoSided::DECR_WRAP); 
     328                    stencil->setFunction(osg::StencilTwoSided::FRONT, osg::StencilTwoSided::ALWAYS,0,~0u); 
     329                    stencil->setOperation(osg::StencilTwoSided::FRONT, osg::StencilTwoSided::KEEP, osg::StencilTwoSided::KEEP, osg::StencilTwoSided::INCR_WRAP); 
     330 
     331 
     332                    osg::ColorMask* colourMask = new osg::ColorMask(false, false, false, false); 
     333 
     334                    osg::StateSet* ss_sv1 = geode->getOrCreateStateSet(); 
     335                    ss_sv1->setRenderBinDetails(0, "RenderBin"); 
     336                    ss_sv1->setAttributeAndModes(stencil,osg::StateAttribute::ON); 
     337                    ss_sv1->setAttribute(colourMask); 
     338                    ss_sv1->setMode(GL_CULL_FACE,osg::StateAttribute::OFF); 
     339                     
     340                    geode->addDrawable(shadowVolume.get()); 
     341                     
     342                    camera->addChild(geode.get()); 
     343 
     344                } 
     345                else 
     346                { 
     347                    osg::notify(osg::NOTICE)<<"STENCIL_TWO_PASSES seleteced"<<std::endl; 
     348 
     349                    osg::Stencil* stencil = new osg::Stencil; 
     350                    stencil->setFunction(osg::Stencil::ALWAYS,0,~0u); 
     351                    stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::KEEP); 
     352 
     353                    osg::ColorMask* colourMask = new osg::ColorMask(false, false, false, false); 
     354 
     355                    osg::StateSet* ss_sv1 = geode->getOrCreateStateSet(); 
     356                    ss_sv1->setRenderBinDetails(0, "RenderBin"); 
     357                    ss_sv1->setAttributeAndModes(stencil,osg::StateAttribute::ON); 
     358                    ss_sv1->setAttribute(colourMask); 
     359                    ss_sv1->setMode(GL_CULL_FACE,osg::StateAttribute::ON); 
     360                     
     361                    geode->addDrawable(shadowVolume.get()); 
     362                     
     363                    camera->addChild(geode.get()); 
     364                } 
     365 
    312366            } 
    313              
    314             if (true) 
    315             { 
    316                 osg::ref_ptr<osg::Geode> geode = new osg::Geode; 
    317                 occluder->computeShadowVolumeGeometry(lightpos, *shadowVolume); 
    318                 geode->addDrawable(shadowVolume.get()); 
    319  
    320                 // switch off the writing to the color bit planes. 
    321                 osg::ColorMask* colourMask = new osg::ColorMask; 
    322                 colourMask->setMask(false,false,false,false); 
    323  
    324                 osg::Stencil* stencil = new osg::Stencil; 
    325                 stencil->setFunction(osg::Stencil::ALWAYS,0,~0u); 
    326                 stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::DECR); 
    327  
    328                 osg::StateSet* ss_sv1 = geode->getOrCreateStateSet(); 
    329                 ss_sv1->setRenderBinDetails(1, "RenderBin"); 
    330                 ss_sv1->setAttributeAndModes(stencil,osg::StateAttribute::ON); 
    331                 ss_sv1->setAttribute(colourMask); 
    332                  
    333                 ss_sv1->setAttributeAndModes(new osg::CullFace(osg::CullFace::FRONT), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); 
    334  
    335                 camera->addChild(geode.get()); 
    336             } 
    337  
     367 
     368 
     369            // render scene graph adding contribution of light 
    338370            { 
    339371                osg::Group* second_model_group = new osg::Group; 
     
    364396                ss1->setAttributeAndModes(stencil,osg::StateAttribute::ON); 
    365397 
    366  
    367398                osg::BlendFunc* blend = new osg::BlendFunc; 
    368399                blend->setFunction(osg::BlendFunc::ONE, osg::BlendFunc::ONE); 
     
    382413    viewer.setSceneData(group.get()); 
    383414 
    384  
    385     viewer.setCameraManipulator(new osgGA::TrackballManipulator()); 
    386  
    387  
    388     osg::notify(osg::NOTICE)<<"Warning: Stencil buffer required, but not yet switched on."<<std::endl; 
    389  
    390  
    391415    // create the windows and run the threads. 
    392416    viewer.realize(); 
     417 
     418    osgDB::writeNodeFile(*group,"test.osg"); 
    393419 
    394420    while (!viewer.done())