source: anuga_core/source/swollen_viewer/swollen/spotlight.cpp @ 3581

Last change on this file since 3581 was 72, checked in by darran, 19 years ago
  • work in progress
File size: 2.1 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.125
10#define DEF_DEFAULT_CONE_HEIGHT 0.25
11
12
13SpotLight::SpotLight(osg::StateSet* rootStateSet, int num)
14{
15    // positioning container
16    _transform = new osg::MatrixTransform;
17    _transform->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.7,0.7,0.7,1));
24    _light->setDiffuse(osg::Vec4(1,1,1,1));
25    _light->setSpecular(osg::Vec4(1,1,1,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    _transform->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    _transform->addChild(_marker);
47
48    setPosition( osg::Vec3(0,0,1) );  // default overhead
49}
50
51
52void 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
61void 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    _transform->setMatrix( osg::Matrix::rotate( quat ) * osg::Matrix::translate(v) );
70}
71
72
73
74SpotLight::~SpotLight()
75{
76}
Note: See TracBrowser for help on using the repository browser.