2011-03-19 10:07:16 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2012-06-08 14:11:48 +08:00
|
|
|
Copyright (c) 2009 Leonardo Kasperavičius
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-07-04 19:20:16 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2011-03-19 10:07:16 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-08-28 17:03:29 +08:00
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCParticleSystemQuad.h"
|
2014-11-17 01:29:05 +08:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2015-01-13 12:06:50 +08:00
|
|
|
#include "2d/CCCamera.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCSpriteFrame.h"
|
2014-05-10 09:39:25 +08:00
|
|
|
#include "2d/CCParticleBatchNode.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTextureAtlas.h"
|
2014-08-28 17:03:29 +08:00
|
|
|
#include "renderer/ccGLStateCache.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
2014-05-10 09:39:25 +08:00
|
|
|
#include "base/CCDirector.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCEventType.h"
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "base/CCConfiguration.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCEventListenerCustom.h"
|
|
|
|
#include "base/CCEventDispatcher.h"
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2014-08-28 17:03:29 +08:00
|
|
|
#include "deprecated/CCString.h"
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
2010-12-28 11:55:34 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ParticleSystemQuad::ParticleSystemQuad()
|
2013-12-18 17:47:20 +08:00
|
|
|
:_quads(nullptr)
|
|
|
|
,_indices(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
,_VAOname(0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
memset(_buffersVBO, 0, sizeof(_buffersVBO));
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ParticleSystemQuad::~ParticleSystemQuad()
|
2010-12-28 11:55:34 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
if (nullptr == _batchNode)
|
2012-04-13 11:40:56 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_FREE(_quads);
|
|
|
|
CC_SAFE_FREE(_indices);
|
|
|
|
glDeleteBuffers(2, &_buffersVBO[0]);
|
2013-11-13 14:52:16 +08:00
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
2013-11-12 20:18:04 +08:00
|
|
|
glDeleteVertexArrays(1, &_VAOname);
|
|
|
|
GL::bindVAO(0);
|
|
|
|
}
|
2012-04-13 11:40:56 +08:00
|
|
|
}
|
2010-12-28 11:55:34 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation ParticleSystemQuad
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-11-15 08:21:49 +08:00
|
|
|
ParticleSystemQuad * ParticleSystemQuad::create(const std::string& filename)
|
2011-06-14 16:27:47 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
ParticleSystemQuad *ret = new (std::nothrow) ParticleSystemQuad();
|
2013-11-15 08:21:49 +08:00
|
|
|
if (ret && ret->initWithFile(filename))
|
2011-06-14 16:27:47 +08:00
|
|
|
{
|
2013-11-15 08:21:49 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2011-06-14 16:27:47 +08:00
|
|
|
}
|
2013-11-15 08:21:49 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return ret;
|
2011-06-14 16:27:47 +08:00
|
|
|
}
|
|
|
|
|
2013-11-15 08:21:49 +08:00
|
|
|
ParticleSystemQuad * ParticleSystemQuad::createWithTotalParticles(int numberOfParticles) {
|
2014-08-28 07:31:57 +08:00
|
|
|
ParticleSystemQuad *ret = new (std::nothrow) ParticleSystemQuad();
|
2013-11-15 08:21:49 +08:00
|
|
|
if (ret && ret->initWithTotalParticles(numberOfParticles))
|
2012-09-21 08:24:18 +08:00
|
|
|
{
|
2013-11-15 08:21:49 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-09-21 08:24:18 +08:00
|
|
|
}
|
2013-11-15 08:21:49 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return ret;
|
2012-09-21 08:24:18 +08:00
|
|
|
}
|
|
|
|
|
2014-05-06 06:50:08 +08:00
|
|
|
ParticleSystemQuad * ParticleSystemQuad::create(ValueMap &dictionary)
|
|
|
|
{
|
|
|
|
ParticleSystemQuad *ret = new (std::nothrow) ParticleSystemQuad();
|
|
|
|
if (ret && ret->initWithDictionary(dictionary))
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//implementation ParticleSystemQuad
|
|
|
|
// overriding the init method
|
|
|
|
bool ParticleSystemQuad::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();
|
|
|
|
}
|
|
|
|
|
2014-05-13 10:12:56 +08:00
|
|
|
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));
|
2014-05-06 06:50:08 +08:00
|
|
|
|
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
// Need to listen the event only when not use batchnode, because it will use VBO
|
2014-07-09 23:03:04 +08:00
|
|
|
auto listener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, CC_CALLBACK_1(ParticleSystemQuad::listenRendererRecreated, this));
|
2014-05-06 06:50:08 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-21 08:24:18 +08:00
|
|
|
|
2011-07-04 19:20:16 +08:00
|
|
|
// pointRect should be in Texture coordinates, not pixel coordinates
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystemQuad::initTexCoordsWithRect(const Rect& pointRect)
|
2010-12-28 11:55:34 +08:00
|
|
|
{
|
2012-03-14 14:55:17 +08:00
|
|
|
// convert to Tex coords
|
2010-12-28 11:55:34 +08:00
|
|
|
|
2013-07-12 14:30:26 +08:00
|
|
|
Rect rect = Rect(
|
2010-12-28 11:55:34 +08:00
|
|
|
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());
|
|
|
|
|
2011-01-05 17:01:08 +08:00
|
|
|
GLfloat wide = (GLfloat) pointRect.size.width;
|
|
|
|
GLfloat high = (GLfloat) pointRect.size.height;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_texture)
|
2011-03-19 10:07:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
wide = (GLfloat)_texture->getPixelsWide();
|
|
|
|
high = (GLfloat)_texture->getPixelsHigh();
|
2011-01-05 17:01:08 +08:00
|
|
|
}
|
2010-12-28 11:55:34 +08:00
|
|
|
|
2011-03-19 10:07:16 +08:00
|
|
|
#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 + (rect.size.width*2-2) / (wide*2);
|
|
|
|
GLfloat top = bottom + (rect.size.height*2-2) / (high*2);
|
|
|
|
#else
|
|
|
|
GLfloat left = rect.origin.x / wide;
|
|
|
|
GLfloat bottom = rect.origin.y / high;
|
|
|
|
GLfloat right = left + rect.size.width / wide;
|
|
|
|
GLfloat top = bottom + rect.size.height / high;
|
|
|
|
#endif // ! CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
|
2010-12-28 11:55:34 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Important. Texture in cocos2d are inverted, so the Y component should be inverted
|
2014-11-17 01:29:05 +08:00
|
|
|
std::swap(top, bottom);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
V3F_C4B_T2F_Quad *quads = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
unsigned int start = 0, end = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_batchNode)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
quads = _batchNode->getTextureAtlas()->getQuads();
|
|
|
|
start = _atlasIndex;
|
|
|
|
end = _atlasIndex + _totalParticles;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
quads = _quads;
|
2012-04-19 14:35:52 +08:00
|
|
|
start = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
end = _totalParticles;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for(unsigned int i=start; i<end; i++)
|
|
|
|
{
|
|
|
|
// bottom-left vertex:
|
|
|
|
quads[i].bl.texCoords.u = left;
|
|
|
|
quads[i].bl.texCoords.v = bottom;
|
|
|
|
// bottom-right vertex:
|
|
|
|
quads[i].br.texCoords.u = right;
|
|
|
|
quads[i].br.texCoords.v = bottom;
|
|
|
|
// top-left vertex:
|
|
|
|
quads[i].tl.texCoords.u = left;
|
|
|
|
quads[i].tl.texCoords.v = top;
|
|
|
|
// top-right vertex:
|
|
|
|
quads[i].tr.texCoords.u = right;
|
|
|
|
quads[i].tr.texCoords.v = top;
|
|
|
|
}
|
2010-12-28 11:55:34 +08:00
|
|
|
}
|
2014-02-13 10:33:57 +08:00
|
|
|
|
|
|
|
void ParticleSystemQuad::updateTexCoords()
|
|
|
|
{
|
|
|
|
if (_texture)
|
|
|
|
{
|
|
|
|
const Size& s = _texture->getContentSize();
|
|
|
|
initTexCoordsWithRect(Rect(0, 0, s.width, s.height));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystemQuad::setTextureWithRect(Texture2D *texture, const Rect& rect)
|
2010-12-28 11:55:34 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// Only update the texture if is different from the current one
|
2013-06-15 14:03:30 +08:00
|
|
|
if( !_texture || texture->getName() != _texture->getName() )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
ParticleSystem::setTexture(texture);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-12-28 11:55:34 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->initTexCoordsWithRect(rect);
|
2010-12-28 11:55:34 +08:00
|
|
|
}
|
2014-02-13 10:33:57 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystemQuad::setTexture(Texture2D* texture)
|
2010-12-28 11:55:34 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
const Size& s = texture->getContentSize();
|
2013-07-12 14:30:26 +08:00
|
|
|
this->setTextureWithRect(texture, Rect(0, 0, s.width, s.height));
|
2010-12-28 11:55:34 +08:00
|
|
|
}
|
2014-02-13 10:33:57 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystemQuad::setDisplayFrame(SpriteFrame *spriteFrame)
|
2010-12-28 11:55:34 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
CCASSERT(spriteFrame->getOffsetInPixels().equals(Vec2::ZERO),
|
2012-08-01 15:30:12 +08:00
|
|
|
"QuadParticle only supports SpriteFrames with no offsets");
|
2010-12-28 11:55:34 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// update texture before updating texture rect
|
2013-06-15 14:03:30 +08:00
|
|
|
if ( !_texture || spriteFrame->getTexture()->getName() != _texture->getName())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
this->setTexture(spriteFrame->getTexture());
|
|
|
|
}
|
2010-12-28 11:55:34 +08:00
|
|
|
}
|
2012-04-13 17:24:50 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystemQuad::initIndices()
|
2010-12-28 11:55:34 +08:00
|
|
|
{
|
2013-09-08 11:26:38 +08:00
|
|
|
for(int i = 0; i < _totalParticles; ++i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2011-07-04 19:20:16 +08:00
|
|
|
const unsigned int i6 = i*6;
|
|
|
|
const unsigned int i4 = i*4;
|
2013-06-15 14:03:30 +08:00
|
|
|
_indices[i6+0] = (GLushort) i4+0;
|
|
|
|
_indices[i6+1] = (GLushort) i4+1;
|
|
|
|
_indices[i6+2] = (GLushort) i4+2;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_indices[i6+5] = (GLushort) i4+1;
|
|
|
|
_indices[i6+4] = (GLushort) i4+2;
|
|
|
|
_indices[i6+3] = (GLushort) i4+3;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-12-28 11:55:34 +08:00
|
|
|
}
|
2012-04-13 17:24:50 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void ParticleSystemQuad::updateQuadWithParticle(tParticle* particle, const Vec2& newPosition)
|
2010-12-28 11:55:34 +08:00
|
|
|
{
|
2013-07-05 16:49:22 +08:00
|
|
|
V3F_C4B_T2F_Quad *quad;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_batchNode)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-05 16:49:22 +08:00
|
|
|
V3F_C4B_T2F_Quad *batchQuads = _batchNode->getTextureAtlas()->getQuads();
|
2013-06-15 14:03:30 +08:00
|
|
|
quad = &(batchQuads[_atlasIndex+particle->atlasIndex]);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
quad = &(_quads[_particleIdx]);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-07-05 16:49:22 +08:00
|
|
|
Color4B color = (_opacityModifyRGB)
|
|
|
|
? Color4B( particle->color.r*particle->color.a*255, particle->color.g*particle->color.a*255, particle->color.b*particle->color.a*255, particle->color.a*255)
|
|
|
|
: Color4B( particle->color.r*255, particle->color.g*255, particle->color.b*255, particle->color.a*255);
|
2012-06-08 14:11:48 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
quad->bl.colors = color;
|
|
|
|
quad->br.colors = color;
|
|
|
|
quad->tl.colors = color;
|
|
|
|
quad->tr.colors = color;
|
|
|
|
|
|
|
|
// vertices
|
|
|
|
GLfloat size_2 = particle->size/2;
|
2012-04-24 15:02:18 +08:00
|
|
|
if (particle->rotation)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
GLfloat x1 = -size_2;
|
|
|
|
GLfloat y1 = -size_2;
|
|
|
|
|
|
|
|
GLfloat x2 = size_2;
|
|
|
|
GLfloat y2 = size_2;
|
|
|
|
GLfloat x = newPosition.x;
|
|
|
|
GLfloat y = newPosition.y;
|
|
|
|
|
|
|
|
GLfloat r = (GLfloat)-CC_DEGREES_TO_RADIANS(particle->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;
|
|
|
|
|
|
|
|
// bottom-right vertex:
|
|
|
|
quad->br.vertices.x = bx;
|
|
|
|
quad->br.vertices.y = by;
|
|
|
|
|
|
|
|
// top-left vertex:
|
|
|
|
quad->tl.vertices.x = dx;
|
|
|
|
quad->tl.vertices.y = dy;
|
|
|
|
|
|
|
|
// top-right vertex:
|
|
|
|
quad->tr.vertices.x = cx;
|
|
|
|
quad->tr.vertices.y = cy;
|
2012-04-24 15:02:18 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// bottom-left vertex:
|
|
|
|
quad->bl.vertices.x = newPosition.x - size_2;
|
|
|
|
quad->bl.vertices.y = newPosition.y - size_2;
|
|
|
|
|
|
|
|
// bottom-right vertex:
|
|
|
|
quad->br.vertices.x = newPosition.x + size_2;
|
|
|
|
quad->br.vertices.y = newPosition.y - size_2;
|
|
|
|
|
|
|
|
// top-left vertex:
|
|
|
|
quad->tl.vertices.x = newPosition.x - size_2;
|
|
|
|
quad->tl.vertices.y = newPosition.y + size_2;
|
|
|
|
|
|
|
|
// top-right vertex:
|
|
|
|
quad->tr.vertices.x = newPosition.x + size_2;
|
|
|
|
quad->tr.vertices.y = newPosition.y + size_2;
|
|
|
|
}
|
2010-12-28 11:55:34 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystemQuad::postStep()
|
2010-12-28 11:55:34 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
|
2013-11-19 11:17:02 +08:00
|
|
|
|
|
|
|
// Option 1: Sub Data
|
2013-06-15 14:03:30 +08:00
|
|
|
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(_quads[0])*_totalParticles, _quads);
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2013-11-19 11:17:02 +08:00
|
|
|
// Option 2: Data
|
|
|
|
// glBufferData(GL_ARRAY_BUFFER, sizeof(quads_[0]) * particleCount, quads_, GL_DYNAMIC_DRAW);
|
|
|
|
|
|
|
|
// Option 3: Orphaning + glMapBuffer
|
2013-12-18 17:47:20 +08:00
|
|
|
// glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0])*_totalParticles, nullptr, GL_STREAM_DRAW);
|
2013-11-19 11:17:02 +08:00
|
|
|
// 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);
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2013-11-19 11:17:02 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2010-12-28 11:55:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// overriding draw method
|
2014-05-31 07:42:05 +08:00
|
|
|
void ParticleSystemQuad::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
|
2013-12-03 04:13:05 +08:00
|
|
|
{
|
2014-03-27 16:47:21 +08:00
|
|
|
CCASSERT( _particleIdx == 0 || _particleIdx == _particleCount, "Abnormal error in particle quad");
|
2013-12-05 10:20:22 +08:00
|
|
|
//quad command
|
|
|
|
if(_particleIdx > 0)
|
|
|
|
{
|
2015-01-13 12:06:50 +08:00
|
|
|
if (flags & FLAGS_RENDER_AS_3D)
|
|
|
|
{
|
|
|
|
float depth = Camera::getVisitingCamera()->getDepthInView(transform);
|
|
|
|
_quadCommand.init(depth, _texture->getName(), getGLProgramState(), _blendFunc, _quads, _particleIdx, transform);
|
|
|
|
renderer->addCommand(&_quadCommand);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_quadCommand.init(_globalZOrder, _texture->getName(), getGLProgramState(), _blendFunc, _quads, _particleIdx, transform);
|
|
|
|
renderer->addCommand(&_quadCommand);
|
|
|
|
}
|
2013-12-05 10:20:22 +08:00
|
|
|
}
|
2013-12-03 04:13:05 +08:00
|
|
|
}
|
|
|
|
|
2013-09-08 11:26:38 +08:00
|
|
|
void ParticleSystemQuad::setTotalParticles(int tp)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-09-17 15:02:24 +08:00
|
|
|
// If we are setting the total number of particles to a number higher
|
2012-04-19 14:35:52 +08:00
|
|
|
// than what is allocated, we need to allocate new arrays
|
2013-06-15 14:03:30 +08:00
|
|
|
if( tp > _allocatedParticles )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Allocate new memory
|
2013-06-20 14:13:12 +08:00
|
|
|
size_t particlesSize = tp * sizeof(tParticle);
|
2013-06-15 14:03:30 +08:00
|
|
|
size_t quadsSize = sizeof(_quads[0]) * tp * 1;
|
|
|
|
size_t indicesSize = sizeof(_indices[0]) * tp * 6 * 1;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
tParticle* particlesNew = (tParticle*)realloc(_particles, particlesSize);
|
2013-07-05 16:49:22 +08:00
|
|
|
V3F_C4B_T2F_Quad* quadsNew = (V3F_C4B_T2F_Quad*)realloc(_quads, quadsSize);
|
2013-06-15 14:03:30 +08:00
|
|
|
GLushort* indicesNew = (GLushort*)realloc(_indices, indicesSize);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (particlesNew && quadsNew && indicesNew)
|
|
|
|
{
|
|
|
|
// Assign pointers
|
2013-06-15 14:03:30 +08:00
|
|
|
_particles = particlesNew;
|
|
|
|
_quads = quadsNew;
|
|
|
|
_indices = indicesNew;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Clear the memory
|
2013-06-15 14:03:30 +08:00
|
|
|
memset(_particles, 0, particlesSize);
|
|
|
|
memset(_quads, 0, quadsSize);
|
|
|
|
memset(_indices, 0, indicesSize);
|
2014-02-11 18:25:57 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_allocatedParticles = tp;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Out of memory, failed to resize some array
|
2013-06-15 14:03:30 +08:00
|
|
|
if (particlesNew) _particles = particlesNew;
|
|
|
|
if (quadsNew) _quads = quadsNew;
|
|
|
|
if (indicesNew) _indices = indicesNew;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CCLOG("Particle system: out of memory");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalParticles = tp;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Init particles
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_batchNode)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-09-08 11:26:38 +08:00
|
|
|
for (int i = 0; i < _totalParticles; i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_particles[i].atlasIndex=i;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-27 16:24:33 +08:00
|
|
|
initIndices();
|
2013-11-13 14:52:16 +08:00
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
2013-11-12 20:18:04 +08:00
|
|
|
setupVBOandVAO();
|
|
|
|
}
|
2013-11-13 14:52:16 +08:00
|
|
|
else
|
|
|
|
{
|
2013-11-12 20:18:04 +08:00
|
|
|
setupVBO();
|
|
|
|
}
|
2014-02-11 18:25:57 +08:00
|
|
|
|
|
|
|
// fixed http://www.cocos2d-x.org/issues/3990
|
|
|
|
// Updates texture coords.
|
2014-02-13 10:33:57 +08:00
|
|
|
updateTexCoords();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalParticles = tp;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-02-27 16:24:33 +08:00
|
|
|
|
2014-07-29 10:48:20 +08:00
|
|
|
// fixed issue #5762
|
|
|
|
// reset the emission rate
|
|
|
|
setEmissionRate(_totalParticles / _life);
|
|
|
|
|
2013-02-27 16:24:33 +08:00
|
|
|
resetSystem();
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
2011-03-19 10:07:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystemQuad::setupVBOandVAO()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-02-27 16:24:33 +08:00
|
|
|
// clean VAO
|
2013-06-15 14:03:30 +08:00
|
|
|
glDeleteBuffers(2, &_buffersVBO[0]);
|
|
|
|
glDeleteVertexArrays(1, &_VAOname);
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindVAO(0);
|
2013-02-27 16:24:33 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glGenVertexArrays(1, &_VAOname);
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindVAO(_VAOname);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
#define kQuadSize sizeof(_quads[0].bl)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glGenBuffers(2, &_buffersVBO[0]);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _totalParticles, _quads, GL_DYNAMIC_DRAW);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// vertices
|
2013-07-25 17:48:22 +08:00
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, vertices));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// colors
|
2013-07-25 17:48:22 +08:00
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, colors));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// tex coords
|
2014-05-09 03:34:26 +08:00
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, texCoords));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _totalParticles * 6, _indices, GL_STATIC_DRAW);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
// Must unbind the VAO before changing the element buffer.
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindVAO(0);
|
2012-04-19 14:35:52 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystemQuad::setupVBO()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
glDeleteBuffers(2, &_buffersVBO[0]);
|
2013-02-27 16:24:33 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glGenBuffers(2, &_buffersVBO[0]);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _totalParticles, _quads, GL_DYNAMIC_DRAW);
|
2012-04-19 14:35:52 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _totalParticles * 6, _indices, GL_STATIC_DRAW);
|
2012-04-19 14:35:52 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
}
|
|
|
|
|
2014-07-09 23:03:04 +08:00
|
|
|
void ParticleSystemQuad::listenRendererRecreated(EventCustom* event)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2015-01-06 09:44:19 +08:00
|
|
|
//when comes to foreground in android, _buffersVBO and _VAOname is a wild handle
|
|
|
|
//before recreating, we need to reset them to 0
|
2015-01-05 17:53:31 +08:00
|
|
|
memset(_buffersVBO, 0, sizeof(_buffersVBO));
|
2013-11-13 14:52:16 +08:00
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
2015-01-06 09:44:19 +08:00
|
|
|
_VAOname = 0;
|
2012-04-24 15:02:18 +08:00
|
|
|
setupVBOandVAO();
|
2013-11-12 20:18:04 +08:00
|
|
|
}
|
2013-11-13 14:52:16 +08:00
|
|
|
else
|
|
|
|
{
|
2012-04-24 15:02:18 +08:00
|
|
|
setupVBO();
|
2013-11-12 20:18:04 +08:00
|
|
|
}
|
2012-04-24 15:02:18 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ParticleSystemQuad::allocMemory()
|
2012-03-14 14:55:17 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( !_batchNode, "Memory should not be alloced when not using batchNode");
|
2011-03-19 10:07:16 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_FREE(_quads);
|
|
|
|
CC_SAFE_FREE(_indices);
|
2012-04-13 17:24:50 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
_quads = (V3F_C4B_T2F_Quad*)malloc(_totalParticles * sizeof(V3F_C4B_T2F_Quad));
|
2013-06-15 14:03:30 +08:00
|
|
|
_indices = (GLushort*)malloc(_totalParticles * 6 * sizeof(GLushort));
|
2012-04-07 12:02:40 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if( !_quads || !_indices)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Particle system: not enough memory");
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_FREE(_quads);
|
|
|
|
CC_SAFE_FREE(_indices);
|
2012-03-21 21:53:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-07 12:02:40 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
memset(_quads, 0, _totalParticles * sizeof(V3F_C4B_T2F_Quad));
|
2013-06-15 14:03:30 +08:00
|
|
|
memset(_indices, 0, _totalParticles * 6 * sizeof(GLushort));
|
2012-04-07 12:02:40 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
2010-12-28 11:55:34 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystemQuad::setBatchNode(ParticleBatchNode * batchNode)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _batchNode != batchNode )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
ParticleBatchNode* oldBatch = _batchNode;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ParticleSystem::setBatchNode(batchNode);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// NEW: is self render ?
|
|
|
|
if( ! batchNode )
|
|
|
|
{
|
|
|
|
allocMemory();
|
2013-02-27 16:24:33 +08:00
|
|
|
initIndices();
|
2012-04-19 14:35:52 +08:00
|
|
|
setTexture(oldBatch->getTexture());
|
2013-11-13 14:52:16 +08:00
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
2013-11-12 20:18:04 +08:00
|
|
|
setupVBOandVAO();
|
|
|
|
}
|
2013-11-13 14:52:16 +08:00
|
|
|
else
|
|
|
|
{
|
2013-11-12 20:18:04 +08:00
|
|
|
setupVBO();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
// OLD: was it self render ? cleanup
|
|
|
|
else if( !oldBatch )
|
|
|
|
{
|
|
|
|
// copy current state to batch
|
2013-07-05 16:49:22 +08:00
|
|
|
V3F_C4B_T2F_Quad *batchQuads = _batchNode->getTextureAtlas()->getQuads();
|
|
|
|
V3F_C4B_T2F_Quad *quad = &(batchQuads[_atlasIndex] );
|
2013-06-15 14:03:30 +08:00
|
|
|
memcpy( quad, _quads, _totalParticles * sizeof(_quads[0]) );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_FREE(_quads);
|
|
|
|
CC_SAFE_FREE(_indices);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glDeleteBuffers(2, &_buffersVBO[0]);
|
2013-08-03 20:00:32 +08:00
|
|
|
memset(_buffersVBO, 0, sizeof(_buffersVBO));
|
2013-11-13 14:52:16 +08:00
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
2013-11-12 20:18:04 +08:00
|
|
|
glDeleteVertexArrays(1, &_VAOname);
|
|
|
|
GL::bindVAO(0);
|
|
|
|
_VAOname = 0;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2010-12-28 11:55:34 +08:00
|
|
|
}
|
2011-03-19 10:07:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ParticleSystemQuad * ParticleSystemQuad::create() {
|
2014-08-28 07:31:57 +08:00
|
|
|
ParticleSystemQuad *particleSystemQuad = new (std::nothrow) ParticleSystemQuad();
|
2013-12-18 17:47:20 +08:00
|
|
|
if (particleSystemQuad && particleSystemQuad->init())
|
2012-06-02 07:38:43 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
particleSystemQuad->autorelease();
|
|
|
|
return particleSystemQuad;
|
2012-06-02 07:38:43 +08:00
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(particleSystemQuad);
|
|
|
|
return nullptr;
|
2012-06-02 07:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-12-13 06:30:22 +08:00
|
|
|
std::string ParticleSystemQuad::getDescription() const
|
|
|
|
{
|
|
|
|
return StringUtils::format("<ParticleSystemQuad | Tag = %d, Total Particles = %d>", _tag, _totalParticles);
|
|
|
|
}
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_END
|