StateSet 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. #ifndef OSG_STATESET
  14. #define OSG_STATESET 1
  15. #include <osg/Object>
  16. #include <osg/StateAttribute>
  17. #include <osg/ref_ptr>
  18. #include <osg/Uniform>
  19. #include <map>
  20. #include <vector>
  21. #include <string>
  22. #ifndef GL_RESCALE_NORMAL
  23. // allow compilation against GL1.1 headers.
  24. #define GL_RESCALE_NORMAL 0x803A
  25. #endif
  26. namespace osg {
  27. // forward declare for the purposes of the UpdateCallback.
  28. class NodeVisitor;
  29. /** Stores a set of modes and attributes which represent a set of OpenGL state.
  30. * Notice that a \c StateSet contains just a subset of the whole OpenGL state.
  31. * <p>In OSG, each \c Drawable and each \c Node has a reference to a
  32. * \c StateSet. These <tt>StateSet</tt>s can be shared between
  33. * different <tt>Drawable</tt>s and <tt>Node</tt>s (that is, several
  34. * <tt>Drawable</tt>s and <tt>Node</tt>s can reference the same \c StateSet).
  35. * Indeed, this practice is recommended whenever possible,
  36. * as this minimizes expensive state changes in the graphics pipeline.
  37. */
  38. class OSG_EXPORT StateSet : public Object
  39. {
  40. public :
  41. StateSet();
  42. StateSet(const StateSet&,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  43. virtual Object* cloneType() const { return new StateSet(); }
  44. virtual Object* clone(const CopyOp& copyop) const { return new StateSet(*this,copyop); }
  45. virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const StateSet*>(obj)!=NULL; }
  46. virtual const char* libraryName() const { return "osg"; }
  47. virtual const char* className() const { return "StateSet"; }
  48. /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
  49. int compare(const StateSet& rhs,bool compareAttributeContents=false) const;
  50. bool operator < (const StateSet& rhs) const { return compare(rhs)<0; }
  51. bool operator == (const StateSet& rhs) const { return compare(rhs)==0; }
  52. bool operator != (const StateSet& rhs) const { return compare(rhs)!=0; }
  53. /** Convert 'this' into a StateSet pointer if Object is a StateSet, otherwise return 0.
  54. * Equivalent to dynamic_cast<StateSet*>(this).*/
  55. virtual StateSet* asStateSet() { return this; }
  56. /** convert 'const this' into a const StateSet pointer if Object is a StateSet, otherwise return 0.
  57. * Equivalent to dynamic_cast<const StateSet*>(this).*/
  58. virtual const StateSet* asStateSet() const { return this; }
  59. /** A vector of osg::Object pointers which is used to store the parent(s) of this Stateset, the parents could be osg::Node or osg::Drawable.*/
  60. typedef std::vector<Node*> ParentList;
  61. /** Get the parent list of this StateSet. */
  62. inline const ParentList& getParents() const { return _parents; }
  63. /** Get the a copy of parent list of node. A copy is returned to
  64. * prevent modification of the parent list.*/
  65. inline ParentList getParents() { return _parents; }
  66. inline Node* getParent(unsigned int i) { return _parents[i]; }
  67. /**
  68. * Get a single const parent of this StateSet.
  69. * @param i index of the parent to get.
  70. * @return the parent i.
  71. */
  72. inline const Node* getParent(unsigned int i) const { return _parents[i]; }
  73. /**
  74. * Get the number of parents of this StateSet.
  75. * @return the number of parents of this StateSet.
  76. */
  77. inline unsigned int getNumParents() const { return static_cast<unsigned int>(_parents.size()); }
  78. /** Compute the DataVariance based on an assessment of callback etc.*/
  79. virtual void computeDataVariance();
  80. /** Set all the modes to on or off so that it defines a
  81. complete state, typically used for a default global state.*/
  82. void setGlobalDefaults();
  83. /** Clear the StateSet of all modes and attributes.*/
  84. void clear();
  85. /** Merge this \c StateSet with the \c StateSet passed as parameter.
  86. * Every mode and attribute in this \c StateSet that is marked with
  87. * \c StateAttribute::OVERRIDE is replaced with the
  88. * equivalent mode or attribute from \c rhs.
  89. */
  90. void merge(const StateSet& rhs);
  91. /** a container to map GLModes to their respective GLModeValues.*/
  92. typedef std::map<StateAttribute::GLMode,StateAttribute::GLModeValue> ModeList;
  93. /** Set this \c StateSet to contain the specified \c GLMode with a given
  94. * value.
  95. * @note Don't use this method to set modes related to textures. For this
  96. * purpose, use \c setTextureMode(), that accepts an extra parameter
  97. * specifying which texture unit shall be affected by the call.
  98. */
  99. void setMode(StateAttribute::GLMode mode, StateAttribute::GLModeValue value);
  100. /** Remove \c mode from this \c StateSet.
  101. * @note Don't use this method to remove modes related to textures. For
  102. * this purpose, use \c removeTextureMode(), that accepts an extra
  103. * parameter specifying which texture unit shall be affected by
  104. * the call.
  105. */
  106. void removeMode(StateAttribute::GLMode mode);
  107. /** Get the value for a given \c GLMode.
  108. * @param mode The \c GLMode whose value is desired.
  109. * @return If \c mode is contained within this \c StateSet, returns the
  110. * value associated with it. Otherwise, returns
  111. * \c StateAttribute::INHERIT.
  112. * @note Don't use this method to get the value of modes related to
  113. * textures. For this purpose, use \c removeTextureMode(), that
  114. * accepts an extra parameter specifying which texture unit shall
  115. * be affected by the call.
  116. */
  117. StateAttribute::GLModeValue getMode(StateAttribute::GLMode mode) const;
  118. /** Set the list of all <tt>GLMode</tt>s contained in this \c StateSet.*/
  119. inline void setModeList(ModeList& ml) { _modeList=ml; }
  120. /** Return the list of all <tt>GLMode</tt>s contained in this \c StateSet.*/
  121. inline ModeList& getModeList() { return _modeList; }
  122. /** Return the \c const list of all <tt>GLMode</tt>s contained in this
  123. * <tt>const StateSet</tt>.
  124. */
  125. inline const ModeList& getModeList() const { return _modeList; }
  126. /** Simple pairing between an attribute and its override flag.*/
  127. typedef std::pair<ref_ptr<StateAttribute>,StateAttribute::OverrideValue> RefAttributePair;
  128. /** a container to map <StateAttribyte::Types,Member> to their respective RefAttributePair.*/
  129. typedef std::map<StateAttribute::TypeMemberPair,RefAttributePair> AttributeList;
  130. /** Set this StateSet to contain specified attribute and override flag.*/
  131. void setAttribute(StateAttribute *attribute, StateAttribute::OverrideValue value=StateAttribute::OFF);
  132. template<class T> void setAttribute(const ref_ptr<T>& attribute, StateAttribute::OverrideValue value=StateAttribute::OFF) { setAttribute(attribute.get(), value); }
  133. /** Set this StateSet to contain specified attribute and set the associated GLMode's to specified value.*/
  134. void setAttributeAndModes(StateAttribute *attribute, StateAttribute::GLModeValue value=StateAttribute::ON);
  135. template<class T> void setAttributeAndModes(const ref_ptr<T>& attribute, StateAttribute::GLModeValue value=StateAttribute::ON) { setAttributeAndModes(attribute.get(), value); }
  136. /** remove attribute of specified type from StateSet.*/
  137. void removeAttribute(StateAttribute::Type type, unsigned int member=0);
  138. /** remove attribute from StateSet.*/
  139. void removeAttribute(StateAttribute *attribute);
  140. template<class T> void removeAttribute(const ref_ptr<T>& attribute) { removeAttribute(attribute.get()); }
  141. /** Get specified StateAttribute for specified type.
  142. * Returns NULL if no type is contained within StateSet.*/
  143. StateAttribute* getAttribute(StateAttribute::Type type, unsigned int member = 0);
  144. /** Get specified const StateAttribute for specified type.
  145. * Returns NULL if no type is contained within const StateSet.*/
  146. const StateAttribute* getAttribute(StateAttribute::Type type, unsigned int member = 0) const;
  147. /** Get specified RefAttributePair for specified type.
  148. * Returns NULL if no type is contained within StateSet.*/
  149. RefAttributePair* getAttributePair(StateAttribute::Type type, unsigned int member = 0);
  150. /** Get specified RefAttributePair for specified type.
  151. * Returns NULL if no type is contained within StateSet.*/
  152. const RefAttributePair* getAttributePair(StateAttribute::Type type, unsigned int member = 0) const;
  153. /** set the list of all StateAttributes contained in this StateSet.*/
  154. inline void setAttributeList(AttributeList& al) { _attributeList=al; }
  155. /** return the list of all StateAttributes contained in this StateSet.*/
  156. inline AttributeList& getAttributeList() { return _attributeList; }
  157. /** return the const list of all StateAttributes contained in this const StateSet.*/
  158. inline const AttributeList& getAttributeList() const { return _attributeList; }
  159. typedef std::vector<ModeList> TextureModeList;
  160. /** Set this \c StateSet to contain specified \c GLMode with a given
  161. * value.
  162. * @param unit The texture unit to be affected (used with
  163. * multi-texturing).
  164. * @param mode The OpenGL mode to be added to the \c StateSet.
  165. * @param value The value to be assigned to \c mode.
  166. */
  167. void setTextureMode(unsigned int unit,StateAttribute::GLMode mode, StateAttribute::GLModeValue value);
  168. /** Remove texture mode from StateSet.*/
  169. void removeTextureMode(unsigned int unit,StateAttribute::GLMode mode);
  170. /** Get specified GLModeValue for specified GLMode.
  171. * returns INHERIT if no GLModeValue is contained within StateSet.*/
  172. StateAttribute::GLModeValue getTextureMode(unsigned int unit,StateAttribute::GLMode mode) const;
  173. /** set the list of all Texture related GLModes contained in this StateSet.*/
  174. inline void setTextureModeList(TextureModeList& tml) { _textureModeList=tml; }
  175. /** return the list of all Texture related GLModes contained in this StateSet.*/
  176. inline TextureModeList& getTextureModeList() { return _textureModeList; }
  177. /** return the const list of all Texture related GLModes contained in this const StateSet.*/
  178. inline const TextureModeList& getTextureModeList() const { return _textureModeList; }
  179. /** Return the number texture units active in the TextureModeList.*/
  180. inline unsigned int getNumTextureModeLists() const { return static_cast<unsigned int>(_textureModeList.size()); }
  181. typedef std::vector<AttributeList> TextureAttributeList;
  182. /** Set this StateSet to contain specified attribute and override flag.*/
  183. void setTextureAttribute(unsigned int unit,StateAttribute *attribute, StateAttribute::OverrideValue value=StateAttribute::OFF);
  184. template<class T> void setTextureAttribute(unsigned int unit, const ref_ptr<T>& attribute, StateAttribute::OverrideValue value=StateAttribute::OFF) { setTextureAttribute( unit, attribute.get(), value); }
  185. /** Set this StateSet to contain specified attribute and set the associated GLMode's to specified value.*/
  186. void setTextureAttributeAndModes(unsigned int unit,StateAttribute *attribute, StateAttribute::GLModeValue value=StateAttribute::ON);
  187. template<class T> void setTextureAttributeAndModes(unsigned int unit, const ref_ptr<T>& attribute, StateAttribute::OverrideValue value=StateAttribute::ON) { setTextureAttributeAndModes( unit, attribute.get(), value); }
  188. /** remove texture attribute of specified type from StateSet.*/
  189. void removeTextureAttribute(unsigned int unit, StateAttribute::Type type);
  190. /** remove texture attribute from StateSet.*/
  191. void removeTextureAttribute(unsigned int unit, StateAttribute *attribute);
  192. template<class T> void removeTextureAttribute(unsigned int unit, const ref_ptr<T>& attribute) { removeTextureAttribute(unit, attribute.get()); }
  193. /** Get specified Texture related StateAttribute for specified type.
  194. * Returns NULL if no type is contained within StateSet.*/
  195. StateAttribute* getTextureAttribute(unsigned int unit, StateAttribute::Type type);
  196. /** Get specified Texture related const StateAttribute for specified type.
  197. * Returns NULL if no type is contained within const StateSet.*/
  198. const StateAttribute* getTextureAttribute(unsigned int unit, StateAttribute::Type type) const;
  199. /** Get specified Texture related RefAttributePair for specified type.
  200. * Returns NULL if no type is contained within StateSet.*/
  201. RefAttributePair* getTextureAttributePair(unsigned int unit, StateAttribute::Type type);
  202. /** Get specified Texture related RefAttributePair for specified type.
  203. * Returns NULL if no type is contained within StateSet.*/
  204. const RefAttributePair* getTextureAttributePair(unsigned int unit, StateAttribute::Type type) const;
  205. /** Set the list of all Texture related StateAttributes contained in this StateSet.*/
  206. inline void setTextureAttributeList(TextureAttributeList& tal) { _textureAttributeList=tal; }
  207. /** Return the list of all Texture related StateAttributes contained in this StateSet.*/
  208. inline TextureAttributeList& getTextureAttributeList() { return _textureAttributeList; }
  209. /** Return the const list of all Texture related StateAttributes contained in this const StateSet.*/
  210. inline const TextureAttributeList& getTextureAttributeList() const { return _textureAttributeList; }
  211. /** Return the number of texture units active in the TextureAttributeList.*/
  212. inline unsigned int getNumTextureAttributeLists() const { return static_cast<unsigned int>(_textureAttributeList.size()); }
  213. void setAssociatedModes(const StateAttribute* attribute, StateAttribute::GLModeValue value);
  214. void removeAssociatedModes(const StateAttribute* attribute);
  215. void setAssociatedTextureModes(unsigned int unit, const StateAttribute* attribute, StateAttribute::GLModeValue value);
  216. void removeAssociatedTextureModes(unsigned int unit, const StateAttribute* attribute);
  217. /** Simple pairing between a Uniform and its override flag.*/
  218. typedef std::pair<ref_ptr<Uniform>,StateAttribute::OverrideValue> RefUniformPair;
  219. /** a container to map Uniform name to its respective RefUniformPair.*/
  220. typedef std::map<std::string,RefUniformPair> UniformList;
  221. /** Set this StateSet to contain specified uniform and override flag.*/
  222. void addUniform(Uniform* uniform, StateAttribute::OverrideValue value=StateAttribute::ON);
  223. template<class T> void addUniform(const ref_ptr<T>& uniform, StateAttribute::OverrideValue value=StateAttribute::ON) { addUniform( uniform.get(), value); }
  224. /** remove uniform of specified name from StateSet.*/
  225. void removeUniform(const std::string& name);
  226. /** remove Uniform from StateSet.*/
  227. void removeUniform(Uniform* uniform);
  228. template<class T> void removeUniform(const ref_ptr<T>& uniform) { removeUniform(uniform.get()); }
  229. /** Get Uniform for specified name.
  230. * Returns NULL if no matching Uniform is contained within StateSet.*/
  231. Uniform* getUniform(const std::string& name);
  232. /** Get Uniform for specified name, if one is not available create it, add it to this StateSet and return a pointer to it.*/
  233. Uniform* getOrCreateUniform(const std::string& name, Uniform::Type type, unsigned int numElements=1);
  234. /** Get const Uniform for specified name.
  235. * Returns NULL if no matching Uniform is contained within StateSet.*/
  236. const Uniform* getUniform(const std::string& name) const;
  237. /** Get specified RefUniformPair for specified Uniform name.
  238. * Returns NULL if no Uniform is contained within StateSet.*/
  239. const RefUniformPair* getUniformPair(const std::string& name) const;
  240. /** set the list of all Uniforms contained in this StateSet.*/
  241. inline void setUniformList(UniformList& al) { _uniformList=al; }
  242. /** return the list of all Uniforms contained in this StateSet.*/
  243. inline UniformList& getUniformList() { return _uniformList; }
  244. /** return the const list of all Uniforms contained in this const StateSet.*/
  245. inline const UniformList& getUniformList() const { return _uniformList; }
  246. typedef std::pair<std::string, StateAttribute::OverrideValue> DefinePair;
  247. typedef std::map<std::string, DefinePair> DefineList;
  248. /** Added define pass on to shaders that use utilize that define, as specified by the GLSL \#pragma import_defines(..) and \#pragma requires(..). */
  249. void setDefine(const std::string& defineName, StateAttribute::OverrideValue value=StateAttribute::ON);
  250. /** Added define with value to pass on to shaders that use utilize that define, as specified by the GLSL \#pragma import_defines(..) and \#pragma requires(..). */
  251. void setDefine(const std::string& defineName, const std::string& defineValue, StateAttribute::OverrideValue value=StateAttribute::ON);
  252. DefinePair* getDefinePair(const std::string& defineName) { DefineList::iterator itr = _defineList.find(defineName); return (itr!=_defineList.end()) ? &(itr->second) : 0; }
  253. const DefinePair* getDefinePair(const std::string& defineName) const { DefineList::const_iterator itr = _defineList.find(defineName); return (itr!=_defineList.end()) ? &(itr->second) : 0; }
  254. /** Remove define*/
  255. void removeDefine(const std::string& defineName);
  256. /** Set the list of defines to pass on to shaders.*/
  257. void setDefineList(const DefineList& dl) { _defineList = dl; }
  258. /** Get the list of defines to pass on to shaders.*/
  259. DefineList& getDefineList() { return _defineList; }
  260. /** Get the const list of defines to pass on to shaders.*/
  261. const DefineList& getDefineList() const { return _defineList; }
  262. enum RenderingHint
  263. {
  264. DEFAULT_BIN = 0,
  265. OPAQUE_BIN = 1,
  266. TRANSPARENT_BIN = 2
  267. };
  268. /** Set the \c RenderingHint of this \c StateSet. \c RenderingHint is
  269. * used by the renderer to determine which draw bin to drop associated
  270. * <tt>osg::Drawable</tt>s in. Typically, users will set this to either
  271. * \c StateSet::OPAQUE_BIN or \c StateSet::TRANSPARENT_BIN.
  272. * <tt>Drawable</tt>s in the opaque bin are sorted by their
  273. * \c StateSet, so that the number of expensive changes in the OpenGL
  274. * state is minimized. <tt>Drawable</tt>s in the transparent bin are
  275. * sorted by depth, so that objects farther from the viewer are
  276. * rendered first (and hence alpha blending works nicely for
  277. * translucent objects).
  278. */
  279. void setRenderingHint(int hint);
  280. /** Get the \c RenderingHint of this \c StateSet.*/
  281. inline int getRenderingHint() const { return _renderingHint; }
  282. enum RenderBinMode
  283. {
  284. INHERIT_RENDERBIN_DETAILS =0,
  285. USE_RENDERBIN_DETAILS =1,
  286. OVERRIDE_RENDERBIN_DETAILS =2,
  287. PROTECTED_RENDERBIN_DETAILS =4,
  288. OVERRIDE_PROTECTED_RENDERBIN_DETAILS = OVERRIDE_RENDERBIN_DETAILS|PROTECTED_RENDERBIN_DETAILS
  289. };
  290. /** Set the render bin details.*/
  291. void setRenderBinDetails(int binNum,const std::string& binName,RenderBinMode mode=USE_RENDERBIN_DETAILS);
  292. /** Set the render bin details to inherit.*/
  293. void setRenderBinToInherit();
  294. /** Get whether the render bin details are set and should be used.*/
  295. inline bool useRenderBinDetails() const { return _binMode!=INHERIT_RENDERBIN_DETAILS; }
  296. /** Set the render bin mode.*/
  297. inline void setRenderBinMode(RenderBinMode mode) { _binMode=mode; }
  298. /** Get the render bin mode.*/
  299. inline RenderBinMode getRenderBinMode() const { return _binMode; }
  300. /** Set the render bin number.*/
  301. inline void setBinNumber(int num) { _binNum=num; }
  302. /** Get the render bin number.*/
  303. inline int getBinNumber() const { return _binNum; }
  304. /** Set the render bin name.*/
  305. inline void setBinName(const std::string& name) { _binName=name; }
  306. /** Get the render bin name.*/
  307. inline const std::string& getBinName() const { return _binName; }
  308. /** By default render bins will be nested within each other dependent
  309. * upon where they are set in the scene graph. This can be problematic
  310. * if a transparent render bin is attached to an opaque render bin
  311. * which is attached to another transparent render bin as these render
  312. * bins will be sorted separately, giving the wrong draw ordering for
  313. * back-to-front transparency. Therefore, to prevent render bins being
  314. * nested, call setNestRenderBins(false). */
  315. inline void setNestRenderBins(bool val) { _nestRenderBins = val; }
  316. /** Get whether associated RenderBin should be nested within parents RenderBin.*/
  317. inline bool getNestRenderBins() const { return _nestRenderBins; }
  318. struct OSG_EXPORT Callback : public virtual osg::Callback
  319. {
  320. Callback() {}
  321. Callback(const Callback& org,const CopyOp& copyop):
  322. osg::Object(org, copyop),
  323. osg::Callback(org, copyop) {}
  324. META_Object(osg,Callback);
  325. /** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
  326. virtual bool run(osg::Object* object, osg::Object* data);
  327. /** do customized callback code.*/
  328. virtual void operator() (StateSet*, NodeVisitor*) {}
  329. };
  330. /** Set the Update Callback which allows users to attach customize the updating of an object during the update traversal.*/
  331. void setUpdateCallback(Callback* ac);
  332. template<class T> void setUpdateCallback(const ref_ptr<T>& ac) { setUpdateCallback(ac.get()); }
  333. /** Get the non const Update Callback.*/
  334. Callback* getUpdateCallback() { return _updateCallback.get(); }
  335. /** Get the const Update Callback.*/
  336. const Callback* getUpdateCallback() const { return _updateCallback.get(); }
  337. /** Return whether this StateSet has update callbacks associated with it, and therefore must be traversed.*/
  338. bool requiresUpdateTraversal() const { return _updateCallback.valid() || getNumChildrenRequiringUpdateTraversal()!=0; }
  339. /** Get the number of Objects of this StateSet which require Update traversal,
  340. * since they have an Update Callback attached to them or their children.*/
  341. inline unsigned int getNumChildrenRequiringUpdateTraversal() const { return _numChildrenRequiringUpdateTraversal; }
  342. /** Run the update callbacks attached directly to this StateSet or to its children.*/
  343. void runUpdateCallbacks(osg::NodeVisitor* nv);
  344. /** Set the Event Callback which allows users to attach customize the updating of an object during the event traversal.*/
  345. void setEventCallback(Callback* ac);
  346. template<class T> void setEventCallback(const ref_ptr<T>& ec) { setEventCallback(ec.get()); }
  347. /** Get the non const Event Callback.*/
  348. Callback* getEventCallback() { return _eventCallback.get(); }
  349. /** Get the const Event Callback.*/
  350. const Callback* getEventCallback() const { return _eventCallback.get(); }
  351. /** Return whether this StateSet has event callbacks associated with it, and therefore must be traversed.*/
  352. bool requiresEventTraversal() const { return _eventCallback.valid() || getNumChildrenRequiringEventTraversal()!=0; }
  353. /** Get the number of Objects of this StateSet which require Event traversal,
  354. * since they have an Eevnt Callback attached to them or their children.*/
  355. inline unsigned int getNumChildrenRequiringEventTraversal() const { return _numChildrenRequiringEventTraversal; }
  356. /** Run the event callbacks attached directly to this StateSet or to its children.*/
  357. void runEventCallbacks(osg::NodeVisitor* nv);
  358. /** Check the modes associated with this StateSet are supported by current OpenGL drivers,
  359. * and if not set the associated mode in osg::State to be black listed/invalid.
  360. * Return true if all associated modes are valid.*/
  361. bool checkValidityOfAssociatedModes(State& state) const;
  362. /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
  363. virtual void setThreadSafeRefUnref(bool threadSafe);
  364. /** call compile on all StateAttributes contained within this StateSet.*/
  365. void compileGLObjects(State& state) const;
  366. /** Resize any per context GLObject buffers to specified size. */
  367. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  368. /** call release on all StateAttributes contained within this StateSet.*/
  369. virtual void releaseGLObjects(State* state=0) const;
  370. protected :
  371. virtual ~StateSet();
  372. StateSet& operator = (const StateSet&) { return *this; }
  373. void addParent(osg::Node* object);
  374. void removeParent(osg::Node* object);
  375. ParentList _parents;
  376. friend class osg::Node;
  377. friend class osg::Drawable;
  378. friend class osg::Uniform;
  379. friend class osg::StateAttribute;
  380. ModeList _modeList;
  381. AttributeList _attributeList;
  382. TextureModeList _textureModeList;
  383. TextureAttributeList _textureAttributeList;
  384. UniformList _uniformList;
  385. DefineList _defineList;
  386. inline ModeList& getOrCreateTextureModeList(unsigned int unit)
  387. {
  388. if (unit>=_textureModeList.size()) _textureModeList.resize(unit+1);
  389. return _textureModeList[unit];
  390. }
  391. inline AttributeList& getOrCreateTextureAttributeList(unsigned int unit)
  392. {
  393. if (unit>=_textureAttributeList.size()) _textureAttributeList.resize(unit+1);
  394. return _textureAttributeList[unit];
  395. }
  396. int compareModes(const ModeList& lhs, const ModeList& rhs);
  397. int compareAttributePtrs(const AttributeList& lhs, const AttributeList& rhs);
  398. int compareAttributeContents(const AttributeList& lhs, const AttributeList& rhs);
  399. void setMode(ModeList& modeList,StateAttribute::GLMode mode, StateAttribute::GLModeValue value);
  400. void setModeToInherit(ModeList& modeList, StateAttribute::GLMode mode);
  401. StateAttribute::GLModeValue getMode(const ModeList& modeList, StateAttribute::GLMode mode) const;
  402. void setAttribute(AttributeList& attributeList,StateAttribute *attribute, StateAttribute::OverrideValue value=StateAttribute::OFF);
  403. StateAttribute* getAttribute(AttributeList& attributeList, StateAttribute::Type type, unsigned int member);
  404. const StateAttribute* getAttribute(const AttributeList& attributeList, StateAttribute::Type type, unsigned int member) const;
  405. RefAttributePair* getAttributePair(AttributeList& attributeList, StateAttribute::Type type, unsigned int member);
  406. const RefAttributePair* getAttributePair(const AttributeList& attributeList, StateAttribute::Type type, unsigned int member) const;
  407. int _renderingHint;
  408. RenderBinMode _binMode;
  409. int _binNum;
  410. std::string _binName;
  411. bool _nestRenderBins;
  412. ref_ptr<Callback> _updateCallback;
  413. unsigned int _numChildrenRequiringUpdateTraversal;
  414. void setNumChildrenRequiringUpdateTraversal(unsigned int num);
  415. ref_ptr<Callback> _eventCallback;
  416. unsigned int _numChildrenRequiringEventTraversal;
  417. void setNumChildrenRequiringEventTraversal(unsigned int num);
  418. };
  419. extern OSG_EXPORT bool isTextureMode(StateAttribute::GLMode mode);
  420. }
  421. #endif