DebugRenderer.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "DebugRenderer.h"
  24. #include "Renderer.h"
  25. #include "MathUtils.h"
  26. namespace crown
  27. {
  28. //-----------------------------------------------------------------------------
  29. DebugRenderer::DebugRenderer(Renderer& renderer) :
  30. m_renderer(renderer),
  31. m_lines_count(0)
  32. {
  33. }
  34. //-----------------------------------------------------------------------------
  35. DebugRenderer::~DebugRenderer()
  36. {
  37. }
  38. //-----------------------------------------------------------------------------
  39. void DebugRenderer::add_line(const Vec3& start, const Vec3& end, const Color4& color, bool depth_write)
  40. {
  41. if (m_lines_count >= MAX_DEBUG_LINES)
  42. {
  43. return;
  44. }
  45. m_lines[m_lines_count * 2 + 0] = start;
  46. m_lines[m_lines_count * 2 + 1] = end;
  47. m_colors[m_lines_count * 2 + 0] = color;
  48. m_colors[m_lines_count * 2 + 1] = color;
  49. m_depth_writes[m_lines_count * 2 + 0] = depth_write;
  50. m_depth_writes[m_lines_count * 2 + 1] = depth_write;
  51. m_lines_count++;
  52. }
  53. //-----------------------------------------------------------------------------
  54. void DebugRenderer::add_sphere(const Vec3& center, const float radius, const Color4& color, bool depth_write)
  55. {
  56. const uint32_t deg_step = 15;
  57. // XZ plane
  58. for (uint32_t deg = 0; deg < 360; deg += deg_step)
  59. {
  60. float rad0 = math::deg_to_rad(deg);
  61. float rad1 = math::deg_to_rad(deg + deg_step);
  62. Vec3 start(math::cos(rad0) * radius, 0, -math::sin(rad0) * radius);
  63. Vec3 end (math::cos(rad1) * radius, 0, -math::sin(rad1) * radius);
  64. add_line(center + start, center + end, color, depth_write);
  65. }
  66. // XY plane
  67. for (uint32_t deg = 0; deg < 360; deg += deg_step)
  68. {
  69. float rad0 = math::deg_to_rad(deg);
  70. float rad1 = math::deg_to_rad(deg + deg_step);
  71. Vec3 start(math::cos(rad0) * radius, math::sin(rad0) * radius, 0);
  72. Vec3 end (math::cos(rad1) * radius, math::sin(rad1) * radius, 0);
  73. add_line(center + start, center + end, color, depth_write);
  74. }
  75. // YZ plane
  76. for (uint32_t deg = 0; deg < 360; deg += deg_step)
  77. {
  78. float rad0 = math::deg_to_rad(deg);
  79. float rad1 = math::deg_to_rad(deg + deg_step);
  80. Vec3 start(0, math::sin(rad0) * radius, -math::cos(rad0) * radius);
  81. Vec3 end (0, math::sin(rad1) * radius, -math::cos(rad1) * radius);
  82. add_line(center + start, center + end, color, depth_write);
  83. }
  84. }
  85. //-----------------------------------------------------------------------------
  86. void DebugRenderer::add_box(const Vec3& min, const Vec3& max, const Color4& color, bool depth_write)
  87. {
  88. // Back lines
  89. add_line(min , Vec3(max.x, min.y, min.z), color, depth_write);
  90. add_line(Vec3(max.x, min.y, min.z), Vec3(max.x, max.y, min.z), color, depth_write);
  91. add_line(Vec3(max.x, max.y, min.z), Vec3(min.x, max.y, min.z), color, depth_write);
  92. add_line(Vec3(min.x, max.y, min.z), min , color, depth_write);
  93. // Front lines
  94. add_line(Vec3(min.x, min.y, max.z), Vec3(max.x, min.y, max.z), color, depth_write);
  95. add_line(Vec3(max.x, min.y, max.z), Vec3(max.x, max.y, max.z), color, depth_write);
  96. add_line(Vec3(max.x, max.y, max.z), Vec3(min.x, max.y, max.z), color, depth_write);
  97. add_line(Vec3(min.x, max.y, max.z), Vec3(min.x, min.y, max.z), color, depth_write);
  98. // Connect back and front vertices
  99. add_line(min , Vec3(min.x, min.y, max.z), color, depth_write);
  100. add_line(Vec3(max.x, min.y, min.z), Vec3(max.x, min.y, max.z), color, depth_write);
  101. add_line(Vec3(max.x, max.y, min.z), Vec3(max.x, max.y, max.z), color, depth_write);
  102. add_line(Vec3(min.x, max.y, min.z), Vec3(min.x, max.y, max.z), color, depth_write);
  103. }
  104. //-----------------------------------------------------------------------------
  105. void DebugRenderer::add_pose(const Mat4& pose, bool depth_write)
  106. {
  107. add_line(pose.translation(), pose.translation() + pose.x(), Color4::RED, depth_write);
  108. add_line(pose.translation(), pose.translation() + pose.y(), Color4::GREEN, depth_write);
  109. add_line(pose.translation(), pose.translation() + pose.z(), Color4::BLUE, depth_write);
  110. }
  111. //-----------------------------------------------------------------------------
  112. void DebugRenderer::draw_all()
  113. {
  114. if (m_lines_count > 0)
  115. {
  116. // m_renderer.draw_lines(m_lines[0].to_float_ptr(), m_colors[0].to_float_ptr(), m_lines_count * 2);
  117. }
  118. m_lines_count = 0;
  119. }
  120. } // namespace crown