Developer Documentation
FileLgt.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 "FileLgt.hh"
45 
46  #include <QtWidgets>
47 
49 }
50 
52  return QString( tr("Light definition files ( *.lgt )") );
53 };
54 
56  return QString( tr("Light definition files ( *.lgt )") );
57 };
58 
60  DataType type = DATA_LIGHT;
61  return type;
62 }
63 
64 int FileLightPlugin::loadObject(QString _filename)
65 {
66  int id = -1;
67  emit addEmptyObject( DATA_LIGHT, id );
68 
69  LightObject* lightObject = 0;
70  if(PluginFunctions::getObject( id, lightObject))
71  {
72  if( lightObject )
73  {
74 
75  QFileInfo fi(_filename);
76 
77  if ( fi.exists() ){
78  LightSource* light = PluginFunctions::lightSource(lightObject);
79 
80  QString name = lightObject->name();
81 
82  QSettings settings(_filename, QSettings::IniFormat);
83  settings.beginGroup("LIGHT");
84 
85  if ( settings.contains("Name") ) {
86 
87  QString name = settings.value("Name").toString();
88 
89  lightObject->setName( name );
90  } else {
91  lightObject->setFromFileName(_filename);
92  }
93 
94  if (settings.contains("PositionX")) {
95  ACG::Vec3d position;
96  position[0] = settings.value("PositionX").toDouble();
97  position[1] = settings.value("PositionY").toDouble();
98  position[2] = settings.value("PositionZ").toDouble();
99 
100  light->position(position);
101  }
102 
103  if (settings.contains("DirectionX")) {
104  ACG::Vec3d direction;
105  direction[0] = settings.value("DirectionX").toDouble();
106  direction[1] = settings.value("DirectionY").toDouble();
107  direction[2] = settings.value("DirectionZ").toDouble();
108 
109  light->direction(direction);
110  }
111 
112  if (settings.contains("SpotDirectionX")) {
113  ACG::Vec3d spotDirection;
114  spotDirection[0] = settings.value("SpotDirectionX").toDouble();
115  spotDirection[1] = settings.value("SpotDirectionY").toDouble();
116  spotDirection[2] = settings.value("SpotDirectionZ").toDouble();
117 
118  light->spotDirection(spotDirection);
119  }
120 
121  if (settings.contains("AmbientColorR")) {
122  ACG::Vec4f ambientColor;
123  ambientColor[0] = settings.value("AmbientColorR").toDouble();
124  ambientColor[1] = settings.value("AmbientColorG").toDouble();
125  ambientColor[2] = settings.value("AmbientColorB").toDouble();
126  if (settings.contains("AmbientColorA"))
127  ambientColor[3] = settings.value("AmbientColorA").toDouble();
128  else
129  ambientColor[3] = 1.0f;
130 
131  light->ambientColor(ambientColor);
132  }
133 
134  if (settings.contains("DiffuseColorR")) {
135  ACG::Vec4f diffuseColor;
136  diffuseColor[0] = settings.value("DiffuseColorR").toDouble();
137  diffuseColor[1] = settings.value("DiffuseColorG").toDouble();
138  diffuseColor[2] = settings.value("DiffuseColorB").toDouble();
139  if (settings.contains("DiffuseColorA"))
140  diffuseColor[3] = settings.value("DiffuseColorA").toDouble();
141  else
142  diffuseColor[3] = 1.0f;
143 
144  light->diffuseColor(diffuseColor);
145  }
146 
147  if (settings.contains("SpecularColorR")) {
148  ACG::Vec4f specularColor;
149  specularColor[0] = settings.value("SpecularColorR").toDouble();
150  specularColor[1] = settings.value("SpecularColorG").toDouble();
151  specularColor[2] = settings.value("SpecularColorB").toDouble();
152  if (settings.contains("SpecularColorA"))
153  specularColor[3] = settings.value("SpecularColorA").toDouble();
154  else
155  specularColor[3] = 1.0f;
156 
157  light->specularColor(specularColor);
158  }
159 
160  if (settings.contains("FixedPosition")) {
161 
162  bool fixedPosition = settings.value("FixedPosition").toBool();
163 
164  light->fixedPosition(fixedPosition);
165  }
166 
167  if (settings.contains("SpotExponent")) {
168 
169  float spotExponent = settings.value("SpotExponent").toDouble();
170 
171  light->spotExponent(spotExponent);
172  }
173 
174  if (settings.contains("SpotCutOff")) {
175 
176  float spotCutOff = settings.value("SpotCutOff").toDouble();
177 
178  light->spotCutoff(spotCutOff);
179  }
180 
181  if (settings.contains("ConstantAttenuation")) {
182 
183  float cAttenuation = settings.value("ConstantAttenuation").toDouble();
184 
185  light->constantAttenuation(cAttenuation);
186  }
187 
188  if (settings.contains("LinearAttenuation")) {
189 
190  float lAttenuation = settings.value("LinearAttenuation").toDouble();
191 
192  light->linearAttenuation(lAttenuation);
193  }
194 
195  if (settings.contains("QuadraticAttenuation")) {
196 
197  float qAttenuation = settings.value("QuadraticAttenuation").toDouble();
198 
199  light->quadraticAttenuation(qAttenuation);
200  }
201 
202  if (settings.contains("Radius")) {
203 
204  float radius = settings.value("Radius").toDouble();
205 
206  light->radius(radius);
207  }
208 
209  if (settings.contains("Enabled")) {
210 
211  bool enabled = settings.value("Enabled").toBool();
212 
213  enabled ? light->enable() : light->disable();
214  }
215 
216  settings.endGroup();
217  }
218 
219  emit updatedObject( lightObject->id(), UPDATE_ALL );
220  emit openedFile( lightObject->id() );
221 
222  }
223 
224  }
225 
226  return id;
227 };
228 
229 bool FileLightPlugin::saveObject(int _id, QString _filename)
230 {
231 
232  BaseObjectData* obj(0);
233  if(PluginFunctions::getObject( _id, obj))
234  {
235  LightObject* lightObject = PluginFunctions::lightObject(obj);
236  if( lightObject )
237  {
238  LightSource* light = PluginFunctions::lightSource(lightObject);
239 
240  QSettings settings(_filename, QSettings::IniFormat);
241  settings.beginGroup("LIGHT");
242 
243  settings.setValue("Name",lightObject->name());
244 
245  if(!light->directional()) {
246  settings.setValue("PositionX", light->position()[0]);
247  settings.setValue("PositionY", light->position()[1]);
248  settings.setValue("PositionZ", light->position()[2]);
249  } else {
250  settings.setValue("DirectionX", light->direction()[0]);
251  settings.setValue("DirectionY", light->direction()[1]);
252  settings.setValue("DirectionZ", light->direction()[2]);
253  }
254 
255  settings.setValue("SpotDirectionX", light->spotDirection()[0]);
256  settings.setValue("SpotDirectionY", light->spotDirection()[1]);
257  settings.setValue("SpotDirectionZ", light->spotDirection()[2]);
258 
259  settings.setValue("AmbientColorR", light->ambientColor()[0]);
260  settings.setValue("AmbientColorG", light->ambientColor()[1]);
261  settings.setValue("AmbientColorB", light->ambientColor()[2]);
262  settings.setValue("AmbientColorA", light->ambientColor()[3]);
263 
264  settings.setValue("DiffuseColorR", light->diffuseColor()[0]);
265  settings.setValue("DiffuseColorG", light->diffuseColor()[1]);
266  settings.setValue("DiffuseColorB", light->diffuseColor()[2]);
267  settings.setValue("DiffuseColorA", light->diffuseColor()[3]);
268 
269  settings.setValue("SpecularColorR", light->specularColor()[0]);
270  settings.setValue("SpecularColorG", light->specularColor()[1]);
271  settings.setValue("SpecularColorB", light->specularColor()[2]);
272  settings.setValue("SpecularColorA", light->specularColor()[3]);
273 
274  settings.setValue("FixedPosition", light->fixedPosition());
275 
276  settings.setValue("SpotExponent", light->spotExponent());
277 
278  settings.setValue("SpotCutOff", light->spotCutoff());
279 
280  settings.setValue("ConstantAttenuation", light->constantAttenuation());
281 
282  settings.setValue("LinearAttenuation", light->linearAttenuation());
283 
284  settings.setValue("QuadraticAttenuation", light->quadraticAttenuation());
285 
286  settings.setValue("Radius", light->radius());
287 
288  settings.setValue("Enabled", light->enabled());
289 
290  settings.endGroup();
291 
292  obj->setFromFileName(_filename);
293  obj->setName(obj->filename());
294  }
295  }
296 
297  return true;
298 }
299 
300 
QString name()
Return a name for the plugin.
Definition: FileLgt.hh:85
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
bool directional() const
Check if the light source is a directional light source.
Definition: LightNode.cc:127
void spotDirection(Vec3d _pos)
Set spot direction.
Definition: LightNode.cc:141
void specularColor(Vec4f _color)
set Specular color for LightSource
Definition: LightNode.cc:160
int id() const
Definition: BaseObject.cc:190
void enable()
enable LightSource
Definition: LightNode.cc:131
void setFromFileName(const QString &_filename)
Definition: BaseObject.cc:716
LightObject * lightObject(BaseObjectData *_object)
Cast an BaseObject to a LightObject if possible.
DataType supportedType()
Return your supported object type( e.g. DATA_TRIANGLE_MESH )
Definition: FileLgt.cc:59
void fixedPosition(bool _state)
make LightSource fixed or moveable with ModelViewMatrix
Definition: LightNode.cc:172
virtual void setName(QString _name)
path to the file from which the object is loaded ( defaults to "." )
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
QString name() const
return the name of the object. The name defaults to NONAME if unset.
Definition: BaseObject.cc:730
QString filename() const
return the filename of the object
Definition: BaseObject.cc:706
void disable()
disable LightSource
Definition: LightNode.cc:134
Structure to hold options for one LightSource.
Definition: LightNode.hh:85
#define DATA_LIGHT
Definition: Light.hh:58
bool enabled() const
Get light source status.
Definition: LightNode.cc:137
void diffuseColor(Vec4f _color)
set Diffuse color for LightSource
Definition: LightNode.cc:154
Vec3d direction() const
Get direction of the light source.
Definition: LightNode.cc:123
Predefined datatypes.
Definition: DataTypes.hh:83
void position(Vec3d _pos)
Set position for LightSource.
Definition: LightNode.cc:108
QString getSaveFilters()
Definition: FileLgt.cc:55
LightSource * lightSource(BaseObjectData *_object)
Get the lightSource in this Object.
void setName(QString _name)
Set the name of the Object.
Definition: LightObject.cc:213
QString getLoadFilters()
Definition: FileLgt.cc:51
void ambientColor(Vec4f _color)
set Ambient color for LightSource
Definition: LightNode.cc:148
void initializePlugin()
Initialize Plugin.
Definition: FileLgt.cc:48
float radius() const
Get light source radius.
Definition: LightNode.hh:206