CCDrawNode.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
  2. * Copyright (c) 2012 cocos2d-x.org
  3. * Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. * Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #include "2d/CCDrawNode.h"
  25. #include <stddef.h> // offsetof
  26. #include "base/ccTypes.h"
  27. #include "base/CCEventType.h"
  28. #include "base/CCConfiguration.h"
  29. #include "renderer/CCRenderer.h"
  30. #include "base/CCDirector.h"
  31. #include "base/CCEventListenerCustom.h"
  32. #include "base/CCEventDispatcher.h"
  33. #include "2d/CCActionCatmullRom.h"
  34. #include "base/ccUtils.h"
  35. #include "renderer/ccShaders.h"
  36. #include "renderer/backend/ProgramState.h"
  37. NS_CC_BEGIN
  38. static inline Tex2F v2ToTex2F(const Vec2 &v)
  39. {
  40. return {v.x, v.y};
  41. }
  42. // implementation of DrawNode
  43. DrawNode::DrawNode(float lineWidth)
  44. : _lineWidth(lineWidth)
  45. {
  46. _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
  47. #if CC_ENABLE_CACHE_TEXTURE_DATA
  48. //TODO new-renderer: interface setupBuffer removal
  49. // Need to listen the event only when not use batchnode, because it will use VBO
  50. // auto listener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom* event){
  51. // /** listen the event that renderer was recreated on Android/WP8 */
  52. // this->setupBuffer();
  53. // });
  54. // _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
  55. #endif
  56. }
  57. DrawNode::~DrawNode()
  58. {
  59. free(_buffer);
  60. _buffer = nullptr;
  61. free(_bufferGLPoint);
  62. _bufferGLPoint = nullptr;
  63. free(_bufferGLLine);
  64. _bufferGLLine = nullptr;
  65. CC_SAFE_RELEASE(_programState);
  66. CC_SAFE_RELEASE(_programStatePoint);
  67. CC_SAFE_RELEASE(_programStateLine);
  68. }
  69. DrawNode* DrawNode::create(float defaultLineWidth)
  70. {
  71. DrawNode* ret = new (std::nothrow) DrawNode(defaultLineWidth);
  72. if (ret && ret->init())
  73. {
  74. ret->autorelease();
  75. }
  76. else
  77. {
  78. CC_SAFE_DELETE(ret);
  79. }
  80. return ret;
  81. }
  82. void DrawNode::ensureCapacity(int count)
  83. {
  84. CCASSERT(count>=0, "capacity must be >= 0");
  85. if(_bufferCount + count > _bufferCapacity)
  86. {
  87. _bufferCapacity += MAX(_bufferCapacity, count);
  88. _buffer = (V2F_C4B_T2F*)realloc(_buffer, _bufferCapacity*sizeof(V2F_C4B_T2F));
  89. _customCommand.createVertexBuffer(sizeof(V2F_C4B_T2F), _bufferCapacity, CustomCommand::BufferUsage::STATIC);
  90. _customCommand.updateVertexBuffer(_buffer, _bufferCapacity*sizeof(V2F_C4B_T2F));
  91. }
  92. }
  93. void DrawNode::ensureCapacityGLPoint(int count)
  94. {
  95. CCASSERT(count>=0, "capacity must be >= 0");
  96. if(_bufferCountGLPoint + count > _bufferCapacityGLPoint)
  97. {
  98. _bufferCapacityGLPoint += MAX(_bufferCapacityGLPoint, count);
  99. _bufferGLPoint = (V2F_C4B_T2F*)realloc(_bufferGLPoint, _bufferCapacityGLPoint*sizeof(V2F_C4B_T2F));
  100. _customCommandGLPoint.createVertexBuffer(sizeof(V2F_C4B_T2F), _bufferCapacityGLPoint, CustomCommand::BufferUsage::STATIC);
  101. _customCommandGLPoint.updateVertexBuffer(_bufferGLPoint, _bufferCapacityGLPoint*sizeof(V2F_C4B_T2F));
  102. }
  103. }
  104. void DrawNode::ensureCapacityGLLine(int count)
  105. {
  106. CCASSERT(count>=0, "capacity must be >= 0");
  107. if(_bufferCountGLLine + count > _bufferCapacityGLLine)
  108. {
  109. _bufferCapacityGLLine += MAX(_bufferCapacityGLLine, count);
  110. _bufferGLLine = (V2F_C4B_T2F*)realloc(_bufferGLLine, _bufferCapacityGLLine*sizeof(V2F_C4B_T2F));
  111. _customCommandGLLine.createVertexBuffer(sizeof(V2F_C4B_T2F), _bufferCapacityGLLine, CustomCommand::BufferUsage::STATIC);
  112. _customCommandGLLine.updateVertexBuffer(_bufferGLLine, _bufferCapacityGLLine*sizeof(V2F_C4B_T2F));
  113. }
  114. }
  115. bool DrawNode::init()
  116. {
  117. _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
  118. updateShader();
  119. ensureCapacity(512);
  120. ensureCapacityGLPoint(64);
  121. ensureCapacityGLLine(256);
  122. _dirty = true;
  123. _dirtyGLLine = true;
  124. _dirtyGLPoint = true;
  125. return true;
  126. }
  127. void DrawNode::updateShader()
  128. {
  129. CC_SAFE_RELEASE(_programState);
  130. auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_COLOR_LENGTH_TEXTURE);
  131. _programState = new (std::nothrow) backend::ProgramState(program);
  132. _customCommand.getPipelineDescriptor().programState = _programState;
  133. setVertexLayout(_customCommand);
  134. _customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
  135. _customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE);
  136. CC_SAFE_RELEASE(_programStatePoint);
  137. program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_COLOR_TEXTURE_AS_POINTSIZE);
  138. _programStatePoint = new (std::nothrow) backend::ProgramState(program);
  139. _customCommandGLPoint.getPipelineDescriptor().programState = _programStatePoint;
  140. setVertexLayout(_customCommandGLPoint);
  141. _customCommandGLPoint.setDrawType(CustomCommand::DrawType::ARRAY);
  142. _customCommandGLPoint.setPrimitiveType(CustomCommand::PrimitiveType::POINT);
  143. CC_SAFE_RELEASE(_programStateLine);
  144. program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_COLOR_LENGTH_TEXTURE);
  145. _programStateLine = new (std::nothrow) backend::ProgramState(program);
  146. _customCommandGLLine.getPipelineDescriptor().programState = _programStateLine;
  147. setVertexLayout(_customCommandGLLine);
  148. _customCommandGLLine.setDrawType(CustomCommand::DrawType::ARRAY);
  149. _customCommandGLLine.setPrimitiveType(CustomCommand::PrimitiveType::LINE);
  150. }
  151. void DrawNode::setVertexLayout(CustomCommand& cmd)
  152. {
  153. auto* programState = cmd.getPipelineDescriptor().programState;
  154. auto layout = programState->getVertexLayout();
  155. const auto& attributeInfo = programState->getProgram()->getActiveAttributes();
  156. auto iter = attributeInfo.find("a_position");
  157. if(iter != attributeInfo.end())
  158. {
  159. layout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT2, 0, false);
  160. }
  161. iter = attributeInfo.find("a_texCoord");
  162. if(iter != attributeInfo.end())
  163. {
  164. layout->setAttribute("a_texCoord", iter->second.location, backend::VertexFormat::FLOAT2, offsetof(V2F_C4B_T2F, texCoords), false);
  165. }
  166. iter = attributeInfo.find("a_color");
  167. if(iter != attributeInfo.end())
  168. {
  169. layout->setAttribute("a_color", iter->second.location, backend::VertexFormat::UBYTE4, offsetof(V2F_C4B_T2F, colors), true);
  170. }
  171. layout->setLayout(sizeof(V2F_C4B_T2F));
  172. }
  173. void DrawNode::updateBlendState(CustomCommand& cmd)
  174. {
  175. backend::BlendDescriptor& blendDescriptor = cmd.getPipelineDescriptor().blendDescriptor;
  176. blendDescriptor.blendEnabled = true;
  177. if (_blendFunc == BlendFunc::ALPHA_NON_PREMULTIPLIED)
  178. {
  179. blendDescriptor.sourceRGBBlendFactor = backend::BlendFactor::SRC_ALPHA;
  180. blendDescriptor.destinationRGBBlendFactor = backend::BlendFactor::ONE_MINUS_SRC_ALPHA;
  181. blendDescriptor.sourceAlphaBlendFactor = backend::BlendFactor::SRC_ALPHA;
  182. blendDescriptor.destinationAlphaBlendFactor = backend::BlendFactor::ONE_MINUS_SRC_ALPHA;
  183. setOpacityModifyRGB(false);
  184. }
  185. else
  186. {
  187. blendDescriptor.sourceRGBBlendFactor = backend::BlendFactor::ONE;
  188. blendDescriptor.destinationRGBBlendFactor = backend::BlendFactor::ONE_MINUS_SRC_ALPHA;
  189. blendDescriptor.sourceAlphaBlendFactor = backend::BlendFactor::ONE;
  190. blendDescriptor.destinationAlphaBlendFactor = backend::BlendFactor::ONE_MINUS_SRC_ALPHA;
  191. setOpacityModifyRGB(true);
  192. }
  193. }
  194. void DrawNode::updateUniforms(const Mat4 &transform, CustomCommand& cmd)
  195. {
  196. auto& pipelineDescriptor = cmd.getPipelineDescriptor();
  197. const auto& matrixP = _director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
  198. Mat4 matrixMVP = matrixP * transform;
  199. auto mvpLocation = pipelineDescriptor.programState->getUniformLocation("u_MVPMatrix");
  200. pipelineDescriptor.programState->setUniform(mvpLocation, matrixMVP.m, sizeof(matrixMVP.m));
  201. float alpha = _displayedOpacity / 255.0f;
  202. auto alphaUniformLocation = pipelineDescriptor.programState->getUniformLocation("u_alpha");
  203. pipelineDescriptor.programState->setUniform(alphaUniformLocation, &alpha, sizeof(alpha));
  204. }
  205. void DrawNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
  206. {
  207. if(_bufferCount)
  208. {
  209. updateBlendState(_customCommand);
  210. updateUniforms(transform, _customCommand);
  211. _customCommand.init(_globalZOrder);
  212. renderer->addCommand(&_customCommand);
  213. }
  214. if(_bufferCountGLPoint)
  215. {
  216. updateBlendState(_customCommandGLPoint);
  217. updateUniforms(transform, _customCommandGLPoint);
  218. _customCommandGLPoint.init(_globalZOrder);
  219. renderer->addCommand(&_customCommandGLPoint);
  220. }
  221. if(_bufferCountGLLine)
  222. {
  223. updateBlendState(_customCommandGLLine);
  224. updateUniforms(transform, _customCommandGLLine);
  225. _customCommandGLLine.setLineWidth(_lineWidth);
  226. _customCommandGLLine.init(_globalZOrder);
  227. renderer->addCommand(&_customCommandGLLine);
  228. }
  229. }
  230. void DrawNode::drawPoint(const Vec2& position, const float pointSize, const Color4F &color)
  231. {
  232. ensureCapacityGLPoint(1);
  233. V2F_C4B_T2F *point = _bufferGLPoint + _bufferCountGLPoint;
  234. *point = {position, Color4B(color), Tex2F(pointSize,0)};
  235. _customCommandGLPoint.updateVertexBuffer(point, _bufferCountGLPoint*sizeof(V2F_C4B_T2F), sizeof(V2F_C4B_T2F));
  236. _bufferCountGLPoint += 1;
  237. _dirtyGLPoint = true;
  238. _customCommandGLPoint.setVertexDrawInfo(0, _bufferCountGLPoint);
  239. }
  240. void DrawNode::drawPoints(const Vec2 *position, unsigned int numberOfPoints, const Color4F &color)
  241. {
  242. drawPoints(position, numberOfPoints, 1.0, color);
  243. }
  244. void DrawNode::drawPoints(const Vec2 *position, unsigned int numberOfPoints, const float pointSize, const Color4F &color)
  245. {
  246. ensureCapacityGLPoint(numberOfPoints);
  247. V2F_C4B_T2F *point = _bufferGLPoint + _bufferCountGLPoint;
  248. for(unsigned int i=0; i < numberOfPoints; i++)
  249. {
  250. *(point + i) = {position[i], Color4B(color), Tex2F(pointSize,0)};
  251. }
  252. _customCommandGLPoint.updateVertexBuffer(point, _bufferCountGLPoint*sizeof(V2F_C4B_T2F), numberOfPoints*sizeof(V2F_C4B_T2F));
  253. _bufferCountGLPoint += numberOfPoints;
  254. _dirtyGLPoint = true;
  255. _customCommandGLPoint.setVertexDrawInfo(0, _bufferCountGLPoint);
  256. }
  257. void DrawNode::drawLine(const Vec2 &origin, const Vec2 &destination, const Color4F &color)
  258. {
  259. ensureCapacityGLLine(2);
  260. V2F_C4B_T2F *point = _bufferGLLine + _bufferCountGLLine;
  261. *point = {origin, Color4B(color), Tex2F(0.0, 0.0)};
  262. *(point+1) = {destination, Color4B(color), Tex2F(0.0, 0.0)};
  263. _customCommandGLLine.updateVertexBuffer(point, _bufferCountGLLine*sizeof(V2F_C4B_T2F), 2*sizeof(V2F_C4B_T2F));
  264. _bufferCountGLLine += 2;
  265. _dirtyGLLine = true;
  266. _customCommandGLLine.setVertexDrawInfo(0, _bufferCountGLLine);
  267. }
  268. void DrawNode::drawRect(const Vec2 &origin, const Vec2 &destination, const Color4F &color)
  269. {
  270. drawLine(origin, Vec2(destination.x, origin.y), color);
  271. drawLine(Vec2(destination.x, origin.y), destination, color);
  272. drawLine(destination, Vec2(origin.x, destination.y), color);
  273. drawLine(Vec2(origin.x, destination.y), origin, color);
  274. }
  275. void DrawNode::drawPoly(const Vec2 *poli, unsigned int numberOfPoints, bool closePolygon, const Color4F &color)
  276. {
  277. unsigned int vertex_count;
  278. if(closePolygon)
  279. {
  280. vertex_count = 2 * numberOfPoints;
  281. ensureCapacityGLLine(vertex_count);
  282. }
  283. else
  284. {
  285. vertex_count = 2 * (numberOfPoints - 1);
  286. ensureCapacityGLLine(vertex_count);
  287. }
  288. V2F_C4B_T2F *point = _bufferGLLine + _bufferCountGLLine;
  289. V2F_C4B_T2F *cursor = point;
  290. unsigned int i = 0;
  291. for(; i < numberOfPoints - 1; i++)
  292. {
  293. *point = {poli[i], Color4B(color), Tex2F(0.0, 0.0)};
  294. *(point + 1) = {poli[i+1], Color4B(color), Tex2F(0.0, 0.0)};
  295. point += 2;
  296. }
  297. if(closePolygon)
  298. {
  299. *point = {poli[i], Color4B(color), Tex2F(0.0, 0.0)};
  300. *(point + 1) = {poli[0], Color4B(color), Tex2F(0.0, 0.0)};
  301. }
  302. _customCommandGLLine.updateVertexBuffer(cursor, _bufferCountGLLine*sizeof(V2F_C4B_T2F), vertex_count*sizeof(V2F_C4B_T2F));
  303. _bufferCountGLLine += vertex_count;
  304. _customCommandGLLine.setVertexDrawInfo(0, _bufferCountGLLine);
  305. }
  306. void DrawNode::drawCircle(const Vec2& center, float radius, float angle, unsigned int segments, bool drawLineToCenter, float scaleX, float scaleY, const Color4F &color)
  307. {
  308. const float coef = 2.0f * (float)M_PI/segments;
  309. Vec2 *vertices = new (std::nothrow) Vec2[segments+2];
  310. if( ! vertices )
  311. return;
  312. for(unsigned int i = 0;i <= segments; i++) {
  313. float rads = i*coef;
  314. float j = radius * cosf(rads + angle) * scaleX + center.x;
  315. float k = radius * sinf(rads + angle) * scaleY + center.y;
  316. vertices[i].x = j;
  317. vertices[i].y = k;
  318. }
  319. if(drawLineToCenter)
  320. {
  321. vertices[segments+1].x = center.x;
  322. vertices[segments+1].y = center.y;
  323. drawPoly(vertices, segments+2, true, color);
  324. }
  325. else
  326. drawPoly(vertices, segments+1, true, color);
  327. CC_SAFE_DELETE_ARRAY(vertices);
  328. }
  329. void DrawNode::drawCircle(const Vec2 &center, float radius, float angle, unsigned int segments, bool drawLineToCenter, const Color4F &color)
  330. {
  331. drawCircle(center, radius, angle, segments, drawLineToCenter, 1.0f, 1.0f, color);
  332. }
  333. void DrawNode::drawQuadBezier(const Vec2 &origin, const Vec2 &control, const Vec2 &destination, unsigned int segments, const Color4F &color)
  334. {
  335. Vec2* vertices = new (std::nothrow) Vec2[segments + 1];
  336. if( ! vertices )
  337. return;
  338. float t = 0.0f;
  339. for(unsigned int i = 0; i < segments; i++)
  340. {
  341. vertices[i].x = powf(1 - t, 2) * origin.x + 2.0f * (1 - t) * t * control.x + t * t * destination.x;
  342. vertices[i].y = powf(1 - t, 2) * origin.y + 2.0f * (1 - t) * t * control.y + t * t * destination.y;
  343. t += 1.0f / segments;
  344. }
  345. vertices[segments].x = destination.x;
  346. vertices[segments].y = destination.y;
  347. drawPoly(vertices, segments+1, false, color);
  348. CC_SAFE_DELETE_ARRAY(vertices);
  349. }
  350. void DrawNode::drawCubicBezier(const Vec2 &origin, const Vec2 &control1, const Vec2 &control2, const Vec2 &destination, unsigned int segments, const Color4F &color)
  351. {
  352. Vec2* vertices = new (std::nothrow) Vec2[segments + 1];
  353. if( ! vertices )
  354. return;
  355. float t = 0;
  356. for (unsigned int i = 0; i < segments; i++)
  357. {
  358. 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;
  359. 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;
  360. t += 1.0f / segments;
  361. }
  362. vertices[segments].x = destination.x;
  363. vertices[segments].y = destination.y;
  364. drawPoly(vertices, segments+1, false, color);
  365. CC_SAFE_DELETE_ARRAY(vertices);
  366. }
  367. void DrawNode::drawCardinalSpline(PointArray *config, float tension, unsigned int segments, const Color4F &color)
  368. {
  369. Vec2* vertices = new (std::nothrow) Vec2[segments + 1];
  370. if( ! vertices )
  371. return;
  372. ssize_t p;
  373. float lt;
  374. float deltaT = 1.0f / config->count();
  375. for( unsigned int i=0; i < segments+1;i++) {
  376. float dt = (float)i / segments;
  377. // border
  378. if( dt == 1 ) {
  379. p = config->count() - 1;
  380. lt = 1;
  381. } else {
  382. p = static_cast<ssize_t>(dt / deltaT);
  383. lt = (dt - deltaT * (float)p) / deltaT;
  384. }
  385. // Interpolate
  386. Vec2 pp0 = config->getControlPointAtIndex(p-1);
  387. Vec2 pp1 = config->getControlPointAtIndex(p+0);
  388. Vec2 pp2 = config->getControlPointAtIndex(p+1);
  389. Vec2 pp3 = config->getControlPointAtIndex(p+2);
  390. Vec2 newPos = ccCardinalSplineAt( pp0, pp1, pp2, pp3, tension, lt);
  391. vertices[i].x = newPos.x;
  392. vertices[i].y = newPos.y;
  393. }
  394. drawPoly(vertices, segments+1, false, color);
  395. CC_SAFE_DELETE_ARRAY(vertices);
  396. }
  397. void DrawNode::drawCatmullRom(PointArray *points, unsigned int segments, const Color4F &color)
  398. {
  399. drawCardinalSpline( points, 0.5f, segments, color);
  400. }
  401. void DrawNode::drawDot(const Vec2 &pos, float radius, const Color4F &color)
  402. {
  403. unsigned int vertex_count = 2*3;
  404. ensureCapacity(vertex_count);
  405. V2F_C4B_T2F a = {Vec2(pos.x - radius, pos.y - radius), Color4B(color), Tex2F(-1.0, -1.0) };
  406. V2F_C4B_T2F b = {Vec2(pos.x - radius, pos.y + radius), Color4B(color), Tex2F(-1.0, 1.0) };
  407. V2F_C4B_T2F c = {Vec2(pos.x + radius, pos.y + radius), Color4B(color), Tex2F( 1.0, 1.0) };
  408. V2F_C4B_T2F d = {Vec2(pos.x + radius, pos.y - radius), Color4B(color), Tex2F( 1.0, -1.0) };
  409. V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
  410. V2F_C4B_T2F_Triangle triangle0 = {a, b, c};
  411. V2F_C4B_T2F_Triangle triangle1 = {a, c, d};
  412. triangles[0] = triangle0;
  413. triangles[1] = triangle1;
  414. _customCommand.updateVertexBuffer(triangles, _bufferCount*sizeof(V2F_C4B_T2F), vertex_count*sizeof(V2F_C4B_T2F));
  415. _bufferCount += vertex_count;
  416. _dirty = true;
  417. _customCommand.setVertexDrawInfo(0, _bufferCount);
  418. }
  419. void DrawNode::drawRect(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, const Vec2& p4, const Color4F &color)
  420. {
  421. drawLine(p1, p2, color);
  422. drawLine(p2, p3, color);
  423. drawLine(p3, p4, color);
  424. drawLine(p4, p1, color);
  425. }
  426. void DrawNode::drawSegment(const Vec2 &from, const Vec2 &to, float radius, const Color4F &color)
  427. {
  428. unsigned int vertex_count = 6*3;
  429. ensureCapacity(vertex_count);
  430. Vec2 a = from;
  431. Vec2 b = to;
  432. Vec2 n = ((b - a).getPerp()).getNormalized();
  433. Vec2 t = n.getPerp();
  434. Vec2 nw = n * radius;
  435. Vec2 tw = t * radius;
  436. Vec2 v0 = b - (nw + tw);
  437. Vec2 v1 = b + (nw - tw);
  438. Vec2 v2 = b - nw;
  439. Vec2 v3 = b + nw;
  440. Vec2 v4 = a - nw;
  441. Vec2 v5 = a + nw;
  442. Vec2 v6 = a - (nw - tw);
  443. Vec2 v7 = a + (nw + tw);
  444. V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
  445. V2F_C4B_T2F_Triangle triangles0 = {
  446. {v0, Color4B(color), v2ToTex2F(-(n + t))},
  447. {v1, Color4B(color), v2ToTex2F(n - t)},
  448. {v2, Color4B(color), v2ToTex2F(-n)},
  449. };
  450. triangles[0] = triangles0;
  451. V2F_C4B_T2F_Triangle triangles1 = {
  452. {v3, Color4B(color), v2ToTex2F(n)},
  453. {v1, Color4B(color), v2ToTex2F(n - t)},
  454. {v2, Color4B(color), v2ToTex2F(-n)},
  455. };
  456. triangles[1] = triangles1;
  457. V2F_C4B_T2F_Triangle triangles2 = {
  458. {v3, Color4B(color), v2ToTex2F(n)},
  459. {v4, Color4B(color), v2ToTex2F(-n)},
  460. {v2, Color4B(color), v2ToTex2F(-n)},
  461. };
  462. triangles[2] = triangles2;
  463. V2F_C4B_T2F_Triangle triangles3 = {
  464. {v3, Color4B(color), v2ToTex2F(n)},
  465. {v4, Color4B(color), v2ToTex2F(-n)},
  466. {v5, Color4B(color), v2ToTex2F(n) },
  467. };
  468. triangles[3] = triangles3;
  469. V2F_C4B_T2F_Triangle triangles4 = {
  470. {v6, Color4B(color), v2ToTex2F(t - n)},
  471. {v4, Color4B(color), v2ToTex2F(-n) },
  472. {v5, Color4B(color), v2ToTex2F(n)},
  473. };
  474. triangles[4] = triangles4;
  475. V2F_C4B_T2F_Triangle triangles5 = {
  476. {v6, Color4B(color), v2ToTex2F(t - n)},
  477. {v7, Color4B(color), v2ToTex2F(t + n)},
  478. {v5, Color4B(color), v2ToTex2F(n)},
  479. };
  480. triangles[5] = triangles5;
  481. _customCommand.updateVertexBuffer(triangles, _bufferCount*sizeof(V2F_C4B_T2F), vertex_count*sizeof(V2F_C4B_T2F));
  482. _bufferCount += vertex_count;
  483. _dirty = true;
  484. _customCommand.setVertexDrawInfo(0, _bufferCount);
  485. }
  486. void DrawNode::drawPolygon(const Vec2 *verts, int count, const Color4F &fillColor, float borderWidth, const Color4F &borderColor)
  487. {
  488. CCASSERT(count >= 0, "invalid count value");
  489. bool outline = (borderColor.a > 0.0f && borderWidth > 0.0f);
  490. auto triangle_count = outline ? (3*count - 2) : (count - 2);
  491. auto vertex_count = 3*triangle_count;
  492. ensureCapacity(vertex_count);
  493. V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
  494. V2F_C4B_T2F_Triangle *cursor = triangles;
  495. for (int i = 0; i < count-2; i++)
  496. {
  497. V2F_C4B_T2F_Triangle tmp = {
  498. {verts[0], Color4B(fillColor), v2ToTex2F(Vec2::ZERO)},
  499. {verts[i+1], Color4B(fillColor), v2ToTex2F(Vec2::ZERO)},
  500. {verts[i+2], Color4B(fillColor), v2ToTex2F(Vec2::ZERO)},
  501. };
  502. *cursor++ = tmp;
  503. }
  504. if(outline)
  505. {
  506. struct ExtrudeVerts {Vec2 offset, n;};
  507. struct ExtrudeVerts* extrude = (struct ExtrudeVerts*)malloc(sizeof(struct ExtrudeVerts) * count);
  508. for (int i = 0; i < count; i++)
  509. {
  510. Vec2 v0 = verts[(i-1+count)%count];
  511. Vec2 v1 = verts[i];
  512. Vec2 v2 = verts[(i+1)%count];
  513. Vec2 n1 = ((v1 - v0).getPerp()).getNormalized();
  514. Vec2 n2 = ((v2 - v1).getPerp()).getNormalized();
  515. Vec2 offset = (n1 + n2) * (1.0f / (Vec2::dot(n1, n2) + 1.0f));
  516. extrude[i] = {offset, n2};
  517. }
  518. for(int i = 0; i < count; i++)
  519. {
  520. int j = (i+1)%count;
  521. Vec2 v0 = verts[i];
  522. Vec2 v1 = verts[j];
  523. Vec2 n0 = extrude[i].n;
  524. Vec2 offset0 = extrude[i].offset;
  525. Vec2 offset1 = extrude[j].offset;
  526. Vec2 inner0 = v0 - offset0 * borderWidth;
  527. Vec2 inner1 = v1 - offset1 * borderWidth;
  528. Vec2 outer0 = v0 + offset0 * borderWidth;
  529. Vec2 outer1 = v1 + offset1 * borderWidth;
  530. V2F_C4B_T2F_Triangle tmp1 = {
  531. {inner0, Color4B(borderColor), v2ToTex2F(-n0)},
  532. {inner1, Color4B(borderColor), v2ToTex2F(-n0)},
  533. {outer1, Color4B(borderColor), v2ToTex2F(n0)}
  534. };
  535. *cursor++ = tmp1;
  536. V2F_C4B_T2F_Triangle tmp2 = {
  537. {inner0, Color4B(borderColor), v2ToTex2F(-n0)},
  538. {outer0, Color4B(borderColor), v2ToTex2F(n0)},
  539. {outer1, Color4B(borderColor), v2ToTex2F(n0)}
  540. };
  541. *cursor++ = tmp2;
  542. }
  543. free(extrude);
  544. }
  545. _customCommand.updateVertexBuffer(triangles, _bufferCount*sizeof(V2F_C4B_T2F), vertex_count*sizeof(V2F_C4B_T2F));
  546. _bufferCount += vertex_count;
  547. _customCommand.setVertexDrawInfo(0, _bufferCount);
  548. _dirty = true;
  549. }
  550. void DrawNode::drawSolidRect(const Vec2 &origin, const Vec2 &destination, const Color4F &color)
  551. {
  552. Vec2 vertices[] = {
  553. origin,
  554. Vec2(destination.x, origin.y),
  555. destination,
  556. Vec2(origin.x, destination.y)
  557. };
  558. drawSolidPoly(vertices, 4, color);
  559. }
  560. void DrawNode::drawSolidPoly(const Vec2 *poli, unsigned int numberOfPoints, const Color4F &color)
  561. {
  562. drawPolygon(poli, numberOfPoints, color, 0.0, Color4F());
  563. }
  564. void DrawNode::drawSolidCircle(const Vec2& center, float radius, float angle, unsigned int segments, float scaleX, float scaleY, const Color4F &color)
  565. {
  566. const float coef = 2.0f * (float)M_PI/segments;
  567. Vec2 *vertices = new (std::nothrow) Vec2[segments];
  568. if( ! vertices )
  569. return;
  570. for(unsigned int i = 0;i < segments; i++)
  571. {
  572. float rads = i*coef;
  573. float j = radius * cosf(rads + angle) * scaleX + center.x;
  574. float k = radius * sinf(rads + angle) * scaleY + center.y;
  575. vertices[i].x = j;
  576. vertices[i].y = k;
  577. }
  578. drawSolidPoly(vertices, segments, color);
  579. CC_SAFE_DELETE_ARRAY(vertices);
  580. }
  581. void DrawNode::drawSolidCircle( const Vec2& center, float radius, float angle, unsigned int segments, const Color4F& color)
  582. {
  583. drawSolidCircle(center, radius, angle, segments, 1.0f, 1.0f, color);
  584. }
  585. void DrawNode::drawTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, const Color4F &color)
  586. {
  587. unsigned int vertex_count = 3;
  588. ensureCapacity(vertex_count);
  589. Color4B col = Color4B(color);
  590. V2F_C4B_T2F a = {p1, col, Tex2F(0.0, 0.0) };
  591. V2F_C4B_T2F b = {p2, col, Tex2F(0.0, 0.0) };
  592. V2F_C4B_T2F c = {p3, col, Tex2F(0.0, 0.0) };
  593. V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
  594. V2F_C4B_T2F_Triangle triangle = {a, b, c};
  595. triangles[0] = triangle;
  596. _customCommand.updateVertexBuffer(triangles, _bufferCount*sizeof(V2F_C4B_T2F), vertex_count*sizeof(V2F_C4B_T2F));
  597. _bufferCount += vertex_count;
  598. _dirty = true;
  599. _customCommand.setVertexDrawInfo(0, _bufferCount);
  600. }
  601. void DrawNode::clear()
  602. {
  603. _bufferCount = 0;
  604. _dirty = true;
  605. _bufferCountGLLine = 0;
  606. _dirtyGLLine = true;
  607. _bufferCountGLPoint = 0;
  608. _dirtyGLPoint = true;
  609. _lineWidth = 0;
  610. }
  611. const BlendFunc& DrawNode::getBlendFunc() const
  612. {
  613. return _blendFunc;
  614. }
  615. void DrawNode::setBlendFunc(const BlendFunc &blendFunc)
  616. {
  617. _blendFunc = blendFunc;
  618. }
  619. void DrawNode::setLineWidth(float lineWidth)
  620. {
  621. _lineWidth = lineWidth;
  622. }
  623. float DrawNode::getLineWidth()
  624. {
  625. return this->_lineWidth;
  626. }
  627. void DrawNode::visit(Renderer* renderer, const Mat4 &parentTransform, uint32_t parentFlags)
  628. {
  629. if (_isolated)
  630. {
  631. //ignore `parentTransform` from parent
  632. Node::visit(renderer, Mat4::IDENTITY, parentFlags);
  633. }
  634. else
  635. {
  636. Node::visit(renderer, parentTransform, parentFlags);
  637. }
  638. }
  639. NS_CC_END