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  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 
53 //=============================================================================
54 //
55 // CLASS LightSourceNode - IMPLEMENTATION
56 //
57 //=============================================================================
58 
59 
60 //== INCLUDES =================================================================
61 
62 
63 #include "LightSourceNode.hh"
64 
65 
66 //== NAMESPACES ===============================================================
67 
68 namespace ACG {
69 namespace SceneGraph {
70 
71 
72 //== IMPLEMENTATION ==========================================================
73 
74 
76  const std::string& _name)
77  : BaseNode(_parent, _name)
78 {
79  lights_.resize(8);
80  lightsSave_.resize(8);
81  enable(GL_LIGHT0);;
82 }
83 
84 
85 //----------------------------------------------------------------------------
86 
87 void LightSourceNode::enter(GLState& _state, const DrawModes::DrawMode& /* _drawmode */ )
88 {
89  // save old lights
90  for(unsigned int i=0; i<lightsSave_.size(); i++)
91  {
92  // save only if enabled
93  if(glIsEnabled(index2gl(i)))
94  {
95  lightsSave_[i].enabled = true;
96 
99  }
100  else lightsSave_[i].enabled = false;
101  }
102 
103  // set new lights
104  for(unsigned int i=0; i<lights_.size(); i++)
105  {
106  if(lights_[i].enabled)
107  {
108  // correct Position for fixed Lights
109  if(lights_[i].fixedPosition)
110  lights_[i].realPosition =
111  _state.inverse_modelview()*lights_[i].position;
112  else lights_[i].realPosition = lights_[i].position;
113 
116  }
118 
119  }
120 }
121 
122 
123 //----------------------------------------------------------------------------
124 
125 
126 void LightSourceNode::leave(GLState& /* _state */ , const DrawModes::DrawMode& /* _drawmode*/ )
127 {
128  // restore old enabled lights
129  for(unsigned int i=0; i<lights_.size(); i++)
130  {
131  if(lightsSave_[i].enabled)
132  {
135  }
137  }
138 }
139 
140 //----------------------------------------------------------------------------
141 
142 void LightSourceNode::set_parameters(GLenum _index, LightSource& _light)
143 {
144 
145  // set preferences of _light for GL_LIGHT#_index
146  glLightfv(_index, GL_AMBIENT, (GLfloat *)_light.ambientColor.data());
147  glLightfv(_index, GL_DIFFUSE, (GLfloat *)_light.diffuseColor.data());
148  glLightfv(_index, GL_SPECULAR, (GLfloat *)_light.specularColor.data());
149 
150  glLightfv(_index, GL_POSITION, (GLfloat *)_light.realPosition.data());
151  glLightfv(_index, GL_SPOT_DIRECTION, (GLfloat *)_light.spotDirection.data());
152 
153  glLightf(_index, GL_SPOT_EXPONENT, _light.spotExponent);
154  glLightf(_index, GL_SPOT_CUTOFF, _light.spotCutoff);
155  glLightf(_index, GL_CONSTANT_ATTENUATION, _light.constantAttenuation);
156  glLightf(_index, GL_LINEAR_ATTENUATION, _light.linearAttenuation);
157  glLightf(_index, GL_QUADRATIC_ATTENUATION, _light.quadraticAttenuation);
158 }
159 
160 //----------------------------------------------------------------------------
161 
162 void LightSourceNode::get_parameters(GLenum _index, LightSource& _light)
163 {
164  // get preferences of GL_LIGHT#_index and store them in _light
165  glGetLightfv(_index, GL_AMBIENT, (GLfloat *)_light.ambientColor.data());
166  glGetLightfv(_index, GL_DIFFUSE, (GLfloat *)_light.diffuseColor.data());
167  glGetLightfv(_index, GL_SPECULAR, (GLfloat *)_light.specularColor.data());
168  glGetLightfv(_index, GL_POSITION, (GLfloat *)_light.position.data());
169  glGetLightfv(_index, GL_SPOT_DIRECTION, (GLfloat *)_light.spotDirection.data());
170 
171  glGetLightfv(_index, GL_SPOT_EXPONENT, &_light.spotExponent);
172  glGetLightfv(_index, GL_SPOT_CUTOFF, &_light.spotCutoff);
173  glGetLightfv(_index, GL_CONSTANT_ATTENUATION, &_light.constantAttenuation);
174  glGetLightfv(_index, GL_LINEAR_ATTENUATION, &_light.linearAttenuation);
175  glGetLightfv(_index, GL_QUADRATIC_ATTENUATION, &_light.quadraticAttenuation);
176 }
177 //=============================================================================
178 } // namespace SceneGraph
179 } // namespace ACG
180 //=============================================================================
GLenum index2gl(int _nr)
return GL_LIGHT* for light _nr
std::vector< LightSource > lightsSave_
save old LightSources
void enable(GLenum _nr)
enable LightSource _nr
Structure to hold options for one LightSource.
LightSourceNode(BaseNode *_parent=0, const std::string &_name="<LightSourceNode>")
Default constructor. Applies all properties.
const GLMatrixd & inverse_modelview() const
get inverse modelview matrix
Definition: GLState.hh:814
void enter(GLState &_state, const DrawModes::DrawMode &_drawmode)
set current Light Sources
static void enable(GLenum _cap)
replaces glEnable, but supports locking
std::vector< LightSource > lights_
store LightSources of this node
void get_parameters(GLenum _index, LightSource &_light)
get _light Options in OpenGL for GL_LIGHT::_index
void leave(GLState &_state, const DrawModes::DrawMode &_drawmode)
restores original Light Sources
static void disable(GLenum _cap)
replaces glDisable, but supports locking
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
void set_parameters(GLenum _index, LightSource &_light)
set _light Options in OpenGL for GL_LIGHT::_index
Scalar * data()
access to Scalar array
Definition: Vector11T.hh:193