【问题标题】:Draw circle arc with GL_TRIANGLE_STRIP使用 GL_TRIANGLE_STRIP 绘制圆弧
【发布时间】:2013-08-10 22:29:38
【问题描述】:

我正在尝试编写一个方法,该方法将使用 GL_TRIANGLE_STRIP 从起始角度到结束角度绘制弧线。我写了以下代码,但有以下问题:

  1. 我似乎无法获得正确的角度。它们似乎偏离了应有的位置(不是 90/45/180)。
  2. 如果两者之间的总角度大于 180 度,则圆弧将在两者之间的圆上绘制较小的角度。即如果总角度为 200 度,它将在圆的另一部分绘制 160 度的弧。

我花了很多时间试图做到这一点,并认为让另一双眼睛看着我的代码会很有帮助。下图显示了我试图在角度之间创建的三角形条带。弄清楚这部分后,我将应用纹理。感谢您的帮助!

-(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);

}

【问题讨论】:

  • 你能发一张它的截图吗?这个想法看起来大部分是正确的。但是您没有包含angle1 的定义。如果你没有初始化它,它可以解释偏移量。对于另一个问题,您总是可以在对它们进行数学运算之前对角度进行排序。例如,确保你总是从低到高。
  • 你是对的。确保我从最低角度开始,然后到更高的角度是主要的解决方法。谢谢你的建议。 angle1 是一个错字,实际上应该是 startAngle 我已经更改了。
  • ps:通过对每个角度进行一次 deg2rad 计算而不是 4 次来加快速度并使其更易于阅读

标签: ios opengl-es cocos2d-iphone


【解决方案1】:

我的完整循环的 android 代码。 段 = 20; 坐标 [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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多