TemplatePrimitiveFunctor 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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_TERMPLATEPRIMITIVEFUNCTOR
  14. #define OSG_TERMPLATEPRIMITIVEFUNCTOR 1
  15. #include <osg/PrimitiveSet>
  16. #include <osg/Notify>
  17. namespace osg {
  18. /** Provides access to the primitives that compose an \c osg::Drawable.
  19. * <p>Notice that \c TemplatePrimitiveFunctor is a class template, and that it inherits
  20. * from its template parameter \c T. This template parameter must implement
  21. * <tt>operator()(const osg::Vec3 v1, const osg::Vec3 v2, const osg::Vec3
  22. * v3, bool treatVertexDataAsTemporary)</tt>,
  23. * <tt>operator()(const osg::Vec3 v1, const osg::Vec3 v2, bool
  24. * treatVertexDataAsTemporary)</tt>, <tt>operator()(const osg::Vec3 v1,
  25. * const osg::Vec3 v2, const osg::Vec3 v3, bool treatVertexDataAsTemporary)</tt>,
  26. * and <tt>operator()(const osg::Vec3 v1, const osg::Vec3 v2, const osg::Vec3 v3,
  27. * const osg::Vec3 v4, bool treatVertexDataAsTemporary)</tt> which will be called
  28. * for the matching primitive when the functor is applied to a \c Drawable.
  29. * Parameters \c v1, \c v2, \c v3, and \c v4 are the vertices of the primitive.
  30. * The last parameter, \c treatVertexDataAsTemporary, indicates whether these
  31. * vertices are coming from a "real" vertex array, or from a temporary vertex array,
  32. * created by the \c TemplatePrimitiveFunctor from some other geometry representation.
  33. * @see \c PrimitiveFunctor for general usage hints.
  34. */
  35. template<class T>
  36. class TemplatePrimitiveFunctor : public PrimitiveFunctor, public T
  37. {
  38. public:
  39. TemplatePrimitiveFunctor()
  40. {
  41. _vertexArraySize=0;
  42. _vertexArrayPtr=0;
  43. }
  44. virtual ~TemplatePrimitiveFunctor() {}
  45. virtual void setVertexArray(unsigned int,const Vec2*)
  46. {
  47. notify(WARN)<<"Triangle Functor does not support Vec2* vertex arrays"<<std::endl;
  48. }
  49. virtual void setVertexArray(unsigned int count,const Vec3* vertices)
  50. {
  51. _vertexArraySize = count;
  52. _vertexArrayPtr = vertices;
  53. }
  54. virtual void setVertexArray(unsigned int,const Vec4* )
  55. {
  56. notify(WARN)<<"Triangle Functor does not support Vec4* vertex arrays"<<std::endl;
  57. }
  58. virtual void setVertexArray(unsigned int,const Vec2d*)
  59. {
  60. notify(WARN)<<"Triangle Functor does not support Vec2d* vertex arrays"<<std::endl;
  61. }
  62. virtual void setVertexArray(unsigned int,const Vec3d*)
  63. {
  64. notify(WARN)<<"Triangle Functor does not support Vec3d* vertex arrays"<<std::endl;
  65. }
  66. virtual void setVertexArray(unsigned int,const Vec4d* )
  67. {
  68. notify(WARN)<<"Triangle Functor does not support Vec4d* vertex arrays"<<std::endl;
  69. }
  70. virtual void drawArrays(GLenum mode,GLint first,GLsizei count)
  71. {
  72. if (_vertexArrayPtr==0 || count==0) return;
  73. switch(mode)
  74. {
  75. case(GL_TRIANGLES): {
  76. const Vec3* vlast = &_vertexArrayPtr[first+count];
  77. for(const Vec3* vptr=&_vertexArrayPtr[first];vptr<vlast;vptr+=3)
  78. this->operator()(*(vptr),*(vptr+1),*(vptr+2),false);
  79. break;
  80. }
  81. case(GL_TRIANGLE_STRIP): {
  82. const Vec3* vptr = &_vertexArrayPtr[first];
  83. for(GLsizei i=2;i<count;++i,++vptr)
  84. {
  85. if ((i%2)) this->operator()(*(vptr),*(vptr+2),*(vptr+1),false);
  86. else this->operator()(*(vptr),*(vptr+1),*(vptr+2),false);
  87. }
  88. break;
  89. }
  90. case(GL_QUADS): {
  91. const Vec3* vptr = &_vertexArrayPtr[first];
  92. for(GLsizei i=3;i<count;i+=4,vptr+=4)
  93. {
  94. this->operator()(*(vptr),*(vptr+1),*(vptr+2),*(vptr+3),false);
  95. }
  96. break;
  97. }
  98. case(GL_QUAD_STRIP): {
  99. const Vec3* vptr = &_vertexArrayPtr[first];
  100. for(GLsizei i=3;i<count;i+=2,vptr+=2)
  101. {
  102. this->operator()(*(vptr),*(vptr+1),*(vptr+3),*(vptr+2),false);
  103. }
  104. break;
  105. }
  106. case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
  107. case(GL_TRIANGLE_FAN): {
  108. const Vec3* vfirst = &_vertexArrayPtr[first];
  109. const Vec3* vptr = vfirst+1;
  110. for(GLsizei i=2;i<count;++i,++vptr)
  111. {
  112. this->operator()(*(vfirst),*(vptr),*(vptr+1),false);
  113. }
  114. break;
  115. }
  116. case(GL_POINTS): {
  117. const Vec3* vlast = &_vertexArrayPtr[first+count];
  118. for(const Vec3* vptr=&_vertexArrayPtr[first];vptr<vlast;vptr+=1)
  119. this->operator()(*(vptr),false);
  120. break;
  121. }
  122. case(GL_LINES): {
  123. const Vec3* vlast = &_vertexArrayPtr[first+count-1];
  124. for(const Vec3* vptr=&_vertexArrayPtr[first];vptr<vlast;vptr+=2)
  125. this->operator()(*(vptr),*(vptr+1),false);
  126. break;
  127. }
  128. case(GL_LINE_STRIP): {
  129. const Vec3* vlast = &_vertexArrayPtr[first+count-1];
  130. for(const Vec3* vptr=&_vertexArrayPtr[first];vptr<vlast;vptr+=1)
  131. this->operator()(*(vptr),*(vptr+1),false);
  132. break;
  133. }
  134. case(GL_LINE_STRIP_ADJACENCY): {
  135. const Vec3* vlast = &_vertexArrayPtr[first+count-2];
  136. for(const Vec3* vptr=&_vertexArrayPtr[first+1];vptr<vlast;vptr+=1)
  137. this->operator()(*(vptr),*(vptr+1),false);
  138. break;
  139. }
  140. case(GL_LINE_LOOP): {
  141. const Vec3* vlast = &_vertexArrayPtr[first+count-1];
  142. for(const Vec3* vptr=&_vertexArrayPtr[first];vptr<vlast;vptr+=1)
  143. this->operator()(*(vptr),*(vptr+1),false);
  144. this->operator()(*(vlast),_vertexArrayPtr[first],false);
  145. break;
  146. }
  147. default:
  148. break;
  149. }
  150. }
  151. template<class IndexType>
  152. void drawElementsTemplate(GLenum mode,GLsizei count,const IndexType* indices)
  153. {
  154. if (indices==0 || count==0) return;
  155. typedef const IndexType* IndexPointer;
  156. switch(mode)
  157. {
  158. case(GL_TRIANGLES): {
  159. IndexPointer ilast = &indices[count];
  160. for(IndexPointer iptr=indices;iptr<ilast;iptr+=3)
  161. this->operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],_vertexArrayPtr[*(iptr+2)],false);
  162. break;
  163. }
  164. case(GL_TRIANGLE_STRIP): {
  165. IndexPointer iptr = indices;
  166. for(GLsizei i=2;i<count;++i,++iptr)
  167. {
  168. if ((i%2)) this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+2)],
  169. _vertexArrayPtr[*(iptr+1)],false);
  170. else this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],
  171. _vertexArrayPtr[*(iptr+2)],false);
  172. }
  173. break;
  174. }
  175. case(GL_QUADS): {
  176. IndexPointer iptr = indices;
  177. for(GLsizei i=3;i<count;i+=4,iptr+=4)
  178. {
  179. this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],
  180. _vertexArrayPtr[*(iptr+2)],_vertexArrayPtr[*(iptr+3)],
  181. false);
  182. }
  183. break;
  184. }
  185. case(GL_QUAD_STRIP): {
  186. IndexPointer iptr = indices;
  187. for(GLsizei i=3;i<count;i+=2,iptr+=2)
  188. {
  189. this->operator()(_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],
  190. _vertexArrayPtr[*(iptr+3)],_vertexArrayPtr[*(iptr+2)],
  191. false);
  192. }
  193. break;
  194. }
  195. case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN
  196. case(GL_TRIANGLE_FAN): {
  197. IndexPointer iptr = indices;
  198. const Vec3& vfirst = _vertexArrayPtr[*iptr];
  199. ++iptr;
  200. for(GLsizei i=2;i<count;++i,++iptr)
  201. {
  202. this->operator()(vfirst,_vertexArrayPtr[*(iptr)],_vertexArrayPtr[*(iptr+1)],
  203. false);
  204. }
  205. break;
  206. }
  207. case(GL_POINTS): {
  208. IndexPointer ilast = &indices[count];
  209. for(IndexPointer iptr=indices;iptr<ilast;iptr+=1)
  210. this->operator()(_vertexArrayPtr[*iptr],false);
  211. break;
  212. }
  213. case(GL_LINES): {
  214. IndexPointer ilast = &indices[count-1];
  215. for(IndexPointer iptr=indices;iptr<ilast;iptr+=2)
  216. this->operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],
  217. false);
  218. break;
  219. }
  220. case(GL_LINE_STRIP): {
  221. IndexPointer ilast = &indices[count-1];
  222. for(IndexPointer iptr=indices;iptr<ilast;iptr+=1)
  223. this->operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],
  224. false);
  225. break;
  226. }
  227. case(GL_LINE_STRIP_ADJACENCY): {
  228. IndexPointer ilast = &indices[count-2];
  229. for(IndexPointer iptr=&indices[1];iptr<ilast;iptr+=1)
  230. this->operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],
  231. false);
  232. break;
  233. }
  234. case(GL_LINE_LOOP): {
  235. IndexPointer ilast = &indices[count-1];
  236. for(IndexPointer iptr=indices;iptr<ilast;iptr+=1)
  237. this->operator()(_vertexArrayPtr[*iptr],_vertexArrayPtr[*(iptr+1)],
  238. false);
  239. this->operator()(_vertexArrayPtr[*(ilast)],_vertexArrayPtr[indices[0]],
  240. false);
  241. break;
  242. }
  243. default:
  244. break;
  245. }
  246. }
  247. virtual void drawElements(GLenum mode,GLsizei count,const GLubyte* indices)
  248. {
  249. drawElementsTemplate(mode, count, indices);
  250. }
  251. virtual void drawElements(GLenum mode,GLsizei count,const GLushort* indices)
  252. {
  253. drawElementsTemplate(mode, count, indices);
  254. }
  255. virtual void drawElements(GLenum mode,GLsizei count,const GLuint* indices)
  256. {
  257. drawElementsTemplate(mode, count, indices);
  258. }
  259. protected:
  260. unsigned int _vertexArraySize;
  261. const Vec3* _vertexArrayPtr;
  262. };
  263. }
  264. #endif