DebugDraw.h 11 KB

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