Developer Documentation
LightSourceNode.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 
43 
44 
45 
46 
47 //=============================================================================
48 //
49 // CLASS LightSourceNode - IMPLEMENTATION
50 //
51 //=============================================================================
52 
53 
54 //== INCLUDES =================================================================
55 
56 
57 #include "LightSourceNode.hh"
58 
59 
60 //== NAMESPACES ===============================================================
61 
62 namespace ACG {
63 namespace SceneGraph {
64 
65 
66 //== IMPLEMENTATION ==========================================================
67 
68 
70  const std::string& _name)
71  : BaseNode(_parent, _name)
72 {
73  lights_.resize(8);
74  lightsSave_.resize(8);
75  enable(GL_LIGHT0);;
76 }
77 
78 
79 //----------------------------------------------------------------------------
80 
81 void LightSourceNode::enter(GLState& _state, const DrawModes::DrawMode& /* _drawmode */ )
82 {
83  // save old lights
84  for(unsigned int i=0; i<lightsSave_.size(); i++)
85  {
86  // save only if enabled
87  if(glIsEnabled(index2gl(i)))
88  {
89  lightsSave_[i].enabled = true;
90 
93  }
94  else lightsSave_[i].enabled = false;
95  }
96 
97  // set new lights
98  for(unsigned int i=0; i<lights_.size(); i++)
99  {
100  if(lights_[i].enabled)
101  {
102  // correct Position for fixed Lights
103  if(lights_[i].fixedPosition)
104  lights_[i].realPosition =
105  _state.inverse_modelview() * lights_[i].position;
106  else lights_[i].realPosition = lights_[i].position;
107 
110  }
112 
113  }
114 }
115 
116 
117 //----------------------------------------------------------------------------
118 
119 
120 void LightSourceNode::leave(GLState& /* _state */ , const DrawModes::DrawMode& /* _drawmode*/ )
121 {
122  // restore old enabled lights
123  for(unsigned int i=0; i<lights_.size(); i++)
124  {
125  if(lightsSave_[i].enabled)
126  {
129  }
131  }
132 }
133 
134 //----------------------------------------------------------------------------
135 
136 void LightSourceNode::set_parameters(GLenum _index, LightSource& _light)
137 {
138 
139  // set preferences of _light for GL_LIGHT#_index
140  glLightfv(_index, GL_AMBIENT, (GLfloat *)_light.ambientColor.data());
141  glLightfv(_index, GL_DIFFUSE, (GLfloat *)_light.diffuseColor.data());
142  glLightfv(_index, GL_SPECULAR, (GLfloat *)_light.specularColor.data());
143 
144  glLightfv(_index, GL_POSITION, (GLfloat *)_light.realPosition.data());
145  glLightfv(_index, GL_SPOT_DIRECTION, (GLfloat *)_light.spotDirection.data());
146 
147  glLightf(_index, GL_SPOT_EXPONENT, _light.spotExponent);
148  glLightf(_index, GL_SPOT_CUTOFF, _light.spotCutoff);
149  glLightf(_index, GL_CONSTANT_ATTENUATION, _light.constantAttenuation);
150  glLightf(_index, GL_LINEAR_ATTENUATION, _light.linearAttenuation);
151  glLightf(_index, GL_QUADRATIC_ATTENUATION, _light.quadraticAttenuation);
152 }
153 
154 //----------------------------------------------------------------------------
155 
156 void LightSourceNode::get_parameters(GLenum _index, LightSource& _light)
157 {
158  // get preferences of GL_LIGHT#_index and store them in _light
159  glGetLightfv(_index, GL_AMBIENT, (GLfloat *)_light.ambientColor.data());
160  glGetLightfv(_index, GL_DIFFUSE, (GLfloat *)_light.diffuseColor.data());
161  glGetLightfv(_index, GL_SPECULAR, (GLfloat *)_light.specularColor.data());
162  glGetLightfv(_index, GL_POSITION, (GLfloat *)_light.position.data());
163  glGetLightfv(_index, GL_SPOT_DIRECTION, (GLfloat *)_light.spotDirection.data());
164 
165  glGetLightfv(_index, GL_SPOT_EXPONENT, &_light.spotExponent);
166  glGetLightfv(_index, GL_SPOT_CUTOFF, &_light.spotCutoff);
167  glGetLightfv(_index, GL_CONSTANT_ATTENUATION, &_light.constantAttenuation);
168  glGetLightfv(_index, GL_LINEAR_ATTENUATION, &_light.linearAttenuation);
169  glGetLightfv(_index, GL_QUADRATIC_ATTENUATION, &_light.quadraticAttenuation);
170 }
171 //=============================================================================
172 } // namespace SceneGraph
173 } // namespace ACG
174 //=============================================================================
Namespace providing different geometric functions concerning angles.
const GLMatrixd & inverse_modelview() const
get inverse modelview matrix
Definition: GLState.hh:811
void set_parameters(GLenum _index, LightSource &_light)
set _light Options in OpenGL for GL_LIGHT::_index
static void enable(GLenum _cap, bool _warnRemoved=true)
replaces glEnable, but supports locking
void get_parameters(GLenum _index, LightSource &_light)
get _light Options in OpenGL for GL_LIGHT::_index
static void disable(GLenum _cap, bool _warnRemoved=true)
replaces glDisable, but supports locking
Scalar * data()
access to Scalar array
Definition: Vector11T.hh:195
GLenum index2gl(int _nr)
return GL_LIGHT* for light _nr
void leave(GLState &_state, const DrawModes::DrawMode &_drawmode)
restores original Light Sources
std::vector< LightSource > lightsSave_
save old LightSources
Structure to hold options for one LightSource.
void enable(GLenum _nr)
enable LightSource _nr
void enter(GLState &_state, const DrawModes::DrawMode &_drawmode)
set current Light Sources
LightSourceNode(BaseNode *_parent=0, const std::string &_name="<LightSourceNode>")
Default constructor. Applies all properties.
std::vector< LightSource > lights_
store LightSources of this node