Cosmo
Cosmo

Reputation: 148

Interpolate 3D circle to 2D to draw a polyline

I want to improve my 3d circle rendering code by reducing the amount of world2screen calls.

The 3D center and radius of the circle is known, and I have a working example which converts the 3D circle in world coordinates to a 2D poly line with screen coordinates. However when profiling I noticed that the excessive amount of world2screen calls is really a bottle neck when rendering a lot of circles. I guess I can just project a limited amount of points to 2D using w2s and interpolate the rest of them?

The minimum amount of points should be 3 for an circle/ellipse, however I didn't find a working solution/algorithm for my issue yet. Can anyone help me or point me in the right direction with this?

void
draw_circle_3d(const glm::vec3& center, float radius)
{
  constexpr auto DRAW_CIRCLE_3D_SEGMENTS = 64;

  std::vector<glm::vec2> screen_points;
  screen_points.reserve(DRAW_CIRCLE_3D_SEGMENTS);

  constexpr float slice = glm::two_pi<float>() / static_cast<float>(DRAW_CIRCLE_3D_SEGMENTS);
  for (size_t i = 0; i < DRAW_CIRCLE_3D_SEGMENTS; ++i) {
    float theta = slice * static_cast<float>(i);
    auto dir = glm::vec3{ radius * std::cos(theta), radius * std::sin(theta), 0 };

    glm::vec2 screen_pos = world2screen(center + dir);
    screen_points.emplace_back(screen_pos);
  }

  draw_poly_line(screen_points);
}
``

Upvotes: 1

Views: 153

Answers (0)

Related Questions