DebugLine.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "DebugLine.h"
  24. #include "MathUtils.h"
  25. #include "Color4.h"
  26. #include "Vector3.h"
  27. #include <bgfx.h>
  28. namespace crown
  29. {
  30. namespace debug_line
  31. {
  32. static const uint8_t vs_debug_line_bin_h[335] =
  33. {
  34. 0x56, 0x53, 0x48, 0x02, 0xa4, 0x8b, 0xef, 0x49, 0x01, 0x00, 0x0f, 0x75, 0x5f, 0x6d, 0x6f, 0x64, // VSH....I...u_mod
  35. 0x65, 0x6c, 0x56, 0x69, 0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x09, 0x01, 0x00, 0x00, 0x01, 0x00, // elViewProj......
  36. 0x2a, 0x01, 0x00, 0x00, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x20, 0x6d, 0x65, // *...attribute me
  37. 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x61, 0x5f, 0x63, 0x6f, 0x6c, // diump vec4 a_col
  38. 0x6f, 0x72, 0x30, 0x3b, 0x0a, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x20, 0x6d, // or0;.attribute m
  39. 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x33, 0x20, 0x61, 0x5f, 0x70, 0x6f, // ediump vec3 a_po
  40. 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0a, 0x76, 0x61, 0x72, 0x79, 0x69, 0x6e, 0x67, 0x20, // sition;.varying
  41. 0x6d, 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x76, 0x5f, 0x63, // mediump vec4 v_c
  42. 0x6f, 0x6c, 0x6f, 0x72, 0x30, 0x3b, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x6d, // olor0;.uniform m
  43. 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x6d, 0x61, 0x74, 0x34, 0x20, 0x75, 0x5f, 0x6d, 0x6f, // ediump mat4 u_mo
  44. 0x64, 0x65, 0x6c, 0x56, 0x69, 0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x3b, 0x0a, 0x76, 0x6f, 0x69, // delViewProj;.voi
  45. 0x64, 0x20, 0x6d, 0x61, 0x69, 0x6e, 0x20, 0x28, 0x29, 0x0a, 0x7b, 0x0a, 0x20, 0x20, 0x6d, 0x65, // d main ().{. me
  46. 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, // diump vec4 tmpva
  47. 0x72, 0x5f, 0x31, 0x3b, 0x0a, 0x20, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, 0x72, 0x5f, 0x31, 0x2e, // r_1;. tmpvar_1.
  48. 0x77, 0x20, 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x3b, 0x0a, 0x20, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, // w = 1.0;. tmpva
  49. 0x72, 0x5f, 0x31, 0x2e, 0x78, 0x79, 0x7a, 0x20, 0x3d, 0x20, 0x61, 0x5f, 0x70, 0x6f, 0x73, 0x69, // r_1.xyz = a_posi
  50. 0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0a, 0x20, 0x20, 0x67, 0x6c, 0x5f, 0x50, 0x6f, 0x73, 0x69, 0x74, // tion;. gl_Posit
  51. 0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x28, 0x75, 0x5f, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x56, 0x69, // ion = (u_modelVi
  52. 0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x20, 0x2a, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, 0x72, 0x5f, // ewProj * tmpvar_
  53. 0x31, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x76, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x30, 0x20, 0x3d, // 1);. v_color0 =
  54. 0x20, 0x61, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x30, 0x3b, 0x0a, 0x7d, 0x0a, 0x0a, 0x00, // a_color0;.}...
  55. };
  56. static const uint8_t fs_debug_line_bin_h[91] =
  57. {
  58. 0x46, 0x53, 0x48, 0x02, 0xa4, 0x8b, 0xef, 0x49, 0x00, 0x00, 0x4c, 0x00, 0x00, 0x00, 0x76, 0x61, // FSH....I..L...va
  59. 0x72, 0x79, 0x69, 0x6e, 0x67, 0x20, 0x6d, 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, // rying mediump ve
  60. 0x63, 0x34, 0x20, 0x76, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x30, 0x3b, 0x0a, 0x76, 0x6f, 0x69, // c4 v_color0;.voi
  61. 0x64, 0x20, 0x6d, 0x61, 0x69, 0x6e, 0x20, 0x28, 0x29, 0x0a, 0x7b, 0x0a, 0x20, 0x20, 0x67, 0x6c, // d main ().{. gl
  62. 0x5f, 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x63, // _FragColor = v_c
  63. 0x6f, 0x6c, 0x6f, 0x72, 0x30, 0x3b, 0x0a, 0x7d, 0x0a, 0x0a, 0x00, // olor0;.}...
  64. };
  65. static bgfx::VertexDecl s_decl;
  66. static bgfx::ProgramHandle s_prog;
  67. void init()
  68. {
  69. s_decl
  70. .begin()
  71. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  72. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  73. .end();
  74. bgfx::ShaderHandle vs = bgfx::createShader(
  75. bgfx::makeRef(vs_debug_line_bin_h, sizeof(vs_debug_line_bin_h)));
  76. bgfx::ShaderHandle fs = bgfx::createShader(
  77. bgfx::makeRef(fs_debug_line_bin_h, sizeof(fs_debug_line_bin_h)));
  78. s_prog = bgfx::createProgram(vs, fs, true);
  79. }
  80. void shutdown()
  81. {
  82. bgfx::destroyProgram(s_prog);
  83. }
  84. } // namespace debug_line
  85. //-----------------------------------------------------------------------------
  86. DebugLine::DebugLine(bool depth_test)
  87. : m_depth_test(depth_test)
  88. , m_num_lines(0)
  89. {
  90. }
  91. //-----------------------------------------------------------------------------
  92. void DebugLine::add_line(const Color4& color, const Vector3& start, const Vector3& end)
  93. {
  94. if (m_num_lines >= CE_MAX_DEBUG_LINES)
  95. return;
  96. m_lines[m_num_lines].p0[0] = start.x;
  97. m_lines[m_num_lines].p0[1] = start.y;
  98. m_lines[m_num_lines].p0[2] = start.z;
  99. m_lines[m_num_lines].c0 = color4::to_abgr(color);
  100. m_lines[m_num_lines].p1[0] = end.x;
  101. m_lines[m_num_lines].p1[1] = end.y;
  102. m_lines[m_num_lines].p1[2] = end.z;
  103. m_lines[m_num_lines].c1 = color4::to_abgr(color);
  104. m_num_lines++;
  105. }
  106. //-----------------------------------------------------------------------------
  107. void DebugLine::add_sphere(const Color4& color, const Vector3& center, const float radius)
  108. {
  109. const uint32_t deg_step = 15;
  110. for (uint32_t deg = 0; deg < 360; deg += deg_step)
  111. {
  112. const float rad0 = math::deg_to_rad(deg);
  113. const float rad1 = math::deg_to_rad(deg + deg_step);
  114. // XZ plane
  115. const Vector3 start0(math::cos(rad0) * radius, 0, -math::sin(rad0) * radius);
  116. const Vector3 end0 (math::cos(rad1) * radius, 0, -math::sin(rad1) * radius);
  117. add_line(color, center + start0, center + end0);
  118. // XY plane
  119. const Vector3 start1(math::cos(rad0) * radius, math::sin(rad0) * radius, 0);
  120. const Vector3 end1 (math::cos(rad1) * radius, math::sin(rad1) * radius, 0);
  121. add_line(color, center + start1, center + end1);
  122. // YZ plane
  123. const Vector3 start2(0, math::sin(rad0) * radius, -math::cos(rad0) * radius);
  124. const Vector3 end2 (0, math::sin(rad1) * radius, -math::cos(rad1) * radius);
  125. add_line(color, center + start2, center + end2);
  126. }
  127. }
  128. //-----------------------------------------------------------------------------
  129. void DebugLine::clear()
  130. {
  131. m_num_lines = 0;
  132. }
  133. //-----------------------------------------------------------------------------
  134. void DebugLine::commit()
  135. {
  136. if (!m_num_lines)
  137. return;
  138. bgfx::TransientVertexBuffer tvb;
  139. bgfx::allocTransientVertexBuffer(&tvb, CE_MAX_DEBUG_LINES * 2, debug_line::s_decl);
  140. memcpy(tvb.data, m_lines, sizeof(Line) * m_num_lines);
  141. bgfx::setState(BGFX_STATE_PT_LINES
  142. | BGFX_STATE_RGB_WRITE
  143. | BGFX_STATE_DEPTH_WRITE
  144. | (m_depth_test ? BGFX_STATE_DEPTH_TEST_LESS
  145. : BGFX_STATE_DEPTH_TEST_ALWAYS)
  146. | BGFX_STATE_CULL_CW);
  147. bgfx::setProgram(debug_line::s_prog);
  148. bgfx::setVertexBuffer(&tvb, 0, m_num_lines * 2);
  149. bgfx::submit(0);
  150. }
  151. } // namespace crown