1

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);
}
``
Cosmo
  • 138
  • 5
  • How is your `world2screen` function implemented? How many circles do you have? I'm quite surprised it's the bottleneck instead of, say, `cos`/`sin` calls next to each circle. – yeputons Dec 16 '22 at 11:34
  • @yeputons well with bottleneck I mean it's kinda a noticeable high impact. As a test I profiled drawing 1k random positioned circles and got the following results: [profiling results](https://i.postimg.cc/vT6Wvhzv/profiling.png). – Cosmo Dec 16 '22 at 12:49
  • 2
    are you sure its ellipse? you know if you use perspective projection then its not an ellipse ... You still can approximate with cubics ( I would go for 4 or 8 [interpolation cubics like Catmul-rom](https://stackoverflow.com/a/22806242/2521214) in case you have [BEZIER you can use that too by converting the points](https://stackoverflow.com/a/22582447/2521214) ) – Spektre Dec 17 '22 at 06:01
  • yes you are right. it's still a circle but can look like an ellipse due to the perspective. thanks for pointing that out, I'll check it. – Cosmo Dec 18 '22 at 11:39
  • @Cosmo no its not circle nor ellipse due to perspective (visual size depends on distance to camera) if circle is not paralel to projection plane it looks like ellipse but its deformed so fitting ellipses will not work as the circumference is not composed of elliptic curves anymore... – Spektre Dec 18 '22 at 12:25
  • Draw the ellipse-like shape on the 2D screen based on four positions (on the major and minor axis) and center. (i.e. 5 w2s calls per circle. If you don't need the center, 4 calls.) – fana Dec 21 '22 at 04:47
  • The direction on the 3D circle corresponding to the major axis and minor axis on 2D can probably be calculated from the relationship between the normal of the circle and the direction of the camera. The direction of the major axis will be cross-product of both (?) – fana Dec 21 '22 at 04:51

0 Answers0