CCDrawingPrimitives.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2013 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2017 Chukong Technologies Inc.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. /*
  24. *
  25. * IMPORTANT IMPORTANT IMPORTANT IMPORTANT
  26. *
  27. *
  28. * LEGACY FUNCTIONS
  29. *
  30. * USE DrawNode instead
  31. *
  32. */
  33. #include "2d/CCDrawingPrimitives.h"
  34. #include <string.h>
  35. #include <cmath>
  36. #include "2d/CCActionCatmullRom.h"
  37. #include "base/CCDirector.h"
  38. #include "renderer/ccGLStateCache.h"
  39. #include "renderer/CCGLProgramCache.h"
  40. #include "renderer/CCRenderer.h"
  41. #include "platform/CCGL.h"
  42. NS_CC_BEGIN
  43. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  44. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  45. #elif _MSC_VER >= 1400 //vs 2005 or higher
  46. #pragma warning (push)
  47. #pragma warning (disable: 4996)
  48. #endif
  49. #ifndef M_PI
  50. #define M_PI 3.14159265358979323846
  51. #endif
  52. namespace DrawPrimitives {
  53. static bool s_initialized = false;
  54. static GLProgram* s_shader = nullptr;
  55. static int s_colorLocation = -1;
  56. static Color4F s_color(1.0f,1.0f,1.0f,1.0f);
  57. static int s_pointSizeLocation = -1;
  58. static GLfloat s_pointSize = 1.0f;
  59. static void lazy_init()
  60. {
  61. if( ! s_initialized ) {
  62. //
  63. // Position and 1 color passed as a uniform (to simulate glColor4ub )
  64. //
  65. s_shader = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_U_COLOR);
  66. s_shader->retain();
  67. s_colorLocation = s_shader->getUniformLocation("u_color");
  68. CHECK_GL_ERROR_DEBUG();
  69. s_pointSizeLocation = s_shader->getUniformLocation("u_pointSize");
  70. CHECK_GL_ERROR_DEBUG();
  71. s_initialized = true;
  72. }
  73. }
  74. // When switching from background to foreground on android, we want the parameters to be initialized again
  75. void init()
  76. {
  77. lazy_init();
  78. }
  79. void free()
  80. {
  81. CC_SAFE_RELEASE_NULL(s_shader);
  82. s_initialized = false;
  83. }
  84. void drawPoint(const Vec2& point)
  85. {
  86. lazy_init();
  87. Vec2 p;
  88. p.x = point.x;
  89. p.y = point.y;
  90. GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
  91. s_shader->use();
  92. s_shader->setUniformsForBuiltins();
  93. s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
  94. s_shader->setUniformLocationWith1f(s_pointSizeLocation, s_pointSize);
  95. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, &p);
  96. glDrawArrays(GL_POINTS, 0, 1);
  97. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,1);
  98. }
  99. void drawPoints( const Vec2 *points, unsigned int numberOfPoints )
  100. {
  101. lazy_init();
  102. GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
  103. s_shader->use();
  104. s_shader->setUniformsForBuiltins();
  105. s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
  106. s_shader->setUniformLocationWith1f(s_pointSizeLocation, s_pointSize);
  107. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, points);
  108. glDrawArrays(GL_POINTS, 0, (GLsizei) numberOfPoints);
  109. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, numberOfPoints);
  110. }
  111. void drawLine(const Vec2& origin, const Vec2& destination)
  112. {
  113. lazy_init();
  114. Vec2 vertices[2] = {
  115. Vec2(origin.x, origin.y),
  116. Vec2(destination.x, destination.y)
  117. };
  118. s_shader->use();
  119. s_shader->setUniformsForBuiltins();
  120. s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
  121. GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
  122. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  123. glDrawArrays(GL_LINES, 0, 2);
  124. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,2);
  125. }
  126. void drawRect( Vec2 origin, Vec2 destination )
  127. {
  128. drawLine(Vec2(origin.x, origin.y), Vec2(destination.x, origin.y));
  129. drawLine(Vec2(destination.x, origin.y), Vec2(destination.x, destination.y));
  130. drawLine(Vec2(destination.x, destination.y), Vec2(origin.x, destination.y));
  131. drawLine(Vec2(origin.x, destination.y), Vec2(origin.x, origin.y));
  132. }
  133. void drawSolidRect(Vec2 origin, Vec2 destination, Color4F color)
  134. {
  135. Vec2 vertices[] = {
  136. origin,
  137. Vec2(destination.x, origin.y),
  138. destination,
  139. Vec2(origin.x, destination.y)
  140. };
  141. drawSolidPoly(vertices, 4, color );
  142. }
  143. void drawPoly(const Vec2 *poli, unsigned int numberOfPoints, bool closePolygon)
  144. {
  145. lazy_init();
  146. s_shader->use();
  147. s_shader->setUniformsForBuiltins();
  148. s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
  149. GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
  150. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, poli);
  151. if( closePolygon )
  152. glDrawArrays(GL_LINE_LOOP, 0, (GLsizei) numberOfPoints);
  153. else
  154. glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) numberOfPoints);
  155. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, numberOfPoints);
  156. }
  157. void drawSolidPoly(const Vec2 *poli, unsigned int numberOfPoints, Color4F color)
  158. {
  159. lazy_init();
  160. s_shader->use();
  161. s_shader->setUniformsForBuiltins();
  162. s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &color.r, 1);
  163. GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
  164. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, poli);
  165. glDrawArrays(GL_TRIANGLE_FAN, 0, (GLsizei) numberOfPoints);
  166. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, numberOfPoints);
  167. }
  168. void drawCircle( const Vec2& center, float radius, float angle, unsigned int segments, bool drawLineToCenter, float scaleX, float scaleY)
  169. {
  170. lazy_init();
  171. int additionalSegment = 1;
  172. if (drawLineToCenter)
  173. additionalSegment++;
  174. const float coef = 2.0f * (float)M_PI/segments;
  175. GLfloat *vertices = (GLfloat*)calloc( sizeof(GLfloat)*2*(segments+2), 1);
  176. if( ! vertices )
  177. return;
  178. for(unsigned int i = 0;i <= segments; i++) {
  179. float rads = i*coef;
  180. GLfloat j = radius * cosf(rads + angle) * scaleX + center.x;
  181. GLfloat k = radius * sinf(rads + angle) * scaleY + center.y;
  182. vertices[i*2] = j;
  183. vertices[i*2+1] = k;
  184. }
  185. vertices[(segments+1)*2] = center.x;
  186. vertices[(segments+1)*2+1] = center.y;
  187. s_shader->use();
  188. s_shader->setUniformsForBuiltins();
  189. s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
  190. GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
  191. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  192. glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segments+additionalSegment);
  193. ::free( vertices );
  194. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,segments+additionalSegment);
  195. }
  196. void drawCircle( const Vec2& center, float radius, float angle, unsigned int segments, bool drawLineToCenter)
  197. {
  198. drawCircle(center, radius, angle, segments, drawLineToCenter, 1.0f, 1.0f);
  199. }
  200. void drawSolidCircle( const Vec2& center, float radius, float angle, unsigned int segments, float scaleX, float scaleY)
  201. {
  202. lazy_init();
  203. const float coef = 2.0f * (float)M_PI/segments;
  204. GLfloat *vertices = (GLfloat*)calloc( sizeof(GLfloat)*2*(segments+2), 1);
  205. if( ! vertices )
  206. return;
  207. for(unsigned int i = 0;i <= segments; i++) {
  208. float rads = i*coef;
  209. GLfloat j = radius * cosf(rads + angle) * scaleX + center.x;
  210. GLfloat k = radius * sinf(rads + angle) * scaleY + center.y;
  211. vertices[i*2] = j;
  212. vertices[i*2+1] = k;
  213. }
  214. vertices[(segments+1)*2] = center.x;
  215. vertices[(segments+1)*2+1] = center.y;
  216. s_shader->use();
  217. s_shader->setUniformsForBuiltins();
  218. s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
  219. GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
  220. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  221. glDrawArrays(GL_TRIANGLE_FAN, 0, (GLsizei) segments+1);
  222. ::free( vertices );
  223. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,segments+1);
  224. }
  225. void drawSolidCircle( const Vec2& center, float radius, float angle, unsigned int segments)
  226. {
  227. drawSolidCircle(center, radius, angle, segments, 1.0f, 1.0f);
  228. }
  229. void drawQuadBezier(const Vec2& origin, const Vec2& control, const Vec2& destination, unsigned int segments)
  230. {
  231. lazy_init();
  232. Vec2* vertices = new (std::nothrow) Vec2[segments + 1];
  233. float t = 0.0f;
  234. for(unsigned int i = 0; i < segments; i++)
  235. {
  236. vertices[i].x = powf(1 - t, 2) * origin.x + 2.0f * (1 - t) * t * control.x + t * t * destination.x;
  237. vertices[i].y = powf(1 - t, 2) * origin.y + 2.0f * (1 - t) * t * control.y + t * t * destination.y;
  238. t += 1.0f / segments;
  239. }
  240. vertices[segments].x = destination.x;
  241. vertices[segments].y = destination.y;
  242. s_shader->use();
  243. s_shader->setUniformsForBuiltins();
  244. s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
  245. GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
  246. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  247. glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segments + 1);
  248. CC_SAFE_DELETE_ARRAY(vertices);
  249. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,segments+1);
  250. }
  251. void drawCatmullRom( PointArray *points, unsigned int segments )
  252. {
  253. drawCardinalSpline( points, 0.5f, segments );
  254. }
  255. void drawCardinalSpline( PointArray *config, float tension, unsigned int segments )
  256. {
  257. lazy_init();
  258. Vec2* vertices = new (std::nothrow) Vec2[segments + 1];
  259. ssize_t p;
  260. float lt;
  261. float deltaT = 1.0f / config->count();
  262. for( unsigned int i=0; i < segments+1;i++) {
  263. float dt = (float)i / segments;
  264. // border
  265. if( dt == 1 ) {
  266. p = config->count() - 1;
  267. lt = 1;
  268. } else {
  269. p = dt / deltaT;
  270. lt = (dt - deltaT * (float)p) / deltaT;
  271. }
  272. // Interpolate
  273. Vec2 pp0 = config->getControlPointAtIndex(p-1);
  274. Vec2 pp1 = config->getControlPointAtIndex(p+0);
  275. Vec2 pp2 = config->getControlPointAtIndex(p+1);
  276. Vec2 pp3 = config->getControlPointAtIndex(p+2);
  277. Vec2 newPos = ccCardinalSplineAt( pp0, pp1, pp2, pp3, tension, lt);
  278. vertices[i].x = newPos.x;
  279. vertices[i].y = newPos.y;
  280. }
  281. s_shader->use();
  282. s_shader->setUniformsForBuiltins();
  283. s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*)&s_color.r, 1);
  284. GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
  285. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  286. glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segments + 1);
  287. CC_SAFE_DELETE_ARRAY(vertices);
  288. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,segments+1);
  289. }
  290. void drawCubicBezier(const Vec2& origin, const Vec2& control1, const Vec2& control2, const Vec2& destination, unsigned int segments)
  291. {
  292. lazy_init();
  293. Vec2* vertices = new (std::nothrow) Vec2[segments + 1];
  294. float t = 0;
  295. for (unsigned int i = 0; i < segments; i++)
  296. {
  297. vertices[i].x = powf(1 - t, 3) * origin.x + 3.0f * powf(1 - t, 2) * t * control1.x + 3.0f * (1 - t) * t * t * control2.x + t * t * t * destination.x;
  298. vertices[i].y = powf(1 - t, 3) * origin.y + 3.0f * powf(1 - t, 2) * t * control1.y + 3.0f * (1 - t) * t * t * control2.y + t * t * t * destination.y;
  299. t += 1.0f / segments;
  300. }
  301. vertices[segments].x = destination.x;
  302. vertices[segments].y = destination.y;
  303. s_shader->use();
  304. s_shader->setUniformsForBuiltins();
  305. s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
  306. GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
  307. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  308. glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segments + 1);
  309. CC_SAFE_DELETE_ARRAY(vertices);
  310. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,segments+1);
  311. }
  312. void setDrawColor4F( GLfloat r, GLfloat g, GLfloat b, GLfloat a )
  313. {
  314. s_color.r = r;
  315. s_color.g = g;
  316. s_color.b = b;
  317. s_color.a = a;
  318. }
  319. void setPointSize( GLfloat pointSize )
  320. {
  321. s_pointSize = pointSize * CC_CONTENT_SCALE_FACTOR();
  322. // TODO: glPointSize( pointSize );
  323. }
  324. void setDrawColor4B( GLubyte r, GLubyte g, GLubyte b, GLubyte a )
  325. {
  326. s_color.r = r/255.0f;
  327. s_color.g = g/255.0f;
  328. s_color.b = b/255.0f;
  329. s_color.a = a/255.0f;
  330. }
  331. } // DrawPrimitives namespace
  332. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  333. #pragma GCC diagnostic warning "-Wdeprecated-declarations"
  334. #elif _MSC_VER >= 1400 //vs 2005 or higher
  335. #pragma warning (pop)
  336. #endif
  337. NS_CC_END