Developer Documentation

LightObject.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 // Light Object
47 //
48 //=============================================================================
49
50 #define LIGHTOBJECT_C
51
52 //== INCLUDES =================================================================
53
54 #include <OpenFlipper/common/Types.hh>
55 #include <OpenFlipper/BasePlugin/PluginFunctions.hh>
56 #include <OpenFlipper/BasePlugin/PluginFunctionsSceneGraphWidget.hh>
57 #include <ACG/QtWidgets/QtSceneGraphWidget.hh>
58 #include "LightWidget.hh"
59
60 #include <ACG/Scenegraph/MaterialNode.hh>
61
62
63 //== DEFINES ==================================================================
64
65 //== TYPEDEFS =================================================================
66
67 //== CLASS DEFINITION =========================================================
68
69 class LightWidgetGenerator : public ACG::QtWidgets::SceneGraphWidgetGenerator {
70 public:
71 // constructor
72 LightWidgetGenerator() {
73 }
74
75
76 virtual QWidget* getWidget(ACG::SceneGraph::BaseNode* _node ) {
77
78 // Check if we can handle this type
79 if ( ! canHandle( _node->className() ) )
80 return 0;
81
82 // Check if we already have a widget, otherwise generate a new one.
83 if ( widgetMap_.contains(_node) ) {
84 return widgetMap_[_node];
85 } else {
86 QWidget* widget = new LightWidget(_node);
87 widgetMap_[_node] = widget;
88 return widget;
89 }
90
91 }
92
93 virtual bool canHandle(std::string _className) {
94 return ( _className == std::string("LightNode") );
95 }
96
97 virtual std::string handles() {
98 return std::string("LightNode");
99 }
100
101 virtual QString contextMenuName() {
102 return QString("Edit Light");
103 }
104 };
105
106 static LightWidgetGenerator lightWidgetGenerator_;
107
111 LightObject::LightObject( ) :
112 BaseObjectData(),
113 lightNode_(0),
114 lightNodeVis_(0)
115 {
116 setDataType(DATA_LIGHT);
117 setTypeIcon(DATA_LIGHT,"LightType.png");
118 init();
119
120 // Make sure, the generator is added to the widget
121 PluginFunctions::addSceneGraphGenerator( &lightWidgetGenerator_ );
122 }
123
124 //=============================================================================
125
126
130 LightObject::LightObject(const LightObject & _object) :
131 BaseObjectData(_object),
132 defaultLightSource_(false)
133 {
134
135 init(_object.lightNode_);
136
137 setName( name() );
138 }
139
143 LightObject::~LightObject()
144 {
145 // Delete the data attached to this object ( this will remove all perObject data)
146 // Not the best way to do it but it will work.
147 // This is only necessary if people use references to the light below and
148 // they do something with the light in the destructor of their
149 // perObjectData.
150 deleteData();
151
152 // Move children to parent
153 BaseNode* parent = lightNode_->parent();
154
155 // First, collect all children as the iterator will get invalid if we delete while iterating!
156 std::vector< BaseNode*> children;
157 for (BaseNode::ChildIter cIt=lightNode_->childrenBegin(); cIt!=lightNode_->childrenEnd(); ++cIt)
158 children.push_back( (*cIt) );
159
160 // Move the children
161 for (unsigned int i = 0 ; i < children.size(); ++i )
162 children[i]->set_parent(parent);
163
164 // Delete the scenegraph node
165 delete lightNode_;
166 }
167
171 void LightObject::cleanup() {
172
173 BaseObjectData::cleanup();
174
175 lightNode_ = NULL;
176
177 setDataType(DATA_LIGHT);
178 setTypeIcon(DATA_LIGHT,"LightType.png");
179
180 init();
181
182 }
183
187 BaseObject* LightObject::copy() {
188 LightObject* object = new LightObject(*this);
189 return dynamic_cast< BaseObject* >(object);
190 }
191
194 void LightObject::init(LightNode* _light, LightNode* _lightVis) {
195
196 // Light nodes have to be on top of all other nodes.
197 lightNode_ = new LightNode( 0 , "LightNode");
198 lightNode_->visualize(false);
199 PluginFunctions::addGlobalStatusNode(lightNode_);
200
201 lightNodeVis_ = new LightNode( materialNode() , "LightNode Visualization");
202 lightNodeVis_->visualize(true);
203 lightNodeVis_->show();
204 }
205
206 // ===============================================================================
207 // Name/Path Handling
208 // ===============================================================================
209
213 void LightObject::setName( QString _name ) {
214 BaseObjectData::setName(_name);
215
216 std::string nodename = std::string("LightNode for Light " + _name.toUtf8() );
217 lightNode_->name( nodename );
218
219 std::string nodenameVis = std::string("LightNode Visualization for Light " + _name.toUtf8() );
220 lightNodeVis_->name( nodenameVis );
221 }
222
223 // ===============================================================================
224 // Visualization
225 // ===============================================================================
226
227 LightNode* LightObject::lightNode() {
228 return lightNode_;
229 }
230
231 LightNode* LightObject::lightNodeVis() {
232 return lightNodeVis_;
233 }
234
235 bool LightObject::hasNode(BaseNode* _node) {
236 if ( BaseObjectData::hasNode(_node) )
237 return true;
238
239 if ( _node == lightNode_ )
240 return true;
241
242 return false;
243 }
244
245 // ===============================================================================
246 // Object information
247 // ===============================================================================
248
254 QString LightObject::getObjectinfo() {
255 QString output;
256
257 output += "========================================================================\n";
258 output += BaseObjectData::getObjectinfo();
259
260 if ( dataType( DATA_LIGHT ) )
261 output += "Object Contains Light : ";
262
263 ACG::Vec3d pos = lightSource_.position();
264 // ACG::Vec3f nor = lightNode_->normal();
265
266 output += " Position ( " + QString::number(pos[0]) + ", " + QString::number(pos[1]) + ", " + QString::number(pos[2]) + ")";
267 // output += " Normal ( " + QString::number(nor[0]) + ", " + QString::number(nor[1]) + ", " + QString::number(nor[2]) + ")";
268
269 output += "========================================================================\n";
270 return output;
271 }
272
273 // ===============================================================================
274 // Picking
275 // ===============================================================================
276
283 bool LightObject::picked( uint _node_idx ) {
284 return ( _node_idx == lightNode_->id() || _node_idx == lightNodeVis_->id() );
285 }
286
287 void LightObject::enablePicking( bool _enable ) {
288 lightNode_->enablePicking( _enable );
289 }
290
291 bool LightObject::pickingEnabled() {
292 return lightNode_->pickingEnabled();
293 }
294
295 void LightObject::show() {
296 if ( !visible_ ) {
297 BaseObjectData::show();
298 visible_ = true;
299
300 lightNodeVis_->visualize(true);
301
302 emit visibilityChanged( id() );
303 }
304 }
305
306 void LightObject::hide() {
307 if ( visible_ ) {
308 BaseObjectData::hide();
309 visible_ = false;
310
311 emit visibilityChanged( id() );
312 }
313 }
314
315 void LightObject::visible(bool _visible) {
316 if ( _visible )
317 show();
318 else
319 hide();
320 }
321
322 bool LightObject::visible() {
323 return visible_;
324 }
325
326 LightSource* LightObject::lightSource() {
327 return &lightSource_;
328 }
329
330 void LightObject::update(UpdateType _type) {
331
332 if(lightSource_.enabled()) {
333 lightNode_->setMultipassStatus(ACG::SceneGraph::BaseNode::ALLPASSES);
334 lightNode_->set_status( ACG::SceneGraph::BaseNode::Active );
335 } else {
336 lightNode_->setMultipassStatus(ACG::SceneGraph::BaseNode::NOPASS);
337 lightNode_->set_status( ACG::SceneGraph::BaseNode::HideNode );
338 }
339
340 lightNode_->setLightSource(lightSource_);
341 lightNodeVis_->setLightSource(lightSource_);
342 }
343
344 //=============================================================================
345
BaseObjectData::show
virtual void show()
Sets the whole Scenegraph subtree of this node to visible.
Definition: BaseObjectData.cc:188
LightObject::hasNode
virtual bool hasNode(BaseNode *_node)
Light status node.
Definition: LightObject.cc:235
LightObject::LightObject
LightObject()
constructor
Definition: LightObject.cc:111
UpdateType
Update type class.
Definition: UpdateType.hh:60
BaseObject::getObjectinfo
virtual QString getObjectinfo()
Get all Info for the Object as a string.
Definition: BaseObject.cc:244
PluginFunctions::addSceneGraphGenerator
void addSceneGraphGenerator(ACG::QtWidgets::SceneGraphWidgetGenerator *_generator)
Add a scenegraph generator ( the handled type will be extracted from the generator) ...
Definition: PluginFunctions.cc:1156
LightObject::getObjectinfo
QString getObjectinfo()
Get all Info for the Object as a string.
Definition: LightObject.cc:254
ACG::SceneGraph::BaseNode::className
virtual const std::string & className() const =0
Return class name (implemented by the ACG_CLASSNAME macro)
BaseObject::visibilityChanged
void visibilityChanged(int _objectId)
ACG::SceneGraph::ACG::SceneGraph::BaseNode::enablePicking
void enablePicking(bool _enable)
Definition: MeshNode2T_impl.hh:265
LightObject::lightNodeVis
LightNode * lightNodeVis()
Get the scenegraph Node.
Definition: LightObject.cc:231
BaseObject::visible_
bool visible_
Definition: BaseObject.hh:274
LightObject::update
virtual void update(UpdateType _type=UPDATE_ALL)
Update the Light Object.
Definition: LightObject.cc:330
LightObject::lightSource
LightSource * lightSource()
Definition: LightObject.cc:326
LightObject::show
virtual void show()
Show Light Node.
Definition: LightObject.cc:295
BaseObjectData::materialNode
MaterialNode * materialNode()
get a pointer to the materialnode
Definition: BaseObjectData.cc:263
BaseObject::setDataType
void setDataType(DataType _type)
Definition: BaseObject.cc:233
ACG::SceneGraph::LightNode::visualize
bool visualize()
Should node be visualized?
Definition: LightNode.hh:290
ACG::SceneGraph::ACG::SceneGraph::BaseNode::name
std::string name() const
Returns: name of node (needs not be unique)
Definition: MeshNode2T_impl.hh:415
PluginFunctions.hh
BaseObject::deleteData
void deleteData()
Delete all data attached to this object ( calls delete on each object )
Definition: BaseObject.cc:812
BaseObjectData::hasNode
virtual bool hasNode(BaseNode *_node)
Check if the given node is owned by this object.
Definition: BaseObjectData.cc:229
LightWidgetGenerator::handles
virtual std::string handles()
return the type this generator handles
Definition: LightObject.cc:97
ACG::SceneGraph::BaseNode::HideNode
Hide this node, but draw children.
Definition: BaseNode.hh:394
LightObject
Definition: LightObject.hh:72
LightObject::hide
virtual void hide()
Hide Light Node.
Definition: LightObject.cc:306
ACG::SceneGraph::LightNode
Definition: LightNode.hh:249
BaseObjectData::cleanup
virtual void cleanup()
Definition: BaseObjectData.cc:102
LightObject::pickingEnabled
bool pickingEnabled()
Check if picking is enabled for this Object.
Definition: LightObject.cc:291
ACG::SceneGraph::ACG::SceneGraph::BaseNode::id
unsigned int id() const
Definition: MeshNode2T_impl.hh:423
ACG::SceneGraph::ACG::SceneGraph::BaseNode::set_status
void set_status(StatusMode _s)
Set the status of this node.
Definition: MeshNode2T_impl.hh:403
LightObject::enablePicking
void enablePicking(bool _enable)
Enable or disable picking for this Object.
Definition: LightObject.cc:287
BaseObjectData::setName
virtual void setName(QString _name)
path to the file from which the object is loaded ( defaults to "." )
Definition: BaseObjectData.cc:355
BaseObject::name
QString name() const
return the name of the object. The name defaults to NONAME if unset.
Definition: BaseObject.cc:730
setTypeIcon
DLLEXPORT void setTypeIcon(DataType _id, QString _icon)
Set an Icon for a given DataType.
Definition: Types.cc:223
LightWidgetGenerator::contextMenuName
virtual QString contextMenuName()
Return a name for your widget in the context menu.
Definition: LightObject.cc:101
LightObject::~LightObject
virtual ~LightObject()
destructor
Definition: LightObject.cc:143
ACG::SceneGraph::ACG::SceneGraph::BaseNode::show
void show()
Show node: set status to Active.
Definition: MeshNode2T_impl.hh:407
LightWidgetGenerator
Definition: LightObject.cc:69
ACG::SceneGraph::LightSource
Structure to hold options for one LightSource.
Definition: LightNode.hh:85
LightObject::picked
bool picked(uint _node_idx)
detect if the node has been picked
Definition: LightObject.cc:283
DATA_LIGHT
#define DATA_LIGHT
Definition: Light.hh:58
ACG::SceneGraph::LightSource::enabled
bool enabled() const
Get light source status.
Definition: LightNode.cc:137
PluginFunctions::addGlobalStatusNode
void addGlobalStatusNode(ACG::SceneGraph::BaseNode *_node)
Adds a global status node.
Definition: PluginFunctions.cc:965
LightObject::defaultLightSource_
bool defaultLightSource_
Definition: LightObject.hh:134
LightObject::copy
BaseObject * copy()
Definition: LightObject.cc:187
ACG::QtWidgets::SceneGraphWidgetGenerator
Definition: QtSceneGraphWidget.hh:81
LightObject::lightSource_
LightSource lightSource_
Definition: LightObject.hh:130
LightWidget
Definition: LightWidget.hh:51
Types.hh
ACG::SceneGraph::ACG::SceneGraph::BaseNode::childrenBegin
ChildIter childrenBegin()
Returns: begin-iterator of children.
Definition: MeshNode2T_impl.hh:294
LightObject::lightNode
LightNode * lightNode()
Get the scenegraph Node.
Definition: LightObject.cc:227
ACG::SceneGraph::ACG::SceneGraph::BaseNode::parent
BaseNode * parent()
Get the nodes parent node.
Definition: MeshNode2T_impl.hh:372
PluginFunctionsSceneGraphWidget.hh
ACG::SceneGraph::BaseNode
Definition: BaseNode.hh:104
BaseObject
Definition: BaseObject.hh:82
ACG::SceneGraph::LightSource::position
void position(Vec3d _pos)
Set position for LightSource.
Definition: LightNode.cc:108
OpenMesh::VectorT
Definition: Vector11T.hh:83
LightWidgetGenerator::canHandle
virtual bool canHandle(std::string _className)
returns if the widgets can handle the given class
Definition: LightObject.cc:93
ACG::SceneGraph::ACG::SceneGraph::BaseNode::pickingEnabled
bool pickingEnabled()
Definition: MeshNode2T_impl.hh:269
ACG::SceneGraph::BaseNode::Active
Draw node & children.
Definition: BaseNode.hh:392
BaseObject::parent
BaseObject * parent()
Get the parent item ( 0 if rootitem )
Definition: BaseObject.cc:466
LightNode
ACG::SceneGraph::LightNode LightNode
Simple Name for PlaneNode.
Definition: LightTypes.hh:69
ACG::SceneGraph::ACG::SceneGraph::BaseNode::childrenEnd
ChildIter childrenEnd()
Returns: end-iterator of children.
Definition: MeshNode2T_impl.hh:298
LightObject::visible
virtual bool visible()
Show Light Node.
Definition: LightObject.cc:322
LightObject::init
virtual void init(LightNode *_light=0, LightNode *_lightVis=0)
Initialize current object, including all related nodes.
Definition: LightObject.cc:194
BaseObjectData
Definition: BaseObjectData.hh:102
LightObject::setName
void setName(QString _name)
Set the name of the Object.
Definition: LightObject.cc:213
LightObject::cleanup
virtual void cleanup()
Reset current object, including all related nodes.
Definition: LightObject.cc:171
ACG::SceneGraph::BaseNode::ChildIter
std::vector< BaseNode * >::iterator ChildIter
allows to iterate over children
Definition: BaseNode.hh:286
LightWidgetGenerator::getWidget
virtual QWidget * getWidget(ACG::SceneGraph::BaseNode *_node)
Get a widget for this Node.
Definition: LightObject.cc:76
ACG::SceneGraph::ACG::SceneGraph::BaseNode::setMultipassStatus
void setMultipassStatus(const MultipassBitMask _passStatus)
Set multipass settings for the nodes status functions.
Definition: MeshNode2T_impl.hh:505
BaseObject::dataType
DataType dataType() const
Definition: BaseObject.cc:229
ACG::SceneGraph::LightNode::setLightSource
void setLightSource(LightSource _light)
Set the light source parameters.
Definition: LightNode.hh:262
BaseObjectData::hide
virtual void hide()
Sets the whole Scenegraph subtree of this node to invisible.
Definition: BaseObjectData.cc:197
LightObject::lightNode_
LightNode * lightNode_
Light status node.
Definition: LightObject.hh:172
LightObject::lightNodeVis_
LightNode * lightNodeVis_
Light rendering node (only for rendering purposes)
Definition: LightObject.hh:175