我正在尝试编写一种方法,使用GL_TRIANGLE_STRIP从起始角度到终止角度绘制圆弧。我编写了以下代码但有以下问题:
我花了很多时间试图做到这一点,并认为让另一双眼睛看着我的代码是有帮助的。下图显示了我想在角度之间创建的三角形条带。在我弄清楚这部分之后,我将应用纹理。谢谢你的帮助!
-(void) drawArcFrom:(CGFloat)startAngle to:(CGFloat)endAngle position:(CGFloat)position radius:(CGPoint)radius {
CGFloat segmentWidth = 10.0;
CGFloat increment = fabsf(endAngle - startAngle) / segmentWidth;
int numSegs = fabsf(endAngle - startAngle) / segmentWidth;
int direction = (endAngle - startAngle > 0) ? 1 : -1;
ccVertex2F vertices[numSegs * 2];
for (int i = 0; i < numSegs; i++) {
CGFloat angle = startAngle - (i * increment * direction);
CGPoint outsidePoint = ccpAdd(position, ccp(sinf(CC_DEGREES_TO_RADIANS(angle)) * (radius + 4), cosf(CC_DEGREES_TO_RADIANS(angle)) * (radius + 4)));
CGPoint insidePoint = ccpAdd(position, ccp(sinf(CC_DEGREES_TO_RADIANS(angle)) * (radius - 4), cosf(CC_DEGREES_TO_RADIANS(angle)) * (radius - 4)));
vertices[i * 2] = (ccVertex2F) {outsidePoint.x, outsidePoint.y };
vertices[i * 2 + 1] = (ccVertex2F) {insidePoint.x, insidePoint.y };
}
glVertexPointer(2, GL_FLOAT, 0, vertices);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei) numSegs * 2);
}
答案 0 :(得分:1)
我的Android代码为完整的圆圈。 SEGMENTS = 20; coords [x,y,z]
float w2 = width / 2f;
float h2 = height / 2f;
double radius = Math.min(w2, h2);
double PI2 = Math.PI * 2d;
coords = new float[SEGMENTS * 2 * 3];
double angle;
int index = 0;
double min_radius = radius - circle_width;
double max_radius = radius + circle_width;
for (int i = 0; i < SEGMENTS; i++, index += 6) {
angle = (PI2 * (double) i) / (double) (SEGMENTS - 1);
double sin_angle = Math.sin(angle);
double cos_angle = Math.cos(angle);
coords[index + 0] = (float) (cos_angle * max_radius);
coords[index + 1] = (float) (sin_angle * max_radius);
coords[index + 2] = 0f;
coords[index + 3] = (float) (cos_angle * min_radius);
coords[index + 4] = (float) (sin_angle * min_radius);
coords[index + 5] = 0f;
}