OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
StateAttribute.h
Go to the documentation of this file.
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/Callback>
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 
33 namespace osg {
34 
35 
36 // forward declare NodeVisitor, State & StateSet
37 class NodeVisitor;
38 class State;
39 class ShaderComposer;
40 class StateSet;
41 class Texture;
42 
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 
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 
69 #define COMPARE_StateAttribute_Parameter(parameter) \
70  if (parameter<rhs.parameter) return -1; \
71  if (rhs.parameter<parameter) return 1;
72 
73 
77 {
78  public :
79 
81  typedef GLenum GLMode;
85  typedef unsigned int GLModeValue;
89  typedef unsigned int OverrideValue;
90 
98  enum Values
99  {
101  OFF = 0x0,
103  ON = 0x1,
105  OVERRIDE = 0x2,
107  PROTECTED = 0x4,
109  INHERIT = 0x8
110  };
111 
113  // typedef unsigned int Type;
114 
123  enum Type
124  {
126 
136 
138 
170 
174 
177 
178  // osgNVExt namespace
182 
185 
186  // osgNVSlang namespace
188 
189  // osgNVParse
191 
194 
196 
198 
200 
202 
204 
205  CAPABILITY = 100
206  };
207 
209  typedef std::pair<Type,unsigned int> TypeMemberPair;
210 
211  StateAttribute();
212 
214  Object(sa,copyop),
215  _shaderComponent(sa._shaderComponent),
216  _updateCallback(copyop(sa._updateCallback.get())),
217  _eventCallback(copyop(sa._eventCallback.get()))
218  {}
219 
220 
223  virtual Object* cloneType() const = 0;
224 
227  virtual Object* clone(const CopyOp&) const = 0;
228 
230  virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const StateAttribute*>(obj)!=NULL; }
231 
233  virtual const char* libraryName() const { return "osg"; }
234 
236  virtual const char* className() const { return "StateAttribute"; }
237 
238 
241  virtual StateAttribute* asStateAttribute() { return this; }
242 
245  virtual const StateAttribute* asStateAttribute() const { return this; }
246 
248  virtual Texture* asTexture() { return 0; }
249 
251  virtual const Texture* asTexture() const { return 0; }
252 
253 
255  virtual Type getType() const = 0;
256 
258  virtual unsigned int getMember() const { return 0; }
259 
261  inline TypeMemberPair getTypeMemberPair() const { return TypeMemberPair(getType(),getMember()); }
262 
264  virtual bool isTextureAttribute() const { return false; }
265 
267  virtual int compare(const StateAttribute& sa) const = 0;
268 
269  bool operator < (const StateAttribute& rhs) const { return compare(rhs)<0; }
270  bool operator == (const StateAttribute& rhs) const { return compare(rhs)==0; }
271  bool operator != (const StateAttribute& rhs) const { return compare(rhs)!=0; }
272 
273 
275  typedef std::vector<StateSet*> ParentList;
276 
278  inline const ParentList& getParents() const { return _parents; }
279 
280  inline StateSet* getParent(unsigned int i) { return _parents[i]; }
286  inline const StateSet* getParent(unsigned int i) const { return _parents[i]; }
287 
292  inline unsigned int getNumParents() const { return static_cast<unsigned int>(_parents.size()); }
293 
294  void setShaderComponent(ShaderComponent* sc) { _shaderComponent = sc; }
295  ShaderComponent* getShaderComponent() { return _shaderComponent.get(); }
296  const ShaderComponent* getShaderComponent() const { return _shaderComponent.get(); }
297 
298  struct ModeUsage
299  {
300  virtual ~ModeUsage() {}
301  virtual void usesMode(GLMode mode) = 0;
302  virtual void usesTextureMode(GLMode mode) = 0;
303  };
304 
306  virtual bool getModeUsage(ModeUsage&) const
307  {
308  // default to no GLMode's associated with use of the StateAttribute.
309  return false;
310  }
311 
316  {
317  // default to no black listed GLMode's associated with use of the StateAttribute.
318  return true;
319  }
320 
321  // provide callback for backwards compatibility.
323 
325  void setUpdateCallback(StateAttributeCallback* uc);
326 
328  StateAttributeCallback* getUpdateCallback() { return _updateCallback.get(); }
329 
331  const StateAttributeCallback* getUpdateCallback() const { return _updateCallback.get(); }
332 
333 
335  void setEventCallback(StateAttributeCallback* ec);
336 
338  StateAttributeCallback* getEventCallback() { return _eventCallback.get(); }
339 
341  const StateAttributeCallback* getEventCallback() const { return _eventCallback.get(); }
342 
343 
349  virtual void apply(State&) const {}
350 
352  virtual void compileGLObjects(State&) const {}
353 
355  virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/) {}
356 
360  virtual void releaseGLObjects(State* =0) const {}
361 
362 
363  protected:
364 
365  virtual ~StateAttribute() {}
366 
367  void addParent(osg::StateSet* object);
368  void removeParent(osg::StateSet* object);
369 
370  ParentList _parents;
371  friend class osg::StateSet;
372 
374 
377 };
378 
379 }
380 
381 #endif
StateAttribute(const StateAttribute &sa, const CopyOp &copyop=CopyOp::SHALLOW_COPY)
#define OSG_EXPORT
Definition: Export.h:43
virtual void apply(State &) const
TypeMemberPair getTypeMemberPair() const
virtual bool checkValidityOfAssociatedModes(osg::State &) const
#define NULL
Definition: Export.h:59
osg::StateAttributeCallback Callback
virtual const char * className() const
virtual StateAttribute * asStateAttribute()
const StateAttributeCallback * getEventCallback() const
unsigned int getNumParents() const
ref_ptr< StateAttributeCallback > _updateCallback
virtual const char * libraryName() const
StateAttributeCallback * getEventCallback()
StateAttributeCallback * getUpdateCallback()
ShaderComponent * getShaderComponent()
virtual const StateAttribute * asStateAttribute() const
std::vector< StateSet * > ParentList
T * clone(const T *t, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
Definition: Object.h:242
unsigned int OverrideValue
const ShaderComponent * getShaderComponent() const
ref_ptr< ShaderComponent > _shaderComponent
virtual void releaseGLObjects(State *=0) const
virtual void compileGLObjects(State &) const
T * cloneType(const T *t)
Definition: Object.h:284
unsigned int GLModeValue
void setShaderComponent(ShaderComponent *sc)
virtual Texture * asTexture()
virtual bool isTextureAttribute() const
const ParentList & getParents() const
const StateAttributeCallback * getUpdateCallback() const
StateSet * getParent(unsigned int i)
virtual bool getModeUsage(ModeUsage &) const
ref_ptr< StateAttributeCallback > _eventCallback
virtual bool isSameKindAs(const Object *obj) const
Definition: AlphaFunc.h:19
const StateSet * getParent(unsigned int i) const
virtual const Texture * asTexture() const
std::pair< Type, unsigned int > TypeMemberPair
virtual unsigned int getMember() const
virtual void resizeGLObjectBuffers(unsigned int)