CCMotionStreak.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /****************************************************************************
  2. Copyright (c) 2011 ForzeField Studios S.L.
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2017 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  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. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN false EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "2d/CCMotionStreak.h"
  23. #include "math/CCVertex.h"
  24. #include "base/CCDirector.h"
  25. #include "renderer/CCTextureCache.h"
  26. #include "renderer/ccGLStateCache.h"
  27. #include "renderer/CCTexture2D.h"
  28. #include "renderer/CCRenderer.h"
  29. #include "renderer/CCGLProgramState.h"
  30. NS_CC_BEGIN
  31. MotionStreak::MotionStreak()
  32. : _fastMode(false)
  33. , _startingPositionInitialized(false)
  34. , _texture(nullptr)
  35. , _blendFunc(BlendFunc::ALPHA_NON_PREMULTIPLIED)
  36. , _stroke(0.0f)
  37. , _fadeDelta(0.0f)
  38. , _minSeg(0.0f)
  39. , _maxPoints(0)
  40. , _nuPoints(0)
  41. , _previousNuPoints(0)
  42. , _pointVertexes(nullptr)
  43. , _pointState(nullptr)
  44. , _vertices(nullptr)
  45. , _colorPointer(nullptr)
  46. , _texCoords(nullptr)
  47. , _target(nullptr)
  48. {
  49. }
  50. MotionStreak::~MotionStreak()
  51. {
  52. CC_SAFE_RELEASE(_texture);
  53. CC_SAFE_FREE(_pointState);
  54. CC_SAFE_FREE(_pointVertexes);
  55. CC_SAFE_FREE(_vertices);
  56. CC_SAFE_FREE(_colorPointer);
  57. CC_SAFE_FREE(_texCoords);
  58. }
  59. MotionStreak* MotionStreak::create(float fade, float minSeg, float stroke, const Color3B& color, const std::string& path)
  60. {
  61. MotionStreak *ret = new (std::nothrow) MotionStreak();
  62. if (ret && ret->initWithFade(fade, minSeg, stroke, color, path))
  63. {
  64. ret->autorelease();
  65. return ret;
  66. }
  67. CC_SAFE_DELETE(ret);
  68. return nullptr;
  69. }
  70. MotionStreak* MotionStreak::create(float fade, float minSeg, float stroke, const Color3B& color, Texture2D* texture)
  71. {
  72. MotionStreak *ret = new (std::nothrow) MotionStreak();
  73. if (ret && ret->initWithFade(fade, minSeg, stroke, color, texture))
  74. {
  75. ret->autorelease();
  76. return ret;
  77. }
  78. CC_SAFE_DELETE(ret);
  79. return nullptr;
  80. }
  81. bool MotionStreak::initWithFade(float fade, float minSeg, float stroke, const Color3B& color, const std::string& path)
  82. {
  83. CCASSERT(!path.empty(), "Invalid filename");
  84. Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(path);
  85. if (texture == nullptr) {
  86. return false;
  87. }
  88. return initWithFade(fade, minSeg, stroke, color, texture);
  89. }
  90. bool MotionStreak::initWithFade(float fade, float minSeg, float stroke, const Color3B& color, Texture2D* texture)
  91. {
  92. Node::setPosition(Vec2::ZERO);
  93. setAnchorPoint(Vec2::ZERO);
  94. setIgnoreAnchorPointForPosition(true);
  95. _startingPositionInitialized = false;
  96. _positionR.setZero();
  97. _fastMode = true;
  98. _minSeg = (minSeg == -1.0f) ? stroke/5.0f : minSeg;
  99. _minSeg *= _minSeg;
  100. _stroke = stroke;
  101. _fadeDelta = 1.0f/fade;
  102. double fps = 1/Director::getInstance()->getAnimationInterval();
  103. _maxPoints = (int)(fade*fps)+2;
  104. _nuPoints = 0;
  105. _pointState = (float *)malloc(sizeof(float) * _maxPoints);
  106. _pointVertexes = (Vec2*)malloc(sizeof(Vec2) * _maxPoints);
  107. _vertices = (Vec2*)malloc(sizeof(Vec2) * _maxPoints * 2);
  108. _texCoords = (Tex2F*)malloc(sizeof(Tex2F) * _maxPoints * 2);
  109. _colorPointer = (GLubyte*)malloc(sizeof(GLubyte) * _maxPoints * 2 * 4);
  110. // Set blend mode
  111. _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
  112. // shader state
  113. setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR, texture));
  114. setTexture(texture);
  115. setColor(color);
  116. scheduleUpdate();
  117. return true;
  118. }
  119. void MotionStreak::setPosition(const Vec2& position)
  120. {
  121. if (!_startingPositionInitialized) {
  122. _startingPositionInitialized = true;
  123. }
  124. _positionR = position;
  125. }
  126. void MotionStreak::setPosition(float x, float y)
  127. {
  128. if (!_startingPositionInitialized) {
  129. _startingPositionInitialized = true;
  130. }
  131. _positionR.x = x;
  132. _positionR.y = y;
  133. }
  134. const Vec2& MotionStreak::getPosition() const
  135. {
  136. return _positionR;
  137. }
  138. void MotionStreak::getPosition(float* x, float* y) const
  139. {
  140. *x = _positionR.x;
  141. *y = _positionR.y;
  142. }
  143. float MotionStreak::getPositionX() const
  144. {
  145. return _positionR.x;
  146. }
  147. Vec3 MotionStreak::getPosition3D() const
  148. {
  149. return Vec3(_positionR.x, _positionR.y, getPositionZ());
  150. }
  151. void MotionStreak::setPositionX(float x)
  152. {
  153. if (!_startingPositionInitialized) {
  154. _startingPositionInitialized = true;
  155. }
  156. _positionR.x = x;
  157. }
  158. float MotionStreak::getPositionY() const
  159. {
  160. return _positionR.y;
  161. }
  162. void MotionStreak::setPositionY(float y)
  163. {
  164. if (!_startingPositionInitialized) {
  165. _startingPositionInitialized = true;
  166. }
  167. _positionR.y = y;
  168. }
  169. void MotionStreak::tintWithColor(const Color3B& colors)
  170. {
  171. setColor(colors);
  172. // Fast assignation
  173. for(unsigned int i = 0; i<_nuPoints*2; i++)
  174. {
  175. *((Color3B*) (_colorPointer+i*4)) = colors;
  176. }
  177. }
  178. Texture2D* MotionStreak::getTexture(void) const
  179. {
  180. return _texture;
  181. }
  182. void MotionStreak::setTexture(Texture2D *texture)
  183. {
  184. if (_texture != texture)
  185. {
  186. CC_SAFE_RETAIN(texture);
  187. CC_SAFE_RELEASE(_texture);
  188. _texture = texture;
  189. }
  190. }
  191. void MotionStreak::setBlendFunc(const BlendFunc &blendFunc)
  192. {
  193. _blendFunc = blendFunc;
  194. }
  195. const BlendFunc& MotionStreak::getBlendFunc(void) const
  196. {
  197. return _blendFunc;
  198. }
  199. void MotionStreak::setOpacity(GLubyte /*opacity*/)
  200. {
  201. CCASSERT(false, "Set opacity no supported");
  202. }
  203. GLubyte MotionStreak::getOpacity(void) const
  204. {
  205. CCASSERT(false, "Opacity no supported");
  206. return 0;
  207. }
  208. void MotionStreak::setOpacityModifyRGB(bool /*bValue*/)
  209. {
  210. }
  211. bool MotionStreak::isOpacityModifyRGB(void) const
  212. {
  213. return false;
  214. }
  215. void MotionStreak::followNode(Node* target,Vec2 offset)
  216. {
  217. _target = target;
  218. _targetOffset = offset;
  219. if (_target != nullptr) {
  220. Vec2 worldPos = target->convertToWorldSpace(offset);
  221. // Vec2 worldPos = _target->convertToWorldSpace(_targetOffset);
  222. Vec2 pos = this->getParent()->convertToNodeSpace(worldPos);
  223. setPosition(pos);
  224. }
  225. }
  226. void MotionStreak::update(float delta)
  227. {
  228. if (!_startingPositionInitialized)
  229. {
  230. return;
  231. }
  232. delta *= _fadeDelta;
  233. unsigned int newIdx, newIdx2, i, i2;
  234. unsigned int mov = 0;
  235. // Update current points
  236. for(i = 0; i<_nuPoints; i++)
  237. {
  238. _pointState[i]-=delta;
  239. if(_pointState[i] <= 0)
  240. mov++;
  241. else
  242. {
  243. newIdx = i-mov;
  244. if(mov>0)
  245. {
  246. // Move data
  247. _pointState[newIdx] = _pointState[i];
  248. // Move point
  249. _pointVertexes[newIdx] = _pointVertexes[i];
  250. // Move vertices
  251. i2 = i*2;
  252. newIdx2 = newIdx*2;
  253. _vertices[newIdx2] = _vertices[i2];
  254. _vertices[newIdx2+1] = _vertices[i2+1];
  255. // Move color
  256. i2 *= 4;
  257. newIdx2 *= 4;
  258. _colorPointer[newIdx2+0] = _colorPointer[i2+0];
  259. _colorPointer[newIdx2+1] = _colorPointer[i2+1];
  260. _colorPointer[newIdx2+2] = _colorPointer[i2+2];
  261. _colorPointer[newIdx2+4] = _colorPointer[i2+4];
  262. _colorPointer[newIdx2+5] = _colorPointer[i2+5];
  263. _colorPointer[newIdx2+6] = _colorPointer[i2+6];
  264. }else
  265. newIdx2 = newIdx*8;
  266. const GLubyte op = (GLubyte)(_pointState[newIdx] * 255.0f);
  267. _colorPointer[newIdx2+3] = op;
  268. _colorPointer[newIdx2+7] = op;
  269. }
  270. }
  271. _nuPoints-=mov;
  272. // Append new point
  273. bool appendNewPoint = true;
  274. if(_nuPoints >= _maxPoints)
  275. {
  276. appendNewPoint = false;
  277. }
  278. else if(_nuPoints>0)
  279. {
  280. bool a1 = _pointVertexes[_nuPoints-1].getDistanceSq(_positionR) < _minSeg;
  281. bool a2 = (_nuPoints == 1) ? false : (_pointVertexes[_nuPoints-2].getDistanceSq(_positionR)< (_minSeg * 2.0f));
  282. if(a1 || a2)
  283. {
  284. appendNewPoint = false;
  285. }
  286. }
  287. if(appendNewPoint)
  288. {
  289. _pointVertexes[_nuPoints] = _positionR;
  290. _pointState[_nuPoints] = 1.0f;
  291. // Color assignment
  292. const unsigned int offset = _nuPoints*8;
  293. *((Color3B*)(_colorPointer + offset)) = _displayedColor;
  294. *((Color3B*)(_colorPointer + offset+4)) = _displayedColor;
  295. // Opacity
  296. _colorPointer[offset+3] = 255;
  297. _colorPointer[offset+7] = 255;
  298. // Generate polygon
  299. if(_nuPoints > 0 && _fastMode )
  300. {
  301. if(_nuPoints > 1)
  302. {
  303. ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, _nuPoints, 1);
  304. }
  305. else
  306. {
  307. ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, 0, 2);
  308. }
  309. }
  310. _nuPoints ++;
  311. }
  312. if( ! _fastMode )
  313. {
  314. ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, 0, _nuPoints);
  315. }
  316. // Updated Tex Coords only if they are different than previous step
  317. if( _nuPoints && _previousNuPoints != _nuPoints ) {
  318. float texDelta = 1.0f / _nuPoints;
  319. for( i=0; i < _nuPoints; i++ ) {
  320. _texCoords[i*2] = Tex2F(0, texDelta*i);
  321. _texCoords[i*2+1] = Tex2F(1, texDelta*i);
  322. }
  323. _previousNuPoints = _nuPoints;
  324. }
  325. if (_target != nullptr) {
  326. Vec2 worldPos = _target->convertToWorldSpace(_targetOffset);
  327. Vec2 pos = this->getParent()->convertToNodeSpace(worldPos);
  328. setPosition(pos);
  329. }
  330. }
  331. void MotionStreak::reset()
  332. {
  333. _nuPoints = 0;
  334. }
  335. void MotionStreak::onDraw(const Mat4 &transform, uint32_t /*flags*/)
  336. {
  337. getGLProgram()->use();
  338. getGLProgram()->setUniformsForBuiltins(transform);
  339. GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX );
  340. GL::blendFunc( _blendFunc.src, _blendFunc.dst );
  341. GL::bindTexture2D( _texture );
  342. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, _vertices);
  343. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, _texCoords);
  344. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, _colorPointer);
  345. glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)_nuPoints*2);
  346. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _nuPoints*2);
  347. }
  348. void MotionStreak::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
  349. {
  350. if(_nuPoints <= 1)
  351. return;
  352. _customCommand.init(_globalZOrder, transform, flags);
  353. _customCommand.func = CC_CALLBACK_0(MotionStreak::onDraw, this, transform, flags);
  354. renderer->addCommand(&_customCommand);
  355. }
  356. NS_CC_END