source: Swollen/swollen/spotlight.cpp @ 31

Last change on this file since 31 was 6, checked in by darran, 20 years ago

new import

File size: 2.0 KB
Line 
1
2#include <math.h>
3#include <spotlight.h>
4#include <osg/ShapeDrawable>
5#include <iostream>
6
7#define DEF_DEFAULT_ANGLE 45
8#define DEF_DEFAULT_EXPONENT 10
9#define DEF_DEFAULT_CONE_RADIUS 0.05
10#define DEF_DEFAULT_CONE_HEIGHT 0.1
11
12
13SpotLight::SpotLight(osg::StateSet* rootStateSet, int num)
14{
15    // positioning container
16    _group = new osg::MatrixTransform;
17    _group->setCullingActive(false);
18
19    // OpenGL light
20    _light = new osg::Light;
21    _light->setLightNum(num);
22    _light->setPosition(osg::Vec4(0,0,0,1));
23    _light->setAmbient(osg::Vec4(0.5,0.5,0.5,1));
24    _light->setDiffuse(osg::Vec4(1,1,1,1));
25    _light->setSpotCutoff(DEF_DEFAULT_ANGLE);
26    _light->setSpotExponent(DEF_DEFAULT_EXPONENT);
27
28    // Scenegraph node
29    _source = new osg::LightSource;
30    _source->setReferenceFrame( osg::LightSource::RELATIVE_TO_PARENTS );
31    _source->setLight(_light);
32    _source->setLocalStateSetModes( osg::StateAttribute::ON ); 
33    _source->setStateSetModes( *rootStateSet, osg::StateAttribute::ON );
34    _group->addChild( _source );
35
36    // create cone marker for spotlight and rotate towards origin
37    _marker = new osg::Geode;
38    _shape = new osg::Cone;
39    _shape->setRadius(DEF_DEFAULT_CONE_RADIUS);
40    _shape->setHeight(DEF_DEFAULT_CONE_HEIGHT);
41    _shape->setCenter( osg::Vec3(0,0,0) );
42    _marker->addDrawable( new osg::ShapeDrawable(_shape) );
43    _group->addChild(_marker);
44
45    setPosition( osg::Vec3(0,0,1) );  // default overhead
46}
47
48
49void SpotLight::setSpotAngle( float degrees )
50{
51    _light->setSpotCutoff( degrees );
52    float radians = degrees * osg::PI / 180.0;
53    float cone_radius = tan( radians/2.0 ) * _shape->getHeight();
54    _shape->setRadius( cone_radius );
55}
56
57
58void SpotLight::setPosition(osg::Vec3 v)
59{
60    osg::Vec3 origindir;
61    osg::Quat quat;
62    origindir.set( -v );
63    origindir.normalize();
64    quat.makeRotate( osg::Vec3(0,0,-1), origindir );
65
66    _group->setMatrix( osg::Matrix::rotate( quat ) * osg::Matrix::translate(v) );
67}
68
69
70
71SpotLight::~SpotLight()
72{
73}
Note: See TracBrowser for help on using the repository browser.