#include #include #include #include #define DEF_DEFAULT_ANGLE 45 #define DEF_DEFAULT_EXPONENT 10 #define DEF_DEFAULT_CONE_RADIUS 0.125 #define DEF_DEFAULT_CONE_HEIGHT 0.25 SpotLight::SpotLight(osg::StateSet* rootStateSet, int num) { // positioning container _group = new osg::MatrixTransform; _group->setCullingActive(false); // OpenGL light _light = new osg::Light; _light->setLightNum(num); _light->setPosition(osg::Vec4(0,0,0,1)); _light->setAmbient(osg::Vec4(0.0,0.0,0.0,1)); _light->setDiffuse(osg::Vec4(1,1,1,1)); _light->setSpecular(osg::Vec4(1,1,1,1)); _light->setSpotCutoff(DEF_DEFAULT_ANGLE); _light->setSpotExponent(DEF_DEFAULT_EXPONENT); // Scenegraph node _source = new osg::LightSource; _source->setReferenceFrame( osg::LightSource::RELATIVE_RF ); _source->setLight( _light ); _source->setLocalStateSetModes( osg::StateAttribute::ON ); _source->setStateSetModes( *rootStateSet, osg::StateAttribute::ON ); _group->addChild( _source ); // create cone marker for spotlight and rotate towards origin _marker = new osg::Geode; _shape = new osg::Cone; _shape->setRadius(DEF_DEFAULT_CONE_RADIUS); _shape->setHeight(DEF_DEFAULT_CONE_HEIGHT); _shape->setCenter( osg::Vec3(0,0,0) ); _marker->addDrawable( new osg::ShapeDrawable(_shape) ); // omit marker cone for now (FIX ME) _group->addChild(_marker); setPosition( osg::Vec3(0,0,1) ); // default overhead } void SpotLight::setSpotAngle( float degrees ) { _light->setSpotCutoff( degrees ); float radians = degrees * osg::PI / 180.0; float cone_radius = tan( radians/2.0 ) * _shape->getHeight(); _shape->setRadius( cone_radius ); } void SpotLight::setPosition(osg::Vec3 v) { osg::Vec3 origindir; osg::Quat quat; origindir.set( -v ); origindir.normalize(); quat.makeRotate( osg::Vec3(0,0,-1), origindir ); _group->setMatrix( osg::Matrix::rotate( quat ) * osg::Matrix::translate(v) ); } SpotLight::~SpotLight() { }