Hi all,

I'm working on inserting the shadow capability to our app. 
for learning purpose i created a small and simple program. 
if i don't add camera to the scene - i do see the shadow, but when I add a 
camera to the scene - i don't . 
I'm sure i'm missing something and/or doing something wrong but I don’t know 
what  :( 

I attached cpp with my code...


PLEASE HELP    :) 

Thanks a lot!

Cheers,
Hadas

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=13760#13760



// ShadowTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <conio.h>
#include <ctype.h>

#include <osg/ArgumentParser>
#include <osg/Group>
#include <osg/Geode>
#include <osg/Geometry>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include <osgViewer/Viewer>
#include <osg/CameraNode>

#include <osgShadow/ShadowedScene>
#include <osgShadow/ShadowMap>
#include <osgShadow/ShadowTexture>

#include <osg/LightSource>
#include <osg/Material>

static int ReceivesShadowTraversalMask = 0x1;
static int CastsShadowTraversalMask = 0x2;

int _tmain(int argc, char* argv[])
{
        //================================================
        // application standard initialization
        //------------------------------------
        // use an ArgumentParser object to manage the program arguments.
        osg::ArgumentParser arguments(&argc,argv);

        // set up the usage document, in case we need to print out how to use 
this program.
        arguments.getApplicationUsage()->setDescription(                
arguments.getApplicationName()+ ",,,");
        arguments.getApplicationUsage()->addCommandLineOption(  "-h or 
--help","Display this information");
        arguments.getApplicationUsage()->setCommandLineUsage(   
arguments.getApplicationName()+ " [options] filename ...");

        // construct the viewer.
        osgViewer::Viewer viewer(arguments);

        // if user request help write it out to cout.
        if (arguments.read("-h") || arguments.read("--help"))
        {
                return 1;
        }
        // any option left unread are converted into errors to write out later.
        arguments.reportRemainingOptionsAsUnrecognized();

        // report any errors if they have occurred when parsing the program 
arguments.
        if (arguments.errors())
        {
                return 1;
        }
        //================================================

        // scene objects
        osg::Group* root = new osg::Group;

        osg::Node* modelNode = NULL;
        std::string FilePathName = "C:\\OSG\\OpenSceneGraph-Data\\cessna.osg";
        modelNode       =       
(osg::Group*)osgDB::readNodeFile((std::string)FilePathName); 

        osg::Node * m2 = 
(osg::Group*)osgDB::readNodeFile("C:\\OSG\\OpenSceneGraph-Data\\cow.osg");
        osg::Matrix mat;
        mat.setTrans(osg::Vec3(0,0,15));
        osg::MatrixTransform *ModelTransform = new osg::MatrixTransform();
        ModelTransform->setMatrix(mat);
        ModelTransform->addChild(m2);
        
        root->addChild(ModelTransform);
        root->addChild(modelNode);
        //================================================

        //shadow
        osgShadow::ShadowedScene * shadowedScene = new osgShadow::ShadowedScene;
                                //osgShadow::ShadowMap * sm = new 
osgShadow::ShadowMap;
                                //shadowedScene->setShadowTechnique(sm);
        osgShadow::ShadowTexture * st = new osgShadow::ShadowTexture;
        shadowedScene->setShadowTechnique(st);

        osg::Material * mtrl = new osg::Material;
        mtrl->setColorMode(osg::Material::DIFFUSE);
        mtrl->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
        mtrl->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
        mtrl->setShininess(osg::Material::FRONT_AND_BACK, 64);
        root->getOrCreateStateSet()->setAttributeAndModes(mtrl, 
osg::StateAttribute::ON);

        m2->setNodeMask(CastsShadowTraversalMask);
        modelNode->setNodeMask(ReceivesShadowTraversalMask);
        
shadowedScene->setReceivesShadowTraversalMask(ReceivesShadowTraversalMask);
        shadowedScene->setCastsShadowTraversalMask(CastsShadowTraversalMask);

        
        // light source 
        osg::Vec3 lightPosition(0,0,30); 
        osg::LightSource* ls = new osg::LightSource;
        ls->getLight()->setPosition(osg::Vec4(lightPosition,1));
        ls->getLight()->setAmbient(osg::Vec4(0.2,0.2,0.2,1.0));
        ls->getLight()->setDiffuse(osg::Vec4(0.6,0.6,0.6,1.0));

        //shadowedScene->setReceivesShadowTraversalMask(1);
        //shadowedScene->setCastsShadowTraversalMask(1);


        // camera
        osg::Camera * camera_node = NULL;

        /**/
                camera_node = new osg::Camera;
                // inherit the main cameras view
                camera_node->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
                camera_node->setViewMatrix(osg::Matrix::identity());
                camera_node->setProjectionMatrixAsOrtho2D(-40,40,-40,40);

                // set the camera to render before the main camera.
                camera_node->setRenderOrder(osg::CameraNode::NESTED_RENDER);

                // tell the camera to use OpenGL frame buffer object where 
supported.
                
camera_node->setRenderTargetImplementation(osg::CameraNode::FRAME_BUFFER_OBJECT);
        /**/
        //------------------------------------------------------
        if(camera_node != NULL)
        {
                camera_node->addChild(root);
                camera_node->addChild(ls);

                shadowedScene->addChild(camera_node);
                viewer.setSceneData( shadowedScene); 

        }
        else
        {
                shadowedScene->addChild(root);
                shadowedScene->addChild(ls);
                // add the scene to the viewer
                viewer.setSceneData( shadowedScene) 
        }

        // create the window and run the threads.
        //---------------------------------------
        return  viewer.run();

}

// ShadowTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <conio.h>
#include <ctype.h>

#include <osg/ArgumentParser>
#include <osg/Group>
#include <osg/Geode>
#include <osg/Geometry>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include <osgViewer/Viewer>
#include <osg/CameraNode>

#include <osgShadow/ShadowedScene>
#include <osgShadow/ShadowMap>
#include <osgShadow/ShadowTexture>

#include <osg/LightSource>
#include <osg/Material>

static int ReceivesShadowTraversalMask = 0x1;
static int CastsShadowTraversalMask = 0x2;

int _tmain(int argc, char* argv[])
{
        //================================================
        // application standard initialization
        //------------------------------------
        // use an ArgumentParser object to manage the program arguments.
        osg::ArgumentParser arguments(&argc,argv);

        // set up the usage document, in case we need to print out how to use 
this program.
        arguments.getApplicationUsage()->setDescription(                
arguments.getApplicationName()+ ",,,");
        arguments.getApplicationUsage()->addCommandLineOption(  "-h or 
--help","Display this information");
        arguments.getApplicationUsage()->setCommandLineUsage(   
arguments.getApplicationName()+ " [options] filename ...");

        // construct the viewer.
        osgViewer::Viewer viewer(arguments);

        // if user request help write it out to cout.
        if (arguments.read("-h") || arguments.read("--help"))
        {
                return 1;
        }
        // any option left unread are converted into errors to write out later.
        arguments.reportRemainingOptionsAsUnrecognized();

        // report any errors if they have occurred when parsing the program 
arguments.
        if (arguments.errors())
        {
                return 1;
        }
        //================================================

        // scene objects
        osg::Group* root = new osg::Group;

        osg::Node* modelNode = NULL;
        std::string FilePathName = "C:\\OSG\\OpenSceneGraph-Data\\cessna.osg";
        modelNode       =       
(osg::Group*)osgDB::readNodeFile((std::string)FilePathName); 

        osg::Node * m2 = 
(osg::Group*)osgDB::readNodeFile("C:\\OSG\\OpenSceneGraph-Data\\cow.osg");
        osg::Matrix mat;
        mat.setTrans(osg::Vec3(0,0,15));
        osg::MatrixTransform *ModelTransform = new osg::MatrixTransform();
        ModelTransform->setMatrix(mat);
        ModelTransform->addChild(m2);
        
        root->addChild(ModelTransform);
        root->addChild(modelNode);
        //================================================

        //shadow
        osgShadow::ShadowedScene * shadowedScene = new osgShadow::ShadowedScene;
                                //osgShadow::ShadowMap * sm = new 
osgShadow::ShadowMap;
                                //shadowedScene->setShadowTechnique(sm);
        osgShadow::ShadowTexture * st = new osgShadow::ShadowTexture;
        shadowedScene->setShadowTechnique(st);

        osg::Material * mtrl = new osg::Material;
        mtrl->setColorMode(osg::Material::DIFFUSE);
        mtrl->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
        mtrl->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
        mtrl->setShininess(osg::Material::FRONT_AND_BACK, 64);
        root->getOrCreateStateSet()->setAttributeAndModes(mtrl, 
osg::StateAttribute::ON);

        m2->setNodeMask(CastsShadowTraversalMask);
        modelNode->setNodeMask(ReceivesShadowTraversalMask);
        
shadowedScene->setReceivesShadowTraversalMask(ReceivesShadowTraversalMask);
        shadowedScene->setCastsShadowTraversalMask(CastsShadowTraversalMask);

        
        // light source 
        osg::Vec3 lightPosition(0,0,30); 
        osg::LightSource* ls = new osg::LightSource;
        ls->getLight()->setPosition(osg::Vec4(lightPosition,1));
        ls->getLight()->setAmbient(osg::Vec4(0.2,0.2,0.2,1.0));
        ls->getLight()->setDiffuse(osg::Vec4(0.6,0.6,0.6,1.0));

        //shadowedScene->setReceivesShadowTraversalMask(1);
        //shadowedScene->setCastsShadowTraversalMask(1);


        // camera
        osg::Camera * camera_node = NULL;

        /**/
                camera_node = new osg::Camera;
                // inherit the main cameras view
                camera_node->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
                camera_node->setViewMatrix(osg::Matrix::identity());
                camera_node->setProjectionMatrixAsOrtho2D(-40,40,-40,40);

                // set the camera to render before the main camera.
                camera_node->setRenderOrder(osg::CameraNode::NESTED_RENDER);

                // tell the camera to use OpenGL frame buffer object where 
supported.
                
camera_node->setRenderTargetImplementation(osg::CameraNode::FRAME_BUFFER_OBJECT);
        /**/
        //------------------------------------------------------
        if(camera_node != NULL)
        {
                camera_node->addChild(root);
                camera_node->addChild(ls);

                shadowedScene->addChild(camera_node);
                viewer.setSceneData( shadowedScene); 

        }
        else
        {
                shadowedScene->addChild(root);
                shadowedScene->addChild(ls);
                // add the scene to the viewer
                viewer.setSceneData( shadowedScene) 
        }

        // create the window and run the threads.
        //---------------------------------------
        return  viewer.run();

}

_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to