debug_line.cpp 7.6 KB

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