- 精通Cocos2d-x游戲開發(進階卷)
- 王永寶
- 1147字
- 2020-11-28 22:37:10
11.3 渲染接口詳解
DrawNode提供的渲染接口與DrawingPrimitives基本一致,它們支持繪制點、線段、多邊形等圖元,由于DrawingPrimitives即將被廢棄,所以這里只介紹DrawNode提供的繪制接口。
11.3.1 繪制點
/** 傳入Vec2坐標、點的尺寸和顏色參數,繪制一個點 */ void drawPoint(const Vec2& point, const float pointSize, const Color4F &color); /** 傳入Vec2坐標數組、數組長度以及顏色參數,繪制一系列點 */ void drawPoints(const Vec2 *position, unsigned int numberOfPoints, const Color4F &color); /** 傳入Vec2坐標數組、數組長度、點的尺寸以及顏色參數,繪制一系列點 */ void drawPoints(const Vec2 *position, unsigned int numberOfPoints, const float pointSize, const Color4F &color);
11.3.2 繪制線段、矩形、多邊形與圓形
/** 傳入兩個坐標、一個顏色參數,繪制一條線段 */ void drawLine(const Vec2 &origin, const Vec2 &destination, const Color4F &color); /** 傳入兩個坐標、一個顏色參數,繪制一個矩形 */ void drawRect(const Vec2 &origin, const Vec2 &destination, const Color4F &color); /** 傳入Vec2坐標數組、數組長度、是否閉合以及顏色參數,繪制一個多邊形 closePolygon參數為true時多邊形閉合,false多邊形不閉合 */ void drawPoly(const Vec2 *poli, unsigned int numberOfPoints, bool closePolygon, const Color4F &color); /** 根據圓心、半徑、角度(一般為0)、分段(越高越精細)、drawLineToCenter選項、x 和y軸的縮放比例、顏色等參數繪制一個圓,drawLineToCenter參數為true時會從圓心到每 個分段繪制一條線段 */ void drawCircle( const Vec2& center, float radius, float angle, unsigned int segments, bool drawLineToCenter, float scaleX, float scaleY, const Color4F &color); /** 根據圓心、半徑、角度(一般為0)、分段(越高越精細)、drawLineToCenter選項、顏 色等參數繪制一個圓 */ void drawCircle(const Vec2 ¢er, float radius, float angle, unsigned int segments, bool drawLineToCenter, const Color4F &color);
11.3.3 繪制貝塞爾曲線
/** 傳入起點坐標、控制點坐標、終點坐標、分段以及顏色,繪制一條貝塞爾曲線 */ void drawQuadBezier(const Vec2 &origin, const Vec2 &control, const Vec2 &destination, unsigned int segments, const Color4F &color); /** 傳入起點坐標、兩個控制點坐標、終點坐標、分段以及顏色,繪制一條貝塞爾曲線 */ void drawCubicBezier(const Vec2 &origin, const Vec2 &control1, const Vec2 &control2, const Vec2 &destination, unsigned int segments, const Color4F &color);
11.3.4 繪制CardinalSpline
/** 傳入一系列點、張力、分段以及顏色來繪制一條基數樣條曲線 */ void drawCardinalSpline(PointArray *config, float tension, unsigned int segments, const Color4F &color);
使用drawCardinalSpline繪制一條線段,如圖11-2演示了當設置了不同的張力參數時,線段的表現。以下4張圖片設置的張力依次為0.1、0.5、1.0和2.0。

圖11-2 不同張力下的CardinalSpline
11.3.5 繪制凱特摩曲線
/** 傳入一系列點、分段以及顏色,繪制一條凱特摩曲線 */ void drawCatmullRom(PointArray *points, unsigned int segments, const Color4F &color);
如圖11-3使用了與圖11-2一樣的線段參數,演示了凱特摩曲線的繪制。

圖11-3 凱特摩曲線
11.3.6 繪制實心圖元
/** 傳入一個坐標、半徑以及顏色,繪制一個圓點 */ void drawDot(const Vec2 &pos, float radius, const Color4F &color); /** 傳入4個坐標和顏色,繪制一個四邊形 */ void drawRect(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, const Vec2& p4, const Color4F &color); /** 傳入起始和結束坐標以及顏色,繪制一個實心矩形 */ void drawSolidRect(const Vec2 &origin, const Vec2 &destination, const Color4F &color); /** 傳入一系列點和顏色,繪制一個實心多邊形 */ void drawSolidPoly(const Vec2 *poli, unsigned int numberOfPoints, const Color4F &color); /** 傳入圓心、半徑、角度、分段、x和y軸的縮放值以及顏色參數,繪制一個實心圓 */ void drawSolidCircle(const Vec2& center, float radius, float angle, unsigned int segments, float scaleX, float scaleY, const Color4F &color); /** 傳入圓心、半徑、角度、分段以及顏色參數,繪制一個實心圓 */ void drawSolidCircle(const Vec2& center, float radius, float angle, unsigned int segments, const Color4F& color); /** 傳入起點、終點、半徑以及顏色,繪制一個弓形片段 */ void drawSegment(const Vec2 &from, const Vec2 &to, float radius, const Color4F &color); /** 傳入一系列點、填充顏色、邊框寬度、邊框顏色,繪制一個帶邊框的實心多邊形 */ void drawPolygon(const Vec2 *verts, int count, const Color4F &fillColor, float borderWidth, const Color4F &borderColor); /** 傳入3個坐標以及一個顏色,繪制一個三角形*/ void drawTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, const Color4F &color);