CCPUBillboardChain.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  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. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "extensions/Particle3D/PU/CCPUBillboardChain.h"
  22. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  23. #include "base/CCDirector.h"
  24. #include "renderer/CCMeshCommand.h"
  25. #include "renderer/CCRenderer.h"
  26. #include "renderer/CCTextureCache.h"
  27. #include "renderer/CCGLProgramState.h"
  28. #include "renderer/CCGLProgramCache.h"
  29. #include "renderer/CCVertexIndexBuffer.h"
  30. #include "renderer/CCVertexAttribBinding.h"
  31. #include "2d/CCCamera.h"
  32. #include "3d/CCSprite3D.h"
  33. NS_CC_BEGIN
  34. const size_t PUBillboardChain::SEGMENT_EMPTY = std::numeric_limits<size_t>::max();
  35. //-----------------------------------------------------------------------
  36. PUBillboardChain::Element::Element()
  37. {
  38. }
  39. //-----------------------------------------------------------------------
  40. PUBillboardChain::Element::Element(const Vec3 &pos,
  41. float w,
  42. float tex,
  43. const Vec4 &col,
  44. const Quaternion &ori) :
  45. position(pos),
  46. width(w),
  47. texCoord(tex),
  48. color(col),
  49. orientation(ori)
  50. {
  51. }
  52. //-----------------------------------------------------------------------
  53. PUBillboardChain::PUBillboardChain(const std::string& /*name*/, const std::string &texFile, size_t maxElements,
  54. size_t numberOfChains, bool useTextureCoords, bool useColours, bool dynamic)
  55. :_maxElementsPerChain(maxElements),
  56. _chainCount(numberOfChains),
  57. _useTexCoords(useTextureCoords),
  58. _useVertexColour(useColours),
  59. _dynamic(dynamic),
  60. _vertexDeclDirty(true),
  61. _buffersNeedRecreating(true),
  62. _boundsDirty(true),
  63. _indexContentDirty(true),
  64. _vertexContentDirty(true),
  65. _texCoordDir(TCD_U),
  66. _faceCamera(true),
  67. _normalBase(Vec3::UNIT_X),
  68. _meshCommand(nullptr),
  69. _texture(nullptr),
  70. _glProgramState(nullptr),
  71. _indexBuffer(nullptr),
  72. _vertexBuffer(nullptr),
  73. _texFile(texFile)
  74. {
  75. _stateBlock = RenderState::StateBlock::create();
  76. CC_SAFE_RETAIN(_stateBlock);
  77. _stateBlock->setCullFace(false);
  78. _stateBlock->setCullFaceSide(RenderState::CULL_FACE_SIDE_BACK);
  79. _stateBlock->setDepthTest(false);
  80. _stateBlock->setDepthWrite(false);
  81. _stateBlock->setBlend(true);
  82. _otherTexCoordRange[0] = 0.0f;
  83. _otherTexCoordRange[1] = 1.0f;
  84. setupChainContainers();
  85. init(texFile);
  86. }
  87. //-----------------------------------------------------------------------
  88. PUBillboardChain::~PUBillboardChain()
  89. {
  90. CC_SAFE_DELETE(_meshCommand);
  91. CC_SAFE_RELEASE(_stateBlock);
  92. //CC_SAFE_RELEASE(_texture);
  93. CC_SAFE_RELEASE(_glProgramState);
  94. CC_SAFE_RELEASE(_vertexBuffer);
  95. CC_SAFE_RELEASE(_indexBuffer);
  96. }
  97. //-----------------------------------------------------------------------
  98. void PUBillboardChain::setupChainContainers(void)
  99. {
  100. // Allocate enough space for everything
  101. _chainElementList.resize(_chainCount * _maxElementsPerChain);
  102. //_vertexData->vertexCount = _chainElementList.size() * 2;
  103. // Configure chains
  104. _chainSegmentList.resize(_chainCount);
  105. for (size_t i = 0; i < _chainCount; ++i)
  106. {
  107. ChainSegment& seg = _chainSegmentList[i];
  108. seg.start = i * _maxElementsPerChain;
  109. seg.tail = seg.head = SEGMENT_EMPTY;
  110. }
  111. }
  112. //-----------------------------------------------------------------------
  113. void PUBillboardChain::setupVertexDeclaration(void)
  114. {
  115. //if (_vertexDeclDirty)
  116. //{
  117. // VertexDeclaration* decl = _vertexData->vertexDeclaration;
  118. // decl->removeAllElements();
  119. // size_t offset = 0;
  120. // // Add a description for the buffer of the positions of the vertices
  121. // decl->addElement(0, offset, VET_FLOAT3, VES_POSITION);
  122. // offset += VertexElement::getTypeSize(VET_FLOAT3);
  123. // if (_useVertexColour)
  124. // {
  125. // decl->addElement(0, offset, VET_COLOUR, VES_DIFFUSE);
  126. // offset += VertexElement::getTypeSize(VET_COLOUR);
  127. // }
  128. // if (_useTexCoords)
  129. // {
  130. // decl->addElement(0, offset, VET_FLOAT2, VES_TEXTURE_COORDINATES);
  131. // }
  132. // if (!_useTexCoords && !_useVertexColour)
  133. // {
  134. // LogManager::getSingleton().logMessage(
  135. // "Error - BillboardChain '" + mName + "' is using neither "
  136. // "texture coordinates or vertex colours; it will not be "
  137. // "visible on some rendering APIs so you should change this "
  138. // "so you use one or the other.");
  139. // }
  140. // _vertexDeclDirty = false;
  141. //}
  142. }
  143. //-----------------------------------------------------------------------
  144. void PUBillboardChain::setupBuffers(void)
  145. {
  146. //setupVertexDeclaration();
  147. if (_buffersNeedRecreating)
  148. {
  149. CC_SAFE_RELEASE(_vertexBuffer);
  150. CC_SAFE_RELEASE(_indexBuffer);
  151. GLsizei stride = sizeof(VertexInfo);
  152. _vertexBuffer = VertexBuffer::create(stride, (int)_chainElementList.size() * 2);
  153. _vertexBuffer->retain();
  154. VertexInfo vi = {Vec3(0.0f, 0.0f, 0.0f), Vec2(0.0f, 0.0f), Vec4::ONE};
  155. _vertices.resize(_chainElementList.size() * 2, vi);
  156. _indexBuffer = IndexBuffer::create(IndexBuffer::IndexType::INDEX_TYPE_SHORT_16, (int)(_chainCount * _maxElementsPerChain * 6));
  157. _indexBuffer->retain();
  158. _indices.resize(_chainCount * _maxElementsPerChain * 6, 0);
  159. //// Create the vertex buffer (always dynamic due to the camera adjust)
  160. //HardwareVertexBufferSharedPtr pBuffer =
  161. // HardwareBufferManager::getSingleton().createVertexBuffer(
  162. // _vertexData->vertexDeclaration->getVertexSize(0),
  163. // _vertexData->vertexCount,
  164. // HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
  165. //// (re)Bind the buffer
  166. //// Any existing buffer will lose its reference count and be destroyed
  167. //_vertexData->vertexBufferBinding->setBinding(0, pBuffer);
  168. //_indexData->indexBuffer =
  169. // HardwareBufferManager::getSingleton().createIndexBuffer(
  170. // HardwareIndexBuffer::IT_16BIT,
  171. // _chainCount * _maxElementsPerChain * 6, // max we can use
  172. // _dynamic? HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY : HardwareBuffer::HBU_STATIC_WRITE_ONLY);
  173. //// NB we don't set the indexCount on IndexData here since we will
  174. //// probably use less than the maximum number of indices
  175. _buffersNeedRecreating = false;
  176. }
  177. }
  178. //-----------------------------------------------------------------------
  179. void PUBillboardChain::setMaxChainElements(size_t maxElements)
  180. {
  181. _maxElementsPerChain = maxElements;
  182. setupChainContainers();
  183. _buffersNeedRecreating = _indexContentDirty = _vertexContentDirty = true;
  184. }
  185. //-----------------------------------------------------------------------
  186. void PUBillboardChain::setNumberOfChains(size_t numChains)
  187. {
  188. _chainCount = numChains;
  189. setupChainContainers();
  190. _buffersNeedRecreating = _indexContentDirty = _vertexContentDirty = true;
  191. }
  192. //-----------------------------------------------------------------------
  193. void PUBillboardChain::setUseTextureCoords(bool use)
  194. {
  195. _useTexCoords = use;
  196. _vertexDeclDirty = _buffersNeedRecreating = true;
  197. _indexContentDirty = _vertexContentDirty = true;
  198. }
  199. //-----------------------------------------------------------------------
  200. void PUBillboardChain::setTextureCoordDirection(PUBillboardChain::TexCoordDirection dir)
  201. {
  202. _texCoordDir = dir;
  203. _vertexContentDirty = true;
  204. }
  205. //-----------------------------------------------------------------------
  206. void PUBillboardChain::setOtherTextureCoordRange(float start, float end)
  207. {
  208. _otherTexCoordRange[0] = start;
  209. _otherTexCoordRange[1] = end;
  210. _vertexContentDirty = true;
  211. }
  212. //-----------------------------------------------------------------------
  213. void PUBillboardChain::setUseVertexColours(bool use)
  214. {
  215. _useVertexColour = use;
  216. _vertexDeclDirty = _buffersNeedRecreating = true;
  217. _indexContentDirty = _vertexContentDirty = true;
  218. }
  219. //-----------------------------------------------------------------------
  220. void PUBillboardChain::setDynamic(bool dyn)
  221. {
  222. _dynamic = dyn;
  223. _buffersNeedRecreating = _indexContentDirty = _vertexContentDirty = true;
  224. }
  225. //-----------------------------------------------------------------------
  226. void PUBillboardChain::addChainElement(size_t chainIndex,
  227. const PUBillboardChain::Element& dtls)
  228. {
  229. CCASSERT(chainIndex < _chainCount, "chainIndex out of bounds");
  230. ChainSegment& seg = _chainSegmentList[chainIndex];
  231. if (seg.head == SEGMENT_EMPTY)
  232. {
  233. // Tail starts at end, head grows backwards
  234. seg.tail = _maxElementsPerChain - 1;
  235. seg.head = seg.tail;
  236. }
  237. else
  238. {
  239. if (seg.head == 0)
  240. {
  241. // Wrap backwards
  242. seg.head = _maxElementsPerChain - 1;
  243. }
  244. else
  245. {
  246. // Just step backward
  247. --seg.head;
  248. }
  249. // Run out of elements?
  250. if (seg.head == seg.tail)
  251. {
  252. // Move tail backwards too, losing the end of the segment and re-using
  253. // it in the head
  254. if (seg.tail == 0)
  255. seg.tail = _maxElementsPerChain - 1;
  256. else
  257. --seg.tail;
  258. }
  259. }
  260. // Set the details
  261. _chainElementList[seg.start + seg.head] = dtls;
  262. _vertexContentDirty = true;
  263. _indexContentDirty = true;
  264. _boundsDirty = true;
  265. //// tell parent node to update bounds
  266. //if (mParentNode)
  267. // mParentNode->needUpdate();
  268. }
  269. //-----------------------------------------------------------------------
  270. void PUBillboardChain::removeChainElement(size_t chainIndex)
  271. {
  272. CCASSERT(chainIndex < _chainCount, "chainIndex out of bounds");
  273. ChainSegment& seg = _chainSegmentList[chainIndex];
  274. if (seg.head == SEGMENT_EMPTY)
  275. return; // do nothing, nothing to remove
  276. if (seg.tail == seg.head)
  277. {
  278. // last item
  279. seg.head = seg.tail = SEGMENT_EMPTY;
  280. }
  281. else if (seg.tail == 0)
  282. {
  283. seg.tail = _maxElementsPerChain - 1;
  284. }
  285. else
  286. {
  287. --seg.tail;
  288. }
  289. // we removed an entry so indexes need updating
  290. _vertexContentDirty = true;
  291. _indexContentDirty = true;
  292. _boundsDirty = true;
  293. //// tell parent node to update bounds
  294. //if (mParentNode)
  295. // mParentNode->needUpdate();
  296. }
  297. //-----------------------------------------------------------------------
  298. void PUBillboardChain::clearChain(size_t chainIndex)
  299. {
  300. CCASSERT(chainIndex < _chainCount, "chainIndex out of bounds");
  301. ChainSegment& seg = _chainSegmentList[chainIndex];
  302. // Just reset head & tail
  303. seg.tail = seg.head = SEGMENT_EMPTY;
  304. // we removed an entry so indexes need updating
  305. _vertexContentDirty = true;
  306. _indexContentDirty = true;
  307. _boundsDirty = true;
  308. //// tell parent node to update bounds
  309. //if (mParentNode)
  310. // mParentNode->needUpdate();
  311. }
  312. //-----------------------------------------------------------------------
  313. void PUBillboardChain::clearAllChains(void)
  314. {
  315. for (size_t i = 0; i < _chainCount; ++i)
  316. {
  317. clearChain(i);
  318. }
  319. }
  320. //-----------------------------------------------------------------------
  321. void PUBillboardChain::setFaceCamera( bool faceCamera, const Vec3 &normalVector )
  322. {
  323. _faceCamera = faceCamera;
  324. _normalBase = normalVector;
  325. _normalBase.normalize();
  326. _vertexContentDirty = true;
  327. }
  328. //-----------------------------------------------------------------------
  329. void PUBillboardChain::updateChainElement(size_t chainIndex, size_t elementIndex,
  330. const PUBillboardChain::Element& dtls)
  331. {
  332. CCASSERT(chainIndex < _chainCount, "chainIndex out of bounds");
  333. ChainSegment& seg = _chainSegmentList[chainIndex];
  334. CCASSERT(seg.head != SEGMENT_EMPTY, "Chain segment is empty");
  335. size_t idx = seg.head + elementIndex;
  336. // adjust for the edge and start
  337. idx = (idx % _maxElementsPerChain) + seg.start;
  338. _chainElementList[idx] = dtls;
  339. _vertexContentDirty = true;
  340. _boundsDirty = true;
  341. //// tell parent node to update bounds
  342. //if (mParentNode)
  343. // mParentNode->needUpdate();
  344. }
  345. //-----------------------------------------------------------------------
  346. const PUBillboardChain::Element&
  347. PUBillboardChain::getChainElement(size_t chainIndex, size_t elementIndex) const
  348. {
  349. CCASSERT(chainIndex < _chainCount, "chainIndex out of bounds");
  350. const ChainSegment& seg = _chainSegmentList[chainIndex];
  351. size_t idx = seg.head + elementIndex;
  352. // adjust for the edge and start
  353. idx = (idx % _maxElementsPerChain) + seg.start;
  354. return _chainElementList[idx];
  355. }
  356. //-----------------------------------------------------------------------
  357. size_t PUBillboardChain::getNumChainElements(size_t chainIndex) const
  358. {
  359. CCASSERT(chainIndex < _chainCount, "chainIndex out of bounds");
  360. const ChainSegment& seg = _chainSegmentList[chainIndex];
  361. if( seg.tail < seg.head )
  362. {
  363. return seg.tail - seg.head + _maxElementsPerChain + 1;
  364. }
  365. else
  366. {
  367. return seg.tail - seg.head + 1;
  368. }
  369. }
  370. //-----------------------------------------------------------------------
  371. void PUBillboardChain::updateVertexBuffer(const Mat4 &camMat)
  372. {
  373. setupBuffers();
  374. // The contents of the vertex buffer are correct if they are not dirty
  375. // and the camera used to build the vertex buffer is still the current
  376. // camera.
  377. if (!_vertexContentDirty)
  378. return;
  379. VertexInfo vi = {Vec3(0.0f, 0.0f, 0.0f), Vec2(0.0f, 0.0f), Vec4::ONE};
  380. _vertices.assign(_vertices.size(), vi);
  381. //HardwareVertexBufferSharedPtr pBuffer =
  382. // _vertexData->vertexBufferBinding->getBuffer(0);
  383. //void* pBufferStart = pBuffer->lock(HardwareBuffer::HBL_DISCARD);
  384. //const Vector3& camPos = cam->getDerivedPosition();
  385. //Vector3 eyePos = mParentNode->_getDerivedOrientation().Inverse() *
  386. // (camPos - mParentNode->_getDerivedPosition()) / mParentNode->_getDerivedScale();
  387. Vec3 eyePos(camMat.m[12], camMat.m[13], camMat.m[14]);
  388. Vec3 chainTangent;
  389. for (ChainSegmentList::iterator segi = _chainSegmentList.begin();
  390. segi != _chainSegmentList.end(); ++segi)
  391. {
  392. ChainSegment& seg = *segi;
  393. // Skip 0 or 1 element segment counts
  394. if (seg.head != SEGMENT_EMPTY && seg.head != seg.tail)
  395. {
  396. size_t laste = seg.head;
  397. for (size_t e = seg.head; ; ++e) // until break
  398. {
  399. // Wrap forwards
  400. if (e == _maxElementsPerChain)
  401. e = 0;
  402. Element& elem = _chainElementList[e + seg.start];
  403. CCASSERT (((e + seg.start) * 2) < 65536, "Too many elements!");
  404. unsigned short vertexIndex = static_cast<unsigned short>((e + seg.start) * 2);
  405. //// Determine base pointer to vertex #1
  406. //void* pBase = static_cast<void*>(
  407. // static_cast<char*>(pBufferStart) +
  408. // pBuffer->getVertexSize() * baseIdx);
  409. // Get index of next item
  410. size_t nexte = e + 1;
  411. if (nexte == _maxElementsPerChain)
  412. nexte = 0;
  413. if (e == seg.head)
  414. {
  415. // No laste, use next item
  416. chainTangent = _chainElementList[nexte + seg.start].position - elem.position;
  417. }
  418. else if (e == seg.tail)
  419. {
  420. // No nexte, use only last item
  421. chainTangent = elem.position - _chainElementList[laste + seg.start].position;
  422. }
  423. else
  424. {
  425. // A mid position, use tangent across both prev and next
  426. chainTangent = _chainElementList[nexte + seg.start].position - _chainElementList[laste + seg.start].position;
  427. }
  428. Vec3 vP1ToEye;
  429. //if( _faceCamera )
  430. vP1ToEye = eyePos - elem.position;
  431. //else
  432. // vP1ToEye = elem.orientation * _normalBase;
  433. Vec3 vPerpendicular;
  434. Vec3::cross(chainTangent, vP1ToEye, &vPerpendicular);
  435. vPerpendicular.normalize();
  436. vPerpendicular *= (elem.width * 0.5f);
  437. Vec3 pos0 = elem.position - vPerpendicular;
  438. Vec3 pos1 = elem.position + vPerpendicular;
  439. //float* pFloat = static_cast<float*>(pBase);
  440. //// pos1
  441. //*pFloat++ = pos0.x;
  442. //*pFloat++ = pos0.y;
  443. //*pFloat++ = pos0.z;
  444. _vertices[vertexIndex + 0].position = pos0;
  445. //pBase = static_cast<void*>(pFloat);
  446. if (_useVertexColour)
  447. {
  448. //RGBA* pCol = static_cast<RGBA*>(pBase);
  449. //Root::getSingleton().convertColourValue(elem.colour, pCol);
  450. //pCol++;
  451. //pBase = static_cast<void*>(pCol);
  452. _vertices[vertexIndex + 0].color = elem.color;
  453. }
  454. if (_useTexCoords)
  455. {
  456. //pFloat = static_cast<float*>(pBase);
  457. if (_texCoordDir == TCD_U)
  458. {
  459. //*pFloat++ = elem.texCoord;
  460. //*pFloat++ = _otherTexCoordRange[0];
  461. _vertices[vertexIndex + 0].uv.x = elem.texCoord;
  462. _vertices[vertexIndex + 0].uv.y = _otherTexCoordRange[0];
  463. }
  464. else
  465. {
  466. //*pFloat++ = _otherTexCoordRange[0];
  467. //*pFloat++ = elem.texCoord;
  468. _vertices[vertexIndex + 0].uv.x = _otherTexCoordRange[0];
  469. _vertices[vertexIndex + 0].uv.y = elem.texCoord;
  470. }
  471. //pBase = static_cast<void*>(pFloat);
  472. }
  473. // pos2
  474. //pFloat = static_cast<float*>(pBase);
  475. //*pFloat++ = pos1.x;
  476. //*pFloat++ = pos1.y;
  477. //*pFloat++ = pos1.z;
  478. //pBase = static_cast<void*>(pFloat);
  479. _vertices[vertexIndex + 1].position = pos1;
  480. if (_useVertexColour)
  481. {
  482. //RGBA* pCol = static_cast<RGBA*>(pBase);
  483. //Root::getSingleton().convertColourValue(elem.colour, pCol);
  484. //pCol++;
  485. //pBase = static_cast<void*>(pCol);
  486. _vertices[vertexIndex + 1].color = elem.color;
  487. }
  488. if (_useTexCoords)
  489. {
  490. //pFloat = static_cast<float*>(pBase);
  491. if (_texCoordDir == TCD_U)
  492. {
  493. //*pFloat++ = elem.texCoord;
  494. //*pFloat++ = _otherTexCoordRange[1];
  495. _vertices[vertexIndex + 1].uv.x = elem.texCoord;
  496. _vertices[vertexIndex + 1].uv.y = _otherTexCoordRange[1];
  497. }
  498. else
  499. {
  500. //*pFloat++ = _otherTexCoordRange[1];
  501. //*pFloat++ = elem.texCoord;
  502. _vertices[vertexIndex + 1].uv.x = _otherTexCoordRange[1];
  503. _vertices[vertexIndex + 1].uv.y = elem.texCoord;
  504. }
  505. }
  506. if (e == seg.tail)
  507. break; // last one
  508. laste = e;
  509. //vertexIndex += 2;
  510. } // element
  511. } // segment valid?
  512. } // each segment
  513. _vertexBuffer->updateVertices(&_vertices[0], (int)_vertices.size(), 0);
  514. //pBuffer->unlock();
  515. //_vertexCameraUsed = cam;
  516. _vertexContentDirty = false;
  517. }
  518. //-----------------------------------------------------------------------
  519. void PUBillboardChain::updateIndexBuffer(void)
  520. {
  521. setupBuffers();
  522. if (_indexContentDirty)
  523. {
  524. //uint16* pShort = static_cast<uint16*>(
  525. // _indexData->indexBuffer->lock(HardwareBuffer::HBL_DISCARD));
  526. //_indexData->indexCount = 0;
  527. // indexes
  528. unsigned short index = 0;
  529. for (ChainSegmentList::iterator segi = _chainSegmentList.begin();
  530. segi != _chainSegmentList.end(); ++segi)
  531. {
  532. ChainSegment& seg = *segi;
  533. // Skip 0 or 1 element segment counts
  534. if (seg.head != SEGMENT_EMPTY && seg.head != seg.tail)
  535. {
  536. // Start from head + 1 since it's only useful in pairs
  537. size_t laste = seg.head;
  538. while(1) // until break
  539. {
  540. size_t e = laste + 1;
  541. // Wrap forwards
  542. if (e == _maxElementsPerChain)
  543. e = 0;
  544. // indexes of this element are (e * 2) and (e * 2) + 1
  545. // indexes of the last element are the same, -2
  546. CCASSERT (((e + seg.start) * 2) < 65536, "Too many elements!");
  547. unsigned short baseIdx = static_cast<unsigned short>((e + seg.start) * 2);
  548. unsigned short lastBaseIdx = static_cast<unsigned short>((laste + seg.start) * 2);
  549. //*pShort++ = lastBaseIdx;
  550. //*pShort++ = lastBaseIdx + 1;
  551. //*pShort++ = baseIdx;
  552. //*pShort++ = lastBaseIdx + 1;
  553. //*pShort++ = baseIdx + 1;
  554. //*pShort++ = baseIdx;
  555. //_indexData->indexCount += 6;
  556. _indices[index++] = lastBaseIdx;
  557. _indices[index++] = lastBaseIdx + 1;
  558. _indices[index++] = baseIdx;
  559. _indices[index++] = lastBaseIdx + 1;
  560. _indices[index++] = baseIdx + 1;
  561. _indices[index++] = baseIdx;
  562. if (e == seg.tail)
  563. break; // last one
  564. laste = e;
  565. }
  566. }
  567. }
  568. _indexBuffer->updateIndices(&_indices[0], (int)_indices.size(), 0);
  569. //_indexData->indexBuffer->unlock();
  570. _indexContentDirty = false;
  571. }
  572. }
  573. //-----------------------------------------------------------------------
  574. void PUBillboardChain::init( const std::string &texFile )
  575. {
  576. GLProgram* glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_PARTICLE_COLOR);
  577. if (!texFile.empty())
  578. {
  579. auto tex = Director::getInstance()->getTextureCache()->addImage(texFile);
  580. if (tex)
  581. {
  582. _texture = tex;
  583. glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_PARTICLE_TEXTURE);
  584. }
  585. }
  586. auto glProgramState = GLProgramState::create(glProgram);
  587. glProgramState->retain();
  588. GLsizei stride = sizeof(VertexInfo);
  589. glProgramState->setVertexAttribPointer(s_attributeNames[GLProgram::VERTEX_ATTRIB_POSITION], 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)offsetof(VertexInfo, position));
  590. glProgramState->setVertexAttribPointer(s_attributeNames[GLProgram::VERTEX_ATTRIB_TEX_COORD], 2, GL_FLOAT, GL_FALSE, stride, (GLvoid*)offsetof(VertexInfo, uv));
  591. glProgramState->setVertexAttribPointer(s_attributeNames[GLProgram::VERTEX_ATTRIB_COLOR], 4, GL_FLOAT, GL_FALSE, stride, (GLvoid*)offsetof(VertexInfo, color));
  592. _glProgramState = glProgramState;
  593. _meshCommand = new (std::nothrow) MeshCommand();
  594. _meshCommand->setSkipBatching(true);
  595. _meshCommand->setTransparent(true);
  596. _stateBlock->setDepthTest(true);
  597. _stateBlock->setDepthWrite(false);
  598. _stateBlock->setCullFaceSide(RenderState::CULL_FACE_SIDE_BACK);
  599. _stateBlock->setCullFace(true);
  600. }
  601. void PUBillboardChain::render( Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem )
  602. {
  603. auto camera = Camera::getVisitingCamera();
  604. auto cameraMat = camera->getNodeToWorldTransform();
  605. if (!_chainSegmentList.empty())
  606. {
  607. updateVertexBuffer(cameraMat);
  608. updateIndexBuffer();
  609. if (!_vertices.empty() && !_indices.empty())
  610. {
  611. GLuint texId = this->getTextureName();
  612. _stateBlock->setBlendFunc(particleSystem->getBlendFunc());
  613. _meshCommand->init(0,
  614. texId,
  615. _glProgramState,
  616. _stateBlock,
  617. _vertexBuffer->getVBO(),
  618. _indexBuffer->getVBO(),
  619. GL_TRIANGLES,
  620. GL_UNSIGNED_SHORT,
  621. _indices.size(),
  622. transform,
  623. Node::FLAGS_RENDER_AS_3D);
  624. _meshCommand->setSkipBatching(true);
  625. _meshCommand->setTransparent(true);
  626. _glProgramState->setUniformVec4("u_color", Vec4(1,1,1,1));
  627. renderer->addCommand(_meshCommand);
  628. }
  629. }
  630. }
  631. void PUBillboardChain::setDepthTest( bool isDepthTest )
  632. {
  633. _stateBlock->setDepthTest(isDepthTest);
  634. }
  635. void PUBillboardChain::setDepthWrite( bool isDepthWrite )
  636. {
  637. _stateBlock->setDepthWrite(isDepthWrite);
  638. }
  639. void PUBillboardChain::setBlendFunc(const BlendFunc& blendFunc)
  640. {
  641. _stateBlock->setBlendFunc(blendFunc);
  642. }
  643. GLuint PUBillboardChain::getTextureName()
  644. {
  645. if (Director::getInstance()->getTextureCache()->getTextureForKey(_texFile) == nullptr)
  646. {
  647. _texture = nullptr;
  648. this->init("");
  649. }
  650. else if (_texture == nullptr)
  651. {
  652. this->init(_texFile);
  653. }
  654. if (_texture == nullptr)
  655. return 0;
  656. return _texture->getName();
  657. }
  658. //-----------------------------------------------------------------------
  659. //void PUBillboardChain::_updateRenderQueue(RenderQueue* queue)
  660. //{
  661. // updateIndexBuffer();
  662. //
  663. // if (mIndexData->indexCount > 0)
  664. // {
  665. // if (mRenderQueuePrioritySet)
  666. // queue->addRenderable(this, mRenderQueueID, mRenderQueuePriority);
  667. // else if (mRenderQueueIDSet)
  668. // queue->addRenderable(this, mRenderQueueID);
  669. // else
  670. // queue->addRenderable(this);
  671. // }
  672. //
  673. //}
  674. //-----------------------------------------------------------------------
  675. //void PUBillboardChain::getRenderOperation(RenderOperation& op)
  676. //{
  677. // op.indexData = mIndexData;
  678. // op.operationType = RenderOperation::OT_TRIANGLE_LIST;
  679. // op.srcRenderable = this;
  680. // op.useIndexes = true;
  681. // op.vertexData = mVertexData;
  682. //}
  683. //-----------------------------------------------------------------------
  684. //bool PUBillboardChain::preRender(SceneManager* sm, RenderSystem* rsys)
  685. //{
  686. // // Retrieve the current viewport from the scene manager.
  687. // // The viewport is only valid during a viewport update.
  688. // Viewport *currentViewport = sm->getCurrentViewport();
  689. // if( !currentViewport )
  690. // return false;
  691. //
  692. // updateVertexBuffer(currentViewport->getCamera());
  693. // return true;
  694. //}
  695. //-----------------------------------------------------------------------
  696. //void PUBillboardChain::getWorldTransforms(Matrix4* xform) const
  697. //{
  698. // *xform = _getParentNodeFullTransform();
  699. //}
  700. //---------------------------------------------------------------------
  701. NS_CC_END