Developer Documentation
LightSourceNode.hh
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
50 //
51 //=============================================================================
52 
53 #ifndef ACG_LIGHTSOURCE_NODE_HH
54 #define ACG_LIGHTSOURCE_NODE_HH
55 
56 
57 //== INCLUDES =================================================================
58 
59 #include "BaseNode.hh"
60 #include "../GL/gl.hh"
61 #include <string>
62 #include <vector>
63 
64 
65 //== NAMESPACES ===============================================================
66 
67 namespace ACG {
68 namespace SceneGraph {
69 
70 
71 //== CLASS DEFINITION =========================================================
72 
73 
85 class ACGDLLEXPORT LightSourceNode : public BaseNode
86 {
87 
89  struct LightSource
90  {
91  //default Constructor
92  LightSource()
93  {
94  // set OpenGL defaults
95  enabled = false;
96  fixedPosition = false;
97 
98  ambientColor = Vec4f(0.1f,0.1f,0.1f,1.0f);
99  diffuseColor = Vec4f(1.0f,1.0f,1.0f,1.0f);
100  specularColor = Vec4f(1.0f,1.0f,1.0f,1.0f);
101  position = Vec4f(0.0f,0.0f,1.0f,0.0f);
102  realPosition = Vec4f(0.0f,0.0f,1.0f,0.0f);
103  spotDirection = Vec3f(0.0f,0.0f,-1.0f);
104  spotExponent = 0;
105  spotCutoff = 180;
106 
107  constantAttenuation = 1;
108  linearAttenuation = 0;
109  quadraticAttenuation = 0;
110  }
111 
112  bool enabled;
113  bool fixedPosition;
114  Vec4f ambientColor;
115  Vec4f diffuseColor;
116  Vec4f specularColor;
117  Vec4f position;
118  Vec4f realPosition;
119  Vec3f spotDirection;
120  float spotExponent;
121  float spotCutoff;
122  float constantAttenuation;
123  float linearAttenuation;
124  float quadraticAttenuation;
125  };
126 
127 
128 public:
129 
131  LightSourceNode( BaseNode* _parent = 0,
132  const std::string& _name = "<LightSourceNode>");
133 
135  virtual ~LightSourceNode() {}
136 
137 
138  ACG_CLASSNAME(LightSourceNode);
139 
141  void enter(GLState& _state, const DrawModes::DrawMode& _drawmode) override;
143  void leave(GLState& _state, const DrawModes::DrawMode& _drawmode) override;
144 
146  void enable(GLenum _nr)
147  { lights_[gl2index(_nr)].enabled = true; }
148 
150  void disable(GLenum _nr)
151  { lights_[gl2index(_nr)].enabled = false; }
152 
154  void set_position(GLenum _nr, Vec4f _pos)
155  { lights_[gl2index(_nr)].position = _pos; }
156 
158  void set_position(GLenum _nr, Vec3f _pos)
159  { set_position(_nr, Vec4f(_pos[0], _pos[1], _pos[2], 1)); }
160 
162  void set_direction(GLenum _nr, Vec3f _pos)
163  { set_position(_nr, Vec4f(_pos[0], _pos[1], _pos[2], 0)); }
164 
166  void set_ambient_color( GLenum _nr, Vec4f _color)
167  { lights_[gl2index(_nr)].ambientColor = _color; }
168 
170  void set_diffuse_color( GLenum _nr, Vec4f _color)
171  { lights_[gl2index(_nr)].diffuseColor = _color; }
172 
174  void set_specular_color( GLenum _nr, Vec4f _color)
175  { lights_[gl2index(_nr)].specularColor = _color; }
176 
178  void fixed_position(GLenum _nr, bool _state)
179  { lights_[gl2index(_nr)].fixedPosition = _state; }
180 
181 private:
182 
184  int gl2index( GLenum _nr)
185  { return( _nr - GL_LIGHT0); }
186 
188  GLenum index2gl( int _nr)
189  { return( _nr + GL_LIGHT0); }
190 
192  void set_parameters(GLenum _index, LightSource& _light);
193 
195  void get_parameters(GLenum _index, LightSource& _light);
196 
197 private:
198 
200  std::vector<LightSource> lights_;
201 
203  std::vector<LightSource> lightsSave_;
204 };
205 
206 
207 //=============================================================================
208 } // namespace SceneGraph
209 } // namespace ACG
210 //=============================================================================
211 #endif // ACG_LIGHTSOURCE_NODE_HH defined
212 //=============================================================================
213 
void set_position(GLenum _nr, Vec4f _pos)
set position ( _pos = 1) or direction ( _pos = 0) of LightSource
Structure to hold options for one LightSource.
void disable(GLenum _nr)
disable LightSource _nr
Namespace providing different geometric functions concerning angles.
VectorT< float, 4 > Vec4f
Definition: VectorT.hh:138
ACG::SceneGraph::LightSource LightSource
Simple Name for the LightSource.
Definition: LightTypes.hh:66
void set_position(GLenum _nr, Vec3f _pos)
set position for Point-LightSource
std::vector< LightSource > lightsSave_
save old LightSources
void set_diffuse_color(GLenum _nr, Vec4f _color)
set Diffuse color for LightSource _nr
int gl2index(GLenum _nr)
return index in vector for GL_LIGHT*
VectorT< float, 3 > Vec3f
Definition: VectorT.hh:119
void set_direction(GLenum _nr, Vec3f _pos)
set direction for directional LightSource
std::vector< LightSource > lights_
store LightSources of this node
GLenum index2gl(int _nr)
return GL_LIGHT* for light _nr
virtual ~LightSourceNode()
Destructor.
void enable(GLenum _nr)
enable LightSource _nr
void set_specular_color(GLenum _nr, Vec4f _color)
set Specular color for LightSource _nr
void fixed_position(GLenum _nr, bool _state)
make LightSource fixed or moveable with ModelViewMatrix
void set_ambient_color(GLenum _nr, Vec4f _color)
set Ambient color for LightSource _nr