gfxDrawUtil.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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
  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. void drawRectFill( const Point2F &upperL, const Point2F &lowerR, const ColorI &color );
  47. void drawRectFill( const RectF &rect, const ColorI &color );
  48. void drawRectFill( const Point2I &upperLeft, const Point2I &lowerRight, const ColorI &color );
  49. void drawRectFill( const RectI &rect, const ColorI &color );
  50. void draw2DSquare( const Point2F &screenPoint, F32 width, F32 spinAngle = 0.0f );
  51. //-----------------------------------------------------------------------------
  52. // Draw Lines
  53. //-----------------------------------------------------------------------------
  54. void drawLine( const Point3F &startPt, const Point3F &endPt, const ColorI &color );
  55. void drawLine( const Point2F &startPt, const Point2F &endPt, const ColorI &color );
  56. void drawLine( const Point2I &startPt, const Point2I &endPt, const ColorI &color );
  57. void drawLine( F32 x1, F32 y1, F32 x2, F32 y2, const ColorI &color );
  58. void drawLine( F32 x1, F32 y1, F32 z1, F32 x2, F32 y2, F32 z2, const ColorI &color );
  59. //-----------------------------------------------------------------------------
  60. // Draw Text
  61. //-----------------------------------------------------------------------------
  62. U32 drawText( GFont *font, const Point2I &ptDraw, const UTF8 *in_string, const ColorI *colorTable = NULL, const U32 maxColorIndex = 9, F32 rot = 0.f );
  63. 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 );
  64. U32 drawText( GFont *font, const Point2I &ptDraw, const UTF16 *in_string, const ColorI *colorTable = NULL, const U32 maxColorIndex = 9, F32 rot = 0.f );
  65. 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 );
  66. U32 drawText( GFont *font, const Point2F &ptDraw, const UTF8 *in_string, const ColorI *colorTable = NULL, const U32 maxColorIndex = 9, F32 rot = 0.f );
  67. 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 );
  68. U32 drawText( GFont *font, const Point2F &ptDraw, const UTF16 *in_string, const ColorI *colorTable = NULL, const U32 maxColorIndex = 9, F32 rot = 0.f );
  69. 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 );
  70. //-----------------------------------------------------------------------------
  71. // Color Modulation
  72. //-----------------------------------------------------------------------------
  73. void setBitmapModulation( const ColorI &modColor );
  74. void setTextAnchorColor( const ColorI &ancColor );
  75. void clearBitmapModulation();
  76. void getBitmapModulation( ColorI *color );
  77. //-----------------------------------------------------------------------------
  78. // Draw Bitmaps
  79. //-----------------------------------------------------------------------------
  80. 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);
  81. 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);
  82. 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);
  83. 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);
  84. 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);
  85. 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);
  86. 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);
  87. 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);
  88. //-----------------------------------------------------------------------------
  89. // Draw 3D Shapes
  90. //-----------------------------------------------------------------------------
  91. void drawTriangle( const GFXStateBlockDesc &desc, const Point3F &p0, const Point3F &p1, const Point3F &p2, const ColorI &color, const MatrixF *xfm = NULL );
  92. void drawPolygon( const GFXStateBlockDesc& desc, const Point3F* points, U32 numPoints, const ColorI& color, const MatrixF* xfm = NULL );
  93. void drawCube( const GFXStateBlockDesc &desc, const Point3F &size, const Point3F &pos, const ColorI &color, const MatrixF *xfm = NULL );
  94. void drawCube( const GFXStateBlockDesc &desc, const Box3F &box, const ColorI &color, const MatrixF *xfm = NULL );
  95. void drawObjectBox( const GFXStateBlockDesc &desc, const Point3F &size, const Point3F &pos, const MatrixF &objMat, const ColorI &color );
  96. void drawSphere( const GFXStateBlockDesc &desc, F32 radius, const Point3F &pos, const ColorI &color, bool drawTop = true, bool drawBottom = true, const MatrixF *xfm = NULL );
  97. void drawCapsule( const GFXStateBlockDesc &desc, const Point3F &center, F32 radius, F32 height, const ColorI &color, const MatrixF *xfm = NULL );
  98. void drawCone( const GFXStateBlockDesc &desc, const Point3F &basePnt, const Point3F &tipPnt, F32 baseRadius, const ColorI &color );
  99. void drawCylinder( const GFXStateBlockDesc &desc, const Point3F &basePnt, const Point3F &tipPnt, F32 baseRadius, const ColorI &color );
  100. void drawArrow( const GFXStateBlockDesc &desc, const Point3F &start, const Point3F &end, const ColorI &color, F32 baseRad = 0.0f);
  101. void drawFrustum( const Frustum& f, const ColorI &color );
  102. /// Draw a solid or wireframe (depending on fill mode of @a desc) polyhedron with the given color.
  103. ///
  104. /// @param desc Render state description.
  105. /// @param poly Polyhedron.
  106. /// @param color Color.
  107. /// @param xfm Optional matrix to transform all vertices of the given polyhedron by.
  108. void drawPolyhedron( const GFXStateBlockDesc &desc, const AnyPolyhedron &poly, const ColorI &color, const MatrixF *xfm = NULL );
  109. /// Draws a solid XY plane centered on the point with the specified dimensions.
  110. void drawSolidPlane( const GFXStateBlockDesc &desc, const Point3F &pos, const Point2F &size, const ColorI &color );
  111. enum Plane
  112. {
  113. PlaneXY,
  114. PlaneXZ,
  115. PlaneYZ
  116. };
  117. /// Draws a grid on XY, XZ, or YZ plane centered on the point with the specified size and step size.
  118. void drawPlaneGrid( const GFXStateBlockDesc &desc, const Point3F &pos, const Point2F &size, const Point2F &step, const ColorI &color, Plane plane = PlaneXY );
  119. /// Draws axis lines representing the passed matrix.
  120. /// If scale is NULL axes will be drawn the length they exist within the MatrixF.
  121. /// If colors is NULL the default colors are RED, GREEEN, BLUE ( x, y, z ).
  122. void drawTransform( const GFXStateBlockDesc &desc, const MatrixF &mat, const Point3F *scale = NULL, const ColorI colors[3] = NULL );
  123. protected:
  124. void _setupStateBlocks();
  125. void _drawWireTriangle( const GFXStateBlockDesc &desc, const Point3F &p0, const Point3F &p1, const Point3F &p2, const ColorI &color, const MatrixF *xfm = NULL );
  126. void _drawSolidTriangle( const GFXStateBlockDesc &desc, const Point3F &p0, const Point3F &p1, const Point3F &p2, const ColorI &color, const MatrixF *xfm = NULL );
  127. void _drawWireCube( const GFXStateBlockDesc &desc, const Point3F &size, const Point3F &pos, const ColorI &color, const MatrixF *xfm = NULL );
  128. void _drawSolidCube( const GFXStateBlockDesc &desc, const Point3F &size, const Point3F &pos, const ColorI &color, const MatrixF *xfm = NULL );
  129. void _drawWireCapsule( const GFXStateBlockDesc &desc, const Point3F &center, F32 radius, F32 height, const ColorI &color, const MatrixF *xfm = NULL );
  130. void _drawSolidCapsule( const GFXStateBlockDesc &desc, const Point3F &center, F32 radius, F32 height, const ColorI &color, const MatrixF *xfm = NULL );
  131. void _drawWirePolyhedron( const GFXStateBlockDesc &desc, const AnyPolyhedron &poly, const ColorI &color, const MatrixF *xfm = NULL );
  132. void _drawSolidPolyhedron( const GFXStateBlockDesc &desc, const AnyPolyhedron &poly, const ColorI &color, const MatrixF *xfm = NULL );
  133. protected:
  134. /// The device we're rendering to.
  135. GFXDevice *mDevice;
  136. /// Bitmap modulation color; bitmaps are multiplied by this color when
  137. /// drawn.
  138. GFXVertexColor mBitmapModulation;
  139. /// Base text color; what color text is drawn at when no other color is
  140. /// specified.
  141. GFXVertexColor mTextAnchorColor;
  142. GFXStateBlockRef mBitmapStretchSB;
  143. GFXStateBlockRef mBitmapStretchLinearSB;
  144. GFXStateBlockRef mBitmapStretchWrapSB;
  145. GFXStateBlockRef mBitmapStretchWrapLinearSB;
  146. GFXStateBlockRef mRectFillSB;
  147. FontRenderBatcher* mFontRenderBatcher;
  148. };
  149. #endif // _GFX_GFXDRAWER_H_