2011-03-19 10:07:16 +08:00
|
|
|
/****************************************************************************
|
2012-06-08 14:11:48 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
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
|
2011-07-04 19:20:16 +08:00
|
|
|
Copyright (c) 2011 Zynga 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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-04-25 16:18:04 +08:00
|
|
|
#include "CCGL.h"
|
2011-03-19 10:07:16 +08:00
|
|
|
#include "CCParticleSystemQuad.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCSpriteFrame.h"
|
2011-11-28 17:28:43 +08:00
|
|
|
#include "CCDirector.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCParticleBatchNode.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCTextureAtlas.h"
|
|
|
|
#include "CCShaderCache.h"
|
|
|
|
#include "ccGLStateCache.h"
|
|
|
|
#include "CCGLProgram.h"
|
|
|
|
#include "TransformUtils.h"
|
|
|
|
#include "CCNotificationCenter.h"
|
2012-04-24 15:02:18 +08:00
|
|
|
#include "CCEventType.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// extern
|
2012-03-14 14:55:17 +08:00
|
|
|
#include "kazmath/GL/matrix.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
|
|
|
//implementation ParticleSystemQuad
|
2010-12-28 11:55:34 +08:00
|
|
|
// overriding the init method
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ParticleSystemQuad::initWithTotalParticles(unsigned int numberOfParticles)
|
2010-12-28 11:55:34 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// base initialization
|
2013-06-20 14:13:12 +08:00
|
|
|
if( ParticleSystem::initWithTotalParticles(numberOfParticles) )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// allocating data space
|
|
|
|
if( ! this->allocMemory() ) {
|
|
|
|
this->release();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-28 11:55:36 +08:00
|
|
|
initIndices();
|
2012-04-19 14:35:52 +08:00
|
|
|
#if CC_TEXTURE_ATLAS_USE_VAO
|
2012-04-13 17:24:50 +08:00
|
|
|
setupVBOandVAO();
|
|
|
|
#else
|
|
|
|
setupVBO();
|
2012-04-19 14:35:52 +08:00
|
|
|
#endif
|
|
|
|
|
2013-09-16 20:38:03 +08:00
|
|
|
setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
|
2012-04-24 15:02:18 +08:00
|
|
|
|
2013-07-17 13:53:16 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2012-04-24 15:02:18 +08:00
|
|
|
// Need to listen the event only when not use batchnode, because it will use VBO
|
2013-07-12 06:24:23 +08:00
|
|
|
NotificationCenter::getInstance()->addObserver(this,
|
2013-06-20 14:13:12 +08:00
|
|
|
callfuncO_selector(ParticleSystemQuad::listenBackToForeground),
|
2012-04-24 15:02:18 +08:00
|
|
|
EVNET_COME_TO_FOREGROUND,
|
|
|
|
NULL);
|
2013-07-17 13:53:16 +08:00
|
|
|
#endif
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2010-12-28 11:55:34 +08:00
|
|
|
}
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ParticleSystemQuad::ParticleSystemQuad()
|
2013-06-15 14:03:30 +08:00
|
|
|
:_quads(NULL)
|
|
|
|
,_indices(NULL)
|
2012-04-13 17:24:50 +08:00
|
|
|
#if CC_TEXTURE_ATLAS_USE_VAO
|
2013-06-15 14:03:30 +08:00
|
|
|
,_VAOname(0)
|
2012-04-13 17:24:50 +08:00
|
|
|
#endif
|
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-06-15 14:03:30 +08:00
|
|
|
if (NULL == _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]);
|
2012-04-19 14:35:52 +08:00
|
|
|
#if CC_TEXTURE_ATLAS_USE_VAO
|
2013-06-15 14:03:30 +08:00
|
|
|
glDeleteVertexArrays(1, &_VAOname);
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindVAO(0);
|
2012-04-13 17:24:50 +08:00
|
|
|
#endif
|
2012-04-13 11:40:56 +08:00
|
|
|
}
|
2012-04-24 15:02:18 +08:00
|
|
|
|
2013-07-17 13:53:16 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2013-07-12 06:24:23 +08:00
|
|
|
NotificationCenter::getInstance()->removeObserver(this, EVNET_COME_TO_FOREGROUND);
|
2013-07-17 13:53:16 +08:00
|
|
|
#endif
|
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-06-20 14:13:12 +08:00
|
|
|
ParticleSystemQuad * ParticleSystemQuad::create(const char *plistFile)
|
2011-06-14 16:27:47 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
ParticleSystemQuad *pRet = new ParticleSystemQuad();
|
2011-06-14 16:27:47 +08:00
|
|
|
if (pRet && pRet->initWithFile(plistFile))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return pRet;
|
2011-06-14 16:27:47 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ParticleSystemQuad * ParticleSystemQuad::createWithTotalParticles(unsigned int numberOfParticles) {
|
|
|
|
ParticleSystemQuad *pRet = new ParticleSystemQuad();
|
2012-09-21 08:24:18 +08:00
|
|
|
if (pRet && pRet->initWithTotalParticles(numberOfParticles))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
CC_SWAP( top, bottom, float);
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
V3F_C4B_T2F_Quad *quads = NULL;
|
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
|
|
|
}
|
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
|
|
|
}
|
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
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystemQuad::setDisplayFrame(SpriteFrame *spriteFrame)
|
2010-12-28 11:55:34 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(spriteFrame->getOffsetInPixels().equals(Point::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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystemQuad::updateQuadWithParticle(tParticle* particle, const Point& 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]);
|
2012-11-14 18:05:15 +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
|
|
|
|
|
|
|
// Option 2: Data
|
|
|
|
// glBufferData(GL_ARRAY_BUFFER, sizeof(quads_[0]) * particleCount, quads_, GL_DYNAMIC_DRAW);
|
|
|
|
|
|
|
|
// Option 3: Orphaning + glMapBuffer
|
2013-06-15 14:03:30 +08:00
|
|
|
// glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0])*_totalParticles, NULL, GL_STREAM_DRAW);
|
2012-12-01 05:22:01 +08:00
|
|
|
// void *buf = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
2013-06-15 14:03:30 +08:00
|
|
|
// memcpy(buf, _quads, sizeof(_quads[0])*_totalParticles);
|
2012-12-01 05:22:01 +08:00
|
|
|
// glUnmapBuffer(GL_ARRAY_BUFFER);
|
2012-11-14 18:05:15 +08:00
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2010-12-28 11:55:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// overriding draw method
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystemQuad::draw()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(!_batchNode,"draw should not be called when added to a particleBatchNode");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CC_NODE_DRAW_SETUP();
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindTexture2D( _texture->getName() );
|
|
|
|
GL::blendFunc( _blendFunc.src, _blendFunc.dst );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( _particleIdx == _particleCount, "Abnormal error in particle quad");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
#if CC_TEXTURE_ATLAS_USE_VAO
|
|
|
|
//
|
|
|
|
// Using VBO and VAO
|
|
|
|
//
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindVAO(_VAOname);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
#if CC_REBIND_INDICES_BUFFER
|
2013-06-15 14:03:30 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
|
2012-04-19 14:35:52 +08:00
|
|
|
#endif
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glDrawElements(GL_TRIANGLES, (GLsizei) _particleIdx*6, GL_UNSIGNED_SHORT, 0);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
#if CC_REBIND_INDICES_BUFFER
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
//
|
|
|
|
// Using VBO without VAO
|
|
|
|
//
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
#define kQuadSize sizeof(_quads[0].bl)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 13:49:24 +08:00
|
|
|
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
|
2012-04-19 14:35:52 +08:00
|
|
|
// vertices
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, 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
|
|
|
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
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 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]);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glDrawElements(GL_TRIANGLES, (GLsizei) _particleIdx*6, GL_UNSIGNED_SHORT, 0);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CC_INCREMENT_GL_DRAWS(1);
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2012-03-16 13:42:53 +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-02-27 16:24:33 +08:00
|
|
|
// XXX: Bug? If the quads are cleared, then drawing doesn't work... WHY??? XXX
|
2013-06-15 14:03:30 +08:00
|
|
|
memset(_particles, 0, particlesSize);
|
|
|
|
memset(_quads, 0, quadsSize);
|
|
|
|
memset(_indices, 0, indicesSize);
|
2012-04-19 14:35:52 +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();
|
2012-04-19 14:35:52 +08:00
|
|
|
#if CC_TEXTURE_ATLAS_USE_VAO
|
|
|
|
setupVBOandVAO();
|
|
|
|
#else
|
|
|
|
setupVBO();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
resetSystem();
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
2011-03-19 10:07:16 +08:00
|
|
|
|
2012-04-13 17:24:50 +08:00
|
|
|
#if CC_TEXTURE_ATLAS_USE_VAO
|
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
|
2013-07-25 17:48:22 +08:00
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 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();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystemQuad::listenBackToForeground(Object *obj)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
|
|
|
#if CC_TEXTURE_ATLAS_USE_VAO
|
|
|
|
setupVBOandVAO();
|
|
|
|
#else
|
|
|
|
setupVBO();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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( ( !_quads && !_indices), "Memory already alloced");
|
|
|
|
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());
|
|
|
|
#if CC_TEXTURE_ATLAS_USE_VAO
|
2012-04-13 17:24:50 +08:00
|
|
|
setupVBOandVAO();
|
|
|
|
#else
|
|
|
|
setupVBO();
|
2012-04-19 14:35:52 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
// 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));
|
2012-04-19 14:35:52 +08:00
|
|
|
#if CC_TEXTURE_ATLAS_USE_VAO
|
2013-06-15 14:03:30 +08:00
|
|
|
glDeleteVertexArrays(1, &_VAOname);
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindVAO(0);
|
2013-08-05 15:42:01 +08:00
|
|
|
_VAOname = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
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() {
|
|
|
|
ParticleSystemQuad *pParticleSystemQuad = new ParticleSystemQuad();
|
2012-06-02 07:38:43 +08:00
|
|
|
if (pParticleSystemQuad && pParticleSystemQuad->init())
|
|
|
|
{
|
|
|
|
pParticleSystemQuad->autorelease();
|
|
|
|
return pParticleSystemQuad;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pParticleSystemQuad);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_END
|