TriangleFunctor 13 KB

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