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