source: Swollen/swollen/spotlight.cpp @ 62

Last change on this file since 62 was 36, checked in by darran, 20 years ago
  • uniform scaling
  • spotlight removed
  • start-up on frame 1 bug fixed
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_RF );
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
44    // omit marker cone for now (FIX ME)
45    // _group->addChild(_marker);
46
47    setPosition( osg::Vec3(0,0,1) );  // default overhead
48}
49
50
51void SpotLight::setSpotAngle( float degrees )
52{
53    _light->setSpotCutoff( degrees );
54    float radians = degrees * osg::PI / 180.0;
55    float cone_radius = tan( radians/2.0 ) * _shape->getHeight();
56    _shape->setRadius( cone_radius );
57}
58
59
60void SpotLight::setPosition(osg::Vec3 v)
61{
62    osg::Vec3 origindir;
63    osg::Quat quat;
64    origindir.set( -v );
65    origindir.normalize();
66    quat.makeRotate( osg::Vec3(0,0,-1), origindir );
67
68    _group->setMatrix( osg::Matrix::rotate( quat ) * osg::Matrix::translate(v) );
69}
70
71
72
73SpotLight::~SpotLight()
74{
75}
Note: See TracBrowser for help on using the repository browser.