NoSense
NoSense

Reputation: 959

Draw part of a circle using OpenGL?

I'm trying to draw only a sector/part of a 3d circle - 2 angles are given (start, end), the coordinates of the center, the radius and the width of the circle:

sector of a circle

Could you help me to do that?

Upvotes: 1

Views: 3243

Answers (1)

Stefan Haustein
Stefan Haustein

Reputation: 18793

Formula for calculating points on a circle, given the radius, center (x0/y0) and angle (in radians)

float x = radius * cos(angle) + x0;
float y = radius * sin(angle) + y0;

Use this to build the corresponding triangle strip:

float[] coordinates = new float[steps * 3];
float t = start_angle;
int pos = 0;
for (int i = 0; i < steps; i++) {
  float x_inner = radius_inner * cos(t) + x0;
  float y_inner = radius_inner * sin(t) + y0;

  float x_outer = radius_outer * cos(t) + x0;
  float y_outer = radius_outer * sin(t) + y0;

  coordinates[pos++] = x_inner;
  coordinates[pos++] = y_inner;
  coordinates[pos++] = 0f;

  coordinates[pos++] = x_outer;
  coordinates[pos++] = y_outer;
  coordinates[pos++] = 0f;

  t += (end_angle - start_angle) / steps;
}

// Now you need to hand over the coordinates to gl here in your preferred way,
// then call glDrawArrays(GL_TRIANGLE_STRIP, 0, steps * 2);

Upvotes: 2

Related Questions