DebugDraw.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <OgreHardwareVertexBuffer.h>
  17. #include <OgreRenderQueueListener.h>
  18. #include <OgreRoot.h>
  19. #include <OgreSingleton.h>
  20. #include <OgreFont.h>
  21. class DebugDraw :
  22. public Ogre::RenderQueueListener, public Ogre::Singleton<DebugDraw>
  23. {
  24. public:
  25. /**
  26. * Constructor.
  27. */
  28. DebugDraw();
  29. /**
  30. * Destructor.
  31. */
  32. virtual ~DebugDraw();
  33. /**
  34. * Sets the colour of the element to draw.
  35. *
  36. * @param colour[in] The color to apply.
  37. */
  38. void SetColour(const Ogre::ColourValue& colour);
  39. /**
  40. * Sets the screen space.
  41. *
  42. * @param screen_space[in] The screen space.
  43. * @todo Understand and document properly.
  44. */
  45. void SetScreenSpace(const bool screen_space);
  46. /**
  47. * Set the Z coordinate.
  48. *
  49. * @param z[in] The Z coordinate.
  50. */
  51. void SetZ(const float z);
  52. /**
  53. * Sets the fade distance for text.
  54. *
  55. * @param fade_s[in] The text starts fading at this distance.
  56. * @param fade_e[in] The text has faded completely at this distance.
  57. */
  58. void SetFadeDistance(const float fade_s, const float fade_e);
  59. /**
  60. * Text alignment modes.
  61. */
  62. enum TextAlignment{
  63. /**
  64. * Left aligned text.
  65. */
  66. LEFT,
  67. /**
  68. * Right aligned text.
  69. */
  70. RIGHT,
  71. /**
  72. * Center aligned text.
  73. */
  74. CENTER
  75. };
  76. void SetTextAlignment(TextAlignment alignment);
  77. /**
  78. * Draws a line in 2D space.
  79. *
  80. * If the debug vertex limit has been reached or if it would be
  81. * exceeded by drawing the line, a warning message will be printed and
  82. * nothing will be done.
  83. *
  84. * @param x1[in] X coordinate of the starting point.
  85. * @param y1[in] Y coordinate of the starting point.
  86. * @param x2[in] X coordinate of the ending point.
  87. * @param y2[in] Y coordinate of the ending point.
  88. */
  89. void Line(
  90. const float x1, const float y1, const float x2, const float y2
  91. );
  92. /**
  93. * Draws a line in 3D space.
  94. *
  95. * If the debug vertex limit has been reached or if it would be
  96. * exceeded by drawing the line, a warning message will be printed and
  97. * nothing will be done.
  98. *
  99. * @param point1[in] Starting point.
  100. * @param point2[in] Ending point.
  101. */
  102. void Line3d(const Ogre::Vector3& point1, const Ogre::Vector3& point2);
  103. /**
  104. * Draws a triangle in 3D space.
  105. *
  106. * If the debug vertex limit has been reached or if it would be
  107. * exceeded by drawing the triangle, a warning message will be printed
  108. * and nothing will be done.
  109. *
  110. * @param point1[in] A triangle vertex point.
  111. * @param point2[in] A triangle vertex point.
  112. * @param point3[in] A triangle vertex point.
  113. */
  114. void Triangle3d(
  115. const Ogre::Vector3& point1, const Ogre::Vector3& point2,
  116. const Ogre::Vector3& point3
  117. );
  118. /**
  119. * Draws a quad in 2D space.
  120. *
  121. * If the debug vertex limit has been reached or if it would be
  122. * exceeded by drawing the quad, a warning message will be printed and
  123. * nothing will be done.
  124. *
  125. * @param x1[in] X coordinate of the first point.
  126. * @param y1[in] Y coordinate of the first point.
  127. * @param x2[in] X coordinate of the second point.
  128. * @param y2[in] Y coordinate of the second point.
  129. * @param x3[in] X coordinate of the third point.
  130. * @param y3[in] Y coordinate of the third point.
  131. * @param x4[in] X coordinate of the fourth point.
  132. * @param y4[in] Y coordinate of the fourth point.
  133. */
  134. void Quad(
  135. const float x1, const float y1, const float x2, const float y2,
  136. const float x3, const float y3, const float x4, const float y4
  137. );
  138. /**
  139. * Writes debug text on the game screen.
  140. *
  141. * The text won't be warped automatically. If the maximum number of
  142. * debug letters has been written, or if it would be exceeded by
  143. * writing, a warning message will be printed to console and nothing
  144. * will be done.
  145. *
  146. * @param x[in] Left position of the text in the screen.
  147. * @param y[in] Left position of the text in the screen.
  148. * @param text[in] The text to write.
  149. */
  150. void Text(const float x, const float y, const Ogre::String& text);
  151. /**
  152. * Writes debug text on the game screen.
  153. *
  154. * The text will be warped automatically. If the maximum number of
  155. * debug letters has been written, or if it would be exceeded by
  156. * writing, a warning message will be printed to console and nothing
  157. * will be done.
  158. *
  159. * @param point[in] Top left point of the screen where the text will be
  160. * written.
  161. * @param x[in] Text width.
  162. * @param y[in] Text height.
  163. * @param text[in] The text to write.
  164. */
  165. void Text(
  166. const Ogre::Vector3& point, const float x, const float y,
  167. const Ogre::String& text
  168. );
  169. /**
  170. * Ends the render queue.
  171. *
  172. * @param queueGroupId[in] The group id of the queue to end.
  173. * @param invocation[in]
  174. * @param repeatThisInvocation[in]
  175. * @todo Understand and document.
  176. */
  177. void renderQueueEnded(
  178. Ogre::uint8 queueGroupId, const Ogre::String& invocation,
  179. bool& repeatThisInvocation
  180. );
  181. private:
  182. /**
  183. * Creates a 2D line vertex buffer (2 vertices).
  184. */
  185. void CreateLineVertexBuffer();
  186. /**
  187. * Destroys a 2D line vertex buffer (2 vertices).
  188. */
  189. void DestroyLineVertexBuffer();
  190. /**
  191. * Creates a 3D line vertex buffer (2 vertices).
  192. */
  193. void CreateLine3dVertexBuffer();
  194. /**
  195. * Destroys a 3D line vertex buffer (2 vertices).
  196. */
  197. void DestroyLine3dVertexBuffer();
  198. /**
  199. * Creates a 3D triangle vertex buffer (3 vertices).
  200. */
  201. void CreateTriangle3dVertexBuffer();
  202. /**
  203. * Destroys a 3D triangle vertex buffer (3 vertices).
  204. */
  205. void DestroyTriangle3dVertexBuffer();
  206. /**
  207. * Creates a quad vertex buffer (4 vertices).
  208. */
  209. void CreateQuadVertexBuffer();
  210. /**
  211. * Destroys a quad vertex buffer (4 vertices).
  212. */
  213. void DestroyQuadVertexBuffer();
  214. /**
  215. * Creates a text vertex buffer.
  216. */
  217. void CreateTextVertexBuffer();
  218. /**
  219. * Destroys a text vertex buffer.
  220. */
  221. void DestroyTextVertexBuffer();
  222. /**
  223. * The scene manager.
  224. */
  225. Ogre::SceneManager* scene_manager_;
  226. /**
  227. * The render system.
  228. */
  229. Ogre::RenderSystem* render_system_;
  230. /**
  231. * A 2D line render operation.
  232. */
  233. Ogre::RenderOperation line_render_operation_;
  234. /**
  235. * A 2D line vertex buffer (2 vertices).
  236. */
  237. Ogre::HardwareVertexBufferSharedPtr line_vertex_buffer_;
  238. /**
  239. * Maximum number of vertices for debug 2D lines.
  240. */
  241. unsigned int line_max_vertex_;
  242. /**
  243. * A 3D line render operation.
  244. */
  245. Ogre::RenderOperation line_3d_render_operation_;
  246. /**
  247. * A 3D line vertex buffer (2 vertices).
  248. */
  249. Ogre::HardwareVertexBufferSharedPtr line_3d_vertex_buffer_;
  250. /**
  251. * Maximum number of vertices for debug 3D lines.
  252. */
  253. unsigned int line_3d_max_vertex_;
  254. /**
  255. * A 3D triangle render operation.
  256. */
  257. Ogre::RenderOperation triangle_3d_render_operation_;
  258. /**
  259. * A 3D triangle vertex buffer (3 vertices).
  260. */
  261. Ogre::HardwareVertexBufferSharedPtr triangle_3d_vertex_buffer_;
  262. /**
  263. * Maximum number of vertices for debug 3D triangles.
  264. */
  265. unsigned int triangle_3d_max_vertex_;
  266. /**
  267. * A quad render operation.
  268. */
  269. Ogre::RenderOperation quad_render_operation_;
  270. /**
  271. * A quad vertex buffer (4 vertices).
  272. */
  273. Ogre::HardwareVertexBufferSharedPtr quad_vertex_buffer_;
  274. /**
  275. * Maximum number of vertices for debug quads.
  276. */
  277. unsigned int quad_max_vertex_;
  278. /**
  279. * A text render operation.
  280. */
  281. Ogre::RenderOperation text_render_operation_;
  282. /**
  283. * A text vertex buffer.
  284. *
  285. * @todo Confirm if it's 6 vertices per letter.
  286. */
  287. Ogre::HardwareVertexBufferSharedPtr text_vertex_buffer_;
  288. /**
  289. * Maximum number of vertices for debug text.
  290. *
  291. * @todo Confirm if it's 6 vertices per letter.
  292. */
  293. unsigned int text_max_vertex_;
  294. /**
  295. * Font to use for debug text.
  296. */
  297. Ogre::FontPtr font_;
  298. /**
  299. * Font height for the debug text.
  300. */
  301. int font_height_;
  302. /**
  303. * Alignment for debug text.
  304. */
  305. TextAlignment text_alignment_;
  306. /**
  307. * Material for a 2D object.
  308. */
  309. Ogre::MaterialPtr material_;
  310. /**
  311. * Material for a 3D object.
  312. */
  313. Ogre::MaterialPtr material_3d_;
  314. /**
  315. * Color for the debug object or text.
  316. */
  317. Ogre::ColourValue colour_;
  318. /**
  319. * The screen space.
  320. */
  321. bool screen_space_;
  322. /**
  323. * Z coordinate for the debug object.
  324. */
  325. float z_coordinate_;
  326. /**
  327. * For text fading. The text start fading from this distance.
  328. */
  329. float fade_start_square_;
  330. /**
  331. * For text fading. The text fully fades at this distance.
  332. */
  333. float fade_end_square_;
  334. };
  335. /**
  336. * Shortcut for a DebugDraw singleton.
  337. */
  338. #define DEBUG_DRAW DebugDraw::getSingleton()