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

Revision 9599, 13.3 kB (checked in by robert, 4 years ago)

Fixed warnings, updated NEWS

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