CCMotionStreak3D.cpp 11 KB

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