1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSG_VertexArrayState
#define OSG_VertexArrayState 1
#include <osg/Referenced>
#include <osg/GLExtensions>
#include <osg/Array>
#include <osg/AttributeDispatchers>
namespace osg {
class OSG_EXPORT VertexArrayState : public osg::Referenced
{
public:
VertexArrayState(osg::State* state);
struct ArrayDispatch : public osg::Referenced
{
ArrayDispatch():
array(0),
modifiedCount(0xffffffff),
active(false) {}
virtual bool isVertexAttribDispatch() const { return false; }
virtual const char* className() const = 0; // { return "ArrayDispatch"; }
virtual void enable_and_dispatch(osg::State& /*state*/, const osg::Array* /*new_array*/) {} // = 0;
virtual void enable_and_dispatch(osg::State& /*state*/, const osg::Array* /*new_array*/, const osg::GLBufferObject* /*vbo*/) {} // = 0;
virtual void enable_and_dispatch(osg::State& /*state*/, GLint /*size*/, GLenum /*type*/, GLsizei /*stride*/, const GLvoid * /*ptr*/, GLboolean /*normalized*/) {} // = 0;
virtual void dispatch(osg::State& /*state*/, const osg::Array* /*new_array*/) {} // = 0;
virtual void dispatch(osg::State& /*state*/, const osg::Array* /*new_array*/, const osg::GLBufferObject* /*vbo*/) {} // = 0;
virtual void dispatch(osg::State& /*state*/, GLint /*size*/, GLenum /*type*/, GLsizei /*stride*/, const GLvoid * /*ptr*/, GLboolean /*normalized*/) {} // = 0;
virtual void disable(osg::State& /*state*/) {} // = 0;
const osg::Array* array;
unsigned int modifiedCount;
bool active;
};
typedef std::vector< ref_ptr<ArrayDispatch> > ArrayDispatchList;
void setCurrentVertexBufferObject(osg::GLBufferObject* vbo) { _currentVBO = vbo; }
GLBufferObject* getCurrentVertexBufferObject() { return _currentVBO; }
inline void bindVertexBufferObject(osg::GLBufferObject* vbo)
{
if (vbo->isDirty())
{
vbo->compileBuffer();
_currentVBO = vbo;
}
else if (vbo != _currentVBO)
{
vbo->bindBuffer();
_currentVBO = vbo;
}
}
inline void unbindVertexBufferObject()
{
if (!_currentVBO) return;
_ext->glBindBuffer(GL_ARRAY_BUFFER_ARB,0);
_currentVBO = 0;
}
void setCurrentElementBufferObject(osg::GLBufferObject* ebo) { _currentEBO = ebo; }
GLBufferObject* getCurrentElementBufferObject() { return _currentEBO; }
inline void bindElementBufferObject(osg::GLBufferObject* ebo)
{
if (ebo->isDirty())
{
ebo->compileBuffer();
_currentEBO = ebo;
}
else if (ebo != _currentEBO)
{
ebo->bindBuffer();
_currentEBO = ebo;
}
}
inline void unbindElementBufferObject()
{
if (!_currentEBO) return;
_ext->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB,0);
_currentEBO = 0;
}
void resetBufferObjectPointers() { _currentVBO = 0; _currentEBO = 0; }
bool correctArrayDispatchAssigned(const ArrayDispatch* ad);
void assignAllDispatchers();
void assignVertexArrayDispatcher();
void assignNormalArrayDispatcher();
void assignColorArrayDispatcher();
void assignSecondaryColorArrayDispatcher();
void assignFogCoordArrayDispatcher();
void assignTexCoordArrayDispatcher(unsigned int numUnits);
void assignVertexAttribArrayDispatcher(unsigned int numUnits);
inline void setVertexBufferObjectSupported(bool flag) { _isVertexBufferObjectSupported = flag; }
inline bool isVertexBufferObjectSupported() const { return _isVertexBufferObjectSupported; }
void setArray(ArrayDispatch* vad, osg::State& state, const osg::Array* new_array);
void setArray(ArrayDispatch* vad, osg::State& state, GLint size, GLenum type, GLsizei stride, const GLvoid *ptr, GLboolean normalized);
inline void disable(ArrayDispatch* vad, osg::State& state) { vad->disable(state); vad->array=0; vad->modifiedCount=0xffffffff; vad->active=false; }
void setInterleavedArrays( osg::State& state, GLenum format, GLsizei stride, const GLvoid* pointer);
inline void setVertexArray(osg::State& state, const osg::Array* array) { setArray(_vertexArray.get(), state, array); }
inline void setVertexArray(osg::State& state, GLint size, GLenum type, GLsizei stride, const GLvoid *ptr, GLboolean normalized=GL_FALSE) { setArray(_vertexArray.get(), state, size, type, stride, ptr, normalized); }
inline void disableVertexArray(osg::State& state) { disable(_vertexArray.get(), state); }
inline void setNormalArray(osg::State& state, const osg::Array* array) { setArray(_normalArray.get(), state, array); }
inline void setNormalArray(osg::State& state, GLenum type, GLsizei stride, const GLvoid *ptr, GLboolean normalized=GL_FALSE ) { setArray(_normalArray.get(), state, 3, type, stride, ptr, normalized); }
inline void disableNormalArray(osg::State& state) { disable(_normalArray.get(), state); }
inline void setColorArray(osg::State& state, const osg::Array* array) { setArray(_colorArray.get(), state, array); }
inline void setColorArray(osg::State& state, GLint size, GLenum type, GLsizei stride, const GLvoid *ptr, GLboolean normalized=GL_TRUE ) { setArray(_colorArray.get(), state, size, type, stride, ptr, normalized); }
inline void disableColorArray(osg::State& state) { disable(_colorArray.get(), state); }
inline void setSecondaryColorArray(osg::State& state, const osg::Array* array) { setArray(_secondaryColorArray.get(), state, array); }
inline void disableSecondaryColorArray(osg::State& state) { disable(_secondaryColorArray.get(), state); }
inline void setFogCoordArray(osg::State& state, const osg::Array* array) { setArray(_fogCoordArray.get(), state, array); }
inline void disableFogCoordArray(osg::State& state) { disable(_fogCoordArray.get(), state); }
inline void setTexCoordArray(osg::State& state, unsigned int unit, const osg::Array* array) { setArray(_texCoordArrays[unit].get(), state, array); }
inline void setTexCoordArray(osg::State& state, unsigned int unit, GLint size, GLenum type, GLsizei stride, const GLvoid *ptr, GLboolean normalized=GL_FALSE ) { setArray(_texCoordArrays[unit].get(), state, size, type, stride, ptr, normalized); }
inline void disableTexCoordArray(osg::State& state, unsigned int unit) { disable(_texCoordArrays[unit].get(),state); }
inline void disableTexCoordArrayAboveAndIncluding(osg::State& state, unsigned int index);
inline void setVertexAttribArray(osg::State& state, unsigned int unit, const osg::Array* array) { setArray(_vertexAttribArrays[unit].get(), state, array); }
inline void disableVertexAttribArray(osg::State& state, unsigned int unit) { disable(_vertexAttribArrays[unit].get(), state); }
inline void disableVertexAttribArrayAboveAndIncluding(osg::State& state, unsigned int index);
/** Mark all the vertex attributes as being disabled but leave the disabling till a later call to applyDisablingOfVertexAttributes.*/
inline void lazyDisablingOfVertexAttributes();
/** Disable all the vertex attributes that have been marked as to be disabled.*/
inline void applyDisablingOfVertexAttributes(osg::State& state);
// Verex Array Object methods.
void generateVertexArrayObject();
void deleteVertexArrayObject();
GLuint getVertexArrayObject() const { return _vertexArrayObject; }
void setRequiresSetArrays(bool flag) { _requiresSetArrays = flag; }
bool getRequiresSetArrays() const { return _requiresSetArrays; }
void dirty();
void release();
public:
virtual ~VertexArrayState();
// osg::GLBufferObject* getGLBufferObject(osg::Array* array);
osg::State* _state;
osg::ref_ptr<ObserverSet> _stateObserverSet;
osg::ref_ptr<osg::GLExtensions> _ext;
bool _isVertexBufferObjectSupported;
GLuint _vertexArrayObject;
osg::ref_ptr<ArrayDispatch> _vertexArray;
osg::ref_ptr<ArrayDispatch> _normalArray;
osg::ref_ptr<ArrayDispatch> _colorArray;
osg::ref_ptr<ArrayDispatch> _secondaryColorArray;
osg::ref_ptr<ArrayDispatch> _fogCoordArray;
ArrayDispatchList _texCoordArrays;
ArrayDispatchList _vertexAttribArrays;
typedef std::vector<ArrayDispatch*> ActiveDispatchers;
ActiveDispatchers _activeDispatchers;
ActiveDispatchers _previous_activeDispatchers;
GLBufferObject* _currentVBO;
GLBufferObject* _currentEBO;
bool _requiresSetArrays;
};
class OSG_EXPORT VertexArrayStateList
{
public:
VertexArrayStateList();
VertexArrayStateList& operator = (const VertexArrayStateList& rhs);
inline void clear() { _array.clear(); }
inline bool empty() const { return _array.empty(); }
inline unsigned int size() const { return _array.size(); }
inline void resize(unsigned int newSize) { _array.resize(newSize); }
inline ref_ptr<VertexArrayState>& operator[] (unsigned int pos)
{
// automatically resize array.
if (_array.size()<=pos)
_array.resize(pos+1,0);
return _array[pos];
}
inline const ref_ptr<VertexArrayState>& operator[] (unsigned int pos) const
{
// automatically resize array.
if (_array.size()<=pos)
_array.resize(pos+1,0);
return _array[pos];
}
void assignAllDispatchers();
void assignVertexArrayDispatcher();
void assignNormalArrayDispatcher();
void assignColorArrayDispatcher();
void assignSecondaryColorArrayDispatcher();
void assignFogCoordArrayDispatcher();
void assignTexCoordArrayDispatcher(unsigned int numUnits);
void assignVertexAttribArrayDispatcher(unsigned int numUnits);
protected:
typedef std::vector< osg::ref_ptr<VertexArrayState> > Array;
mutable Array _array;
};
inline void VertexArrayState::lazyDisablingOfVertexAttributes()
{
_activeDispatchers.swap(_previous_activeDispatchers);
_activeDispatchers.clear();
for(ActiveDispatchers::iterator itr = _previous_activeDispatchers.begin();
itr != _previous_activeDispatchers.end();
++itr)
{
ArrayDispatch* ad = (*itr);
// ad->array = 0;
ad->active = false;
}
}
inline void VertexArrayState::applyDisablingOfVertexAttributes(osg::State& state)
{
for(ActiveDispatchers::iterator itr = _previous_activeDispatchers.begin();
itr != _previous_activeDispatchers.end();
++itr)
{
ArrayDispatch* ad = (*itr);
if (!ad->active)
{
ad->disable(state);
ad->array = 0;
ad->modifiedCount = 0xffffffff;
}
}
_previous_activeDispatchers.clear();
}
inline void VertexArrayState::disableTexCoordArrayAboveAndIncluding(osg::State& state, unsigned int index)
{
for(unsigned int i=index; i<_texCoordArrays.size(); ++i)
{
disable(_texCoordArrays[i].get(), state);
}
}
inline void VertexArrayState::disableVertexAttribArrayAboveAndIncluding(osg::State& state, unsigned int index)
{
for(unsigned int i=index; i<_vertexAttribArrays.size(); ++i)
{
disable(_vertexAttribArrays[i].get(), state);
}
}
}
#endif
|