CZGPolygonLayer 是一个用于绘制任意三角形集合的 cocos2d CCNode 子类。实际上,它是对 OpenGL 三角形绘制的抽象,但您可以使用它作为 CCNode。
CZGPolygonLayer 被封装成一个 cocoapod。最简单的方法是将它作为 pod 添加到项目中。CZGPolygonLayer 依赖于已经作为 pod 安装的 cocos2d。如果您还没有这样做,我建议将 cocos2d 作为 pod 添加到现有项目中,然后删除设置项目时添加的 cocos2d 文件。
为了快速回顾,以下是 GL_TRIANGLE_STRIP 绘制顶点的顺序:
// Points in the polygon are drawn as GL_TRIANGLE_STRIP.
// If you've got a number of points in a polygon, they're drawn in the
// following order:
//
// v1----v3----v5
// |\ |\ |\
// | \ | \ | \
// | \ | \ | \
// v0---v2---v4----v6
// See the HelloWorldLayer class in CZGPolygonLayerExample project for some sample usage.
// We're going to draw an n-sided radially symmetrical polygon.
self.polygon = [CZGPolygonLayer nodeWithPoints: 100];
_polygon.position = center;
// polygon point positions are updated in update: method
// We move the points in and out from the center to create an amoeba-like effect
[self update: 0];
[self addChild: _polygon];
...
- (void) update: (ccTime) dt {
_time += dt;
int sides = _polygon.numberOfPoints;
float relativeMovement = 0.1;
for (int i=0; i< sides; i++) {
int vertexIndex = i%2 ? (i+1)/2 : (sides-(i/2))%sides;
// we're going to move each individual vertex in and out, hovering around a radius of 200.0
float radius = (relativeMovement*sinf(_time - (M_PI*2.0*i/sides)) + 1.0 - relativeMovement) * 200.0;
[_polygon setPoint: ccp(radius*cosf(M_PI*2.0*vertexIndex/sides),
radius*sinf(M_PI*2.0*vertexIndex/sides))
atIndex: i];
[_polygon setFillColor: ccc4f(0.0, 1.0, 0.0, 1.0) atIndex: i];
}
}
// For a quick refresher, here's the order that GL_TRIANGLE_STRIP draws vertices:
// Points in the polygon are drawn as GL_TRIANGLE_STRIP.
// If you've got a number of points in a polygon, they're drawn in the
// following order:
//
// v2----v3----v4
// |\ | /|
// | \ | / |
// | \ |/ |
// v1---v0-----v5
// Draw an n-pointed star using GL_TRIANGLE_FAN
int points = 5;
float radius = 100;
self.star = [CZGPolygonLayer nodeWithPoints: points*2+2];
float angleIncrement = M_PI/points;
for (int i=0; i< points*2+1; i+=2) {
float innerAngle = i*angleIncrement;
float outerAngle = (1 + i)*angleIncrement;
// inner point
[self.star setPoint: ccp(0.5*radius*cosf(innerAngle), 0.5*radius*sinf(innerAngle)) atIndex: i+1];
[self.star setFillColor: ccc4f(1.0, 0.0, 0.0, 1.0) atIndex: i+1];
// outer point
if (i+2 < points*2+2) {
[self.star setPoint: ccp(radius*cosf(outerAngle), radius*sinf(outerAngle)) atIndex: i+2];
[self.star setFillColor: ccc4f(1.0, 0.0, 0.0, 1.0) atIndex: i+2];
}
}
[self.star setFillColor: ccc4f(1.0, 1.0, 0.0, 1.0) atIndex: 0];
self.star.drawMode = GL_TRIANGLE_FAN;
self.star.position = ccp(center.x - radius*2,
center.y - radius*2);
[self addChild: self.star];
// Draw an n-pointed star using GL_TRIANGLES
self.coloredStar = [CZGPolygonLayer nodeWithPoints: points*6];
for (int i=0; i< points; i++) {
float currentAngle = 2*i*angleIncrement;
float middleAngle = (1 + 2*i)*angleIncrement;
float nextAngle = (2 + 2*i)*angleIncrement;
CGPoint center = ccp(0,0);
CGPoint current = ccp(radius*cosf(currentAngle), radius*sinf(currentAngle));
CGPoint middle = ccp(0.5*radius*cosf(middleAngle), 0.5*radius*sinf(middleAngle));
CGPoint next = ccp(radius*cosf(nextAngle), radius*sinf(nextAngle));
// First triangle
[self.coloredStar setPoint: center atIndex: i*6];
[self.coloredStar setFillColor: ccc4f(1.0, 1.0, 0.0, 1.0) atIndex: i*6];
[self.coloredStar setPoint: middle atIndex: i*6+1];
[self.coloredStar setFillColor: ccc4f(1.0, 1.0, 0.0, 1.0) atIndex: i*6+1];
[self.coloredStar setPoint: current atIndex: i*6+2];
[self.coloredStar setFillColor: ccc4f(0.8, 0.8, 0.0, 1.0) atIndex: i*6+2];
// Second triangle
[self.coloredStar setPoint: center atIndex: i*6+3];
[self.coloredStar setFillColor: ccc4f(0.8, 0.8, 0.0, 1.0) atIndex: i*6+3];
[self.coloredStar setPoint: next atIndex: i*6+4];
[self.coloredStar setFillColor: ccc4f(0.7, 0.7, 0.0, 1.0) atIndex: i*6+4];
[self.coloredStar setPoint: middle atIndex: i*6+5];
[self.coloredStar setFillColor: ccc4f(0.6, 0.6, 0.0, 1.0) atIndex: i*6+5];
}
self.coloredStar.drawMode = GL_TRIANGLES;
self.coloredStar.position = ccp(center.x + radius*2,
center.y + radius*2);
[self addChild: self.coloredStar];