diff --git a/cocos/2d/CCDrawNode.cpp b/cocos/2d/CCDrawNode.cpp index 446b92b2b055..6dc5dbce2ef3 100644 --- a/cocos/2d/CCDrawNode.cpp +++ b/cocos/2d/CCDrawNode.cpp @@ -127,11 +127,11 @@ bool Triangulate::isInsideTriangle(float Ax, float Ay, apx= Px - Ax; apy= Py - Ay; bpx= Px - Bx; bpy= Py - By; cpx= Px - Cx; cpy= Py - Cy; - + aCROSSbp = ax*bpy - ay*bpx; cCROSSap = cx*apy - cy*apx; bCROSScp = bx*cpy - by*cpx; - + return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f)); } @@ -139,18 +139,18 @@ bool Triangulate::checkSnip(const Vec2 *verts,int u,int v,int w,int n,int *V) { int p; float Ax, Ay, Bx, By, Cx, Cy, Px, Py; - + Ax = verts[V[u]].x; Ay = verts[V[u]].y; - + Bx = verts[V[v]].x; By = verts[V[v]].y; - + Cx = verts[V[w]].x; Cy = verts[V[w]].y; - + if ( EPSILON > (((Bx-Ax)*(Cy-Ay)) - ((By-Ay)*(Cx-Ax))) ) return false; - + for (p=0;p V(n); /* we want a counter-clockwise polygon in V */ if ( 0.0f < computeArea(verts,n) ) + { for (int v=0; vsupportsShareableVAO()) { GL::bindVAO(0); @@ -267,14 +281,14 @@ DrawNode* DrawNode::create(GLfloat defaultLineWidth) { CC_SAFE_DELETE(ret); } - + return ret; } void DrawNode::ensureCapacity(int count) { CCASSERT(count>=0, "capacity must be >= 0"); - + if(_bufferCount + count > _bufferCapacity) { _bufferCapacity += MAX(_bufferCapacity, count); @@ -285,7 +299,7 @@ void DrawNode::ensureCapacity(int count) void DrawNode::ensureCapacityGLPoint(int count) { CCASSERT(count>=0, "capacity must be >= 0"); - + if(_bufferCountGLPoint + count > _bufferCapacityGLPoint) { _bufferCapacityGLPoint += MAX(_bufferCapacityGLPoint, count); @@ -296,7 +310,7 @@ void DrawNode::ensureCapacityGLPoint(int count) void DrawNode::ensureCapacityGLLine(int count) { CCASSERT(count>=0, "capacity must be >= 0"); - + if(_bufferCountGLLine + count > _bufferCapacityGLLine) { _bufferCapacityGLLine += MAX(_bufferCapacityGLLine, count); @@ -382,16 +396,16 @@ bool DrawNode::init() _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED; setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR)); - + ensureCapacity(512); ensureCapacityGLPoint(64); ensureCapacityGLLine(256); - + setupBuffer(); - + _dirty = true; _dirtyGLLine = true; - _dirtyGLPoint = true; + _dirtyGLPoint = true; return true; } @@ -403,14 +417,14 @@ void DrawNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) _customCommand.func = CC_CALLBACK_0(DrawNode::onDraw, this, transform, flags); renderer->addCommand(&_customCommand); } - + if(_bufferCountGLPoint) { _customCommandGLPoint.init(_globalZOrder, transform, flags); _customCommandGLPoint.func = CC_CALLBACK_0(DrawNode::onDrawGLPoint, this, transform, flags); renderer->addCommand(&_customCommandGLPoint); } - + if(_bufferCountGLLine) { _customCommandGLLine.init(_globalZOrder, transform, flags); @@ -430,7 +444,7 @@ void DrawNode::onDraw(const Mat4 &transform, uint32_t /*flags*/) { glBindBuffer(GL_ARRAY_BUFFER, _vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacity, _buffer, GL_STREAM_DRAW); - + _dirty = false; } if (Configuration::getInstance()->supportsShareableVAO()) @@ -452,12 +466,12 @@ void DrawNode::onDraw(const Mat4 &transform, uint32_t /*flags*/) glDrawArrays(GL_TRIANGLES, 0, _bufferCount); glBindBuffer(GL_ARRAY_BUFFER, 0); - + if (Configuration::getInstance()->supportsShareableVAO()) { GL::bindVAO(0); } - + CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _bufferCount); CHECK_GL_ERROR_DEBUG(); } @@ -495,12 +509,12 @@ void DrawNode::onDrawGLLine(const Mat4 &transform, uint32_t /*flags*/) glLineWidth(_lineWidth); glDrawArrays(GL_LINES, 0, _bufferCountGLLine); - + if (Configuration::getInstance()->supportsShareableVAO()) { GL::bindVAO(0); } - + glBindBuffer(GL_ARRAY_BUFFER, 0); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,_bufferCountGLLine); @@ -520,10 +534,10 @@ void DrawNode::onDrawGLPoint(const Mat4 &transform, uint32_t /*flags*/) { glBindBuffer(GL_ARRAY_BUFFER, _vboGLPoint); glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLPoint, _bufferGLPoint, GL_STREAM_DRAW); - + _dirtyGLPoint = false; } - + if (Configuration::getInstance()->supportsShareableVAO()) { GL::bindVAO(_vaoGLPoint); @@ -536,16 +550,16 @@ void DrawNode::onDrawGLPoint(const Mat4 &transform, uint32_t /*flags*/) glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords)); } - + glDrawArrays(GL_POINTS, 0, _bufferCountGLPoint); - + if (Configuration::getInstance()->supportsShareableVAO()) { GL::bindVAO(0); } - + glBindBuffer(GL_ARRAY_BUFFER, 0); - + CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,_bufferCountGLPoint); CHECK_GL_ERROR_DEBUG(); } @@ -553,11 +567,11 @@ void DrawNode::onDrawGLPoint(const Mat4 &transform, uint32_t /*flags*/) void DrawNode::drawPoint(const Vec2& position, const float pointSize, const Color4F &color) { ensureCapacityGLPoint(1); - + V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLPoint + _bufferCountGLPoint); V2F_C4B_T2F a = {position, Color4B(color), Tex2F(pointSize,0)}; *point = a; - + _bufferCountGLPoint += 1; _dirtyGLPoint = true; } @@ -570,15 +584,15 @@ void DrawNode::drawPoints(const Vec2 *position, unsigned int numberOfPoints, con void DrawNode::drawPoints(const Vec2 *position, unsigned int numberOfPoints, const float pointSize, const Color4F &color) { ensureCapacityGLPoint(numberOfPoints); - + V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLPoint + _bufferCountGLPoint); - + for(unsigned int i=0; i < numberOfPoints; i++,point++) { V2F_C4B_T2F a = {position[i], Color4B(color), Tex2F(pointSize,0)}; *point = a; } - + _bufferCountGLPoint += numberOfPoints; _dirtyGLPoint = true; } @@ -586,15 +600,15 @@ void DrawNode::drawPoints(const Vec2 *position, unsigned int numberOfPoints, con 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; } @@ -620,15 +634,15 @@ void DrawNode::drawPoly(const Vec2 *poli, unsigned int numberOfPoints, bool clos vertex_count = 2 * (numberOfPoints - 1); ensureCapacityGLLine(vertex_count); } - + V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLLine + _bufferCountGLLine); - + unsigned int i = 0; for(; icount(); - + for( unsigned int i=0; i < segments+1;i++) { - + float dt = (float)i / segments; - + // border if( dt == 1 ) { p = config->count() - 1; @@ -741,20 +755,20 @@ void DrawNode::drawCardinalSpline(PointArray *config, float tension, unsigned i 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); } @@ -767,20 +781,20 @@ void DrawNode::drawDot(const Vec2 &pos, float radius, const Color4F &color) { unsigned int vertex_count = 2*3; ensureCapacity(vertex_count); - + 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); V2F_C4B_T2F_Triangle triangle0 = {a, b, c}; V2F_C4B_T2F_Triangle triangle1 = {a, c, d}; triangles[0] = triangle0; triangles[1] = triangle1; - + _bufferCount += vertex_count; - + _dirty = true; } @@ -796,14 +810,14 @@ void DrawNode::drawSegment(const Vec2 &from, const Vec2 &to, float radius, const { unsigned int vertex_count = 6*3; ensureCapacity(vertex_count); - + 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)); @@ -814,24 +828,24 @@ void DrawNode::drawSegment(const Vec2 &from, const Vec2 &to, float radius, const 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); - + V2F_C4B_T2F_Triangle triangles0 = { {v0, Color4B(color), __t(v2fneg(v2fadd(n, t)))}, {v1, Color4B(color), __t(v2fsub(n, t))}, {v2, Color4B(color), __t(v2fneg(n))}, }; triangles[0] = triangles0; - + V2F_C4B_T2F_Triangle triangles1 = { {v3, Color4B(color), __t(n)}, {v1, Color4B(color), __t(v2fsub(n, t))}, {v2, Color4B(color), __t(v2fneg(n))}, }; triangles[1] = triangles1; - + V2F_C4B_T2F_Triangle triangles2 = { {v3, Color4B(color), __t(n)}, {v4, Color4B(color), __t(v2fneg(n))}, @@ -859,25 +873,25 @@ void DrawNode::drawSegment(const Vec2 &from, const Vec2 &to, float radius, const {v5, Color4B(color), __t(n)}, }; triangles[5] = triangles5; - + _bufferCount += vertex_count; - + _dirty = true; } void DrawNode::drawPolygon(const Vec2 *verts, int count, const Color4F &fillColor, float borderWidth, const Color4F &borderColor) { CCASSERT(count >= 0, "invalid count value"); - + bool outline = (borderColor.a > 0.0f && borderWidth > 0.0f); - + auto triangle_count = outline ? (3*count - 2) : (count - 2); auto 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; - + cursor = Triangulate::processTriangles(verts,cursor,count,fillColor); if(outline)