CCDrawNode.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
  2. * Copyright (c) 2012 cocos2d-x.org
  3. * Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  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 THE
  21. * SOFTWARE.
  22. */
  23. #include "2d/CCDrawNode.h"
  24. #include "base/CCEventType.h"
  25. #include "base/CCConfiguration.h"
  26. #include "renderer/CCRenderer.h"
  27. #include "renderer/ccGLStateCache.h"
  28. #include "renderer/CCGLProgramState.h"
  29. #include "renderer/CCGLProgramCache.h"
  30. #include "base/CCDirector.h"
  31. #include "base/CCEventListenerCustom.h"
  32. #include "base/CCEventDispatcher.h"
  33. #include "2d/CCActionCatmullRom.h"
  34. #include "platform/CCGL.h"
  35. NS_CC_BEGIN
  36. // Vec2 == CGPoint in 32-bits, but not in 64-bits (OS X)
  37. // that's why the "v2f" functions are needed
  38. static Vec2 v2fzero(0.0f,0.0f);
  39. static inline Vec2 v2f(float x, float y)
  40. {
  41. Vec2 ret(x, y);
  42. return ret;
  43. }
  44. static inline Vec2 v2fadd(const Vec2 &v0, const Vec2 &v1)
  45. {
  46. return v2f(v0.x+v1.x, v0.y+v1.y);
  47. }
  48. static inline Vec2 v2fsub(const Vec2 &v0, const Vec2 &v1)
  49. {
  50. return v2f(v0.x-v1.x, v0.y-v1.y);
  51. }
  52. static inline Vec2 v2fmult(const Vec2 &v, float s)
  53. {
  54. return v2f(v.x * s, v.y * s);
  55. }
  56. static inline Vec2 v2fperp(const Vec2 &p0)
  57. {
  58. return v2f(-p0.y, p0.x);
  59. }
  60. static inline Vec2 v2fneg(const Vec2 &p0)
  61. {
  62. return v2f(-p0.x, - p0.y);
  63. }
  64. static inline float v2fdot(const Vec2 &p0, const Vec2 &p1)
  65. {
  66. return p0.x * p1.x + p0.y * p1.y;
  67. }
  68. static inline Vec2 v2fnormalize(const Vec2 &p)
  69. {
  70. Vec2 r(p.x, p.y);
  71. r.normalize();
  72. return v2f(r.x, r.y);
  73. }
  74. static inline Vec2 __v2f(const Vec2 &v)
  75. {
  76. //#ifdef __LP64__
  77. return v2f(v.x, v.y);
  78. // #else
  79. // return * ((Vec2*) &v);
  80. // #endif
  81. }
  82. static inline Tex2F __t(const Vec2 &v)
  83. {
  84. return *(Tex2F*)&v;
  85. }
  86. // implementation of DrawNode
  87. DrawNode::DrawNode(GLfloat lineWidth)
  88. : _vao(0)
  89. , _vbo(0)
  90. , _vaoGLPoint(0)
  91. , _vboGLPoint(0)
  92. , _vaoGLLine(0)
  93. , _vboGLLine(0)
  94. , _bufferCapacity(0)
  95. , _bufferCount(0)
  96. , _buffer(nullptr)
  97. , _bufferCapacityGLPoint(0)
  98. , _bufferCountGLPoint(0)
  99. , _bufferGLPoint(nullptr)
  100. , _bufferCapacityGLLine(0)
  101. , _bufferCountGLLine(0)
  102. , _bufferGLLine(nullptr)
  103. , _dirty(false)
  104. , _dirtyGLPoint(false)
  105. , _dirtyGLLine(false)
  106. , _lineWidth(lineWidth)
  107. , _defaultLineWidth(lineWidth)
  108. {
  109. _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
  110. }
  111. DrawNode::~DrawNode()
  112. {
  113. free(_buffer);
  114. _buffer = nullptr;
  115. free(_bufferGLPoint);
  116. _bufferGLPoint = nullptr;
  117. free(_bufferGLLine);
  118. _bufferGLLine = nullptr;
  119. glDeleteBuffers(1, &_vbo);
  120. glDeleteBuffers(1, &_vboGLLine);
  121. glDeleteBuffers(1, &_vboGLPoint);
  122. _vbo = 0;
  123. _vboGLPoint = 0;
  124. _vboGLLine = 0;
  125. if (Configuration::getInstance()->supportsShareableVAO())
  126. {
  127. GL::bindVAO(0);
  128. glDeleteVertexArrays(1, &_vao);
  129. glDeleteVertexArrays(1, &_vaoGLLine);
  130. glDeleteVertexArrays(1, &_vaoGLPoint);
  131. _vao = _vaoGLLine = _vaoGLPoint = 0;
  132. }
  133. }
  134. DrawNode* DrawNode::create(GLfloat defaultLineWidth)
  135. {
  136. DrawNode* ret = new (std::nothrow) DrawNode(defaultLineWidth);
  137. if (ret && ret->init())
  138. {
  139. ret->autorelease();
  140. }
  141. else
  142. {
  143. CC_SAFE_DELETE(ret);
  144. }
  145. return ret;
  146. }
  147. void DrawNode::ensureCapacity(int count)
  148. {
  149. CCASSERT(count>=0, "capacity must be >= 0");
  150. if(_bufferCount + count > _bufferCapacity)
  151. {
  152. _bufferCapacity += MAX(_bufferCapacity, count);
  153. _buffer = (V2F_C4B_T2F*)realloc(_buffer, _bufferCapacity*sizeof(V2F_C4B_T2F));
  154. }
  155. }
  156. void DrawNode::ensureCapacityGLPoint(int count)
  157. {
  158. CCASSERT(count>=0, "capacity must be >= 0");
  159. if(_bufferCountGLPoint + count > _bufferCapacityGLPoint)
  160. {
  161. _bufferCapacityGLPoint += MAX(_bufferCapacityGLPoint, count);
  162. _bufferGLPoint = (V2F_C4B_T2F*)realloc(_bufferGLPoint, _bufferCapacityGLPoint*sizeof(V2F_C4B_T2F));
  163. }
  164. }
  165. void DrawNode::ensureCapacityGLLine(int count)
  166. {
  167. CCASSERT(count>=0, "capacity must be >= 0");
  168. if(_bufferCountGLLine + count > _bufferCapacityGLLine)
  169. {
  170. _bufferCapacityGLLine += MAX(_bufferCapacityGLLine, count);
  171. _bufferGLLine = (V2F_C4B_T2F*)realloc(_bufferGLLine, _bufferCapacityGLLine*sizeof(V2F_C4B_T2F));
  172. }
  173. }
  174. bool DrawNode::init()
  175. {
  176. _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
  177. setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR));
  178. ensureCapacity(512);
  179. ensureCapacityGLPoint(64);
  180. ensureCapacityGLLine(256);
  181. if (Configuration::getInstance()->supportsShareableVAO())
  182. {
  183. glGenVertexArrays(1, &_vao);
  184. GL::bindVAO(_vao);
  185. glGenBuffers(1, &_vbo);
  186. glBindBuffer(GL_ARRAY_BUFFER, _vbo);
  187. glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)* _bufferCapacity, _buffer, GL_STREAM_DRAW);
  188. // vertex
  189. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
  190. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
  191. // color
  192. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
  193. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
  194. // texcoord
  195. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
  196. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
  197. glGenVertexArrays(1, &_vaoGLLine);
  198. GL::bindVAO(_vaoGLLine);
  199. glGenBuffers(1, &_vboGLLine);
  200. glBindBuffer(GL_ARRAY_BUFFER, _vboGLLine);
  201. glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLLine, _bufferGLLine, GL_STREAM_DRAW);
  202. // vertex
  203. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
  204. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
  205. // color
  206. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
  207. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
  208. // texcoord
  209. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
  210. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
  211. glGenVertexArrays(1, &_vaoGLPoint);
  212. GL::bindVAO(_vaoGLPoint);
  213. glGenBuffers(1, &_vboGLPoint);
  214. glBindBuffer(GL_ARRAY_BUFFER, _vboGLPoint);
  215. glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLPoint, _bufferGLPoint, GL_STREAM_DRAW);
  216. // vertex
  217. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
  218. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
  219. // color
  220. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
  221. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
  222. // Texture coord as pointsize
  223. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
  224. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
  225. GL::bindVAO(0);
  226. glBindBuffer(GL_ARRAY_BUFFER, 0);
  227. }
  228. else
  229. {
  230. glGenBuffers(1, &_vbo);
  231. glBindBuffer(GL_ARRAY_BUFFER, _vbo);
  232. glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)* _bufferCapacity, _buffer, GL_STREAM_DRAW);
  233. glGenBuffers(1, &_vboGLLine);
  234. glBindBuffer(GL_ARRAY_BUFFER, _vboGLLine);
  235. glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLLine, _bufferGLLine, GL_STREAM_DRAW);
  236. glGenBuffers(1, &_vboGLPoint);
  237. glBindBuffer(GL_ARRAY_BUFFER, _vboGLPoint);
  238. glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLPoint, _bufferGLPoint, GL_STREAM_DRAW);
  239. glBindBuffer(GL_ARRAY_BUFFER, 0);
  240. }
  241. CHECK_GL_ERROR_DEBUG();
  242. _dirty = true;
  243. _dirtyGLLine = true;
  244. _dirtyGLPoint = true;
  245. #if CC_ENABLE_CACHE_TEXTURE_DATA
  246. // Need to listen the event only when not use batchnode, because it will use VBO
  247. auto listener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom* event){
  248. /** listen the event that renderer was recreated on Android/WP8 */
  249. this->init();
  250. });
  251. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
  252. #endif
  253. return true;
  254. }
  255. void DrawNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
  256. {
  257. if(_bufferCount)
  258. {
  259. _customCommand.init(_globalZOrder, transform, flags);
  260. _customCommand.func = CC_CALLBACK_0(DrawNode::onDraw, this, transform, flags);
  261. renderer->addCommand(&_customCommand);
  262. }
  263. if(_bufferCountGLPoint)
  264. {
  265. _customCommandGLPoint.init(_globalZOrder, transform, flags);
  266. _customCommandGLPoint.func = CC_CALLBACK_0(DrawNode::onDrawGLPoint, this, transform, flags);
  267. renderer->addCommand(&_customCommandGLPoint);
  268. }
  269. if(_bufferCountGLLine)
  270. {
  271. _customCommandGLLine.init(_globalZOrder, transform, flags);
  272. _customCommandGLLine.func = CC_CALLBACK_0(DrawNode::onDrawGLLine, this, transform, flags);
  273. renderer->addCommand(&_customCommandGLLine);
  274. }
  275. }
  276. void DrawNode::onDraw(const Mat4 &transform, uint32_t /*flags*/)
  277. {
  278. getGLProgramState()->apply(transform);
  279. auto glProgram = this->getGLProgram();
  280. glProgram->setUniformLocationWith1f(glProgram->getUniformLocation("u_alpha"), _displayedOpacity / 255.0);
  281. GL::blendFunc(_blendFunc.src, _blendFunc.dst);
  282. if (_dirty)
  283. {
  284. glBindBuffer(GL_ARRAY_BUFFER, _vbo);
  285. glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacity, _buffer, GL_STREAM_DRAW);
  286. _dirty = false;
  287. }
  288. if (Configuration::getInstance()->supportsShareableVAO())
  289. {
  290. GL::bindVAO(_vao);
  291. }
  292. else
  293. {
  294. GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
  295. glBindBuffer(GL_ARRAY_BUFFER, _vbo);
  296. // vertex
  297. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
  298. // color
  299. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
  300. // texcoord
  301. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
  302. }
  303. glDrawArrays(GL_TRIANGLES, 0, _bufferCount);
  304. glBindBuffer(GL_ARRAY_BUFFER, 0);
  305. if (Configuration::getInstance()->supportsShareableVAO())
  306. {
  307. GL::bindVAO(0);
  308. }
  309. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _bufferCount);
  310. CHECK_GL_ERROR_DEBUG();
  311. }
  312. void DrawNode::onDrawGLLine(const Mat4 &transform, uint32_t /*flags*/)
  313. {
  314. auto glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR);
  315. glProgram->use();
  316. glProgram->setUniformsForBuiltins(transform);
  317. glProgram->setUniformLocationWith1f(glProgram->getUniformLocation("u_alpha"), _displayedOpacity / 255.0);
  318. GL::blendFunc(_blendFunc.src, _blendFunc.dst);
  319. if (_dirtyGLLine)
  320. {
  321. glBindBuffer(GL_ARRAY_BUFFER, _vboGLLine);
  322. glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLLine, _bufferGLLine, GL_STREAM_DRAW);
  323. _dirtyGLLine = false;
  324. }
  325. if (Configuration::getInstance()->supportsShareableVAO())
  326. {
  327. GL::bindVAO(_vaoGLLine);
  328. }
  329. else
  330. {
  331. glBindBuffer(GL_ARRAY_BUFFER, _vboGLLine);
  332. GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
  333. // vertex
  334. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
  335. // color
  336. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
  337. // texcoord
  338. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
  339. }
  340. glLineWidth(_lineWidth);
  341. glDrawArrays(GL_LINES, 0, _bufferCountGLLine);
  342. if (Configuration::getInstance()->supportsShareableVAO())
  343. {
  344. GL::bindVAO(0);
  345. }
  346. glBindBuffer(GL_ARRAY_BUFFER, 0);
  347. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,_bufferCountGLLine);
  348. CHECK_GL_ERROR_DEBUG();
  349. }
  350. void DrawNode::onDrawGLPoint(const Mat4 &transform, uint32_t /*flags*/)
  351. {
  352. auto glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_COLOR_TEXASPOINTSIZE);
  353. glProgram->use();
  354. glProgram->setUniformsForBuiltins(transform);
  355. glProgram->setUniformLocationWith1f(glProgram->getUniformLocation("u_alpha"), _displayedOpacity / 255.0);
  356. GL::blendFunc(_blendFunc.src, _blendFunc.dst);
  357. if (_dirtyGLPoint)
  358. {
  359. glBindBuffer(GL_ARRAY_BUFFER, _vboGLPoint);
  360. glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLPoint, _bufferGLPoint, GL_STREAM_DRAW);
  361. _dirtyGLPoint = false;
  362. }
  363. if (Configuration::getInstance()->supportsShareableVAO())
  364. {
  365. GL::bindVAO(_vaoGLPoint);
  366. }
  367. else
  368. {
  369. glBindBuffer(GL_ARRAY_BUFFER, _vboGLPoint);
  370. GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
  371. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
  372. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
  373. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
  374. }
  375. glDrawArrays(GL_POINTS, 0, _bufferCountGLPoint);
  376. if (Configuration::getInstance()->supportsShareableVAO())
  377. {
  378. GL::bindVAO(0);
  379. }
  380. glBindBuffer(GL_ARRAY_BUFFER, 0);
  381. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,_bufferCountGLPoint);
  382. CHECK_GL_ERROR_DEBUG();
  383. }
  384. void DrawNode::drawPoint(const Vec2& position, const float pointSize, const Color4F &color)
  385. {
  386. ensureCapacityGLPoint(1);
  387. V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLPoint + _bufferCountGLPoint);
  388. V2F_C4B_T2F a = {position, Color4B(color), Tex2F(pointSize,0)};
  389. *point = a;
  390. _bufferCountGLPoint += 1;
  391. _dirtyGLPoint = true;
  392. }
  393. void DrawNode::drawPoints(const Vec2 *position, unsigned int numberOfPoints, const Color4F &color)
  394. {
  395. drawPoints(position, numberOfPoints, 1.0, color);
  396. }
  397. void DrawNode::drawPoints(const Vec2 *position, unsigned int numberOfPoints, const float pointSize, const Color4F &color)
  398. {
  399. ensureCapacityGLPoint(numberOfPoints);
  400. V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLPoint + _bufferCountGLPoint);
  401. for(unsigned int i=0; i < numberOfPoints; i++,point++)
  402. {
  403. V2F_C4B_T2F a = {position[i], Color4B(color), Tex2F(pointSize,0)};
  404. *point = a;
  405. }
  406. _bufferCountGLPoint += numberOfPoints;
  407. _dirtyGLPoint = true;
  408. }
  409. void DrawNode::drawLine(const Vec2 &origin, const Vec2 &destination, const Color4F &color)
  410. {
  411. ensureCapacityGLLine(2);
  412. V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLLine + _bufferCountGLLine);
  413. V2F_C4B_T2F a = {origin, Color4B(color), Tex2F(0.0, 0.0)};
  414. V2F_C4B_T2F b = {destination, Color4B(color), Tex2F(0.0, 0.0)};
  415. *point = a;
  416. *(point+1) = b;
  417. _bufferCountGLLine += 2;
  418. _dirtyGLLine = true;
  419. }
  420. void DrawNode::drawRect(const Vec2 &origin, const Vec2 &destination, const Color4F &color)
  421. {
  422. drawLine(Vec2(origin.x, origin.y), Vec2(destination.x, origin.y), color);
  423. drawLine(Vec2(destination.x, origin.y), Vec2(destination.x, destination.y), color);
  424. drawLine(Vec2(destination.x, destination.y), Vec2(origin.x, destination.y), color);
  425. drawLine(Vec2(origin.x, destination.y), Vec2(origin.x, origin.y), color);
  426. }
  427. void DrawNode::drawPoly(const Vec2 *poli, unsigned int numberOfPoints, bool closePolygon, const Color4F &color)
  428. {
  429. unsigned int vertex_count;
  430. if(closePolygon)
  431. {
  432. vertex_count = 2 * numberOfPoints;
  433. ensureCapacityGLLine(vertex_count);
  434. }
  435. else
  436. {
  437. vertex_count = 2 * (numberOfPoints - 1);
  438. ensureCapacityGLLine(vertex_count);
  439. }
  440. V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLLine + _bufferCountGLLine);
  441. unsigned int i = 0;
  442. for(; i<numberOfPoints-1; i++)
  443. {
  444. V2F_C4B_T2F a = {poli[i], Color4B(color), Tex2F(0.0, 0.0)};
  445. V2F_C4B_T2F b = {poli[i+1], Color4B(color), Tex2F(0.0, 0.0)};
  446. *point = a;
  447. *(point+1) = b;
  448. point += 2;
  449. }
  450. if(closePolygon)
  451. {
  452. V2F_C4B_T2F a = {poli[i], Color4B(color), Tex2F(0.0, 0.0)};
  453. V2F_C4B_T2F b = {poli[0], Color4B(color), Tex2F(0.0, 0.0)};
  454. *point = a;
  455. *(point+1) = b;
  456. }
  457. _bufferCountGLLine += vertex_count;
  458. }
  459. void DrawNode::drawCircle(const Vec2& center, float radius, float angle, unsigned int segments, bool drawLineToCenter, float scaleX, float scaleY, const Color4F &color)
  460. {
  461. const float coef = 2.0f * (float)M_PI/segments;
  462. Vec2 *vertices = new (std::nothrow) Vec2[segments+2];
  463. if( ! vertices )
  464. return;
  465. for(unsigned int i = 0;i <= segments; i++) {
  466. float rads = i*coef;
  467. GLfloat j = radius * cosf(rads + angle) * scaleX + center.x;
  468. GLfloat k = radius * sinf(rads + angle) * scaleY + center.y;
  469. vertices[i].x = j;
  470. vertices[i].y = k;
  471. }
  472. if(drawLineToCenter)
  473. {
  474. vertices[segments+1].x = center.x;
  475. vertices[segments+1].y = center.y;
  476. drawPoly(vertices, segments+2, true, color);
  477. }
  478. else
  479. drawPoly(vertices, segments+1, true, color);
  480. CC_SAFE_DELETE_ARRAY(vertices);
  481. }
  482. void DrawNode::drawCircle(const Vec2 &center, float radius, float angle, unsigned int segments, bool drawLineToCenter, const Color4F &color)
  483. {
  484. drawCircle(center, radius, angle, segments, drawLineToCenter, 1.0f, 1.0f, color);
  485. }
  486. void DrawNode::drawQuadBezier(const Vec2 &origin, const Vec2 &control, const Vec2 &destination, unsigned int segments, const Color4F &color)
  487. {
  488. Vec2* vertices = new (std::nothrow) Vec2[segments + 1];
  489. if( ! vertices )
  490. return;
  491. float t = 0.0f;
  492. for(unsigned int i = 0; i < segments; i++)
  493. {
  494. vertices[i].x = powf(1 - t, 2) * origin.x + 2.0f * (1 - t) * t * control.x + t * t * destination.x;
  495. vertices[i].y = powf(1 - t, 2) * origin.y + 2.0f * (1 - t) * t * control.y + t * t * destination.y;
  496. t += 1.0f / segments;
  497. }
  498. vertices[segments].x = destination.x;
  499. vertices[segments].y = destination.y;
  500. drawPoly(vertices, segments+1, false, color);
  501. CC_SAFE_DELETE_ARRAY(vertices);
  502. }
  503. void DrawNode::drawCubicBezier(const Vec2 &origin, const Vec2 &control1, const Vec2 &control2, const Vec2 &destination, unsigned int segments, const Color4F &color)
  504. {
  505. Vec2* vertices = new (std::nothrow) Vec2[segments + 1];
  506. if( ! vertices )
  507. return;
  508. float t = 0;
  509. for (unsigned int i = 0; i < segments; i++)
  510. {
  511. 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;
  512. 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;
  513. t += 1.0f / segments;
  514. }
  515. vertices[segments].x = destination.x;
  516. vertices[segments].y = destination.y;
  517. drawPoly(vertices, segments+1, false, color);
  518. CC_SAFE_DELETE_ARRAY(vertices);
  519. }
  520. void DrawNode::drawCardinalSpline(PointArray *config, float tension, unsigned int segments, const Color4F &color)
  521. {
  522. Vec2* vertices = new (std::nothrow) Vec2[segments + 1];
  523. if( ! vertices )
  524. return;
  525. ssize_t p;
  526. float lt;
  527. float deltaT = 1.0f / config->count();
  528. for( unsigned int i=0; i < segments+1;i++) {
  529. float dt = (float)i / segments;
  530. // border
  531. if( dt == 1 ) {
  532. p = config->count() - 1;
  533. lt = 1;
  534. } else {
  535. p = dt / deltaT;
  536. lt = (dt - deltaT * (float)p) / deltaT;
  537. }
  538. // Interpolate
  539. Vec2 pp0 = config->getControlPointAtIndex(p-1);
  540. Vec2 pp1 = config->getControlPointAtIndex(p+0);
  541. Vec2 pp2 = config->getControlPointAtIndex(p+1);
  542. Vec2 pp3 = config->getControlPointAtIndex(p+2);
  543. Vec2 newPos = ccCardinalSplineAt( pp0, pp1, pp2, pp3, tension, lt);
  544. vertices[i].x = newPos.x;
  545. vertices[i].y = newPos.y;
  546. }
  547. drawPoly(vertices, segments+1, false, color);
  548. CC_SAFE_DELETE_ARRAY(vertices);
  549. }
  550. void DrawNode::drawCatmullRom(PointArray *points, unsigned int segments, const Color4F &color)
  551. {
  552. drawCardinalSpline( points, 0.5f, segments, color);
  553. }
  554. void DrawNode::drawDot(const Vec2 &pos, float radius, const Color4F &color)
  555. {
  556. unsigned int vertex_count = 2*3;
  557. ensureCapacity(vertex_count);
  558. V2F_C4B_T2F a = {Vec2(pos.x - radius, pos.y - radius), Color4B(color), Tex2F(-1.0, -1.0) };
  559. V2F_C4B_T2F b = {Vec2(pos.x - radius, pos.y + radius), Color4B(color), Tex2F(-1.0, 1.0) };
  560. V2F_C4B_T2F c = {Vec2(pos.x + radius, pos.y + radius), Color4B(color), Tex2F( 1.0, 1.0) };
  561. V2F_C4B_T2F d = {Vec2(pos.x + radius, pos.y - radius), Color4B(color), Tex2F( 1.0, -1.0) };
  562. V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
  563. V2F_C4B_T2F_Triangle triangle0 = {a, b, c};
  564. V2F_C4B_T2F_Triangle triangle1 = {a, c, d};
  565. triangles[0] = triangle0;
  566. triangles[1] = triangle1;
  567. _bufferCount += vertex_count;
  568. _dirty = true;
  569. }
  570. void DrawNode::drawRect(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, const Vec2& p4, const Color4F &color)
  571. {
  572. drawLine(Vec2(p1.x, p1.y), Vec2(p2.x, p2.y), color);
  573. drawLine(Vec2(p2.x, p2.y), Vec2(p3.x, p3.y), color);
  574. drawLine(Vec2(p3.x, p3.y), Vec2(p4.x, p4.y), color);
  575. drawLine(Vec2(p4.x, p4.y), Vec2(p1.x, p1.y), color);
  576. }
  577. void DrawNode::drawSegment(const Vec2 &from, const Vec2 &to, float radius, const Color4F &color)
  578. {
  579. unsigned int vertex_count = 6*3;
  580. ensureCapacity(vertex_count);
  581. Vec2 a = __v2f(from);
  582. Vec2 b = __v2f(to);
  583. Vec2 n = v2fnormalize(v2fperp(v2fsub(b, a)));
  584. Vec2 t = v2fperp(n);
  585. Vec2 nw = v2fmult(n, radius);
  586. Vec2 tw = v2fmult(t, radius);
  587. Vec2 v0 = v2fsub(b, v2fadd(nw, tw));
  588. Vec2 v1 = v2fadd(b, v2fsub(nw, tw));
  589. Vec2 v2 = v2fsub(b, nw);
  590. Vec2 v3 = v2fadd(b, nw);
  591. Vec2 v4 = v2fsub(a, nw);
  592. Vec2 v5 = v2fadd(a, nw);
  593. Vec2 v6 = v2fsub(a, v2fsub(nw, tw));
  594. Vec2 v7 = v2fadd(a, v2fadd(nw, tw));
  595. V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
  596. V2F_C4B_T2F_Triangle triangles0 = {
  597. {v0, Color4B(color), __t(v2fneg(v2fadd(n, t)))},
  598. {v1, Color4B(color), __t(v2fsub(n, t))},
  599. {v2, Color4B(color), __t(v2fneg(n))},
  600. };
  601. triangles[0] = triangles0;
  602. V2F_C4B_T2F_Triangle triangles1 = {
  603. {v3, Color4B(color), __t(n)},
  604. {v1, Color4B(color), __t(v2fsub(n, t))},
  605. {v2, Color4B(color), __t(v2fneg(n))},
  606. };
  607. triangles[1] = triangles1;
  608. V2F_C4B_T2F_Triangle triangles2 = {
  609. {v3, Color4B(color), __t(n)},
  610. {v4, Color4B(color), __t(v2fneg(n))},
  611. {v2, Color4B(color), __t(v2fneg(n))},
  612. };
  613. triangles[2] = triangles2;
  614. V2F_C4B_T2F_Triangle triangles3 = {
  615. {v3, Color4B(color), __t(n)},
  616. {v4, Color4B(color), __t(v2fneg(n))},
  617. {v5, Color4B(color), __t(n) },
  618. };
  619. triangles[3] = triangles3;
  620. V2F_C4B_T2F_Triangle triangles4 = {
  621. {v6, Color4B(color), __t(v2fsub(t, n))},
  622. {v4, Color4B(color), __t(v2fneg(n)) },
  623. {v5, Color4B(color), __t(n)},
  624. };
  625. triangles[4] = triangles4;
  626. V2F_C4B_T2F_Triangle triangles5 = {
  627. {v6, Color4B(color), __t(v2fsub(t, n))},
  628. {v7, Color4B(color), __t(v2fadd(n, t))},
  629. {v5, Color4B(color), __t(n)},
  630. };
  631. triangles[5] = triangles5;
  632. _bufferCount += vertex_count;
  633. _dirty = true;
  634. }
  635. void DrawNode::drawPolygon(const Vec2 *verts, int count, const Color4F &fillColor, float borderWidth, const Color4F &borderColor)
  636. {
  637. CCASSERT(count >= 0, "invalid count value");
  638. bool outline = (borderColor.a > 0.0f && borderWidth > 0.0f);
  639. auto triangle_count = outline ? (3*count - 2) : (count - 2);
  640. auto vertex_count = 3*triangle_count;
  641. ensureCapacity(vertex_count);
  642. V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
  643. V2F_C4B_T2F_Triangle *cursor = triangles;
  644. for (int i = 0; i < count-2; i++)
  645. {
  646. V2F_C4B_T2F_Triangle tmp = {
  647. {verts[0], Color4B(fillColor), __t(v2fzero)},
  648. {verts[i+1], Color4B(fillColor), __t(v2fzero)},
  649. {verts[i+2], Color4B(fillColor), __t(v2fzero)},
  650. };
  651. *cursor++ = tmp;
  652. }
  653. if(outline)
  654. {
  655. struct ExtrudeVerts {Vec2 offset, n;};
  656. struct ExtrudeVerts* extrude = (struct ExtrudeVerts*)malloc(sizeof(struct ExtrudeVerts)*count);
  657. memset(extrude, 0, sizeof(struct ExtrudeVerts)*count);
  658. for (int i = 0; i < count; i++)
  659. {
  660. Vec2 v0 = __v2f(verts[(i-1+count)%count]);
  661. Vec2 v1 = __v2f(verts[i]);
  662. Vec2 v2 = __v2f(verts[(i+1)%count]);
  663. Vec2 n1 = v2fnormalize(v2fperp(v2fsub(v1, v0)));
  664. Vec2 n2 = v2fnormalize(v2fperp(v2fsub(v2, v1)));
  665. Vec2 offset = v2fmult(v2fadd(n1, n2), 1.0f / (v2fdot(n1, n2) + 1.0f));
  666. struct ExtrudeVerts tmp = {offset, n2};
  667. extrude[i] = tmp;
  668. }
  669. for(int i = 0; i < count; i++)
  670. {
  671. int j = (i+1)%count;
  672. Vec2 v0 = __v2f(verts[i]);
  673. Vec2 v1 = __v2f(verts[j]);
  674. Vec2 n0 = extrude[i].n;
  675. Vec2 offset0 = extrude[i].offset;
  676. Vec2 offset1 = extrude[j].offset;
  677. Vec2 inner0 = v2fsub(v0, v2fmult(offset0, borderWidth));
  678. Vec2 inner1 = v2fsub(v1, v2fmult(offset1, borderWidth));
  679. Vec2 outer0 = v2fadd(v0, v2fmult(offset0, borderWidth));
  680. Vec2 outer1 = v2fadd(v1, v2fmult(offset1, borderWidth));
  681. V2F_C4B_T2F_Triangle tmp1 = {
  682. {inner0, Color4B(borderColor), __t(v2fneg(n0))},
  683. {inner1, Color4B(borderColor), __t(v2fneg(n0))},
  684. {outer1, Color4B(borderColor), __t(n0)}
  685. };
  686. *cursor++ = tmp1;
  687. V2F_C4B_T2F_Triangle tmp2 = {
  688. {inner0, Color4B(borderColor), __t(v2fneg(n0))},
  689. {outer0, Color4B(borderColor), __t(n0)},
  690. {outer1, Color4B(borderColor), __t(n0)}
  691. };
  692. *cursor++ = tmp2;
  693. }
  694. free(extrude);
  695. }
  696. _bufferCount += vertex_count;
  697. _dirty = true;
  698. }
  699. void DrawNode::drawSolidRect(const Vec2 &origin, const Vec2 &destination, const Color4F &color)
  700. {
  701. Vec2 vertices[] = {
  702. origin,
  703. Vec2(destination.x, origin.y),
  704. destination,
  705. Vec2(origin.x, destination.y)
  706. };
  707. drawSolidPoly(vertices, 4, color );
  708. }
  709. void DrawNode::drawSolidPoly(const Vec2 *poli, unsigned int numberOfPoints, const Color4F &color)
  710. {
  711. drawPolygon(poli, numberOfPoints, color, 0.0, Color4F(0.0, 0.0, 0.0, 0.0));
  712. }
  713. void DrawNode::drawSolidCircle(const Vec2& center, float radius, float angle, unsigned int segments, float scaleX, float scaleY, const Color4F &color)
  714. {
  715. const float coef = 2.0f * (float)M_PI/segments;
  716. Vec2 *vertices = new (std::nothrow) Vec2[segments];
  717. if( ! vertices )
  718. return;
  719. for(unsigned int i = 0;i < segments; i++)
  720. {
  721. float rads = i*coef;
  722. GLfloat j = radius * cosf(rads + angle) * scaleX + center.x;
  723. GLfloat k = radius * sinf(rads + angle) * scaleY + center.y;
  724. vertices[i].x = j;
  725. vertices[i].y = k;
  726. }
  727. drawSolidPoly(vertices, segments, color);
  728. CC_SAFE_DELETE_ARRAY(vertices);
  729. }
  730. void DrawNode::drawSolidCircle( const Vec2& center, float radius, float angle, unsigned int segments, const Color4F& color)
  731. {
  732. drawSolidCircle(center, radius, angle, segments, 1.0f, 1.0f, color);
  733. }
  734. void DrawNode::drawTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, const Color4F &color)
  735. {
  736. unsigned int vertex_count = 3;
  737. ensureCapacity(vertex_count);
  738. Color4B col = Color4B(color);
  739. V2F_C4B_T2F a = {Vec2(p1.x, p1.y), col, Tex2F(0.0, 0.0) };
  740. V2F_C4B_T2F b = {Vec2(p2.x, p2.y), col, Tex2F(0.0, 0.0) };
  741. V2F_C4B_T2F c = {Vec2(p3.x, p3.y), col, Tex2F(0.0, 0.0) };
  742. V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
  743. V2F_C4B_T2F_Triangle triangle = {a, b, c};
  744. triangles[0] = triangle;
  745. _bufferCount += vertex_count;
  746. _dirty = true;
  747. }
  748. void DrawNode::drawQuadraticBezier(const Vec2& from, const Vec2& control, const Vec2& to, unsigned int segments, const Color4F &color)
  749. {
  750. drawQuadBezier(from, control, to, segments, color);
  751. }
  752. void DrawNode::clear()
  753. {
  754. _bufferCount = 0;
  755. _dirty = true;
  756. _bufferCountGLLine = 0;
  757. _dirtyGLLine = true;
  758. _bufferCountGLPoint = 0;
  759. _dirtyGLPoint = true;
  760. _lineWidth = _defaultLineWidth;
  761. }
  762. const BlendFunc& DrawNode::getBlendFunc() const
  763. {
  764. return _blendFunc;
  765. }
  766. void DrawNode::setBlendFunc(const BlendFunc &blendFunc)
  767. {
  768. _blendFunc = blendFunc;
  769. }
  770. void DrawNode::setLineWidth(GLfloat lineWidth)
  771. {
  772. _lineWidth = lineWidth;
  773. }
  774. GLfloat DrawNode::getLineWidth()
  775. {
  776. return this->_lineWidth;
  777. }
  778. NS_CC_END