| [5328] | 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| [1529] | 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 | */ |
|---|
| [51] | 13 | |
|---|
| [10] | 14 | #ifndef OSG_STATEATTRIBUTE |
|---|
| 15 | #define OSG_STATEATTRIBUTE 1 |
|---|
| 16 | |
|---|
| [585] | 17 | #include <osg/Export> |
|---|
| [10] | 18 | #include <osg/Object> |
|---|
| 19 | #include <osg/GL> |
|---|
| 20 | |
|---|
| [26] | 21 | #include <typeinfo> |
|---|
| [3488] | 22 | #include <utility> |
|---|
| [4091] | 23 | #include <vector> |
|---|
| [26] | 24 | |
|---|
| [3766] | 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 | |
|---|
| [10] | 31 | namespace osg { |
|---|
| 32 | |
|---|
| [3222] | 33 | |
|---|
| [4084] | 34 | // forward declare NodeVisitor, State & StateSet |
|---|
| 35 | class NodeVisitor; |
|---|
| [10] | 36 | class State; |
|---|
| 37 | class StateSet; |
|---|
| [8655] | 38 | class Texture; |
|---|
| [10] | 39 | |
|---|
| [26] | 40 | /** META_StateAttribute macro define the standard clone, isSameKindAs, |
|---|
| 41 | * className and getType methods. |
|---|
| [3375] | 42 | * Use when subclassing from Object to make it more convenient to define |
|---|
| [26] | 43 | * the standard pure virtual methods which are required for all Object |
|---|
| 44 | * subclasses.*/ |
|---|
| [740] | 45 | #define META_StateAttribute(library,name,type) \ |
|---|
| [1418] | 46 | virtual osg::Object* cloneType() const { return new name(); } \ |
|---|
| 47 | virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new name (*this,copyop); } \ |
|---|
| [327] | 48 | virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const name *>(obj)!=NULL; } \ |
|---|
| [740] | 49 | virtual const char* libraryName() const { return #library; } \ |
|---|
| [26] | 50 | virtual const char* className() const { return #name; } \ |
|---|
| [1133] | 51 | virtual Type getType() const { return type; } |
|---|
| [26] | 52 | |
|---|
| [3375] | 53 | /** COMPARE_StateAttribute_Types macro is a helper for implementing the StateAtribute::compare(..) method.*/ |
|---|
| [26] | 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. |
|---|
| [3375] | 64 | * Macro assumes that variable rhs has been correctly defined by preceding code |
|---|
| [26] | 65 | * macro.*/ |
|---|
| 66 | #define COMPARE_StateAttribute_Parameter(parameter) \ |
|---|
| 67 | if (parameter<rhs.parameter) return -1; \ |
|---|
| 68 | if (rhs.parameter<parameter) return 1; |
|---|
| 69 | |
|---|
| [1115] | 70 | |
|---|
| [3375] | 71 | /** Base class for state attributes. |
|---|
| [10] | 72 | */ |
|---|
| [4021] | 73 | class OSG_EXPORT StateAttribute : public Object |
|---|
| [10] | 74 | { |
|---|
| 75 | public : |
|---|
| 76 | |
|---|
| 77 | /** GLMode is the value used in glEnable/glDisable(mode) */ |
|---|
| 78 | typedef GLenum GLMode; |
|---|
| [3375] | 79 | /** GLModeValue is used to specify whether a mode is enabled (ON) or disabled (OFF). |
|---|
| [39] | 80 | * GLMoveValue is also used to specify the override behavior of modes from parent to children. |
|---|
| [10] | 81 | * See enum Value description for more details.*/ |
|---|
| 82 | typedef unsigned int GLModeValue; |
|---|
| [3375] | 83 | /** Override is used to specify the override behavior of StateAttributes |
|---|
| 84 | * from parent to children. |
|---|
| [10] | 85 | * See enum Value description for more details.*/ |
|---|
| [2306] | 86 | typedef unsigned int OverrideValue; |
|---|
| [10] | 87 | |
|---|
| [3375] | 88 | /** list values which can be used to set either GLModeValues or OverrideValues. |
|---|
| 89 | * When using in conjunction with GLModeValues, all Values have meaning. |
|---|
| [7648] | 90 | * When using in conjunction with StateAttribute OverrideValue only |
|---|
| [3375] | 91 | * OFF,OVERRIDE and INHERIT are meaningful. |
|---|
| 92 | * However, they are useful when using GLModeValue |
|---|
| [10] | 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, |
|---|
| [3375] | 101 | /** Overriding of GLMode's or StateAttributes is enabled, so that state below it is overridden.*/ |
|---|
| [10] | 102 | OVERRIDE = 0x2, |
|---|
| [3375] | 103 | /** Protecting of GLMode's or StateAttributes is enabled, so that state from above cannot override this and below state.*/ |
|---|
| [1008] | 104 | PROTECTED = 0x4, |
|---|
| [3375] | 105 | /** means that GLMode or StateAttribute should be inherited from above.*/ |
|---|
| [1008] | 106 | INHERIT = 0x8 |
|---|
| [10] | 107 | }; |
|---|
| [248] | 108 | |
|---|
| [26] | 109 | /** Type identifier to differentiate between different state types. */ |
|---|
| [3482] | 110 | // typedef unsigned int Type; |
|---|
| [26] | 111 | |
|---|
| [10] | 112 | /** Values of StateAttribute::Type used to aid identification |
|---|
| [39] | 113 | * of different StateAttribute subclasses. Each subclass defines |
|---|
| [3375] | 114 | * its own value in the virtual Type getType() method. When |
|---|
| [10] | 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 |
|---|
| [3375] | 118 | * needs to override a standard StateAttriubte then simply use |
|---|
| 119 | * that type's value. */ |
|---|
| [3482] | 120 | enum Type |
|---|
| [10] | 121 | { |
|---|
| [299] | 122 | TEXTURE, |
|---|
| [225] | 123 | |
|---|
| [489] | 124 | POLYGONMODE, |
|---|
| 125 | POLYGONOFFSET, |
|---|
| [299] | 126 | MATERIAL, |
|---|
| 127 | ALPHAFUNC, |
|---|
| 128 | ANTIALIAS, |
|---|
| 129 | COLORTABLE, |
|---|
| 130 | CULLFACE, |
|---|
| 131 | FOG, |
|---|
| 132 | FRONTFACE, |
|---|
| [225] | 133 | |
|---|
| [299] | 134 | LIGHT, |
|---|
| [233] | 135 | |
|---|
| [299] | 136 | POINT, |
|---|
| 137 | LINEWIDTH, |
|---|
| [422] | 138 | LINESTIPPLE, |
|---|
| [1164] | 139 | POLYGONSTIPPLE, |
|---|
| [299] | 140 | SHADEMODEL, |
|---|
| 141 | TEXENV, |
|---|
| [3494] | 142 | TEXENVFILTER, |
|---|
| [299] | 143 | TEXGEN, |
|---|
| 144 | TEXMAT, |
|---|
| [426] | 145 | LIGHTMODEL, |
|---|
| [867] | 146 | BLENDFUNC, |
|---|
| [3922] | 147 | BLENDEQUATION, |
|---|
| 148 | LOGICOP, |
|---|
| [299] | 149 | STENCIL, |
|---|
| 150 | COLORMASK, |
|---|
| 151 | DEPTH, |
|---|
| 152 | VIEWPORT, |
|---|
| [4407] | 153 | SCISSOR, |
|---|
| [2306] | 154 | BLENDCOLOR, |
|---|
| 155 | MULTISAMPLE, |
|---|
| [299] | 156 | CLIPPLANE, |
|---|
| [1448] | 157 | COLORMATRIX, |
|---|
| 158 | VERTEXPROGRAM, |
|---|
| [2032] | 159 | FRAGMENTPROGRAM, |
|---|
| [3482] | 160 | POINTSPRITE, |
|---|
| [4039] | 161 | PROGRAM, |
|---|
| [5169] | 162 | CLAMPCOLOR, |
|---|
| [6824] | 163 | HINT, |
|---|
| [4039] | 164 | |
|---|
| [3482] | 165 | /// osgFX namespace |
|---|
| 166 | VALIDATOR, |
|---|
| 167 | VIEWMATRIXEXTRACTOR, |
|---|
| [3500] | 168 | |
|---|
| [4039] | 169 | /// osgNV namespace |
|---|
| [3922] | 170 | OSGNV_PARAMETER_BLOCK, |
|---|
| 171 | |
|---|
| [3500] | 172 | // osgNVExt namespace |
|---|
| [3922] | 173 | OSGNVEXT_TEXTURE_SHADER, |
|---|
| 174 | OSGNVEXT_VERTEX_PROGRAM, |
|---|
| 175 | OSGNVEXT_REGISTER_COMBINERS, |
|---|
| 176 | |
|---|
| [4039] | 177 | /// osgNVCg namespace |
|---|
| [3922] | 178 | OSGNVCG_PROGRAM, |
|---|
| 179 | |
|---|
| 180 | // osgNVSlang namespace |
|---|
| 181 | OSGNVSLANG_PROGRAM, |
|---|
| 182 | |
|---|
| [3500] | 183 | // osgNVParse |
|---|
| [6842] | 184 | OSGNVPARSE_PROGRAM_PARSER |
|---|
| [10] | 185 | }; |
|---|
| [3488] | 186 | |
|---|
| 187 | /** Simple pairing between an attribute type and the member within that attribute type group.*/ |
|---|
| 188 | typedef std::pair<Type,unsigned int> TypeMemberPair; |
|---|
| [10] | 189 | |
|---|
| [7263] | 190 | StateAttribute(); |
|---|
| [327] | 191 | |
|---|
| [331] | 192 | StateAttribute(const StateAttribute& sa,const CopyOp& copyop=CopyOp::SHALLOW_COPY): |
|---|
| [530] | 193 | Object(sa,copyop) {} |
|---|
| [327] | 194 | |
|---|
| [10] | 195 | |
|---|
| [327] | 196 | /** Clone the type of an attribute, with Object* return type. |
|---|
| 197 | Must be defined by derived classes.*/ |
|---|
| 198 | virtual Object* cloneType() const = 0; |
|---|
| [10] | 199 | |
|---|
| [327] | 200 | /** Clone an attribute, with Object* return type. |
|---|
| 201 | Must be defined by derived classes.*/ |
|---|
| [331] | 202 | virtual Object* clone(const CopyOp&) const = 0; |
|---|
| [327] | 203 | |
|---|
| [3488] | 204 | /** Return true if this and obj are of the same kind of object.*/ |
|---|
| [10] | 205 | virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const StateAttribute*>(obj)!=NULL; } |
|---|
| 206 | |
|---|
| [3488] | 207 | /** Return the name of the attribute's library.*/ |
|---|
| [740] | 208 | virtual const char* libraryName() const { return "osg"; } |
|---|
| 209 | |
|---|
| [3488] | 210 | /** Return the name of the attribute's class type.*/ |
|---|
| [10] | 211 | virtual const char* className() const { return "StateAttribute"; } |
|---|
| 212 | |
|---|
| [8655] | 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 | |
|---|
| [3488] | 221 | /** Return the Type identifier of the attribute's class type.*/ |
|---|
| [1133] | 222 | virtual Type getType() const = 0; |
|---|
| [26] | 223 | |
|---|
| [3488] | 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.*/ |
|---|
| [836] | 231 | virtual bool isTextureAttribute() const { return false; } |
|---|
| [451] | 232 | |
|---|
| [3488] | 233 | /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/ |
|---|
| [26] | 234 | virtual int compare(const StateAttribute& sa) const = 0; |
|---|
| [10] | 235 | |
|---|
| [26] | 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; } |
|---|
| [4091] | 239 | |
|---|
| [836] | 240 | |
|---|
| [4091] | 241 | /** A vector of osg::StateSet pointers which is used to store the parent(s) of this StateAttribute.*/ |
|---|
| 242 | typedef std::vector<StateSet*> ParentList; |
|---|
| [26] | 243 | |
|---|
| [4091] | 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 | */ |
|---|
| [9599] | 259 | inline unsigned int getNumParents() const { return static_cast<unsigned int>(_parents.size()); } |
|---|
| [4091] | 260 | |
|---|
| 261 | |
|---|
| [2773] | 262 | struct ModeUsage |
|---|
| [10] | 263 | { |
|---|
| [4461] | 264 | virtual ~ModeUsage() {} |
|---|
| [2773] | 265 | virtual void usesMode(GLMode mode) = 0; |
|---|
| 266 | virtual void usesTextureMode(GLMode mode) = 0; |
|---|
| 267 | }; |
|---|
| 268 | |
|---|
| [3488] | 269 | /** Return the modes associated with this StateAttribute.*/ |
|---|
| [2773] | 270 | virtual bool getModeUsage(ModeUsage&) const |
|---|
| 271 | { |
|---|
| [39] | 272 | // default to no GLMode's associated with use of the StateAttribute. |
|---|
| [2773] | 273 | return false; |
|---|
| [10] | 274 | } |
|---|
| [4084] | 275 | |
|---|
| [5001] | 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.*/ |
|---|
| [5004] | 279 | virtual bool checkValidityOfAssociatedModes(osg::State&) const |
|---|
| [5001] | 280 | { |
|---|
| 281 | // default to no black listed GLMode's associated with use of the StateAttribute. |
|---|
| 282 | return true; |
|---|
| 283 | } |
|---|
| [4084] | 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.*/ |
|---|
| [4092] | 294 | virtual void operator () (StateAttribute*, NodeVisitor*) {} |
|---|
| [4084] | 295 | }; |
|---|
| 296 | |
|---|
| 297 | /** Set the UpdateCallback which allows users to attach customize the updating of an object during the update traversal.*/ |
|---|
| [4094] | 298 | void setUpdateCallback(Callback* uc); |
|---|
| [4084] | 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.*/ |
|---|
| [4094] | 308 | void setEventCallback(Callback* ec); |
|---|
| [4084] | 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 | |
|---|
| [10] | 316 | |
|---|
| 317 | /** apply the OpenGL state attributes. |
|---|
| [5573] | 318 | * The render info for the current OpenGL context is passed |
|---|
| [10] | 319 | * in to allow the StateAttribute to obtain details on the |
|---|
| 320 | * the current context and state. |
|---|
| 321 | */ |
|---|
| [5573] | 322 | virtual void apply(State&) const {} |
|---|
| 323 | |
|---|
| [5882] | 324 | /** Default to nothing to compile - all state is applied immediately. */ |
|---|
| [3159] | 325 | virtual void compileGLObjects(State&) const {} |
|---|
| [10] | 326 | |
|---|
| [5882] | 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 |
|---|
| [7648] | 331 | object is passed, otherwise release OpenGL objects for all graphics context if |
|---|
| [3159] | 332 | State object pointer NULL.*/ |
|---|
| 333 | virtual void releaseGLObjects(State* =0) const {} |
|---|
| 334 | |
|---|
| 335 | |
|---|
| [10] | 336 | protected: |
|---|
| 337 | |
|---|
| 338 | virtual ~StateAttribute() {} |
|---|
| 339 | |
|---|
| [4091] | 340 | void addParent(osg::StateSet* object); |
|---|
| 341 | void removeParent(osg::StateSet* object); |
|---|
| 342 | |
|---|
| 343 | ParentList _parents; |
|---|
| 344 | friend class osg::StateSet; |
|---|
| 345 | |
|---|
| [4084] | 346 | ref_ptr<Callback> _updateCallback; |
|---|
| 347 | ref_ptr<Callback> _eventCallback; |
|---|
| [10] | 348 | }; |
|---|
| 349 | |
|---|
| [349] | 350 | } |
|---|
| [10] | 351 | |
|---|
| 352 | #endif |
|---|