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
Light(void)19 Light::Light( void )
20 {
21 init();
22 }
23
Light(unsigned int lightnum)24 Light::Light(unsigned int lightnum)
25 {
26 init();
27 _lightnum = lightnum;
28 }
29
~Light(void)30 Light::~Light( void )
31 {
32 }
33
34
init(void)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
setLightNum(int num)61 void Light::setLightNum(int num)
62 {
63 if (_lightnum==num) return;
64
65 ReassignToParents needToReassingToParentsWhenMemberValueChanges(this);
66
67 _lightnum = num;
68 }
69
captureLightState()70 void Light::captureLightState()
71 {
72 #ifdef OSG_GL_FIXED_FUNCTION_AVAILABLE
73 glGetLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_AMBIENT, _ambient.ptr() );
74 glGetLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_DIFFUSE, _diffuse.ptr() );
75 glGetLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPECULAR, _specular.ptr() );
76 glGetLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_POSITION, _position.ptr() );
77 glGetLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPOT_DIRECTION, _direction.ptr() );
78 glGetLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPOT_EXPONENT, &_spot_exponent );
79 glGetLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPOT_CUTOFF, &_spot_cutoff );
80 glGetLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_CONSTANT_ATTENUATION, &_constant_attenuation );
81 glGetLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_LINEAR_ATTENUATION, &_linear_attenuation );
82 glGetLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_QUADRATIC_ATTENUATION, &_quadratic_attenuation );
83 #else
84 OSG_NOTICE<<"Warning: Light::captureLightState() - not supported."<<std::endl;
85 #endif
86 }
87
apply(State &) const88 void Light::apply(State&) const
89 {
90 #ifdef OSG_GL_FIXED_FUNCTION_AVAILABLE
91 glLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_AMBIENT, _ambient.ptr() );
92 glLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_DIFFUSE, _diffuse.ptr() );
93 glLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPECULAR, _specular.ptr() );
94 glLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_POSITION, _position.ptr() );
95 glLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPOT_DIRECTION, _direction.ptr() );
96 glLightf ( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPOT_EXPONENT, _spot_exponent );
97 glLightf ( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPOT_CUTOFF, _spot_cutoff );
98 glLightf ( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_CONSTANT_ATTENUATION, _constant_attenuation );
99 glLightf ( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_LINEAR_ATTENUATION, _linear_attenuation );
100 glLightf ( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_QUADRATIC_ATTENUATION, _quadratic_attenuation );
101 #else
102 OSG_NOTICE<<"Warning: Light::apply(State&) - not supported."<<std::endl;
103 #endif
104 }
105