forked from cocos2d/cocos2d-x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCCParticleSystemQuad.cpp
583 lines (482 loc) · 17.9 KB
/
CCParticleSystemQuad.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2009 Leonardo Kasperavičius
Copyright (c) 2011 Zynga 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 "CCGL.h"
#include "CCParticleSystemQuad.h"
#include "CCSpriteFrame.h"
#include "CCDirector.h"
#include "CCParticleBatchNode.h"
#include "CCTextureAtlas.h"
#include "CCShaderCache.h"
#include "ccGLStateCache.h"
#include "CCGLProgram.h"
#include "support/TransformUtils.h"
#include "extensions/CCNotificationCenter/CCNotificationCenter.h"
#include "CCEventType.h"
// extern
#include "kazmath/GL/matrix.h"
NS_CC_BEGIN
//implementation CCParticleSystemQuad
// overriding the init method
bool CCParticleSystemQuad::initWithTotalParticles(unsigned int numberOfParticles)
{
// base initialization
if( CCParticleSystem::initWithTotalParticles(numberOfParticles) )
{
// allocating data space
if( ! this->allocMemory() ) {
this->release();
return false;
}
setupIndices();
#if CC_TEXTURE_ATLAS_USE_VAO
setupVBOandVAO();
#else
setupVBO();
#endif
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));
// Need to listen the event only when not use batchnode, because it will use VBO
extension::CCNotificationCenter::sharedNotificationCenter()->addObserver(this,
callfuncO_selector(CCParticleSystemQuad::listenBackToForeground),
EVNET_COME_TO_FOREGROUND,
NULL);
return true;
}
return false;
}
CCParticleSystemQuad::CCParticleSystemQuad()
:m_pQuads(NULL)
,m_pIndices(NULL)
#if CC_TEXTURE_ATLAS_USE_VAO
,m_uVAOname(0)
#endif
{
memset(m_pBuffersVBO, 0, sizeof(m_pBuffersVBO));
}
CCParticleSystemQuad::~CCParticleSystemQuad()
{
if (NULL == m_pBatchNode)
{
CC_SAFE_FREE(m_pQuads);
CC_SAFE_FREE(m_pIndices);
glDeleteBuffers(2, &m_pBuffersVBO[0]);
#if CC_TEXTURE_ATLAS_USE_VAO
glDeleteVertexArrays(1, &m_uVAOname);
#endif
}
extension::CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, EVNET_COME_TO_FOREGROUND);
}
// implementation CCParticleSystemQuad
CCParticleSystemQuad * CCParticleSystemQuad::particleWithFile(const char *plistFile)
{
return CCParticleSystemQuad::create(plistFile);
}
CCParticleSystemQuad * CCParticleSystemQuad::create(const char *plistFile)
{
CCParticleSystemQuad *pRet = new CCParticleSystemQuad();
if (pRet && pRet->initWithFile(plistFile))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return pRet;
}
// pointRect should be in Texture coordinates, not pixel coordinates
void CCParticleSystemQuad::initTexCoordsWithRect(const CCRect& pointRect)
{
// convert to Tex coords
CCRect rect = CCRectMake(
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) pointRect.size.width;
GLfloat high = (GLfloat) pointRect.size.height;
if (m_pTexture)
{
wide = (GLfloat)m_pTexture->getPixelsWide();
high = (GLfloat)m_pTexture->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 + (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
// Important. Texture in cocos2d are inverted, so the Y component should be inverted
CC_SWAP( top, bottom, float);
ccV3F_C4B_T2F_Quad *quads = NULL;
unsigned int start = 0, end = 0;
if (m_pBatchNode)
{
quads = m_pBatchNode->getTextureAtlas()->getQuads();
start = m_uAtlasIndex;
end = m_uAtlasIndex + m_uTotalParticles;
}
else
{
quads = m_pQuads;
start = 0;
end = m_uTotalParticles;
}
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;
}
}
void CCParticleSystemQuad::setTextureWithRect(CCTexture2D *texture, const CCRect& rect)
{
// Only update the texture if is different from the current one
if( !m_pTexture || texture->getName() != m_pTexture->getName() )
{
CCParticleSystem::setTexture(texture);
}
this->initTexCoordsWithRect(rect);
}
void CCParticleSystemQuad::setTexture(CCTexture2D* texture)
{
const CCSize& s = texture->getContentSize();
this->setTextureWithRect(texture, CCRectMake(0, 0, s.width, s.height));
}
void CCParticleSystemQuad::setDisplayFrame(CCSpriteFrame *spriteFrame)
{
CCAssert( CCPoint::CCPointEqualToPoint( spriteFrame->getOffsetInPixels() , CCPointZero ), "QuadParticle only supports SpriteFrames with no offsets");
// update texture before updating texture rect
if ( !m_pTexture || spriteFrame->getTexture()->getName() != m_pTexture->getName())
{
this->setTexture(spriteFrame->getTexture());
}
}
void CCParticleSystemQuad::setupIndices()
{
for(unsigned int i = 0; i < m_uTotalParticles; ++i)
{
const unsigned int i6 = i*6;
const unsigned int i4 = i*4;
m_pIndices[i6+0] = (GLushort) i4+0;
m_pIndices[i6+1] = (GLushort) i4+1;
m_pIndices[i6+2] = (GLushort) i4+2;
m_pIndices[i6+5] = (GLushort) i4+1;
m_pIndices[i6+4] = (GLushort) i4+2;
m_pIndices[i6+3] = (GLushort) i4+3;
}
}
void CCParticleSystemQuad::updateQuadWithParticle(tCCParticle* particle, const CCPoint& newPosition)
{
ccV3F_C4B_T2F_Quad *quad;
if (m_pBatchNode)
{
ccV3F_C4B_T2F_Quad *batchQuads = m_pBatchNode->getTextureAtlas()->getQuads();
quad = &(batchQuads[m_uAtlasIndex+particle->atlasIndex]);
}
else
{
quad = &(m_pQuads[m_uParticleIdx]);
}
ccColor4B color = (m_bOpacityModifyRGB)
? ccc4( 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)
: ccc4( particle->color.r*255, particle->color.g*255, particle->color.b*255, particle->color.a*255);
quad->bl.colors = color;
quad->br.colors = color;
quad->tl.colors = color;
quad->tr.colors = color;
// vertices
GLfloat size_2 = particle->size/2;
if (particle->rotation)
{
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;
}
else
{
// 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;
}
}
void CCParticleSystemQuad::postStep()
{
glBindBuffer(GL_ARRAY_BUFFER, m_pBuffersVBO[0] );
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(m_pQuads[0])*m_uParticleCount, m_pQuads);
glBindBuffer(GL_ARRAY_BUFFER, 0);
CHECK_GL_ERROR_DEBUG();
}
// overriding draw method
void CCParticleSystemQuad::draw()
{
CCAssert(!m_pBatchNode,"draw should not be called when added to a particleBatchNode");
CC_NODE_DRAW_SETUP();
ccGLBindTexture2D( m_pTexture->getName() );
ccGLBlendFunc( m_tBlendFunc.src, m_tBlendFunc.dst );
CCAssert( m_uParticleIdx == m_uParticleCount, "Abnormal error in particle quad");
#if CC_TEXTURE_ATLAS_USE_VAO
//
// Using VBO and VAO
//
glBindVertexArray( m_uVAOname );
#if CC_REBIND_INDICES_BUFFER
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_pBuffersVBO[1]);
#endif
glDrawElements(GL_TRIANGLES, (GLsizei) m_uParticleIdx*6, GL_UNSIGNED_SHORT, 0);
#if CC_REBIND_INDICES_BUFFER
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
#endif
glBindVertexArray( 0 );
#else
//
// Using VBO without VAO
//
#define kQuadSize sizeof(m_pQuads[0].bl)
ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex );
glBindBuffer(GL_ARRAY_BUFFER, m_pBuffersVBO[0]);
// vertices
glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( ccV3F_C4B_T2F, vertices));
// colors
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof( ccV3F_C4B_T2F, colors));
// tex coords
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( ccV3F_C4B_T2F, texCoords));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_pBuffersVBO[1]);
glDrawElements(GL_TRIANGLES, (GLsizei) m_uParticleIdx*6, GL_UNSIGNED_SHORT, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
#endif
CC_INCREMENT_GL_DRAWS(1);
CHECK_GL_ERROR_DEBUG();
}
void CCParticleSystemQuad::setTotalParticles(unsigned int tp)
{
// If we are setting the total numer of particles to a number higher
// than what is allocated, we need to allocate new arrays
if( tp > m_uAllocatedParticles )
{
// Allocate new memory
size_t particlesSize = tp * sizeof(tCCParticle);
size_t quadsSize = sizeof(m_pQuads[0]) * tp * 1;
size_t indicesSize = sizeof(m_pIndices[0]) * tp * 6 * 1;
tCCParticle* particlesNew = (tCCParticle*)realloc(m_pParticles, particlesSize);
ccV3F_C4B_T2F_Quad* quadsNew = (ccV3F_C4B_T2F_Quad*)realloc(m_pQuads, quadsSize);
GLushort* indicesNew = (GLushort*)realloc(m_pIndices, indicesSize);
if (particlesNew && quadsNew && indicesNew)
{
// Assign pointers
m_pParticles = particlesNew;
m_pQuads = quadsNew;
m_pIndices = indicesNew;
// Clear the memory
memset(m_pParticles, 0, particlesSize);
memset(m_pQuads, 0, quadsSize);
memset(m_pIndices, 0, indicesSize);
m_uAllocatedParticles = tp;
}
else
{
// Out of memory, failed to resize some array
if (particlesNew) m_pParticles = particlesNew;
if (quadsNew) m_pQuads = quadsNew;
if (indicesNew) m_pIndices = indicesNew;
CCLOG("Particle system: out of memory");
return;
}
m_uTotalParticles = tp;
// Init particles
if (m_pBatchNode)
{
for (unsigned int i = 0; i < m_uTotalParticles; i++)
{
m_pParticles[i].atlasIndex=i;
}
}
setupIndices();
#if CC_TEXTURE_ATLAS_USE_VAO
setupVBOandVAO();
#else
setupVBO();
#endif
}
else
{
m_uTotalParticles = tp;
}
}
#if CC_TEXTURE_ATLAS_USE_VAO
void CCParticleSystemQuad::setupVBOandVAO()
{
glGenVertexArrays(1, &m_uVAOname);
glBindVertexArray(m_uVAOname);
#define kQuadSize sizeof(m_pQuads[0].bl)
glGenBuffers(2, &m_pBuffersVBO[0]);
glBindBuffer(GL_ARRAY_BUFFER, m_pBuffersVBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(m_pQuads[0]) * m_uTotalParticles, m_pQuads, GL_DYNAMIC_DRAW);
// vertices
glEnableVertexAttribArray(kCCVertexAttrib_Position);
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( ccV3F_C4B_T2F, vertices));
// colors
glEnableVertexAttribArray(kCCVertexAttrib_Color);
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof( ccV3F_C4B_T2F, colors));
// tex coords
glEnableVertexAttribArray(kCCVertexAttrib_TexCoords);
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( ccV3F_C4B_T2F, texCoords));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_pBuffersVBO[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(m_pIndices[0]) * m_uTotalParticles * 6, m_pIndices, GL_STATIC_DRAW);
glBindVertexArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
CHECK_GL_ERROR_DEBUG();
}
#else
void CCParticleSystemQuad::setupVBO()
{
glGenBuffers(2, &m_pBuffersVBO[0]);
glBindBuffer(GL_ARRAY_BUFFER, m_pBuffersVBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(m_pQuads[0]) * m_uTotalParticles, m_pQuads, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_pBuffersVBO[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(m_pIndices[0]) * m_uTotalParticles * 6, m_pIndices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
CHECK_GL_ERROR_DEBUG();
}
#endif
void CCParticleSystemQuad::listenBackToForeground(CCObject *obj)
{
#if CC_TEXTURE_ATLAS_USE_VAO
setupVBOandVAO();
#else
setupVBO();
#endif
}
bool CCParticleSystemQuad::allocMemory()
{
CCAssert( ( !m_pQuads && !m_pIndices), "Memory already alloced");
CCAssert( !m_pBatchNode, "Memory should not be alloced when not using batchNode");
CC_SAFE_FREE(m_pQuads);
CC_SAFE_FREE(m_pIndices);
m_pQuads = (ccV3F_C4B_T2F_Quad*)malloc(m_uTotalParticles * sizeof(ccV3F_C4B_T2F_Quad));
m_pIndices = (GLushort*)malloc(m_uTotalParticles * 6 * sizeof(GLushort));
if( !m_pQuads || !m_pIndices)
{
CCLOG("cocos2d: Particle system: not enough memory");
CC_SAFE_FREE(m_pQuads);
CC_SAFE_FREE(m_pIndices);
return false;
}
memset(m_pQuads, 0, m_uTotalParticles * sizeof(ccV3F_C4B_T2F_Quad));
memset(m_pIndices, 0, m_uTotalParticles * 6 * sizeof(GLushort));
return true;
}
void CCParticleSystemQuad::setBatchNode(CCParticleBatchNode * batchNode)
{
if( m_pBatchNode != batchNode )
{
CCParticleBatchNode* oldBatch = m_pBatchNode;
CCParticleSystem::setBatchNode(batchNode);
// NEW: is self render ?
if( ! batchNode )
{
allocMemory();
setupIndices();
setTexture(oldBatch->getTexture());
#if CC_TEXTURE_ATLAS_USE_VAO
setupVBOandVAO();
#else
setupVBO();
#endif
}
// OLD: was it self render ? cleanup
else if( !oldBatch )
{
// copy current state to batch
ccV3F_C4B_T2F_Quad *batchQuads = m_pBatchNode->getTextureAtlas()->getQuads();
ccV3F_C4B_T2F_Quad *quad = &(batchQuads[m_uAtlasIndex] );
memcpy( quad, m_pQuads, m_uTotalParticles * sizeof(m_pQuads[0]) );
CC_SAFE_FREE(m_pQuads);
CC_SAFE_FREE(m_pIndices);
glDeleteBuffers(2, &m_pBuffersVBO[0]);
#if CC_TEXTURE_ATLAS_USE_VAO
glDeleteVertexArrays(1, &m_uVAOname);
#endif
}
}
}
CCParticleSystemQuad * CCParticleSystemQuad::node() {
CCParticleSystemQuad *pParticleSystemQuad = new CCParticleSystemQuad();
if (pParticleSystemQuad && pParticleSystemQuad->init())
{
pParticleSystemQuad->autorelease();
return pParticleSystemQuad;
}
CC_SAFE_DELETE(pParticleSystemQuad);
return NULL;
}
NS_CC_END