DebugDraw.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #ifndef DEBUGDRAW_H
  19. #define DEBUGDRAW_H
  20. // Some math headers don't have PI defined.
  21. static const float DU_PI = 3.14159265f;
  22. enum duDebugDrawPrimitives
  23. {
  24. DU_DRAW_POINTS,
  25. DU_DRAW_LINES,
  26. DU_DRAW_TRIS,
  27. DU_DRAW_QUADS,
  28. };
  29. /// Abstract debug draw interface.
  30. struct duDebugDraw
  31. {
  32. virtual ~duDebugDraw() = 0;
  33. virtual void depthMask(bool state) = 0;
  34. virtual void texture(bool state) = 0;
  35. /// Begin drawing primitives.
  36. /// @param prim [in] primitive type to draw, one of rcDebugDrawPrimitives.
  37. /// @param size [in] size of a primitive, applies to point size and line width only.
  38. virtual void begin(duDebugDrawPrimitives prim, float size = 1.0f) = 0;
  39. /// Submit a vertex
  40. /// @param pos [in] position of the verts.
  41. /// @param color [in] color of the verts.
  42. virtual void vertex(const float* pos, unsigned int color) = 0;
  43. /// Submit a vertex
  44. /// @param x,y,z [in] position of the verts.
  45. /// @param color [in] color of the verts.
  46. virtual void vertex(const float x, const float y, const float z, unsigned int color) = 0;
  47. /// Submit a vertex
  48. /// @param pos [in] position of the verts.
  49. /// @param color [in] color of the verts.
  50. virtual void vertex(const float* pos, unsigned int color, const float* uv) = 0;
  51. /// Submit a vertex
  52. /// @param x,y,z [in] position of the verts.
  53. /// @param color [in] color of the verts.
  54. virtual void vertex(const float x, const float y, const float z, unsigned int color, const float u, const float v) = 0;
  55. /// End drawing primitives.
  56. virtual void end() = 0;
  57. /// Compute a color for given area.
  58. virtual unsigned int areaToCol(unsigned int area);
  59. };
  60. inline unsigned int duRGBA(int r, int g, int b, int a)
  61. {
  62. return ((unsigned int)r) | ((unsigned int)g << 8) | ((unsigned int)b << 16) | ((unsigned int)a << 24);
  63. }
  64. inline unsigned int duRGBAf(float fr, float fg, float fb, float fa)
  65. {
  66. unsigned char r = (unsigned char)(fr*255.0f);
  67. unsigned char g = (unsigned char)(fg*255.0f);
  68. unsigned char b = (unsigned char)(fb*255.0f);
  69. unsigned char a = (unsigned char)(fa*255.0f);
  70. return duRGBA(r,g,b,a);
  71. }
  72. unsigned int duIntToCol(int i, int a);
  73. void duIntToCol(int i, float* col);
  74. inline unsigned int duMultCol(const unsigned int col, const unsigned int d)
  75. {
  76. const unsigned int r = col & 0xff;
  77. const unsigned int g = (col >> 8) & 0xff;
  78. const unsigned int b = (col >> 16) & 0xff;
  79. const unsigned int a = (col >> 24) & 0xff;
  80. return duRGBA((r*d) >> 8, (g*d) >> 8, (b*d) >> 8, a);
  81. }
  82. inline unsigned int duDarkenCol(unsigned int col)
  83. {
  84. return ((col >> 1) & 0x007f7f7f) | (col & 0xff000000);
  85. }
  86. inline unsigned int duLerpCol(unsigned int ca, unsigned int cb, unsigned int u)
  87. {
  88. const unsigned int ra = ca & 0xff;
  89. const unsigned int ga = (ca >> 8) & 0xff;
  90. const unsigned int ba = (ca >> 16) & 0xff;
  91. const unsigned int aa = (ca >> 24) & 0xff;
  92. const unsigned int rb = cb & 0xff;
  93. const unsigned int gb = (cb >> 8) & 0xff;
  94. const unsigned int bb = (cb >> 16) & 0xff;
  95. const unsigned int ab = (cb >> 24) & 0xff;
  96. unsigned int r = (ra*(255-u) + rb*u)/255;
  97. unsigned int g = (ga*(255-u) + gb*u)/255;
  98. unsigned int b = (ba*(255-u) + bb*u)/255;
  99. unsigned int a = (aa*(255-u) + ab*u)/255;
  100. return duRGBA(r,g,b,a);
  101. }
  102. inline unsigned int duTransCol(unsigned int c, unsigned int a)
  103. {
  104. return (a<<24) | (c & 0x00ffffff);
  105. }
  106. void duCalcBoxColors(unsigned int* colors, unsigned int colTop, unsigned int colSide);
  107. void duDebugDrawCylinderWire(struct duDebugDraw* dd, float minx, float miny, float minz,
  108. float maxx, float maxy, float maxz, unsigned int col, const float lineWidth);
  109. void duDebugDrawBoxWire(struct duDebugDraw* dd, float minx, float miny, float minz,
  110. float maxx, float maxy, float maxz, unsigned int col, const float lineWidth);
  111. void duDebugDrawArc(struct duDebugDraw* dd, const float x0, const float y0, const float z0,
  112. const float x1, const float y1, const float z1, const float h,
  113. const float as0, const float as1, unsigned int col, const float lineWidth);
  114. void duDebugDrawArrow(struct duDebugDraw* dd, const float x0, const float y0, const float z0,
  115. const float x1, const float y1, const float z1,
  116. const float as0, const float as1, unsigned int col, const float lineWidth);
  117. void duDebugDrawCircle(struct duDebugDraw* dd, const float x, const float y, const float z,
  118. const float r, unsigned int col, const float lineWidth);
  119. void duDebugDrawCross(struct duDebugDraw* dd, const float x, const float y, const float z,
  120. const float size, unsigned int col, const float lineWidth);
  121. void duDebugDrawBox(struct duDebugDraw* dd, float minx, float miny, float minz,
  122. float maxx, float maxy, float maxz, const unsigned int* fcol);
  123. void duDebugDrawCylinder(struct duDebugDraw* dd, float minx, float miny, float minz,
  124. float maxx, float maxy, float maxz, unsigned int col);
  125. void duDebugDrawGridXZ(struct duDebugDraw* dd, const float ox, const float oy, const float oz,
  126. const int w, const int h, const float size,
  127. const unsigned int col, const float lineWidth);
  128. // Versions without begin/end, can be used to draw multiple primitives.
  129. void duAppendCylinderWire(struct duDebugDraw* dd, float minx, float miny, float minz,
  130. float maxx, float maxy, float maxz, unsigned int col);
  131. void duAppendBoxWire(struct duDebugDraw* dd, float minx, float miny, float minz,
  132. float maxx, float maxy, float maxz, unsigned int col);
  133. void duAppendBoxPoints(struct duDebugDraw* dd, float minx, float miny, float minz,
  134. float maxx, float maxy, float maxz, unsigned int col);
  135. void duAppendArc(struct duDebugDraw* dd, const float x0, const float y0, const float z0,
  136. const float x1, const float y1, const float z1, const float h,
  137. const float as0, const float as1, unsigned int col);
  138. void duAppendArrow(struct duDebugDraw* dd, const float x0, const float y0, const float z0,
  139. const float x1, const float y1, const float z1,
  140. const float as0, const float as1, unsigned int col);
  141. void duAppendCircle(struct duDebugDraw* dd, const float x, const float y, const float z,
  142. const float r, unsigned int col);
  143. void duAppendCross(struct duDebugDraw* dd, const float x, const float y, const float z,
  144. const float size, unsigned int col);
  145. void duAppendBox(struct duDebugDraw* dd, float minx, float miny, float minz,
  146. float maxx, float maxy, float maxz, const unsigned int* fcol);
  147. void duAppendCylinder(struct duDebugDraw* dd, float minx, float miny, float minz,
  148. float maxx, float maxy, float maxz, unsigned int col);
  149. class duDisplayList : public duDebugDraw
  150. {
  151. float* m_pos;
  152. unsigned int* m_color;
  153. int m_size;
  154. int m_cap;
  155. bool m_depthMask;
  156. duDebugDrawPrimitives m_prim;
  157. float m_primSize;
  158. void resize(int cap);
  159. public:
  160. duDisplayList(int cap = 512);
  161. ~duDisplayList();
  162. virtual void depthMask(bool state);
  163. virtual void begin(duDebugDrawPrimitives prim, float size = 1.0f);
  164. virtual void vertex(const float x, const float y, const float z, unsigned int color);
  165. virtual void vertex(const float* pos, unsigned int color);
  166. virtual void end();
  167. void clear();
  168. void draw(struct duDebugDraw* dd);
  169. private:
  170. // Explicitly disabled copy constructor and copy assignment operator.
  171. duDisplayList(const duDisplayList&);
  172. duDisplayList& operator=(const duDisplayList&);
  173. };
  174. #endif // DEBUGDRAW_H