Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

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