gfxDrawUtil.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _GFX_GFXDRAWER_H_
  23. #define _GFX_GFXDRAWER_H_
  24. #ifndef _GFXDEVICE_H_
  25. #include "gfx/gfxDevice.h"
  26. #endif
  27. #ifndef _MPOLYHEDRON_H_
  28. #include "math/mPolyhedron.h"
  29. #endif
  30. class FontRenderBatcher;
  31. class Frustum;
  32. /// Helper class containing utility functions for useful drawing routines
  33. /// (line, box, rect, billboard, text).
  34. class GFXDrawUtil
  35. {
  36. public:
  37. GFXDrawUtil(GFXDevice *);
  38. ~GFXDrawUtil();
  39. //-----------------------------------------------------------------------------
  40. // Draw Rectangles : OUTLINE
  41. //-----------------------------------------------------------------------------
  42. void drawRect(const Point2F& upperLeft, const Point2F& lowerRight, const ColorI& color);
  43. void drawRect(const RectF& rect, const ColorI& color);
  44. void drawRect(const Point2I& upperLeft, const Point2I& lowerRight, const ColorI& color);
  45. void drawRect(const RectI& rect, const ColorI& color);
  46. //-----------------------------------------------------------------------------
  47. // Draw Rectangles : FILL
  48. //-----------------------------------------------------------------------------
  49. void drawRectFill(const Point2F& upperL, const Point2F& lowerR, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0), bool gradientFill = false);
  50. void drawRectFill(const RectF& rect, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0), bool gradientFill = false);
  51. void drawRectFill(const Point2I& upperLeft, const Point2I& lowerRight, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0), bool gradientFill = false);
  52. void drawRectFill(const RectI& rect, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0), bool gradientFill = false);
  53. void drawRoundedRect(const F32& cornerRadius, const RectI& rect, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0), bool gradientFill = false);
  54. void drawRoundedRect(const F32& cornerRadius, const Point2I& upperLeft, const Point2I& lowerRight, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0), bool gradientFill = false);
  55. void drawRoundedRect(const F32& cornerRadius, const Point2F& upperLeft, const Point2F& lowerRight, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0), bool gradientFill = false);
  56. void draw2DSquare(const Point2F& screenPoint, F32 width, F32 spinAngle = 0.0f);
  57. //-----------------------------------------------------------------------------
  58. // Draw Circle : FILL
  59. //-----------------------------------------------------------------------------
  60. void drawCircleFill(const RectI& rect, const ColorI& color, F32 radius, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0));
  61. void drawCircleFill(const Point2I& upperLeft, const Point2I& lowerRight, const ColorI& color, F32 radius, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0));
  62. void drawCircleFill(const Point2F& upperLeft, const Point2F& lowerRight, const ColorI& color, F32 radius, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0));
  63. //-----------------------------------------------------------------------------
  64. // Draw Lines : Single Pixel
  65. //-----------------------------------------------------------------------------
  66. void drawLine( const Point3F &startPt, const Point3F &endPt, const ColorI &color );
  67. void drawLine( const Point2F &startPt, const Point2F &endPt, const ColorI &color );
  68. void drawLine( const Point2I &startPt, const Point2I &endPt, const ColorI &color );
  69. void drawLine( F32 x1, F32 y1, F32 x2, F32 y2, const ColorI &color );
  70. void drawLine( F32 x1, F32 y1, F32 z1, F32 x2, F32 y2, F32 z2, const ColorI &color );
  71. //-----------------------------------------------------------------------------
  72. // Draw Lines : Thick
  73. //-----------------------------------------------------------------------------
  74. void drawThickLine(const Point2I& startPt, const Point2I& endPt, const ColorI& color, const F32& thickness);
  75. void drawThickLine(const Point2F& startPt, const Point2F& endPt, const ColorI& color, const F32& thickness);
  76. void drawThickLine(F32 x1, F32 y1, F32 z1, F32 x2, F32 y2, F32 z2, const ColorI& color, const F32& thickness);
  77. //-----------------------------------------------------------------------------
  78. // Draw Text
  79. //-----------------------------------------------------------------------------
  80. U32 drawText( GFont *font, const Point2I &ptDraw, const UTF8 *in_string, const ColorI *colorTable = NULL, const U32 maxColorIndex = 9, F32 rot = 0.f );
  81. U32 drawTextN( GFont *font, const Point2I &ptDraw, const UTF8 *in_string, U32 n, const ColorI *colorTable = NULL, const U32 maxColorIndex = 9, F32 rot = 0.f );
  82. U32 drawText( GFont *font, const Point2I &ptDraw, const UTF16 *in_string, const ColorI *colorTable = NULL, const U32 maxColorIndex = 9, F32 rot = 0.f );
  83. U32 drawTextN( GFont *font, const Point2I &ptDraw, const UTF16 *in_string, U32 n, const ColorI *colorTable = NULL, const U32 maxColorIndex = 9, F32 rot = 0.f );
  84. U32 drawText( GFont *font, const Point2F &ptDraw, const UTF8 *in_string, const ColorI *colorTable = NULL, const U32 maxColorIndex = 9, F32 rot = 0.f );
  85. U32 drawTextN( GFont *font, const Point2F &ptDraw, const UTF8 *in_string, U32 n, const ColorI *colorTable = NULL, const U32 maxColorIndex = 9, F32 rot = 0.f );
  86. U32 drawText( GFont *font, const Point2F &ptDraw, const UTF16 *in_string, const ColorI *colorTable = NULL, const U32 maxColorIndex = 9, F32 rot = 0.f );
  87. U32 drawTextN( GFont *font, const Point2F &ptDraw, const UTF16 *in_string, U32 n, const ColorI *colorTable = NULL, const U32 maxColorIndex = 9, F32 rot = 0.f );
  88. //-----------------------------------------------------------------------------
  89. // Color Modulation
  90. //-----------------------------------------------------------------------------
  91. void setBitmapModulation( const ColorI &modColor );
  92. void setTextAnchorColor( const ColorI &ancColor );
  93. void clearBitmapModulation();
  94. void getBitmapModulation( ColorI *color );
  95. //-----------------------------------------------------------------------------
  96. // Draw Bitmaps
  97. //-----------------------------------------------------------------------------
  98. void drawBitmap( GFXTextureObject*texture, const Point2F &in_rAt, const GFXBitmapFlip in_flip = GFXBitmapFlip_None, const GFXTextureFilterType filter = GFXTextureFilterPoint , bool in_wrap = true, F32 angle = 0.0f);
  99. void drawBitmapSR( GFXTextureObject*texture, const Point2F &in_rAt, const RectF &srcRect, const GFXBitmapFlip in_flip = GFXBitmapFlip_None, const GFXTextureFilterType filter = GFXTextureFilterPoint , bool in_wrap = true, F32 angle = 0.0f);
  100. void drawBitmapStretch( GFXTextureObject*texture, const RectF &dstRect, const GFXBitmapFlip in_flip = GFXBitmapFlip_None, const GFXTextureFilterType filter = GFXTextureFilterPoint , bool in_wrap = true, F32 angle = 0.0f);
  101. void drawBitmapStretchSR( GFXTextureObject*texture, const RectF &dstRect, const RectF &srcRect, const GFXBitmapFlip in_flip = GFXBitmapFlip_None, const GFXTextureFilterType filter = GFXTextureFilterPoint , bool in_wrap = true, F32 angle = 0.0f);
  102. void drawBitmap( GFXTextureObject*texture, const Point2I &in_rAt, const GFXBitmapFlip in_flip = GFXBitmapFlip_None, const GFXTextureFilterType filter = GFXTextureFilterPoint , bool in_wrap = true, F32 angle = 0.0f);
  103. void drawBitmapSR( GFXTextureObject*texture, const Point2I &in_rAt, const RectI &srcRect, const GFXBitmapFlip in_flip = GFXBitmapFlip_None, const GFXTextureFilterType filter = GFXTextureFilterPoint , bool in_wrap = true, F32 angle = 0.0f);
  104. void drawBitmapStretch( GFXTextureObject*texture, const RectI &dstRect, const GFXBitmapFlip in_flip = GFXBitmapFlip_None, const GFXTextureFilterType filter = GFXTextureFilterPoint , bool in_wrap = true, F32 angle = 0.0f);
  105. void drawBitmapStretchSR( GFXTextureObject*texture, const RectI &dstRect, const RectI &srcRect, const GFXBitmapFlip in_flip = GFXBitmapFlip_None, const GFXTextureFilterType filter = GFXTextureFilterPoint , bool in_wrap = true, F32 angle = 0.0f);
  106. //-----------------------------------------------------------------------------
  107. // Draw 3D Shapes
  108. //-----------------------------------------------------------------------------
  109. void drawTriangle( const GFXStateBlockDesc &desc, const Point3F &p0, const Point3F &p1, const Point3F &p2, const ColorI &color, const MatrixF *xfm = NULL );
  110. void drawPolygon( const GFXStateBlockDesc& desc, const Point3F* points, U32 numPoints, const ColorI& color, const MatrixF* xfm = NULL );
  111. void drawCube( const GFXStateBlockDesc &desc, const Point3F &size, const Point3F &pos, const ColorI &color, const MatrixF *xfm = NULL );
  112. void drawCube( const GFXStateBlockDesc &desc, const Box3F &box, const ColorI &color, const MatrixF *xfm = NULL );
  113. void drawObjectBox( const GFXStateBlockDesc &desc, const Point3F &size, const Point3F &pos, const MatrixF &objMat, const ColorI &color );
  114. void drawSphere( const GFXStateBlockDesc &desc, F32 radius, const Point3F &pos, const ColorI &color, bool drawTop = true, bool drawBottom = true, const MatrixF *xfm = NULL );
  115. void drawCapsule( const GFXStateBlockDesc &desc, const Point3F &center, F32 radius, F32 height, const ColorI &color, const MatrixF *xfm = NULL );
  116. void drawCone( const GFXStateBlockDesc &desc, const Point3F &basePnt, const Point3F &tipPnt, F32 baseRadius, const ColorI &color );
  117. void drawCylinder( const GFXStateBlockDesc &desc, const Point3F &basePnt, const Point3F &tipPnt, F32 baseRadius, const ColorI &color );
  118. void drawArrow( const GFXStateBlockDesc &desc, const Point3F &start, const Point3F &end, const ColorI &color, F32 baseRad = 0.0f);
  119. void drawFrustum( const Frustum& f, const ColorI &color );
  120. /// Draw a solid or wireframe (depending on fill mode of @a desc) polyhedron with the given color.
  121. ///
  122. /// @param desc Render state description.
  123. /// @param poly Polyhedron.
  124. /// @param color Color.
  125. /// @param xfm Optional matrix to transform all vertices of the given polyhedron by.
  126. void drawPolyhedron( const GFXStateBlockDesc &desc, const AnyPolyhedron &poly, const ColorI &color, const MatrixF *xfm = NULL );
  127. /// Draws a solid XY plane centered on the point with the specified dimensions.
  128. void drawSolidPlane( const GFXStateBlockDesc &desc, const Point3F &pos, const Point2F &size, const ColorI &color );
  129. enum Plane
  130. {
  131. PlaneXY,
  132. PlaneXZ,
  133. PlaneYZ
  134. };
  135. /// Draws a grid on XY, XZ, or YZ plane centered on the point with the specified size and step size.
  136. void drawPlaneGrid( const GFXStateBlockDesc &desc, const Point3F &pos, const Point2F &size, const Point2F &step, const ColorI &color, Plane plane = PlaneXY );
  137. /// Draws axis lines representing the passed matrix.
  138. /// If scale is NULL axes will be drawn the length they exist within the MatrixF.
  139. /// If colors is NULL the default colors are RED, GREEEN, BLUE ( x, y, z ).
  140. void drawTransform( const GFXStateBlockDesc &desc, const MatrixF &mat, const Point3F *scale = NULL, const ColorI colors[3] = NULL );
  141. protected:
  142. void _setupStateBlocks();
  143. void _drawWireTriangle( const GFXStateBlockDesc &desc, const Point3F &p0, const Point3F &p1, const Point3F &p2, const ColorI &color, const MatrixF *xfm = NULL );
  144. void _drawSolidTriangle( const GFXStateBlockDesc &desc, const Point3F &p0, const Point3F &p1, const Point3F &p2, const ColorI &color, const MatrixF *xfm = NULL );
  145. void _drawWireCube( const GFXStateBlockDesc &desc, const Point3F &size, const Point3F &pos, const ColorI &color, const MatrixF *xfm = NULL );
  146. void _drawSolidCube( const GFXStateBlockDesc &desc, const Point3F &size, const Point3F &pos, const ColorI &color, const MatrixF *xfm = NULL );
  147. void _drawWireCapsule( const GFXStateBlockDesc &desc, const Point3F &center, F32 radius, F32 height, const ColorI &color, const MatrixF *xfm = NULL );
  148. void _drawSolidCapsule( const GFXStateBlockDesc &desc, const Point3F &center, F32 radius, F32 height, const ColorI &color, const MatrixF *xfm = NULL );
  149. void _drawWirePolyhedron( const GFXStateBlockDesc &desc, const AnyPolyhedron &poly, const ColorI &color, const MatrixF *xfm = NULL );
  150. void _drawSolidPolyhedron( const GFXStateBlockDesc &desc, const AnyPolyhedron &poly, const ColorI &color, const MatrixF *xfm = NULL );
  151. protected:
  152. /// The device we're rendering to.
  153. GFXDevice *mDevice;
  154. /// Bitmap modulation color; bitmaps are multiplied by this color when
  155. /// drawn.
  156. GFXVertexColor mBitmapModulation;
  157. /// Base text color; what color text is drawn at when no other color is
  158. /// specified.
  159. GFXVertexColor mTextAnchorColor;
  160. GFXStateBlockRef mBitmapStretchSB;
  161. GFXStateBlockRef mBitmapStretchLinearSB;
  162. GFXStateBlockRef mBitmapStretchWrapSB;
  163. GFXStateBlockRef mBitmapStretchWrapLinearSB;
  164. GFXStateBlockRef mRectFillSB;
  165. FontRenderBatcher* mFontRenderBatcher;
  166. // Expanded shaders
  167. // rounded rectangle.
  168. GFXShaderRef mRoundRectangleShader;
  169. GFXShaderConstBufferRef mRoundRectangleShaderConsts;
  170. // thick line.
  171. GFXShaderRef mCircleShader;
  172. GFXShaderConstBufferRef mCircleShaderConsts;
  173. // thick line.
  174. GFXShaderRef mThickLineShader;
  175. GFXShaderConstBufferRef mThickLineShaderConsts;
  176. };
  177. #endif // _GFX_GFXDRAWER_H_