ios - 使用 GL_TRIANGLE_STRIP 绘制圆弧

标签 ios opengl-es cocos2d-iphone

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

  1. 我似乎无法找到合适的角度。它们似乎与应有的位置偏移了一个奇数(不是 90/45/180)。
  2. 如果两者之间的总角度大于 180 度,则圆弧将在两者之间的圆上绘制较小的角度。即,如果总角度为 200 度,它将在圆的另一部分绘制一条 160 度的圆弧。

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

triangle strips

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

}

最佳答案

我的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;
    }

关于ios - 使用 GL_TRIANGLE_STRIP 绘制圆弧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18166952/

相关文章:

ios - MKMapView 内的自定义核心图形叠加 : can't display stroked ellipse

android - android中的opengl 2d图像网格

ios - -[CCTextureAtlas drawNumberOfQuads :fromIndex:] 556 中的 OpenGL 错误 0x0500

ios - 为应用程序设置设计表格 View 的最佳做法?

ios - 如何禁用 Firebase 实时数据库 iOS 离线写入

android - 在没有 SurfaceView 的 Android 上获取 GPU 信息

ios - cocos2d音频文件问题

ios - CCBReader与CocosBuilder的兼容版本

ios - 打开具有特定模式的 URL 时打开 iOS 应用程序

ios : Displaying a simple 3D model with GLEssentials sample code