1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 2 * 3 * This library is open source and may be redistributed and/or modified under 4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 5 * (at your option) any later version. The full license is in LICENSE file 6 * included with this distribution, and on the openscenegraph.org website. 7 * 8 * This library is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * OpenSceneGraph Public License for more details. 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 // OSG_DEBUG << "_ambient "<<_ambient<<std::endl; 50 // OSG_DEBUG << "_diffuse "<<_diffuse<<std::endl; 51 // OSG_DEBUG << "_specular "<<_specular<<std::endl; 52 // OSG_DEBUG << "_position "<<_position<<std::endl; 53 // OSG_DEBUG << "_direction "<<_direction<<std::endl; 54 // OSG_DEBUG << "_spot_exponent "<<_spot_exponent<<std::endl; 55 // OSG_DEBUG << "_spot_cutoff "<<_spot_cutoff<<std::endl; 56 // OSG_DEBUG << "_constant_attenuation "<<_constant_attenuation<<std::endl; 57 // OSG_DEBUG << "_linear_attenuation "<<_linear_attenuation<<std::endl; 58 // OSG_DEBUG << "_quadratic_attenuation "<<_quadratic_attenuation<<std::endl; 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 // take a reference to this clip plane to prevent it from going out of scope 72 // when we remove it temporarily from its parents. 73 osg::ref_ptr<Light> lightRef = this; 74 75 // copy the parents as they _parents list will be changed by the subsequent removeAttributes. 76 ParentList parents = _parents; 77 78 // remove this attribute from its parents as its position is being changed 79 // and would no longer be valid. 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 // assign the hint target 90 _lightnum = num; 91 92 // add this attribute back into its original parents with its new position 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 } 137