gfxDrawUtil.h

Engine/source/gfx/gfxDrawUtil.h

More...

Classes:

class

Helper class containing utility functions for useful drawing routines (line, box, rect, billboard, text).

Detailed Description

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