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