AttributeDispatchers 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #ifndef OSG_ATTRIBUTEDISPATCHERS
  14. #define OSG_ATTRIBUTEDISPATCHERS 1
  15. #include <osg/ref_ptr>
  16. #include <osg/Array>
  17. #include <osg/Matrixd>
  18. namespace osg {
  19. // forward declare
  20. class State;
  21. class AttributeDispatchMap;
  22. struct AttributeDispatch : public osg::Referenced
  23. {
  24. virtual void assign(const GLvoid*) {}
  25. virtual void operator() (unsigned int) {};
  26. };
  27. /** Helper class for managing the dispatch to OpenGL of various attribute arrays such as stored in osg::Geometry.*/
  28. class OSG_EXPORT AttributeDispatchers : public osg::Referenced
  29. {
  30. public:
  31. AttributeDispatchers();
  32. ~AttributeDispatchers();
  33. void setState(osg::State* state);
  34. void reset();
  35. void setUseVertexAttribAlias(bool flag) { _useVertexAttribAlias = flag; }
  36. bool getUseVertexAttribAlias() const { return _useVertexAttribAlias; }
  37. #define DISPATCH_OR_ACTIVATE(array, dispatcher) \
  38. if (array) { \
  39. unsigned int binding = array->getBinding(); \
  40. if (binding==osg::Array::BIND_OVERALL) \
  41. { \
  42. AttributeDispatch* at = dispatcher; \
  43. if (at) (*at)(0); \
  44. } \
  45. else if (binding==osg::Array:: BIND_PER_PRIMITIVE_SET) \
  46. { \
  47. AttributeDispatch* at = dispatcher; \
  48. if (at) _activeDispatchList.push_back(at); \
  49. } \
  50. }
  51. void activateColorArray(osg::Array* array) { DISPATCH_OR_ACTIVATE(array, colorDispatcher(array)); }
  52. void activateNormalArray(osg::Array* array) { DISPATCH_OR_ACTIVATE(array, normalDispatcher(array)); }
  53. void activateSecondaryColorArray(osg::Array* array) { DISPATCH_OR_ACTIVATE(array, secondaryColorDispatcher(array)); }
  54. void activateFogCoordArray(osg::Array* array) { DISPATCH_OR_ACTIVATE(array, fogCoordDispatcher(array)); }
  55. void activateVertexAttribArray(unsigned int unit, osg::Array* array) { DISPATCH_OR_ACTIVATE(array, vertexAttribDispatcher(unit , array)); }
  56. AttributeDispatch* normalDispatcher(Array* array);
  57. AttributeDispatch* colorDispatcher(Array* array);
  58. AttributeDispatch* secondaryColorDispatcher(Array* array);
  59. AttributeDispatch* fogCoordDispatcher(Array* array);
  60. AttributeDispatch* vertexAttribDispatcher(unsigned int unit, Array* array);
  61. void dispatch(unsigned int index)
  62. {
  63. for(AttributeDispatchList::iterator itr = _activeDispatchList.begin();
  64. itr != _activeDispatchList.end();
  65. ++itr)
  66. {
  67. (*(*itr))(index);
  68. }
  69. }
  70. bool active() const { return !_activeDispatchList.empty(); }
  71. protected:
  72. void init();
  73. void assignTexCoordDispatchers(unsigned int unit);
  74. void assignVertexAttribDispatchers(unsigned int unit);
  75. bool _initialized;
  76. State* _state;
  77. AttributeDispatchMap* _normalDispatchers;
  78. AttributeDispatchMap* _colorDispatchers;
  79. AttributeDispatchMap* _secondaryColorDispatchers;
  80. AttributeDispatchMap* _fogCoordDispatchers;
  81. typedef std::vector<AttributeDispatchMap*> AttributeDispatchMapList;
  82. AttributeDispatchMapList _vertexAttribDispatchers;
  83. typedef std::vector<AttributeDispatch*> AttributeDispatchList;
  84. AttributeDispatchList _activeDispatchList;
  85. bool _useVertexAttribAlias;
  86. };
  87. }
  88. #endif