12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022 |
- /****************************************************************************
- Copyright (c) 2008-2010 Ricardo Quesada
- Copyright (c) 2009 Leonardo Kasperavičius
- Copyright (c) 2010-2012 cocos2d-x.org
- Copyright (c) 2011 Zynga Inc.
- Copyright (c) 2013-2017 Chukong Technologies Inc.
- http://www.cocos2d-x.org
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- //#include "ZMLParticleSystemQuad.h"
- #include "ZMLParticleSystemQuad.h"
- //#include "ZGHeader.h"
- //#include <algorithm>
- //#include "2d/CCSpriteFrame.h"
- //#include "2d/CCParticleBatchNode.h"
- //#include "renderer/CCTextureAtlas.h"
- //#include "renderer/ccGLStateCache.h"
- //#include "renderer/CCRenderer.h"
- //#include "base/CCDirector.h"
- //#include "base/CCEventType.h"
- //#include "base/CCConfiguration.h"
- //#include "base/CCEventListenerCustom.h"
- //#include "base/CCEventDispatcher.h"
- //#include "base/ccUTF8.h"
- //#include "common/CocosConfig.h"
- NS_RU_BEGIN
- ZMLParticleSystemQuad::ZMLParticleSystemQuad()
- :_quads(nullptr)
- ,_indices(nullptr)
- ,_VAOname(0)
- {
- m_lightSprite = nullptr;
- memset(_buffersVBO, 0, sizeof(_buffersVBO));
- _vertexInfo = (PARTICLE_VERTEX_INFO*)malloc(100 * sizeof(PARTICLE_VERTEX_INFO));
- memset(_vertexInfo,1,100 * sizeof(PARTICLE_VERTEX_INFO));
- }
- ZMLParticleSystemQuad::~ZMLParticleSystemQuad()
- {
- if (m_lightSprite)
- {
- if (m_lightSprite->isRunning())
- {
- m_lightSprite->removeFromParent();
- }
- m_lightSprite->release();
- m_lightSprite = nullptr;
- }
- CC_SAFE_FREE(_vertexInfo);
-
- if (nullptr == _batchNode)
- {
- CC_SAFE_FREE(_quads);
- CC_SAFE_FREE(_indices);
- glDeleteBuffers(2, &_buffersVBO[0]);
- if (Configuration::getInstance()->supportsShareableVAO())
- {
- glDeleteVertexArrays(1, &_VAOname);
- GL::bindVAO(0);
- }
- }
- }
- // implementation ZMLParticleSystemQuad
- ZMLParticleSystemQuad * ZMLParticleSystemQuad::create(const std::string& filename)
- {
- ZMLParticleSystemQuad *ret = new (std::nothrow) ZMLParticleSystemQuad();
- if (ret && ret->initWithFile(filename))
- {
- ret->autorelease();
- return ret;
- }
- CC_SAFE_DELETE(ret);
- return ret;
- }
- ZMLParticleSystemQuad * ZMLParticleSystemQuad::createWithTotalParticles(int numberOfParticles) {
- ZMLParticleSystemQuad *ret = new (std::nothrow) ZMLParticleSystemQuad();
- if (ret && ret->initWithTotalParticles(numberOfParticles))
- {
- ret->autorelease();
- return ret;
- }
- CC_SAFE_DELETE(ret);
- return ret;
- }
- ZMLParticleSystemQuad * ZMLParticleSystemQuad::create(ValueMap &dictionary)
- {
- ZMLParticleSystemQuad *ret = new (std::nothrow) ZMLParticleSystemQuad();
- if (ret && ret->initWithDictionary(dictionary))
- {
- ret->autorelease();
- return ret;
- }
- CC_SAFE_DELETE(ret);
- return ret;
- }
- ZMLParticleSystemQuad* ZMLParticleSystemQuad::createWithJsonString(string jsonStr){
- ZMLParticleSystemQuad *ret = new (std::nothrow) ZMLParticleSystemQuad();
- if (ret && ret->initWithJsonString(jsonStr))
- {
- ret->autorelease();
- return ret;
- }
- CC_SAFE_DELETE(ret);
- return ret;
- }
- ZMLParticleSystemQuad* ZMLParticleSystemQuad::createWithJsonFile(string path){
- ZMLParticleSystemQuad *ret = new (std::nothrow) ZMLParticleSystemQuad();
- if (ret && ret->initWithJsonFile(path))
- {
- if (ret->getModule(ParticleSystemModuleFlag::TEXTURE_SHEET) == nullptr) {
- ret->autoUpdateTexture();
- }
-
- ret->_plName = path;
- ret->autorelease();
- return ret;
- }
- CC_SAFE_DELETE(ret);
- return ret;
- }
- //implementation ZMLParticleSystemQuad
- // overriding the init method
- bool ZMLParticleSystemQuad::initWithTotalParticles(int numberOfParticles)
- {
- // base initialization
- if( ParticleSystem::initWithTotalParticles(numberOfParticles) )
- {
- // allocating data space
- if( ! this->allocMemory() ) {
- this->release();
- return false;
- }
- initIndices();
- if (Configuration::getInstance()->supportsShareableVAO())
- {
- setupVBOandVAO();
- }
- else
- {
- setupVBO();
- }
-
- UpdateGLProgramState();
-
- #if CC_ENABLE_CACHE_TEXTURE_DATA
- // Need to listen the event only when not use batchnode, because it will use VBO
- auto listener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, CC_CALLBACK_1(ZMLParticleSystemQuad::listenRendererRecreated, this));
- _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
- #endif
- return true;
- }
- return false;
- }
- void ZMLParticleSystemQuad::UpdateGLProgramState()
- {
- auto fileUtiles = FileUtils::getInstance();
- if(_shaderType == SHADER_COLOR)
- {
- setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));
- }else {
- static string shaders[] = {"fx_alpha","fx_distort_additive","efx_normalMapped"};
- #if PARTICLE_EDITOR_MODE
- auto fragmentFullPath = fileUtiles->fullPathForFilename("shaders/" + shaders[_shaderType-1] + ".fsh");
- #else
- auto fragmentFullPath = fileUtiles->fullPathForFilename(shaders[_shaderType-1] + ".fsh");
- #endif
- auto fragSource = fileUtiles->getStringFromFile(fragmentFullPath);
-
- #if PARTICLE_EDITOR_MODE
- GLProgram* glProgram = GLProgram::createWithByteArrays(ccPositionTextureColor_noMVP_vert, fragSource.c_str());
- #else
- GLProgram* glProgram = GLProgramCache::getInstance()->getGLProgram(shaders[_shaderType-1]);
- #endif
-
- GLProgramState* state = GLProgramState::create(glProgram);
-
- if (_maskName.length() > 0)
- {
- #if PARTICLE_EDITOR_MODE
- auto alphaTexture = Director::getInstance()->getTextureCache()->addImage("img/" + _maskName);
- #else
- auto alphaTexture = Director::getInstance()->getTextureCache()->addImage(_maskName);
- #endif
-
- if (alphaTexture )
- {
- if (_shaderType != SHADER_NORMAL)
- {
- Texture2D::TexParams texParams = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
- alphaTexture->setTexParameters(texParams);
- }
-
- state->setUniformTexture("alphaTexture", alphaTexture);
- }
- }
-
- state->setUniformFloat("color_power", _colorPower);
-
- if (_shaderType == SHADER_NORMAL)
- {
- ClightSprite* lightSprite_dft=new ClightSprite();
- lightSprite_dft->autorelease();
- lightSprite_dft->init("unknown.png");
- this->setLightSprite(lightSprite_dft);
- lightSprite_dft->setPosition(Vec2(-1000,-1000));
- }
-
- setGLProgramState(state);
-
- }
- }
- void ZMLParticleSystemQuad::SetAlphaTexture(Texture2D* tex)
- {
- if (_maskName.length() > 0)
- {
- if (_shaderType != SHADER_NORMAL)
- {
- Texture2D::TexParams texParams = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
- tex->setTexParameters(texParams);
- }
- auto state = getGLProgramState();
- state->setUniformTexture("alphaTexture", tex);
- }
- }
- // pointRect should be in Texture coordinates, not pixel coordinates
- void ZMLParticleSystemQuad::initTexCoordsWithRect(const Rect& pointRect, bool isrotaed, Vec2 offset, Vec2 originalSize)
- {
- // convert to Tex coords
-
- Rect rect = Rect(
- pointRect.origin.x * CC_CONTENT_SCALE_FACTOR(),
- pointRect.origin.y * CC_CONTENT_SCALE_FACTOR(),
- pointRect.size.width * CC_CONTENT_SCALE_FACTOR(),
- pointRect.size.height * CC_CONTENT_SCALE_FACTOR());
- GLfloat wide = (GLfloat) rect.size.width;
- GLfloat high = (GLfloat) rect.size.height;
-
- float rw = rect.size.width;
- float rh = rect.size.height;
-
- if (isrotaed)
- std::swap(rw, rh);
-
- if (_texture)
- {
- wide = (GLfloat)_texture->getPixelsWide();
- high = (GLfloat)_texture->getPixelsHigh();
- }
- #if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
- GLfloat left = (rect.origin.x*2+1) / (wide*2);
- GLfloat bottom = (rect.origin.y*2+1) / (high*2);
- GLfloat right = left + (rw*2-2) / (wide*2);
- GLfloat top = bottom + (rh*2-2) / (high*2);
- #else
- GLfloat left = rect.origin.x / wide;
- GLfloat bottom = rect.origin.y / high;
- GLfloat right = left + rw / wide;
- GLfloat top = bottom + rh / high;
- #endif // ! CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
- // Important. Texture in cocos2d are inverted, so the Y component should be inverted
- std::swap(top, bottom);
-
- CCASSERT(_texNumber < 100, "纹理数量超过上限");
- if (isrotaed)
- {
- _vertexInfo[_texNumber].bl.u = left;
- _vertexInfo[_texNumber].bl.v = top;
- _vertexInfo[_texNumber].br.u = left;
- _vertexInfo[_texNumber].br.v = bottom;
- _vertexInfo[_texNumber].tl.u = right;
- _vertexInfo[_texNumber].tl.v = top;
- _vertexInfo[_texNumber].tr.u = right;
- _vertexInfo[_texNumber].tr.v = bottom;
-
- }
- else
- {
- _vertexInfo[_texNumber].bl.u = left;
- _vertexInfo[_texNumber].bl.v = bottom;
- _vertexInfo[_texNumber].br.u = right;
- _vertexInfo[_texNumber].br.v = bottom;
- _vertexInfo[_texNumber].tl.u = left;
- _vertexInfo[_texNumber].tl.v = top;
- _vertexInfo[_texNumber].tr.u = right;
- _vertexInfo[_texNumber].tr.v = top;
- }
- if (!offset.isZero()){
- // CCLOG("");
- }
- float originalSizeX_2 = 2 / originalSize.x; // 宽的一半的倒数
- float originalSizeY_2 = 2 / originalSize.y;
- float x1 = (pointRect.size.width * 0.5 - offset.x) * originalSizeX_2;
- float x2 = (pointRect.size.width * 0.5 + offset.x) * originalSizeX_2;
-
- float y1 = (pointRect.size.height * 0.5 - offset.y) * originalSizeY_2;
- float y2 = (pointRect.size.height * 0.5 + offset.y) * originalSizeY_2;
- _vertexInfo[_texNumber].TexturePercentage.set(x1, x2, y1, y2);
-
- _texNumber++;
-
- }
- void ZMLParticleSystemQuad::autoUpdateTexture()
- {
- Texture2D* mainTex = nullptr;
- if (_mainTexName.length() > 0)
- {
- #if PARTICLE_EDITOR_MODE
- mainTex = Director::getInstance()->getTextureCache()->addImage("img/" + _mainTexName);
- #else
- mainTex = Director::getInstance()->getTextureCache()->addImage(_mainTexName);
- #endif
- if (FileUtils::getInstance()->fullPathForFilename(_mainTexName).size() == 0){//判断粒子图片是否在大图中
- SpriteFrame* frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(_mainTexName);
- this->addSpriteFrame(frame);
- } else {
- setTexture(mainTex);
- }
- }
- }
- void ZMLParticleSystemQuad::updateTexCoords()
- {
- if (_texture)
- {
- // 走小图
- const Size& s = _texture->getContentSize();
- // bool isrotaed, Vec2 offset, Vec2 originalSize
- initTexCoordsWithRect(Rect(0, 0, s.width, s.height),false,Vec2(0,0),Vec2(s.width, s.height));
- }
- }
- void ZMLParticleSystemQuad::setTextureWithRect(Texture2D *texture, const Rect& rect, bool isrotaed, Vec2 offset, Vec2 originalSize)
- {
- // Only update the texture if is different from the current one
- if( !_texture)
- {
- ParticleSystem::setTexture(texture);
- } else {
- // || texture->getName() != _texture->getName()
- CCASSERT(texture->getName() == _texture->getName(), "不能瞎几把换纹理");
- }
- this->initTexCoordsWithRect(rect,isrotaed,offset,originalSize);
- }
- void ZMLParticleSystemQuad::setTexture(Texture2D* texture)
- {
- const Size& s = texture->getContentSize();
- this->setTextureWithRect(texture, Rect(0, 0, s.width, s.height),false,Vec2(0,0),Vec2(s.width, s.height));
- }
- void ZMLParticleSystemQuad::addSpriteFrame(SpriteFrame *spriteFrame){
-
- bool isrotaed = spriteFrame->isRotated();
- Vec2 offset = spriteFrame->getOffset();
- Vec2 originalSize = spriteFrame->getOriginalSize();
- auto pointRect = spriteFrame->getRect();
- this->setTextureWithRect(spriteFrame->getTexture(), pointRect,isrotaed,offset,originalSize);
- }
- void ZMLParticleSystemQuad::addSpriteFrame(const std::string &spriteFrameName){
- CCASSERT(!spriteFrameName.empty(), "spriteFrameName must not be empty");
- if (spriteFrameName.empty())
- {
- return;
- }
-
- SpriteFrameCache *cache = SpriteFrameCache::getInstance();
- SpriteFrame *spriteFrame = cache->getSpriteFrameByName(spriteFrameName);
-
-
- if (CocosConfig::getAutoAddSingleImage2Cache()) {
- if (spriteFrame == nullptr)
- {
- Texture2D * texture = Director::getInstance()->getTextureCache()->addImage(spriteFrameName.c_str());
-
- if(texture != nullptr)
- {
-
- Rect bounds = Rect(0, 0, texture->getContentSize().width, texture->getContentSize().height);
-
- spriteFrame = SpriteFrame::createWithTexture(texture, bounds);
-
- cache->addSpriteFrame(spriteFrame, spriteFrameName.c_str());
- }
- }
- }
-
- CCASSERT(spriteFrame, std::string("Invalid spriteFrameName :").append(spriteFrameName).c_str());
-
- addSpriteFrame(spriteFrame);
- }
- void ZMLParticleSystemQuad::initIndices()
- {
- for(int i = 0; i < _totalParticles; ++i)
- {
- const unsigned int i6 = i*6;
- const unsigned int i4 = i*4;
- _indices[i6+0] = (GLushort) i4+0;
- _indices[i6+1] = (GLushort) i4+1;
- _indices[i6+2] = (GLushort) i4+2;
- _indices[i6+5] = (GLushort) i4+1;
- _indices[i6+4] = (GLushort) i4+2;
- _indices[i6+3] = (GLushort) i4+3;
- }
- }
- inline void updatePosWithParticle(V3F_C4B_T2F_Quad *quad, const Vec3& newPosition, float rotation, float sizeWidth, float sizeHeigh, Vec4 texturePercentage)
- {
- // vertices
- GLfloat sizeWidth_2 = sizeWidth/2;
- GLfloat sizeHeigh_2 = sizeHeigh/2;
- GLfloat x1 = -sizeWidth_2 * texturePercentage.x;
- GLfloat y1 = -sizeHeigh_2 * texturePercentage.z;
- GLfloat x2 = sizeWidth_2 * texturePercentage.y;
- GLfloat y2 = sizeHeigh_2 * texturePercentage.w;
-
- GLfloat x = newPosition.x;
- GLfloat y = newPosition.y;
- GLfloat z = newPosition.z;
- GLfloat r = (GLfloat)-CC_DEGREES_TO_RADIANS(rotation);
- GLfloat cr = cosf(r);
- GLfloat sr = sinf(r);
- GLfloat ax = x1 * cr - y1 * sr + x;
- GLfloat ay = x1 * sr + y1 * cr + y;
- GLfloat bx = x2 * cr - y1 * sr + x;
- GLfloat by = x2 * sr + y1 * cr + y;
- GLfloat cx = x2 * cr - y2 * sr + x;
- GLfloat cy = x2 * sr + y2 * cr + y;
- GLfloat dx = x1 * cr - y2 * sr + x;
- GLfloat dy = x1 * sr + y2 * cr + y;
-
- // bottom-left
- quad->bl.vertices.x = ax;
- quad->bl.vertices.y = ay;
- quad->bl.vertices.z = z;
-
- // bottom-right vertex:
- quad->br.vertices.x = bx;
- quad->br.vertices.y = by;
- quad->br.vertices.z = z;
-
- // top-left vertex:
- quad->tl.vertices.x = dx;
- quad->tl.vertices.y = dy;
- quad->tl.vertices.z = z;
-
- // top-right vertex:
- quad->tr.vertices.x = cx;
- quad->tr.vertices.y = cy;
- quad->tr.vertices.z = z;
- }
- void ZMLParticleSystemQuad::updateParticleQuads()
- {
- if (_particleCount <= 0) {
- return;
- }
-
- Vec2 currentPosition;
- if (_positionType == PositionType::FREE)
- {
- currentPosition = this->convertToWorldSpace(Vec2::ZERO);
- }
- else if (_positionType == PositionType::RELATIVE)
- {
- currentPosition = _position;
- }
-
- V3F_C4B_T2F_Quad *startQuad;
- Vec2 pos = Vec2::ZERO;
- if (_batchNode)
- {
- V3F_C4B_T2F_Quad *batchQuads = _batchNode->getTextureAtlas()->getQuads();
- startQuad = &(batchQuads[_atlasIndex]);
- pos = _position;
- }
- else
- {
- startQuad = &(_quads[0]);
- }
-
- if( _positionType == PositionType::FREE )
- {
- Vec3 p1(currentPosition.x, currentPosition.y, 0);
- Mat4 worldToNodeTM = getWorldToNodeTransform();
- worldToNodeTM.transformPoint(&p1);
- Vec3 p2;
- Vec2 newPos;
- Vec3 newPos2;
- float* startX = _particleData.startPosX;
- float* startY = _particleData.startPosY;
- float* x = _particleData.posx;
- float* y = _particleData.posy;
- float* z = _particleDataExpansion.posz;
- int* texIndex = _particleDataExpansion.textureIndex;
- float* r = _particleData.rotation;
- float* sw = _particleDataExpansion.sizeWidth;
- float* sh = _particleDataExpansion.sizeHeight;
- V3F_C4B_T2F_Quad* quadStart = startQuad;
- for (int i = 0 ; i < _particleCount; ++i, ++startX, ++startY, ++x, ++y, ++z, ++quadStart, ++sw, ++sh, ++r, ++texIndex)
- {
- p2.set(*startX, *startY, 0);
- worldToNodeTM.transformPoint(&p2);
- newPos.set(*x,*y);
- p2 = p1 - p2;
- newPos.x -= p2.x - pos.x;
- newPos.y -= p2.y - pos.y;
- newPos2.set(newPos.x, newPos.y, *z);
- int index = *texIndex;
- Vec4 texturePercentage = _vertexInfo[index].TexturePercentage;
- updatePosWithParticle(quadStart, newPos2, *r, *sw, *sh, texturePercentage);
- }
- }
- else if( _positionType == PositionType::RELATIVE )
- {
- Vec2 newPos;
- Vec3 newPos2;
- float* startX = _particleData.startPosX;
- float* startY = _particleData.startPosY;
- float* x = _particleData.posx;
- float* y = _particleData.posy;
- float* z = _particleDataExpansion.posz;
- int* texIndex = _particleDataExpansion.textureIndex;
- float* r = _particleData.rotation;
- float* sw = _particleDataExpansion.sizeWidth;
- float* sh = _particleDataExpansion.sizeHeight;
- V3F_C4B_T2F_Quad* quadStart = startQuad;
- for (int i = 0 ; i < _particleCount; ++i, ++startX, ++startY, ++x, ++y, ++z, ++quadStart, ++sw, ++sh, ++r, ++texIndex)
- {
- newPos.set(*x, *y);
- newPos.x = *x - (currentPosition.x - *startX);
- newPos.y = *y - (currentPosition.y - *startY);
- newPos += pos;
- newPos2.set(newPos.x, newPos.y, *z);
- int index = *texIndex;
- Vec4 texturePercentage = _vertexInfo[index].TexturePercentage;
- updatePosWithParticle(quadStart, newPos2, *r, *sw, *sh, texturePercentage);
- }
- }
- else
- {
- Vec2 newPos;
- Vec3 newPos2;
- float* startX = _particleData.startPosX;
- float* startY = _particleData.startPosY;
- float* x = _particleData.posx;
- float* y = _particleData.posy;
- float* z = _particleDataExpansion.posz;
- int* texIndex = _particleDataExpansion.textureIndex;
- float* r = _particleData.rotation;
- float* sw = _particleDataExpansion.sizeWidth;
- float* sh = _particleDataExpansion.sizeHeight;
- V3F_C4B_T2F_Quad* quadStart = startQuad;
- for (int i = 0 ; i < _particleCount; ++i, ++startX, ++startY, ++x, ++y, ++z, ++quadStart, ++sw, ++sh, ++r, ++texIndex)
- {
- newPos.set(*x + pos.x, *y + pos.y);
- newPos2.set(newPos.x, newPos.y, *z);
- int index = *texIndex;
- Vec4 texturePercentage = _vertexInfo[index].TexturePercentage;
- updatePosWithParticle(quadStart, newPos2, *r, *sw, *sh, texturePercentage);
- }
- }
-
- //set color
- if(_opacityModifyRGB)
- {
- V3F_C4B_T2F_Quad* quad = startQuad;
- float* r = _particleData.colorR;
- float* g = _particleData.colorG;
- float* b = _particleData.colorB;
- float* a = _particleData.colorA;
-
- for (int i = 0; i < _particleCount; ++i,++quad,++r,++g,++b,++a)
- {
- GLubyte colorR = *r * *a * 255 * _displayedColor.r / 255 * _displayedOpacity / 255;
- GLubyte colorG = *g * *a * 255 * _displayedColor.g / 255 * _displayedOpacity / 255;
- GLubyte colorB = *b * *a * 255 * _displayedColor.b / 255 * _displayedOpacity / 255;
- GLubyte colorA = *a * 255 * _displayedOpacity / 255;
- quad->bl.colors.set(colorR, colorG, colorB, colorA);
- quad->br.colors.set(colorR, colorG, colorB, colorA);
- quad->tl.colors.set(colorR, colorG, colorB, colorA);
- quad->tr.colors.set(colorR, colorG, colorB, colorA);
- }
- }
- else
- {
- V3F_C4B_T2F_Quad* quad = startQuad;
- float* r = _particleData.colorR;
- float* g = _particleData.colorG;
- float* b = _particleData.colorB;
- float* a = _particleData.colorA;
-
- for (int i = 0; i < _particleCount; ++i,++quad,++r,++g,++b,++a)
- {
- GLubyte colorR = *r * 255 * _displayedColor.r / 255 * _displayedOpacity / 255;
- GLubyte colorG = *g * 255 * _displayedColor.g / 255 * _displayedOpacity / 255;
- GLubyte colorB = *b * 255 * _displayedColor.b / 255 * _displayedOpacity / 255;
- GLubyte colorA = *a * 255 * _displayedOpacity / 255;
- quad->bl.colors.set(colorR, colorG, colorB, colorA);
- quad->br.colors.set(colorR, colorG, colorB, colorA);
- quad->tl.colors.set(colorR, colorG, colorB, colorA);
- quad->tr.colors.set(colorR, colorG, colorB, colorA);
- }
- }
-
- {
- //设置顶点
- V3F_C4B_T2F_Quad* quad = startQuad;
- int* texIndex = _particleDataExpansion.textureIndex;
- for (int i = 0; i < _particleCount; ++i, ++quad, ++texIndex) {
- int index = *texIndex;
- quad->bl.texCoords.u = _vertexInfo[index].bl.u;
- quad->bl.texCoords.v = _vertexInfo[index].bl.v;
- // bottom-right vertex:
- quad->br.texCoords.u = _vertexInfo[index].br.u;
- quad->br.texCoords.v = _vertexInfo[index].br.v;
- // top-left vertex:
- quad->tl.texCoords.u = _vertexInfo[index].tl.u;
- quad->tl.texCoords.v = _vertexInfo[index].tl.v;
- // top-right vertex:
- quad->tr.texCoords.u = _vertexInfo[index].tr.u;
- quad->tr.texCoords.v = _vertexInfo[index].tr.v;
- }
- }
- }
- void ZMLParticleSystemQuad::postStep()
- {
- glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
-
- // Option 1: Sub Data
- glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(_quads[0])*_totalParticles, _quads);
-
- // Option 2: Data
- // glBufferData(GL_ARRAY_BUFFER, sizeof(quads_[0]) * particleCount, quads_, GL_DYNAMIC_DRAW);
-
- // Option 3: Orphaning + glMapBuffer
- // glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0])*_totalParticles, nullptr, GL_STREAM_DRAW);
- // void *buf = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
- // memcpy(buf, _quads, sizeof(_quads[0])*_totalParticles);
- // glUnmapBuffer(GL_ARRAY_BUFFER);
-
- glBindBuffer(GL_ARRAY_BUFFER, 0);
-
- CHECK_GL_ERROR_DEBUG();
- }
- void ZMLParticleSystemQuad::onPassUnifoAndBindTex(const Mat4 &transform, uint32_t flags){
-
- // assert(getGLProgram()==m_program);
- //----prepare uniform values
- Vec4 diffuseM_tmp={m_material.m_diffuse.r,m_material.m_diffuse.g,
- m_material.m_diffuse.b,m_material.m_diffuse.a};
- Vec4 ambientM_tmp = {m_material.m_ambient.r,m_material.m_ambient.g,m_material.m_ambient.b,m_material.m_ambient.a};
- Vec4 diffuseL_tmp={m_lightSprite->getDiffuse().r,m_lightSprite->getDiffuse().g,
- m_lightSprite->getDiffuse().b,m_lightSprite->getDiffuse().a};
- Vec4 ambientL_tmp={m_lightSprite->getAmbient().r,m_lightSprite->getAmbient().g,
- m_lightSprite->getAmbient().b,m_lightSprite->getAmbient().a};
-
- TextureSheetAnimationModule * module = dynamic_cast<TextureSheetAnimationModule*>(getModule(ParticleSystemModuleFlag::TEXTURE_SHEET));
- Vec2 contentSize_tmp = Vec2::ONE;
- if (module != nullptr)
- {
- contentSize_tmp = {module->getTileWidth(),module->getTileHeight()};
- }else {
- contentSize_tmp={getTexture()->getContentSizeInPixels().width,getTexture()->getContentSizeInPixels().height};
- }
- this->getNodeToParentTransform();//in order this->parentToNodeTransform() got right result, we must call this->getNodeToParentTransform() first, this is a bug of cocos2dx 3.3,see:http://www.cnblogs.com/wantnon/p/4330226.html
- Point lightPosInLocalSpace= PointApplyAffineTransform(m_lightSprite->getPosition(),this->getParentToNodeAffineTransform());
- Vec4 lightPosInLocalSpace_tmp = {lightPosInLocalSpace.x,lightPosInLocalSpace.y,m_lightSprite->getZ(),1};
-
- getGLProgramState()->setUniformVec4("u_diffuseM", diffuseM_tmp);//("u_diffuseM", 4, diffuseM_tmp);
- getGLProgramState()->setUniformVec4("u_ambientM", ambientM_tmp);
- getGLProgramState()->setUniformVec4("u_diffuseL", diffuseL_tmp);
- getGLProgramState()->setUniformVec4("u_ambientL", ambientL_tmp);
- getGLProgramState()->setUniformVec2("u_contentSize", contentSize_tmp);
-
- getGLProgramState()->setUniformVec4("u_lightPosInLocalSpace",lightPosInLocalSpace_tmp);
-
- getGLProgramState()->setUniformFloat("color_power", _colorPower);
- // getGLProgramState()->setUniformFloat("u_kBump", _colorPower); // 1 - 4
- }
- // overriding draw method
- void ZMLParticleSystemQuad::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
- {
- //quad command
- if(_particleCount > 0)
- {
- if (_shaderType == SHADER_NORMAL)
- {
- //passUnifo and bindTex
- _passUnifoAndBindTexCommand.init(_globalZOrder);
- _passUnifoAndBindTexCommand.func = CC_CALLBACK_0(ZMLParticleSystemQuad::onPassUnifoAndBindTex, this,transform,flags);
- Director::getInstance()->getRenderer()->addCommand(&_passUnifoAndBindTexCommand);
- }
- _quadCommand.init(_globalZOrder, _texture, getGLProgramState(), _blendFunc, _quads, _particleCount, transform, flags);
- renderer->addCommand(&_quadCommand);
- }
- }
- void ZMLParticleSystemQuad::setTotalParticles(int tp)
- {
- // If we are setting the total number of particles to a number higher
- // than what is allocated, we need to allocate new arrays
- if( tp > _allocatedParticles )
- {
- // Allocate new memory
- size_t quadsSize = sizeof(_quads[0]) * tp * 1;
- size_t indicesSize = sizeof(_indices[0]) * tp * 6 * 1;
- _particleData.release();
- if (!_particleData.init(tp))
- {
- CCLOG("Particle system: not enough memory");
- return;
- }
-
- _particleDataExpansion.release();
- if( !_particleDataExpansion.init(tp))
- {
- CCLOG("Particle system: not enough memory");
- return ;
- }
-
- V3F_C4B_T2F_Quad* quadsNew = (V3F_C4B_T2F_Quad*)realloc(_quads, quadsSize);
- GLushort* indicesNew = (GLushort*)realloc(_indices, indicesSize);
- if (quadsNew && indicesNew)
- {
- // Assign pointers
- _quads = quadsNew;
- _indices = indicesNew;
- // Clear the memory
- memset(_quads, 0, quadsSize);
- memset(_indices, 0, indicesSize);
-
- _allocatedParticles = tp;
- }
- else
- {
- // Out of memory, failed to resize some array
- if (quadsNew) _quads = quadsNew;
- if (indicesNew) _indices = indicesNew;
- CCLOG("Particle system: out of memory");
- return;
- }
- _totalParticles = tp;
- // Init particles
- if (_batchNode)
- {
- for (int i = 0; i < _totalParticles; i++)
- {
- _particleData.atlasIndex[i] = i;
- }
- }
- initIndices();
- if (Configuration::getInstance()->supportsShareableVAO())
- {
- setupVBOandVAO();
- }
- else
- {
- setupVBO();
- }
-
- // fixed http://www.cocos2d-x.org/issues/3990
- // Updates texture coords.
- updateTexCoords();
- }
- else
- {
- _totalParticles = tp;
- }
-
- // fixed issue #5762
- // reset the emission rate
- if(CocosConfig::isCCBParticleUseEmitrate() == false){
- setEmissionRate(_totalParticles / _life);
- }
-
- resetSystem();
- }
- void ZMLParticleSystemQuad::setupVBOandVAO()
- {
- // clean VAO
- glDeleteBuffers(2, &_buffersVBO[0]);
- glDeleteVertexArrays(1, &_VAOname);
- GL::bindVAO(0);
-
- glGenVertexArrays(1, &_VAOname);
- GL::bindVAO(_VAOname);
- #define kQuadSize sizeof(_quads[0].bl)
- glGenBuffers(2, &_buffersVBO[0]);
- glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
- glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _totalParticles, _quads, GL_DYNAMIC_DRAW);
- // vertices
- glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
- glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, vertices));
- // colors
- glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
- glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, colors));
- // tex coords
- glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
- glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, texCoords));
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _totalParticles * 6, _indices, GL_STATIC_DRAW);
- // Must unbind the VAO before changing the element buffer.
- GL::bindVAO(0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- CHECK_GL_ERROR_DEBUG();
- }
- void ZMLParticleSystemQuad::setupVBO()
- {
- glDeleteBuffers(2, &_buffersVBO[0]);
-
- glGenBuffers(2, &_buffersVBO[0]);
- glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
- glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _totalParticles, _quads, GL_DYNAMIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _totalParticles * 6, _indices, GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- CHECK_GL_ERROR_DEBUG();
- }
- void ZMLParticleSystemQuad::listenRendererRecreated(EventCustom* /*event*/)
- {
- //when comes to foreground in android, _buffersVBO and _VAOname is a wild handle
- //before recreating, we need to reset them to 0
- memset(_buffersVBO, 0, sizeof(_buffersVBO));
- if (Configuration::getInstance()->supportsShareableVAO())
- {
- _VAOname = 0;
- setupVBOandVAO();
- }
- else
- {
- setupVBO();
- }
- }
- bool ZMLParticleSystemQuad::allocMemory()
- {
- CCASSERT( !_batchNode, "Memory should not be alloced when not using batchNode");
- CC_SAFE_FREE(_quads);
- CC_SAFE_FREE(_indices);
- _quads = (V3F_C4B_T2F_Quad*)malloc(_totalParticles * sizeof(V3F_C4B_T2F_Quad));
- _indices = (GLushort*)malloc(_totalParticles * 6 * sizeof(GLushort));
-
- if( !_quads || !_indices)
- {
- CCLOG("cocos2d: Particle system: not enough memory");
- CC_SAFE_FREE(_quads);
- CC_SAFE_FREE(_indices);
- return false;
- }
- memset(_quads, 0, _totalParticles * sizeof(V3F_C4B_T2F_Quad));
- memset(_indices, 0, _totalParticles * 6 * sizeof(GLushort));
- return true;
- }
- void ZMLParticleSystemQuad::setBatchNode(ParticleBatchNode * batchNode)
- {
- if( _batchNode != batchNode )
- {
- ParticleBatchNode* oldBatch = _batchNode;
- ParticleSystem::setBatchNode(batchNode);
- // NEW: is self render ?
- if( ! batchNode )
- {
- allocMemory();
- initIndices();
- setTexture(oldBatch->getTexture());
- if (Configuration::getInstance()->supportsShareableVAO())
- {
- setupVBOandVAO();
- }
- else
- {
- setupVBO();
- }
- }
- // OLD: was it self render ? cleanup
- else if( !oldBatch )
- {
- // copy current state to batch
- V3F_C4B_T2F_Quad *batchQuads = _batchNode->getTextureAtlas()->getQuads();
- V3F_C4B_T2F_Quad *quad = &(batchQuads[_atlasIndex] );
- memcpy( quad, _quads, _totalParticles * sizeof(_quads[0]) );
- CC_SAFE_FREE(_quads);
- CC_SAFE_FREE(_indices);
- glDeleteBuffers(2, &_buffersVBO[0]);
- memset(_buffersVBO, 0, sizeof(_buffersVBO));
- if (Configuration::getInstance()->supportsShareableVAO())
- {
- glDeleteVertexArrays(1, &_VAOname);
- GL::bindVAO(0);
- _VAOname = 0;
- }
- }
- }
- }
- ZMLParticleSystemQuad * ZMLParticleSystemQuad::create() {
- ZMLParticleSystemQuad *zmlParticleSystemQuad = new (std::nothrow) ZMLParticleSystemQuad();
- if (zmlParticleSystemQuad && zmlParticleSystemQuad->init())
- {
- zmlParticleSystemQuad->autorelease();
- return zmlParticleSystemQuad;
- }
- CC_SAFE_DELETE(zmlParticleSystemQuad);
- return nullptr;
- }
- void ZMLParticleSystemQuad::onExit()
- {
- if (_cbOnExit != nullptr && _plName.size() > 0) {
-
- _particleCount = 0;
- _cbOnExit(_plName, this);
- }
- ZMLParticleSystem::onExit();
- }
- void ZMLParticleSystemQuad::setCbOnExit(std::function<void(const std::string&, ZMLParticleSystem*)> cb) {
- _cbOnExit = cb;
- }
- std::string ZMLParticleSystemQuad::getDescription() const
- {
- return StringUtils::format("<ZMLParticleSystemQuad | Tag = %d, Total Particles = %d>", _tag, _totalParticles);
- }
- NS_RU_END
|