[osg-users] How to create spotlight with a distinguishable boundary(circular)?

Kamal Grover kamalgrover0892 at gmail.com
Tue Nov 22 09:57:40 PST 2016


Hi,
My purpose is to create a shadow of the object that are in the path of the defined spotlight . For this I am using osg::ShadowMap with a slightly modified shader. My problem is that i am not able to create a distinguishable round boundary of the spotlight on the terrain . 
screenshot:    h t t p s ://1drv(dot)ms/i/s!Ap2iNXvnTFKAlGta82IIGfNbk7Ln

The code for creating the spotlight is as follows:

Code:

osg::Node* createSpotLightNode2(const osg::Vec3& position, const osg::Vec3& direction, float angle, unsigned int lightNum)
{
    // create light source.
    osg::LightSource* lightsource = new osg::LightSource;
    osg::Light* light = lightsource->getLight();
    light->setLightNum(lightNum);
    light->setAmbient(osg::Vec4(0.00f, 0.00f, 0.00f, 1.0f));
    light->setDiffuse(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
    light->setSpecular(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
    light->setPosition(osg::Vec4(position, 1.0f));
    light->setConstantAttenuation(1);
    light->setLinearAttenuation(0);
    light->setQuadraticAttenuation(0);
    light->setSpotCutoff(angle);
    light->setDirection(direction);
    light->setSpotExponent(0);

    lightsource->getOrCreateStateSet()->setMode(GL_LIGHT0 + lightNum, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
    return lightsource;
}




The following is the code for the fragment shader:

Code:

static const char fragmentShaderSource_withBaseTexture[] =
"uniform sampler2D osgShadow_baseTexture; \n"
"uniform sampler2DShadow osgShadow_shadowTexture; \n"
"uniform vec2 osgShadow_ambientBias; \n"
"\n"
"void main(void) \n"
"{ \n"
"    vec4 color = gl_Color * texture2D( osgShadow_baseTexture, gl_TexCoord[0].xy ); \n"
"   if (1 == shadow2DProj( osgShadow_shadowTexture, gl_TexCoord[1] )) \n"
"   { \n"
"       gl_FragColor = color;\n"
"   } \n"
"   else \n"
"   {    \n"
"       gl_FragColor = ( /*color +*/ vec4(0.5,0.0,0.0,0.1)) * (osgShadow_ambientBias.x + shadow2DProj( osgShadow_shadowTexture, gl_TexCoord[0] ) * osgShadow_ambientBias.y) ;\n"
"   }   \n"
"}\n";




The code for create the scene is as follow: 

Code:

osg::Node* createModel()
{
    osgShadow::ShadowedScene* root = new osgShadow::ShadowedScene;
    osg::Node* cessna = createModelPlane();
    osg::Node* terrain = createTerrain();
    root->addChild(cessna);
    root->addChild(terrain);
    
    osg::ref_ptr<osgShadow::ShadowMap> sm = new osgShadow::ShadowMap;
    sm->clearShaderList();
    sm->addShader(new osg::Shader(osg::Shader::Type::FRAGMENT, fragmentShaderSource_withBaseTexture));
    sm->setAmbientBias(osg::Vec2(0.2f, 0.8f));
    root->setShadowTechnique(sm.get());

    osg::Vec3 lightpos(00.0f, 0.0f, 100.0f);
    osg::Vec3f dir(0.0f, 0.0f, -1.0f);
    float halfangle = 30;
    dir.normalize();
    root->addChild(createSpotLightNode2(lightpos, dir, halfangle,0));
    return root;
}





PS. Is there a better way to simulate this considering , I have to create shadows from multiple lights . Shadow should be constrained to the circle the light makes on the terrian . 

PPS. On a side note : Is there a way to disable self shadows ? Idealy i am required to create a translucent cone of green light with translucent shadows in red.

Any help / suggestions / criticism of any sorts is highly appreciated and welcomed.
... 

Thanks in Advance!

Cheers,
Kamal

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



-------------- next part --------------
A non-text attachment was scrubbed...
Name: 2016-11-22 (1).png
Type: image/png
Size: 497037 bytes
Desc: not available
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20161122/5114754a/attachment-0002.png>


More information about the osg-users mailing list