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.

439 lines
19 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. #include <lol/engine-internal.h>
  11. namespace lol
  12. {
  13. //Resets draw infos
  14. void Debug::DrawSetupReset()
  15. {
  16. g_scene->SetLineTime();
  17. g_scene->SetLineMask();
  18. }
  19. //Sets draw infos
  20. void Debug::DrawSetupTime(float new_time)
  21. {
  22. g_scene->SetLineTime(new_time);
  23. }
  24. //--
  25. void Debug::DrawSetupMask(int new_mask)
  26. {
  27. g_scene->SetLineMask(new_mask);
  28. }
  29. //--
  30. void Debug::DrawSetupSegment(float new_segment_size)
  31. {
  32. g_scene->SetLineSegmentSize(new_segment_size);
  33. }
  34. //--
  35. void Debug::DrawSetupColor(vec4 color)
  36. {
  37. g_scene->SetLineColor(color);
  38. }
  39. //--
  40. void Debug::DrawSetup(float new_time, int new_mask, float segment_size, vec4 color)
  41. {
  42. Debug::DrawSetupTime(new_time);
  43. Debug::DrawSetupMask(new_mask);
  44. Debug::DrawSetupSegment(segment_size);
  45. Debug::DrawSetupColor(color);
  46. }
  47. //Screen to world conversion
  48. vec3 Debug::WorldToScreen(vec3 pos)
  49. {
  50. return Debug::WorldToScreen(vec4(pos, 1.f));
  51. }
  52. vec3 Debug::WorldToScreen(vec3 pos, mat4 view_proj)
  53. {
  54. return Debug::WorldToScreen(vec4(pos, 1.f), view_proj);
  55. }
  56. vec3 Debug::WorldToScreen(vec4 pos)
  57. {
  58. if (!g_scene || !g_scene->GetCamera())
  59. return vec3::zero;
  60. mat4 const view_proj = g_scene->GetCamera()->GetProjection() * g_scene->GetCamera()->GetView();
  61. return Debug::WorldToScreen(pos, view_proj);
  62. }
  63. vec3 Debug::WorldToScreen(vec4 pos, mat4 view_proj)
  64. {
  65. vec4 screen_pos = view_proj * pos;
  66. return (screen_pos / screen_pos.w).xyz;
  67. }
  68. //--
  69. vec3 Debug::ScreenToWorld(vec2 pos, float z)
  70. { return Debug::ScreenToWorld(vec3(pos, z)); }
  71. vec3 Debug::ScreenToWorld(vec3 pos)
  72. {
  73. if (!g_scene || !g_scene->GetCamera())
  74. return vec3::zero;
  75. mat4 const inv_view_proj = inverse(g_scene->GetCamera()->GetProjection() * g_scene->GetCamera()->GetView());
  76. return Debug::ScreenToWorld(pos, inv_view_proj);
  77. }
  78. vec3 Debug::ScreenToWorld(vec2 pos, mat4 inv_view_proj, float z)
  79. { return Debug::ScreenToWorld(vec3(pos, z), inv_view_proj); }
  80. vec3 Debug::ScreenToWorld(vec3 pos, mat4 inv_view_proj)
  81. {
  82. vec4 screen_pos = inv_view_proj * vec4(pos, 1.f);
  83. return (screen_pos / screen_pos.w).xyz;
  84. }
  85. vec3 Debug::ScreenToWorld(vec3 pos, mat4 view, mat4 proj)
  86. {
  87. vec4 screen_pos = inverse(proj) * vec4(pos, 1.f);
  88. return (inverse(view) * (screen_pos / screen_pos.w)).xyz;
  89. }
  90. //Draw stuff in World
  91. //-- LINE: 3D -2D - 3D_to_2D --------------------------------------------------
  92. void Debug::DrawLine(vec3 a, vec3 b) { Debug::DrawLine(a, b, g_scene->GetLineColor()); }
  93. void Debug::DrawLine(vec2 a, vec3 b, float az) { Debug::DrawLine(a, b, g_scene->GetLineColor(), az); }
  94. void Debug::DrawLine(vec2 a, vec2 b, float az, float bz) { Debug::DrawLine(a, b, g_scene->GetLineColor(), az, bz); }
  95. void Debug::DrawLine(vec3 a, vec3 b, vec4 color) { g_scene->AddLine(a, b, color); }
  96. void Debug::DrawLine(vec2 a, vec3 b, vec4 color, float az) { g_scene->AddLine(a, b, color, az); }
  97. void Debug::DrawLine(vec2 a, vec2 b, vec4 color, float az, float bz){ g_scene->AddLine(a, b, color, az, bz); }
  98. //-- GIZMO --------------------------------------------------------------------
  99. void Debug::DrawGizmo(vec3 pos, vec3 x, vec3 y, vec3 z, float size)
  100. {
  101. Debug::DrawLine(pos, pos + x * size, Color::red);
  102. Debug::DrawLine(pos, pos + y * size, Color::green);
  103. Debug::DrawLine(pos, pos + z * size, Color::blue);
  104. }
  105. void Debug::DrawGizmo(vec2 pos, vec3 x, vec3 y, vec3 z, float size, float posz)
  106. {
  107. Debug::DrawLine(pos, pos + x.xy * size, Color::red, posz, posz + x.z * size);
  108. Debug::DrawLine(pos, pos + y.xy * size, Color::green, posz, posz + y.z * size);
  109. Debug::DrawLine(pos, pos + z.xy * size, Color::blue, posz, posz + z.z * size);
  110. }
  111. //-- GRID ---------------------------------------------------------------------
  112. void Debug::DrawGrid(vec3 pos, vec3 x, vec3 y, vec3 z, float size, bool draw_3d)
  113. {
  114. float seg_sz = g_scene->GetLineSegmentSize();
  115. int seg_nb = lol::max((int)(size / seg_sz), 1);
  116. seg_sz = size / (float)seg_nb;
  117. //Draw Gizmo
  118. Debug::DrawGizmo(pos, x, y, z, size);
  119. Debug::DrawLine(pos, pos - x * size, Color::gray);
  120. Debug::DrawLine(pos, pos - y * size, Color::gray);
  121. Debug::DrawLine(pos, pos - z * size, Color::gray);
  122. //grid
  123. for (float i = -seg_nb; i <= seg_nb; i++)
  124. {
  125. if (i != 0)
  126. {
  127. float iter = seg_sz * ((float)i);
  128. Debug::DrawLine(vec3(-size, 0, iter), vec3(size, 0, iter), Color::gray_dark);
  129. Debug::DrawLine(vec3(iter, 0, -size), vec3(iter, 0, size), Color::gray_dark);
  130. if (draw_3d)
  131. {
  132. Debug::DrawLine(vec3(0, -size, iter), vec3(0, size, iter), Color::gray_dark);
  133. Debug::DrawLine(vec3(0, iter, -size), vec3(0, iter, size), Color::gray_dark);
  134. Debug::DrawLine(vec3(-size, iter, 0), vec3(size, iter, 0), Color::gray_dark);
  135. Debug::DrawLine(vec3(iter, -size, 0), vec3(iter, size, 0), Color::gray_dark);
  136. }
  137. }
  138. }
  139. }
  140. //-- ARROW: 3D -2D - 3D_to_2D -------------------------------------------------
  141. void Debug::DrawArrow(vec3 a, vec3 b, vec2 s) { Debug::DrawArrow(a, b, vec3(s.x, s.y, s.y)); }
  142. 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); }
  143. 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); }
  144. void Debug::DrawArrow(vec3 a, vec3 b, vec3 s) { Debug::DrawArrow(a, b, vec3(s.x, s.y, s.y), g_scene->GetLineColor()); }
  145. 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); }
  146. 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); }
  147. void Debug::DrawArrow(vec3 a, vec3 b, vec3 s, vec4 color)
  148. {
  149. vec3 z = s.x * normalize(b - a);
  150. vec3 x = s.z * orthonormal(b - a);
  151. vec3 y = s.y * cross(normalize(x), normalize(z));
  152. Debug::DrawLine(a, b, color);
  153. Debug::DrawLine(b, b - z + x, color);
  154. Debug::DrawLine(b, b - z - x, color);
  155. Debug::DrawLine(b, b - z + y, color);
  156. Debug::DrawLine(b, b - z - y, color);
  157. Debug::DrawLine(b - z + x, b - z + y, color);
  158. Debug::DrawLine(b - z + x, b - z - y, color);
  159. Debug::DrawLine(b - z - x, b - z + y, color);
  160. Debug::DrawLine(b - z - x, b - z - y, color);
  161. Debug::DrawLine(b - z + x, b - z - x, color);
  162. Debug::DrawLine(b - z + y, b - z - y, color);
  163. }
  164. void Debug::DrawArrow(vec2 a, vec3 b, vec3 s, vec4 color, float az)
  165. {
  166. vec3 bn = Debug::WorldToScreen(b);
  167. DrawArrow(a, bn.xy, s, color, az, bn.z);
  168. }
  169. void Debug::DrawArrow(vec2 a, vec2 b, vec3 s, vec4 color, float az, float bz)
  170. {
  171. vec3 an = vec3(a, az);
  172. vec3 bn = vec3(b, bz);
  173. vec3 z = s.x * normalize(bn - an);
  174. vec3 x = s.z * orthonormal(bn - an);
  175. vec3 y = s.y * cross(normalize(x), normalize(z));
  176. Debug::DrawLine(a, b, color, az, bz);
  177. Debug::DrawLine(b, b - (z + x).xy, color, bz, bz - (z + x).z);
  178. Debug::DrawLine(b, b - (z - x).xy, color, bz, bz - (z - x).z);
  179. Debug::DrawLine(b, b - (z + y).xy, color, bz, bz - (z + y).z);
  180. Debug::DrawLine(b, b - (z - y).xy, color, bz, bz - (z - y).z);
  181. Debug::DrawLine(b - (z + x).xy, b - (z + y).xy, color, bz - (z + x).z, bz - (z + y).z);
  182. Debug::DrawLine(b - (z + x).xy, b - (z - y).xy, color, bz - (z + x).z, bz - (z - y).z);
  183. Debug::DrawLine(b - (z - x).xy, b - (z + y).xy, color, bz - (z - x).z, bz - (z + y).z);
  184. Debug::DrawLine(b - (z - x).xy, b - (z - y).xy, color, bz - (z - x).z, bz - (z - y).z);
  185. Debug::DrawLine(b - (z + x).xy, b - (z - x).xy, color, bz - (z + x).z, bz - (z - x).z);
  186. Debug::DrawLine(b - (z + y).xy, b - (z - y).xy, color, bz - (z + y).z, bz - (z - y).z);
  187. }
  188. //-- BOX: 3D -2D - 3D_to_2D ---------------------------------------------------
  189. void Debug::DrawBox(box3 a) { Debug::DrawBox(a.A, a.B, g_scene->GetLineColor()); }
  190. void Debug::DrawBox(box2 a) { Debug::DrawBox(a.A, a.B, g_scene->GetLineColor()); }
  191. void Debug::DrawBox(box3 a, vec4 color) { Debug::DrawBox(a.A, a.B, color); }
  192. void Debug::DrawBox(box2 a, vec4 color) { Debug::DrawBox(a.A, a.B, color); }
  193. void Debug::DrawBox(vec3 a, vec3 b) { Debug::DrawBox(a, b, g_scene->GetLineColor()); }
  194. void Debug::DrawBox(vec2 a, vec2 b) { Debug::DrawBox(a, b, g_scene->GetLineColor()); }
  195. void Debug::DrawBox(vec2 a, float s) { Debug::DrawBox(a, s, g_scene->GetLineColor()); }
  196. void Debug::DrawBox(vec3 a, vec3 b, vec4 color) { Debug::DrawBox(a, b, mat4::identity, color); }
  197. void Debug::DrawBox(vec2 a, vec2 b, vec4 color) { Debug::DrawBox(a, b, mat2::identity, color); }
  198. void Debug::DrawBox(vec2 a, float s, vec4 color) { Debug::DrawBox(a, s, mat2::identity, color); }
  199. void Debug::DrawBox(box3 a, mat4 transform) { Debug::DrawBox(a.A, a.B, transform, g_scene->GetLineColor()); }
  200. void Debug::DrawBox(box2 a, mat2 transform) { Debug::DrawBox(a.A, a.B, transform, g_scene->GetLineColor()); }
  201. void Debug::DrawBox(box3 a, mat4 transform, vec4 color) { Debug::DrawBox(a.A, a.B, transform, color); }
  202. void Debug::DrawBox(box2 a, mat2 transform, vec4 color) { Debug::DrawBox(a.A, a.B, transform, color); }
  203. void Debug::DrawBox(vec3 a, vec3 b, mat4 transform) { Debug::DrawBox(a, b, transform, g_scene->GetLineColor()); }
  204. void Debug::DrawBox(vec2 a, vec2 b, mat2 transform) { Debug::DrawBox(a, b, transform, g_scene->GetLineColor()); }
  205. void Debug::DrawBox(vec2 a, float s, mat2 transform) { Debug::DrawBox(a, s, transform, g_scene->GetLineColor()); }
  206. void Debug::DrawBox(vec3 a, vec3 b, mat4 transform, vec4 color)
  207. {
  208. vec4 v[8];
  209. for (int i = 0; i < 8; i++)
  210. {
  211. v[i].x = i & 1 ? a.x : b.x;
  212. v[i].y = i & 2 ? a.y : b.y;
  213. v[i].z = i & 4 ? a.z : b.z;
  214. v[i].w = 1.f;
  215. }
  216. for (int i = 0; i < 4; i++)
  217. {
  218. int j = ((i & 1) << 1) | ((i >> 1) ^ 1);
  219. Debug::DrawLine((transform * v[i]).xyz, (transform * v[i + 4]).xyz, color);
  220. Debug::DrawLine((transform * v[i]).xyz, (transform * v[j]).xyz, color);
  221. Debug::DrawLine((transform * v[i + 4]).xyz, (transform * v[j + 4]).xyz, color);
  222. }
  223. }
  224. void Debug::DrawBox(vec2 a, vec2 b, mat2 transform, vec4 color)
  225. {
  226. vec2 v[4];
  227. v[0] = a;
  228. v[1] = vec2(a.x, b.y);
  229. v[2] = b;
  230. v[3] = vec2(b.x, a.y);
  231. int i = 0;
  232. Debug::DrawLine((transform * v[0]).xy, (transform * v[1]).xy, color);
  233. Debug::DrawLine((transform * v[1]).xy, (transform * v[2]).xy, color);
  234. Debug::DrawLine((transform * v[2]).xy, (transform * v[3]).xy, color);
  235. Debug::DrawLine((transform * v[3]).xy, (transform * v[0]).xy, color);
  236. }
  237. void Debug::DrawBox(vec2 a, float s, mat2 transform, vec4 color)
  238. {
  239. vec2 b = s * vec2(1.f, g_renderer->GetXYRatio());
  240. Debug::DrawBox(a - b, a + b, transform, color);
  241. }
  242. //-- CIRCLE -------------------------------------------------------------------
  243. void Debug::DrawCircle(vec2 a, float s) { Debug::DrawCircle(a, s * vec2(1.f, g_renderer->GetXYRatio()), g_scene->GetLineColor()); }
  244. void Debug::DrawCircle(vec3 a, vec3 n) { Debug::DrawCircle(a, n, g_scene->GetLineColor()); }
  245. void Debug::DrawCircle(vec2 a, vec2 s) { Debug::DrawCircle(a, s * vec2(1.f, g_renderer->GetXYRatio()), g_scene->GetLineColor()); }
  246. void Debug::DrawCircle(vec3 a, vec3 x, vec3 y) { Debug::DrawCircle(a, x, y, g_scene->GetLineColor()); }
  247. void Debug::DrawCircle(vec2 a, vec2 x, vec2 y) { Debug::DrawCircle(a, x, y, g_scene->GetLineColor()); }
  248. void Debug::DrawCircle(vec3 a, vec3 n, vec4 color)
  249. {
  250. vec3 x = orthogonal(n);
  251. vec3 y = cross(normalize(n), normalize(x)) * length(n);
  252. DrawCircle(a, x, y, color);
  253. }
  254. void Debug::DrawCircle(vec2 a, vec2 s, vec4 color)
  255. {
  256. vec2 x = vec2::axis_x * s.x;
  257. vec2 y = vec2::axis_y * s.y;
  258. DrawCircle(a, x, y, color);
  259. }
  260. //--
  261. void Debug::DrawCircle(vec3 a, vec3 x, vec3 y, vec4 color)
  262. {
  263. float size = F_PI * 2.f * lol::max(length(x), length(y));
  264. int segment_nb = lol::max(1, (int)((size * .25f) / g_scene->GetLineSegmentSize()));
  265. for (int i = 0; i < segment_nb; i++)
  266. {
  267. float a0 = (((float)i) / (float)segment_nb) * F_PI_2;
  268. float a1 = (((float)i + 1) / (float)segment_nb) * F_PI_2;
  269. vec2 p0 = vec2(lol::cos(a0), lol::sin(a0));
  270. vec2 p1 = vec2(lol::cos(a1), lol::sin(a1));
  271. Debug::DrawLine(a + p0.x * x + p0.y * y, a + p1.x * x + p1.y * y, color);
  272. Debug::DrawLine(a + p0.x * -x + p0.y * -y, a + p1.x * -x + p1.y * -y, color);
  273. Debug::DrawLine(a + p0.x * x + p0.y * -y, a + p1.x * x + p1.y * -y, color);
  274. Debug::DrawLine(a + p0.x * -x + p0.y * y, a + p1.x * -x + p1.y * y, color);
  275. }
  276. }
  277. //--
  278. void Debug::DrawCircle(vec2 a, vec2 x, vec2 y, vec4 color)
  279. {
  280. float size = F_PI * 2.f * lol::max(length(x), length(y));
  281. int segment_nb = lol::max(1, (int)((size * .25f) / g_scene->GetLineSegmentSize()));
  282. for (int i = 0; i < segment_nb; i++)
  283. {
  284. float a0 = (((float)i) / (float)segment_nb) * F_PI_2;
  285. float a1 = (((float)i + 1) / (float)segment_nb) * F_PI_2;
  286. vec2 p0 = vec2(lol::cos(a0), lol::sin(a0));
  287. vec2 p1 = vec2(lol::cos(a1), lol::sin(a1));
  288. Debug::DrawLine(a + p0.x * x + p0.y * y, a + p1.x * x + p1.y * y, color);
  289. Debug::DrawLine(a + p0.x * -x + p0.y * -y, a + p1.x * -x + p1.y * -y, color);
  290. Debug::DrawLine(a + p0.x * x + p0.y * -y, a + p1.x * x + p1.y * -y, color);
  291. Debug::DrawLine(a + p0.x * -x + p0.y * y, a + p1.x * -x + p1.y * y, color);
  292. }
  293. }
  294. //-- SPHERE -------------------------------------------------------------------
  295. void Debug::DrawSphere(vec3 a, float s) { Debug::DrawSphere(a, s, g_scene->GetLineColor()); }
  296. 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); }
  297. void Debug::DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z) { Debug::DrawSphere(a, x, y, z, g_scene->GetLineColor()); }
  298. void Debug::DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z, vec4 color)
  299. {
  300. Debug::DrawCircle(a, x, y, color);
  301. Debug::DrawCircle(a, x, (y + z) * .707f, color);
  302. Debug::DrawCircle(a, x, (y - z) * .707f, color);
  303. Debug::DrawCircle(a, z, x, color);
  304. Debug::DrawCircle(a, z, (x + y) * .707f, color);
  305. Debug::DrawCircle(a, z, (x - y) * .707f, color);
  306. Debug::DrawCircle(a, y, z, color);
  307. Debug::DrawCircle(a, y, (z + x) * .707f, color);
  308. Debug::DrawCircle(a, y, (z - x) * .707f, color);
  309. }
  310. //-- CAPSULE ------------------------------------------------------------------
  311. void Debug::DrawCapsule(vec3 a, float s, vec3 h) { Debug::DrawCapsule(a, s, h, g_scene->GetLineColor()); }
  312. void Debug::DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h) { Debug::DrawCapsule(a, x, y, z, h, g_scene->GetLineColor()); }
  313. void Debug::DrawCapsule(vec3 a, float s, vec3 h, vec4 color)
  314. {
  315. vec3 x = orthonormal(h) * s;
  316. vec3 y = cross(normalize(h), normalize(x)) * s;
  317. Debug::DrawCapsule(a, x, y, normalize(h) * s, h, color);
  318. }
  319. //--
  320. void Debug::DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h, vec4 color)
  321. {
  322. float size = F_PI * 2.f * lol::max(length(x), length(y));
  323. int segment_nb = lol::max(1, (int)((size * .25f) / g_scene->GetLineSegmentSize()));
  324. for (int i = -1; i < 2; i += 2)
  325. {
  326. vec3 b = a + h * .5f * (float)i;
  327. vec3 c = a - h * .5f * (float)i;
  328. Debug::DrawCircle(b, x, y, color);
  329. Debug::DrawLine(b + x * (float)i, c + x * (float)i, color);
  330. Debug::DrawLine(b + y * (float)i, c + y * (float)i, color);
  331. for (int j = 0; j < segment_nb; j++)
  332. {
  333. float a0 = (((float)j) / (float)segment_nb) * F_PI_2;
  334. float a1 = (((float)j + 1) / (float)segment_nb) * F_PI_2;
  335. vec2 p0 = vec2(lol::cos(a0), lol::sin(a0));
  336. vec2 p1 = vec2(lol::cos(a1), lol::sin(a1));
  337. Debug::DrawLine(b + p0.x * x + p0.y * z * (float)i, b + p1.x * x + p1.y * z * (float)i, color);
  338. Debug::DrawLine(b + p0.x * -x + p0.y * z * (float)i, b + p1.x * -x + p1.y * z * (float)i, color);
  339. Debug::DrawLine(b + p0.x * y + p0.y * z * (float)i, b + p1.x * y + p1.y * z * (float)i, color);
  340. Debug::DrawLine(b + p0.x * -y + p0.y * z * (float)i, b + p1.x * -y + p1.y * z * (float)i, color);
  341. }
  342. }
  343. }
  344. //-- VIEW PROJ ----------------------------------------------------------------
  345. void Debug::DrawViewProj(mat4 view, mat4 proj) { Debug::DrawViewProj(view, proj, g_scene->GetLineColor()); }
  346. void Debug::DrawViewProj(mat4 view_proj) { Debug::DrawViewProj(view_proj, g_scene->GetLineColor()); }
  347. void Debug::DrawViewProj(mat4 view, mat4 proj, vec4 color)
  348. {
  349. mat4 const view_proj = proj * view;
  350. //Pos to center
  351. vec3 p0 = (inverse(view) * vec4(vec3::zero, 1.f)).xyz;
  352. //Near plane
  353. mat4 const inv_view_proj = inverse(view_proj);
  354. vec4 p[4] = { inv_view_proj * vec4(-1.f, 1.f, -1.f, 1.f),
  355. inv_view_proj * vec4( 1.f, 1.f, -1.f, 1.f),
  356. inv_view_proj * vec4( 1.f,-1.f, -1.f, 1.f),
  357. inv_view_proj * vec4(-1.f,-1.f, -1.f, 1.f) };
  358. for (int i = 0; i < 4; i++)
  359. p[i] = p[i] / p[i].w;
  360. //Draw near
  361. for (int i = 0; i < 4; i++)
  362. Debug::DrawLine(p[i].xyz, p0, color);
  363. Debug::DrawViewProj(view_proj, color);
  364. }
  365. //--
  366. void Debug::DrawViewProj(mat4 view_proj, vec4 color)
  367. {
  368. //Near plane
  369. mat4 const inv_view_proj = inverse(view_proj);
  370. vec4 p[8] = { inv_view_proj * vec4(-1.f, 1.f, 1.f, 1.f),
  371. inv_view_proj * vec4( 1.f, 1.f, 1.f, 1.f),
  372. inv_view_proj * vec4( 1.f,-1.f, 1.f, 1.f),
  373. inv_view_proj * vec4(-1.f,-1.f, 1.f, 1.f),
  374. inv_view_proj * vec4(-1.f, 1.f,-1.f, 1.f),
  375. inv_view_proj * vec4( 1.f, 1.f,-1.f, 1.f),
  376. inv_view_proj * vec4( 1.f,-1.f,-1.f, 1.f),
  377. inv_view_proj * vec4(-1.f,-1.f,-1.f, 1.f)
  378. };
  379. for (int i = 0; i < 8; i++)
  380. p[i] = p[i] / p[i].w;
  381. //Draw near
  382. for (int i = 0; i < 4; i++)
  383. Debug::DrawLine(p[i].xyz, p[(i + 1) % 4].xyz, color);
  384. //Draw far
  385. for (int i = 4; i < 8; i++)
  386. Debug::DrawLine(p[i].xyz, p[(i - 4 + 1) % 4 + 4].xyz, color);
  387. //Draw near to far
  388. for (int i = 0; i < 4; i++)
  389. Debug::DrawLine(p[i].xyz, p[i + 4].xyz, color);
  390. //Draw diagonal
  391. for (int i = 2; i < 6; i++)
  392. Debug::DrawLine(p[i].xyz, p[i + ((i < 4)?(-2):(+2))].xyz, color);
  393. }
  394. } /* namespace lol */