here is the code. The osgspotlight sample with :
- flatten the terrain (only a quad - all zerors height)
- put closer the plane
- change the far plane from 100 to 10
- use of 1D texture

What causes the mirroring on the light? to me looks like it should be
mirrored along the Y axis (negative x-ses) instead of mirroring it along the
X axis. It is half of the projected thing. It should be displayed. Is this a
bug?


#include <osg/Notify>
#include <osg/MatrixTransform>
#include <osg/ShapeDrawable>
#include <osg/PositionAttitudeTransform>
#include <osg/Geometry>
#include <osg/Texture2D>
#include <osg/Texture1D>
#include <osg/Geode>
#include <osg/LightSource>
#include <osg/TexGenNode>
#include <osgShadow/ShadowedScene>
#include <osgShadow/LightSpacePerspectiveShadowMap>
#include <osgShadow/ParallelSplitShadowMap>

#include <osgUtil/Optimizer>

#include <osgDB/Registry>
#include <osgDB/ReadFile>

#include <osgViewer/Viewer>


// for the grid data..
#include "../osghangglide/terrain_coords.h"

static int sunit=2;

osg::Image* createSpotLightImage(const osg::Vec4& centerColour, const
osg::Vec4& backgroudColour, unsigned int size, float power)
{
    osg::Image* image = new osg::Image;
    image->allocateImage(size,1,1,
                         GL_RGBA,GL_UNSIGNED_BYTE);


    float mid = (float(size)-1)*0.5f;
    float div = 2.0f/float(size);
    for(unsigned int r=0;r<1;++r)
    {
        unsigned char* ptr = image->data(0,r,0);
        for(unsigned int c=0;c<size;++c)
        {
unsigned char clr = 128-osg::minimum((int)c,128);
            float dx = (float(c) - mid)*div;
            float dy = (float(r) - mid)*div;
            float r = powf(1.0f-sqrtf(dx*dx+dy*dy),power);
            if (r<0.0f) r=0.0f;
            osg::Vec4 color = centerColour*r+backgroudColour*(1.0f-r);
            *ptr++ = (unsigned char)clr;//((color[0])*255.0f);
            *ptr++ = (unsigned char)clr;//((color[1])*255.0f);
            *ptr++ = (unsigned char)clr;//((color[2])*255.0f);
            *ptr++ = (unsigned char)255;//((color[3])*255.0f);
        }
    }
    return image;

    //return osgDB::readImageFile("spot.dds");
}

osg::StateSet* createSpotLightDecoratorState(unsigned int lightNum, unsigned
int textureUnit)
{
    osg::StateSet* stateset = new osg::StateSet;

    stateset->setMode(GL_LIGHT0+lightNum, osg::StateAttribute::ON);

    osg::Vec4 centerColour(1.0f,1.0f,1.0f,1.0f);
    osg::Vec4 ambientColour(0.05f,0.05f,0.05f,1.0f);

    // set up spot light texture
    osg::Texture1D* texture = new osg::Texture1D();
    texture->setImage(createSpotLightImage(centerColour, ambientColour, 256,
1.0));
    texture->setBorderColor(osg::Vec4(ambientColour));
    texture->setWrap(osg::Texture::WRAP_S,osg::Texture::CLAMP_TO_BORDER);
    texture->setWrap(osg::Texture::WRAP_T,osg::Texture::CLAMP_TO_BORDER);
    texture->setWrap(osg::Texture::WRAP_R,osg::Texture::CLAMP_TO_BORDER);

stateset->setTextureMode(sunit,GL_TEXTURE_1D,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE|osg::StateAttribute::PROTECTED);

stateset->setTextureMode(sunit,GL_TEXTURE_2D,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE|osg::StateAttribute::PROTECTED);

 
stateset->setTextureMode(sunit,GL_TEXTURE_3D,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE|osg::StateAttribute::PROTECTED);
    stateset->setTextureAttributeAndModes(sunit, texture,
osg::StateAttribute::ON|osg::StateAttribute::PROTECTED);

    // set up tex gens
    stateset->setTextureMode(sunit, GL_TEXTURE_GEN_S,
osg::StateAttribute::ON|osg::StateAttribute::PROTECTED);
    stateset->setTextureMode(sunit, GL_TEXTURE_GEN_T,
osg::StateAttribute::ON|osg::StateAttribute::PROTECTED);
    stateset->setTextureMode(sunit, GL_TEXTURE_GEN_R,
osg::StateAttribute::ON|osg::StateAttribute::PROTECTED);
    stateset->setTextureMode(sunit, GL_TEXTURE_GEN_Q,
osg::StateAttribute::ON|osg::StateAttribute::PROTECTED);

    return stateset;
}


osg::Node* createSpotLightNode(const osg::Vec3& position, const osg::Vec3&
direction, float angle, unsigned int lightNum, unsigned int textureUnit)
{
    osg::Group* group = new osg::Group;

    // create light source.
    osg::LightSource* lightsource = new osg::LightSource;
    osg::Light* light = lightsource->getLight();
    light->setLightNum(lightNum);
    light->setPosition(osg::Vec4(position,1.0f));
    light->setAmbient(osg::Vec4(0.00f,0.00f,0.05f,1.0f));
    light->setDiffuse(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
    group->addChild(lightsource);

    // create tex gen.

    osg::Vec3 up(0.0f,0.0f,1.0f);
    up = (direction ^ up) ^ direction;
    up.normalize();

    osg::TexGenNode* texgenNode = new osg::TexGenNode;
    texgenNode->setTextureUnit(sunit);
    osg::TexGen* texgen = texgenNode->getTexGen();
    texgen->setMode(osg::TexGen::EYE_LINEAR);
    texgen->setPlanesFromMatrix(osg::Matrixd::lookAt(position,
position+direction, up) *

 osg::Matrixd::perspective(angle,1.0,0.1,10));


    group->addChild(texgenNode);

    return group;

}


osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float
radius,double looptime)
{
    // set up the animation path
    osg::AnimationPath* animationPath = new osg::AnimationPath;
    animationPath->setLoopMode(osg::AnimationPath::LOOP);

    int numSamples = 40;
    float yaw = 0.0f;
    float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f);
    float roll = osg::inDegrees(30.0f);

    double time=0.0f;
    double time_delta = looptime/(double)numSamples;
    for(int i=0;i<numSamples;++i)
    {
        osg::Vec3
position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f));
        osg::Quat
rotation(osg::Quat(roll,osg::Vec3(0.0,1.0,0.0))*osg::Quat(-(yaw+osg::inDegrees(90.0f)),osg::Vec3(0.0,0.0,1.0)));


 
animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation));

        yaw += yaw_delta;
        time += time_delta;

    }
    return animationPath;
}

osg::Node* createBase(const osg::Vec3& center,float radius)
{

    osg::Geode* geode = new osg::Geode;

    // set up the texture of the base.
    osg::StateSet* stateset = new osg::StateSet();
    osg::Image* image = osgDB::readImageFile("Images/lz.rgb");
    if (image)
    {
        osg::Texture2D* texture = new osg::Texture2D;
        texture->setImage(image);

 stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
    }

    geode->setStateSet( stateset );


    osg::HeightField* grid = new osg::HeightField;
    grid->allocate(38,39);
    grid->setOrigin(center+osg::Vec3(-radius,-radius,0.0f));
    grid->setXInterval(radius*2.0f/(float)(38-1));
    grid->setYInterval(radius*2.0f/(float)(39-1));

    float minHeight = 0;//FLT_MAX;
    float maxHeight = 0;//-FLT_MAX;


    unsigned int r;
    for(r=0;r<39;++r)
    {
        for(unsigned int c=0;c<38;++c)
        {
            float h = 0;//vertex[r+c*39][2];
            if (h>maxHeight) maxHeight=h;
            if (h<minHeight) minHeight=h;
        }
    }

    float hieghtScale = radius*0.5f/(maxHeight-minHeight);
    float hieghtOffset = -(minHeight+maxHeight)*0.5f;

    for(r=0;r<39;++r)
    {
        for(unsigned int c=0;c<38;++c)
        {
            float h = vertex[r+c*39][2];
            grid->setHeight(c,r,0);
        }
    }

    geode->addDrawable(new osg::ShapeDrawable(grid));

    osg::Group* group = new osg::Group;
    group->addChild(geode);

    return group;

}

osg::Node* createMovingModel(const osg::Vec3& center, float radius)
{
    float animationLength = 10.0f;

    osg::AnimationPath* animationPath =
createAnimationPath(center,radius,animationLength);

    osg::Group* model = new osg::Group;

    osg::Node* cessna = osgDB::readNodeFile("cessna.osg");
    if (cessna)
    {
        const osg::BoundingSphere& bs = cessna->getBound();

        float size = radius/bs.radius()*0.3f;
        osg::MatrixTransform* positioned = new osg::MatrixTransform;
        positioned->setDataVariance(osg::Object::STATIC);
        positioned->setMatrix(osg::Matrix::translate(-bs.center())*
                              osg::Matrix::scale(size,size,size)*

 osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,2.0f));

        positioned->addChild(cessna);

        osg::MatrixTransform* xform = new osg::MatrixTransform;
        xform->setUpdateCallback(new
osg::AnimationPathCallback(animationPath,0.0f,2.0));
        xform->addChild(positioned);

        xform->addChild(createSpotLightNode(osg::Vec3(0.0f,0.0f,0.0f),
osg::Vec3(0.0f,1.0f,-1.0f), 120, 0, 1));

        model->addChild(xform);
    }

    return model;
}



int ReceivesShadowTraversalMask = 0x1;
int CastsShadowTraversalMask = 0x2;
osg::Node* createModel()
{
    osg::Vec3 center(0.0f,0.0f,0.0f);
    float radius = 100.0f;
    osg::Vec3 lightPosition(center+osg::Vec3(0.0f,0.0f,radius));

    // the shadower model
    osg::Node* shadower = createMovingModel(center,radius*0.5f);

    // the shadowed model
    osg::Node* shadowed =
createBase(center-osg::Vec3(0.0f,0.0f,radius*0.1),radius);


osgShadow::ShadowedScene* scene = new osgShadow::ShadowedScene;
osg::ref_ptr<osgShadow::LightSpacePerspectiveShadowMap> sm = new
osgShadow::LightSpacePerspectiveShadowMap;
 //scene->setShadowTechnique(sm.get());

scene->setReceivesShadowTraversalMask(ReceivesShadowTraversalMask);
    scene->setCastsShadowTraversalMask(CastsShadowTraversalMask);

shadowed->setNodeMask(ReceivesShadowTraversalMask);
 shadower->setNodeMask(CastsShadowTraversalMask);

    scene->setStateSet(createSpotLightDecoratorState(0,1));

    scene->addChild(shadower);
    scene->addChild(shadowed);

    return scene;
}


int main(int, char **)
{
    // construct the viewer.
    osgViewer::Viewer viewer;

    // add the spoit light model to the viewer
    viewer.setSceneData( createModel() );

    // run the viewer main frame loop.
    return viewer.run();
}

Nick

http://www.linkedin.com/in/tnikolov
Sent from Gümüşsuyu, İstanbul, Turkey

On Fri, Dec 11, 2009 at 3:47 PM, Robert Osfield <[email protected]>wrote:

> Hi Nick,
>
> On Fri, Dec 11, 2009 at 1:01 PM, Trajce Nikolov
> <[email protected]> wrote:
> > one more thing. The matrix that I pass to TexGen seem to not be taken
> into
> > account. No matter how I set the far plane it shows same results. Is this
> > expected behavior ?
>
> I can't comment on the issues you are seeing as I'm not party to your code.
>
> As for the 1D texture - this would just be a simple alpha textured
> with a region set to zero for behind the light source, and 1.0 or
> attenuated in front.
>
> Robert.
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to