You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

264 line
8.1 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include "core.h"
  14. namespace lol
  15. {
  16. //Resets draw infos
  17. void Debug::DrawSetupReset()
  18. {
  19. g_scene->SetLineTime();
  20. g_scene->SetLineMask();
  21. }
  22. //Sets draw infos
  23. void Debug::DrawSetupTime(float new_time)
  24. {
  25. g_scene->SetLineTime(new_time);
  26. }
  27. //--
  28. void Debug::DrawSetupMask(int new_mask)
  29. {
  30. g_scene->SetLineMask(new_mask);
  31. }
  32. //--
  33. void Debug::DrawSetupSegment(float new_segment_size)
  34. {
  35. g_scene->SetLineSegmentSize(new_segment_size);
  36. }
  37. //--
  38. void Debug::DrawSetupColor(vec4 color)
  39. {
  40. g_scene->SetLineColor(color);
  41. }
  42. //--
  43. void Debug::DrawSetup(float new_time, int new_mask, float segment_size, vec4 color)
  44. {
  45. Debug::DrawSetupTime(new_time);
  46. Debug::DrawSetupMask(new_mask);
  47. Debug::DrawSetupSegment(segment_size);
  48. Debug::DrawSetupColor(color);
  49. }
  50. //Draw stuff
  51. void Debug::DrawLine(vec3 a, vec3 b)
  52. {
  53. Debug::DrawLine(a, b, g_scene->GetLineColor());
  54. }
  55. void Debug::DrawLine(vec3 a, vec3 b, vec4 color)
  56. { g_scene->AddLine(a, b, color); }
  57. //--
  58. void Debug::DrawBox(box3 a)
  59. { DrawBox(a.A, a.B, g_scene->GetLineColor()); }
  60. void Debug::DrawBox(box3 a, vec4 color)
  61. {
  62. DrawBox(a.A, a.B, color);
  63. }
  64. //--
  65. void Debug::DrawBox(vec3 a, vec3 b)
  66. { Debug::DrawBox(a, b, g_scene->GetLineColor()); }
  67. void Debug::DrawBox(vec3 a, vec3 b, vec4 color)
  68. {
  69. Debug::DrawBox(a, b, mat4::identity, color);
  70. }
  71. //--
  72. void Debug::DrawBox(box3 a, mat4 transform)
  73. { DrawBox(a.A, a.B, transform, g_scene->GetLineColor()); }
  74. void Debug::DrawBox(box3 a, mat4 transform, vec4 color)
  75. {
  76. DrawBox(a.A, a.B, transform, color);
  77. }
  78. //--
  79. void Debug::DrawBox(vec3 a, vec3 b, mat4 transform)
  80. { Debug::DrawBox(a, b, transform, g_scene->GetLineColor()); }
  81. void Debug::DrawBox(vec3 a, vec3 b, mat4 transform, vec4 color)
  82. {
  83. vec4 v[8];
  84. for (int i = 0; i < 8; i++)
  85. {
  86. v[i].x = i & 1 ? a.x : b.x;
  87. v[i].y = i & 2 ? a.y : b.y;
  88. v[i].z = i & 4 ? a.z : b.z;
  89. v[i].w = 1.f;
  90. }
  91. for (int i = 0; i < 4; i++)
  92. {
  93. int j = ((i & 1) << 1) | ((i >> 1) ^ 1);
  94. Debug::DrawLine((transform * v[i]).xyz, (transform * v[i + 4]).xyz, color);
  95. Debug::DrawLine((transform * v[i]).xyz, (transform * v[j]).xyz, color);
  96. Debug::DrawLine((transform * v[i + 4]).xyz, (transform * v[j + 4]).xyz, color);
  97. }
  98. }
  99. //--
  100. void Debug::DrawCircle(vec3 a, vec3 n)
  101. { Debug::DrawCircle(a, n, g_scene->GetLineColor()); }
  102. void Debug::DrawCircle(vec3 a, vec3 n, vec4 color)
  103. {
  104. vec3 x = orthogonal(n);
  105. vec3 y = cross(normalize(n), normalize(x)) * length(n);
  106. DrawCircle(a, x, y, color);
  107. }
  108. //--
  109. void Debug::DrawCircle(vec3 a, vec3 x, vec3 y)
  110. { Debug::DrawCircle(a, x, y, g_scene->GetLineColor()); }
  111. void Debug::DrawCircle(vec3 a, vec3 x, vec3 y, vec4 color)
  112. {
  113. float size = F_PI * 2.f * lol::max(length(x), length(y));
  114. int segment_nb = lol::max(1, (int)((size * .25f) / g_scene->GetLineSegmentSize()));
  115. for (int i = 0; i < segment_nb; i++)
  116. {
  117. float a0 = (((float)i) / (float)segment_nb) * F_PI_2;
  118. float a1 = (((float)i + 1) / (float)segment_nb) * F_PI_2;
  119. vec2 p0 = vec2(lol::cos(a0), lol::sin(a0));
  120. vec2 p1 = vec2(lol::cos(a1), lol::sin(a1));
  121. Debug::DrawLine(a + p0.x * x + p0.y * y, a + p1.x * x + p1.y * y, color);
  122. Debug::DrawLine(a + p0.x * -x + p0.y * -y, a + p1.x * -x + p1.y * -y, color);
  123. Debug::DrawLine(a + p0.x * x + p0.y * -y, a + p1.x * x + p1.y * -y, color);
  124. Debug::DrawLine(a + p0.x * -x + p0.y * y, a + p1.x * -x + p1.y * y, color);
  125. }
  126. }
  127. //--
  128. void Debug::DrawSphere(vec3 a, float s)
  129. { Debug::DrawSphere(a, s, g_scene->GetLineColor()); }
  130. void Debug::DrawSphere(vec3 a, float s, vec4 color)
  131. {
  132. Debug::DrawSphere(a, vec3::axis_x * s, vec3::axis_y * s, vec3::axis_z * s, color);
  133. }
  134. //--
  135. void Debug::DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z)
  136. { Debug::DrawSphere(a, x, y, z, g_scene->GetLineColor()); }
  137. void Debug::DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z, vec4 color)
  138. {
  139. Debug::DrawCircle(a, x, y, color);
  140. Debug::DrawCircle(a, y, z, color);
  141. Debug::DrawCircle(a, z, x, color);
  142. }
  143. //--
  144. void Debug::DrawCapsule(vec3 a, float s, vec3 h)
  145. { Debug::DrawCapsule(a, s, h, g_scene->GetLineColor()); }
  146. void Debug::DrawCapsule(vec3 a, float s, vec3 h, vec4 color)
  147. {
  148. vec3 x = orthonormal(h) * s;
  149. vec3 y = cross(normalize(h), normalize(x)) * s;
  150. Debug::DrawCapsule(a, x, y, normalize(h) * s, h, color);
  151. }
  152. //--
  153. void Debug::DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h)
  154. { Debug::DrawCapsule(a, x, y, z, h, g_scene->GetLineColor()); }
  155. void Debug::DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h, vec4 color)
  156. {
  157. float size = F_PI * 2.f * lol::max(length(x), length(y));
  158. int segment_nb = lol::max(1, (int)((size * .25f) / g_scene->GetLineSegmentSize()));
  159. for (int i = -1; i < 2; i += 2)
  160. {
  161. vec3 b = a + h * .5f * (float)i;
  162. vec3 c = a - h * .5f * (float)i;
  163. Debug::DrawCircle(b, x, y, color);
  164. Debug::DrawLine(b + x * (float)i, c + x * (float)i, color);
  165. Debug::DrawLine(b + y * (float)i, c + y * (float)i, color);
  166. for (int j = 0; j < segment_nb; j++)
  167. {
  168. float a0 = (((float)j) / (float)segment_nb) * F_PI_2;
  169. float a1 = (((float)j + 1) / (float)segment_nb) * F_PI_2;
  170. vec2 p0 = vec2(lol::cos(a0), lol::sin(a0));
  171. vec2 p1 = vec2(lol::cos(a1), lol::sin(a1));
  172. Debug::DrawLine(b + p0.x * x + p0.y * z * (float)i, b + p1.x * x + p1.y * z * (float)i, color);
  173. Debug::DrawLine(b + p0.x * -x + p0.y * z * (float)i, b + p1.x * -x + p1.y * z * (float)i, color);
  174. Debug::DrawLine(b + p0.x * y + p0.y * z * (float)i, b + p1.x * y + p1.y * z * (float)i, color);
  175. Debug::DrawLine(b + p0.x * -y + p0.y * z * (float)i, b + p1.x * -y + p1.y * z * (float)i, color);
  176. }
  177. }
  178. }
  179. //--
  180. void Debug::DrawViewProj(mat4 view, mat4 proj)
  181. { Debug::DrawViewProj(view, proj, g_scene->GetLineColor()); }
  182. void Debug::DrawViewProj(mat4 view, mat4 proj, vec4 color)
  183. {
  184. mat4 const view_proj = proj * view;
  185. //Pos to center
  186. vec3 p0 = (inverse(view) * vec4(vec3::zero, 1.f)).xyz;
  187. //Near plane
  188. mat4 const inv_view_proj = inverse(view_proj);
  189. vec4 p[4] = { inv_view_proj * vec4(-1.f, 1.f, -1.f, 1.f),
  190. inv_view_proj * vec4( 1.f, 1.f, -1.f, 1.f),
  191. inv_view_proj * vec4( 1.f,-1.f, -1.f, 1.f),
  192. inv_view_proj * vec4(-1.f,-1.f, -1.f, 1.f) };
  193. for (int i = 0; i < 4; i++)
  194. p[i] = p[i] / p[i].w;
  195. //Draw near
  196. for (int i = 0; i < 4; i++)
  197. Debug::DrawLine(p[i].xyz, p0, color);
  198. Debug::DrawViewProj(view_proj, color);
  199. }
  200. //--
  201. void Debug::DrawViewProj(mat4 view_proj)
  202. { Debug::DrawViewProj(view_proj, g_scene->GetLineColor()); }
  203. void Debug::DrawViewProj(mat4 view_proj, vec4 color)
  204. {
  205. //Near plane
  206. mat4 const inv_view_proj = inverse(view_proj);
  207. vec4 p[8] = { inv_view_proj * vec4(-1.f, 1.f, 1.f, 1.f),
  208. inv_view_proj * vec4( 1.f, 1.f, 1.f, 1.f),
  209. inv_view_proj * vec4( 1.f,-1.f, 1.f, 1.f),
  210. inv_view_proj * vec4(-1.f,-1.f, 1.f, 1.f),
  211. inv_view_proj * vec4(-1.f, 1.f,-1.f, 1.f),
  212. inv_view_proj * vec4( 1.f, 1.f,-1.f, 1.f),
  213. inv_view_proj * vec4( 1.f,-1.f,-1.f, 1.f),
  214. inv_view_proj * vec4(-1.f,-1.f,-1.f, 1.f)
  215. };
  216. for (int i = 0; i < 8; i++)
  217. p[i] = p[i] / p[i].w;
  218. //Draw near
  219. for (int i = 0; i < 4; i++)
  220. Debug::DrawLine(p[i].xyz, p[(i + 1) % 4].xyz, color);
  221. //Draw far
  222. for (int i = 4; i < 8; i++)
  223. Debug::DrawLine(p[i].xyz, p[(i - 4 + 1) % 4 + 4].xyz, color);
  224. //Draw near to far
  225. for (int i = 0; i < 4; i++)
  226. Debug::DrawLine(p[i].xyz, p[i + 4].xyz, color);
  227. //Draw diagonal
  228. for (int i = 2; i < 6; i++)
  229. Debug::DrawLine(p[i].xyz, p[i + ((i < 4)?(-2):(+2))].xyz, color);
  230. }
  231. } /* namespace lol */