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