| 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_ArrayDispatchers |
|---|
| 15 | #define OSG_ArrayDispatchers 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/ref_ptr> |
|---|
| 18 | #include <osg/Array> |
|---|
| 19 | #include <osg/Matrixd> |
|---|
| 20 | #include <osg/GLBeginEndAdapter> |
|---|
| 21 | |
|---|
| 22 | namespace osg { |
|---|
| 23 | |
|---|
| 24 | // forward declare |
|---|
| 25 | class State; |
|---|
| 26 | class AttributeDispatchMap; |
|---|
| 27 | |
|---|
| 28 | struct AttributeDispatch : public osg::Referenced |
|---|
| 29 | { |
|---|
| 30 | virtual void assign(const GLvoid*, const IndexArray*) {} |
|---|
| 31 | virtual void operator() (unsigned int) {}; |
|---|
| 32 | }; |
|---|
| 33 | |
|---|
| 34 | /** Helper class for managing the dispatch to OpenGL of various attribute arrays such as stored in osg::Geometry.*/ |
|---|
| 35 | class OSG_EXPORT ArrayDispatchers : public osg::Referenced |
|---|
| 36 | { |
|---|
| 37 | public: |
|---|
| 38 | |
|---|
| 39 | ArrayDispatchers(); |
|---|
| 40 | ~ArrayDispatchers(); |
|---|
| 41 | |
|---|
| 42 | void setState(osg::State* state); |
|---|
| 43 | |
|---|
| 44 | AttributeDispatch* vertexDispatcher(Array* array, IndexArray* indices); |
|---|
| 45 | AttributeDispatch* normalDispatcher(Array* array, IndexArray* indices); |
|---|
| 46 | AttributeDispatch* colorDispatcher(Array* array, IndexArray* indices); |
|---|
| 47 | AttributeDispatch* secondaryColorDispatcher(Array* array, IndexArray* indices); |
|---|
| 48 | AttributeDispatch* fogCoordDispatcher(Array* array, IndexArray* indices); |
|---|
| 49 | AttributeDispatch* texCoordDispatcher(unsigned int unit, Array* array, IndexArray* indices); |
|---|
| 50 | AttributeDispatch* vertexAttribDispatcher(unsigned int unit, Array* array, IndexArray* indices); |
|---|
| 51 | |
|---|
| 52 | void reset(); |
|---|
| 53 | |
|---|
| 54 | void setUseGLBeginEndAdapter(bool flag) { _useGLBeginEndAdapter = flag; } |
|---|
| 55 | bool getUseGLBeginEndAdapter() const { return _useGLBeginEndAdapter; } |
|---|
| 56 | |
|---|
| 57 | void setUseVertexAttribAlias(bool flag) { _useVertexAttribAlias = flag; } |
|---|
| 58 | bool getUseVertexAttribAlias() const { return _useVertexAttribAlias; } |
|---|
| 59 | |
|---|
| 60 | void activate(unsigned int binding, AttributeDispatch* at) |
|---|
| 61 | { |
|---|
| 62 | if (at) _activeDispatchList[binding].push_back(at); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | void activateVertexArray(unsigned int binding, osg::Array* array, osg::IndexArray* indices) { if (binding && array) activate(binding, vertexDispatcher(array, indices)); } |
|---|
| 66 | void activateColorArray(unsigned int binding, osg::Array* array, osg::IndexArray* indices) { if (binding && array) activate(binding, colorDispatcher(array, indices)); } |
|---|
| 67 | void activateNormalArray(unsigned int binding, osg::Array* array, osg::IndexArray* indices) { if (binding && array) activate(binding, normalDispatcher(array, indices)); } |
|---|
| 68 | void activateSecondaryColorArray(unsigned int binding, osg::Array* array, osg::IndexArray* indices) { if (binding && array) activate(binding, secondaryColorDispatcher(array, indices)); } |
|---|
| 69 | void activateFogCoordArray(unsigned int binding, osg::Array* array, osg::IndexArray* indices) { if (binding && array) activate(binding, fogCoordDispatcher(array, indices)); } |
|---|
| 70 | void activateTexCoordArray(unsigned int binding, unsigned int unit, osg::Array* array, osg::IndexArray* indices) { if (binding && array) activate(binding, texCoordDispatcher(unit, array, indices)); } |
|---|
| 71 | void activateVertexAttribArray(unsigned int binding, unsigned int unit, osg::Array* array, osg::IndexArray* indices) { if (binding && array) activate(binding, vertexAttribDispatcher(unit, array, indices)); } |
|---|
| 72 | |
|---|
| 73 | void dispatch(unsigned int binding, unsigned int index) |
|---|
| 74 | { |
|---|
| 75 | AttributeDispatchList& ad = _activeDispatchList[binding]; |
|---|
| 76 | for(AttributeDispatchList::iterator itr = ad.begin(); |
|---|
| 77 | itr != ad.end(); |
|---|
| 78 | ++itr) |
|---|
| 79 | { |
|---|
| 80 | (*(*itr))(index); |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | bool active(unsigned int binding) const { return !_activeDispatchList[binding].empty(); } |
|---|
| 85 | |
|---|
| 86 | void Begin(GLenum mode) |
|---|
| 87 | { |
|---|
| 88 | #ifdef OSG_GL1_AVAILABLE |
|---|
| 89 | if (_useGLBeginEndAdapter) _glBeginEndAdapter->Begin(mode); |
|---|
| 90 | else ::glBegin(mode); |
|---|
| 91 | #else |
|---|
| 92 | _glBeginEndAdapter->Begin(mode); |
|---|
| 93 | #endif |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | void End() |
|---|
| 97 | { |
|---|
| 98 | #ifdef OSG_GL1_AVAILABLE |
|---|
| 99 | if (_useGLBeginEndAdapter) _glBeginEndAdapter->End(); |
|---|
| 100 | else ::glEnd(); |
|---|
| 101 | #else |
|---|
| 102 | _glBeginEndAdapter->End(); |
|---|
| 103 | #endif |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | protected: |
|---|
| 107 | |
|---|
| 108 | void init(); |
|---|
| 109 | |
|---|
| 110 | void assignTexCoordDispatchers(unsigned int unit); |
|---|
| 111 | void assignVertexAttribDispatchers(unsigned int unit); |
|---|
| 112 | |
|---|
| 113 | bool _initialized; |
|---|
| 114 | State* _state; |
|---|
| 115 | GLBeginEndAdapter* _glBeginEndAdapter; |
|---|
| 116 | |
|---|
| 117 | AttributeDispatchMap* _vertexDispatchers; |
|---|
| 118 | AttributeDispatchMap* _normalDispatchers; |
|---|
| 119 | AttributeDispatchMap* _colorDispatchers; |
|---|
| 120 | AttributeDispatchMap* _secondaryColorDispatchers; |
|---|
| 121 | AttributeDispatchMap* _fogCoordDispatchers; |
|---|
| 122 | |
|---|
| 123 | typedef std::vector<AttributeDispatchMap*> AttributeDispatchMapList; |
|---|
| 124 | AttributeDispatchMapList _texCoordDispatchers; |
|---|
| 125 | AttributeDispatchMapList _vertexAttribDispatchers; |
|---|
| 126 | |
|---|
| 127 | typedef std::vector<AttributeDispatch*> AttributeDispatchList; |
|---|
| 128 | |
|---|
| 129 | typedef std::vector<AttributeDispatchList> ActiveDispatchList; |
|---|
| 130 | ActiveDispatchList _activeDispatchList; |
|---|
| 131 | |
|---|
| 132 | bool _useVertexAttribAlias; |
|---|
| 133 | bool _useGLBeginEndAdapter; |
|---|
| 134 | }; |
|---|
| 135 | |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | #endif |
|---|