diff --git a/cocos/2d/CCDrawNode.cpp b/cocos/2d/CCDrawNode.cpp index 541bd8974c..288c88aba0 100644 --- a/cocos/2d/CCDrawNode.cpp +++ b/cocos/2d/CCDrawNode.cpp @@ -437,6 +437,86 @@ void DrawNode::drawPolygon(Point *verts, unsigned int count, const Color4F &fill free(extrude); } +void DrawNode::drawTriangle(const Point &p1, const Point &p2, const Point &p3, const Color4F &color) +{ + unsigned int vertex_count = 2*3; + ensureCapacity(vertex_count); + + Color4B col = Color4B(color); + V2F_C4B_T2F a = {Vertex2F(p1.x, p1.y), col, Tex2F(0.0, 0.0) }; + V2F_C4B_T2F b = {Vertex2F(p2.x, p2.y), col, Tex2F(0.0, 0.0) }; + V2F_C4B_T2F c = {Vertex2F(p3.x, p3.y), col, Tex2F(0.0, 0.0) }; + + 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; +} + +void DrawNode::drawCubicBezier(const Point& from, const Point& control1, const Point& control2, const Point& to, unsigned int segments, const Color4F &color) +{ + unsigned int vertex_count = (segments + 1) * 3; + ensureCapacity(vertex_count); + + Tex2F texCoord = Tex2F(0.0, 0.0); + Color4B col = Color4B(color); + Vertex2F vertex; + Vertex2F firstVertex = Vertex2F(from.x, from.y); + Vertex2F lastVertex = Vertex2F(to.x, to.y); + + 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; + vertex = Vertex2F(x, y); + + 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; +} + +void DrawNode::drawQuadraticBezier(const Point& from, const Point& control, const Point& to, unsigned int segments, const Color4F &color) +{ + unsigned int vertex_count = (segments + 1) * 3; + ensureCapacity(vertex_count); + + Tex2F texCoord = Tex2F(0.0, 0.0); + Color4B col = Color4B(color); + Vertex2F vertex; + Vertex2F firstVertex = Vertex2F(from.x, from.y); + Vertex2F lastVertex = Vertex2F(to.x, to.y); + + 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; + vertex = Vertex2F(x, y); + + 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; +} + void DrawNode::clear() { _bufferCount = 0; diff --git a/cocos/2d/CCDrawNode.h b/cocos/2d/CCDrawNode.h index 9349430880..231c87c07c 100644 --- a/cocos/2d/CCDrawNode.h +++ b/cocos/2d/CCDrawNode.h @@ -72,6 +72,15 @@ public: * @endcode */ void drawPolygon(Point *verts, unsigned int count, const Color4F &fillColor, float borderWidth, const Color4F &borderColor); + + /** draw a triangle with color */ + void drawTriangle(const Point &p1, const Point &p2, const Point &p3, const Color4F &color); + + /** draw a cubic bezier curve with color and number of segments */ + void drawCubicBezier(const Point& from, const Point& control1, const Point& control2, const Point& to, unsigned int segments, const Color4F &color); + + /** draw a quadratic bezier curve with color and number of segments */ + void drawQuadraticBezier(const Point& from, const Point& control, const Point& to, unsigned int segments, const Color4F &color) /** Clear the geometry in the node's buffer. */ void clear();