root/OpenSceneGraph/trunk/include/osg/StateAttribute @ 13041

Revision 13041, 13.7 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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
14#ifndef OSG_STATEATTRIBUTE
15#define OSG_STATEATTRIBUTE 1
16
17#include <osg/Export>
18#include <osg/Object>
19#include <osg/StateAttributeCallback>
20#include <osg/Shader>
21#include <osg/GL>
22
23#include <typeinfo>
24#include <utility>
25#include <vector>
26
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.
29#ifndef GL_COLOR_SUM
30#define GL_COLOR_SUM    0x8458
31#endif
32
33namespace osg {
34
35
36// forward declare NodeVisitor, State & StateSet
37class NodeVisitor;
38class State;
39class ShaderComposer;
40class StateSet;
41class Texture;
42
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
47  * subclasses.*/
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; }
55
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);
64
65
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
68  * macro.*/
69#define COMPARE_StateAttribute_Parameter(parameter) \
70        if (parameter<rhs.parameter) return -1; \
71        if (rhs.parameter<parameter) return 1;
72
73
74/** Base class for state attributes.
75*/
76class OSG_EXPORT StateAttribute : public Object
77{
78    public :
79
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;
90
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(..).*/
98        enum Values
99        {
100            /** means that associated GLMode and Override is disabled.*/
101            OFF          = 0x0,
102            /** means that associated GLMode is enabled and Override is disabled.*/
103            ON           = 0x1,
104            /** Overriding of GLMode's or StateAttributes is enabled, so that state below it is overridden.*/
105            OVERRIDE     = 0x2,
106            /** Protecting of GLMode's or StateAttributes is enabled, so that state from above cannot override this and below state.*/
107            PROTECTED    = 0x4,
108            /** means that GLMode or StateAttribute should be inherited from above.*/
109            INHERIT      = 0x8
110        };
111
112        /** Type identifier to differentiate between different state types. */
113        // typedef unsigned int Type;
114
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 StateAttriubte then simply use
122          * that type's value. */
123        enum Type
124        {
125            TEXTURE,
126
127            POLYGONMODE,
128            POLYGONOFFSET,
129            MATERIAL,
130            ALPHAFUNC,
131            ANTIALIAS,
132            COLORTABLE,
133            CULLFACE,
134            FOG,
135            FRONTFACE,
136
137            LIGHT,
138
139            POINT,
140            LINEWIDTH,
141            LINESTIPPLE,
142            POLYGONSTIPPLE,
143            SHADEMODEL,
144            TEXENV,
145            TEXENVFILTER,
146            TEXGEN,
147            TEXMAT,
148            LIGHTMODEL,
149            BLENDFUNC,
150            BLENDEQUATION,
151            LOGICOP,
152            STENCIL,
153            COLORMASK,
154            DEPTH,
155            VIEWPORT,
156            SCISSOR,
157            BLENDCOLOR,
158            MULTISAMPLE,
159            CLIPPLANE,
160            COLORMATRIX,
161            VERTEXPROGRAM,
162            FRAGMENTPROGRAM,
163            POINTSPRITE,
164            PROGRAM,
165            CLAMPCOLOR,
166            HINT,
167
168            /// osgFX namespace
169            VALIDATOR,
170            VIEWMATRIXEXTRACTOR,
171
172            /// osgNV namespace
173            OSGNV_PARAMETER_BLOCK,
174
175            // osgNVExt namespace
176            OSGNVEXT_TEXTURE_SHADER,
177            OSGNVEXT_VERTEX_PROGRAM,
178            OSGNVEXT_REGISTER_COMBINERS,
179
180            /// osgNVCg namespace
181            OSGNVCG_PROGRAM,
182
183            // osgNVSlang namespace
184            OSGNVSLANG_PROGRAM,
185
186            // osgNVParse
187            OSGNVPARSE_PROGRAM_PARSER,
188
189            UNIFORMBUFFERBINDING,
190            TRANSFORMFEEDBACKBUFFERBINDING
191        };
192
193        /** Simple pairing between an attribute type and the member within that attribute type group.*/
194        typedef std::pair<Type,unsigned int> TypeMemberPair;
195
196        StateAttribute();
197
198        StateAttribute(const StateAttribute& sa,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
199            Object(sa,copyop),
200            _shaderComponent(sa._shaderComponent),
201            _updateCallback(copyop(sa._updateCallback.get())),
202            _eventCallback(copyop(sa._eventCallback.get()))
203            {}
204
205
206        /** Clone the type of an attribute, with Object* return type.
207            Must be defined by derived classes.*/
208        virtual Object* cloneType() const = 0;
209
210        /** Clone an attribute, with Object* return type.
211            Must be defined by derived classes.*/
212        virtual Object* clone(const CopyOp&) const = 0;
213
214        /** Return true if this and obj are of the same kind of object.*/
215        virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const StateAttribute*>(obj)!=NULL; }
216
217        /** Return the name of the attribute's library.*/
218        virtual const char* libraryName() const { return "osg"; }
219
220        /** Return the name of the attribute's class type.*/
221        virtual const char* className() const { return "StateAttribute"; }
222
223
224        /** Fast alternative to dynamic_cast<> for determining if state attribute is a Texture.*/
225        virtual Texture* asTexture() { return 0; }
226
227        /** Fast alternative to dynamic_cast<> for determining if state attribute is a Texture.*/
228        virtual const Texture* asTexture() const { return 0; }
229
230
231        /** Return the Type identifier of the attribute's class type.*/
232        virtual Type getType() const = 0;
233
234        /** Return the member identifier within the attribute's class type. Used for light number/clip plane number etc.*/
235        virtual unsigned int getMember() const { return 0; }
236
237        /** Return the TypeMemberPair that uniquely identifies this type member.*/
238        inline TypeMemberPair getTypeMemberPair() const { return TypeMemberPair(getType(),getMember()); }
239
240        /** Return true if StateAttribute is a type which controls texturing and needs to be issued w.r.t to specific texture unit.*/
241        virtual bool isTextureAttribute() const { return false; }
242
243        /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
244        virtual int compare(const StateAttribute& sa) const = 0;
245
246        bool operator <  (const StateAttribute& rhs) const { return compare(rhs)<0; }
247        bool operator == (const StateAttribute& rhs) const { return compare(rhs)==0; }
248        bool operator != (const StateAttribute& rhs) const { return compare(rhs)!=0; }
249
250
251        /** A vector of osg::StateSet pointers which is used to store the parent(s) of this StateAttribute.*/
252        typedef std::vector<StateSet*> ParentList;
253
254        /** Get the parent list of this StateAttribute. */
255        inline const ParentList& getParents() const { return _parents; }
256
257        inline StateSet* getParent(unsigned int i)  { return _parents[i]; }
258        /**
259         * Get a single const parent of this StateAttribute.
260         * @param i index of the parent to get.
261         * @return the parent i.
262         */
263        inline const StateSet* getParent(unsigned int i) const  { return _parents[i]; }
264
265        /**
266         * Get the number of parents of this StateAttribute.
267         * @return the number of parents of this StateAttribute.
268         */
269        inline unsigned int getNumParents() const { return static_cast<unsigned int>(_parents.size()); }
270
271        void setShaderComponent(ShaderComponent* sc) { _shaderComponent = sc; }
272        ShaderComponent* getShaderComponent() { return _shaderComponent.get(); }
273        const ShaderComponent* getShaderComponent() const { return _shaderComponent.get(); }
274
275        struct ModeUsage
276        {
277            virtual ~ModeUsage() {}
278            virtual void usesMode(GLMode mode) = 0;
279            virtual void usesTextureMode(GLMode mode) = 0;
280        };
281
282        /** Return the modes associated with this StateAttribute.*/
283        virtual bool getModeUsage(ModeUsage&) const
284        {
285            // default to no GLMode's associated with use of the StateAttribute.
286            return false;
287        }
288
289        /** Check the modes associated with this StateAttribute are supported by current OpenGL drivers,
290          * and if not set the associated mode in osg::State to be black listed/invalid.
291          * Return true if all associated modes are valid.*/
292        virtual bool checkValidityOfAssociatedModes(osg::State&) const
293        {
294            // default to no black listed GLMode's associated with use of the StateAttribute.
295            return true;
296        }
297
298        // provide callback for backwards compatibility.
299        typedef osg::StateAttributeCallback Callback;
300
301        /** Set the UpdateCallback which allows users to attach customize the updating of an object during the update traversal.*/
302        void setUpdateCallback(StateAttributeCallback* uc);
303
304        /** Get the non const UpdateCallback.*/
305        StateAttributeCallback* getUpdateCallback() { return _updateCallback.get(); }
306
307        /** Get the const UpdateCallback.*/
308        const StateAttributeCallback* getUpdateCallback() const { return _updateCallback.get(); }
309
310
311        /** Set the EventCallback which allows users to attach customize the updating of an object during the Event traversal.*/
312        void setEventCallback(StateAttributeCallback* ec);
313
314        /** Get the non const EventCallback.*/
315        StateAttributeCallback* getEventCallback() { return _eventCallback.get(); }
316
317        /** Get the const EventCallback.*/
318        const StateAttributeCallback* getEventCallback() const { return _eventCallback.get(); }
319
320
321        /** apply the OpenGL state attributes.
322          * The render info for the current OpenGL context is passed
323          * in to allow the StateAttribute to obtain details on the
324          * the current context and state.
325          */
326        virtual void apply(State&) const {}
327
328        /** Default to nothing to compile - all state is applied immediately. */
329        virtual void compileGLObjects(State&) const {}
330
331        /** Resize any per context GLObject buffers to specified size. */
332        virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/) {}
333
334        /** Release OpenGL objects in specified graphics context if State
335            object is passed, otherwise release OpenGL objects for all graphics context if
336            State object pointer NULL.*/
337        virtual void releaseGLObjects(State* =0) const {}
338
339
340    protected:
341
342        virtual ~StateAttribute() {}
343
344        void addParent(osg::StateSet* object);
345        void removeParent(osg::StateSet* object);
346
347        ParentList _parents;
348        friend class osg::StateSet;
349
350        ref_ptr<ShaderComponent>          _shaderComponent;
351
352        ref_ptr<StateAttributeCallback>   _updateCallback;
353        ref_ptr<StateAttributeCallback>   _eventCallback;
354};
355
356}
357
358#endif
Note: See TracBrowser for help on using the browser.