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