| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osg/Light> |
|---|
| 14 | #include <osg/StateSet> |
|---|
| 15 | #include <osg/Notify> |
|---|
| 16 | |
|---|
| 17 | using namespace osg; |
|---|
| 18 | |
|---|
| 19 | Light::Light( void ) |
|---|
| 20 | { |
|---|
| 21 | init(); |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | Light::Light(unsigned int lightnum) |
|---|
| 25 | { |
|---|
| 26 | init(); |
|---|
| 27 | _lightnum = lightnum; |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | Light::~Light( void ) |
|---|
| 31 | { |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | void Light::init( void ) |
|---|
| 36 | { |
|---|
| 37 | _lightnum = 0; |
|---|
| 38 | _ambient.set(0.05f,0.05f,0.05f,1.0f); |
|---|
| 39 | _diffuse.set(0.8f,0.8f,0.8f,1.0f); |
|---|
| 40 | _specular.set(0.05f,0.05f,0.05f,1.0f); |
|---|
| 41 | _position.set(0.0f,0.0f,1.0f,0.0f); |
|---|
| 42 | _direction.set(0.0f,0.0f,-1.0f); |
|---|
| 43 | _spot_exponent = 0.0f; |
|---|
| 44 | _spot_cutoff = 180.0f; |
|---|
| 45 | _constant_attenuation = 1.0f; |
|---|
| 46 | _linear_attenuation = 0.0f; |
|---|
| 47 | _quadratic_attenuation = 0.0f; |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | void Light::setLightNum(int num) |
|---|
| 62 | { |
|---|
| 63 | if (_lightnum==num) return; |
|---|
| 64 | |
|---|
| 65 | if (_parents.empty()) |
|---|
| 66 | { |
|---|
| 67 | _lightnum = num; |
|---|
| 68 | return; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | osg::ref_ptr<Light> lightRef = this; |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | ParentList parents = _parents; |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | ParentList::iterator itr; |
|---|
| 81 | for(itr = parents.begin(); |
|---|
| 82 | itr != parents.end(); |
|---|
| 83 | ++itr) |
|---|
| 84 | { |
|---|
| 85 | osg::StateSet* stateset = *itr; |
|---|
| 86 | stateset->removeAttribute(this); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | _lightnum = num; |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | for(itr = parents.begin(); |
|---|
| 94 | itr != parents.end(); |
|---|
| 95 | ++itr) |
|---|
| 96 | { |
|---|
| 97 | osg::StateSet* stateset = *itr; |
|---|
| 98 | stateset->setAttribute(this); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | void Light::captureLightState() |
|---|
| 103 | { |
|---|
| 104 | #ifdef OSG_GL_FIXED_FUNCTION_AVAILABLE |
|---|
| 105 | glGetLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_AMBIENT, _ambient.ptr() ); |
|---|
| 106 | glGetLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_DIFFUSE, _diffuse.ptr() ); |
|---|
| 107 | glGetLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPECULAR, _specular.ptr() ); |
|---|
| 108 | glGetLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_POSITION, _position.ptr() ); |
|---|
| 109 | glGetLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPOT_DIRECTION, _direction.ptr() ); |
|---|
| 110 | glGetLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPOT_EXPONENT, &_spot_exponent ); |
|---|
| 111 | glGetLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPOT_CUTOFF, &_spot_cutoff ); |
|---|
| 112 | glGetLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_CONSTANT_ATTENUATION, &_constant_attenuation ); |
|---|
| 113 | glGetLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_LINEAR_ATTENUATION, &_linear_attenuation ); |
|---|
| 114 | glGetLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_QUADRATIC_ATTENUATION, &_quadratic_attenuation ); |
|---|
| 115 | #else |
|---|
| 116 | OSG_NOTICE<<"Warning: Light::captureLightState() - not supported."<<std::endl; |
|---|
| 117 | #endif |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | void Light::apply(State&) const |
|---|
| 121 | { |
|---|
| 122 | #ifdef OSG_GL_FIXED_FUNCTION_AVAILABLE |
|---|
| 123 | glLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_AMBIENT, _ambient.ptr() ); |
|---|
| 124 | glLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_DIFFUSE, _diffuse.ptr() ); |
|---|
| 125 | glLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPECULAR, _specular.ptr() ); |
|---|
| 126 | glLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_POSITION, _position.ptr() ); |
|---|
| 127 | glLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPOT_DIRECTION, _direction.ptr() ); |
|---|
| 128 | glLightf ( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPOT_EXPONENT, _spot_exponent ); |
|---|
| 129 | glLightf ( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPOT_CUTOFF, _spot_cutoff ); |
|---|
| 130 | glLightf ( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_CONSTANT_ATTENUATION, _constant_attenuation ); |
|---|
| 131 | glLightf ( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_LINEAR_ATTENUATION, _linear_attenuation ); |
|---|
| 132 | glLightf ( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_QUADRATIC_ATTENUATION, _quadratic_attenuation ); |
|---|
| 133 | #else |
|---|
| 134 | OSG_NOTICE<<"Warning: Light::apply(State&) - not supported."<<std::endl; |
|---|
| 135 | #endif |
|---|
| 136 | } |
|---|