| 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_PRIMITIVESET |
|---|
| 15 | #define OSG_PRIMITIVESET 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/GL> |
|---|
| 18 | #include <osg/Object> |
|---|
| 19 | #include <osg/buffered_value> |
|---|
| 20 | #include <osg/Vec2> |
|---|
| 21 | #include <osg/Vec3> |
|---|
| 22 | #include <osg/Vec4> |
|---|
| 23 | #include <osg/Vec2d> |
|---|
| 24 | #include <osg/Vec3d> |
|---|
| 25 | #include <osg/Vec4d> |
|---|
| 26 | #include <osg/MixinVector> |
|---|
| 27 | |
|---|
| 28 | #include <osg/BufferObject> |
|---|
| 29 | |
|---|
| 30 | #include <vector> |
|---|
| 31 | |
|---|
| 32 | namespace osg { |
|---|
| 33 | |
|---|
| 34 | typedef MixinVector<GLsizei> VectorGLsizei; |
|---|
| 35 | typedef MixinVector<GLubyte> VectorGLubyte; |
|---|
| 36 | typedef MixinVector<GLushort> VectorGLushort; |
|---|
| 37 | typedef MixinVector<GLuint> VectorGLuint; |
|---|
| 38 | |
|---|
| 39 | class State; |
|---|
| 40 | |
|---|
| 41 | /** A \c PrimitiveFunctor is used (in conjunction with |
|---|
| 42 | * <tt>osg::Drawable::accept (PrimitiveFunctor&)</tt>) to get access to the |
|---|
| 43 | * primitives that compose the things drawn by OSG. |
|---|
| 44 | * <p>If \c osg::Drawable::accept() is called with a \c PrimitiveFunctor |
|---|
| 45 | * parameter, the \c Drawable will "pretend" it is drawing itself, but instead |
|---|
| 46 | * of calling real OpenGL functions, it will call <tt>PrimitiveFunctor</tt>'s |
|---|
| 47 | * member functions that "mimic" the OpenGL calls. |
|---|
| 48 | * <p>Concrete subclasses of \c PrimitiveFunctor must implement these methods |
|---|
| 49 | * so that they performs whatever they want. |
|---|
| 50 | */ |
|---|
| 51 | class PrimitiveFunctor |
|---|
| 52 | { |
|---|
| 53 | public: |
|---|
| 54 | |
|---|
| 55 | virtual ~PrimitiveFunctor() {} |
|---|
| 56 | |
|---|
| 57 | /** Sets the array of vertices used to describe the primitives. Somehow |
|---|
| 58 | * mimics the OpenGL \c glVertexPointer() function. |
|---|
| 59 | */ |
|---|
| 60 | virtual void setVertexArray(unsigned int count,const Vec2* vertices) = 0; |
|---|
| 61 | |
|---|
| 62 | /** Sets the array of vertices used to describe the primitives. Somehow |
|---|
| 63 | * mimics the OpenGL \c glVertexPointer() function. |
|---|
| 64 | */ |
|---|
| 65 | virtual void setVertexArray(unsigned int count,const Vec3* vertices) = 0; |
|---|
| 66 | |
|---|
| 67 | /** Sets the array of vertices used to describe the primitives. Somehow |
|---|
| 68 | * mimics the OpenGL \c glVertexPointer() function. |
|---|
| 69 | */ |
|---|
| 70 | virtual void setVertexArray(unsigned int count,const Vec4* vertices) = 0; |
|---|
| 71 | |
|---|
| 72 | /** Sets the array of vertices used to describe the primitives. Somehow |
|---|
| 73 | * mimics the OpenGL \c glVertexPointer() function. |
|---|
| 74 | */ |
|---|
| 75 | virtual void setVertexArray(unsigned int count,const Vec2d* vertices) = 0; |
|---|
| 76 | |
|---|
| 77 | /** Sets the array of vertices used to describe the primitives. Somehow |
|---|
| 78 | * mimics the OpenGL \c glVertexPointer() function. |
|---|
| 79 | */ |
|---|
| 80 | virtual void setVertexArray(unsigned int count,const Vec3d* vertices) = 0; |
|---|
| 81 | |
|---|
| 82 | /** Sets the array of vertices used to describe the primitives. Somehow |
|---|
| 83 | * mimics the OpenGL \c glVertexPointer() function. |
|---|
| 84 | */ |
|---|
| 85 | virtual void setVertexArray(unsigned int count,const Vec4d* vertices) = 0; |
|---|
| 86 | |
|---|
| 87 | /// Mimics the OpenGL \c glDrawArrays() function. |
|---|
| 88 | virtual void drawArrays(GLenum mode,GLint first,GLsizei count) = 0; |
|---|
| 89 | |
|---|
| 90 | /// Mimics the OpenGL \c glDrawElements() function. |
|---|
| 91 | virtual void drawElements(GLenum mode,GLsizei count,const GLubyte* indices) = 0; |
|---|
| 92 | |
|---|
| 93 | /// Mimics the OpenGL \c glDrawElements() function. |
|---|
| 94 | virtual void drawElements(GLenum mode,GLsizei count,const GLushort* indices) = 0; |
|---|
| 95 | |
|---|
| 96 | /// Mimics the OpenGL \c glDrawElements() function. |
|---|
| 97 | virtual void drawElements(GLenum mode,GLsizei count,const GLuint* indices) = 0; |
|---|
| 98 | |
|---|
| 99 | /// Mimics the OpenGL \c glBegin() function. |
|---|
| 100 | virtual void begin(GLenum mode) = 0; |
|---|
| 101 | |
|---|
| 102 | /// Mimics the OpenGL \c glVertex() "family of functions". |
|---|
| 103 | virtual void vertex(const Vec2& vert) = 0; |
|---|
| 104 | |
|---|
| 105 | /// Mimics the OpenGL \c glVertex() "family of functions". |
|---|
| 106 | virtual void vertex(const Vec3& vert) = 0; |
|---|
| 107 | |
|---|
| 108 | /// Mimics the OpenGL \c glVertex() "family of functions". |
|---|
| 109 | virtual void vertex(const Vec4& vert) = 0; |
|---|
| 110 | |
|---|
| 111 | /// Mimics the OpenGL \c glVertex() "family of functions". |
|---|
| 112 | virtual void vertex(float x,float y) = 0; |
|---|
| 113 | |
|---|
| 114 | /// Mimics the OpenGL \c glVertex() "family of functions". |
|---|
| 115 | virtual void vertex(float x,float y,float z) = 0; |
|---|
| 116 | |
|---|
| 117 | /// Mimics the OpenGL \c glVertex() "family of functions". |
|---|
| 118 | virtual void vertex(float x,float y,float z,float w) = 0; |
|---|
| 119 | |
|---|
| 120 | /// Mimics the OpenGL \c glEnd() function. |
|---|
| 121 | virtual void end() = 0; |
|---|
| 122 | }; |
|---|
| 123 | |
|---|
| 124 | class PrimitiveIndexFunctor |
|---|
| 125 | { |
|---|
| 126 | public: |
|---|
| 127 | |
|---|
| 128 | virtual ~PrimitiveIndexFunctor() {} |
|---|
| 129 | |
|---|
| 130 | virtual void setVertexArray(unsigned int count,const Vec2* vertices) = 0; |
|---|
| 131 | virtual void setVertexArray(unsigned int count,const Vec3* vertices) = 0; |
|---|
| 132 | virtual void setVertexArray(unsigned int count,const Vec4* vertices) = 0; |
|---|
| 133 | |
|---|
| 134 | virtual void setVertexArray(unsigned int count,const Vec2d* vertices) = 0; |
|---|
| 135 | virtual void setVertexArray(unsigned int count,const Vec3d* vertices) = 0; |
|---|
| 136 | virtual void setVertexArray(unsigned int count,const Vec4d* vertices) = 0; |
|---|
| 137 | |
|---|
| 138 | virtual void drawArrays(GLenum mode,GLint first,GLsizei count) = 0; |
|---|
| 139 | virtual void drawElements(GLenum mode,GLsizei count,const GLubyte* indices) = 0; |
|---|
| 140 | virtual void drawElements(GLenum mode,GLsizei count,const GLushort* indices) = 0; |
|---|
| 141 | virtual void drawElements(GLenum mode,GLsizei count,const GLuint* indices) = 0; |
|---|
| 142 | |
|---|
| 143 | virtual void begin(GLenum mode) = 0; |
|---|
| 144 | virtual void vertex(unsigned int pos) = 0; |
|---|
| 145 | virtual void end() = 0; |
|---|
| 146 | }; |
|---|
| 147 | |
|---|
| 148 | class DrawElements; |
|---|
| 149 | |
|---|
| 150 | class OSG_EXPORT PrimitiveSet : public Object |
|---|
| 151 | { |
|---|
| 152 | public: |
|---|
| 153 | |
|---|
| 154 | enum Type |
|---|
| 155 | { |
|---|
| 156 | PrimitiveType, |
|---|
| 157 | DrawArraysPrimitiveType, |
|---|
| 158 | DrawArrayLengthsPrimitiveType, |
|---|
| 159 | DrawElementsUBytePrimitiveType, |
|---|
| 160 | DrawElementsUShortPrimitiveType, |
|---|
| 161 | DrawElementsUIntPrimitiveType |
|---|
| 162 | }; |
|---|
| 163 | |
|---|
| 164 | enum Mode |
|---|
| 165 | { |
|---|
| 166 | POINTS = GL_POINTS, |
|---|
| 167 | LINES = GL_LINES, |
|---|
| 168 | LINE_STRIP = GL_LINE_STRIP, |
|---|
| 169 | LINE_LOOP = GL_LINE_LOOP, |
|---|
| 170 | TRIANGLES = GL_TRIANGLES, |
|---|
| 171 | TRIANGLE_STRIP = GL_TRIANGLE_STRIP, |
|---|
| 172 | TRIANGLE_FAN = GL_TRIANGLE_FAN, |
|---|
| 173 | QUADS = GL_QUADS, |
|---|
| 174 | QUAD_STRIP = GL_QUAD_STRIP, |
|---|
| 175 | POLYGON = GL_POLYGON |
|---|
| 176 | }; |
|---|
| 177 | |
|---|
| 178 | PrimitiveSet(Type primType=PrimitiveType,GLenum mode=0, int numInstances=0): |
|---|
| 179 | _primitiveType(primType), |
|---|
| 180 | _numInstances(numInstances), |
|---|
| 181 | _mode(mode), |
|---|
| 182 | _modifiedCount(0), |
|---|
| 183 | _rangeModifiedCount(0) {} |
|---|
| 184 | |
|---|
| 185 | PrimitiveSet(const PrimitiveSet& prim,const CopyOp& copyop=CopyOp::SHALLOW_COPY): |
|---|
| 186 | Object(prim,copyop), |
|---|
| 187 | _primitiveType(prim._primitiveType), |
|---|
| 188 | _numInstances(prim._numInstances), |
|---|
| 189 | _mode(prim._mode), |
|---|
| 190 | _modifiedCount(0), |
|---|
| 191 | _rangeModifiedCount(0) {} |
|---|
| 192 | |
|---|
| 193 | virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const PrimitiveSet*>(obj)!=NULL; } |
|---|
| 194 | virtual const char* libraryName() const { return "osg"; } |
|---|
| 195 | virtual const char* className() const { return "PrimitiveSet"; } |
|---|
| 196 | |
|---|
| 197 | Type getType() const { return _primitiveType; } |
|---|
| 198 | virtual const GLvoid* getDataPointer() const { return 0; } |
|---|
| 199 | virtual unsigned int getTotalDataSize() const { return 0; } |
|---|
| 200 | virtual bool supportsBufferObject() const { return false; } |
|---|
| 201 | |
|---|
| 202 | virtual DrawElements* getDrawElements() { return 0; } |
|---|
| 203 | virtual const DrawElements* getDrawElements() const { return 0; } |
|---|
| 204 | |
|---|
| 205 | void setNumInstances(int n) { _numInstances = n; } |
|---|
| 206 | int getNumInstances() const { return _numInstances; } |
|---|
| 207 | |
|---|
| 208 | void setMode(GLenum mode) { _mode = mode; } |
|---|
| 209 | GLenum getMode() const { return _mode; } |
|---|
| 210 | |
|---|
| 211 | virtual void draw(State& state, bool useVertexBufferObjects) const = 0; |
|---|
| 212 | |
|---|
| 213 | virtual void accept(PrimitiveFunctor& functor) const = 0; |
|---|
| 214 | virtual void accept(PrimitiveIndexFunctor& functor) const = 0; |
|---|
| 215 | |
|---|
| 216 | virtual unsigned int index(unsigned int pos) const = 0; |
|---|
| 217 | virtual unsigned int getNumIndices() const = 0; |
|---|
| 218 | virtual void offsetIndices(int offset) = 0; |
|---|
| 219 | |
|---|
| 220 | virtual unsigned int getNumPrimitives() const; |
|---|
| 221 | |
|---|
| 222 | /** Dirty the primitive, which increments the modified count, to force buffer objects to update. */ |
|---|
| 223 | virtual void dirty() { ++_modifiedCount; } |
|---|
| 224 | |
|---|
| 225 | /** Set the modified count value.*/ |
|---|
| 226 | inline void setModifiedCount(unsigned int value) { _modifiedCount=value; } |
|---|
| 227 | |
|---|
| 228 | /** Get modified count value.*/ |
|---|
| 229 | inline unsigned int getModifiedCount() const { return _modifiedCount; } |
|---|
| 230 | |
|---|
| 231 | /** Resize any per context GLObject buffers to specified size. */ |
|---|
| 232 | virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/) {} |
|---|
| 233 | |
|---|
| 234 | /** If State is non-zero, this function releases OpenGL objects for |
|---|
| 235 | * the specified graphics context. Otherwise, releases OpenGL objects |
|---|
| 236 | * for all graphics contexts. */ |
|---|
| 237 | virtual void releaseGLObjects(State* /*state*/=0) const {} |
|---|
| 238 | |
|---|
| 239 | virtual void computeRange() const {} |
|---|
| 240 | |
|---|
| 241 | protected: |
|---|
| 242 | |
|---|
| 243 | virtual ~PrimitiveSet() {} |
|---|
| 244 | |
|---|
| 245 | Type _primitiveType; |
|---|
| 246 | int _numInstances; |
|---|
| 247 | GLenum _mode; |
|---|
| 248 | unsigned int _modifiedCount; |
|---|
| 249 | mutable unsigned int _rangeModifiedCount; |
|---|
| 250 | |
|---|
| 251 | struct ObjectIDModifiedCountPair |
|---|
| 252 | { |
|---|
| 253 | ObjectIDModifiedCountPair(): |
|---|
| 254 | _objectID(0), |
|---|
| 255 | _modifiedCount(0) {} |
|---|
| 256 | |
|---|
| 257 | ObjectIDModifiedCountPair(const ObjectIDModifiedCountPair& obj): |
|---|
| 258 | _objectID(obj._objectID), |
|---|
| 259 | _modifiedCount(obj._modifiedCount) {} |
|---|
| 260 | |
|---|
| 261 | ObjectIDModifiedCountPair& operator = (const ObjectIDModifiedCountPair& obj) |
|---|
| 262 | { |
|---|
| 263 | _objectID = obj._objectID; |
|---|
| 264 | _modifiedCount = obj._modifiedCount; |
|---|
| 265 | return *this; |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | GLuint _objectID; |
|---|
| 269 | unsigned int _modifiedCount; |
|---|
| 270 | }; |
|---|
| 271 | |
|---|
| 272 | typedef osg::buffered_object<ObjectIDModifiedCountPair> GLObjectList; |
|---|
| 273 | }; |
|---|
| 274 | |
|---|
| 275 | class OSG_EXPORT DrawArrays : public PrimitiveSet |
|---|
| 276 | { |
|---|
| 277 | public: |
|---|
| 278 | |
|---|
| 279 | DrawArrays(GLenum mode=0): |
|---|
| 280 | PrimitiveSet(DrawArraysPrimitiveType,mode), |
|---|
| 281 | _first(0), |
|---|
| 282 | _count(0) {} |
|---|
| 283 | |
|---|
| 284 | DrawArrays(GLenum mode, GLint first, GLsizei count, int numInstances=0): |
|---|
| 285 | PrimitiveSet(DrawArraysPrimitiveType, mode, numInstances), |
|---|
| 286 | _first(first), |
|---|
| 287 | _count(count) {} |
|---|
| 288 | |
|---|
| 289 | DrawArrays(const DrawArrays& da,const CopyOp& copyop=CopyOp::SHALLOW_COPY): |
|---|
| 290 | PrimitiveSet(da,copyop), |
|---|
| 291 | _first(da._first), |
|---|
| 292 | _count(da._count) {} |
|---|
| 293 | |
|---|
| 294 | virtual Object* cloneType() const { return new DrawArrays(); } |
|---|
| 295 | virtual Object* clone(const CopyOp& copyop) const { return new DrawArrays(*this,copyop); } |
|---|
| 296 | virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const DrawArrays*>(obj)!=NULL; } |
|---|
| 297 | virtual const char* libraryName() const { return "osg"; } |
|---|
| 298 | virtual const char* className() const { return "DrawArrays"; } |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | void set(GLenum mode,GLint first, GLsizei count) |
|---|
| 302 | { |
|---|
| 303 | _mode = mode; |
|---|
| 304 | _first = first; |
|---|
| 305 | _count = count; |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | void setFirst(GLint first) { _first = first; } |
|---|
| 309 | GLint getFirst() const { return _first; } |
|---|
| 310 | |
|---|
| 311 | void setCount(GLsizei count) { _count = count; } |
|---|
| 312 | GLsizei getCount() const { return _count; } |
|---|
| 313 | |
|---|
| 314 | virtual void draw(State& state, bool useVertexBufferObjects) const; |
|---|
| 315 | |
|---|
| 316 | virtual void accept(PrimitiveFunctor& functor) const; |
|---|
| 317 | virtual void accept(PrimitiveIndexFunctor& functor) const; |
|---|
| 318 | |
|---|
| 319 | virtual unsigned int getNumIndices() const { return static_cast<unsigned int>(_count); } |
|---|
| 320 | virtual unsigned int index(unsigned int pos) const { return static_cast<unsigned int>(_first)+pos; } |
|---|
| 321 | virtual void offsetIndices(int offset) { _first += offset; } |
|---|
| 322 | |
|---|
| 323 | protected: |
|---|
| 324 | |
|---|
| 325 | virtual ~DrawArrays() {} |
|---|
| 326 | |
|---|
| 327 | GLint _first; |
|---|
| 328 | GLsizei _count; |
|---|
| 329 | }; |
|---|
| 330 | |
|---|
| 331 | class OSG_EXPORT DrawArrayLengths : public PrimitiveSet, public VectorGLsizei |
|---|
| 332 | { |
|---|
| 333 | public: |
|---|
| 334 | |
|---|
| 335 | typedef VectorGLsizei vector_type; |
|---|
| 336 | |
|---|
| 337 | DrawArrayLengths(GLenum mode=0): |
|---|
| 338 | PrimitiveSet(DrawArrayLengthsPrimitiveType,mode), |
|---|
| 339 | _first(0) {} |
|---|
| 340 | |
|---|
| 341 | DrawArrayLengths(const DrawArrayLengths& dal,const CopyOp& copyop=CopyOp::SHALLOW_COPY): |
|---|
| 342 | PrimitiveSet(dal,copyop), |
|---|
| 343 | vector_type(dal), |
|---|
| 344 | _first(dal._first) {} |
|---|
| 345 | |
|---|
| 346 | DrawArrayLengths(GLenum mode, GLint first, unsigned int no, GLsizei* ptr) : |
|---|
| 347 | PrimitiveSet(DrawArrayLengthsPrimitiveType,mode), |
|---|
| 348 | vector_type(ptr,ptr+no), |
|---|
| 349 | _first(first) {} |
|---|
| 350 | |
|---|
| 351 | DrawArrayLengths(GLenum mode,GLint first, unsigned int no) : |
|---|
| 352 | PrimitiveSet(DrawArrayLengthsPrimitiveType,mode), |
|---|
| 353 | vector_type(no), |
|---|
| 354 | _first(first) {} |
|---|
| 355 | |
|---|
| 356 | DrawArrayLengths(GLenum mode,GLint first) : |
|---|
| 357 | PrimitiveSet(DrawArrayLengthsPrimitiveType,mode), |
|---|
| 358 | vector_type(), |
|---|
| 359 | _first(first) {} |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | virtual Object* cloneType() const { return new DrawArrayLengths(); } |
|---|
| 363 | virtual Object* clone(const CopyOp& copyop) const { return new DrawArrayLengths(*this,copyop); } |
|---|
| 364 | virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const DrawArrayLengths*>(obj)!=NULL; } |
|---|
| 365 | virtual const char* libraryName() const { return "osg"; } |
|---|
| 366 | virtual const char* className() const { return "DrawArrayLengths"; } |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | void setFirst(GLint first) { _first = first; } |
|---|
| 370 | GLint getFirst() const { return _first; } |
|---|
| 371 | |
|---|
| 372 | virtual void draw(State& state, bool useVertexBufferObjects) const; |
|---|
| 373 | |
|---|
| 374 | virtual void accept(PrimitiveFunctor& functor) const; |
|---|
| 375 | virtual void accept(PrimitiveIndexFunctor& functor) const; |
|---|
| 376 | |
|---|
| 377 | virtual unsigned int getNumIndices() const; |
|---|
| 378 | virtual unsigned int index(unsigned int pos) const { return _first+pos; } |
|---|
| 379 | virtual void offsetIndices(int offset) { _first += offset; } |
|---|
| 380 | |
|---|
| 381 | virtual unsigned int getNumPrimitives() const; |
|---|
| 382 | |
|---|
| 383 | protected: |
|---|
| 384 | |
|---|
| 385 | virtual ~DrawArrayLengths() {} |
|---|
| 386 | |
|---|
| 387 | GLint _first; |
|---|
| 388 | }; |
|---|
| 389 | |
|---|
| 390 | class DrawElements : public PrimitiveSet |
|---|
| 391 | { |
|---|
| 392 | public: |
|---|
| 393 | |
|---|
| 394 | DrawElements(Type primType=PrimitiveType, GLenum mode=0, int numInstances=0): |
|---|
| 395 | PrimitiveSet(primType,mode, numInstances), |
|---|
| 396 | _eboOffset(0) {} |
|---|
| 397 | |
|---|
| 398 | DrawElements(const DrawElements& copy,const CopyOp& copyop=CopyOp::SHALLOW_COPY): |
|---|
| 399 | PrimitiveSet(copy,copyop), |
|---|
| 400 | _eboOffset(0) {} |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | virtual DrawElements* getDrawElements() { return this; } |
|---|
| 404 | virtual const DrawElements* getDrawElements() const { return this; } |
|---|
| 405 | |
|---|
| 406 | virtual void dirty() { ++_modifiedCount; if (_ebo.valid()) _ebo->dirty(); } |
|---|
| 407 | |
|---|
| 408 | /** Set the ElementBufferObject.*/ |
|---|
| 409 | inline void setElementBufferObject(osg::ElementBufferObject* ebo) |
|---|
| 410 | { |
|---|
| 411 | if (_ebo == ebo) return; |
|---|
| 412 | |
|---|
| 413 | if (_ebo.valid()) |
|---|
| 414 | { |
|---|
| 415 | _ebo->removeDrawElements(this); |
|---|
| 416 | } |
|---|
| 417 | |
|---|
| 418 | _ebo = ebo; |
|---|
| 419 | |
|---|
| 420 | if (_ebo.valid()) |
|---|
| 421 | { |
|---|
| 422 | _ebo->addDrawElements(this); |
|---|
| 423 | } |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | /** Get the ElementBufferObject. If no EBO is assigned returns NULL*/ |
|---|
| 427 | inline osg::ElementBufferObject* getElementBufferObject() { return _ebo.get(); } |
|---|
| 428 | |
|---|
| 429 | /** Get the const ElementBufferObject. If no EBO is assigned returns NULL*/ |
|---|
| 430 | inline const osg::ElementBufferObject* getElementBufferObject() const { return _ebo.get(); } |
|---|
| 431 | |
|---|
| 432 | /** Set the offset into the ElementBufferObject, if used.*/ |
|---|
| 433 | inline void setElementBufferObjectOffset(const GLvoid* offset) const { _eboOffset = offset; } |
|---|
| 434 | |
|---|
| 435 | /** Get the offset into the ElementBufferOffset, if used.*/ |
|---|
| 436 | inline const GLvoid* getElementBufferObjectOffset() const { return _eboOffset; } |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | /** Resize any per context GLObject buffers to specified size. */ |
|---|
| 440 | virtual void resizeGLObjectBuffers(unsigned int maxSize) |
|---|
| 441 | { |
|---|
| 442 | if (_ebo.valid()) _ebo->resizeGLObjectBuffers(maxSize); |
|---|
| 443 | } |
|---|
| 444 | |
|---|
| 445 | /** If State is non-zero, this function releases OpenGL objects for |
|---|
| 446 | * the specified graphics context. Otherwise, releases OpenGL objects |
|---|
| 447 | * for all graphics contexts. */ |
|---|
| 448 | virtual void releaseGLObjects(State* state=0) const |
|---|
| 449 | { |
|---|
| 450 | if (_ebo.valid()) _ebo->releaseGLObjects(state); |
|---|
| 451 | } |
|---|
| 452 | |
|---|
| 453 | protected: |
|---|
| 454 | |
|---|
| 455 | virtual ~DrawElements() |
|---|
| 456 | { |
|---|
| 457 | if (_ebo.valid()) |
|---|
| 458 | { |
|---|
| 459 | _ebo->removeDrawElements(this); |
|---|
| 460 | } |
|---|
| 461 | } |
|---|
| 462 | |
|---|
| 463 | osg::ref_ptr<ElementBufferObject> _ebo; |
|---|
| 464 | mutable const GLvoid* _eboOffset; |
|---|
| 465 | |
|---|
| 466 | }; |
|---|
| 467 | |
|---|
| 468 | class OSG_EXPORT DrawElementsUByte : public DrawElements, public VectorGLubyte |
|---|
| 469 | { |
|---|
| 470 | public: |
|---|
| 471 | |
|---|
| 472 | typedef VectorGLubyte vector_type; |
|---|
| 473 | |
|---|
| 474 | DrawElementsUByte(GLenum mode=0): |
|---|
| 475 | DrawElements(DrawElementsUBytePrimitiveType,mode) {} |
|---|
| 476 | |
|---|
| 477 | DrawElementsUByte(const DrawElementsUByte& array, const CopyOp& copyop=CopyOp::SHALLOW_COPY): |
|---|
| 478 | DrawElements(array,copyop), |
|---|
| 479 | vector_type(array) {} |
|---|
| 480 | |
|---|
| 481 | DrawElementsUByte(GLenum mode, unsigned int no, const GLubyte* ptr, int numInstances=0) : |
|---|
| 482 | DrawElements(DrawElementsUBytePrimitiveType,mode,numInstances), |
|---|
| 483 | vector_type(ptr,ptr+no) {} |
|---|
| 484 | |
|---|
| 485 | DrawElementsUByte(GLenum mode, unsigned int no) : |
|---|
| 486 | DrawElements(DrawElementsUBytePrimitiveType,mode), |
|---|
| 487 | vector_type(no) {} |
|---|
| 488 | |
|---|
| 489 | virtual Object* cloneType() const { return new DrawElementsUByte(); } |
|---|
| 490 | virtual Object* clone(const CopyOp& copyop) const { return new DrawElementsUByte(*this,copyop); } |
|---|
| 491 | virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const DrawElementsUByte*>(obj)!=NULL; } |
|---|
| 492 | virtual const char* libraryName() const { return "osg"; } |
|---|
| 493 | virtual const char* className() const { return "DrawElementsUByte"; } |
|---|
| 494 | |
|---|
| 495 | virtual const GLvoid* getDataPointer() const { return empty()?0:&front(); } |
|---|
| 496 | virtual unsigned int getTotalDataSize() const { return static_cast<unsigned int>(size()); } |
|---|
| 497 | virtual bool supportsBufferObject() const { return false; } |
|---|
| 498 | |
|---|
| 499 | virtual void draw(State& state, bool useVertexBufferObjects) const ; |
|---|
| 500 | |
|---|
| 501 | virtual void accept(PrimitiveFunctor& functor) const; |
|---|
| 502 | virtual void accept(PrimitiveIndexFunctor& functor) const; |
|---|
| 503 | |
|---|
| 504 | virtual unsigned int getNumIndices() const { return static_cast<unsigned int>(size()); } |
|---|
| 505 | virtual unsigned int index(unsigned int pos) const { return (*this)[pos]; } |
|---|
| 506 | virtual void offsetIndices(int offset); |
|---|
| 507 | |
|---|
| 508 | virtual void computeRange() const |
|---|
| 509 | { |
|---|
| 510 | if (empty()) |
|---|
| 511 | { |
|---|
| 512 | _minIndex = 0; |
|---|
| 513 | _maxIndex = 0; |
|---|
| 514 | _rangeModifiedCount = _modifiedCount; |
|---|
| 515 | return; |
|---|
| 516 | } |
|---|
| 517 | |
|---|
| 518 | _minIndex = front(); |
|---|
| 519 | _maxIndex = _minIndex; |
|---|
| 520 | |
|---|
| 521 | for(vector_type::const_iterator itr=begin(); itr!=end(); ++itr) |
|---|
| 522 | { |
|---|
| 523 | if (*itr<_minIndex) _minIndex = *itr; |
|---|
| 524 | if (*itr>_maxIndex) _maxIndex = *itr; |
|---|
| 525 | } |
|---|
| 526 | _rangeModifiedCount = _modifiedCount; |
|---|
| 527 | } |
|---|
| 528 | |
|---|
| 529 | protected: |
|---|
| 530 | |
|---|
| 531 | virtual ~DrawElementsUByte(); |
|---|
| 532 | |
|---|
| 533 | mutable unsigned int _minIndex; |
|---|
| 534 | mutable unsigned int _maxIndex; |
|---|
| 535 | }; |
|---|
| 536 | |
|---|
| 537 | |
|---|
| 538 | class OSG_EXPORT DrawElementsUShort : public DrawElements, public VectorGLushort |
|---|
| 539 | { |
|---|
| 540 | public: |
|---|
| 541 | |
|---|
| 542 | typedef VectorGLushort vector_type; |
|---|
| 543 | |
|---|
| 544 | DrawElementsUShort(GLenum mode=0): |
|---|
| 545 | DrawElements(DrawElementsUShortPrimitiveType,mode) {} |
|---|
| 546 | |
|---|
| 547 | DrawElementsUShort(const DrawElementsUShort& array,const CopyOp& copyop=CopyOp::SHALLOW_COPY): |
|---|
| 548 | DrawElements(array,copyop), |
|---|
| 549 | vector_type(array) {} |
|---|
| 550 | |
|---|
| 551 | DrawElementsUShort(GLenum mode, unsigned int no, const GLushort* ptr, int numInstances=0) : |
|---|
| 552 | DrawElements(DrawElementsUShortPrimitiveType,mode,numInstances), |
|---|
| 553 | vector_type(ptr,ptr+no) {} |
|---|
| 554 | |
|---|
| 555 | DrawElementsUShort(GLenum mode, unsigned int no) : |
|---|
| 556 | DrawElements(DrawElementsUShortPrimitiveType,mode), |
|---|
| 557 | vector_type(no) {} |
|---|
| 558 | |
|---|
| 559 | template <class InputIterator> |
|---|
| 560 | DrawElementsUShort(GLenum mode, InputIterator first,InputIterator last) : |
|---|
| 561 | DrawElements(DrawElementsUShortPrimitiveType,mode), |
|---|
| 562 | vector_type(first,last) {} |
|---|
| 563 | |
|---|
| 564 | virtual Object* cloneType() const { return new DrawElementsUShort(); } |
|---|
| 565 | virtual Object* clone(const CopyOp& copyop) const { return new DrawElementsUShort(*this,copyop); } |
|---|
| 566 | virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const DrawElementsUShort*>(obj)!=NULL; } |
|---|
| 567 | virtual const char* libraryName() const { return "osg"; } |
|---|
| 568 | virtual const char* className() const { return "DrawElementsUShort"; } |
|---|
| 569 | |
|---|
| 570 | virtual const GLvoid* getDataPointer() const { return empty()?0:&front(); } |
|---|
| 571 | virtual unsigned int getTotalDataSize() const { return 2u*static_cast<unsigned int>(size()); } |
|---|
| 572 | virtual bool supportsBufferObject() const { return false; } |
|---|
| 573 | |
|---|
| 574 | virtual void draw(State& state, bool useVertexBufferObjects) const; |
|---|
| 575 | |
|---|
| 576 | virtual void accept(PrimitiveFunctor& functor) const; |
|---|
| 577 | virtual void accept(PrimitiveIndexFunctor& functor) const; |
|---|
| 578 | |
|---|
| 579 | virtual unsigned int getNumIndices() const { return static_cast<unsigned int>(size()); } |
|---|
| 580 | virtual unsigned int index(unsigned int pos) const { return (*this)[pos]; } |
|---|
| 581 | virtual void offsetIndices(int offset); |
|---|
| 582 | |
|---|
| 583 | virtual void computeRange() const |
|---|
| 584 | { |
|---|
| 585 | if (empty()) |
|---|
| 586 | { |
|---|
| 587 | _minIndex = 0; |
|---|
| 588 | _maxIndex = 0; |
|---|
| 589 | _rangeModifiedCount = _modifiedCount; |
|---|
| 590 | return; |
|---|
| 591 | } |
|---|
| 592 | |
|---|
| 593 | _minIndex = front(); |
|---|
| 594 | _maxIndex = _minIndex; |
|---|
| 595 | |
|---|
| 596 | for(vector_type::const_iterator itr=begin(); itr!=end(); ++itr) |
|---|
| 597 | { |
|---|
| 598 | if (*itr<_minIndex) _minIndex = *itr; |
|---|
| 599 | if (*itr>_maxIndex) _maxIndex = *itr; |
|---|
| 600 | } |
|---|
| 601 | _rangeModifiedCount = _modifiedCount; |
|---|
| 602 | } |
|---|
| 603 | |
|---|
| 604 | protected: |
|---|
| 605 | |
|---|
| 606 | virtual ~DrawElementsUShort(); |
|---|
| 607 | |
|---|
| 608 | mutable unsigned int _minIndex; |
|---|
| 609 | mutable unsigned int _maxIndex; |
|---|
| 610 | }; |
|---|
| 611 | |
|---|
| 612 | class OSG_EXPORT DrawElementsUInt : public DrawElements, public VectorGLuint |
|---|
| 613 | { |
|---|
| 614 | public: |
|---|
| 615 | |
|---|
| 616 | typedef VectorGLuint vector_type; |
|---|
| 617 | |
|---|
| 618 | DrawElementsUInt(GLenum mode=0): |
|---|
| 619 | DrawElements(DrawElementsUIntPrimitiveType,mode) {} |
|---|
| 620 | |
|---|
| 621 | DrawElementsUInt(const DrawElementsUInt& array,const CopyOp& copyop=CopyOp::SHALLOW_COPY): |
|---|
| 622 | DrawElements(array,copyop), |
|---|
| 623 | vector_type(array) {} |
|---|
| 624 | |
|---|
| 625 | DrawElementsUInt(GLenum mode, unsigned int no, const GLuint* ptr, int numInstances=0) : |
|---|
| 626 | DrawElements(DrawElementsUIntPrimitiveType,mode,numInstances), |
|---|
| 627 | vector_type(ptr,ptr+no) {} |
|---|
| 628 | |
|---|
| 629 | DrawElementsUInt(GLenum mode, unsigned int no) : |
|---|
| 630 | DrawElements(DrawElementsUIntPrimitiveType,mode), |
|---|
| 631 | vector_type(no) {} |
|---|
| 632 | |
|---|
| 633 | template <class InputIterator> |
|---|
| 634 | DrawElementsUInt(GLenum mode, InputIterator first,InputIterator last) : |
|---|
| 635 | DrawElements(DrawElementsUIntPrimitiveType,mode), |
|---|
| 636 | vector_type(first,last) {} |
|---|
| 637 | |
|---|
| 638 | virtual Object* cloneType() const { return new DrawElementsUInt(); } |
|---|
| 639 | virtual Object* clone(const CopyOp& copyop) const { return new DrawElementsUInt(*this,copyop); } |
|---|
| 640 | virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const DrawElementsUInt*>(obj)!=NULL; } |
|---|
| 641 | virtual const char* libraryName() const { return "osg"; } |
|---|
| 642 | virtual const char* className() const { return "DrawElementsUInt"; } |
|---|
| 643 | |
|---|
| 644 | virtual const GLvoid* getDataPointer() const { return empty()?0:&front(); } |
|---|
| 645 | virtual unsigned int getTotalDataSize() const { return 4u*static_cast<unsigned int>(size()); } |
|---|
| 646 | virtual bool supportsBufferObject() const { return false; } |
|---|
| 647 | |
|---|
| 648 | virtual void draw(State& state, bool useVertexBufferObjects) const; |
|---|
| 649 | |
|---|
| 650 | virtual void accept(PrimitiveFunctor& functor) const; |
|---|
| 651 | virtual void accept(PrimitiveIndexFunctor& functor) const; |
|---|
| 652 | |
|---|
| 653 | virtual unsigned int getNumIndices() const { return static_cast<unsigned int>(size()); } |
|---|
| 654 | virtual unsigned int index(unsigned int pos) const { return (*this)[pos]; } |
|---|
| 655 | virtual void offsetIndices(int offset); |
|---|
| 656 | |
|---|
| 657 | |
|---|
| 658 | virtual void computeRange() const |
|---|
| 659 | { |
|---|
| 660 | if (empty()) |
|---|
| 661 | { |
|---|
| 662 | _minIndex = 0; |
|---|
| 663 | _maxIndex = 0; |
|---|
| 664 | _rangeModifiedCount = _modifiedCount; |
|---|
| 665 | return; |
|---|
| 666 | } |
|---|
| 667 | |
|---|
| 668 | _minIndex = front(); |
|---|
| 669 | _maxIndex = _minIndex; |
|---|
| 670 | |
|---|
| 671 | for(vector_type::const_iterator itr=begin(); itr!=end(); ++itr) |
|---|
| 672 | { |
|---|
| 673 | if (*itr<_minIndex) _minIndex = *itr; |
|---|
| 674 | if (*itr>_maxIndex) _maxIndex = *itr; |
|---|
| 675 | } |
|---|
| 676 | _rangeModifiedCount = _modifiedCount; |
|---|
| 677 | } |
|---|
| 678 | |
|---|
| 679 | protected: |
|---|
| 680 | |
|---|
| 681 | virtual ~DrawElementsUInt(); |
|---|
| 682 | |
|---|
| 683 | mutable unsigned int _minIndex; |
|---|
| 684 | mutable unsigned int _maxIndex; |
|---|
| 685 | }; |
|---|
| 686 | |
|---|
| 687 | } |
|---|
| 688 | |
|---|
| 689 | #endif |
|---|