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 | |
---|
13 | SpotLight::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.0,0.0,0.0,1)); |
---|
24 | _light->setDiffuse(osg::Vec4(1,1,1,1)); |
---|
25 | _light->setSpecular(osg::Vec4(1,0,0,1)); |
---|
26 | _light->setSpotCutoff(DEF_DEFAULT_ANGLE); |
---|
27 | _light->setSpotExponent(DEF_DEFAULT_EXPONENT); |
---|
28 | |
---|
29 | // Scenegraph node |
---|
30 | _source = new osg::LightSource; |
---|
31 | _source->setReferenceFrame( osg::LightSource::RELATIVE_RF ); |
---|
32 | _source->setLight( _light ); |
---|
33 | _source->setLocalStateSetModes( osg::StateAttribute::ON ); |
---|
34 | _source->setStateSetModes( *rootStateSet, osg::StateAttribute::ON ); |
---|
35 | _group->addChild( _source ); |
---|
36 | |
---|
37 | // create cone marker for spotlight and rotate towards origin |
---|
38 | _marker = new osg::Geode; |
---|
39 | _shape = new osg::Cone; |
---|
40 | _shape->setRadius(DEF_DEFAULT_CONE_RADIUS); |
---|
41 | _shape->setHeight(DEF_DEFAULT_CONE_HEIGHT); |
---|
42 | _shape->setCenter( osg::Vec3(0,0,0) ); |
---|
43 | _marker->addDrawable( new osg::ShapeDrawable(_shape) ); |
---|
44 | |
---|
45 | // omit marker cone for now (FIX ME) |
---|
46 | _group->addChild(_marker); |
---|
47 | |
---|
48 | setPosition( osg::Vec3(0,0,1) ); // default overhead |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | void SpotLight::setSpotAngle( float degrees ) |
---|
53 | { |
---|
54 | _light->setSpotCutoff( degrees ); |
---|
55 | float radians = degrees * osg::PI / 180.0; |
---|
56 | float cone_radius = tan( radians/2.0 ) * _shape->getHeight(); |
---|
57 | _shape->setRadius( cone_radius ); |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | void SpotLight::setPosition(osg::Vec3 v) |
---|
62 | { |
---|
63 | osg::Vec3 origindir; |
---|
64 | osg::Quat quat; |
---|
65 | origindir.set( -v ); |
---|
66 | origindir.normalize(); |
---|
67 | quat.makeRotate( osg::Vec3(0,0,-1), origindir ); |
---|
68 | |
---|
69 | _group->setMatrix( osg::Matrix::rotate( quat ) * osg::Matrix::translate(v) ); |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | |
---|
74 | SpotLight::~SpotLight() |
---|
75 | { |
---|
76 | } |
---|