CCParticle3DRender.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /****************************************************************************
  2. Copyright (c) 2015-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "extensions/Particle3D/CCParticleSystem3D.h"
  21. #include "extensions/Particle3D/CCParticle3DRender.h"
  22. #include "renderer/CCMeshCommand.h"
  23. #include "renderer/CCRenderer.h"
  24. #include "renderer/CCTextureCache.h"
  25. #include "renderer/CCGLProgramState.h"
  26. #include "renderer/CCGLProgramCache.h"
  27. #include "renderer/CCVertexIndexBuffer.h"
  28. #include "renderer/CCVertexAttribBinding.h"
  29. #include "base/CCDirector.h"
  30. #include "3d/CCSprite3D.h"
  31. #include "2d/CCCamera.h"
  32. NS_CC_BEGIN
  33. Particle3DQuadRender::Particle3DQuadRender()
  34. : _meshCommand(nullptr)
  35. , _texture(nullptr)
  36. , _glProgramState(nullptr)
  37. , _indexBuffer(nullptr)
  38. , _vertexBuffer(nullptr)
  39. , _texFile("")
  40. {
  41. }
  42. Particle3DQuadRender::~Particle3DQuadRender()
  43. {
  44. CC_SAFE_DELETE(_meshCommand);
  45. //CC_SAFE_RELEASE(_texture);
  46. CC_SAFE_RELEASE(_glProgramState);
  47. CC_SAFE_RELEASE(_vertexBuffer);
  48. CC_SAFE_RELEASE(_indexBuffer);
  49. }
  50. Particle3DQuadRender* Particle3DQuadRender::create(const std::string& texFile)
  51. {
  52. auto ret = new (std::nothrow)Particle3DQuadRender();
  53. if (ret && ret->initQuadRender(texFile))
  54. {
  55. ret->_texFile = texFile;
  56. ret->autorelease();
  57. }
  58. else
  59. {
  60. CC_SAFE_DELETE(ret);
  61. }
  62. return ret;
  63. }
  64. void Particle3DQuadRender::render(Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem)
  65. {
  66. //batch and generate draw
  67. const ParticlePool &particlePool = particleSystem->getParticlePool();
  68. if (!_isVisible || particlePool.empty())
  69. return;
  70. if (_vertexBuffer == nullptr){
  71. GLsizei stride = sizeof(Particle3DQuadRender::posuvcolor);
  72. _vertexBuffer = VertexBuffer::create(stride, 4 * particleSystem->getParticleQuota());
  73. if (_vertexBuffer == nullptr)
  74. {
  75. CCLOG("Particle3DQuadRender::render create vertex buffer failed");
  76. return;
  77. }
  78. _vertexBuffer->retain();
  79. }
  80. if (_indexBuffer == nullptr){
  81. _indexBuffer = IndexBuffer::create(IndexBuffer::IndexType::INDEX_TYPE_SHORT_16, 6 * particleSystem->getParticleQuota());
  82. if (_indexBuffer == nullptr)
  83. {
  84. CCLOG("Particle3DQuadRender::render create index buffer failed");
  85. return;
  86. }
  87. _indexBuffer->retain();
  88. }
  89. ParticlePool::PoolList activeParticleList = particlePool.getActiveDataList();
  90. if (_posuvcolors.size() < activeParticleList.size() * 4)
  91. {
  92. _posuvcolors.resize(activeParticleList.size() * 4);
  93. _indexData.resize(activeParticleList.size() * 6);
  94. }
  95. auto camera = Camera::getVisitingCamera();
  96. auto cameraMat = camera->getNodeToWorldTransform();
  97. const Mat4 &viewMat = cameraMat.getInversed();
  98. Mat4 pRotMat;
  99. Vec3 right(cameraMat.m[0], cameraMat.m[1], cameraMat.m[2]);
  100. Vec3 up(cameraMat.m[4], cameraMat.m[5], cameraMat.m[6]);
  101. Vec3 backward(cameraMat.m[8], cameraMat.m[9], cameraMat.m[10]);
  102. Vec3 position; //particle position
  103. int vertexindex = 0;
  104. int index = 0;
  105. for (auto iter : activeParticleList)
  106. {
  107. auto particle = iter;
  108. Vec3 halfwidth = particle->width * 0.5f * right;
  109. Vec3 halfheight = particle->height * 0.5f * up;
  110. //transform.transformPoint(particle->position, &position);
  111. position = particle->position;
  112. _posuvcolors[vertexindex].position = (position + (- halfwidth - halfheight));
  113. _posuvcolors[vertexindex].color = particle->color;
  114. _posuvcolors[vertexindex].uv.set(particle->lb_uv);
  115. _posuvcolors[vertexindex + 1].position = (position + (halfwidth - halfheight));
  116. _posuvcolors[vertexindex + 1].color = particle->color;
  117. _posuvcolors[vertexindex + 1].uv.set(particle->rt_uv.x, particle->lb_uv.y);
  118. _posuvcolors[vertexindex + 2].position = (position + (- halfwidth + halfheight));
  119. _posuvcolors[vertexindex + 2].color = particle->color;
  120. _posuvcolors[vertexindex + 2].uv.set(particle->lb_uv.x, particle->rt_uv.y);
  121. _posuvcolors[vertexindex + 3].position = (position + (halfwidth + halfheight));
  122. _posuvcolors[vertexindex + 3].color = particle->color;
  123. _posuvcolors[vertexindex + 3].uv.set(particle->rt_uv);
  124. _indexData[index] = vertexindex;
  125. _indexData[index + 1] = vertexindex + 1;
  126. _indexData[index + 2] = vertexindex + 3;
  127. _indexData[index + 3] = vertexindex;
  128. _indexData[index + 4] = vertexindex + 3;
  129. _indexData[index + 5] = vertexindex + 2;
  130. index += 6;
  131. vertexindex += 4;
  132. }
  133. _posuvcolors.erase(_posuvcolors.begin() + vertexindex, _posuvcolors.end());
  134. _indexData.erase(_indexData.begin() + index, _indexData.end());
  135. _vertexBuffer->updateVertices(&_posuvcolors[0], vertexindex/* * sizeof(_posuvcolors[0])*/, 0);
  136. _indexBuffer->updateIndices(&_indexData[0], index/* * sizeof(unsigned short)*/, 0);
  137. GLuint texId = (_texture ? _texture->getName() : 0);
  138. float depthZ = -(viewMat.m[2] * transform.m[12] + viewMat.m[6] * transform.m[13] + viewMat.m[10] * transform.m[14] + viewMat.m[14]);
  139. _meshCommand->init(
  140. depthZ,
  141. texId,
  142. _glProgramState,
  143. _stateBlock,
  144. _vertexBuffer->getVBO(),
  145. _indexBuffer->getVBO(),
  146. GL_TRIANGLES,
  147. GL_UNSIGNED_SHORT,
  148. index,
  149. transform,
  150. 0);
  151. _glProgramState->setUniformVec4("u_color", Vec4(1,1,1,1));
  152. renderer->addCommand(_meshCommand);
  153. }
  154. bool Particle3DQuadRender::initQuadRender( const std::string& texFile )
  155. {
  156. GLProgram* glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_PARTICLE_COLOR);
  157. if (!texFile.empty())
  158. {
  159. auto tex = Director::getInstance()->getTextureCache()->addImage(texFile);
  160. if (tex)
  161. {
  162. _texture = tex;
  163. glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_PARTICLE_TEXTURE);
  164. }
  165. else
  166. _texture = nullptr;
  167. }
  168. auto glProgramState = GLProgramState::create(glProgram);
  169. glProgramState->retain();
  170. GLsizei stride = sizeof(Particle3DQuadRender::posuvcolor);
  171. glProgramState->setVertexAttribPointer(s_attributeNames[GLProgram::VERTEX_ATTRIB_POSITION], 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)offsetof(posuvcolor, position));
  172. glProgramState->setVertexAttribPointer(s_attributeNames[GLProgram::VERTEX_ATTRIB_TEX_COORD], 2, GL_FLOAT, GL_FALSE, stride, (GLvoid*)offsetof(posuvcolor, uv));
  173. glProgramState->setVertexAttribPointer(s_attributeNames[GLProgram::VERTEX_ATTRIB_COLOR], 4, GL_FLOAT, GL_FALSE, stride, (GLvoid*)offsetof(posuvcolor, color));
  174. _glProgramState = glProgramState;
  175. //ret->_vertexBuffer = VertexBuffer::create(stride, 4 * 10000);
  176. //ret->_vertexBuffer->retain();
  177. //ret->_indexBuffer = IndexBuffer::create(IndexBuffer::IndexType::INDEX_TYPE_SHORT_16, 6 * 10000);
  178. //ret->_indexBuffer->retain();
  179. _meshCommand = new (std::nothrow) MeshCommand();
  180. _meshCommand->setSkipBatching(true);
  181. _meshCommand->setTransparent(true);
  182. _stateBlock->setDepthTest(_depthTest);
  183. _stateBlock->setDepthWrite(_depthWrite);
  184. _stateBlock->setCullFace(true);
  185. _stateBlock->setCullFaceSide(RenderState::CULL_FACE_SIDE_BACK);
  186. return true;
  187. }
  188. void Particle3DQuadRender::reset()
  189. {
  190. this->initQuadRender(_texFile);
  191. }
  192. //////////////////////////////////////////////////////////////////////////////
  193. Particle3DModelRender::Particle3DModelRender()
  194. : _spriteSize(Vec3::ONE)
  195. {
  196. }
  197. Particle3DModelRender::~Particle3DModelRender()
  198. {
  199. for (auto iter : _spriteList){
  200. iter->release();
  201. }
  202. }
  203. Particle3DModelRender* Particle3DModelRender::create(const std::string& modelFile, const std::string &texFile)
  204. {
  205. auto ret = new (std::nothrow) Particle3DModelRender();
  206. ret->_modelFile = modelFile;
  207. ret->_texFile = texFile;
  208. return ret;
  209. }
  210. void Particle3DModelRender::render(Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem)
  211. {
  212. if (!_isVisible)
  213. return;
  214. if (_spriteList.empty()){
  215. for (unsigned int i = 0; i < particleSystem->getParticleQuota(); ++i){
  216. Sprite3D *sprite = Sprite3D::create(_modelFile);
  217. if (sprite == nullptr)
  218. {
  219. CCLOG("failed to load file %s", _modelFile.c_str());
  220. continue;
  221. }
  222. sprite->setTexture(_texFile);
  223. sprite->retain();
  224. _spriteList.push_back(sprite);
  225. }
  226. if (!_spriteList.empty()){
  227. const AABB &aabb = _spriteList[0]->getAABB();
  228. Vec3 corners[8];
  229. aabb.getCorners(corners);
  230. _spriteSize = corners[3] - corners[6];
  231. }
  232. }
  233. const ParticlePool& particlePool = particleSystem->getParticlePool();
  234. ParticlePool::PoolList activeParticleList = particlePool.getActiveDataList();
  235. Mat4 mat;
  236. Mat4 rotMat;
  237. Mat4 sclMat;
  238. Quaternion q;
  239. transform.decompose(nullptr, &q, nullptr);
  240. unsigned int index = 0;
  241. for (auto iter : activeParticleList)
  242. {
  243. auto particle = iter;
  244. Mat4::createRotation(q * particle->orientation, &rotMat);
  245. sclMat.m[0] = particle->width / _spriteSize.x;
  246. sclMat.m[5] = particle->height / _spriteSize.y;
  247. sclMat.m[10] = particle->depth / _spriteSize.z;
  248. mat = rotMat * sclMat;
  249. mat.m[12] = particle->position.x;
  250. mat.m[13] = particle->position.y;
  251. mat.m[14] = particle->position.z;
  252. _spriteList[index++]->draw(renderer, mat, 0);
  253. }
  254. }
  255. void Particle3DModelRender::reset()
  256. {
  257. for (auto iter : _spriteList){
  258. iter->release();
  259. }
  260. _spriteList.clear();
  261. }
  262. // MARK: Particle3DRender
  263. Particle3DRender::Particle3DRender()
  264. : _particleSystem(nullptr)
  265. , _isVisible(true)
  266. , _rendererScale(Vec3::ONE)
  267. , _depthTest(true)
  268. , _depthWrite(false)
  269. {
  270. _stateBlock = RenderState::StateBlock::create();
  271. _stateBlock->retain();
  272. _stateBlock->setCullFace(false);
  273. _stateBlock->setCullFaceSide(RenderState::CULL_FACE_SIDE_BACK);
  274. _stateBlock->setDepthTest(false);
  275. _stateBlock->setDepthWrite(false);
  276. _stateBlock->setBlend(true);
  277. };
  278. Particle3DRender::~Particle3DRender()
  279. {
  280. _stateBlock->release();
  281. }
  282. void Particle3DRender::copyAttributesTo (Particle3DRender *render)
  283. {
  284. CC_SAFE_RELEASE(render->_stateBlock);
  285. render->_stateBlock = _stateBlock;
  286. CC_SAFE_RETAIN(render->_stateBlock);
  287. render->_isVisible = _isVisible;
  288. render->_rendererScale = _rendererScale;
  289. render->_depthTest = _depthTest;
  290. render->_depthWrite = _depthWrite;
  291. }
  292. void Particle3DRender::notifyStart()
  293. {
  294. setVisible(true);
  295. }
  296. void Particle3DRender::notifyStop()
  297. {
  298. setVisible(false);
  299. }
  300. void Particle3DRender::notifyRescaled( const Vec3& scale )
  301. {
  302. _rendererScale = scale;
  303. }
  304. void Particle3DRender::setDepthTest( bool isDepthTest )
  305. {
  306. _depthTest = isDepthTest;
  307. _stateBlock->setDepthTest(_depthTest);
  308. }
  309. void Particle3DRender::setDepthWrite( bool isDepthWrite )
  310. {
  311. _depthWrite = isDepthWrite;
  312. _stateBlock->setDepthWrite(_depthWrite);
  313. }
  314. void Particle3DRender::setBlendFunc(const BlendFunc &blendFunc)
  315. {
  316. _stateBlock->setBlendFunc(blendFunc);
  317. }
  318. NS_CC_END