axmol/cocos2dx/draw_nodes/CCDrawNode.cpp

444 lines
12 KiB
C++
Raw Normal View History

2012-11-09 12:08:18 +08:00
/* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
* Copyright (c) 2012 cocos2d-x.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "CCDrawNode.h"
#include "support/CCPointExtension.h"
#include "shaders/CCShaderCache.h"
2012-11-16 14:23:14 +08:00
#include "CCGL.h"
2012-11-09 12:08:18 +08:00
NS_CC_BEGIN
// Vertex2F == 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
static Vertex2F v2fzero(0.0f,0.0f);
2012-11-09 12:08:18 +08:00
static inline Vertex2F v2f(float x, float y)
2012-11-09 12:08:18 +08:00
{
Vertex2F ret(x, y);
2012-11-16 17:08:34 +08:00
return ret;
2012-11-09 12:08:18 +08:00
}
static inline Vertex2F v2fadd(const Vertex2F &v0, const Vertex2F &v1)
2012-11-09 12:08:18 +08:00
{
return v2f(v0.x+v1.x, v0.y+v1.y);
}
static inline Vertex2F v2fsub(const Vertex2F &v0, const Vertex2F &v1)
2012-11-09 12:08:18 +08:00
{
return v2f(v0.x-v1.x, v0.y-v1.y);
}
static inline Vertex2F v2fmult(const Vertex2F &v, float s)
2012-11-09 12:08:18 +08:00
{
return v2f(v.x * s, v.y * s);
}
static inline Vertex2F v2fperp(const Vertex2F &p0)
2012-11-09 12:08:18 +08:00
{
return v2f(-p0.y, p0.x);
}
static inline Vertex2F v2fneg(const Vertex2F &p0)
2012-11-09 12:08:18 +08:00
{
return v2f(-p0.x, - p0.y);
}
static inline float v2fdot(const Vertex2F &p0, const Vertex2F &p1)
2012-11-09 12:08:18 +08:00
{
return p0.x * p1.x + p0.y * p1.y;
}
static inline Vertex2F v2fforangle(float _a_)
2012-11-09 12:08:18 +08:00
{
return v2f(cosf(_a_), sinf(_a_));
}
static inline Vertex2F v2fnormalize(const Vertex2F &p)
2012-11-09 12:08:18 +08:00
{
Point r = ccp(p.x, p.y).normalize();
2012-11-09 12:08:18 +08:00
return v2f(r.x, r.y);
}
static inline Vertex2F __v2f(const Point &v)
2012-11-09 12:08:18 +08:00
{
//#ifdef __LP64__
2012-11-09 12:08:18 +08:00
return v2f(v.x, v.y);
// #else
// return * ((Vertex2F*) &v);
// #endif
2012-11-09 12:08:18 +08:00
}
static inline Tex2F __t(const Vertex2F &v)
2012-11-09 12:08:18 +08:00
{
return *(Tex2F*)&v;
2012-11-09 12:08:18 +08:00
}
// implementation of DrawNode
2012-11-09 12:08:18 +08:00
DrawNode::DrawNode()
: _vao(0)
, _vbo(0)
, _bufferCapacity(0)
, _bufferCount(0)
, _buffer(NULL)
, _dirty(false)
2012-11-16 17:08:34 +08:00
{
_blendFunc.src = CC_BLEND_SRC;
_blendFunc.dst = CC_BLEND_DST;
2012-11-16 17:08:34 +08:00
}
2012-11-09 12:08:18 +08:00
DrawNode::~DrawNode()
2012-11-14 18:05:15 +08:00
{
free(_buffer);
_buffer = NULL;
2012-11-14 18:05:15 +08:00
glDeleteBuffers(1, &_vbo);
_vbo = 0;
2012-11-14 18:05:15 +08:00
2012-11-16 14:23:14 +08:00
#if CC_TEXTURE_ATLAS_USE_VAO
glDeleteVertexArrays(1, &_vao);
_vao = 0;
2012-11-16 14:23:14 +08:00
#endif
2012-11-14 18:05:15 +08:00
}
DrawNode* DrawNode::create()
2012-11-09 12:08:18 +08:00
{
DrawNode* pRet = new DrawNode();
2012-11-09 12:08:18 +08:00
if (pRet && pRet->init())
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
void DrawNode::ensureCapacity(unsigned int count)
2012-11-14 18:05:15 +08:00
{
if(_bufferCount + count > _bufferCapacity)
2012-11-14 18:05:15 +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
}
}
bool DrawNode::init()
2012-11-09 12:08:18 +08:00
{
_blendFunc.src = CC_BLEND_SRC;
_blendFunc.dst = CC_BLEND_DST;
setShaderProgram(ShaderCache::sharedShaderCache()->programForKey(kShader_PositionLengthTexureColor));
2012-11-09 12:08:18 +08:00
ensureCapacity(512);
2012-11-16 14:23:14 +08:00
#if CC_TEXTURE_ATLAS_USE_VAO
glGenVertexArrays(1, &_vao);
ccGLBindVAO(_vao);
2012-11-16 14:23:14 +08:00
#endif
2012-11-09 12:08:18 +08:00
glGenBuffers(1, &_vbo);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)* _bufferCapacity, _buffer, GL_STREAM_DRAW);
2012-11-09 12:08:18 +08:00
glEnableVertexAttribArray(kVertexAttrib_Position);
glVertexAttribPointer(kVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
2012-11-09 12:08:18 +08:00
glEnableVertexAttribArray(kVertexAttrib_Color);
glVertexAttribPointer(kVertexAttrib_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
glEnableVertexAttribArray(kVertexAttrib_TexCoords);
glVertexAttribPointer(kVertexAttrib_TexCoords, 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
#if CC_TEXTURE_ATLAS_USE_VAO
2012-11-09 12:08:18 +08:00
ccGLBindVAO(0);
2012-11-16 14:23:14 +08:00
#endif
2012-11-09 12:08:18 +08:00
CHECK_GL_ERROR_DEBUG();
_dirty = true;
2012-11-09 12:08:18 +08:00
return true;
}
void DrawNode::render()
2012-11-14 18:05:15 +08:00
{
if (_dirty)
2012-11-14 18:05:15 +08:00
{
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacity, _buffer, GL_STREAM_DRAW);
_dirty = false;
2012-11-14 18:05:15 +08:00
}
2012-11-16 14:23:14 +08:00
#if CC_TEXTURE_ATLAS_USE_VAO
ccGLBindVAO(_vao);
2012-11-16 14:23:14 +08:00
#else
ccGLEnableVertexAttribs(kVertexAttribFlag_PosColorTex);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
2012-11-16 14:23:14 +08:00
// vertex
glVertexAttribPointer(kVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
2012-11-16 14:23:14 +08:00
// color
glVertexAttribPointer(kVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
2012-11-16 14:23:14 +08:00
// texcood
glVertexAttribPointer(kVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
2012-11-16 14:23:14 +08:00
#endif
glDrawArrays(GL_TRIANGLES, 0, _bufferCount);
2012-11-16 14:23:14 +08:00
glBindBuffer(GL_ARRAY_BUFFER, 0);
2012-11-14 18:05:15 +08:00
CC_INCREMENT_GL_DRAWS(1);
CHECK_GL_ERROR_DEBUG();
}
void DrawNode::draw()
2012-11-14 18:05:15 +08:00
{
ccGLBlendFunc(_blendFunc.src, _blendFunc.dst);
2012-11-14 18:05:15 +08:00
getShaderProgram()->use();
getShaderProgram()->setUniformsForBuiltins();
render();
}
void DrawNode::drawDot(const Point &pos, float radius, const Color4F &color)
2012-11-14 18:05:15 +08:00
{
unsigned int vertex_count = 2*3;
ensureCapacity(vertex_count);
2013-07-08 15:15:22 +08:00
V2F_C4B_T2F a = {Vertex2F(pos.x - radius, pos.y - radius), Color4B(color), Tex2F(-1.0, -1.0) };
V2F_C4B_T2F b = {Vertex2F(pos.x - radius, pos.y + radius), Color4B(color), Tex2F(-1.0, 1.0) };
V2F_C4B_T2F c = {Vertex2F(pos.x + radius, pos.y + radius), Color4B(color), Tex2F( 1.0, 1.0) };
V2F_C4B_T2F d = {Vertex2F(pos.x + radius, pos.y - radius), Color4B(color), Tex2F( 1.0, -1.0) };
2012-11-14 18:05:15 +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
_bufferCount += vertex_count;
2012-11-14 18:05:15 +08:00
_dirty = true;
2012-11-14 18:05:15 +08:00
}
void DrawNode::drawSegment(const Point &from, const Point &to, float radius, const Color4F &color)
2012-11-14 18:05:15 +08:00
{
unsigned int vertex_count = 6*3;
ensureCapacity(vertex_count);
Vertex2F a = __v2f(from);
Vertex2F b = __v2f(to);
2012-11-14 18:05:15 +08:00
Vertex2F n = v2fnormalize(v2fperp(v2fsub(b, a)));
Vertex2F t = v2fperp(n);
2012-11-14 18:05:15 +08:00
Vertex2F nw = v2fmult(n, radius);
Vertex2F tw = v2fmult(t, radius);
Vertex2F v0 = v2fsub(b, v2fadd(nw, tw));
Vertex2F v1 = v2fadd(b, v2fsub(nw, tw));
Vertex2F v2 = v2fsub(b, nw);
Vertex2F v3 = v2fadd(b, nw);
Vertex2F v4 = v2fsub(a, nw);
Vertex2F v5 = v2fadd(a, nw);
Vertex2F v6 = v2fsub(a, v2fsub(nw, tw));
Vertex2F v7 = v2fadd(a, v2fadd(nw, tw));
2012-11-14 18:05:15 +08:00
V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
2012-11-14 18:05:15 +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
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
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;
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;
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;
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
_bufferCount += vertex_count;
2012-11-14 18:05:15 +08:00
_dirty = true;
2012-11-14 18:05:15 +08:00
}
void DrawNode::drawPolygon(Point *verts, unsigned int count, const Color4F &fillColor, float borderWidth, const Color4F &borderColor)
2012-11-14 18:05:15 +08:00
{
struct ExtrudeVerts {Vertex2F 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
2012-11-29 16:31:29 +08:00
for(unsigned int i = 0; i < count; i++)
2012-11-14 18:05:15 +08:00
{
Vertex2F v0 = __v2f(verts[(i-1+count)%count]);
Vertex2F v1 = __v2f(verts[i]);
Vertex2F v2 = __v2f(verts[(i+1)%count]);
2012-11-14 18:05:15 +08:00
Vertex2F n1 = v2fnormalize(v2fperp(v2fsub(v1, v0)));
Vertex2F n2 = v2fnormalize(v2fperp(v2fsub(v2, v1)));
2012-11-14 18:05:15 +08:00
Vertex2F 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
}
bool outline = (borderColor.a > 0.0 && borderWidth > 0.0);
2012-11-14 18:05:15 +08:00
unsigned int triangle_count = 3*count - 2;
unsigned int vertex_count = 3*triangle_count;
ensureCapacity(vertex_count);
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
float inset = (outline == 0.0 ? 0.5 : 0.0);
2012-11-29 16:31:29 +08:00
for(unsigned int i = 0; i < count-2; i++)
2012-11-14 18:05:15 +08:00
{
Vertex2F v0 = v2fsub(__v2f(verts[0 ]), v2fmult(extrude[0 ].offset, inset));
Vertex2F v1 = v2fsub(__v2f(verts[i+1]), v2fmult(extrude[i+1].offset, inset));
Vertex2F v2 = v2fsub(__v2f(verts[i+2]), v2fmult(extrude[i+2].offset, inset));
2012-11-14 18:05:15 +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
}
2012-11-29 16:31:29 +08:00
for(unsigned int i = 0; i < count; i++)
2012-11-14 18:05:15 +08:00
{
int j = (i+1)%count;
Vertex2F v0 = __v2f(verts[i]);
Vertex2F v1 = __v2f(verts[j]);
2012-11-14 18:05:15 +08:00
Vertex2F n0 = extrude[i].n;
2012-11-14 18:05:15 +08:00
Vertex2F offset0 = extrude[i].offset;
Vertex2F offset1 = extrude[j].offset;
2012-11-14 18:05:15 +08:00
if(outline)
{
Vertex2F inner0 = v2fsub(v0, v2fmult(offset0, borderWidth));
Vertex2F inner1 = v2fsub(v1, v2fmult(offset1, borderWidth));
Vertex2F outer0 = v2fadd(v0, v2fmult(offset0, borderWidth));
Vertex2F outer1 = v2fadd(v1, v2fmult(offset1, borderWidth));
2012-11-14 18:05:15 +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;
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 {
Vertex2F inner0 = v2fsub(v0, v2fmult(offset0, 0.5));
Vertex2F inner1 = v2fsub(v1, v2fmult(offset1, 0.5));
Vertex2F outer0 = v2fadd(v0, v2fmult(offset0, 0.5));
Vertex2F outer1 = v2fadd(v1, v2fmult(offset1, 0.5));
2012-11-14 18:05:15 +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;
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
}
}
_bufferCount += vertex_count;
2012-11-14 18:05:15 +08:00
_dirty = true;
free(extrude);
2012-11-14 18:05:15 +08:00
}
void DrawNode::clear()
2012-11-14 18:05:15 +08:00
{
_bufferCount = 0;
_dirty = true;
2012-11-14 18:05:15 +08:00
}
const BlendFunc& DrawNode::getBlendFunc() const
2012-11-14 18:05:15 +08:00
{
return _blendFunc;
2012-11-14 18:05:15 +08:00
}
void DrawNode::setBlendFunc(const BlendFunc &blendFunc)
2012-11-14 18:05:15 +08:00
{
_blendFunc = blendFunc;
2012-11-14 18:05:15 +08:00
}
2012-11-09 12:08:18 +08:00
NS_CC_END