StateAttribute 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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_STATEATTRIBUTE
  14. #define OSG_STATEATTRIBUTE 1
  15. #include <osg/Export>
  16. #include <osg/Object>
  17. #include <osg/Callback>
  18. #include <osg/Shader>
  19. #include <osg/GL>
  20. #include <typeinfo>
  21. #include <utility>
  22. #include <vector>
  23. // define for the GL_EXT_secondary_color extension, GL_COLOR_SUM is OpenGL
  24. // mode to be used to enable and disable the second color.
  25. #ifndef GL_COLOR_SUM
  26. #define GL_COLOR_SUM 0x8458
  27. #endif
  28. namespace osg {
  29. // forward declare NodeVisitor, State & StateSet
  30. class NodeVisitor;
  31. class State;
  32. class ShaderComposer;
  33. class StateSet;
  34. class Texture;
  35. /** META_StateAttribute macro define the standard clone, isSameKindAs,
  36. * className and getType methods.
  37. * Use when subclassing from Object to make it more convenient to define
  38. * the standard pure virtual methods which are required for all Object
  39. * subclasses.*/
  40. #define META_StateAttribute(library,name,type) \
  41. virtual osg::Object* cloneType() const { return new name(); } \
  42. virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new name (*this,copyop); } \
  43. virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const name *>(obj)!=NULL; } \
  44. virtual const char* libraryName() const { return #library; } \
  45. virtual const char* className() const { return #name; } \
  46. virtual Type getType() const { return type; }
  47. /** COMPARE_StateAttribute_Types macro is a helper for implementing the StateAtribute::compare(..) method.*/
  48. #define COMPARE_StateAttribute_Types(TYPE,rhs_attribute) \
  49. if (this==&rhs_attribute) return 0;\
  50. const std::type_info* type_lhs = &typeid(*this);\
  51. const std::type_info* type_rhs = &typeid(rhs_attribute);\
  52. if (type_lhs->before(*type_rhs)) return -1;\
  53. if (*type_lhs != *type_rhs) return 1;\
  54. const TYPE& rhs = static_cast<const TYPE&>(rhs_attribute);
  55. /** COMPARE_StateAttribute_Parameter macro is a helper for implementing the StatateAtribute::compare(..) method.
  56. * Macro assumes that variable rhs has been correctly defined by preceding code
  57. * macro.*/
  58. #define COMPARE_StateAttribute_Parameter(parameter) \
  59. if (parameter<rhs.parameter) return -1; \
  60. if (rhs.parameter<parameter) return 1;
  61. /** Base class for state attributes.
  62. */
  63. class OSG_EXPORT StateAttribute : public Object
  64. {
  65. public :
  66. /** GLMode is the value used in glEnable/glDisable(mode) */
  67. typedef GLenum GLMode;
  68. /** GLModeValue is used to specify whether a mode is enabled (ON) or disabled (OFF).
  69. * GLMoveValue is also used to specify the override behavior of modes from parent to children.
  70. * See enum Value description for more details.*/
  71. typedef unsigned int GLModeValue;
  72. /** Override is used to specify the override behavior of StateAttributes
  73. * from parent to children.
  74. * See enum Value description for more details.*/
  75. typedef unsigned int OverrideValue;
  76. /** list values which can be used to set either GLModeValues or OverrideValues.
  77. * When using in conjunction with GLModeValues, all Values have meaning.
  78. * When using in conjunction with StateAttribute OverrideValue only
  79. * OFF,OVERRIDE and INHERIT are meaningful.
  80. * However, they are useful when using GLModeValue
  81. * and OverrideValue in conjunction with each other as when using
  82. * StateSet::setAttributeAndModes(..).*/
  83. enum Values
  84. {
  85. /** means that associated GLMode and Override is disabled.*/
  86. OFF = 0x0,
  87. /** means that associated GLMode is enabled and Override is disabled.*/
  88. ON = 0x1,
  89. /** Overriding of GLMode's or StateAttributes is enabled, so that state below it is overridden.*/
  90. OVERRIDE = 0x2,
  91. /** Protecting of GLMode's or StateAttributes is enabled, so that state from above cannot override this and below state.*/
  92. PROTECTED = 0x4,
  93. /** means that GLMode or StateAttribute should be inherited from above.*/
  94. INHERIT = 0x8
  95. };
  96. /** Type identifier to differentiate between different state types. */
  97. // typedef unsigned int Type;
  98. /** Values of StateAttribute::Type used to aid identification
  99. * of different StateAttribute subclasses. Each subclass defines
  100. * its own value in the virtual Type getType() method. When
  101. * extending the osg's StateAttribute's simply define your
  102. * own Type value which is unique, using the StateAttribute::Type
  103. * enum as a guide of what values to use. If your new subclass
  104. * needs to override a standard StateAttribute then simply use
  105. * that type's value. */
  106. enum Type
  107. {
  108. TEXTURE,
  109. POLYGONMODE,
  110. POLYGONOFFSET,
  111. MATERIAL,
  112. ALPHAFUNC,
  113. ANTIALIAS,
  114. COLORTABLE,
  115. CULLFACE,
  116. FOG,
  117. FRONTFACE,
  118. LIGHT,
  119. POINT,
  120. LINEWIDTH,
  121. LINESTIPPLE,
  122. POLYGONSTIPPLE,
  123. SHADEMODEL,
  124. TEXENV,
  125. TEXENVFILTER,
  126. TEXGEN,
  127. TEXMAT,
  128. LIGHTMODEL,
  129. BLENDFUNC,
  130. BLENDEQUATION,
  131. LOGICOP,
  132. STENCIL,
  133. COLORMASK,
  134. DEPTH,
  135. VIEWPORT,
  136. SCISSOR,
  137. BLENDCOLOR,
  138. MULTISAMPLE,
  139. CLIPPLANE,
  140. COLORMATRIX,
  141. VERTEXPROGRAM,
  142. FRAGMENTPROGRAM,
  143. POINTSPRITE,
  144. PROGRAM,
  145. CLAMPCOLOR,
  146. HINT,
  147. SAMPLEMASKI,
  148. PRIMITIVERESTARTINDEX,
  149. CLIPCONTROL,
  150. /// osgFX namespace
  151. VALIDATOR,
  152. VIEWMATRIXEXTRACTOR,
  153. /// osgNV namespace
  154. OSGNV_PARAMETER_BLOCK,
  155. // osgNVExt namespace
  156. OSGNVEXT_TEXTURE_SHADER,
  157. OSGNVEXT_VERTEX_PROGRAM,
  158. OSGNVEXT_REGISTER_COMBINERS,
  159. /// osgNVCg namespace
  160. OSGNVCG_PROGRAM,
  161. // osgNVSlang namespace
  162. OSGNVSLANG_PROGRAM,
  163. // osgNVParse
  164. OSGNVPARSE_PROGRAM_PARSER,
  165. UNIFORMBUFFERBINDING,
  166. TRANSFORMFEEDBACKBUFFERBINDING,
  167. ATOMICCOUNTERBUFFERBINDING,
  168. PATCH_PARAMETER,
  169. FRAME_BUFFER_OBJECT,
  170. VERTEX_ATTRIB_DIVISOR,
  171. SHADERSTORAGEBUFFERBINDING,
  172. INDIRECTDRAWBUFFERBINDING,
  173. VIEWPORTINDEXED,
  174. DEPTHRANGEINDEXED,
  175. SCISSORINDEXED,
  176. BINDIMAGETEXTURE,
  177. SAMPLER,
  178. CAPABILITY = 100
  179. };
  180. /** Simple pairing between an attribute type and the member within that attribute type group.*/
  181. typedef std::pair<Type,unsigned int> TypeMemberPair;
  182. StateAttribute();
  183. StateAttribute(const StateAttribute& sa,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  184. Object(sa,copyop),
  185. _shaderComponent(sa._shaderComponent),
  186. _updateCallback(copyop(sa._updateCallback.get())),
  187. _eventCallback(copyop(sa._eventCallback.get()))
  188. {}
  189. /** Clone the type of an attribute, with Object* return type.
  190. Must be defined by derived classes.*/
  191. virtual Object* cloneType() const = 0;
  192. /** Clone an attribute, with Object* return type.
  193. Must be defined by derived classes.*/
  194. virtual Object* clone(const CopyOp&) const = 0;
  195. /** Return true if this and obj are of the same kind of object.*/
  196. virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const StateAttribute*>(obj)!=NULL; }
  197. /** Return the name of the attribute's library.*/
  198. virtual const char* libraryName() const { return "osg"; }
  199. /** Return the name of the attribute's class type.*/
  200. virtual const char* className() const { return "StateAttribute"; }
  201. /** Convert 'this' into a StateAttribute pointer if Object is a StateAttribute, otherwise return 0.
  202. * Equivalent to dynamic_cast<StateAttribute*>(this).*/
  203. virtual StateAttribute* asStateAttribute() { return this; }
  204. /** convert 'const this' into a const StateAttribute pointer if Object is a StateAttribute, otherwise return 0.
  205. * Equivalent to dynamic_cast<const StateAttribute*>(this).*/
  206. virtual const StateAttribute* asStateAttribute() const { return this; }
  207. /** Fast alternative to dynamic_cast<> for determining if state attribute is a Texture.*/
  208. virtual Texture* asTexture() { return 0; }
  209. /** Fast alternative to dynamic_cast<> for determining if state attribute is a Texture.*/
  210. virtual const Texture* asTexture() const { return 0; }
  211. /** Return the Type identifier of the attribute's class type.*/
  212. virtual Type getType() const = 0;
  213. /** Return the member identifier within the attribute's class type. Used for light number/clip plane number etc.*/
  214. virtual unsigned int getMember() const { return 0; }
  215. /** Return the TypeMemberPair that uniquely identifies this type member.*/
  216. inline TypeMemberPair getTypeMemberPair() const { return TypeMemberPair(getType(),getMember()); }
  217. /** Return true if StateAttribute is a type which controls texturing and needs to be issued w.r.t to specific texture unit.*/
  218. virtual bool isTextureAttribute() const { return false; }
  219. /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
  220. virtual int compare(const StateAttribute& sa) const = 0;
  221. bool operator < (const StateAttribute& rhs) const { return compare(rhs)<0; }
  222. bool operator == (const StateAttribute& rhs) const { return compare(rhs)==0; }
  223. bool operator != (const StateAttribute& rhs) const { return compare(rhs)!=0; }
  224. /** A vector of osg::StateSet pointers which is used to store the parent(s) of this StateAttribute.*/
  225. typedef std::vector<StateSet*> ParentList;
  226. /** Get the parent list of this StateAttribute. */
  227. inline const ParentList& getParents() const { return _parents; }
  228. inline StateSet* getParent(unsigned int i) { return _parents[i]; }
  229. /**
  230. * Get a single const parent of this StateAttribute.
  231. * @param i index of the parent to get.
  232. * @return the parent i.
  233. */
  234. inline const StateSet* getParent(unsigned int i) const { return _parents[i]; }
  235. /**
  236. * Get the number of parents of this StateAttribute.
  237. * @return the number of parents of this StateAttribute.
  238. */
  239. inline unsigned int getNumParents() const { return static_cast<unsigned int>(_parents.size()); }
  240. void setShaderComponent(ShaderComponent* sc) { _shaderComponent = sc; }
  241. ShaderComponent* getShaderComponent() { return _shaderComponent.get(); }
  242. const ShaderComponent* getShaderComponent() const { return _shaderComponent.get(); }
  243. struct ModeUsage
  244. {
  245. virtual ~ModeUsage() {}
  246. virtual void usesMode(GLMode mode) = 0;
  247. virtual void usesTextureMode(GLMode mode) = 0;
  248. };
  249. /** Return the modes associated with this StateAttribute.*/
  250. virtual bool getModeUsage(ModeUsage&) const
  251. {
  252. // default to no GLMode's associated with use of the StateAttribute.
  253. return false;
  254. }
  255. /** Check the modes associated with this StateAttribute are supported by current OpenGL drivers,
  256. * and if not set the associated mode in osg::State to be black listed/invalid.
  257. * Return true if all associated modes are valid.*/
  258. virtual bool checkValidityOfAssociatedModes(osg::State&) const
  259. {
  260. // default to no black listed GLMode's associated with use of the StateAttribute.
  261. return true;
  262. }
  263. // provide callback for backwards compatibility.
  264. typedef osg::StateAttributeCallback Callback;
  265. /** Set the UpdateCallback which allows users to attach customize the updating of an object during the update traversal.*/
  266. void setUpdateCallback(StateAttributeCallback* uc);
  267. /** Get the non const UpdateCallback.*/
  268. StateAttributeCallback* getUpdateCallback() { return _updateCallback.get(); }
  269. /** Get the const UpdateCallback.*/
  270. const StateAttributeCallback* getUpdateCallback() const { return _updateCallback.get(); }
  271. /** Set the EventCallback which allows users to attach customize the updating of an object during the Event traversal.*/
  272. void setEventCallback(StateAttributeCallback* ec);
  273. /** Get the non const EventCallback.*/
  274. StateAttributeCallback* getEventCallback() { return _eventCallback.get(); }
  275. /** Get the const EventCallback.*/
  276. const StateAttributeCallback* getEventCallback() const { return _eventCallback.get(); }
  277. /** apply the OpenGL state attributes.
  278. * The render info for the current OpenGL context is passed
  279. * in to allow the StateAttribute to obtain details on the
  280. * the current context and state.
  281. */
  282. virtual void apply(State&) const {}
  283. /** Default to nothing to compile - all state is applied immediately. */
  284. virtual void compileGLObjects(State&) const {}
  285. /** Resize any per context GLObject buffers to specified size. */
  286. virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/) {}
  287. /** Release OpenGL objects in specified graphics context if State
  288. object is passed, otherwise release OpenGL objects for all graphics context if
  289. State object pointer NULL.*/
  290. virtual void releaseGLObjects(State* =0) const {}
  291. protected:
  292. virtual ~StateAttribute() {}
  293. void addParent(osg::StateSet* object);
  294. void removeParent(osg::StateSet* object);
  295. ParentList _parents;
  296. friend class osg::StateSet;
  297. /** Helper class that make is easy to handle changes in a member value.*/
  298. struct ReassignToParents
  299. {
  300. /** Constructor caches and then removes attribute for all of it's parents.*/
  301. ReassignToParents(osg::StateAttribute* att);
  302. /** Destructor then reassigns the attribute to all of the parents.*/
  303. ~ReassignToParents();
  304. ref_ptr<StateAttribute> attribute;
  305. ParentList parents;
  306. };
  307. ref_ptr<ShaderComponent> _shaderComponent;
  308. ref_ptr<StateAttributeCallback> _updateCallback;
  309. ref_ptr<StateAttributeCallback> _eventCallback;
  310. };
  311. }
  312. #endif