2012-11-09 12:08:18 +08:00
|
|
|
/* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
|
|
|
|
* Copyright (c) 2012 cocos2d-x.org
|
2016-08-05 09:42:15 +08:00
|
|
|
* Copyright (c) 2013-2016 Chukong Technologies Inc.
|
2012-11-09 12:08:18 +08:00
|
|
|
*
|
|
|
|
* 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-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCDrawNode.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 "renderer/CCRenderer.h"
|
2014-08-27 13:39:50 +08:00
|
|
|
#include "renderer/ccGLStateCache.h"
|
2014-05-10 09:39:25 +08:00
|
|
|
#include "renderer/CCGLProgramState.h"
|
2014-09-11 15:39:56 +08:00
|
|
|
#include "renderer/CCGLProgramCache.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCEventListenerCustom.h"
|
|
|
|
#include "base/CCEventDispatcher.h"
|
2014-09-11 15:39:56 +08:00
|
|
|
#include "2d/CCActionCatmullRom.h"
|
2014-09-10 07:50:02 +08:00
|
|
|
#include "platform/CCGL.h"
|
2012-11-09 12:08:18 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
// Vec2 == CGPoint in 32-bits, but not in 64-bits (OS X)
|
2012-11-09 12:08:18 +08:00
|
|
|
// that's why the "v2f" functions are needed
|
2014-05-15 01:07:09 +08:00
|
|
|
static Vec2 v2fzero(0.0f,0.0f);
|
2012-11-09 12:08:18 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
static inline Vec2 v2f(float x, float y)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 ret(x, y);
|
2015-01-08 10:22:45 +08:00
|
|
|
return ret;
|
2012-11-09 12:08:18 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
static inline Vec2 v2fadd(const Vec2 &v0, const Vec2 &v1)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
2015-01-08 10:22:45 +08:00
|
|
|
return v2f(v0.x+v1.x, v0.y+v1.y);
|
2012-11-09 12:08:18 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
static inline Vec2 v2fsub(const Vec2 &v0, const Vec2 &v1)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
2015-01-08 10:22:45 +08:00
|
|
|
return v2f(v0.x-v1.x, v0.y-v1.y);
|
2012-11-09 12:08:18 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
static inline Vec2 v2fmult(const Vec2 &v, float s)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
2015-01-08 10:22:45 +08:00
|
|
|
return v2f(v.x * s, v.y * s);
|
2012-11-09 12:08:18 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
static inline Vec2 v2fperp(const Vec2 &p0)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
2015-01-08 10:22:45 +08:00
|
|
|
return v2f(-p0.y, p0.x);
|
2012-11-09 12:08:18 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
static inline Vec2 v2fneg(const Vec2 &p0)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
2015-01-08 10:22:45 +08:00
|
|
|
return v2f(-p0.x, - p0.y);
|
2012-11-09 12:08:18 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
static inline float v2fdot(const Vec2 &p0, const Vec2 &p1)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
2015-01-08 10:22:45 +08:00
|
|
|
return p0.x * p1.x + p0.y * p1.y;
|
2012-11-09 12:08:18 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
static inline Vec2 v2fnormalize(const Vec2 &p)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
Optimize Vec3
small function Vec3 move to Vec3.inl
Added:
add(float xx, float yy, float zz);
setZero();
Change all code:
_vec3 = Vec3(x, y, z); -> _vec3.set(x, y, z);
Vec3 vec3 = Vec3(x, y, z); -> Vec3 vec3(x, y, z);
_vec3 += Vec3(x, y, z); -> _vec3.add(x, y, z);
_vec3 = Vec3::ZERO; -> _vec3.setZero();
2015-04-05 18:09:50 +08:00
|
|
|
Vec2 r(p.x, p.y);
|
|
|
|
r.normalize();
|
2015-01-08 10:22:45 +08:00
|
|
|
return v2f(r.x, r.y);
|
2012-11-09 12:08:18 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
static inline Vec2 __v2f(const Vec2 &v)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
2012-11-20 16:31:33 +08:00
|
|
|
//#ifdef __LP64__
|
2015-01-08 10:22:45 +08:00
|
|
|
return v2f(v.x, v.y);
|
2012-11-20 16:31:33 +08:00
|
|
|
// #else
|
2015-01-08 10:22:45 +08:00
|
|
|
// return * ((Vec2*) &v);
|
2012-11-20 16:31:33 +08:00
|
|
|
// #endif
|
2012-11-09 12:08:18 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
static inline Tex2F __t(const Vec2 &v)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
2015-01-08 10:22:45 +08:00
|
|
|
return *(Tex2F*)&v;
|
2012-11-09 12:08:18 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation of DrawNode
|
2012-11-09 12:08:18 +08:00
|
|
|
|
2015-11-25 23:26:57 +08:00
|
|
|
DrawNode::DrawNode(int lineWidth)
|
2013-06-15 14:03:30 +08:00
|
|
|
: _vao(0)
|
|
|
|
, _vbo(0)
|
2014-10-30 22:25:46 +08:00
|
|
|
, _vaoGLPoint(0)
|
|
|
|
, _vboGLPoint(0)
|
|
|
|
, _vaoGLLine(0)
|
|
|
|
, _vboGLLine(0)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _bufferCapacity(0)
|
|
|
|
, _bufferCount(0)
|
2013-12-18 17:47:20 +08:00
|
|
|
, _buffer(nullptr)
|
2014-09-11 15:39:56 +08:00
|
|
|
, _bufferCapacityGLPoint(0)
|
|
|
|
, _bufferCountGLPoint(0)
|
|
|
|
, _bufferGLPoint(nullptr)
|
|
|
|
, _bufferCapacityGLLine(0)
|
|
|
|
, _bufferCountGLLine(0)
|
|
|
|
, _bufferGLLine(nullptr)
|
2014-10-30 22:25:46 +08:00
|
|
|
, _dirty(false)
|
|
|
|
, _dirtyGLPoint(false)
|
2014-09-11 15:39:56 +08:00
|
|
|
, _dirtyGLLine(false)
|
2015-11-25 23:26:57 +08:00
|
|
|
, _lineWidth(lineWidth)
|
|
|
|
, _defaultLineWidth(lineWidth)
|
2012-11-16 17:08:34 +08:00
|
|
|
{
|
2013-07-26 08:47:42 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
2012-11-16 17:08:34 +08:00
|
|
|
}
|
2012-11-09 12:08:18 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
DrawNode::~DrawNode()
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
free(_buffer);
|
2013-12-18 17:47:20 +08:00
|
|
|
_buffer = nullptr;
|
2014-09-11 15:39:56 +08:00
|
|
|
free(_bufferGLPoint);
|
|
|
|
_bufferGLPoint = nullptr;
|
|
|
|
free(_bufferGLLine);
|
|
|
|
_bufferGLLine = nullptr;
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glDeleteBuffers(1, &_vbo);
|
2014-09-11 15:39:56 +08:00
|
|
|
glDeleteBuffers(1, &_vboGLLine);
|
|
|
|
glDeleteBuffers(1, &_vboGLPoint);
|
2013-06-15 14:03:30 +08:00
|
|
|
_vbo = 0;
|
2014-09-11 15:39:56 +08:00
|
|
|
_vboGLPoint = 0;
|
|
|
|
_vboGLLine = 0;
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2013-11-13 14:52:16 +08:00
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
2015-01-23 18:19:03 +08:00
|
|
|
GL::bindVAO(0);
|
2013-11-12 20:18:04 +08:00
|
|
|
glDeleteVertexArrays(1, &_vao);
|
2014-10-11 12:00:14 +08:00
|
|
|
glDeleteVertexArrays(1, &_vaoGLLine);
|
|
|
|
glDeleteVertexArrays(1, &_vaoGLPoint);
|
2015-01-23 18:19:03 +08:00
|
|
|
_vao = _vaoGLLine = _vaoGLPoint = 0;
|
2013-11-12 20:18:04 +08:00
|
|
|
}
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2015-11-26 09:32:57 +08:00
|
|
|
DrawNode* DrawNode::create(int defaultLineWidth)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
2015-11-26 09:32:57 +08:00
|
|
|
DrawNode* ret = new (std::nothrow) DrawNode(defaultLineWidth);
|
2013-12-18 17:47:20 +08:00
|
|
|
if (ret && ret->init())
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
2012-11-09 12:08:18 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2012-11-09 12:08:18 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2012-11-09 12:08:18 +08:00
|
|
|
}
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
void DrawNode::ensureCapacity(int count)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(count>=0, "capacity must be >= 0");
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_bufferCount + count > _bufferCapacity)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2015-01-08 10:22:45 +08:00
|
|
|
_bufferCapacity += MAX(_bufferCapacity, count);
|
|
|
|
_buffer = (V2F_C4B_T2F*)realloc(_buffer, _bufferCapacity*sizeof(V2F_C4B_T2F));
|
|
|
|
}
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2014-09-11 15:39:56 +08:00
|
|
|
void DrawNode::ensureCapacityGLPoint(int count)
|
|
|
|
{
|
|
|
|
CCASSERT(count>=0, "capacity must be >= 0");
|
|
|
|
|
|
|
|
if(_bufferCountGLPoint + count > _bufferCapacityGLPoint)
|
|
|
|
{
|
2014-09-12 07:33:34 +08:00
|
|
|
_bufferCapacityGLPoint += MAX(_bufferCapacityGLPoint, count);
|
2015-01-20 16:50:51 +08:00
|
|
|
_bufferGLPoint = (V2F_C4B_T2F*)realloc(_bufferGLPoint, _bufferCapacityGLPoint*sizeof(V2F_C4B_T2F));
|
2014-09-12 07:33:34 +08:00
|
|
|
}
|
2014-09-11 15:39:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::ensureCapacityGLLine(int count)
|
|
|
|
{
|
|
|
|
CCASSERT(count>=0, "capacity must be >= 0");
|
|
|
|
|
|
|
|
if(_bufferCountGLLine + count > _bufferCapacityGLLine)
|
|
|
|
{
|
2014-09-12 07:33:34 +08:00
|
|
|
_bufferCapacityGLLine += MAX(_bufferCapacityGLLine, count);
|
|
|
|
_bufferGLLine = (V2F_C4B_T2F*)realloc(_bufferGLLine, _bufferCapacityGLLine*sizeof(V2F_C4B_T2F));
|
|
|
|
}
|
2014-09-11 15:39:56 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool DrawNode::init()
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
2013-07-26 08:47:42 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
2012-11-20 16:31:33 +08:00
|
|
|
|
2014-05-13 10:12:56 +08:00
|
|
|
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR));
|
2012-11-09 12:08:18 +08:00
|
|
|
|
|
|
|
ensureCapacity(512);
|
2014-09-11 15:39:56 +08:00
|
|
|
ensureCapacityGLPoint(64);
|
|
|
|
ensureCapacityGLLine(256);
|
2012-11-09 12:08:18 +08:00
|
|
|
|
2013-11-13 14:52:16 +08:00
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
2013-11-12 20:18:04 +08:00
|
|
|
glGenVertexArrays(1, &_vao);
|
|
|
|
GL::bindVAO(_vao);
|
2015-01-23 18:19:03 +08:00
|
|
|
glGenBuffers(1, &_vbo);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)* _bufferCapacity, _buffer, GL_STREAM_DRAW);
|
|
|
|
// vertex
|
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
|
|
|
|
// color
|
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
|
|
|
|
// texcood
|
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
|
|
|
|
|
2014-09-11 15:39:56 +08:00
|
|
|
glGenVertexArrays(1, &_vaoGLLine);
|
|
|
|
GL::bindVAO(_vaoGLLine);
|
2015-01-23 18:19:03 +08:00
|
|
|
glGenBuffers(1, &_vboGLLine);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vboGLLine);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLLine, _bufferGLLine, GL_STREAM_DRAW);
|
|
|
|
// vertex
|
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
|
|
|
|
// color
|
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
|
|
|
|
// texcood
|
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
|
|
|
|
|
2014-09-11 15:39:56 +08:00
|
|
|
glGenVertexArrays(1, &_vaoGLPoint);
|
|
|
|
GL::bindVAO(_vaoGLPoint);
|
2015-01-23 18:19:03 +08:00
|
|
|
glGenBuffers(1, &_vboGLPoint);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vboGLPoint);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLPoint, _bufferGLPoint, GL_STREAM_DRAW);
|
|
|
|
// vertex
|
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
|
|
|
|
// color
|
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
|
|
|
|
// Texture coord as pointsize
|
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
|
|
|
|
|
|
|
|
GL::bindVAO(0);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
|
2014-09-11 15:39:56 +08:00
|
|
|
}
|
2015-01-23 18:19:03 +08:00
|
|
|
else
|
2013-11-13 14:52:16 +08:00
|
|
|
{
|
2015-01-23 18:19:03 +08:00
|
|
|
glGenBuffers(1, &_vbo);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)* _bufferCapacity, _buffer, GL_STREAM_DRAW);
|
|
|
|
|
|
|
|
glGenBuffers(1, &_vboGLLine);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vboGLLine);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLLine, _bufferGLLine, GL_STREAM_DRAW);
|
|
|
|
|
|
|
|
glGenBuffers(1, &_vboGLPoint);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vboGLPoint);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLPoint, _bufferGLPoint, GL_STREAM_DRAW);
|
2015-01-29 18:06:20 +08:00
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
2013-11-12 20:18:04 +08:00
|
|
|
}
|
2012-11-09 12:08:18 +08:00
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = true;
|
2014-09-11 15:39:56 +08:00
|
|
|
_dirtyGLLine = true;
|
|
|
|
_dirtyGLPoint = true;
|
2012-11-09 12:08:18 +08:00
|
|
|
|
2013-07-17 13:53:16 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2013-07-17 12:53:30 +08:00
|
|
|
// 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, [this](EventCustom* event){
|
|
|
|
/** listen the event that renderer was recreated on Android/WP8 */
|
2013-12-31 10:54:37 +08:00
|
|
|
this->init();
|
|
|
|
});
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2013-07-17 13:53:16 +08:00
|
|
|
#endif
|
2013-07-17 12:53:30 +08:00
|
|
|
|
2012-11-09 12:08:18 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void DrawNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2014-09-11 15:39:56 +08:00
|
|
|
if(_bufferCount)
|
|
|
|
{
|
2015-01-16 06:00:49 +08:00
|
|
|
_customCommand.init(_globalZOrder, transform, flags);
|
2014-09-11 15:39:56 +08:00
|
|
|
_customCommand.func = CC_CALLBACK_0(DrawNode::onDraw, this, transform, flags);
|
|
|
|
renderer->addCommand(&_customCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_bufferCountGLPoint)
|
|
|
|
{
|
2015-01-16 06:00:49 +08:00
|
|
|
_customCommandGLPoint.init(_globalZOrder, transform, flags);
|
2014-09-11 15:39:56 +08:00
|
|
|
_customCommandGLPoint.func = CC_CALLBACK_0(DrawNode::onDrawGLPoint, this, transform, flags);
|
|
|
|
renderer->addCommand(&_customCommandGLPoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_bufferCountGLLine)
|
|
|
|
{
|
2015-01-16 06:00:49 +08:00
|
|
|
_customCommandGLLine.init(_globalZOrder, transform, flags);
|
2014-09-11 15:39:56 +08:00
|
|
|
_customCommandGLLine.func = CC_CALLBACK_0(DrawNode::onDrawGLLine, this, transform, flags);
|
|
|
|
renderer->addCommand(&_customCommandGLLine);
|
|
|
|
}
|
2014-03-06 07:49:08 +08:00
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void DrawNode::onDraw(const Mat4 &transform, uint32_t flags)
|
2014-03-06 07:49:08 +08:00
|
|
|
{
|
2015-09-23 17:34:35 +08:00
|
|
|
getGLProgramState()->apply(transform);
|
2014-09-11 15:39:56 +08:00
|
|
|
|
2014-03-06 07:49:08 +08:00
|
|
|
GL::blendFunc(_blendFunc.src, _blendFunc.dst);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_dirty)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
|
2013-07-05 16:49:22 +08:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacity, _buffer, GL_STREAM_DRAW);
|
2014-09-11 15:39:56 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = false;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
2013-11-13 14:52:16 +08:00
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
2013-11-12 20:18:04 +08:00
|
|
|
GL::bindVAO(_vao);
|
|
|
|
}
|
2013-11-13 14:52:16 +08:00
|
|
|
else
|
|
|
|
{
|
2013-11-12 20:18:04 +08:00
|
|
|
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
|
2014-03-06 07:49:08 +08:00
|
|
|
|
2013-11-12 20:18:04 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
|
|
|
|
// vertex
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
|
|
|
|
// color
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
|
|
|
|
// texcood
|
2014-05-09 03:34:26 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
|
2013-11-12 20:18:04 +08:00
|
|
|
}
|
2014-10-24 12:03:11 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glDrawArrays(GL_TRIANGLES, 0, _bufferCount);
|
2014-11-07 10:05:29 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
2014-09-11 15:39:56 +08:00
|
|
|
|
2015-01-23 18:19:03 +08:00
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
|
|
|
GL::bindVAO(0);
|
|
|
|
}
|
|
|
|
|
2014-09-11 15:39:56 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _bufferCount);
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
}
|
2014-03-06 07:49:08 +08:00
|
|
|
|
2014-09-11 15:39:56 +08:00
|
|
|
void DrawNode::onDrawGLLine(const Mat4 &transform, uint32_t flags)
|
|
|
|
{
|
|
|
|
auto glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR);
|
|
|
|
glProgram->use();
|
|
|
|
glProgram->setUniformsForBuiltins(transform);
|
2015-07-03 11:30:38 +08:00
|
|
|
|
|
|
|
GL::blendFunc(_blendFunc.src, _blendFunc.dst);
|
|
|
|
|
2014-09-11 15:39:56 +08:00
|
|
|
if (_dirtyGLLine)
|
|
|
|
{
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vboGLLine);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLLine, _bufferGLLine, GL_STREAM_DRAW);
|
|
|
|
_dirtyGLLine = false;
|
|
|
|
}
|
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
|
|
|
GL::bindVAO(_vaoGLLine);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vboGLLine);
|
|
|
|
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
|
|
|
|
// vertex
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
|
|
|
|
// color
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
|
|
|
|
// texcood
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
|
|
|
|
}
|
2015-11-16 11:08:20 +08:00
|
|
|
|
2015-07-07 17:47:20 +08:00
|
|
|
glLineWidth(_lineWidth);
|
2014-09-11 15:39:56 +08:00
|
|
|
glDrawArrays(GL_LINES, 0, _bufferCountGLLine);
|
2015-01-23 18:19:03 +08:00
|
|
|
|
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
|
|
|
GL::bindVAO(0);
|
|
|
|
}
|
|
|
|
|
2014-11-07 10:05:29 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
2014-09-11 15:39:56 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,_bufferCountGLLine);
|
2015-11-12 09:49:49 +08:00
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
}
|
|
|
|
|
2014-09-11 15:39:56 +08:00
|
|
|
void DrawNode::onDrawGLPoint(const Mat4 &transform, uint32_t flags)
|
|
|
|
{
|
2015-01-20 16:50:51 +08:00
|
|
|
auto glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_COLOR_TEXASPOINTSIZE);
|
2014-09-12 07:33:34 +08:00
|
|
|
glProgram->use();
|
|
|
|
glProgram->setUniformsForBuiltins(transform);
|
2015-07-03 11:30:38 +08:00
|
|
|
|
|
|
|
GL::blendFunc(_blendFunc.src, _blendFunc.dst);
|
|
|
|
|
2014-10-24 12:03:11 +08:00
|
|
|
if (_dirtyGLPoint)
|
|
|
|
{
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vboGLPoint);
|
2015-01-20 16:50:51 +08:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLPoint, _bufferGLPoint, GL_STREAM_DRAW);
|
2014-10-24 12:03:11 +08:00
|
|
|
|
|
|
|
_dirtyGLPoint = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
|
|
|
GL::bindVAO(_vaoGLPoint);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vboGLPoint);
|
2015-01-20 16:50:51 +08:00
|
|
|
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
|
2015-01-20 18:09:11 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
|
2014-10-24 12:03:11 +08:00
|
|
|
}
|
2014-09-11 15:39:56 +08:00
|
|
|
|
|
|
|
glDrawArrays(GL_POINTS, 0, _bufferCountGLPoint);
|
2015-01-23 18:19:03 +08:00
|
|
|
|
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
|
|
|
GL::bindVAO(0);
|
|
|
|
}
|
|
|
|
|
2014-11-07 10:05:29 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
2014-09-11 15:39:56 +08:00
|
|
|
|
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,_bufferCountGLPoint);
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::drawPoint(const Vec2& position, const float pointSize, const Color4F &color)
|
|
|
|
{
|
2014-10-24 12:03:11 +08:00
|
|
|
ensureCapacityGLPoint(1);
|
|
|
|
|
2015-01-20 16:50:51 +08:00
|
|
|
V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLPoint + _bufferCountGLPoint);
|
|
|
|
V2F_C4B_T2F a = {position, Color4B(color), Tex2F(pointSize,0)};
|
2014-09-11 15:39:56 +08:00
|
|
|
*point = a;
|
|
|
|
|
|
|
|
_bufferCountGLPoint += 1;
|
2014-10-24 12:03:11 +08:00
|
|
|
_dirtyGLPoint = true;
|
2014-09-11 15:39:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::drawPoints(const Vec2 *position, unsigned int numberOfPoints, const Color4F &color)
|
2015-01-13 22:30:50 +08:00
|
|
|
{
|
|
|
|
drawPoints(position, numberOfPoints, 1.0, color);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::drawPoints(const Vec2 *position, unsigned int numberOfPoints, const float pointSize, const Color4F &color)
|
2014-09-11 15:39:56 +08:00
|
|
|
{
|
|
|
|
ensureCapacityGLPoint(numberOfPoints);
|
|
|
|
|
2015-01-20 16:50:51 +08:00
|
|
|
V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLPoint + _bufferCountGLPoint);
|
2014-09-11 15:39:56 +08:00
|
|
|
|
2014-10-09 17:19:43 +08:00
|
|
|
for(unsigned int i=0; i < numberOfPoints; i++,point++)
|
2014-09-11 15:39:56 +08:00
|
|
|
{
|
2015-01-20 16:50:51 +08:00
|
|
|
V2F_C4B_T2F a = {position[i], Color4B(color), Tex2F(pointSize,0)};
|
2014-09-11 15:39:56 +08:00
|
|
|
*point = a;
|
|
|
|
}
|
|
|
|
|
|
|
|
_bufferCountGLPoint += numberOfPoints;
|
2014-10-24 12:03:11 +08:00
|
|
|
_dirtyGLPoint = true;
|
2014-09-11 15:39:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::drawLine(const Vec2 &origin, const Vec2 &destination, const Color4F &color)
|
|
|
|
{
|
|
|
|
ensureCapacityGLLine(2);
|
|
|
|
|
|
|
|
V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLLine + _bufferCountGLLine);
|
|
|
|
|
|
|
|
V2F_C4B_T2F a = {origin, Color4B(color), Tex2F(0.0, 0.0)};
|
|
|
|
V2F_C4B_T2F b = {destination, Color4B(color), Tex2F(0.0, 0.0)};
|
|
|
|
|
|
|
|
*point = a;
|
|
|
|
*(point+1) = b;
|
|
|
|
|
|
|
|
_bufferCountGLLine += 2;
|
|
|
|
_dirtyGLLine = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::drawRect(const Vec2 &origin, const Vec2 &destination, const Color4F &color)
|
|
|
|
{
|
|
|
|
drawLine(Vec2(origin.x, origin.y), Vec2(destination.x, origin.y), color);
|
|
|
|
drawLine(Vec2(destination.x, origin.y), Vec2(destination.x, destination.y), color);
|
|
|
|
drawLine(Vec2(destination.x, destination.y), Vec2(origin.x, destination.y), color);
|
|
|
|
drawLine(Vec2(origin.x, destination.y), Vec2(origin.x, origin.y), color);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::drawPoly(const Vec2 *poli, unsigned int numberOfPoints, bool closePolygon, const Color4F &color)
|
|
|
|
{
|
|
|
|
unsigned int vertext_count;
|
|
|
|
if(closePolygon)
|
|
|
|
{
|
|
|
|
vertext_count = 2 * numberOfPoints;
|
|
|
|
ensureCapacityGLLine(vertext_count);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vertext_count = 2 * (numberOfPoints - 1);
|
|
|
|
ensureCapacityGLLine(vertext_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLLine + _bufferCountGLLine);
|
|
|
|
|
2014-10-09 17:19:43 +08:00
|
|
|
unsigned int i = 0;
|
|
|
|
for(; i<numberOfPoints-1; i++)
|
2014-09-11 15:39:56 +08:00
|
|
|
{
|
|
|
|
V2F_C4B_T2F a = {poli[i], Color4B(color), Tex2F(0.0, 0.0)};
|
|
|
|
V2F_C4B_T2F b = {poli[i+1], Color4B(color), Tex2F(0.0, 0.0)};
|
|
|
|
|
|
|
|
*point = a;
|
|
|
|
*(point+1) = b;
|
|
|
|
point += 2;
|
|
|
|
}
|
|
|
|
if(closePolygon)
|
|
|
|
{
|
|
|
|
V2F_C4B_T2F a = {poli[i], Color4B(color), Tex2F(0.0, 0.0)};
|
|
|
|
V2F_C4B_T2F b = {poli[0], Color4B(color), Tex2F(0.0, 0.0)};
|
|
|
|
*point = a;
|
|
|
|
*(point+1) = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
_bufferCountGLLine += vertext_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::drawCircle(const Vec2& center, float radius, float angle, unsigned int segments, bool drawLineToCenter, float scaleX, float scaleY, const Color4F &color)
|
|
|
|
{
|
|
|
|
const float coef = 2.0f * (float)M_PI/segments;
|
|
|
|
|
2014-09-12 11:12:20 +08:00
|
|
|
Vec2 *vertices = new (std::nothrow) Vec2[segments+2];
|
2014-09-11 15:39:56 +08:00
|
|
|
if( ! vertices )
|
|
|
|
return;
|
|
|
|
|
|
|
|
for(unsigned int i = 0;i <= segments; i++) {
|
|
|
|
float rads = i*coef;
|
|
|
|
GLfloat j = radius * cosf(rads + angle) * scaleX + center.x;
|
|
|
|
GLfloat k = radius * sinf(rads + angle) * scaleY + center.y;
|
|
|
|
|
|
|
|
vertices[i].x = j;
|
|
|
|
vertices[i].y = k;
|
|
|
|
}
|
|
|
|
if(drawLineToCenter)
|
|
|
|
{
|
|
|
|
vertices[segments+1].x = center.x;
|
|
|
|
vertices[segments+1].y = center.y;
|
|
|
|
drawPoly(vertices, segments+2, true, color);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
drawPoly(vertices, segments+1, true, color);
|
|
|
|
|
|
|
|
CC_SAFE_DELETE_ARRAY(vertices);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::drawCircle(const Vec2 ¢er, float radius, float angle, unsigned int segments, bool drawLineToCenter, const Color4F &color)
|
|
|
|
{
|
|
|
|
drawCircle(center, radius, angle, segments, drawLineToCenter, 1.0f, 1.0f, color);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::drawQuadBezier(const Vec2 &origin, const Vec2 &control, const Vec2 &destination, unsigned int segments, const Color4F &color)
|
|
|
|
{
|
2014-09-12 14:50:41 +08:00
|
|
|
Vec2* vertices = new (std::nothrow) Vec2[segments + 1];
|
2014-09-12 11:12:20 +08:00
|
|
|
if( ! vertices )
|
|
|
|
return;
|
2014-09-11 15:39:56 +08:00
|
|
|
|
|
|
|
float t = 0.0f;
|
|
|
|
for(unsigned int i = 0; i < segments; i++)
|
|
|
|
{
|
|
|
|
vertices[i].x = powf(1 - t, 2) * origin.x + 2.0f * (1 - t) * t * control.x + t * t * destination.x;
|
|
|
|
vertices[i].y = powf(1 - t, 2) * origin.y + 2.0f * (1 - t) * t * control.y + t * t * destination.y;
|
|
|
|
t += 1.0f / segments;
|
|
|
|
}
|
|
|
|
vertices[segments].x = destination.x;
|
|
|
|
vertices[segments].y = destination.y;
|
|
|
|
|
|
|
|
drawPoly(vertices, segments+1, false, color);
|
|
|
|
|
|
|
|
CC_SAFE_DELETE_ARRAY(vertices);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::drawCubicBezier(const Vec2 &origin, const Vec2 &control1, const Vec2 &control2, const Vec2 &destination, unsigned int segments, const Color4F &color)
|
|
|
|
{
|
2014-09-12 11:12:20 +08:00
|
|
|
Vec2* vertices = new (std::nothrow) Vec2[segments + 1];
|
|
|
|
if( ! vertices )
|
|
|
|
return;
|
2014-09-11 15:39:56 +08:00
|
|
|
|
|
|
|
float t = 0;
|
|
|
|
for (unsigned int i = 0; i < segments; i++)
|
|
|
|
{
|
|
|
|
vertices[i].x = powf(1 - t, 3) * origin.x + 3.0f * powf(1 - t, 2) * t * control1.x + 3.0f * (1 - t) * t * t * control2.x + t * t * t * destination.x;
|
|
|
|
vertices[i].y = powf(1 - t, 3) * origin.y + 3.0f * powf(1 - t, 2) * t * control1.y + 3.0f * (1 - t) * t * t * control2.y + t * t * t * destination.y;
|
|
|
|
t += 1.0f / segments;
|
|
|
|
}
|
|
|
|
vertices[segments].x = destination.x;
|
|
|
|
vertices[segments].y = destination.y;
|
|
|
|
|
|
|
|
drawPoly(vertices, segments+1, false, color);
|
|
|
|
|
|
|
|
CC_SAFE_DELETE_ARRAY(vertices);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::drawCardinalSpline(PointArray *config, float tension, unsigned int segments, const Color4F &color)
|
|
|
|
{
|
2014-09-12 11:12:20 +08:00
|
|
|
Vec2* vertices = new (std::nothrow) Vec2[segments + 1];
|
|
|
|
if( ! vertices )
|
|
|
|
return;
|
2014-09-11 15:39:56 +08:00
|
|
|
|
|
|
|
ssize_t p;
|
|
|
|
float lt;
|
|
|
|
float deltaT = 1.0f / config->count();
|
|
|
|
|
|
|
|
for( unsigned int i=0; i < segments+1;i++) {
|
|
|
|
|
|
|
|
float dt = (float)i / segments;
|
|
|
|
|
|
|
|
// border
|
|
|
|
if( dt == 1 ) {
|
|
|
|
p = config->count() - 1;
|
|
|
|
lt = 1;
|
|
|
|
} else {
|
|
|
|
p = dt / deltaT;
|
|
|
|
lt = (dt - deltaT * (float)p) / deltaT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interpolate
|
|
|
|
Vec2 pp0 = config->getControlPointAtIndex(p-1);
|
|
|
|
Vec2 pp1 = config->getControlPointAtIndex(p+0);
|
|
|
|
Vec2 pp2 = config->getControlPointAtIndex(p+1);
|
|
|
|
Vec2 pp3 = config->getControlPointAtIndex(p+2);
|
|
|
|
|
|
|
|
Vec2 newPos = ccCardinalSplineAt( pp0, pp1, pp2, pp3, tension, lt);
|
|
|
|
vertices[i].x = newPos.x;
|
|
|
|
vertices[i].y = newPos.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
drawPoly(vertices, segments+1, false, color);
|
|
|
|
|
|
|
|
CC_SAFE_DELETE_ARRAY(vertices);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::drawCatmullRom(PointArray *points, unsigned int segments, const Color4F &color)
|
|
|
|
{
|
|
|
|
drawCardinalSpline( points, 0.5f, segments, color);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void DrawNode::drawDot(const Vec2 &pos, float radius, const Color4F &color)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
|
|
|
unsigned int vertex_count = 2*3;
|
|
|
|
ensureCapacity(vertex_count);
|
2015-01-08 10:22:45 +08:00
|
|
|
|
|
|
|
V2F_C4B_T2F a = {Vec2(pos.x - radius, pos.y - radius), Color4B(color), Tex2F(-1.0, -1.0) };
|
|
|
|
V2F_C4B_T2F b = {Vec2(pos.x - radius, pos.y + radius), Color4B(color), Tex2F(-1.0, 1.0) };
|
|
|
|
V2F_C4B_T2F c = {Vec2(pos.x + radius, pos.y + radius), Color4B(color), Tex2F( 1.0, 1.0) };
|
|
|
|
V2F_C4B_T2F d = {Vec2(pos.x + radius, pos.y - radius), Color4B(color), Tex2F( 1.0, -1.0) };
|
|
|
|
|
|
|
|
V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
|
2013-07-05 16:49:22 +08:00
|
|
|
V2F_C4B_T2F_Triangle triangle0 = {a, b, c};
|
|
|
|
V2F_C4B_T2F_Triangle triangle1 = {a, c, d};
|
2015-01-08 10:22:45 +08:00
|
|
|
triangles[0] = triangle0;
|
|
|
|
triangles[1] = triangle1;
|
|
|
|
|
|
|
|
_bufferCount += vertex_count;
|
|
|
|
|
|
|
|
_dirty = true;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2015-01-07 11:44:16 +08:00
|
|
|
void DrawNode::drawRect(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, const Vec2& p4, const Color4F &color)
|
2014-09-11 15:39:56 +08:00
|
|
|
{
|
2015-01-07 11:44:16 +08:00
|
|
|
drawLine(Vec2(p1.x, p1.y), Vec2(p2.x, p2.y), color);
|
|
|
|
drawLine(Vec2(p2.x, p2.y), Vec2(p3.x, p3.y), color);
|
|
|
|
drawLine(Vec2(p3.x, p3.y), Vec2(p4.x, p4.y), color);
|
|
|
|
drawLine(Vec2(p4.x, p4.y), Vec2(p1.x, p1.y), color);
|
2014-09-11 15:39:56 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void DrawNode::drawSegment(const Vec2 &from, const Vec2 &to, float radius, const Color4F &color)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
|
|
|
unsigned int vertex_count = 6*3;
|
|
|
|
ensureCapacity(vertex_count);
|
2015-01-08 10:22:45 +08:00
|
|
|
|
|
|
|
Vec2 a = __v2f(from);
|
|
|
|
Vec2 b = __v2f(to);
|
|
|
|
|
|
|
|
|
|
|
|
Vec2 n = v2fnormalize(v2fperp(v2fsub(b, a)));
|
|
|
|
Vec2 t = v2fperp(n);
|
|
|
|
|
|
|
|
Vec2 nw = v2fmult(n, radius);
|
|
|
|
Vec2 tw = v2fmult(t, radius);
|
|
|
|
Vec2 v0 = v2fsub(b, v2fadd(nw, tw));
|
|
|
|
Vec2 v1 = v2fadd(b, v2fsub(nw, tw));
|
|
|
|
Vec2 v2 = v2fsub(b, nw);
|
|
|
|
Vec2 v3 = v2fadd(b, nw);
|
|
|
|
Vec2 v4 = v2fsub(a, nw);
|
|
|
|
Vec2 v5 = v2fadd(a, nw);
|
|
|
|
Vec2 v6 = v2fsub(a, v2fsub(nw, tw));
|
|
|
|
Vec2 v7 = v2fadd(a, v2fadd(nw, tw));
|
|
|
|
|
|
|
|
|
|
|
|
V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
V2F_C4B_T2F_Triangle triangles0 = {
|
2013-07-08 15:15:22 +08:00
|
|
|
{v0, Color4B(color), __t(v2fneg(v2fadd(n, t)))},
|
|
|
|
{v1, Color4B(color), __t(v2fsub(n, t))},
|
|
|
|
{v2, Color4B(color), __t(v2fneg(n))},
|
2012-11-16 17:08:34 +08:00
|
|
|
};
|
2015-01-08 10:22:45 +08:00
|
|
|
triangles[0] = triangles0;
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
V2F_C4B_T2F_Triangle triangles1 = {
|
2013-07-08 15:15:22 +08:00
|
|
|
{v3, Color4B(color), __t(n)},
|
|
|
|
{v1, Color4B(color), __t(v2fsub(n, t))},
|
|
|
|
{v2, Color4B(color), __t(v2fneg(n))},
|
2012-11-16 17:08:34 +08:00
|
|
|
};
|
2015-01-08 10:22:45 +08:00
|
|
|
triangles[1] = triangles1;
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
V2F_C4B_T2F_Triangle triangles2 = {
|
2013-07-08 15:15:22 +08:00
|
|
|
{v3, Color4B(color), __t(n)},
|
|
|
|
{v4, Color4B(color), __t(v2fneg(n))},
|
|
|
|
{v2, Color4B(color), __t(v2fneg(n))},
|
2012-11-16 17:08:34 +08:00
|
|
|
};
|
2015-01-08 10:22:45 +08:00
|
|
|
triangles[2] = triangles2;
|
2012-11-16 17:08:34 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
V2F_C4B_T2F_Triangle triangles3 = {
|
2013-07-08 15:15:22 +08:00
|
|
|
{v3, Color4B(color), __t(n)},
|
|
|
|
{v4, Color4B(color), __t(v2fneg(n))},
|
|
|
|
{v5, Color4B(color), __t(n) },
|
2012-11-16 17:08:34 +08:00
|
|
|
};
|
|
|
|
triangles[3] = triangles3;
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
V2F_C4B_T2F_Triangle triangles4 = {
|
2013-07-08 15:15:22 +08:00
|
|
|
{v6, Color4B(color), __t(v2fsub(t, n))},
|
|
|
|
{v4, Color4B(color), __t(v2fneg(n)) },
|
|
|
|
{v5, Color4B(color), __t(n)},
|
2012-11-16 17:08:34 +08:00
|
|
|
};
|
2015-01-08 10:22:45 +08:00
|
|
|
triangles[4] = triangles4;
|
2012-11-16 17:08:34 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
V2F_C4B_T2F_Triangle triangles5 = {
|
2013-07-08 15:15:22 +08:00
|
|
|
{v6, Color4B(color), __t(v2fsub(t, n))},
|
|
|
|
{v7, Color4B(color), __t(v2fadd(n, t))},
|
|
|
|
{v5, Color4B(color), __t(n)},
|
2012-11-16 17:08:34 +08:00
|
|
|
};
|
2015-01-08 10:22:45 +08:00
|
|
|
triangles[5] = triangles5;
|
|
|
|
|
|
|
|
_bufferCount += vertex_count;
|
|
|
|
|
|
|
|
_dirty = true;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2014-09-11 15:39:56 +08:00
|
|
|
void DrawNode::drawPolygon(const Vec2 *verts, int count, const Color4F &fillColor, float borderWidth, const Color4F &borderColor)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
CCASSERT(count >= 0, "invalid count value");
|
2014-11-27 18:31:04 +08:00
|
|
|
|
2016-07-04 22:12:45 +08:00
|
|
|
bool outline = (borderColor.a > 0.0f && borderWidth > 0.0f);
|
2014-11-27 18:31:04 +08:00
|
|
|
|
|
|
|
auto triangle_count = outline ? (3*count - 2) : (count - 2);
|
|
|
|
auto vertex_count = 3*triangle_count;
|
2012-11-14 18:05:15 +08:00
|
|
|
ensureCapacity(vertex_count);
|
2014-11-27 18:31:04 +08:00
|
|
|
|
|
|
|
V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
|
|
|
|
V2F_C4B_T2F_Triangle *cursor = triangles;
|
|
|
|
|
|
|
|
for (int i = 0; i < count-2; i++)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-07-05 16:49:22 +08:00
|
|
|
V2F_C4B_T2F_Triangle tmp = {
|
2014-11-27 18:31:04 +08:00
|
|
|
{verts[0], Color4B(fillColor), __t(v2fzero)},
|
|
|
|
{verts[i+1], Color4B(fillColor), __t(v2fzero)},
|
|
|
|
{verts[i+2], Color4B(fillColor), __t(v2fzero)},
|
2012-11-16 17:08:34 +08:00
|
|
|
};
|
2014-11-27 18:31:04 +08:00
|
|
|
|
|
|
|
*cursor++ = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(outline)
|
|
|
|
{
|
|
|
|
struct ExtrudeVerts {Vec2 offset, n;};
|
|
|
|
struct ExtrudeVerts* extrude = (struct ExtrudeVerts*)malloc(sizeof(struct ExtrudeVerts)*count);
|
|
|
|
memset(extrude, 0, sizeof(struct ExtrudeVerts)*count);
|
|
|
|
|
|
|
|
for (int i = 0; i < count; i++)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2014-11-27 18:31:04 +08:00
|
|
|
Vec2 v0 = __v2f(verts[(i-1+count)%count]);
|
|
|
|
Vec2 v1 = __v2f(verts[i]);
|
|
|
|
Vec2 v2 = __v2f(verts[(i+1)%count]);
|
|
|
|
|
|
|
|
Vec2 n1 = v2fnormalize(v2fperp(v2fsub(v1, v0)));
|
|
|
|
Vec2 n2 = v2fnormalize(v2fperp(v2fsub(v2, v1)));
|
|
|
|
|
2016-07-04 22:12:45 +08:00
|
|
|
Vec2 offset = v2fmult(v2fadd(n1, n2), 1.0f / (v2fdot(n1, n2) + 1.0f));
|
2014-11-27 18:31:04 +08:00
|
|
|
struct ExtrudeVerts tmp = {offset, n2};
|
|
|
|
extrude[i] = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
int j = (i+1)%count;
|
|
|
|
Vec2 v0 = __v2f(verts[i]);
|
|
|
|
Vec2 v1 = __v2f(verts[j]);
|
|
|
|
|
|
|
|
Vec2 n0 = extrude[i].n;
|
|
|
|
|
|
|
|
Vec2 offset0 = extrude[i].offset;
|
|
|
|
Vec2 offset1 = extrude[j].offset;
|
|
|
|
|
|
|
|
Vec2 inner0 = v2fsub(v0, v2fmult(offset0, borderWidth));
|
|
|
|
Vec2 inner1 = v2fsub(v1, v2fmult(offset1, borderWidth));
|
|
|
|
Vec2 outer0 = v2fadd(v0, v2fmult(offset0, borderWidth));
|
|
|
|
Vec2 outer1 = v2fadd(v1, v2fmult(offset1, borderWidth));
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
V2F_C4B_T2F_Triangle tmp1 = {
|
2013-07-08 15:15:22 +08:00
|
|
|
{inner0, Color4B(borderColor), __t(v2fneg(n0))},
|
|
|
|
{inner1, Color4B(borderColor), __t(v2fneg(n0))},
|
|
|
|
{outer1, Color4B(borderColor), __t(n0)}
|
2012-11-16 17:08:34 +08:00
|
|
|
};
|
2014-11-27 18:31:04 +08:00
|
|
|
*cursor++ = tmp1;
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
V2F_C4B_T2F_Triangle tmp2 = {
|
2013-07-08 15:15:22 +08:00
|
|
|
{inner0, Color4B(borderColor), __t(v2fneg(n0))},
|
|
|
|
{outer0, Color4B(borderColor), __t(n0)},
|
|
|
|
{outer1, Color4B(borderColor), __t(n0)}
|
2012-11-16 17:08:34 +08:00
|
|
|
};
|
2014-11-27 18:31:04 +08:00
|
|
|
*cursor++ = tmp2;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(extrude);
|
|
|
|
}
|
|
|
|
|
|
|
|
_bufferCount += vertex_count;
|
|
|
|
|
|
|
|
_dirty = true;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2014-09-11 15:39:56 +08:00
|
|
|
void DrawNode::drawSolidRect(const Vec2 &origin, const Vec2 &destination, const Color4F &color)
|
|
|
|
{
|
|
|
|
Vec2 vertices[] = {
|
|
|
|
origin,
|
|
|
|
Vec2(destination.x, origin.y),
|
|
|
|
destination,
|
|
|
|
Vec2(origin.x, destination.y)
|
|
|
|
};
|
|
|
|
|
|
|
|
drawSolidPoly(vertices, 4, color );
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::drawSolidPoly(const Vec2 *poli, unsigned int numberOfPoints, const Color4F &color)
|
|
|
|
{
|
|
|
|
drawPolygon(poli, numberOfPoints, color, 0.0, Color4F(0.0, 0.0, 0.0, 0.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::drawSolidCircle(const Vec2& center, float radius, float angle, unsigned int segments, float scaleX, float scaleY, const Color4F &color)
|
|
|
|
{
|
|
|
|
const float coef = 2.0f * (float)M_PI/segments;
|
|
|
|
|
2014-09-12 11:12:20 +08:00
|
|
|
Vec2 *vertices = new (std::nothrow) Vec2[segments];
|
2014-09-11 15:39:56 +08:00
|
|
|
if( ! vertices )
|
|
|
|
return;
|
|
|
|
|
2014-09-12 11:12:20 +08:00
|
|
|
for(unsigned int i = 0;i < segments; i++)
|
2014-09-11 15:39:56 +08:00
|
|
|
{
|
|
|
|
float rads = i*coef;
|
|
|
|
GLfloat j = radius * cosf(rads + angle) * scaleX + center.x;
|
|
|
|
GLfloat k = radius * sinf(rads + angle) * scaleY + center.y;
|
|
|
|
|
|
|
|
vertices[i].x = j;
|
|
|
|
vertices[i].y = k;
|
|
|
|
}
|
|
|
|
|
|
|
|
drawSolidPoly(vertices, segments, color);
|
|
|
|
|
2014-09-12 11:12:20 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(vertices);
|
2014-09-11 15:39:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::drawSolidCircle( const Vec2& center, float radius, float angle, unsigned int segments, const Color4F& color)
|
|
|
|
{
|
|
|
|
drawSolidCircle(center, radius, angle, segments, 1.0f, 1.0f, color);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void DrawNode::drawTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, const Color4F &color)
|
2013-10-14 11:41:55 +08:00
|
|
|
{
|
2014-09-11 15:39:56 +08:00
|
|
|
unsigned int vertex_count = 3;
|
2013-10-14 11:41:55 +08:00
|
|
|
ensureCapacity(vertex_count);
|
|
|
|
|
|
|
|
Color4B col = Color4B(color);
|
2014-05-15 01:07:09 +08:00
|
|
|
V2F_C4B_T2F a = {Vec2(p1.x, p1.y), col, Tex2F(0.0, 0.0) };
|
|
|
|
V2F_C4B_T2F b = {Vec2(p2.x, p2.y), col, Tex2F(0.0, 0.0) };
|
|
|
|
V2F_C4B_T2F c = {Vec2(p3.x, p3.y), col, Tex2F(0.0, 0.0) };
|
2013-10-14 11:41:55 +08:00
|
|
|
|
|
|
|
V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
|
|
|
|
V2F_C4B_T2F_Triangle triangle = {a, b, c};
|
|
|
|
triangles[0] = triangle;
|
|
|
|
|
|
|
|
_bufferCount += vertex_count;
|
|
|
|
_dirty = true;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void DrawNode::drawQuadraticBezier(const Vec2& from, const Vec2& control, const Vec2& to, unsigned int segments, const Color4F &color)
|
2013-10-14 11:41:55 +08:00
|
|
|
{
|
2014-09-11 15:39:56 +08:00
|
|
|
drawQuadBezier(from, control, to, segments, color);
|
2013-10-14 11:41:55 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void DrawNode::clear()
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_bufferCount = 0;
|
|
|
|
_dirty = true;
|
2014-09-11 15:39:56 +08:00
|
|
|
_bufferCountGLLine = 0;
|
|
|
|
_dirtyGLLine = true;
|
|
|
|
_bufferCountGLPoint = 0;
|
|
|
|
_dirtyGLPoint = true;
|
2015-11-25 23:26:57 +08:00
|
|
|
_lineWidth = _defaultLineWidth;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
const BlendFunc& DrawNode::getBlendFunc() const
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _blendFunc;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void DrawNode::setBlendFunc(const BlendFunc &blendFunc)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_blendFunc = blendFunc;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2015-07-07 17:47:20 +08:00
|
|
|
void DrawNode::setLineWidth(int lineWidth)
|
|
|
|
{
|
|
|
|
_lineWidth = lineWidth;
|
|
|
|
}
|
|
|
|
|
2015-11-13 09:09:02 +08:00
|
|
|
float DrawNode::getLineWidth()
|
2015-11-12 09:49:49 +08:00
|
|
|
{
|
|
|
|
return this->_lineWidth;
|
|
|
|
}
|
|
|
|
|
2012-11-09 12:08:18 +08:00
|
|
|
NS_CC_END
|