OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
StateSet.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
2  *
3  * This library is open source and may be redistributed and/or modified under
4  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5  * (at your option) any later version. The full license is in LICENSE file
6  * included with this distribution, and on the openscenegraph.org website.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * OpenSceneGraph Public License for more details.
12 */
13 
14 #ifndef OSG_STATESET
15 #define OSG_STATESET 1
16 
17 #include <osg/Object>
18 #include <osg/StateAttribute>
19 #include <osg/ref_ptr>
20 #include <osg/Uniform>
21 
22 #include <map>
23 #include <vector>
24 #include <string>
25 
26 #ifndef GL_RESCALE_NORMAL
27 // allow compilation against GL1.1 headers.
28 #define GL_RESCALE_NORMAL 0x803A
29 #endif
30 
31 namespace osg {
32 
33 // forward declare for the purposes of the UpdateCallback.
34 class NodeVisitor;
35 
45 class OSG_EXPORT StateSet : public Object
46 {
47  public :
48 
49 
50  StateSet();
51  StateSet(const StateSet&,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
52 
53  virtual Object* cloneType() const { return new StateSet(); }
54  virtual Object* clone(const CopyOp& copyop) const { return new StateSet(*this,copyop); }
55  virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const StateSet*>(obj)!=NULL; }
56  virtual const char* libraryName() const { return "osg"; }
57  virtual const char* className() const { return "StateSet"; }
58 
60  int compare(const StateSet& rhs,bool compareAttributeContents=false) const;
61 
62  bool operator < (const StateSet& rhs) const { return compare(rhs)<0; }
63  bool operator == (const StateSet& rhs) const { return compare(rhs)==0; }
64  bool operator != (const StateSet& rhs) const { return compare(rhs)!=0; }
65 
66 
68  typedef std::vector<Node*> ParentList;
69 
71  inline const ParentList& getParents() const { return _parents; }
72 
75  inline ParentList getParents() { return _parents; }
76 
77  inline Node* getParent(unsigned int i) { return _parents[i]; }
83  inline const Node* getParent(unsigned int i) const { return _parents[i]; }
84 
89  inline unsigned int getNumParents() const { return static_cast<unsigned int>(_parents.size()); }
90 
91 
93  virtual void computeDataVariance();
94 
95 
98  void setGlobalDefaults();
99 
101  void clear();
102 
108  void merge(const StateSet& rhs);
109 
111  typedef std::map<StateAttribute::GLMode,StateAttribute::GLModeValue> ModeList;
112 
119  void setMode(StateAttribute::GLMode mode, StateAttribute::GLModeValue value);
120 
127  void removeMode(StateAttribute::GLMode mode);
128 
140 
142  inline void setModeList(ModeList& ml) { _modeList=ml; }
143 
145  inline ModeList& getModeList() { return _modeList; }
146 
150  inline const ModeList& getModeList() const { return _modeList; }
151 
152 
153 
155  typedef std::pair<ref_ptr<StateAttribute>,StateAttribute::OverrideValue> RefAttributePair;
156 
158  typedef std::map<StateAttribute::TypeMemberPair,RefAttributePair> AttributeList;
159 
161  void setAttribute(StateAttribute *attribute, StateAttribute::OverrideValue value=StateAttribute::OFF);
162 
164  void setAttributeAndModes(StateAttribute *attribute, StateAttribute::GLModeValue value=StateAttribute::ON);
165 
167  void removeAttribute(StateAttribute::Type type, unsigned int member=0);
168 
170  void removeAttribute(StateAttribute *attribute);
171 
174  StateAttribute* getAttribute(StateAttribute::Type type, unsigned int member = 0);
175 
178  const StateAttribute* getAttribute(StateAttribute::Type type, unsigned int member = 0) const;
179 
182  const RefAttributePair* getAttributePair(StateAttribute::Type type, unsigned int member = 0) const;
183 
185  inline void setAttributeList(AttributeList& al) { _attributeList=al; }
186 
188  inline AttributeList& getAttributeList() { return _attributeList; }
189 
191  inline const AttributeList& getAttributeList() const { return _attributeList; }
192 
193 
194 
195  typedef std::vector<ModeList> TextureModeList;
196 
204  void setTextureMode(unsigned int unit,StateAttribute::GLMode mode, StateAttribute::GLModeValue value);
205 
207  void removeTextureMode(unsigned int unit,StateAttribute::GLMode mode);
208 
211  StateAttribute::GLModeValue getTextureMode(unsigned int unit,StateAttribute::GLMode mode) const;
212 
214  inline void setTextureModeList(TextureModeList& tml) { _textureModeList=tml; }
215 
217  inline TextureModeList& getTextureModeList() { return _textureModeList; }
218 
220  inline const TextureModeList& getTextureModeList() const { return _textureModeList; }
221 
223  inline unsigned int getNumTextureModeLists() const { return static_cast<unsigned int>(_textureModeList.size()); }
224 
225  typedef std::vector<AttributeList> TextureAttributeList;
226 
228  void setTextureAttribute(unsigned int unit,StateAttribute *attribute, StateAttribute::OverrideValue value=StateAttribute::OFF);
230  void setTextureAttributeAndModes(unsigned int unit,StateAttribute *attribute, StateAttribute::GLModeValue value=StateAttribute::ON);
231 
233  void removeTextureAttribute(unsigned int unit, StateAttribute::Type type);
234 
236  void removeTextureAttribute(unsigned int unit, StateAttribute *attribute);
237 
240  StateAttribute* getTextureAttribute(unsigned int unit,StateAttribute::Type type);
241 
244  const StateAttribute* getTextureAttribute(unsigned int unit,StateAttribute::Type type) const;
245 
248  const RefAttributePair* getTextureAttributePair(unsigned int unit,StateAttribute::Type type) const;
249 
251  inline void setTextureAttributeList(TextureAttributeList& tal) { _textureAttributeList=tal; }
252 
254  inline TextureAttributeList& getTextureAttributeList() { return _textureAttributeList; }
255 
257  inline const TextureAttributeList& getTextureAttributeList() const { return _textureAttributeList; }
258 
260  inline unsigned int getNumTextureAttributeLists() const { return static_cast<unsigned int>(_textureAttributeList.size()); }
261 
262 
263  void setAssociatedModes(const StateAttribute* attribute, StateAttribute::GLModeValue value);
264  void removeAssociatedModes(const StateAttribute* attribute);
265 
266  void setAssociatedTextureModes(unsigned int unit,const StateAttribute* attribute, StateAttribute::GLModeValue value);
267  void removeAssociatedTextureModes(unsigned int unit,const StateAttribute* attribute);
268 
269 
270 
271 
273  typedef std::pair<ref_ptr<Uniform>,StateAttribute::OverrideValue> RefUniformPair;
274 
276  typedef std::map<std::string,RefUniformPair> UniformList;
277 
279  void addUniform(Uniform* uniform, StateAttribute::OverrideValue value=StateAttribute::ON);
280 
282  void removeUniform(const std::string& name);
283 
285  void removeUniform(Uniform* uniform);
286 
289  Uniform* getUniform(const std::string& name);
290 
292  Uniform* getOrCreateUniform(const std::string& name, Uniform::Type type, unsigned int numElements=1);
293 
296  const Uniform* getUniform(const std::string& name) const;
297 
300  const RefUniformPair* getUniformPair(const std::string& name) const;
301 
303  inline void setUniformList(UniformList& al) { _uniformList=al; }
304 
306  inline UniformList& getUniformList() { return _uniformList; }
307 
309  inline const UniformList& getUniformList() const { return _uniformList; }
310 
311 
312  typedef std::pair<std::string, StateAttribute::OverrideValue> DefinePair;
313  typedef std::map<std::string, DefinePair> DefineList;
314 
316  void setDefine(const std::string& defineName, StateAttribute::OverrideValue value=StateAttribute::ON);
317 
319  void setDefine(const std::string& defineName, const std::string& defineValue, StateAttribute::OverrideValue value=StateAttribute::ON);
320 
321  DefinePair* getDefinePair(const std::string& defineName) { DefineList::iterator itr = _defineList.find(defineName); return (itr!=_defineList.end()) ? &(itr->second) : 0; }
322  const DefinePair* getDefinePair(const std::string& defineName) const { DefineList::const_iterator itr = _defineList.find(defineName); return (itr!=_defineList.end()) ? &(itr->second) : 0; }
323 
324 
326  void removeDefine(const std::string& defineName);
327 
328 
330  void setDefineList(const DefineList& dl) { _defineList = dl; }
331 
333  DefineList& getDefineList() { return _defineList; }
334 
336  const DefineList& getDefineList() const { return _defineList; }
337 
338 
339 
341  {
342  DEFAULT_BIN = 0,
343  OPAQUE_BIN = 1,
344  TRANSPARENT_BIN = 2
345  };
346 
358  void setRenderingHint(int hint);
359 
361  inline int getRenderingHint() const { return _renderingHint; }
362 
364  {
365  INHERIT_RENDERBIN_DETAILS =0,
366  USE_RENDERBIN_DETAILS =1,
367  OVERRIDE_RENDERBIN_DETAILS =2,
368  PROTECTED_RENDERBIN_DETAILS =4,
369  OVERRIDE_PROTECTED_RENDERBIN_DETAILS = OVERRIDE_RENDERBIN_DETAILS|PROTECTED_RENDERBIN_DETAILS
370  };
371 
373  void setRenderBinDetails(int binNum,const std::string& binName,RenderBinMode mode=USE_RENDERBIN_DETAILS);
374 
376  void setRenderBinToInherit();
377 
379  inline bool useRenderBinDetails() const { return _binMode!=INHERIT_RENDERBIN_DETAILS; }
380 
382  inline void setRenderBinMode(RenderBinMode mode) { _binMode=mode; }
383 
385  inline RenderBinMode getRenderBinMode() const { return _binMode; }
386 
388  inline void setBinNumber(int num) { _binNum=num; }
389 
391  inline int getBinNumber() const { return _binNum; }
392 
394  inline void setBinName(const std::string& name) { _binName=name; }
395 
397  inline const std::string& getBinName() const { return _binName; }
398 
406  inline void setNestRenderBins(bool val) { _nestRenderBins = val; }
407 
409  inline bool getNestRenderBins() const { return _nestRenderBins; }
410 
411 
412  struct OSG_EXPORT Callback : public virtual osg::Callback
413  {
414  Callback() {}
415 
416  Callback(const Callback&,const CopyOp&) {}
417 
419 
421  virtual bool run(osg::Object* object, osg::Object* data);
422 
424  virtual void operator() (StateSet*, NodeVisitor*) {}
425  };
426 
428  void setUpdateCallback(Callback* ac);
429 
431  Callback* getUpdateCallback() { return _updateCallback.get(); }
432 
434  const Callback* getUpdateCallback() const { return _updateCallback.get(); }
435 
437  bool requiresUpdateTraversal() const { return _updateCallback.valid() || getNumChildrenRequiringUpdateTraversal()!=0; }
438 
441  inline unsigned int getNumChildrenRequiringUpdateTraversal() const { return _numChildrenRequiringUpdateTraversal; }
442 
444  void runUpdateCallbacks(osg::NodeVisitor* nv);
445 
446 
448  void setEventCallback(Callback* ac);
449 
451  Callback* getEventCallback() { return _eventCallback.get(); }
452 
454  const Callback* getEventCallback() const { return _eventCallback.get(); }
455 
457  bool requiresEventTraversal() const { return _eventCallback.valid() || getNumChildrenRequiringEventTraversal()!=0; }
458 
461  inline unsigned int getNumChildrenRequiringEventTraversal() const { return _numChildrenRequiringEventTraversal; }
462 
464  void runEventCallbacks(osg::NodeVisitor* nv);
465 
469  bool checkValidityOfAssociatedModes(State& state) const;
470 
472  virtual void setThreadSafeRefUnref(bool threadSafe);
473 
475  void compileGLObjects(State& state) const;
476 
478  virtual void resizeGLObjectBuffers(unsigned int maxSize);
479 
481  virtual void releaseGLObjects(State* state=0) const;
482 
483  protected :
484 
485 
486  virtual ~StateSet();
487 
488  StateSet& operator = (const StateSet&) { return *this; }
489 
490  void addParent(osg::Node* object);
491  void removeParent(osg::Node* object);
492 
493  ParentList _parents;
494  friend class osg::Node;
495  friend class osg::Drawable;
496  friend class osg::Uniform;
497  friend class osg::StateAttribute;
498 
499  ModeList _modeList;
500  AttributeList _attributeList;
501 
502  TextureModeList _textureModeList;
503  TextureAttributeList _textureAttributeList;
504 
505  UniformList _uniformList;
506  DefineList _defineList;
507 
508  inline ModeList& getOrCreateTextureModeList(unsigned int unit)
509  {
510  if (unit>=_textureModeList.size()) _textureModeList.resize(unit+1);
511  return _textureModeList[unit];
512  }
513 
514  inline AttributeList& getOrCreateTextureAttributeList(unsigned int unit)
515  {
516  if (unit>=_textureAttributeList.size()) _textureAttributeList.resize(unit+1);
517  return _textureAttributeList[unit];
518  }
519 
520  int compareModes(const ModeList& lhs,const ModeList& rhs);
521  int compareAttributePtrs(const AttributeList& lhs,const AttributeList& rhs);
522  int compareAttributeContents(const AttributeList& lhs,const AttributeList& rhs);
523 
524  void setMode(ModeList& modeList,StateAttribute::GLMode mode, StateAttribute::GLModeValue value);
525  void setModeToInherit(ModeList& modeList,StateAttribute::GLMode mode);
526  StateAttribute::GLModeValue getMode(const ModeList& modeList,StateAttribute::GLMode mode) const;
527 
528  void setAttribute(AttributeList& attributeList,StateAttribute *attribute, const StateAttribute::OverrideValue value=StateAttribute::OFF);
529 
530  StateAttribute* getAttribute(AttributeList& attributeList,const StateAttribute::Type type, unsigned int member);
531  const StateAttribute* getAttribute(const AttributeList& attributeList,const StateAttribute::Type type, unsigned int member) const;
532  const RefAttributePair* getAttributePair(const AttributeList& attributeList,const StateAttribute::Type type, unsigned int member) const;
533 
535 
537  int _binNum;
538  std::string _binName;
540 
543  void setNumChildrenRequiringUpdateTraversal(unsigned int num);
544 
547  void setNumChildrenRequiringEventTraversal(unsigned int num);
548 
549 };
550 
552 
553 }
554 
555 #endif
const ModeList & getModeList() const
Definition: StateSet.h:150
void setBinNumber(int num)
Definition: StateSet.h:388
#define OSG_EXPORT
Definition: Export.h:43
const Callback * getEventCallback() const
Definition: StateSet.h:454
const TextureModeList & getTextureModeList() const
Definition: StateSet.h:220
bool _nestRenderBins
Definition: StateSet.h:539
unsigned int _numChildrenRequiringUpdateTraversal
Definition: StateSet.h:542
void setModeList(ModeList &ml)
Definition: StateSet.h:142
const Node * getParent(unsigned int i) const
Definition: StateSet.h:83
std::vector< Node * > ParentList
Definition: StateSet.h:68
unsigned int getNumParents() const
Definition: StateSet.h:89
#define NULL
Definition: Export.h:59
unsigned int _numChildrenRequiringEventTraversal
Definition: StateSet.h:546
std::string _binName
Definition: StateSet.h:538
RenderBinMode getRenderBinMode() const
Definition: StateSet.h:385
Callback * getUpdateCallback()
Definition: StateSet.h:431
TextureAttributeList & getTextureAttributeList()
Definition: StateSet.h:254
virtual const char * libraryName() const
Definition: StateSet.h:56
void setRenderBinMode(RenderBinMode mode)
Definition: StateSet.h:382
Node * getParent(unsigned int i)
Definition: StateSet.h:77
unsigned int getNumChildrenRequiringUpdateTraversal() const
Definition: StateSet.h:441
ModeList & getModeList()
Definition: StateSet.h:145
ParentList _parents
Definition: StateSet.h:493
const DefinePair * getDefinePair(const std::string &defineName) const
Definition: StateSet.h:322
int _renderingHint
Definition: StateSet.h:534
virtual Object * cloneType() const
Definition: StateSet.h:53
AttributeList _attributeList
Definition: StateSet.h:500
TextureModeList & getTextureModeList()
Definition: StateSet.h:217
const ParentList & getParents() const
Definition: StateSet.h:71
DefinePair * getDefinePair(const std::string &defineName)
Definition: StateSet.h:321
std::pair< ref_ptr< StateAttribute >, StateAttribute::OverrideValue > RefAttributePair
Definition: StateSet.h:155
std::pair< ref_ptr< Uniform >, StateAttribute::OverrideValue > RefUniformPair
Definition: StateSet.h:273
std::map< std::string, DefinePair > DefineList
Definition: StateSet.h:313
std::map< StateAttribute::GLMode, StateAttribute::GLModeValue > ModeList
Definition: StateSet.h:111
Callback * getEventCallback()
Definition: StateSet.h:451
const AttributeList & getAttributeList() const
Definition: StateSet.h:191
bool requiresEventTraversal() const
Definition: StateSet.h:457
RenderBinMode _binMode
Definition: StateSet.h:536
unsigned int OverrideValue
std::map< StateAttribute::TypeMemberPair, RefAttributePair > AttributeList
Definition: StateSet.h:158
#define META_Object(library, name)
Definition: Object.h:42
std::vector< ModeList > TextureModeList
Definition: StateSet.h:195
void setDefineList(const DefineList &dl)
Definition: StateSet.h:330
const std::string & getBinName() const
Definition: StateSet.h:397
ParentList getParents()
Definition: StateSet.h:75
ModeList _modeList
Definition: StateSet.h:499
DefineList & getDefineList()
Definition: StateSet.h:333
unsigned int GLModeValue
bool getNestRenderBins() const
Definition: StateSet.h:409
ModeList & getOrCreateTextureModeList(unsigned int unit)
Definition: StateSet.h:508
void setTextureModeList(TextureModeList &tml)
Definition: StateSet.h:214
bool useRenderBinDetails() const
Definition: StateSet.h:379
unsigned int getNumChildrenRequiringEventTraversal() const
Definition: StateSet.h:461
TextureAttributeList _textureAttributeList
Definition: StateSet.h:503
UniformList & getUniformList()
Definition: StateSet.h:306
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum type
Definition: GLU.h:71
void setUniformList(UniformList &al)
Definition: StateSet.h:303
virtual const char * className() const
Definition: StateSet.h:57
void setBinName(const std::string &name)
Definition: StateSet.h:394
const DefineList & getDefineList() const
Definition: StateSet.h:336
virtual Object * clone(const CopyOp &copyop) const
Definition: StateSet.h:54
void setNestRenderBins(bool val)
Definition: StateSet.h:406
int getRenderingHint() const
Definition: StateSet.h:361
AttributeList & getAttributeList()
Definition: StateSet.h:188
int getBinNumber() const
Definition: StateSet.h:391
const UniformList & getUniformList() const
Definition: StateSet.h:309
TextureModeList _textureModeList
Definition: StateSet.h:502
Definition: Node.h:71
OSG_EXPORT bool isTextureMode(StateAttribute::GLMode mode)
std::vector< AttributeList > TextureAttributeList
Definition: StateSet.h:225
Callback(const Callback &, const CopyOp &)
Definition: StateSet.h:416
Definition: AlphaFunc.h:19
std::map< std::string, RefUniformPair > UniformList
Definition: StateSet.h:276
AttributeList & getOrCreateTextureAttributeList(unsigned int unit)
Definition: StateSet.h:514
virtual bool isSameKindAs(const Object *obj) const
Definition: StateSet.h:55
std::pair< std::string, StateAttribute::OverrideValue > DefinePair
Definition: StateSet.h:312
DefineList _defineList
Definition: StateSet.h:506
unsigned int getNumTextureAttributeLists() const
Definition: StateSet.h:260
bool requiresUpdateTraversal() const
Definition: StateSet.h:437
ref_ptr< Callback > _updateCallback
Definition: StateSet.h:541
void setAttributeList(AttributeList &al)
Definition: StateSet.h:185
unsigned int getNumTextureModeLists() const
Definition: StateSet.h:223
UniformList _uniformList
Definition: StateSet.h:505
ref_ptr< Callback > _eventCallback
Definition: StateSet.h:545
const Callback * getUpdateCallback() const
Definition: StateSet.h:434
const TextureAttributeList & getTextureAttributeList() const
Definition: StateSet.h:257
void setTextureAttributeList(TextureAttributeList &tal)
Definition: StateSet.h:251