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
69 LightSourceNode::LightSourceNode( BaseNode* _parent,
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
92 get_parameters(index2gl(i), lightsSave_[i]);
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
108 ACG::GLState::enable(index2gl(i));
109 set_parameters(index2gl(i), lights_[i]);
110 }
111 else ACG::GLState::disable(index2gl(i));
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 {
127 ACG::GLState::enable(index2gl(i));
128 set_parameters(index2gl(i), lightsSave_[i]);
129 }
130 else ACG::GLState::disable(index2gl(i));
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 //=============================================================================
ACG::GLState
Definition: GLState.hh:213
ACG::SceneGraph::LightSourceNode::LightSource
Structure to hold options for one LightSource.
Definition: LightSourceNode.hh:89
ACG
Namespace providing different geometric functions concerning angles.
Definition: BaseObjectData.hh:68
ACG::SceneGraph::ACG::GLState::disable
static void disable(GLenum _cap, bool _warnRemoved=true)
replaces glDisable, but supports locking
ACG::SceneGraph::LightSourceNode::lightsSave_
std::vector< LightSource > lightsSave_
save old LightSources
Definition: LightSourceNode.hh:203
OpenMesh::VectorT::data
Scalar * data()
access to Scalar array
Definition: Vector11T.hh:200
SceneGraph
Definition: QtPrincipalAxisDialog.hh:67
ACG::SceneGraph::LightSourceNode::get_parameters
void get_parameters(GLenum _index, LightSource &_light)
get _light Options in OpenGL for GL_LIGHT::_index
Definition: LightSourceNode.cc:156
ACG::GLState::inverse_modelview
const GLMatrixd & inverse_modelview() const
get inverse modelview matrix
Definition: GLState.hh:811
ACG::SceneGraph::LightSourceNode::enter
void enter(GLState &_state, const DrawModes::DrawMode &_drawmode) override
set current Light Sources
Definition: LightSourceNode.cc:81
ACG::SceneGraph::LightSourceNode::leave
void leave(GLState &_state, const DrawModes::DrawMode &_drawmode) override
restores original Light Sources
Definition: LightSourceNode.cc:120
ACG::SceneGraph::BaseNode
Definition: BaseNode.hh:104
ACG::SceneGraph::LightSourceNode::lights_
std::vector< LightSource > lights_
store LightSources of this node
Definition: LightSourceNode.hh:200
ACG::SceneGraph::LightSourceNode::set_parameters
void set_parameters(GLenum _index, LightSource &_light)
set _light Options in OpenGL for GL_LIGHT::_index
Definition: LightSourceNode.cc:136
ACG::SceneGraph::DrawModes::DrawMode
Specifies a DrawMode.
Definition: DrawModes.hh:288
ACG::SceneGraph::ACG::GLState::enable
static void enable(GLenum _cap, bool _warnRemoved=true)
replaces glEnable, but supports locking
ACG::SceneGraph::LightSourceNode::LightSourceNode
LightSourceNode(BaseNode *_parent=0, const std::string &_name="<LightSourceNode>")
Default constructor. Applies all properties.
Definition: LightSourceNode.cc:69
ACG::SceneGraph::LightSourceNode::index2gl
GLenum index2gl(int _nr)
return GL_LIGHT* for light _nr
Definition: LightSourceNode.hh:188
ACG::SceneGraph::LightSourceNode::enable
void enable(GLenum _nr)
enable LightSource _nr
Definition: LightSourceNode.hh:146