2012-11-09 12:08:18 +08:00
|
|
|
/* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
|
|
|
|
* Copyright (c) 2012 cocos2d-x.org
|
2014-01-07 11:25:07 +08:00
|
|
|
* Copyright (c) 2013-2014 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-05-01 10:09:13 +08:00
|
|
|
#include "2d/CCShaderCache.h"
|
2012-11-16 14:23:14 +08:00
|
|
|
#include "CCGL.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/CCCustomCommand.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCEventListenerCustom.h"
|
|
|
|
#include "base/CCEventDispatcher.h"
|
2012-11-09 12:08:18 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-04-02 15:25:37 +08:00
|
|
|
// Vector2 == 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-04-02 15:25:37 +08:00
|
|
|
static Vector2 v2fzero(0.0f,0.0f);
|
2012-11-09 12:08:18 +08:00
|
|
|
|
2014-04-02 15:25:37 +08:00
|
|
|
static inline Vector2 v2f(float x, float y)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
2014-04-02 15:25:37 +08:00
|
|
|
Vector2 ret(x, y);
|
2012-11-16 17:08:34 +08:00
|
|
|
return ret;
|
2012-11-09 12:08:18 +08:00
|
|
|
}
|
|
|
|
|
2014-04-02 15:25:37 +08:00
|
|
|
static inline Vector2 v2fadd(const Vector2 &v0, const Vector2 &v1)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
|
|
|
return v2f(v0.x+v1.x, v0.y+v1.y);
|
|
|
|
}
|
|
|
|
|
2014-04-02 15:25:37 +08:00
|
|
|
static inline Vector2 v2fsub(const Vector2 &v0, const Vector2 &v1)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
|
|
|
return v2f(v0.x-v1.x, v0.y-v1.y);
|
|
|
|
}
|
|
|
|
|
2014-04-02 15:25:37 +08:00
|
|
|
static inline Vector2 v2fmult(const Vector2 &v, float s)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
|
|
|
return v2f(v.x * s, v.y * s);
|
|
|
|
}
|
|
|
|
|
2014-04-02 15:25:37 +08:00
|
|
|
static inline Vector2 v2fperp(const Vector2 &p0)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
|
|
|
return v2f(-p0.y, p0.x);
|
|
|
|
}
|
|
|
|
|
2014-04-02 15:25:37 +08:00
|
|
|
static inline Vector2 v2fneg(const Vector2 &p0)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
|
|
|
return v2f(-p0.x, - p0.y);
|
|
|
|
}
|
|
|
|
|
2014-04-02 15:25:37 +08:00
|
|
|
static inline float v2fdot(const Vector2 &p0, const Vector2 &p1)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
|
|
|
return p0.x * p1.x + p0.y * p1.y;
|
|
|
|
}
|
|
|
|
|
2014-04-02 15:25:37 +08:00
|
|
|
static inline Vector2 v2fforangle(float _a_)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
|
|
|
return v2f(cosf(_a_), sinf(_a_));
|
|
|
|
}
|
|
|
|
|
2014-04-02 15:25:37 +08:00
|
|
|
static inline Vector2 v2fnormalize(const Vector2 &p)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
2014-05-01 01:10:18 +08:00
|
|
|
Vector2 r = Vector2(p.x, p.y).getNormalized();
|
2012-11-09 12:08:18 +08:00
|
|
|
return v2f(r.x, r.y);
|
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
static inline Vector2 __v2f(const Vector2 &v)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
2012-11-20 16:31:33 +08:00
|
|
|
//#ifdef __LP64__
|
2012-11-09 12:08:18 +08:00
|
|
|
return v2f(v.x, v.y);
|
2012-11-20 16:31:33 +08:00
|
|
|
// #else
|
2014-04-02 15:25:37 +08:00
|
|
|
// return * ((Vector2*) &v);
|
2012-11-20 16:31:33 +08:00
|
|
|
// #endif
|
2012-11-09 12:08:18 +08:00
|
|
|
}
|
|
|
|
|
2014-04-02 15:25:37 +08:00
|
|
|
static inline Tex2F __t(const Vector2 &v)
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
2013-07-05 16:49:22 +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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
DrawNode::DrawNode()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _vao(0)
|
|
|
|
, _vbo(0)
|
|
|
|
, _bufferCapacity(0)
|
|
|
|
, _bufferCount(0)
|
2013-12-18 17:47:20 +08:00
|
|
|
, _buffer(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _dirty(false)
|
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;
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glDeleteBuffers(1, &_vbo);
|
|
|
|
_vbo = 0;
|
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
|
|
|
glDeleteVertexArrays(1, &_vao);
|
|
|
|
GL::bindVAO(0);
|
|
|
|
_vao = 0;
|
|
|
|
}
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
DrawNode* DrawNode::create()
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
DrawNode* ret = new DrawNode();
|
|
|
|
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
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_bufferCapacity += MAX(_bufferCapacity, count);
|
2013-07-05 16:49:22 +08:00
|
|
|
_buffer = (V2F_C4B_T2F*)realloc(_buffer, _bufferCapacity*sizeof(V2F_C4B_T2F));
|
2012-11-14 18:05:15 +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
|
|
|
|
2013-09-16 20:38:03 +08:00
|
|
|
setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR));
|
2012-11-09 12:08:18 +08:00
|
|
|
|
|
|
|
ensureCapacity(512);
|
|
|
|
|
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);
|
|
|
|
}
|
2013-07-17 13:55:31 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glGenBuffers(1, &_vbo);
|
|
|
|
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);
|
2012-11-09 12:08:18 +08:00
|
|
|
|
2013-07-25 17:48:22 +08:00
|
|
|
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));
|
2012-11-09 12:08:18 +08:00
|
|
|
|
2013-07-25 17:48:22 +08:00
|
|
|
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));
|
2012-11-09 12:08:18 +08:00
|
|
|
|
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, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
|
2012-11-09 12:08:18 +08:00
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
2012-11-16 14:23:14 +08:00
|
|
|
|
2013-11-13 14:52:16 +08:00
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
2013-11-12 20:18:04 +08:00
|
|
|
GL::bindVAO(0);
|
|
|
|
}
|
2012-11-09 12:08:18 +08:00
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = 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
|
2013-12-31 10:54:37 +08:00
|
|
|
auto listener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom* event){
|
|
|
|
/** listen the event that coming to foreground on Android */
|
|
|
|
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-04-08 22:07:35 +08:00
|
|
|
void DrawNode::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2014-03-06 07:49:08 +08:00
|
|
|
_customCommand.init(_globalZOrder);
|
|
|
|
_customCommand.func = CC_CALLBACK_0(DrawNode::onDraw, this, transform, transformUpdated);
|
|
|
|
renderer->addCommand(&_customCommand);
|
|
|
|
}
|
|
|
|
|
2014-04-09 09:59:56 +08:00
|
|
|
void DrawNode::onDraw(const Matrix &transform, bool transformUpdated)
|
2014-03-06 07:49:08 +08:00
|
|
|
{
|
|
|
|
getShaderProgram()->use();
|
|
|
|
getShaderProgram()->setUniformsForBuiltins(transform);
|
|
|
|
|
|
|
|
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);
|
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));
|
2014-03-06 07:49:08 +08:00
|
|
|
|
2013-11-12 20:18:04 +08:00
|
|
|
// color
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
|
2014-03-06 07:49:08 +08:00
|
|
|
|
2013-11-12 20:18:04 +08:00
|
|
|
// texcood
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
|
|
|
|
}
|
2012-11-20 16:31:33 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glDrawArrays(GL_TRIANGLES, 0, _bufferCount);
|
2012-11-16 14:23:14 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
2014-03-06 07:49:08 +08:00
|
|
|
|
2014-02-08 11:37:44 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,_bufferCount);
|
2012-11-14 18:05:15 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
void DrawNode::drawDot(const Vector2 &pos, float radius, const Color4F &color)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
|
|
|
unsigned int vertex_count = 2*3;
|
|
|
|
ensureCapacity(vertex_count);
|
|
|
|
|
2014-04-02 15:25:37 +08:00
|
|
|
V2F_C4B_T2F a = {Vector2(pos.x - radius, pos.y - radius), Color4B(color), Tex2F(-1.0, -1.0) };
|
|
|
|
V2F_C4B_T2F b = {Vector2(pos.x - radius, pos.y + radius), Color4B(color), Tex2F(-1.0, 1.0) };
|
|
|
|
V2F_C4B_T2F c = {Vector2(pos.x + radius, pos.y + radius), Color4B(color), Tex2F( 1.0, 1.0) };
|
|
|
|
V2F_C4B_T2F d = {Vector2(pos.x + radius, pos.y - radius), Color4B(color), Tex2F( 1.0, -1.0) };
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
|
|
|
|
V2F_C4B_T2F_Triangle triangle0 = {a, b, c};
|
|
|
|
V2F_C4B_T2F_Triangle triangle1 = {a, c, d};
|
2012-11-16 17:08:34 +08:00
|
|
|
triangles[0] = triangle0;
|
|
|
|
triangles[1] = triangle1;
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_bufferCount += vertex_count;
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = true;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
void DrawNode::drawSegment(const Vector2 &from, const Vector2 &to, float radius, const Color4F &color)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
|
|
|
unsigned int vertex_count = 6*3;
|
|
|
|
ensureCapacity(vertex_count);
|
|
|
|
|
2014-04-02 15:25:37 +08:00
|
|
|
Vector2 a = __v2f(from);
|
|
|
|
Vector2 b = __v2f(to);
|
2012-11-14 18:05:15 +08:00
|
|
|
|
|
|
|
|
2014-04-02 15:25:37 +08:00
|
|
|
Vector2 n = v2fnormalize(v2fperp(v2fsub(b, a)));
|
|
|
|
Vector2 t = v2fperp(n);
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2014-04-02 15:25:37 +08:00
|
|
|
Vector2 nw = v2fmult(n, radius);
|
|
|
|
Vector2 tw = v2fmult(t, radius);
|
|
|
|
Vector2 v0 = v2fsub(b, v2fadd(nw, tw));
|
|
|
|
Vector2 v1 = v2fadd(b, v2fsub(nw, tw));
|
|
|
|
Vector2 v2 = v2fsub(b, nw);
|
|
|
|
Vector2 v3 = v2fadd(b, nw);
|
|
|
|
Vector2 v4 = v2fsub(a, nw);
|
|
|
|
Vector2 v5 = v2fadd(a, nw);
|
|
|
|
Vector2 v6 = v2fsub(a, v2fsub(nw, tw));
|
|
|
|
Vector2 v7 = v2fadd(a, v2fadd(nw, tw));
|
2012-11-14 18:05:15 +08:00
|
|
|
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
|
2012-11-14 18:05:15 +08:00
|
|
|
|
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
|
|
|
};
|
|
|
|
triangles[0] = triangles0;
|
2012-11-14 18:05:15 +08:00
|
|
|
|
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
|
|
|
};
|
|
|
|
triangles[1] = triangles1;
|
2012-11-14 18:05:15 +08:00
|
|
|
|
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
|
|
|
};
|
|
|
|
triangles[2] = triangles2;
|
|
|
|
|
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
|
|
|
};
|
|
|
|
triangles[4] = triangles4;
|
|
|
|
|
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
|
|
|
};
|
|
|
|
triangles[5] = triangles5;
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_bufferCount += vertex_count;
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = true;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
void DrawNode::drawPolygon(Vector2 *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-04-02 15:25:37 +08:00
|
|
|
struct ExtrudeVerts {Vector2 offset, n;};
|
2012-11-16 17:08:34 +08:00
|
|
|
struct ExtrudeVerts* extrude = (struct ExtrudeVerts*)malloc(sizeof(struct ExtrudeVerts)*count);
|
|
|
|
memset(extrude, 0, sizeof(struct ExtrudeVerts)*count);
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
for (int i = 0; i < count; i++)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2014-04-02 15:25:37 +08:00
|
|
|
Vector2 v0 = __v2f(verts[(i-1+count)%count]);
|
|
|
|
Vector2 v1 = __v2f(verts[i]);
|
|
|
|
Vector2 v2 = __v2f(verts[(i+1)%count]);
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2014-04-02 15:25:37 +08:00
|
|
|
Vector2 n1 = v2fnormalize(v2fperp(v2fsub(v1, v0)));
|
|
|
|
Vector2 n2 = v2fnormalize(v2fperp(v2fsub(v2, v1)));
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2014-04-02 15:25:37 +08:00
|
|
|
Vector2 offset = v2fmult(v2fadd(n1, n2), 1.0/(v2fdot(n1, n2) + 1.0));
|
2012-11-16 17:08:34 +08:00
|
|
|
struct ExtrudeVerts tmp = {offset, n2};
|
|
|
|
extrude[i] = tmp;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2013-06-03 22:19:37 +08:00
|
|
|
bool outline = (borderColor.a > 0.0 && borderWidth > 0.0);
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
auto triangle_count = 3*count - 2;
|
|
|
|
auto vertex_count = 3*triangle_count;
|
2012-11-14 18:05:15 +08:00
|
|
|
ensureCapacity(vertex_count);
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
|
|
|
|
V2F_C4B_T2F_Triangle *cursor = triangles;
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2013-08-10 10:36:50 +08:00
|
|
|
float inset = (outline == false ? 0.5 : 0.0);
|
2013-12-05 17:19:01 +08:00
|
|
|
for (int i = 0; i < count-2; i++)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2014-04-02 15:25:37 +08:00
|
|
|
Vector2 v0 = v2fsub(__v2f(verts[0 ]), v2fmult(extrude[0 ].offset, inset));
|
|
|
|
Vector2 v1 = v2fsub(__v2f(verts[i+1]), v2fmult(extrude[i+1].offset, inset));
|
|
|
|
Vector2 v2 = v2fsub(__v2f(verts[i+2]), v2fmult(extrude[i+2].offset, inset));
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
V2F_C4B_T2F_Triangle tmp = {
|
2013-07-08 15:15:22 +08:00
|
|
|
{v0, Color4B(fillColor), __t(v2fzero)},
|
|
|
|
{v1, Color4B(fillColor), __t(v2fzero)},
|
|
|
|
{v2, Color4B(fillColor), __t(v2fzero)},
|
2012-11-16 17:08:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
*cursor++ = tmp;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
for(int i = 0; i < count; i++)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-12-05 17:19:01 +08:00
|
|
|
int j = (i+1)%count;
|
2014-04-02 15:25:37 +08:00
|
|
|
Vector2 v0 = __v2f(verts[i]);
|
|
|
|
Vector2 v1 = __v2f(verts[j]);
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2014-04-02 15:25:37 +08:00
|
|
|
Vector2 n0 = extrude[i].n;
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2014-04-02 15:25:37 +08:00
|
|
|
Vector2 offset0 = extrude[i].offset;
|
|
|
|
Vector2 offset1 = extrude[j].offset;
|
2012-11-14 18:05:15 +08:00
|
|
|
|
|
|
|
if(outline)
|
|
|
|
{
|
2014-04-02 15:25:37 +08:00
|
|
|
Vector2 inner0 = v2fsub(v0, v2fmult(offset0, borderWidth));
|
|
|
|
Vector2 inner1 = v2fsub(v1, v2fmult(offset1, borderWidth));
|
|
|
|
Vector2 outer0 = v2fadd(v0, v2fmult(offset0, borderWidth));
|
|
|
|
Vector2 outer1 = v2fadd(v1, v2fmult(offset1, borderWidth));
|
2012-11-14 18:05:15 +08:00
|
|
|
|
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
|
|
|
};
|
|
|
|
*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
|
|
|
};
|
|
|
|
*cursor++ = tmp2;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
else {
|
2014-04-02 15:25:37 +08:00
|
|
|
Vector2 inner0 = v2fsub(v0, v2fmult(offset0, 0.5));
|
|
|
|
Vector2 inner1 = v2fsub(v1, v2fmult(offset1, 0.5));
|
|
|
|
Vector2 outer0 = v2fadd(v0, v2fmult(offset0, 0.5));
|
|
|
|
Vector2 outer1 = v2fadd(v1, v2fmult(offset1, 0.5));
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
V2F_C4B_T2F_Triangle tmp1 = {
|
2013-07-08 15:15:22 +08:00
|
|
|
{inner0, Color4B(fillColor), __t(v2fzero)},
|
|
|
|
{inner1, Color4B(fillColor), __t(v2fzero)},
|
|
|
|
{outer1, Color4B(fillColor), __t(n0)}
|
2012-11-16 17:08:34 +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(fillColor), __t(v2fzero)},
|
|
|
|
{outer0, Color4B(fillColor), __t(n0)},
|
|
|
|
{outer1, Color4B(fillColor), __t(n0)}
|
2012-11-16 17:08:34 +08:00
|
|
|
};
|
|
|
|
*cursor++ = tmp2;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_bufferCount += vertex_count;
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_dirty = true;
|
2012-11-20 16:31:33 +08:00
|
|
|
|
|
|
|
free(extrude);
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
void DrawNode::drawTriangle(const Vector2 &p1, const Vector2 &p2, const Vector2 &p3, const Color4F &color)
|
2013-10-14 11:41:55 +08:00
|
|
|
{
|
|
|
|
unsigned int vertex_count = 2*3;
|
|
|
|
ensureCapacity(vertex_count);
|
|
|
|
|
|
|
|
Color4B col = Color4B(color);
|
2014-04-02 15:25:37 +08:00
|
|
|
V2F_C4B_T2F a = {Vector2(p1.x, p1.y), col, Tex2F(0.0, 0.0) };
|
|
|
|
V2F_C4B_T2F b = {Vector2(p2.x, p2.y), col, Tex2F(0.0, 0.0) };
|
|
|
|
V2F_C4B_T2F c = {Vector2(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-04-15 18:13:57 +08:00
|
|
|
void DrawNode::drawCubicBezier(const Vector2& from, const Vector2& control1, const Vector2& control2, const Vector2& to, unsigned int segments, const Color4F &color)
|
2013-10-14 11:41:55 +08:00
|
|
|
{
|
|
|
|
unsigned int vertex_count = (segments + 1) * 3;
|
|
|
|
ensureCapacity(vertex_count);
|
|
|
|
|
|
|
|
Tex2F texCoord = Tex2F(0.0, 0.0);
|
|
|
|
Color4B col = Color4B(color);
|
2014-04-02 15:25:37 +08:00
|
|
|
Vector2 vertex;
|
|
|
|
Vector2 firstVertex = Vector2(from.x, from.y);
|
|
|
|
Vector2 lastVertex = Vector2(to.x, to.y);
|
2013-10-14 11:41:55 +08:00
|
|
|
|
|
|
|
float t = 0;
|
|
|
|
for(unsigned int i = segments + 1; i > 0; i--)
|
|
|
|
{
|
|
|
|
float x = powf(1 - t, 3) * from.x + 3.0f * powf(1 - t, 2) * t * control1.x + 3.0f * (1 - t) * t * t * control2.x + t * t * t * to.x;
|
|
|
|
float y = powf(1 - t, 3) * from.y + 3.0f * powf(1 - t, 2) * t * control1.y + 3.0f * (1 - t) * t * t * control2.y + t * t * t * to.y;
|
2014-04-02 15:25:37 +08:00
|
|
|
vertex = Vector2(x, y);
|
2013-10-14 11:41:55 +08:00
|
|
|
|
|
|
|
V2F_C4B_T2F a = {firstVertex, col, texCoord };
|
|
|
|
V2F_C4B_T2F b = {lastVertex, col, texCoord };
|
|
|
|
V2F_C4B_T2F c = {vertex, col, texCoord };
|
|
|
|
V2F_C4B_T2F_Triangle triangle = {a, b, c};
|
|
|
|
((V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount))[0] = triangle;
|
|
|
|
|
|
|
|
lastVertex = vertex;
|
|
|
|
t += 1.0f / segments;
|
|
|
|
_bufferCount += 3;
|
|
|
|
}
|
|
|
|
_dirty = true;
|
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
void DrawNode::drawQuadraticBezier(const Vector2& from, const Vector2& control, const Vector2& to, unsigned int segments, const Color4F &color)
|
2013-10-14 11:41:55 +08:00
|
|
|
{
|
|
|
|
unsigned int vertex_count = (segments + 1) * 3;
|
|
|
|
ensureCapacity(vertex_count);
|
|
|
|
|
|
|
|
Tex2F texCoord = Tex2F(0.0, 0.0);
|
|
|
|
Color4B col = Color4B(color);
|
2014-04-02 15:25:37 +08:00
|
|
|
Vector2 vertex;
|
|
|
|
Vector2 firstVertex = Vector2(from.x, from.y);
|
|
|
|
Vector2 lastVertex = Vector2(to.x, to.y);
|
2013-10-14 11:41:55 +08:00
|
|
|
|
|
|
|
float t = 0;
|
|
|
|
for(unsigned int i = segments + 1; i > 0; i--)
|
|
|
|
{
|
|
|
|
float x = powf(1 - t, 2) * from.x + 2.0f * (1 - t) * t * control.x + t * t * to.x;
|
|
|
|
float y = powf(1 - t, 2) * from.y + 2.0f * (1 - t) * t * control.y + t * t * to.y;
|
2014-04-02 15:25:37 +08:00
|
|
|
vertex = Vector2(x, y);
|
2013-10-14 11:41:55 +08:00
|
|
|
|
|
|
|
V2F_C4B_T2F a = {firstVertex, col, texCoord };
|
|
|
|
V2F_C4B_T2F b = {lastVertex, col, texCoord };
|
|
|
|
V2F_C4B_T2F c = {vertex, col, texCoord };
|
|
|
|
V2F_C4B_T2F_Triangle triangle = {a, b, c};
|
|
|
|
((V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount))[0] = triangle;
|
|
|
|
|
|
|
|
lastVertex = vertex;
|
|
|
|
t += 1.0f / segments;
|
|
|
|
_bufferCount += 3;
|
|
|
|
}
|
|
|
|
_dirty = true;
|
|
|
|
}
|
|
|
|
|
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;
|
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
|
|
|
}
|
|
|
|
|
2012-11-09 12:08:18 +08:00
|
|
|
NS_CC_END
|