Developer Documentation
LightWidget.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 #include "LightWidget.hh"
45 
46 LightWidget::LightWidget( ACG::SceneGraph::BaseNode* _node, QWidget *parent)
47  : QDialog(parent),
48  object_(0),
49  light_(0),
50  updatingWidgets_(false)
51 {
52  setupUi(this);
53 
54  node_ = dynamic_cast< ACG::SceneGraph::LightNode* >(_node);
55 
56  if ( ! node_ ) {
57  std::cerr << "LightWidget could not cast given node to LightNode!" << std::endl;
58  return;
59  }
60 
61  connect(directional,SIGNAL(clicked()),this,SLOT(directionalToggled()));
62 
63  connect(fixedPosition ,SIGNAL(clicked()),this,SLOT(fixedPositionChanged()));
64 
65  connect(xpos,SIGNAL(editingFinished()),this,SLOT(directionalToggled()));
66  connect(ypos,SIGNAL(editingFinished()),this,SLOT(directionalToggled()));
67  connect(zpos,SIGNAL(editingFinished()),this,SLOT(directionalToggled()));
68 
69  connect(xdir,SIGNAL(editingFinished()),this,SLOT(directionalToggled()));
70  connect(ydir,SIGNAL(editingFinished()),this,SLOT(directionalToggled()));
71  connect(zdir,SIGNAL(editingFinished()),this,SLOT(directionalToggled()));
72 
73  connect(spotx,SIGNAL(editingFinished()),this,SLOT(spotChanged()));
74  connect(spoty,SIGNAL(editingFinished()),this,SLOT(spotChanged()));
75  connect(spotz,SIGNAL(editingFinished()),this,SLOT(spotChanged()));
76 
77  connect(angle,SIGNAL(editingFinished()),this,SLOT(spotChanged()));
78  connect(exponent,SIGNAL(editingFinished()),this,SLOT(spotChanged()));
79 
80  brightness->setTracking(true);
81  connect(brightness, SIGNAL(valueChanged(int)), this, SLOT(brightnessChanged(int)));
82 
83  connect(ambientR,SIGNAL(editingFinished()),this,SLOT(ambientChanged()));
84  connect(ambientG,SIGNAL(editingFinished()),this,SLOT(ambientChanged()));
85  connect(ambientB,SIGNAL(editingFinished()),this,SLOT(ambientChanged()));
86  connect(ambientA,SIGNAL(editingFinished()),this,SLOT(ambientChanged()));
87 
88  connect(diffuseR,SIGNAL(editingFinished()),this,SLOT(diffuseChanged()));
89  connect(diffuseG,SIGNAL(editingFinished()),this,SLOT(diffuseChanged()));
90  connect(diffuseB,SIGNAL(editingFinished()),this,SLOT(diffuseChanged()));
91  connect(diffuseA,SIGNAL(editingFinished()),this,SLOT(diffuseChanged()));
92 
93  connect(specularR,SIGNAL(editingFinished()),this,SLOT(specularChanged()));
94  connect(specularG,SIGNAL(editingFinished()),this,SLOT(specularChanged()));
95  connect(specularB,SIGNAL(editingFinished()),this,SLOT(specularChanged()));
96  connect(specularA,SIGNAL(editingFinished()),this,SLOT(specularChanged()));
97 
98  connect(radius,SIGNAL(editingFinished()),this,SLOT(radiusChanged()));
99 }
100 
101 void LightWidget::showEvent ( QShowEvent * /*event*/ )
102 {
103  if ( !getObject() )
104  return;
105 
106  // Block updates
107  updatingWidgets_ = true;
108 
109  directional->setChecked( light_->directional() );
110 
111  // Switch to right tab
112  if ( directional->isChecked() )
113  stackedWidget->setCurrentIndex(1);
114  else
115  stackedWidget->setCurrentIndex(0);
116 
117  xdir->setText(QString::number(light_->direction()[0]));
118  ydir->setText(QString::number(light_->direction()[1]));
119  zdir->setText(QString::number(light_->direction()[2]));
120 
121  xpos->setText(QString::number(light_->position()[0]));
122  ypos->setText(QString::number(light_->position()[1]));
123  zpos->setText(QString::number(light_->position()[2]));
124 
125  fixedPosition->setChecked( light_->fixedPosition() );
126 
127  spotx->setText(QString::number(light_->spotDirection()[0]));
128  spoty->setText(QString::number(light_->spotDirection()[1]));
129  spotz->setText(QString::number(light_->spotDirection()[2]));
130 
131  angle->setText(QString::number(light_->spotCutoff()));
132  exponent->setText(QString::number(light_->spotExponent()));
133 
134  ambientR->setText(QString::number(light_->ambientColor()[0]));
135  ambientG->setText(QString::number(light_->ambientColor()[1]));
136  ambientB->setText(QString::number(light_->ambientColor()[2]));
137  ambientA->setText(QString::number(light_->ambientColor()[3]));
138 
139  diffuseR->setText(QString::number(light_->diffuseColor()[0]));
140  diffuseG->setText(QString::number(light_->diffuseColor()[1]));
141  diffuseB->setText(QString::number(light_->diffuseColor()[2]));
142  diffuseA->setText(QString::number(light_->diffuseColor()[3]));
143 
144  specularR->setText(QString::number(light_->specularColor()[0]));
145  specularG->setText(QString::number(light_->specularColor()[1]));
146  specularB->setText(QString::number(light_->specularColor()[2]));
147  specularA->setText(QString::number(light_->specularColor()[3]));
148 
149  brightness->setSliderPosition((int)(light_->brightness()*100));
150 
151  radius->setText(QString::number(light_->radius()));
152 
153  // Allow updates
154  updatingWidgets_ = false;
155 
156 }
157 
158 
160 
161  // Block if we currently update the widgets or if we dont get the object
162  if (updatingWidgets_ || !getObject() )
163  return;
164 
165  // Switch to right tab
166  if ( directional->isChecked() )
167  stackedWidget->setCurrentIndex(1);
168  else
169  stackedWidget->setCurrentIndex(0);
170 
171  if ( directional->isChecked() )
172  light_->direction( ACG::Vec3d( xdir->text().toDouble() ,ydir->text().toDouble() ,zdir->text().toDouble() ));
173  else
174  light_->position( ACG::Vec3d( xpos->text().toDouble() ,ypos->text().toDouble() ,zpos->text().toDouble() ));
175 
176  updated();
177 
178 }
179 
181 
182  // Block if we currently update the widgets or if we dont get the object
183  if (updatingWidgets_ || !getObject() )
184  return;
185 
186  light_->fixedPosition( fixedPosition->isChecked() );
187 
188  updated();
189 }
190 
192 
193  // Block if we currently update the widgets or if we dont get the object
194  if (updatingWidgets_ || !getObject() )
195  return;
196 
197  light_->ambientColor(ACG::Vec4f(ambientR->text().toDouble(),
198  ambientG->text().toDouble(),
199  ambientB->text().toDouble(),
200  ambientA->text().toDouble()));
201  updated();
202 }
203 
204 void LightWidget::diffuseChanged() {
205 
206  // Block if we currently update the widgets or if we dont get the object
207  if (updatingWidgets_ || !getObject() )
208  return;
209 
210  light_->diffuseColor(ACG::Vec4f(diffuseR->text().toDouble(),
211  diffuseG->text().toDouble(),
212  diffuseB->text().toDouble(),
213  diffuseA->text().toDouble()));
214  updated();
215 }
216 
217 void LightWidget::specularChanged() {
218 
219  // Block if we currently update the widgets or if we dont get the object
220  if (updatingWidgets_ || !getObject() )
221  return;
222 
223  light_->specularColor(ACG::Vec4f(specularR->text().toDouble(),
224  specularG->text().toDouble(),
225  specularB->text().toDouble(),
226  specularA->text().toDouble()));
227  updated();
228 }
229 
230 void LightWidget::brightnessChanged(int _newValue) {
231 
232  // Block if we currently update the widgets or if we dont get the object
233  if (updatingWidgets_ || !getObject() )
234  return;
235 
236  float pos = _newValue;
237  pos /= 100.0f;
238 
239  light_->brightness(pos);
240 
241  updated();
242 }
243 
245 
246  // Block if we currently update the widgets or if we dont get the object
247  if (updatingWidgets_ || !getObject() )
248  return;
249 
250  light_->radius(radius->text().toDouble());
251 
252  updated();
253 }
254 
256  // Block if we currently update the widgets or if we dont get the object
257  if (updatingWidgets_ || !getObject() )
258  return;
259 
260  light_->spotDirection( ACG::Vec3d( spotx->text().toDouble() ,spoty->text().toDouble() ,spotz->text().toDouble() ));
261  light_->spotCutoff(angle->text().toDouble());
262 
263  light_->spotExponent(exponent->text().toDouble());
264 
265  updated();
266 }
267 
269 
270  if ( !object_ ) {
272  if ( o_it->hasNode(node_) ) {
273  object_ = PluginFunctions::lightObject(*o_it);
274  light_ = PluginFunctions::lightSource(object_);
275  return true;
276  }
277  }
278 
279  std::cerr << " Object not found! " << std::endl;
280 
281  // Not Found!
282  return false;
283  }
284 
285  return true;
286 }
287 
289 
290  // Update the object
291  object_->update();
292 
293  // Force a redraw
294  node_->setDirty();
295 
296 }
void position(Vec3d _pos)
Set position for LightSource.
Definition: LightNode.cc:108
void specularColor(Vec4f _color)
set Specular color for LightSource
Definition: LightNode.cc:160
void updated()
Called when the object has been updated.
Definition: LightWidget.cc:288
LightObject * lightObject(BaseObjectData *_object)
Cast an BaseObject to a LightObject if possible.
bool getObject()
Initializes the internal object. Returns true if successfull.
Definition: LightWidget.cc:268
void fixedPositionChanged()
The fixed position checkbox.
Definition: LightWidget.cc:180
const QStringList ALL_OBJECTS
Iterable object range.
void ambientColor(Vec4f _color)
set Ambient color for LightSource
Definition: LightNode.cc:148
float radius() const
Get light source radius.
Definition: LightNode.hh:206
DLLEXPORT ObjectIterator objectsEnd()
Return Iterator to Object End.
void diffuseColor(Vec4f _color)
set Diffuse color for LightSource
Definition: LightNode.cc:154
void spotChanged()
Spot direction changed.
Definition: LightWidget.cc:255
virtual void update(UpdateType _type=UPDATE_ALL)
Update the Light Object.
Definition: LightObject.cc:330
Vec3d direction() const
Get direction of the light source.
Definition: LightNode.cc:123
void setDirty(bool _dirty=true)
mark node for redrawn
virtual void showEvent(QShowEvent *event)
Initialize contents of widget before showing it.
Definition: LightWidget.cc:101
void directionalToggled()
The directional checkbox changed -> update object.
Definition: LightWidget.cc:159
void fixedPosition(bool _state)
make LightSource fixed or moveable with ModelViewMatrix
Definition: LightNode.cc:172
bool directional() const
Check if the light source is a directional light source.
Definition: LightNode.cc:127
void brightnessChanged(int _newValue)
Brightness value has changed.
Definition: LightWidget.cc:230
void spotDirection(Vec3d _pos)
Set spot direction.
Definition: LightNode.cc:141
#define DATA_LIGHT
Definition: Light.hh:58
void ambientChanged()
Color values have changed.
Definition: LightWidget.cc:191
void radiusChanged()
Light radius has changed.
Definition: LightWidget.cc:244
LightSource * lightSource(BaseObjectData *_object)
Get the lightSource in this Object.