debug_line.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 "matrix4x4.h"
  28. #include "config.h"
  29. #include <string.h>
  30. #include <bgfx.h>
  31. namespace crown
  32. {
  33. namespace debug_line
  34. {
  35. #if CROWN_PLATFORM_LINUX || CROWN_PLATFORM_ANDROID
  36. static const uint8_t vs_h[335] =
  37. {
  38. 0x56, 0x53, 0x48, 0x03, 0x1c, 0xf0, 0xa8, 0xc9, 0x01, 0x00, 0x0f, 0x75, 0x5f, 0x6d, 0x6f, 0x64, // VSH........u_mod
  39. 0x65, 0x6c, 0x56, 0x69, 0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x09, 0x01, 0x00, 0x00, 0x01, 0x00, // elViewProj......
  40. 0x2a, 0x01, 0x00, 0x00, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x20, 0x6d, 0x65, // *...attribute me
  41. 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x61, 0x5f, 0x63, 0x6f, 0x6c, // diump vec4 a_col
  42. 0x6f, 0x72, 0x30, 0x3b, 0x0a, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x20, 0x6d, // or0;.attribute m
  43. 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x33, 0x20, 0x61, 0x5f, 0x70, 0x6f, // ediump vec3 a_po
  44. 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0a, 0x76, 0x61, 0x72, 0x79, 0x69, 0x6e, 0x67, 0x20, // sition;.varying
  45. 0x6d, 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x76, 0x5f, 0x63, // mediump vec4 v_c
  46. 0x6f, 0x6c, 0x6f, 0x72, 0x30, 0x3b, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x6d, // olor0;.uniform m
  47. 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x6d, 0x61, 0x74, 0x34, 0x20, 0x75, 0x5f, 0x6d, 0x6f, // ediump mat4 u_mo
  48. 0x64, 0x65, 0x6c, 0x56, 0x69, 0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x3b, 0x0a, 0x76, 0x6f, 0x69, // delViewProj;.voi
  49. 0x64, 0x20, 0x6d, 0x61, 0x69, 0x6e, 0x20, 0x28, 0x29, 0x0a, 0x7b, 0x0a, 0x20, 0x20, 0x6d, 0x65, // d main ().{. me
  50. 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, // diump vec4 tmpva
  51. 0x72, 0x5f, 0x31, 0x3b, 0x0a, 0x20, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, 0x72, 0x5f, 0x31, 0x2e, // r_1;. tmpvar_1.
  52. 0x77, 0x20, 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x3b, 0x0a, 0x20, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, // w = 1.0;. tmpva
  53. 0x72, 0x5f, 0x31, 0x2e, 0x78, 0x79, 0x7a, 0x20, 0x3d, 0x20, 0x61, 0x5f, 0x70, 0x6f, 0x73, 0x69, // r_1.xyz = a_posi
  54. 0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0a, 0x20, 0x20, 0x67, 0x6c, 0x5f, 0x50, 0x6f, 0x73, 0x69, 0x74, // tion;. gl_Posit
  55. 0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x28, 0x75, 0x5f, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x56, 0x69, // ion = (u_modelVi
  56. 0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x20, 0x2a, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, 0x72, 0x5f, // ewProj * tmpvar_
  57. 0x31, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x76, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x30, 0x20, 0x3d, // 1);. v_color0 =
  58. 0x20, 0x61, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x30, 0x3b, 0x0a, 0x7d, 0x0a, 0x0a, 0x00, // a_color0;.}...
  59. };
  60. static const uint8_t fs_h[91] =
  61. {
  62. 0x46, 0x53, 0x48, 0x03, 0x1c, 0xf0, 0xa8, 0xc9, 0x00, 0x00, 0x4c, 0x00, 0x00, 0x00, 0x76, 0x61, // FSH.......L...va
  63. 0x72, 0x79, 0x69, 0x6e, 0x67, 0x20, 0x6d, 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, // rying mediump ve
  64. 0x63, 0x34, 0x20, 0x76, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x30, 0x3b, 0x0a, 0x76, 0x6f, 0x69, // c4 v_color0;.voi
  65. 0x64, 0x20, 0x6d, 0x61, 0x69, 0x6e, 0x20, 0x28, 0x29, 0x0a, 0x7b, 0x0a, 0x20, 0x20, 0x67, 0x6c, // d main ().{. gl
  66. 0x5f, 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x63, // _FragColor = v_c
  67. 0x6f, 0x6c, 0x6f, 0x72, 0x30, 0x3b, 0x0a, 0x7d, 0x0a, 0x0a, 0x00, // olor0;.}...
  68. };
  69. #elif CROWN_PLATFORM_WINDOWS
  70. static const uint8_t vs_h[419] =
  71. {
  72. 0x56, 0x53, 0x48, 0x02, 0x1c, 0xf0, 0xa8, 0xc9, 0x01, 0x00, 0x0f, 0x75, 0x5f, 0x6d, 0x6f, 0x64, // VSH........u_mod
  73. 0x65, 0x6c, 0x56, 0x69, 0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x09, 0x01, 0x00, 0x00, 0x04, 0x00, // elViewProj......
  74. 0x80, 0x01, 0x00, 0x03, 0xfe, 0xff, 0xfe, 0xff, 0x23, 0x00, 0x43, 0x54, 0x41, 0x42, 0x1c, 0x00, // ........#.CTAB..
  75. 0x00, 0x00, 0x57, 0x00, 0x00, 0x00, 0x00, 0x03, 0xfe, 0xff, 0x01, 0x00, 0x00, 0x00, 0x1c, 0x00, // ..W.............
  76. 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x02, 0x00, // ......P...0.....
  77. 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x5f, // [email protected]_
  78. 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x56, 0x69, 0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x00, 0x03, 0x00, // modelViewProj...
  79. 0x03, 0x00, 0x04, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x76, 0x73, // ..............vs
  80. 0x5f, 0x33, 0x5f, 0x30, 0x00, 0x4d, 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66, 0x74, 0x20, 0x28, // _3_0.Microsoft (
  81. 0x52, 0x29, 0x20, 0x48, 0x4c, 0x53, 0x4c, 0x20, 0x53, 0x68, 0x61, 0x64, 0x65, 0x72, 0x20, 0x43, // R) HLSL Shader C
  82. 0x6f, 0x6d, 0x70, 0x69, 0x6c, 0x65, 0x72, 0x20, 0x39, 0x2e, 0x32, 0x39, 0x2e, 0x39, 0x35, 0x32, // ompiler 9.29.952
  83. 0x2e, 0x33, 0x31, 0x31, 0x31, 0x00, 0x51, 0x00, 0x00, 0x05, 0x04, 0x00, 0x0f, 0xa0, 0x00, 0x00, // .3111.Q.........
  84. 0x80, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x00, // .?..............
  85. 0x00, 0x02, 0x0a, 0x00, 0x00, 0x80, 0x00, 0x00, 0x0f, 0x90, 0x1f, 0x00, 0x00, 0x02, 0x00, 0x00, // ................
  86. 0x00, 0x80, 0x01, 0x00, 0x0f, 0x90, 0x1f, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, // ................
  87. 0x0f, 0xe0, 0x1f, 0x00, 0x00, 0x02, 0x0a, 0x00, 0x00, 0x80, 0x01, 0x00, 0x0f, 0xe0, 0x05, 0x00, // ................
  88. 0x00, 0x03, 0x00, 0x00, 0x0f, 0x80, 0x00, 0x00, 0xe4, 0xa0, 0x01, 0x00, 0x00, 0x90, 0x05, 0x00, // ................
  89. 0x00, 0x03, 0x01, 0x00, 0x0f, 0x80, 0x01, 0x00, 0xe4, 0xa0, 0x01, 0x00, 0x55, 0x90, 0x02, 0x00, // ............U...
  90. 0x00, 0x03, 0x00, 0x00, 0x0f, 0x80, 0x00, 0x00, 0xe4, 0x80, 0x01, 0x00, 0xe4, 0x80, 0x05, 0x00, // ................
  91. 0x00, 0x03, 0x01, 0x00, 0x0f, 0x80, 0x02, 0x00, 0xe4, 0xa0, 0x01, 0x00, 0xaa, 0x90, 0x02, 0x00, // ................
  92. 0x00, 0x03, 0x00, 0x00, 0x0f, 0x80, 0x00, 0x00, 0xe4, 0x80, 0x01, 0x00, 0xe4, 0x80, 0x01, 0x00, // ................
  93. 0x00, 0x02, 0x01, 0x00, 0x0f, 0x80, 0x03, 0x00, 0xe4, 0xa0, 0x05, 0x00, 0x00, 0x03, 0x01, 0x00, // ................
  94. 0x0f, 0x80, 0x01, 0x00, 0xe4, 0x80, 0x04, 0x00, 0x00, 0xa0, 0x02, 0x00, 0x00, 0x03, 0x00, 0x00, // ................
  95. 0x0f, 0x80, 0x00, 0x00, 0xe4, 0x80, 0x01, 0x00, 0xe4, 0x80, 0x01, 0x00, 0x00, 0x02, 0x01, 0x00, // ................
  96. 0x0f, 0x80, 0x00, 0x00, 0xe4, 0x90, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x0f, 0xe0, 0x00, 0x00, // ................
  97. 0xe4, 0x80, 0x01, 0x00, 0x00, 0x02, 0x01, 0x00, 0x0f, 0xe0, 0x01, 0x00, 0xe4, 0x80, 0xff, 0xff, // ................
  98. 0x00, 0x00, 0x00, // ...
  99. };
  100. static const uint8_t fs_h[137] =
  101. {
  102. 0x46, 0x53, 0x48, 0x02, 0x1c, 0xf0, 0xa8, 0xc9, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x03, 0xff, 0xff, // FSH.......|.....
  103. 0xfe, 0xff, 0x16, 0x00, 0x43, 0x54, 0x41, 0x42, 0x1c, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, // ....CTAB....#...
  104. 0x00, 0x03, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, // ................
  105. 0x1c, 0x00, 0x00, 0x00, 0x70, 0x73, 0x5f, 0x33, 0x5f, 0x30, 0x00, 0x4d, 0x69, 0x63, 0x72, 0x6f, // ....ps_3_0.Micro
  106. 0x73, 0x6f, 0x66, 0x74, 0x20, 0x28, 0x52, 0x29, 0x20, 0x48, 0x4c, 0x53, 0x4c, 0x20, 0x53, 0x68, // soft (R) HLSL Sh
  107. 0x61, 0x64, 0x65, 0x72, 0x20, 0x43, 0x6f, 0x6d, 0x70, 0x69, 0x6c, 0x65, 0x72, 0x20, 0x39, 0x2e, // ader Compiler 9.
  108. 0x32, 0x39, 0x2e, 0x39, 0x35, 0x32, 0x2e, 0x33, 0x31, 0x31, 0x31, 0x00, 0x1f, 0x00, 0x00, 0x02, // 29.952.3111.....
  109. 0x0a, 0x00, 0x00, 0x80, 0x00, 0x00, 0x0f, 0x90, 0x01, 0x00, 0x00, 0x02, 0x00, 0x08, 0x0f, 0x80, // ................
  110. 0x00, 0x00, 0xe4, 0x90, 0xff, 0xff, 0x00, 0x00, 0x00, // .........
  111. };
  112. #endif
  113. static bgfx::VertexDecl s_decl;
  114. static bgfx::ProgramHandle s_prog;
  115. void init()
  116. {
  117. s_decl
  118. .begin()
  119. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  120. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  121. .end();
  122. bgfx::ShaderHandle vs = bgfx::createShader(
  123. bgfx::makeRef(vs_h, sizeof(vs_h)));
  124. bgfx::ShaderHandle fs = bgfx::createShader(
  125. bgfx::makeRef(fs_h, sizeof(fs_h)));
  126. s_prog = bgfx::createProgram(vs, fs, true);
  127. }
  128. void shutdown()
  129. {
  130. bgfx::destroyProgram(s_prog);
  131. }
  132. } // namespace debug_line
  133. DebugLine::DebugLine(bool depth_test)
  134. : _depth_test(depth_test)
  135. , _num(0)
  136. {
  137. }
  138. void DebugLine::add_line(const Vector3& start, const Vector3& end, const Color4& color)
  139. {
  140. if (_num >= CE_MAX_DEBUG_LINES)
  141. return;
  142. _lines[_num].p0[0] = start.x;
  143. _lines[_num].p0[1] = start.y;
  144. _lines[_num].p0[2] = start.z;
  145. _lines[_num].c0 = color4::to_abgr(color);
  146. _lines[_num].p1[0] = end.x;
  147. _lines[_num].p1[1] = end.y;
  148. _lines[_num].p1[2] = end.z;
  149. _lines[_num].c1 = color4::to_abgr(color);
  150. _num++;
  151. }
  152. void DebugLine::add_sphere(const Vector3& center, const float radius, const Color4& color)
  153. {
  154. const uint32_t deg_step = 15;
  155. for (uint32_t deg = 0; deg < 360; deg += deg_step)
  156. {
  157. const float rad0 = to_rad((float) deg);
  158. const float rad1 = to_rad((float) deg + deg_step);
  159. // XZ plane
  160. const Vector3 start0(cos(rad0) * radius, 0, -sin(rad0) * radius);
  161. const Vector3 end0 (cos(rad1) * radius, 0, -sin(rad1) * radius);
  162. add_line(center + start0, center + end0, color);
  163. // XY plane
  164. const Vector3 start1(cos(rad0) * radius, sin(rad0) * radius, 0);
  165. const Vector3 end1 (cos(rad1) * radius, sin(rad1) * radius, 0);
  166. add_line(center + start1, center + end1, color);
  167. // YZ plane
  168. const Vector3 start2(0, sin(rad0) * radius, -cos(rad0) * radius);
  169. const Vector3 end2 (0, sin(rad1) * radius, -cos(rad1) * radius);
  170. add_line(center + start2, center + end2, color);
  171. }
  172. }
  173. void DebugLine::add_obb(const Matrix4x4& tm, const Vector3& extents, const Color4& color)
  174. {
  175. const Vector3 o = Vector3(tm.t.x, tm.t.y, tm.t.z);
  176. const Vector3 x = Vector3(tm.x.x, tm.x.y, tm.x.z) * (extents.x * 0.5f);
  177. const Vector3 y = Vector3(tm.y.x, tm.y.y, tm.y.z) * (extents.y * 0.5f);
  178. const Vector3 z = Vector3(tm.z.x, tm.z.y, tm.z.z) * (extents.z * 0.5f);
  179. // Back face
  180. add_line(o - x - y - z, o + x - y - z, color);
  181. add_line(o + x - y - z, o + x + y - z, color);
  182. add_line(o + x + y - z, o - x + y - z, color);
  183. add_line(o - x + y - z, o - x - y - z, color);
  184. add_line(o - x - y + z, o + x - y + z, color);
  185. add_line(o + x - y + z, o + x + y + z, color);
  186. add_line(o + x + y + z, o - x + y + z, color);
  187. add_line(o - x + y + z, o - x - y + z, color);
  188. add_line(o - x - y - z, o - x - y + z, color);
  189. add_line(o + x - y - z, o + x - y + z, color);
  190. add_line(o + x + y - z, o + x + y + z, color);
  191. add_line(o - x + y - z, o - x + y + z, color);
  192. }
  193. void DebugLine::clear()
  194. {
  195. _num = 0;
  196. }
  197. void DebugLine::commit()
  198. {
  199. if (!_num)
  200. return;
  201. bgfx::TransientVertexBuffer tvb;
  202. bgfx::allocTransientVertexBuffer(&tvb, CE_MAX_DEBUG_LINES * 2, debug_line::s_decl);
  203. memcpy(tvb.data, _lines, sizeof(Line) * _num);
  204. bgfx::setState(BGFX_STATE_PT_LINES
  205. | BGFX_STATE_RGB_WRITE
  206. | (_depth_test ? (BGFX_STATE_DEPTH_TEST_LESS | BGFX_STATE_DEPTH_WRITE) : 0)
  207. | BGFX_STATE_CULL_CW);
  208. bgfx::setProgram(debug_line::s_prog);
  209. bgfx::setVertexBuffer(&tvb, 0, _num * 2);
  210. bgfx::submit(0);
  211. }
  212. } // namespace crown