Show
Ignore:
Timestamp:
10/02/07 23:45:09 (7 years ago)
Author:
robert
Message:

From Mihair Radu, "Most of the additions are small utility methods:
- set the resolution of the shadow map; it calls dirty() to
re-initialize at next update
- keep a list of Shader objects to use instead of the default ones, if
the list is empty, the default shaders are used
- explicitly create the Uniform variables, so that subsequent additions
that require more Uniforms can put them in a central place
- set a Light or LightSource? to use explicitly for shadow casting,
allows multiple lights in the scene, with one casting shadows

There are two additions that do not ( yet ) function correctly, but in
the present usage they do not interfere with the regular usage of the
techique:
- support for using spotlights, it's using Light.spotCutoff to determine
if it's a spot-light and not point-light,

there is an error in the setup of either the shadow camera or the

texgen, most likely due to the direction of the spotlight, since the
position is being used just like in point or directional lights.
- creation of a debugHUD

the hud is created properly, ( the example included shows it ), but

it displays only white, there has been some discussion of displaying the
shadow map, but I could not find it, the addition of a simple fragment
shader with the appropriate color transform should get this going."

Files:
1 modified

Legend:

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

    r7499 r7539  
    666666        osg::ref_ptr<osgShadow::ShadowMap> sm = new osgShadow::ShadowMap; 
    667667        shadowedScene->setShadowTechnique(sm.get()); 
    668     } 
     668 
     669        int mapres = 1024; 
     670        while (arguments.read("--mapres", mapres))  
     671            sm->setTextureSize(osg::Vec2s(mapres,mapres)); 
     672   } 
    669673     
    670674    osg::ref_ptr<osg::Node> model = osgDB::readNodeFiles(arguments); 
     
    716720    osg::ref_ptr<osg::LightSource> ls = new osg::LightSource; 
    717721    ls->getLight()->setPosition(lightpos); 
     722     
     723    bool spotlight = false; 
     724    if (arguments.read("--spotLight")) 
     725    { 
     726        spotlight = true; 
     727 
     728        osg::Vec3 center = bb.center(); 
     729        osg::Vec3 lightdir = center - osg::Vec3(lightpos.x(), lightpos.y(), lightpos.z()); 
     730        lightdir.normalize(); 
     731        ls->getLight()->setDirection(lightdir); 
     732        ls->getLight()->setSpotCutoff(30.0f); 
     733 
     734        //set the LightSource, only for checking, there is only 1 light in the scene 
     735        dynamic_cast<osgShadow::ShadowMap*>(shadowedScene->getShadowTechnique())->setLight(ls.get()); 
     736    } 
     737 
    718738    if ( arguments.read("--coloured-light")) 
    719739    { 
     
    731751     
    732752    viewer.setSceneData(shadowedScene.get()); 
    733      
     753 
    734754    // create the windows and run the threads. 
    735755    viewer.realize(); 
     756 
     757    // it is done after viewer.realize() so that the windows are already initialized 
     758    if ( arguments.read("--debugHUD")) 
     759    { 
     760        osgViewer::Viewer::Windows windows; 
     761        viewer.getWindows(windows); 
     762         
     763        if (windows.empty()) return 1; 
     764         
     765        osgShadow::ShadowMap* sm = dynamic_cast<osgShadow::ShadowMap*>(shadowedScene->getShadowTechnique()); 
     766        osg::ref_ptr<osg::Camera> hudCamera = sm->makeDebugHUD(); 
     767         
     768        // set up cameras to rendering on the first window available. 
     769        hudCamera->setGraphicsContext(windows[0]); 
     770        hudCamera->setViewport(0,0,windows[0]->getTraits()->width, windows[0]->getTraits()->height); 
     771 
     772        viewer.addSlave(hudCamera.get(), false); 
     773    } 
     774 
    736775 
    737776    // osgDB::writeNodeFile(*group,"test.osg"); 
     
    744783            if (postionalLight) 
    745784            { 
    746                 lightpos.set(bb.center().x()+sinf(t)*bb.radius(), bb.center().y() + cosf(t)*bb.radius(), bb.zMax() + bb.radius()  ,1.0f); 
     785                lightpos.set(bb.center().x()+sinf(t)*bb.radius(), bb.center().y() + cosf(t)*bb.radius(), bb.zMax() + bb.radius()*2.0f  ,1.0f); 
    747786            } 
    748787            else 
     
    753792             
    754793            osg::Vec3f lightDir(-lightpos.x(),-lightpos.y(),-lightpos.z()); 
     794            if(spotlight) 
     795                lightDir =  osg::Vec3(bb.center().x()+sinf(t)*bb.radius()/2.0, bb.center().y() + cosf(t)*bb.radius()/2.0, bb.center().z())  
     796                - osg::Vec3(lightpos.x(), lightpos.y(), lightpos.z()) ; 
    755797            lightDir.normalize(); 
    756798            ls->getLight()->setDirection(lightDir);