PrimitiveSet 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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_PRIMITIVESET
  14. #define OSG_PRIMITIVESET 1
  15. #include <osg/GL>
  16. #include <osg/Object>
  17. #include <osg/buffered_value>
  18. #include <osg/Vec2>
  19. #include <osg/Vec3>
  20. #include <osg/Vec4>
  21. #include <osg/Vec2d>
  22. #include <osg/Vec3d>
  23. #include <osg/Vec4d>
  24. #include <osg/MixinVector>
  25. #include <osg/BufferObject>
  26. #include <vector>
  27. #define OSG_HAS_MULTIDRAWARRAYS
  28. namespace osg {
  29. typedef MixinVector<GLsizei> VectorGLsizei;
  30. typedef MixinVector<GLubyte> VectorGLubyte;
  31. typedef MixinVector<GLushort> VectorGLushort;
  32. typedef MixinVector<GLuint> VectorGLuint;
  33. class State;
  34. /** A \c PrimitiveFunctor is used (in conjunction with
  35. * <tt>osg::Drawable::accept (PrimitiveFunctor&)</tt>) to get access to the
  36. * primitives that compose the things drawn by OSG.
  37. * <p>If \c osg::Drawable::accept() is called with a \c PrimitiveFunctor
  38. * parameter, the \c Drawable will "pretend" it is drawing itself, but instead
  39. * of calling real OpenGL functions, it will call <tt>PrimitiveFunctor</tt>'s
  40. * member functions that "mimic" the OpenGL calls.
  41. * <p>Concrete subclasses of \c PrimitiveFunctor must implement these methods
  42. * so that they performs whatever they want.
  43. */
  44. class PrimitiveFunctor
  45. {
  46. public:
  47. virtual ~PrimitiveFunctor() {}
  48. /** Sets the array of vertices used to describe the primitives. Somehow
  49. * mimics the OpenGL \c glVertexPointer() function.
  50. */
  51. virtual void setVertexArray(unsigned int count,const Vec2* vertices) = 0;
  52. /** Sets the array of vertices used to describe the primitives. Somehow
  53. * mimics the OpenGL \c glVertexPointer() function.
  54. */
  55. virtual void setVertexArray(unsigned int count,const Vec3* vertices) = 0;
  56. /** Sets the array of vertices used to describe the primitives. Somehow
  57. * mimics the OpenGL \c glVertexPointer() function.
  58. */
  59. virtual void setVertexArray(unsigned int count,const Vec4* vertices) = 0;
  60. /** Sets the array of vertices used to describe the primitives. Somehow
  61. * mimics the OpenGL \c glVertexPointer() function.
  62. */
  63. virtual void setVertexArray(unsigned int count,const Vec2d* vertices) = 0;
  64. /** Sets the array of vertices used to describe the primitives. Somehow
  65. * mimics the OpenGL \c glVertexPointer() function.
  66. */
  67. virtual void setVertexArray(unsigned int count,const Vec3d* vertices) = 0;
  68. /** Sets the array of vertices used to describe the primitives. Somehow
  69. * mimics the OpenGL \c glVertexPointer() function.
  70. */
  71. virtual void setVertexArray(unsigned int count,const Vec4d* vertices) = 0;
  72. /// Mimics the OpenGL \c glDrawArrays() function.
  73. virtual void drawArrays(GLenum mode,GLint first,GLsizei count) = 0;
  74. /// Mimics the OpenGL \c glDrawElements() function.
  75. virtual void drawElements(GLenum mode,GLsizei count,const GLubyte* indices) = 0;
  76. /// Mimics the OpenGL \c glDrawElements() function.
  77. virtual void drawElements(GLenum mode,GLsizei count,const GLushort* indices) = 0;
  78. /// Mimics the OpenGL \c glDrawElements() function.
  79. virtual void drawElements(GLenum mode,GLsizei count,const GLuint* indices) = 0;
  80. };
  81. class PrimitiveIndexFunctor
  82. {
  83. public:
  84. virtual ~PrimitiveIndexFunctor() {}
  85. virtual void setVertexArray(unsigned int count,const Vec2* vertices) = 0;
  86. virtual void setVertexArray(unsigned int count,const Vec3* vertices) = 0;
  87. virtual void setVertexArray(unsigned int count,const Vec4* vertices) = 0;
  88. virtual void setVertexArray(unsigned int count,const Vec2d* vertices) = 0;
  89. virtual void setVertexArray(unsigned int count,const Vec3d* vertices) = 0;
  90. virtual void setVertexArray(unsigned int count,const Vec4d* vertices) = 0;
  91. virtual void drawArrays(GLenum mode,GLint first,GLsizei count) = 0;
  92. virtual void drawElements(GLenum mode,GLsizei count,const GLubyte* indices) = 0;
  93. virtual void drawElements(GLenum mode,GLsizei count,const GLushort* indices) = 0;
  94. virtual void drawElements(GLenum mode,GLsizei count,const GLuint* indices) = 0;
  95. };
  96. class DrawElements;
  97. class OSG_EXPORT PrimitiveSet : public BufferData
  98. {
  99. public:
  100. enum Type
  101. {
  102. PrimitiveType,
  103. DrawArraysPrimitiveType,
  104. DrawArrayLengthsPrimitiveType,
  105. DrawElementsUBytePrimitiveType,
  106. DrawElementsUShortPrimitiveType,
  107. DrawElementsUIntPrimitiveType,
  108. MultiDrawArraysPrimitiveType,
  109. DrawArraysIndirectPrimitiveType,
  110. DrawElementsUByteIndirectPrimitiveType,
  111. DrawElementsUShortIndirectPrimitiveType,
  112. DrawElementsUIntIndirectPrimitiveType,
  113. MultiDrawArraysIndirectPrimitiveType,
  114. MultiDrawElementsUByteIndirectPrimitiveType,
  115. MultiDrawElementsUShortIndirectPrimitiveType,
  116. MultiDrawElementsUIntIndirectPrimitiveType
  117. };
  118. enum Mode
  119. {
  120. POINTS = GL_POINTS,
  121. LINES = GL_LINES,
  122. LINE_STRIP = GL_LINE_STRIP,
  123. LINE_LOOP = GL_LINE_LOOP,
  124. TRIANGLES = GL_TRIANGLES,
  125. TRIANGLE_STRIP = GL_TRIANGLE_STRIP,
  126. TRIANGLE_FAN = GL_TRIANGLE_FAN,
  127. QUADS = GL_QUADS,
  128. QUAD_STRIP = GL_QUAD_STRIP,
  129. POLYGON = GL_POLYGON,
  130. LINES_ADJACENCY = GL_LINES_ADJACENCY,
  131. LINE_STRIP_ADJACENCY = GL_LINE_STRIP_ADJACENCY,
  132. TRIANGLES_ADJACENCY = GL_TRIANGLES_ADJACENCY,
  133. TRIANGLE_STRIP_ADJACENCY = GL_TRIANGLE_STRIP_ADJACENCY,
  134. PATCHES = GL_PATCHES
  135. };
  136. PrimitiveSet(Type primType=PrimitiveType,GLenum mode=0, int numInstances=0):
  137. _primitiveType(primType),
  138. _numInstances(numInstances),
  139. _mode(mode) {}
  140. PrimitiveSet(const PrimitiveSet& prim,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  141. BufferData(prim,copyop),
  142. _primitiveType(prim._primitiveType),
  143. _numInstances(prim._numInstances),
  144. _mode(prim._mode) {}
  145. virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const PrimitiveSet*>(obj)!=NULL; }
  146. virtual const char* libraryName() const { return "osg"; }
  147. virtual const char* className() const { return "PrimitiveSet"; }
  148. Type getType() const { return _primitiveType; }
  149. virtual osg::PrimitiveSet* asPrimitiveSet() { return this; }
  150. virtual const osg::PrimitiveSet* asPrimitiveSet() const { return this; }
  151. virtual const GLvoid* getDataPointer() const { return 0; }
  152. virtual unsigned int getTotalDataSize() const { return 0; }
  153. virtual bool supportsBufferObject() const { return false; }
  154. virtual DrawElements* getDrawElements() { return 0; }
  155. virtual const DrawElements* getDrawElements() const { return 0; }
  156. void setNumInstances(int n) { _numInstances = n; }
  157. int getNumInstances() const { return _numInstances; }
  158. void setMode(GLenum mode) { _mode = mode; }
  159. GLenum getMode() const { return _mode; }
  160. virtual void draw(State& state, bool useVertexBufferObjects) const = 0;
  161. virtual void accept(PrimitiveFunctor& functor) const = 0;
  162. virtual void accept(PrimitiveIndexFunctor& functor) const = 0;
  163. virtual unsigned int index(unsigned int pos) const = 0;
  164. virtual unsigned int getNumIndices() const = 0;
  165. virtual void offsetIndices(int offset) = 0;
  166. virtual unsigned int getNumPrimitives() const;
  167. virtual void computeRange() const {}
  168. protected:
  169. virtual ~PrimitiveSet() {}
  170. Type _primitiveType;
  171. int _numInstances;
  172. GLenum _mode;
  173. };
  174. class OSG_EXPORT DrawArrays : public PrimitiveSet
  175. {
  176. public:
  177. DrawArrays(GLenum mode=0):
  178. PrimitiveSet(DrawArraysPrimitiveType,mode),
  179. _first(0),
  180. _count(0) {}
  181. DrawArrays(GLenum mode, GLint first, GLsizei count, int numInstances=0):
  182. PrimitiveSet(DrawArraysPrimitiveType, mode, numInstances),
  183. _first(first),
  184. _count(count) {}
  185. DrawArrays(const DrawArrays& da,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  186. PrimitiveSet(da,copyop),
  187. _first(da._first),
  188. _count(da._count) {}
  189. virtual Object* cloneType() const { return new DrawArrays(); }
  190. virtual Object* clone(const CopyOp& copyop) const { return new DrawArrays(*this,copyop); }
  191. virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const DrawArrays*>(obj)!=NULL; }
  192. virtual const char* libraryName() const { return "osg"; }
  193. virtual const char* className() const { return "DrawArrays"; }
  194. void set(GLenum mode,GLint first, GLsizei count)
  195. {
  196. _mode = mode;
  197. _first = first;
  198. _count = count;
  199. }
  200. void setFirst(GLint first) { _first = first; }
  201. GLint getFirst() const { return _first; }
  202. void setCount(GLsizei count) { _count = count; }
  203. GLsizei getCount() const { return _count; }
  204. virtual void draw(State& state, bool useVertexBufferObjects) const;
  205. virtual void accept(PrimitiveFunctor& functor) const;
  206. virtual void accept(PrimitiveIndexFunctor& functor) const;
  207. virtual unsigned int getNumIndices() const { return static_cast<unsigned int>(_count); }
  208. virtual unsigned int index(unsigned int pos) const { return static_cast<unsigned int>(_first)+pos; }
  209. virtual void offsetIndices(int offset) { _first += offset; }
  210. protected:
  211. virtual ~DrawArrays() {}
  212. GLint _first;
  213. GLsizei _count;
  214. };
  215. class OSG_EXPORT DrawArrayLengths : public PrimitiveSet, public VectorGLsizei
  216. {
  217. public:
  218. typedef VectorGLsizei vector_type;
  219. DrawArrayLengths(GLenum mode=0):
  220. PrimitiveSet(DrawArrayLengthsPrimitiveType,mode),
  221. _first(0) {}
  222. DrawArrayLengths(const DrawArrayLengths& dal,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  223. PrimitiveSet(dal,copyop),
  224. vector_type(dal),
  225. _first(dal._first) {}
  226. DrawArrayLengths(GLenum mode, GLint first, unsigned int no, GLsizei* ptr) :
  227. PrimitiveSet(DrawArrayLengthsPrimitiveType,mode),
  228. vector_type(ptr,ptr+no),
  229. _first(first) {}
  230. DrawArrayLengths(GLenum mode,GLint first, unsigned int no) :
  231. PrimitiveSet(DrawArrayLengthsPrimitiveType,mode),
  232. vector_type(no),
  233. _first(first) {}
  234. DrawArrayLengths(GLenum mode,GLint first) :
  235. PrimitiveSet(DrawArrayLengthsPrimitiveType,mode),
  236. vector_type(),
  237. _first(first) {}
  238. virtual Object* cloneType() const { return new DrawArrayLengths(); }
  239. virtual Object* clone(const CopyOp& copyop) const { return new DrawArrayLengths(*this,copyop); }
  240. virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const DrawArrayLengths*>(obj)!=NULL; }
  241. virtual const char* libraryName() const { return "osg"; }
  242. virtual const char* className() const { return "DrawArrayLengths"; }
  243. void setFirst(GLint first) { _first = first; }
  244. GLint getFirst() const { return _first; }
  245. virtual void draw(State& state, bool useVertexBufferObjects) const;
  246. virtual void accept(PrimitiveFunctor& functor) const;
  247. virtual void accept(PrimitiveIndexFunctor& functor) const;
  248. virtual unsigned int getNumIndices() const;
  249. virtual unsigned int index(unsigned int pos) const { return _first+pos; }
  250. virtual void offsetIndices(int offset) { _first += offset; }
  251. virtual unsigned int getNumPrimitives() const;
  252. protected:
  253. virtual ~DrawArrayLengths() {}
  254. GLint _first;
  255. };
  256. class DrawElements : public PrimitiveSet
  257. {
  258. public:
  259. DrawElements(Type primType=PrimitiveType, GLenum mode=0, int numInstances=0):
  260. PrimitiveSet(primType,mode, numInstances) {}
  261. DrawElements(const DrawElements& copy,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  262. PrimitiveSet(copy,copyop) {}
  263. virtual DrawElements* getDrawElements() { return this; }
  264. virtual const DrawElements* getDrawElements() const { return this; }
  265. /** Set the ElementBufferObject.*/
  266. inline void setElementBufferObject(osg::ElementBufferObject* ebo) { setBufferObject(ebo); }
  267. /** Get the ElementBufferObject. If no EBO is assigned returns NULL*/
  268. inline osg::ElementBufferObject* getElementBufferObject() { return dynamic_cast<osg::ElementBufferObject*>(_bufferObject.get()); }
  269. /** Get the const ElementBufferObject. If no EBO is assigned returns NULL*/
  270. inline const osg::ElementBufferObject* getElementBufferObject() const { return dynamic_cast<const osg::ElementBufferObject*>(_bufferObject.get()); }
  271. virtual GLenum getDataType() = 0;
  272. virtual void resizeElements(unsigned int numIndices) = 0;
  273. virtual void reserveElements(unsigned int numIndices) = 0;
  274. virtual void setElement(unsigned int, unsigned int) = 0;
  275. virtual unsigned int getElement(unsigned int) = 0;
  276. virtual void addElement(unsigned int) = 0;
  277. protected:
  278. virtual ~DrawElements() {}
  279. };
  280. class OSG_EXPORT DrawElementsUByte : public DrawElements, public VectorGLubyte
  281. {
  282. public:
  283. typedef VectorGLubyte vector_type;
  284. DrawElementsUByte(GLenum mode=0):
  285. DrawElements(DrawElementsUBytePrimitiveType,mode) {}
  286. DrawElementsUByte(const DrawElementsUByte& array, const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  287. DrawElements(array,copyop),
  288. vector_type(array) {}
  289. /**
  290. * \param mode One of osg::PrimitiveSet::Mode. Determines the type of primitives used.
  291. * \param no Number of intended elements. This will be the size of the underlying vector.
  292. * \param ptr Pointer to a GLubyte to copy index data from.
  293. * \param numInstances When non zero passed as the number of draw instances to use re.
  294. */
  295. DrawElementsUByte(GLenum mode, unsigned int no, const GLubyte* ptr, int numInstances=0) :
  296. DrawElements(DrawElementsUBytePrimitiveType,mode,numInstances),
  297. vector_type(ptr,ptr+no) {}
  298. /**
  299. * \param mode One of osg::PrimitiveSet::Mode. Determines the type of primitives used.
  300. * \param no Number of intended elements. This will be the size of the underlying vector.
  301. */
  302. DrawElementsUByte(GLenum mode, unsigned int no) :
  303. DrawElements(DrawElementsUBytePrimitiveType,mode),
  304. vector_type(no) {}
  305. virtual Object* cloneType() const { return new DrawElementsUByte(); }
  306. virtual Object* clone(const CopyOp& copyop) const { return new DrawElementsUByte(*this,copyop); }
  307. virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const DrawElementsUByte*>(obj)!=NULL; }
  308. virtual const char* libraryName() const { return "osg"; }
  309. virtual const char* className() const { return "DrawElementsUByte"; }
  310. virtual const GLvoid* getDataPointer() const { return empty()?0:&front(); }
  311. virtual unsigned int getTotalDataSize() const { return static_cast<unsigned int>(size()); }
  312. virtual bool supportsBufferObject() const { return false; }
  313. virtual void draw(State& state, bool useVertexBufferObjects) const ;
  314. virtual void accept(PrimitiveFunctor& functor) const;
  315. virtual void accept(PrimitiveIndexFunctor& functor) const;
  316. virtual unsigned int getNumIndices() const { return static_cast<unsigned int>(size()); }
  317. virtual unsigned int index(unsigned int pos) const { return (*this)[pos]; }
  318. virtual void offsetIndices(int offset);
  319. virtual GLenum getDataType() { return GL_UNSIGNED_BYTE; }
  320. virtual void resizeElements(unsigned int numIndices) { resize(numIndices); }
  321. virtual void reserveElements(unsigned int numIndices) { reserve(numIndices); }
  322. virtual void setElement(unsigned int i, unsigned int v) { (*this)[i] = v; }
  323. virtual unsigned int getElement(unsigned int i) { return (*this)[i]; }
  324. virtual void addElement(unsigned int v) { push_back(GLubyte(v)); }
  325. protected:
  326. virtual ~DrawElementsUByte();
  327. };
  328. class OSG_EXPORT DrawElementsUShort : public DrawElements, public VectorGLushort
  329. {
  330. public:
  331. typedef VectorGLushort vector_type;
  332. DrawElementsUShort(GLenum mode=0):
  333. DrawElements(DrawElementsUShortPrimitiveType,mode) {}
  334. DrawElementsUShort(const DrawElementsUShort& array,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  335. DrawElements(array,copyop),
  336. vector_type(array) {}
  337. /**
  338. * \param mode One of osg::PrimitiveSet::Mode. Determines the type of primitives used.
  339. * \param no Number of intended elements. This will be the size of the underlying vector.
  340. * \param ptr Pointer to a GLushort to copy index data from.
  341. * \param numInstances When non zero passed as the number of draw instances to use re.
  342. */
  343. DrawElementsUShort(GLenum mode, unsigned int no, const GLushort* ptr, int numInstances=0) :
  344. DrawElements(DrawElementsUShortPrimitiveType,mode,numInstances),
  345. vector_type(ptr,ptr+no) {}
  346. /**
  347. * \param mode One of osg::PrimitiveSet::Mode. Determines the type of primitives used.
  348. * \param no Number of intended elements. This will be the size of the underlying vector.
  349. */
  350. DrawElementsUShort(GLenum mode, unsigned int no) :
  351. DrawElements(DrawElementsUShortPrimitiveType,mode),
  352. vector_type(no) {}
  353. template <class InputIterator>
  354. DrawElementsUShort(GLenum mode, InputIterator first,InputIterator last) :
  355. DrawElements(DrawElementsUShortPrimitiveType,mode),
  356. vector_type(first,last) {}
  357. virtual Object* cloneType() const { return new DrawElementsUShort(); }
  358. virtual Object* clone(const CopyOp& copyop) const { return new DrawElementsUShort(*this,copyop); }
  359. virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const DrawElementsUShort*>(obj)!=NULL; }
  360. virtual const char* libraryName() const { return "osg"; }
  361. virtual const char* className() const { return "DrawElementsUShort"; }
  362. virtual const GLvoid* getDataPointer() const { return empty()?0:&front(); }
  363. virtual unsigned int getTotalDataSize() const { return 2u*static_cast<unsigned int>(size()); }
  364. virtual bool supportsBufferObject() const { return false; }
  365. virtual void draw(State& state, bool useVertexBufferObjects) const;
  366. virtual void accept(PrimitiveFunctor& functor) const;
  367. virtual void accept(PrimitiveIndexFunctor& functor) const;
  368. virtual unsigned int getNumIndices() const { return static_cast<unsigned int>(size()); }
  369. virtual unsigned int index(unsigned int pos) const { return (*this)[pos]; }
  370. virtual void offsetIndices(int offset);
  371. virtual GLenum getDataType() { return GL_UNSIGNED_SHORT; }
  372. virtual void resizeElements(unsigned int numIndices) { resize(numIndices); }
  373. virtual void reserveElements(unsigned int numIndices) { reserve(numIndices); }
  374. virtual void setElement(unsigned int i, unsigned int v) { (*this)[i] = v; }
  375. virtual unsigned int getElement(unsigned int i) { return (*this)[i]; }
  376. virtual void addElement(unsigned int v) { push_back(GLushort(v)); }
  377. protected:
  378. virtual ~DrawElementsUShort();
  379. };
  380. class OSG_EXPORT DrawElementsUInt : public DrawElements, public VectorGLuint
  381. {
  382. public:
  383. typedef VectorGLuint vector_type;
  384. DrawElementsUInt(GLenum mode=0):
  385. DrawElements(DrawElementsUIntPrimitiveType,mode) {}
  386. DrawElementsUInt(const DrawElementsUInt& array,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  387. DrawElements(array,copyop),
  388. vector_type(array) {}
  389. /**
  390. * \param mode One of osg::PrimitiveSet::Mode. Determines the type of primitives used.
  391. * \param no Number of intended elements. This will be the size of the underlying vector.
  392. * \param ptr Pointer to a GLuint to copy index data from.
  393. * \param numInstances When non zero passed as the number of draw instances to use re.
  394. */
  395. DrawElementsUInt(GLenum mode, unsigned int no, const GLuint* ptr, int numInstances=0) :
  396. DrawElements(DrawElementsUIntPrimitiveType,mode,numInstances),
  397. vector_type(ptr,ptr+no) {}
  398. /**
  399. * \param mode One of osg::PrimitiveSet::Mode. Determines the type of primitives used.
  400. * \param no Number of intended elements. This will be the size of the underlying vector.
  401. */
  402. DrawElementsUInt(GLenum mode, unsigned int no) :
  403. DrawElements(DrawElementsUIntPrimitiveType,mode),
  404. vector_type(no) {}
  405. template <class InputIterator>
  406. DrawElementsUInt(GLenum mode, InputIterator first,InputIterator last) :
  407. DrawElements(DrawElementsUIntPrimitiveType,mode),
  408. vector_type(first,last) {}
  409. virtual Object* cloneType() const { return new DrawElementsUInt(); }
  410. virtual Object* clone(const CopyOp& copyop) const { return new DrawElementsUInt(*this,copyop); }
  411. virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const DrawElementsUInt*>(obj)!=NULL; }
  412. virtual const char* libraryName() const { return "osg"; }
  413. virtual const char* className() const { return "DrawElementsUInt"; }
  414. virtual const GLvoid* getDataPointer() const { return empty()?0:&front(); }
  415. virtual unsigned int getTotalDataSize() const { return 4u*static_cast<unsigned int>(size()); }
  416. virtual bool supportsBufferObject() const { return false; }
  417. virtual void draw(State& state, bool useVertexBufferObjects) const;
  418. virtual void accept(PrimitiveFunctor& functor) const;
  419. virtual void accept(PrimitiveIndexFunctor& functor) const;
  420. virtual unsigned int getNumIndices() const { return static_cast<unsigned int>(size()); }
  421. virtual unsigned int index(unsigned int pos) const { return (*this)[pos]; }
  422. virtual void offsetIndices(int offset);
  423. virtual GLenum getDataType() { return GL_UNSIGNED_INT; }
  424. virtual void resizeElements(unsigned int numIndices) { resize(numIndices); }
  425. virtual void reserveElements(unsigned int numIndices) { reserve(numIndices); }
  426. virtual void setElement(unsigned int i, unsigned int v) { (*this)[i] = v; }
  427. virtual unsigned int getElement(unsigned int i) { return (*this)[i]; }
  428. virtual void addElement(unsigned int v) { push_back(GLuint(v)); }
  429. protected:
  430. virtual ~DrawElementsUInt();
  431. };
  432. #ifdef OSG_HAS_MULTIDRAWARRAYS
  433. class OSG_EXPORT MultiDrawArrays : public osg::PrimitiveSet
  434. {
  435. public:
  436. MultiDrawArrays(GLenum mode=0):
  437. osg::PrimitiveSet(Type(MultiDrawArraysPrimitiveType), mode) {}
  438. MultiDrawArrays(const MultiDrawArrays& dal,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  439. osg::PrimitiveSet(dal,copyop),
  440. _firsts(dal._firsts),
  441. _counts(dal._counts) {}
  442. virtual osg::Object* cloneType() const { return new MultiDrawArrays(); }
  443. virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new MultiDrawArrays(*this,copyop); }
  444. virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const MultiDrawArrays*>(obj)!=NULL; }
  445. virtual const char* libraryName() const { return "osg"; }
  446. virtual const char* className() const { return "MultiDrawArrays"; }
  447. virtual void draw(osg::State& state, bool useVertexBufferObjects) const;
  448. virtual void accept(PrimitiveFunctor& functor) const;
  449. virtual void accept(PrimitiveIndexFunctor& functor) const;
  450. virtual unsigned int getNumIndices() const;
  451. virtual unsigned int index(unsigned int pos) const;
  452. virtual void offsetIndices(int offset);
  453. virtual unsigned int getNumPrimitives() const;
  454. typedef std::vector<GLint> Firsts;
  455. void setFirsts(const Firsts& firsts) { _firsts = firsts; }
  456. Firsts& getFirsts() { return _firsts; }
  457. const Firsts& getFirsts() const { return _firsts; }
  458. typedef std::vector<GLsizei> Counts;
  459. void setCounts(const Counts& firsts) { _counts = firsts; }
  460. Counts& getCounts() { return _counts; }
  461. const Counts& getCounts() const { return _counts; }
  462. void add(GLint first, GLsizei count);
  463. protected:
  464. Firsts _firsts;
  465. Counts _counts;
  466. };
  467. #endif
  468. }
  469. #endif