line_shapes.inl - Engine C API Reference

line_shapes.inl
  1. #include "vector3.h"
  2. namespace stingray_plugin_foundation
  3. {
  4. inline void line_shapes::line(const Vector3 &p1, const Vector3 &p2, Color8 color, PointList & out_starts, PointList & out_ends, ColorList & out_colors)
  5. {
  6. out_starts.push_back(p1);
  7. out_ends.push_back(p2);
  8. out_colors.push_back(color);
  9. }
  10. inline void line_shapes::xyz_cross(const Vector3 &pos, float radius, Color8 color, PointList & out_starts, PointList & out_ends, ColorList & out_colors)
  11. {
  12. line(pos - vector3(radius,0,0), pos + vector3(radius,0,0), color, out_starts, out_ends, out_colors);
  13. line(pos - vector3(0,radius,0), pos + vector3(0,radius,0), color, out_starts, out_ends, out_colors);
  14. line(pos - vector3(0,0,radius), pos + vector3(0,0,radius), color, out_starts, out_ends, out_colors);
  15. }
  16. inline void line_shapes::circle(const Vector3 &pos, float radius, const Vector3 &normal, Color8 color, PointList & out_starts, PointList & out_ends, ColorList & out_colors, int segments)
  17. {
  18. Vector3 x, y;
  19. make_axes(normal, x, y);
  20. x *= radius;
  21. y *= radius;
  22. Vector3 prev;
  23. for( int i = 0; i <= segments; ++i ) {
  24. float t = ((math::pi*2.0f) / ((float)segments)) * (float)i;
  25. Vector3 point = pos + x * cosf(t) + y * sinf(t);
  26. if (i != 0)
  27. line(prev, point, color, out_starts, out_ends, out_colors);
  28. prev = point;
  29. }
  30. }
  31. inline void line_shapes::cone(const Vector3 &p1, const Vector3 &p2, float radius, Color8 color, PointList & out_starts, PointList & out_ends, ColorList & out_colors, int segments, int bars)
  32. {
  33. Vector3 n = normalize(p1 - p2);
  34. Vector3 x, y;
  35. make_axes(n, x, y);
  36. circle(p2, radius, n, color, out_starts, out_ends, out_colors, segments);
  37. for (int i=0; i<bars; ++i) {
  38. float angle = (float(i)/float(bars)) * 2.0f * math::pi;
  39. Vector3 end = p2 + x*cosf(angle)*radius + y * sinf(angle)*radius;
  40. line(p1, end, color, out_starts, out_ends, out_colors);
  41. }
  42. }
  43. inline void line_shapes::arrow(const Vector3 &from, const Vector3 &to, float head_height, float head_radius, Color8 color, PointList & out_starts, PointList & out_ends, ColorList & out_colors)
  44. {
  45. const Vector3 line_normal = normalize(to - from);
  46. line(from, to, color, out_starts, out_ends, out_colors);
  47. cone(to, to - line_normal * head_height, head_radius, color, out_starts, out_ends, out_colors, 12, 6);
  48. }
  49. inline void line_shapes::sphere(const Vector3 &pos, float radius, Color8 color, PointList & out_starts, PointList & out_ends, ColorList & out_colors, int segments, int parts)
  50. {
  51. if (radius == 0)
  52. return;
  53. for (int i=0; i<parts; ++i) {
  54. float offset = radius*float(i)/float(parts);
  55. float r = sqrtf(radius*radius - offset*offset);
  56. int seg = int( float(segments) * r / radius + 0.5f);
  57. circle(pos + vector3(0,0,offset), r, vector3(0,0,1), color, out_starts, out_ends, out_colors, seg);
  58. circle(pos + vector3(0,offset,0), r, vector3(0,1,0), color, out_starts, out_ends, out_colors, seg);
  59. circle(pos + vector3(offset,0,0), r, vector3(1,0,0), color, out_starts, out_ends, out_colors, seg);
  60. if (i != 0) {
  61. circle(pos - vector3(0,0,offset), r, vector3(0,0,1), color, out_starts, out_ends, out_colors, seg);
  62. circle(pos - vector3(0,offset,0), r, vector3(0,1,0), color, out_starts, out_ends, out_colors, seg);
  63. circle(pos - vector3(offset,0,0), r, vector3(1,0,0), color, out_starts, out_ends, out_colors, seg);
  64. }
  65. }
  66. }
  67. }