2011-03-30 15:00:13 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2011 ForzeField Studios S.L.
|
2012-06-14 15:13:16 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2011-03-30 15:00:13 +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,
|
2012-03-14 16:10:01 +08:00
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN false EVENT SHALL THE
|
2011-03-30 15:00:13 +08:00
|
|
|
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-05-17 05:36:00 +08:00
|
|
|
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "2d/CCMotionStreak.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "math/CCVertex.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "renderer/CCTextureCache.h"
|
2014-05-09 09:01:48 +08:00
|
|
|
#include "renderer/ccGLStateCache.h"
|
2014-08-28 17:03:29 +08:00
|
|
|
#include "renderer/CCTexture2D.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "renderer/CCRenderer.h"
|
2014-08-29 15:39:52 +08:00
|
|
|
#include "renderer/CCGLProgramState.h"
|
2011-03-30 15:00:13 +08:00
|
|
|
|
2012-03-14 14:55:17 +08:00
|
|
|
NS_CC_BEGIN
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MotionStreak::MotionStreak()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _fastMode(false)
|
|
|
|
, _startingPositionInitialized(false)
|
2013-12-18 17:47:20 +08:00
|
|
|
, _texture(nullptr)
|
2013-07-29 14:07:57 +08:00
|
|
|
, _blendFunc(BlendFunc::ALPHA_NON_PREMULTIPLIED)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _stroke(0.0f)
|
|
|
|
, _fadeDelta(0.0f)
|
|
|
|
, _minSeg(0.0f)
|
|
|
|
, _maxPoints(0)
|
|
|
|
, _nuPoints(0)
|
|
|
|
, _previousNuPoints(0)
|
2013-12-18 17:47:20 +08:00
|
|
|
, _pointVertexes(nullptr)
|
|
|
|
, _pointState(nullptr)
|
|
|
|
, _vertices(nullptr)
|
|
|
|
, _colorPointer(nullptr)
|
|
|
|
, _texCoords(nullptr)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MotionStreak::~MotionStreak()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_texture);
|
|
|
|
CC_SAFE_FREE(_pointState);
|
|
|
|
CC_SAFE_FREE(_pointVertexes);
|
|
|
|
CC_SAFE_FREE(_vertices);
|
|
|
|
CC_SAFE_FREE(_colorPointer);
|
|
|
|
CC_SAFE_FREE(_texCoords);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
MotionStreak* MotionStreak::create(float fade, float minSeg, float stroke, const Color3B& color, const std::string& path)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
MotionStreak *ret = new (std::nothrow) MotionStreak();
|
2013-12-18 17:47:20 +08:00
|
|
|
if (ret && ret->initWithFade(fade, minSeg, stroke, color, path))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
MotionStreak* MotionStreak::create(float fade, float minSeg, float stroke, const Color3B& color, Texture2D* texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
MotionStreak *ret = new (std::nothrow) MotionStreak();
|
2013-12-18 17:47:20 +08:00
|
|
|
if (ret && ret->initWithFade(fade, minSeg, stroke, color, texture))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
bool MotionStreak::initWithFade(float fade, float minSeg, float stroke, const Color3B& color, const std::string& path)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-24 10:51:47 +08:00
|
|
|
CCASSERT(!path.empty(), "Invalid filename");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-07 19:11:09 +08:00
|
|
|
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(path);
|
2012-04-19 14:35:52 +08:00
|
|
|
return initWithFade(fade, minSeg, stroke, color, texture);
|
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
bool MotionStreak::initWithFade(float fade, float minSeg, float stroke, const Color3B& color, Texture2D* texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Node::setPosition(Vec2::ZERO);
|
|
|
|
setAnchorPoint(Vec2::ZERO);
|
2016-04-22 20:36:02 +08:00
|
|
|
setIgnoreAnchorPointForPosition(true);
|
2013-06-15 14:03:30 +08:00
|
|
|
_startingPositionInitialized = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-04-20 01:40:52 +08:00
|
|
|
_positionR.setZero();
|
2013-06-15 14:03:30 +08:00
|
|
|
_fastMode = true;
|
|
|
|
_minSeg = (minSeg == -1.0f) ? stroke/5.0f : minSeg;
|
|
|
|
_minSeg *= _minSeg;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_stroke = stroke;
|
|
|
|
_fadeDelta = 1.0f/fade;
|
2015-01-07 03:02:32 +08:00
|
|
|
|
|
|
|
double fps = 1/Director::getInstance()->getAnimationInterval();
|
|
|
|
_maxPoints = (int)(fade*fps)+2;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_nuPoints = 0;
|
|
|
|
_pointState = (float *)malloc(sizeof(float) * _maxPoints);
|
2014-05-15 01:07:09 +08:00
|
|
|
_pointVertexes = (Vec2*)malloc(sizeof(Vec2) * _maxPoints);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
_vertices = (Vec2*)malloc(sizeof(Vec2) * _maxPoints * 2);
|
2013-07-05 16:49:22 +08:00
|
|
|
_texCoords = (Tex2F*)malloc(sizeof(Tex2F) * _maxPoints * 2);
|
2013-06-15 14:03:30 +08:00
|
|
|
_colorPointer = (GLubyte*)malloc(sizeof(GLubyte) * _maxPoints * 2 * 4);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Set blend mode
|
2013-07-26 04:36:19 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
// shader state
|
2016-07-25 17:31:54 +08:00
|
|
|
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR, texture));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
setTexture(texture);
|
|
|
|
setColor(color);
|
|
|
|
scheduleUpdate();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void MotionStreak::setPosition(const Vec2& position)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-03-07 19:06:51 +08:00
|
|
|
if (!_startingPositionInitialized) {
|
|
|
|
_startingPositionInitialized = true;
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_positionR = position;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-03-07 16:32:00 +08:00
|
|
|
void MotionStreak::setPosition(float x, float y)
|
|
|
|
{
|
2014-03-07 19:06:51 +08:00
|
|
|
if (!_startingPositionInitialized) {
|
|
|
|
_startingPositionInitialized = true;
|
|
|
|
}
|
|
|
|
_positionR.x = x;
|
|
|
|
_positionR.y = y;
|
2014-03-07 16:32:00 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
const Vec2& MotionStreak::getPosition() const
|
2014-03-07 16:32:00 +08:00
|
|
|
{
|
|
|
|
return _positionR;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MotionStreak::getPosition(float* x, float* y) const
|
|
|
|
{
|
|
|
|
*x = _positionR.x;
|
|
|
|
*y = _positionR.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
float MotionStreak::getPositionX() const
|
|
|
|
{
|
|
|
|
return _positionR.x;
|
|
|
|
}
|
|
|
|
|
2015-01-29 18:30:00 +08:00
|
|
|
Vec3 MotionStreak::getPosition3D() const
|
|
|
|
{
|
|
|
|
return Vec3(_positionR.x, _positionR.y, getPositionZ());
|
|
|
|
}
|
|
|
|
|
2014-03-07 16:32:00 +08:00
|
|
|
void MotionStreak::setPositionX(float x)
|
|
|
|
{
|
2014-03-07 19:06:51 +08:00
|
|
|
if (!_startingPositionInitialized) {
|
|
|
|
_startingPositionInitialized = true;
|
|
|
|
}
|
|
|
|
_positionR.x = x;
|
2014-03-07 16:32:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float MotionStreak::getPositionY() const
|
|
|
|
{
|
|
|
|
return _positionR.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MotionStreak::setPositionY(float y)
|
|
|
|
{
|
2014-03-07 19:06:51 +08:00
|
|
|
if (!_startingPositionInitialized) {
|
|
|
|
_startingPositionInitialized = true;
|
|
|
|
}
|
|
|
|
_positionR.y = y;
|
2014-03-07 16:32:00 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void MotionStreak::tintWithColor(const Color3B& colors)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
setColor(colors);
|
|
|
|
|
|
|
|
// Fast assignation
|
2013-06-15 14:03:30 +08:00
|
|
|
for(unsigned int i = 0; i<_nuPoints*2; i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-05 16:49:22 +08:00
|
|
|
*((Color3B*) (_colorPointer+i*4)) = colors;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-23 18:26:26 +08:00
|
|
|
Texture2D* MotionStreak::getTexture(void) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _texture;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MotionStreak::setTexture(Texture2D *texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_texture != texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_RETAIN(texture);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_texture);
|
|
|
|
_texture = texture;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void MotionStreak::setBlendFunc(const BlendFunc &blendFunc)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_blendFunc = blendFunc;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
const BlendFunc& MotionStreak::getBlendFunc(void) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _blendFunc;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MotionStreak::setOpacity(GLubyte opacity)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(false, "Set opacity no supported");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 12:50:17 +08:00
|
|
|
GLubyte MotionStreak::getOpacity(void) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(false, "Opacity no supported");
|
2012-04-19 14:35:52 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MotionStreak::setOpacityModifyRGB(bool bValue)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(bValue);
|
|
|
|
}
|
|
|
|
|
2013-07-04 12:50:17 +08:00
|
|
|
bool MotionStreak::isOpacityModifyRGB(void) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MotionStreak::update(float delta)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_startingPositionInitialized)
|
2012-06-08 14:11:48 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
delta *= _fadeDelta;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
unsigned int newIdx, newIdx2, i, i2;
|
|
|
|
unsigned int mov = 0;
|
|
|
|
|
|
|
|
// Update current points
|
2013-06-15 14:03:30 +08:00
|
|
|
for(i = 0; i<_nuPoints; i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_pointState[i]-=delta;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_pointState[i] <= 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
mov++;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newIdx = i-mov;
|
|
|
|
|
|
|
|
if(mov>0)
|
|
|
|
{
|
|
|
|
// Move data
|
2013-06-15 14:03:30 +08:00
|
|
|
_pointState[newIdx] = _pointState[i];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Move point
|
2013-06-15 14:03:30 +08:00
|
|
|
_pointVertexes[newIdx] = _pointVertexes[i];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Move vertices
|
|
|
|
i2 = i*2;
|
|
|
|
newIdx2 = newIdx*2;
|
2013-06-15 14:03:30 +08:00
|
|
|
_vertices[newIdx2] = _vertices[i2];
|
|
|
|
_vertices[newIdx2+1] = _vertices[i2+1];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Move color
|
|
|
|
i2 *= 4;
|
|
|
|
newIdx2 *= 4;
|
2013-06-15 14:03:30 +08:00
|
|
|
_colorPointer[newIdx2+0] = _colorPointer[i2+0];
|
|
|
|
_colorPointer[newIdx2+1] = _colorPointer[i2+1];
|
|
|
|
_colorPointer[newIdx2+2] = _colorPointer[i2+2];
|
|
|
|
_colorPointer[newIdx2+4] = _colorPointer[i2+4];
|
|
|
|
_colorPointer[newIdx2+5] = _colorPointer[i2+5];
|
|
|
|
_colorPointer[newIdx2+6] = _colorPointer[i2+6];
|
2012-04-19 14:35:52 +08:00
|
|
|
}else
|
|
|
|
newIdx2 = newIdx*8;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
const GLubyte op = (GLubyte)(_pointState[newIdx] * 255.0f);
|
|
|
|
_colorPointer[newIdx2+3] = op;
|
|
|
|
_colorPointer[newIdx2+7] = op;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_nuPoints-=mov;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Append new point
|
|
|
|
bool appendNewPoint = true;
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_nuPoints >= _maxPoints)
|
2012-06-08 14:11:48 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
appendNewPoint = false;
|
2012-06-08 14:11:48 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
else if(_nuPoints>0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-11 16:38:58 +08:00
|
|
|
bool a1 = _pointVertexes[_nuPoints-1].getDistanceSq(_positionR) < _minSeg;
|
|
|
|
bool a2 = (_nuPoints == 1) ? false : (_pointVertexes[_nuPoints-2].getDistanceSq(_positionR)< (_minSeg * 2.0f));
|
2012-04-19 14:35:52 +08:00
|
|
|
if(a1 || a2)
|
2012-06-08 14:11:48 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
appendNewPoint = false;
|
2012-06-08 14:11:48 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(appendNewPoint)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_pointVertexes[_nuPoints] = _positionR;
|
|
|
|
_pointState[_nuPoints] = 1.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
// Color assignment
|
2013-06-15 14:03:30 +08:00
|
|
|
const unsigned int offset = _nuPoints*8;
|
2013-07-05 16:49:22 +08:00
|
|
|
*((Color3B*)(_colorPointer + offset)) = _displayedColor;
|
|
|
|
*((Color3B*)(_colorPointer + offset+4)) = _displayedColor;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Opacity
|
2013-06-15 14:03:30 +08:00
|
|
|
_colorPointer[offset+3] = 255;
|
|
|
|
_colorPointer[offset+7] = 255;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Generate polygon
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_nuPoints > 0 && _fastMode )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_nuPoints > 1)
|
2012-06-08 14:11:48 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, _nuPoints, 1);
|
2012-06-08 14:11:48 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
else
|
2012-06-08 14:11:48 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, 0, 2);
|
2012-06-08 14:11:48 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_nuPoints ++;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if( ! _fastMode )
|
2012-06-08 14:11:48 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, 0, _nuPoints);
|
2012-06-08 14:11:48 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 05:26:28 +08:00
|
|
|
// Updated Tex Coords only if they are different than previous step
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _nuPoints && _previousNuPoints != _nuPoints ) {
|
|
|
|
float texDelta = 1.0f / _nuPoints;
|
|
|
|
for( i=0; i < _nuPoints; i++ ) {
|
2013-07-05 16:49:22 +08:00
|
|
|
_texCoords[i*2] = Tex2F(0, texDelta*i);
|
|
|
|
_texCoords[i*2+1] = Tex2F(1, texDelta*i);
|
2012-06-14 05:26:28 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_previousNuPoints = _nuPoints;
|
2012-06-08 14:11:48 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MotionStreak::reset()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_nuPoints = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void MotionStreak::onDraw(const Mat4 &transform, uint32_t flags)
|
2013-12-20 21:21:50 +08:00
|
|
|
{
|
2014-05-09 07:42:36 +08:00
|
|
|
getGLProgram()->use();
|
|
|
|
getGLProgram()->setUniformsForBuiltins(transform);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX );
|
|
|
|
GL::blendFunc( _blendFunc.src, _blendFunc.dst );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2016-08-03 14:29:34 +08:00
|
|
|
GL::bindTexture2D( _texture );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, _vertices);
|
2014-05-09 03:34:26 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, _texCoords);
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, _colorPointer);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)_nuPoints*2);
|
2014-03-01 08:10:48 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _nuPoints*2);
|
2013-12-20 21:21:50 +08:00
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void MotionStreak::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
|
2013-12-20 21:21:50 +08:00
|
|
|
{
|
|
|
|
if(_nuPoints <= 1)
|
|
|
|
return;
|
2015-01-16 06:00:49 +08:00
|
|
|
_customCommand.init(_globalZOrder, transform, flags);
|
2014-05-31 07:42:05 +08:00
|
|
|
_customCommand.func = CC_CALLBACK_0(MotionStreak::onDraw, this, transform, flags);
|
2014-03-01 08:10:48 +08:00
|
|
|
renderer->addCommand(&_customCommand);
|
2012-03-14 16:10:01 +08:00
|
|
|
}
|
2010-08-19 10:14:54 +08:00
|
|
|
|
2012-03-14 14:55:17 +08:00
|
|
|
NS_CC_END
|