Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

393 righe
16 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. //Screen to world conversion
  51. vec3 Debug::WorldToScreen(vec3 pos)
  52. {
  53. return Debug::WorldToScreen(vec4(pos, 1.f));
  54. }
  55. vec3 Debug::WorldToScreen(vec3 pos, mat4 view_proj)
  56. {
  57. return Debug::WorldToScreen(vec4(pos, 1.f), view_proj);
  58. }
  59. vec3 Debug::WorldToScreen(vec4 pos)
  60. {
  61. if (!g_scene || !g_scene->GetCamera())
  62. return vec3::zero;
  63. mat4 const view_proj = g_scene->GetCamera()->GetProjection() * g_scene->GetCamera()->GetView();
  64. return Debug::WorldToScreen(pos, view_proj);
  65. }
  66. vec3 Debug::WorldToScreen(vec4 pos, mat4 view_proj)
  67. {
  68. vec4 screen_pos = view_proj * pos;
  69. return (screen_pos / screen_pos.w).xyz;
  70. }
  71. //--
  72. vec3 Debug::ScreenToWorld(vec2 pos, float z)
  73. { return Debug::ScreenToWorld(vec3(pos, z)); }
  74. vec3 Debug::ScreenToWorld(vec3 pos)
  75. {
  76. if (!g_scene || !g_scene->GetCamera())
  77. return vec3::zero;
  78. mat4 const inv_view_proj = inverse(g_scene->GetCamera()->GetProjection() * g_scene->GetCamera()->GetView());
  79. return Debug::ScreenToWorld(pos, inv_view_proj);
  80. }
  81. vec3 Debug::ScreenToWorld(vec2 pos, mat4 inv_view_proj, float z)
  82. { return Debug::ScreenToWorld(vec3(pos, z), inv_view_proj); }
  83. vec3 Debug::ScreenToWorld(vec3 pos, mat4 inv_view_proj)
  84. {
  85. vec4 screen_pos = inv_view_proj * vec4(pos, 1.f);
  86. return (screen_pos / screen_pos.w).xyz;
  87. }
  88. vec3 Debug::ScreenToWorld(vec3 pos, mat4 view, mat4 proj)
  89. {
  90. vec4 screen_pos = inverse(proj) * vec4(pos, 1.f);
  91. return (inverse(view) * (screen_pos / screen_pos.w)).xyz;
  92. }
  93. //Draw stuff in World
  94. //-- LINE: 3D -2D - 3D_to_2D
  95. void Debug::DrawLine(vec3 a, vec3 b) { Debug::DrawLine(a, b, g_scene->GetLineColor()); }
  96. void Debug::DrawLine(vec2 a, vec3 b, float az) { Debug::DrawLine(a, b, g_scene->GetLineColor(), az); }
  97. void Debug::DrawLine(vec2 a, vec2 b, float az, float bz) { Debug::DrawLine(a, b, g_scene->GetLineColor(), az, bz); }
  98. void Debug::DrawLine(vec3 a, vec3 b, vec4 color) { g_scene->AddLine(a, b, color); }
  99. void Debug::DrawLine(vec2 a, vec3 b, vec4 color, float az) { g_scene->AddLine(a, b, color, az); }
  100. void Debug::DrawLine(vec2 a, vec2 b, vec4 color, float az, float bz){ g_scene->AddLine(a, b, color, az, bz); }
  101. //-- ARROW: 3D -2D - 3D_to_2D
  102. void Debug::DrawArrow(vec3 a, vec3 b, vec2 s) { Debug::DrawArrow(a, b, vec3(s.x, s.y, s.y)); }
  103. void Debug::DrawArrow(vec2 a, vec3 b, vec2 s, float az) { Debug::DrawArrow(a, b.xy, vec3(s.x, s.y, s.y), az, b.z); }
  104. void Debug::DrawArrow(vec2 a, vec2 b, vec2 s, float az, float bz) { Debug::DrawArrow(a, b, vec3(s.x, s.y, s.y), az, bz); }
  105. void Debug::DrawArrow(vec3 a, vec3 b, vec3 s) { Debug::DrawArrow(a, b, vec3(s.x, s.y, s.y), g_scene->GetLineColor()); }
  106. void Debug::DrawArrow(vec2 a, vec3 b, vec3 s, float az) { Debug::DrawArrow(a, b.xy, vec3(s.x, s.y, s.y), g_scene->GetLineColor(), az, b.z); }
  107. void Debug::DrawArrow(vec2 a, vec2 b, vec3 s, float az, float bz) { Debug::DrawArrow(a, b, vec3(s.x, s.y, s.y), g_scene->GetLineColor(), az, bz); }
  108. void Debug::DrawArrow(vec3 a, vec3 b, vec3 s, vec4 color)
  109. {
  110. vec3 z = s.x * normalize(b - a);
  111. vec3 x = s.z * orthonormal(b - a);
  112. vec3 y = s.y * cross(normalize(x), normalize(z));
  113. Debug::DrawLine(a, b, color);
  114. Debug::DrawLine(b, b - z + x, color);
  115. Debug::DrawLine(b, b - z - x, color);
  116. Debug::DrawLine(b, b - z + y, color);
  117. Debug::DrawLine(b, b - z - y, color);
  118. Debug::DrawLine(b - z + x, b - z + y, color);
  119. Debug::DrawLine(b - z + x, b - z - y, color);
  120. Debug::DrawLine(b - z - x, b - z + y, color);
  121. Debug::DrawLine(b - z - x, b - z - y, color);
  122. Debug::DrawLine(b - z + x, b - z - x, color);
  123. Debug::DrawLine(b - z + y, b - z - y, color);
  124. }
  125. void Debug::DrawArrow(vec2 a, vec3 b, vec3 s, vec4 color, float az)
  126. {
  127. vec3 bn = Debug::WorldToScreen(b);
  128. DrawArrow(a, bn.xy, s, color, az, bn.z);
  129. }
  130. void Debug::DrawArrow(vec2 a, vec2 b, vec3 s, vec4 color, float az, float bz)
  131. {
  132. vec3 an = vec3(a, az);
  133. vec3 bn = vec3(b, bz);
  134. vec3 z = s.x * normalize(bn - an);
  135. vec3 x = s.z * orthonormal(bn - an);
  136. vec3 y = s.y * cross(normalize(x), normalize(z));
  137. Debug::DrawLine(a, b, color, az, bz);
  138. Debug::DrawLine(b, b - (z + x).xy, color, bz, bz - (z + x).z);
  139. Debug::DrawLine(b, b - (z - x).xy, color, bz, bz - (z - x).z);
  140. Debug::DrawLine(b, b - (z + y).xy, color, bz, bz - (z + y).z);
  141. Debug::DrawLine(b, b - (z - y).xy, color, bz, bz - (z - y).z);
  142. Debug::DrawLine(b - (z + x).xy, b - (z + y).xy, color, bz - (z + x).z, bz - (z + y).z);
  143. Debug::DrawLine(b - (z + x).xy, b - (z - y).xy, color, bz - (z + x).z, bz - (z - y).z);
  144. Debug::DrawLine(b - (z - x).xy, b - (z + y).xy, color, bz - (z - x).z, bz - (z + y).z);
  145. Debug::DrawLine(b - (z - x).xy, b - (z - y).xy, color, bz - (z - x).z, bz - (z - y).z);
  146. Debug::DrawLine(b - (z + x).xy, b - (z - x).xy, color, bz - (z + x).z, bz - (z - x).z);
  147. Debug::DrawLine(b - (z + y).xy, b - (z - y).xy, color, bz - (z + y).z, bz - (z - y).z);
  148. }
  149. //-- BOX: 3D -2D - 3D_to_2D
  150. void Debug::DrawBox(box3 a) { Debug::DrawBox(a.A, a.B, g_scene->GetLineColor()); }
  151. void Debug::DrawBox(box2 a) { Debug::DrawBox(a.A, a.B, g_scene->GetLineColor()); }
  152. void Debug::DrawBox(box3 a, vec4 color) { Debug::DrawBox(a.A, a.B, color); }
  153. void Debug::DrawBox(box2 a, vec4 color) { Debug::DrawBox(a.A, a.B, color); }
  154. void Debug::DrawBox(vec3 a, vec3 b) { Debug::DrawBox(a, b, g_scene->GetLineColor()); }
  155. void Debug::DrawBox(vec2 a, vec2 b) { Debug::DrawBox(a, b, g_scene->GetLineColor()); }
  156. void Debug::DrawBox(vec2 a, float s) { Debug::DrawBox(a, s, g_scene->GetLineColor()); }
  157. void Debug::DrawBox(vec3 a, vec3 b, vec4 color) { Debug::DrawBox(a, b, mat4::identity, color); }
  158. void Debug::DrawBox(vec2 a, vec2 b, vec4 color) { Debug::DrawBox(a, b, mat2::identity, color); }
  159. void Debug::DrawBox(vec2 a, float s, vec4 color) { Debug::DrawBox(a, s, mat2::identity, color); }
  160. void Debug::DrawBox(box3 a, mat4 transform) { Debug::DrawBox(a.A, a.B, transform, g_scene->GetLineColor()); }
  161. void Debug::DrawBox(box2 a, mat2 transform) { Debug::DrawBox(a.A, a.B, transform, g_scene->GetLineColor()); }
  162. void Debug::DrawBox(box3 a, mat4 transform, vec4 color) { Debug::DrawBox(a.A, a.B, transform, color); }
  163. void Debug::DrawBox(box2 a, mat2 transform, vec4 color) { Debug::DrawBox(a.A, a.B, transform, color); }
  164. void Debug::DrawBox(vec3 a, vec3 b, mat4 transform) { Debug::DrawBox(a, b, transform, g_scene->GetLineColor()); }
  165. void Debug::DrawBox(vec2 a, vec2 b, mat2 transform) { Debug::DrawBox(a, b, transform, g_scene->GetLineColor()); }
  166. void Debug::DrawBox(vec2 a, float s, mat2 transform) { Debug::DrawBox(a, s, transform, g_scene->GetLineColor()); }
  167. void Debug::DrawBox(vec3 a, vec3 b, mat4 transform, vec4 color)
  168. {
  169. vec4 v[8];
  170. for (int i = 0; i < 8; i++)
  171. {
  172. v[i].x = i & 1 ? a.x : b.x;
  173. v[i].y = i & 2 ? a.y : b.y;
  174. v[i].z = i & 4 ? a.z : b.z;
  175. v[i].w = 1.f;
  176. }
  177. for (int i = 0; i < 4; i++)
  178. {
  179. int j = ((i & 1) << 1) | ((i >> 1) ^ 1);
  180. Debug::DrawLine((transform * v[i]).xyz, (transform * v[i + 4]).xyz, color);
  181. Debug::DrawLine((transform * v[i]).xyz, (transform * v[j]).xyz, color);
  182. Debug::DrawLine((transform * v[i + 4]).xyz, (transform * v[j + 4]).xyz, color);
  183. }
  184. }
  185. void Debug::DrawBox(vec2 a, vec2 b, mat2 transform, vec4 color)
  186. {
  187. vec2 v[4];
  188. v[0] = a;
  189. v[1] = vec2(a.x, b.y);
  190. v[2] = b;
  191. v[3] = vec2(b.x, a.y);
  192. int i = 0;
  193. Debug::DrawLine((transform * v[0]).xy, (transform * v[1]).xy, color);
  194. Debug::DrawLine((transform * v[1]).xy, (transform * v[2]).xy, color);
  195. Debug::DrawLine((transform * v[2]).xy, (transform * v[3]).xy, color);
  196. Debug::DrawLine((transform * v[3]).xy, (transform * v[0]).xy, color);
  197. }
  198. void Debug::DrawBox(vec2 a, float s, mat2 transform, vec4 color)
  199. {
  200. vec2 b = s * vec2(1.f, g_renderer->GetXYRatio());
  201. Debug::DrawBox(a - b, a + b, transform, color);
  202. }
  203. //-- CIRCLE
  204. void Debug::DrawCircle(vec2 a, float s) { Debug::DrawCircle(a, s * vec2(1.f, g_renderer->GetXYRatio()), g_scene->GetLineColor()); }
  205. void Debug::DrawCircle(vec3 a, vec3 n) { Debug::DrawCircle(a, n, g_scene->GetLineColor()); }
  206. void Debug::DrawCircle(vec2 a, vec2 s) { Debug::DrawCircle(a, s * vec2(1.f, g_renderer->GetXYRatio()), g_scene->GetLineColor()); }
  207. void Debug::DrawCircle(vec3 a, vec3 x, vec3 y) { Debug::DrawCircle(a, x, y, g_scene->GetLineColor()); }
  208. void Debug::DrawCircle(vec2 a, vec2 x, vec2 y) { Debug::DrawCircle(a, x, y, g_scene->GetLineColor()); }
  209. void Debug::DrawCircle(vec3 a, vec3 n, vec4 color)
  210. {
  211. vec3 x = orthogonal(n);
  212. vec3 y = cross(normalize(n), normalize(x)) * length(n);
  213. DrawCircle(a, x, y, color);
  214. }
  215. void Debug::DrawCircle(vec2 a, vec2 s, vec4 color)
  216. {
  217. vec2 x = vec2::axis_x * s.x;
  218. vec2 y = vec2::axis_y * s.y;
  219. DrawCircle(a, x, y, color);
  220. }
  221. //--
  222. void Debug::DrawCircle(vec3 a, vec3 x, vec3 y, vec4 color)
  223. {
  224. float size = F_PI * 2.f * lol::max(length(x), length(y));
  225. int segment_nb = lol::max(1, (int)((size * .25f) / g_scene->GetLineSegmentSize()));
  226. for (int i = 0; i < segment_nb; i++)
  227. {
  228. float a0 = (((float)i) / (float)segment_nb) * F_PI_2;
  229. float a1 = (((float)i + 1) / (float)segment_nb) * F_PI_2;
  230. vec2 p0 = vec2(lol::cos(a0), lol::sin(a0));
  231. vec2 p1 = vec2(lol::cos(a1), lol::sin(a1));
  232. Debug::DrawLine(a + p0.x * x + p0.y * y, a + p1.x * x + p1.y * y, color);
  233. Debug::DrawLine(a + p0.x * -x + p0.y * -y, a + p1.x * -x + p1.y * -y, color);
  234. Debug::DrawLine(a + p0.x * x + p0.y * -y, a + p1.x * x + p1.y * -y, color);
  235. Debug::DrawLine(a + p0.x * -x + p0.y * y, a + p1.x * -x + p1.y * y, color);
  236. }
  237. }
  238. //--
  239. void Debug::DrawCircle(vec2 a, vec2 x, vec2 y, vec4 color)
  240. {
  241. float size = F_PI * 2.f * lol::max(length(x), length(y));
  242. int segment_nb = lol::max(1, (int)((size * .25f) / g_scene->GetLineSegmentSize()));
  243. for (int i = 0; i < segment_nb; i++)
  244. {
  245. float a0 = (((float)i) / (float)segment_nb) * F_PI_2;
  246. float a1 = (((float)i + 1) / (float)segment_nb) * F_PI_2;
  247. vec2 p0 = vec2(lol::cos(a0), lol::sin(a0));
  248. vec2 p1 = vec2(lol::cos(a1), lol::sin(a1));
  249. Debug::DrawLine(a + p0.x * x + p0.y * y, a + p1.x * x + p1.y * y, color);
  250. Debug::DrawLine(a + p0.x * -x + p0.y * -y, a + p1.x * -x + p1.y * -y, color);
  251. Debug::DrawLine(a + p0.x * x + p0.y * -y, a + p1.x * x + p1.y * -y, color);
  252. Debug::DrawLine(a + p0.x * -x + p0.y * y, a + p1.x * -x + p1.y * y, color);
  253. }
  254. }
  255. //-- SPHERE
  256. void Debug::DrawSphere(vec3 a, float s) { Debug::DrawSphere(a, s, g_scene->GetLineColor()); }
  257. void Debug::DrawSphere(vec3 a, float s, vec4 color) { Debug::DrawSphere(a, vec3::axis_x * s, vec3::axis_y * s, vec3::axis_z * s, color); }
  258. void Debug::DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z) { Debug::DrawSphere(a, x, y, z, g_scene->GetLineColor()); }
  259. void Debug::DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z, vec4 color)
  260. {
  261. Debug::DrawCircle(a, x, y, color);
  262. Debug::DrawCircle(a, x, (y + z) * .707f, color);
  263. Debug::DrawCircle(a, x, (y - z) * .707f, color);
  264. Debug::DrawCircle(a, z, x, color);
  265. Debug::DrawCircle(a, z, (x + y) * .707f, color);
  266. Debug::DrawCircle(a, z, (x - y) * .707f, color);
  267. Debug::DrawCircle(a, y, z, color);
  268. Debug::DrawCircle(a, y, (z + x) * .707f, color);
  269. Debug::DrawCircle(a, y, (z - x) * .707f, color);
  270. }
  271. //-- CAPSULE
  272. void Debug::DrawCapsule(vec3 a, float s, vec3 h) { Debug::DrawCapsule(a, s, h, g_scene->GetLineColor()); }
  273. void Debug::DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h) { Debug::DrawCapsule(a, x, y, z, h, g_scene->GetLineColor()); }
  274. void Debug::DrawCapsule(vec3 a, float s, vec3 h, vec4 color)
  275. {
  276. vec3 x = orthonormal(h) * s;
  277. vec3 y = cross(normalize(h), normalize(x)) * s;
  278. Debug::DrawCapsule(a, x, y, normalize(h) * s, h, color);
  279. }
  280. //--
  281. void Debug::DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h, vec4 color)
  282. {
  283. float size = F_PI * 2.f * lol::max(length(x), length(y));
  284. int segment_nb = lol::max(1, (int)((size * .25f) / g_scene->GetLineSegmentSize()));
  285. for (int i = -1; i < 2; i += 2)
  286. {
  287. vec3 b = a + h * .5f * (float)i;
  288. vec3 c = a - h * .5f * (float)i;
  289. Debug::DrawCircle(b, x, y, color);
  290. Debug::DrawLine(b + x * (float)i, c + x * (float)i, color);
  291. Debug::DrawLine(b + y * (float)i, c + y * (float)i, color);
  292. for (int j = 0; j < segment_nb; j++)
  293. {
  294. float a0 = (((float)j) / (float)segment_nb) * F_PI_2;
  295. float a1 = (((float)j + 1) / (float)segment_nb) * F_PI_2;
  296. vec2 p0 = vec2(lol::cos(a0), lol::sin(a0));
  297. vec2 p1 = vec2(lol::cos(a1), lol::sin(a1));
  298. Debug::DrawLine(b + p0.x * x + p0.y * z * (float)i, b + p1.x * x + p1.y * z * (float)i, color);
  299. Debug::DrawLine(b + p0.x * -x + p0.y * z * (float)i, b + p1.x * -x + p1.y * z * (float)i, color);
  300. Debug::DrawLine(b + p0.x * y + p0.y * z * (float)i, b + p1.x * y + p1.y * z * (float)i, color);
  301. Debug::DrawLine(b + p0.x * -y + p0.y * z * (float)i, b + p1.x * -y + p1.y * z * (float)i, color);
  302. }
  303. }
  304. }
  305. //-- VIEW PROJ
  306. void Debug::DrawViewProj(mat4 view, mat4 proj) { Debug::DrawViewProj(view, proj, g_scene->GetLineColor()); }
  307. void Debug::DrawViewProj(mat4 view_proj) { Debug::DrawViewProj(view_proj, g_scene->GetLineColor()); }
  308. void Debug::DrawViewProj(mat4 view, mat4 proj, vec4 color)
  309. {
  310. mat4 const view_proj = proj * view;
  311. //Pos to center
  312. vec3 p0 = (inverse(view) * vec4(vec3::zero, 1.f)).xyz;
  313. //Near plane
  314. mat4 const inv_view_proj = inverse(view_proj);
  315. vec4 p[4] = { inv_view_proj * vec4(-1.f, 1.f, -1.f, 1.f),
  316. inv_view_proj * vec4( 1.f, 1.f, -1.f, 1.f),
  317. inv_view_proj * vec4( 1.f,-1.f, -1.f, 1.f),
  318. inv_view_proj * vec4(-1.f,-1.f, -1.f, 1.f) };
  319. for (int i = 0; i < 4; i++)
  320. p[i] = p[i] / p[i].w;
  321. //Draw near
  322. for (int i = 0; i < 4; i++)
  323. Debug::DrawLine(p[i].xyz, p0, color);
  324. Debug::DrawViewProj(view_proj, color);
  325. }
  326. //--
  327. void Debug::DrawViewProj(mat4 view_proj, vec4 color)
  328. {
  329. //Near plane
  330. mat4 const inv_view_proj = inverse(view_proj);
  331. vec4 p[8] = { inv_view_proj * vec4(-1.f, 1.f, 1.f, 1.f),
  332. inv_view_proj * vec4( 1.f, 1.f, 1.f, 1.f),
  333. inv_view_proj * vec4( 1.f,-1.f, 1.f, 1.f),
  334. inv_view_proj * vec4(-1.f,-1.f, 1.f, 1.f),
  335. inv_view_proj * vec4(-1.f, 1.f,-1.f, 1.f),
  336. inv_view_proj * vec4( 1.f, 1.f,-1.f, 1.f),
  337. inv_view_proj * vec4( 1.f,-1.f,-1.f, 1.f),
  338. inv_view_proj * vec4(-1.f,-1.f,-1.f, 1.f)
  339. };
  340. for (int i = 0; i < 8; i++)
  341. p[i] = p[i] / p[i].w;
  342. //Draw near
  343. for (int i = 0; i < 4; i++)
  344. Debug::DrawLine(p[i].xyz, p[(i + 1) % 4].xyz, color);
  345. //Draw far
  346. for (int i = 4; i < 8; i++)
  347. Debug::DrawLine(p[i].xyz, p[(i - 4 + 1) % 4 + 4].xyz, color);
  348. //Draw near to far
  349. for (int i = 0; i < 4; i++)
  350. Debug::DrawLine(p[i].xyz, p[i + 4].xyz, color);
  351. //Draw diagonal
  352. for (int i = 2; i < 6; i++)
  353. Debug::DrawLine(p[i].xyz, p[i + ((i < 4)?(-2):(+2))].xyz, color);
  354. }
  355. } /* namespace lol */