bvogelzang
bvogelzang

Reputation: 1415

Draw circle arc with GL_TRIANGLE_STRIP

I am trying to write a method that will draw an arc from a start angle to an end angle using GL_TRIANGLE_STRIP. I've written the following code but have the following problems:

  1. I can't seem to get the proper angles working. They seem offset from where they should be by an odd amount (not 90/45/180).
  2. If the total angle between the two is more that 180 degrees then the arc will draw the smaller angle on the circle between the two. i.e if the total angle is 200 degrees it will draw an arc that is 160 degrees on the other part of the circle.

I've spent way to much time trying to get this right and figured it would be helpful to have another pair of eyes looking at my code. Image below shows the triangle strips I am trying to create between the angles. I'll be applying a texture after I figure this part out. Thanks for your help!

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

}

Upvotes: 2

Views: 2577

Answers (1)

Dmitry
Dmitry

Reputation: 358

My code for android for full circle. 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;
    }

Upvotes: 2

Related Questions