1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
14#ifndef OSG_STATEATTRIBUTE
15#define OSG_STATEATTRIBUTE 1
19#include <osg/Callback>
27// define for the GL_EXT_secondary_color extension, GL_COLOR_SUM is OpenGL
28// mode to be used to enable and disable the second color.
30#define GL_COLOR_SUM 0x8458
36// forward declare NodeVisitor, State & StateSet
43/** META_StateAttribute macro define the standard clone, isSameKindAs,
44 * className and getType methods.
45 * Use when subclassing from Object to make it more convenient to define
46 * the standard pure virtual methods which are required for all Object
48#define META_StateAttribute(library,name,type) \
49 virtual osg::Object* cloneType() const { return new name(); } \
50 virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new name (*this,copyop); } \
51 virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const name *>(obj)!=NULL; } \
52 virtual const char* libraryName() const { return #library; } \
53 virtual const char* className() const { return #name; } \
54 virtual Type getType() const { return type; }
56/** COMPARE_StateAttribute_Types macro is a helper for implementing the StateAtribute::compare(..) method.*/
57#define COMPARE_StateAttribute_Types(TYPE,rhs_attribute) \
58 if (this==&rhs_attribute) return 0;\
59 const std::type_info* type_lhs = &typeid(*this);\
60 const std::type_info* type_rhs = &typeid(rhs_attribute);\
61 if (type_lhs->before(*type_rhs)) return -1;\
62 if (*type_lhs != *type_rhs) return 1;\
63 const TYPE& rhs = static_cast<const TYPE&>(rhs_attribute);
66/** COMPARE_StateAttribute_Parameter macro is a helper for implementing the StatateAtribute::compare(..) method.
67 * Macro assumes that variable rhs has been correctly defined by preceding code
69#define COMPARE_StateAttribute_Parameter(parameter) \
70 if (parameter<rhs.parameter) return -1; \
71 if (rhs.parameter<parameter) return 1;
74/** Base class for state attributes.
76class OSG_EXPORT StateAttribute : public Object
80 /** GLMode is the value used in glEnable/glDisable(mode) */
81 typedef GLenum GLMode;
82 /** GLModeValue is used to specify whether a mode is enabled (ON) or disabled (OFF).
83 * GLMoveValue is also used to specify the override behavior of modes from parent to children.
84 * See enum Value description for more details.*/
85 typedef unsigned int GLModeValue;
86 /** Override is used to specify the override behavior of StateAttributes
87 * from parent to children.
88 * See enum Value description for more details.*/
89 typedef unsigned int OverrideValue;
91 /** list values which can be used to set either GLModeValues or OverrideValues.
92 * When using in conjunction with GLModeValues, all Values have meaning.
93 * When using in conjunction with StateAttribute OverrideValue only
94 * OFF,OVERRIDE and INHERIT are meaningful.
95 * However, they are useful when using GLModeValue
96 * and OverrideValue in conjunction with each other as when using
97 * StateSet::setAttributeAndModes(..).*/
100 /** means that associated GLMode and Override is disabled.*/
102 /** means that associated GLMode is enabled and Override is disabled.*/
104 /** Overriding of GLMode's or StateAttributes is enabled, so that state below it is overridden.*/
106 /** Protecting of GLMode's or StateAttributes is enabled, so that state from above cannot override this and below state.*/
108 /** means that GLMode or StateAttribute should be inherited from above.*/
112 /** Type identifier to differentiate between different state types. */
113 // typedef unsigned int Type;
115 /** Values of StateAttribute::Type used to aid identification
116 * of different StateAttribute subclasses. Each subclass defines
117 * its own value in the virtual Type getType() method. When
118 * extending the osg's StateAttribute's simply define your
119 * own Type value which is unique, using the StateAttribute::Type
120 * enum as a guide of what values to use. If your new subclass
121 * needs to override a standard StateAttribute then simply use
122 * that type's value. */
168 PRIMITIVERESTARTINDEX,
176 OSGNV_PARAMETER_BLOCK,
178 // osgNVExt namespace
179 OSGNVEXT_TEXTURE_SHADER,
180 OSGNVEXT_VERTEX_PROGRAM,
181 OSGNVEXT_REGISTER_COMBINERS,
183 /// osgNVCg namespace
186 // osgNVSlang namespace
190 OSGNVPARSE_PROGRAM_PARSER,
192 UNIFORMBUFFERBINDING,
193 TRANSFORMFEEDBACKBUFFERBINDING,
195 ATOMICCOUNTERBUFFERBINDING,
201 VERTEX_ATTRIB_DIVISOR,
203 SHADERSTORAGEBUFFERBINDING,
205 INDIRECTDRAWBUFFERBINDING,
217 /** Simple pairing between an attribute type and the member within that attribute type group.*/
218 typedef std::pair<Type,unsigned int> TypeMemberPair;
222 StateAttribute(const StateAttribute& sa,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
224 _shaderComponent(sa._shaderComponent),
225 _updateCallback(copyop(sa._updateCallback.get())),
226 _eventCallback(copyop(sa._eventCallback.get()))
230 /** Clone the type of an attribute, with Object* return type.
231 Must be defined by derived classes.*/
232 virtual Object* cloneType() const = 0;
234 /** Clone an attribute, with Object* return type.
235 Must be defined by derived classes.*/
236 virtual Object* clone(const CopyOp&) const = 0;
238 /** Return true if this and obj are of the same kind of object.*/
239 virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const StateAttribute*>(obj)!=NULL; }
241 /** Return the name of the attribute's library.*/
242 virtual const char* libraryName() const { return "osg"; }
244 /** Return the name of the attribute's class type.*/
245 virtual const char* className() const { return "StateAttribute"; }
248 /** Convert 'this' into a StateAttribute pointer if Object is a StateAttribute, otherwise return 0.
249 * Equivalent to dynamic_cast<StateAttribute*>(this).*/
250 virtual StateAttribute* asStateAttribute() { return this; }
252 /** convert 'const this' into a const StateAttribute pointer if Object is a StateAttribute, otherwise return 0.
253 * Equivalent to dynamic_cast<const StateAttribute*>(this).*/
254 virtual const StateAttribute* asStateAttribute() const { return this; }
256 /** Fast alternative to dynamic_cast<> for determining if state attribute is a Texture.*/
257 virtual Texture* asTexture() { return 0; }
259 /** Fast alternative to dynamic_cast<> for determining if state attribute is a Texture.*/
260 virtual const Texture* asTexture() const { return 0; }
263 /** Return the Type identifier of the attribute's class type.*/
264 virtual Type getType() const = 0;
266 /** Return the member identifier within the attribute's class type. Used for light number/clip plane number etc.*/
267 virtual unsigned int getMember() const { return 0; }
269 /** Return the TypeMemberPair that uniquely identifies this type member.*/
270 inline TypeMemberPair getTypeMemberPair() const { return TypeMemberPair(getType(),getMember()); }
272 /** Return true if StateAttribute is a type which controls texturing and needs to be issued w.r.t to specific texture unit.*/
273 virtual bool isTextureAttribute() const { return false; }
275 /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
276 virtual int compare(const StateAttribute& sa) const = 0;
278 bool operator < (const StateAttribute& rhs) const { return compare(rhs)<0; }
279 bool operator == (const StateAttribute& rhs) const { return compare(rhs)==0; }
280 bool operator != (const StateAttribute& rhs) const { return compare(rhs)!=0; }
283 /** A vector of osg::StateSet pointers which is used to store the parent(s) of this StateAttribute.*/
284 typedef std::vector<StateSet*> ParentList;
286 /** Get the parent list of this StateAttribute. */
287 inline const ParentList& getParents() const { return _parents; }
289 inline StateSet* getParent(unsigned int i) { return _parents[i]; }
291 * Get a single const parent of this StateAttribute.
292 * @param i index of the parent to get.
293 * @return the parent i.
295 inline const StateSet* getParent(unsigned int i) const { return _parents[i]; }
298 * Get the number of parents of this StateAttribute.
299 * @return the number of parents of this StateAttribute.
301 inline unsigned int getNumParents() const { return static_cast<unsigned int>(_parents.size()); }
303 void setShaderComponent(ShaderComponent* sc) { _shaderComponent = sc; }
304 ShaderComponent* getShaderComponent() { return _shaderComponent.get(); }
305 const ShaderComponent* getShaderComponent() const { return _shaderComponent.get(); }
309 virtual ~ModeUsage() {}
310 virtual void usesMode(GLMode mode) = 0;
311 virtual void usesTextureMode(GLMode mode) = 0;
314 /** Return the modes associated with this StateAttribute.*/
315 virtual bool getModeUsage(ModeUsage&) const
317 // default to no GLMode's associated with use of the StateAttribute.
321 /** Check the modes associated with this StateAttribute are supported by current OpenGL drivers,
322 * and if not set the associated mode in osg::State to be black listed/invalid.
323 * Return true if all associated modes are valid.*/
324 virtual bool checkValidityOfAssociatedModes(osg::State&) const
326 // default to no black listed GLMode's associated with use of the StateAttribute.
330 // provide callback for backwards compatibility.
331 typedef osg::StateAttributeCallback Callback;
333 /** Set the UpdateCallback which allows users to attach customize the updating of an object during the update traversal.*/
334 void setUpdateCallback(StateAttributeCallback* uc);
336 /** Get the non const UpdateCallback.*/
337 StateAttributeCallback* getUpdateCallback() { return _updateCallback.get(); }
339 /** Get the const UpdateCallback.*/
340 const StateAttributeCallback* getUpdateCallback() const { return _updateCallback.get(); }
343 /** Set the EventCallback which allows users to attach customize the updating of an object during the Event traversal.*/
344 void setEventCallback(StateAttributeCallback* ec);
346 /** Get the non const EventCallback.*/
347 StateAttributeCallback* getEventCallback() { return _eventCallback.get(); }
349 /** Get the const EventCallback.*/
350 const StateAttributeCallback* getEventCallback() const { return _eventCallback.get(); }
353 /** apply the OpenGL state attributes.
354 * The render info for the current OpenGL context is passed
355 * in to allow the StateAttribute to obtain details on the
356 * the current context and state.
358 virtual void apply(State&) const {}
360 /** Default to nothing to compile - all state is applied immediately. */
361 virtual void compileGLObjects(State&) const {}
363 /** Resize any per context GLObject buffers to specified size. */
364 virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/) {}
366 /** Release OpenGL objects in specified graphics context if State
367 object is passed, otherwise release OpenGL objects for all graphics context if
368 State object pointer NULL.*/
369 virtual void releaseGLObjects(State* =0) const {}
374 virtual ~StateAttribute() {}
376 void addParent(osg::StateSet* object);
377 void removeParent(osg::StateSet* object);
380 friend class osg::StateSet;
382 /** Helper class that make is easy to handle changes in a member value.*/
383 struct ReassignToParents
385 /** Constructor caches and then removes attribute for all of it's parents.*/
386 ReassignToParents(osg::StateAttribute* att);
388 /** Destructor then reassigns the attribute to all of the parents.*/
389 ~ReassignToParents();
391 ref_ptr<StateAttribute> attribute;
396 ref_ptr<ShaderComponent> _shaderComponent;
398 ref_ptr<StateAttributeCallback> _updateCallback;
399 ref_ptr<StateAttributeCallback> _eventCallback;