@@ -24,36 +24,32 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
24
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
25
THE SOFTWARE.
26
26
****************************************************************************/
27
-
28
27
#include " 2d/CCMotionStreak.h"
29
28
#include " math/CCVertex.h"
30
29
#include " base/CCDirector.h"
31
30
#include " base/ccUtils.h"
32
31
#include " renderer/CCTextureCache.h"
33
32
#include " renderer/CCTexture2D.h"
34
33
#include " renderer/CCRenderer.h"
35
- #include " renderer/CCGLProgramState .h"
36
- #include " renderer/CCGLProgram .h"
34
+ #include " renderer/CCShaderCache .h"
35
+ #include " renderer/ccShaders .h"
37
36
38
37
NS_CC_BEGIN
39
38
40
39
MotionStreak::MotionStreak ()
41
- : _fastMode(false )
42
- , _startingPositionInitialized(false )
43
- , _texture(nullptr )
44
- , _blendFunc(BlendFunc::ALPHA_NON_PREMULTIPLIED)
45
- , _stroke(0 .0f )
46
- , _fadeDelta(0 .0f )
47
- , _minSeg(0 .0f )
48
- , _maxPoints(0 )
49
- , _nuPoints(0 )
50
- , _previousNuPoints(0 )
51
- , _pointVertexes(nullptr )
52
- , _pointState(nullptr )
53
- , _vertices(nullptr )
54
- , _colorPointer(nullptr )
55
- , _texCoords(nullptr )
56
40
{
41
+ _customCommand.setDrawType (CustomCommand::DrawType::ARRAY);
42
+ _customCommand.setPrimitiveType (CustomCommand::PrimitiveType::TRIANGLE_STRIP);
43
+
44
+ auto & pipelineDescriptor = _customCommand.getPipelineDescriptor ();
45
+ pipelineDescriptor.vertexShader = ShaderCache::newVertexShaderModule (positionTextureColor_vert);
46
+ pipelineDescriptor.fragmentShader = ShaderCache::newFragmentShaderModule (positionTextureColor_frag);
47
+
48
+ auto & vertexLayout = pipelineDescriptor.vertexLayout ;
49
+ vertexLayout.setAtrribute (" a_position" , 0 , backend::VertexFormat::FLOAT_R32G32, 0 , false );
50
+ vertexLayout.setAtrribute (" a_texCoord" , 1 , backend::VertexFormat::FLOAT_R32G32, 2 * sizeof (float ), false );
51
+ vertexLayout.setAtrribute (" a_color" , 2 , backend::VertexFormat::UBYTE_R8G8B8A8, 4 * sizeof (float ), true );
52
+ vertexLayout.setLayout (4 * sizeof (float ) + 4 * sizeof (uint8_t ), backend::VertexStepMode::VERTEX);
57
53
}
58
54
59
55
MotionStreak::~MotionStreak ()
@@ -109,28 +105,23 @@ bool MotionStreak::initWithFade(float fade, float minSeg, float stroke, const Co
109
105
110
106
_positionR.setZero ();
111
107
_fastMode = true ;
112
- _minSeg = (minSeg == -1 .0f ) ? stroke/5 .0f : minSeg;
108
+ _minSeg = (minSeg == -1 .0f ) ? stroke/ 5 .0f : minSeg;
113
109
_minSeg *= _minSeg;
114
110
115
111
_stroke = stroke;
116
- _fadeDelta = 1 .0f / fade;
112
+ _fadeDelta = 1 .0f / fade;
117
113
118
- double fps = 1 / Director::getInstance ()->getAnimationInterval ();
119
- _maxPoints = (int )(fade* fps)+ 2 ;
114
+ double fps = 1 / Director::getInstance ()->getAnimationInterval ();
115
+ _maxPoints = (int )(fade * fps) + 2 ;
120
116
121
- _nuPoints = 0 ;
122
117
_pointState = (float *)malloc (sizeof (float ) * _maxPoints);
123
118
_pointVertexes = (Vec2*)malloc (sizeof (Vec2) * _maxPoints);
124
119
125
- _vertices = (Vec2*)malloc (sizeof (Vec2) * _maxPoints * 2 );
126
- _texCoords = (Tex2F*)malloc (sizeof (Tex2F) * _maxPoints * 2 );
127
- _colorPointer = (GLubyte*)malloc (sizeof (GLubyte) * _maxPoints * 2 * 4 );
128
-
129
- // Set blend mode
130
- _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
131
-
132
- // shader state
133
- setGLProgramState (GLProgramState::getOrCreateWithGLProgramName (GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR, texture));
120
+ _vertexCount = _maxPoints * 2 ;
121
+ _vertices = (Vec2*)malloc (sizeof (Vec2) * _vertexCount);
122
+ _texCoords = (Tex2F*)malloc (sizeof (Tex2F) * _vertexCount);
123
+ _colorPointer = (uint8_t *)malloc (sizeof (uint8_t ) * 4 * _vertexCount);
124
+ _customCommand.createVertexBuffer (sizeof (Vec2) + sizeof (Tex2F) + sizeof (uint8_t ) * 4 , _vertexCount);
134
125
135
126
setTexture (texture);
136
127
setColor (color);
@@ -249,35 +240,33 @@ void MotionStreak::setOpacityModifyRGB(bool /*bValue*/)
249
240
{
250
241
}
251
242
252
- bool MotionStreak::isOpacityModifyRGB (void ) const
243
+ bool MotionStreak::isOpacityModifyRGB () const
253
244
{
254
245
return false ;
255
246
}
256
247
257
248
void MotionStreak::update (float delta)
258
249
{
259
250
if (!_startingPositionInitialized)
260
- {
261
251
return ;
262
- }
263
-
252
+
264
253
delta *= _fadeDelta;
265
254
266
255
unsigned int newIdx, newIdx2, i, i2;
267
256
unsigned int mov = 0 ;
268
257
269
258
// Update current points
270
- for (i = 0 ; i< _nuPoints; i++)
259
+ for (i = 0 ; i < _nuPoints; i++)
271
260
{
272
- _pointState[i]-= delta;
261
+ _pointState[i] -= delta;
273
262
274
- if (_pointState[i] <= 0 )
263
+ if (_pointState[i] <= 0 )
275
264
mov++;
276
265
else
277
266
{
278
- newIdx = i- mov;
267
+ newIdx = i - mov;
279
268
280
- if (mov> 0 )
269
+ if (mov > 0 )
281
270
{
282
271
// Move data
283
272
_pointState[newIdx] = _pointState[i];
@@ -286,8 +275,8 @@ void MotionStreak::update(float delta)
286
275
_pointVertexes[newIdx] = _pointVertexes[i];
287
276
288
277
// Move vertices
289
- i2 = i* 2 ;
290
- newIdx2 = newIdx* 2 ;
278
+ i2 = i * 2 ;
279
+ newIdx2 = newIdx * 2 ;
291
280
_vertices[newIdx2] = _vertices[i2];
292
281
_vertices[newIdx2+1 ] = _vertices[i2+1 ];
293
282
@@ -301,39 +290,34 @@ void MotionStreak::update(float delta)
301
290
_colorPointer[newIdx2+5 ] = _colorPointer[i2+5 ];
302
291
_colorPointer[newIdx2+6 ] = _colorPointer[i2+6 ];
303
292
}else
304
- newIdx2 = newIdx* 8 ;
293
+ newIdx2 = newIdx * 8 ;
305
294
306
- const GLubyte op = (GLubyte )(_pointState[newIdx] * 255 .0f );
295
+ const uint8_t op = (uint8_t )(_pointState[newIdx] * 255 .0f );
307
296
_colorPointer[newIdx2+3 ] = op;
308
297
_colorPointer[newIdx2+7 ] = op;
309
298
}
310
299
}
311
- _nuPoints-= mov;
300
+ _nuPoints -= mov;
312
301
313
302
// Append new point
314
303
bool appendNewPoint = true ;
315
- if (_nuPoints >= _maxPoints)
316
- {
304
+ if (_nuPoints >= _maxPoints)
317
305
appendNewPoint = false ;
318
- }
319
-
320
- else if (_nuPoints>0 )
306
+ else if (_nuPoints > 0 )
321
307
{
322
308
bool a1 = _pointVertexes[_nuPoints-1 ].getDistanceSq (_positionR) < _minSeg;
323
- bool a2 = (_nuPoints == 1 ) ? false : (_pointVertexes[_nuPoints-2 ].getDistanceSq (_positionR)< (_minSeg * 2 .0f ));
324
- if (a1 || a2)
325
- {
309
+ bool a2 = (_nuPoints == 1 ) ? false : (_pointVertexes[_nuPoints-2 ].getDistanceSq (_positionR) < (_minSeg * 2 .0f ));
310
+ if (a1 || a2)
326
311
appendNewPoint = false ;
327
- }
328
312
}
329
313
330
- if (appendNewPoint)
314
+ if (appendNewPoint)
331
315
{
332
316
_pointVertexes[_nuPoints] = _positionR;
333
317
_pointState[_nuPoints] = 1 .0f ;
334
318
335
319
// Color assignment
336
- const unsigned int offset = _nuPoints* 8 ;
320
+ const unsigned int offset = _nuPoints * 8 ;
337
321
*((Color3B*)(_colorPointer + offset)) = _displayedColor;
338
322
*((Color3B*)(_colorPointer + offset+4 )) = _displayedColor;
339
323
@@ -342,7 +326,7 @@ void MotionStreak::update(float delta)
342
326
_colorPointer[offset+7 ] = 255 ;
343
327
344
328
// Generate polygon
345
- if (_nuPoints > 0 && _fastMode )
329
+ if (_nuPoints > 0 && _fastMode )
346
330
{
347
331
if (_nuPoints > 1 )
348
332
{
@@ -354,18 +338,17 @@ void MotionStreak::update(float delta)
354
338
}
355
339
}
356
340
357
- _nuPoints ++;
341
+ _nuPoints++;
358
342
}
359
343
360
- if ( ! _fastMode )
361
- {
344
+
345
+ if (! _fastMode)
362
346
ccVertexLineToPolygon (_pointVertexes, _stroke, _vertices, 0 , _nuPoints);
363
- }
364
347
365
348
// Updated Tex Coords only if they are different than previous step
366
- if ( _nuPoints && _previousNuPoints != _nuPoints ) {
349
+ if ( _nuPoints && _previousNuPoints != _nuPoints) {
367
350
float texDelta = 1 .0f / _nuPoints;
368
- for ( i= 0 ; i < _nuPoints; i++ ) {
351
+ for (i = 0 ; i < _nuPoints; i++) {
369
352
_texCoords[i*2 ] = Tex2F (0 , texDelta*i);
370
353
_texCoords[i*2 +1 ] = Tex2F (1 , texDelta*i);
371
354
}
@@ -379,34 +362,33 @@ void MotionStreak::reset()
379
362
_nuPoints = 0 ;
380
363
}
381
364
382
- void MotionStreak::onDraw (const Mat4 &transform, uint32_t /* flags*/ )
383
- {
384
- getGLProgram ()->use ();
385
- getGLProgram ()->setUniformsForBuiltins (transform);
386
-
387
- glEnableVertexAttribArray (GLProgram::VERTEX_ATTRIB_POSITION);
388
- glEnableVertexAttribArray (GLProgram::VERTEX_ATTRIB_TEX_COORD);
389
- glEnableVertexAttribArray (GLProgram::VERTEX_ATTRIB_COLOR);
390
- utils::setBlending (_blendFunc.src , _blendFunc.dst );
391
-
392
- glActiveTexture (GL_TEXTURE0);
393
- glBindTexture (GL_TEXTURE_2D, _texture->getName ());
394
-
395
- glVertexAttribPointer (GLProgram::VERTEX_ATTRIB_POSITION, 2 , GL_FLOAT, GL_FALSE, 0 , _vertices);
396
- glVertexAttribPointer (GLProgram::VERTEX_ATTRIB_TEX_COORD, 2 , GL_FLOAT, GL_FALSE, 0 , _texCoords);
397
- glVertexAttribPointer (GLProgram::VERTEX_ATTRIB_COLOR, 4 , GL_UNSIGNED_BYTE, GL_TRUE, 0 , _colorPointer);
398
-
399
- glDrawArrays (GL_TRIANGLE_STRIP, 0 , (GLsizei)_nuPoints*2 );
400
- CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES (1 , _nuPoints*2 );
401
- }
402
-
403
365
void MotionStreak::draw (Renderer *renderer, const Mat4 &transform, uint32_t flags)
404
366
{
405
367
if (_nuPoints <= 1 )
406
368
return ;
407
- _customCommand.init (_globalZOrder, transform, flags);
408
- _customCommand.func = CC_CALLBACK_0 (MotionStreak::onDraw, this , transform, flags);
369
+
370
+ auto drawCount = _nuPoints * 2 ;
371
+
372
+ _customCommand.init (_globalZOrder, _blendFunc);
373
+ _customCommand.setVertexDrawInfo (0 , drawCount);
409
374
renderer->addCommand (&_customCommand);
375
+
376
+ auto & bindGroup = _customCommand.getPipelineDescriptor ().bindGroup ;
377
+ bindGroup.setTexture (" u_texture" , 0 , _texture->getBackendTexture ());
378
+
379
+ const auto & projectionMat = Director::getInstance ()->getMatrix (MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
380
+ Mat4 finalMat = projectionMat * transform;
381
+ bindGroup.setUniform (" u_MVPMatrix" , finalMat.m , sizeof (Mat4));
382
+
383
+ unsigned int offset = 0 ;
384
+ unsigned int vertexSize = sizeof (Vec2) + sizeof (Vec2) + sizeof (uint8_t ) * 4 ;
385
+ for (unsigned int i = 0 ; i < drawCount; ++i)
386
+ {
387
+ offset = i * vertexSize;
388
+ _customCommand.updateVertexBuffer (&_vertices[i], offset, sizeof (_vertices[0 ]));
389
+ _customCommand.updateVertexBuffer (&_texCoords[i], offset + sizeof (_vertices[0 ]), sizeof (_texCoords[0 ]) );
390
+ _customCommand.updateVertexBuffer (&_colorPointer[i * 4 ], offset + sizeof (_vertices[0 ]) + sizeof (_texCoords[0 ]), 4 * sizeof (uint8_t ));
391
+ }
410
392
}
411
393
412
394
NS_CC_END
0 commit comments