dgl.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 _DGL_H_
  23. #define _DGL_H_
  24. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. #ifndef _PLATFORMGL_H_
  28. #include "platform/platformGL.h"
  29. #endif
  30. class TextureObject;
  31. class GFont;
  32. class MatrixF;
  33. class RectI;
  34. class ColorI;
  35. class ColorF;
  36. class Point2I;
  37. class Point2F;
  38. class Point3F;
  39. //------------------------------------------------------------------------------
  40. //-------------------------------------- Bitmap Drawing
  41. //
  42. /// Enumerations used for telling the bitmap drawing how/if to flip a bitmap
  43. /// @see dglDrawBitmap
  44. /// @see dglDrawBitmapStretch
  45. /// @see dglDrawBitmapSR
  46. /// @see dglDrawBitmapStretchSR
  47. enum GFlipConstants
  48. {
  49. GFlip_None = 0,
  50. GFlip_X = BIT(0),
  51. GFlip_Y = BIT(1),
  52. GFlip_XY = GFlip_X | GFlip_Y
  53. };
  54. /// @defgroup dgl_bitmap_mod Bitmap Modulation
  55. /// These functions control a modulation color that is used to modulate all drawn objects
  56. /// @{
  57. /// Sets the current color to modulate objects with, similar to glColor3fv()
  58. /// @see dglGetBitmapModulation
  59. /// @see dglClearBitmapModulation
  60. void dglSetBitmapModulation(const ColorF& in_rColor);
  61. /// Gets the current color modulation in float format [0,1]
  62. /// @see dglSetBitmapModulation
  63. /// @see dglClearBitmapModulation
  64. void dglGetBitmapModulation(ColorF* color);
  65. /// Gets the current color modulation in integer format [0,256)
  66. /// @see dglSetBitmapModulation
  67. /// @see dglClearBitmapModulation
  68. void dglGetBitmapModulation(ColorI* color);
  69. /// Sets current color modulation to white, so no modulation takes place
  70. /// @see dglSetBitmapModulation
  71. /// @see dglGetBitmapModulation
  72. void dglClearBitmapModulation();
  73. /// @}
  74. // Note that you must call this _after_ SetBitmapModulation if the two are different
  75. // SetBMod sets the text anchor to the modulation color
  76. /// Sets the anchor color for text coloring, useful when mixing text colors
  77. void dglSetTextAnchorColor(const ColorF&);
  78. /// @defgroup dgl_bitmap_draw Bitmap Drawing Functions
  79. /// These functions allow you to draw a bitmap.
  80. /// Each function will draw the bitmap in a specific way regarding stretching
  81. /// @{
  82. /// Draws a bitmap, starting from the lower left corner, going up and right
  83. /// @param texObject texture object to be drawn
  84. /// @param in_rAt where to draw the texture in 2d coordinates
  85. /// @param in_flip enumerated constant representing any flipping to be done about the x and/or y axis
  86. void dglDrawBitmap(TextureObject* texObject,
  87. const Point2I& in_rAt,
  88. const U32 in_flip = GFlip_None);
  89. /// Draws a power-of-two bitmap that is tiled. Requires a POT bitmap to draw properly.
  90. /// @param texObject texture object to be drawn
  91. /// @param in_rTile rectangle where the texture will be drawn in 2d coordinates
  92. /// @param in_flip enumerated constant representing any flipping to be done about the x and/or y axis
  93. void dglDrawBitmapTile(TextureObject* texObject,
  94. const RectI& in_rTile,
  95. const U32 in_flip = GFlip_None,
  96. F32 fSpin = 0.0f,
  97. bool bSilhouette = false);
  98. /// Draws a bitmap that is stretched
  99. /// @param texObject texture object to be drawn
  100. /// @param in_rStretch rectangle where the texture will be drawn in 2d coordinates
  101. /// @param in_flip enumerated constant representing any flipping to be done about the x and/or y axis
  102. void dglDrawBitmapStretch(TextureObject* texObject,
  103. const RectI& in_rStretch,
  104. const U32 in_flip = GFlip_None,
  105. F32 fSpin = 0.0f,
  106. bool bSilhouette = false);
  107. /// Draws a sub region of a texture
  108. /// @param texObject texture object to be drawn
  109. /// @param in_rAt point where the texture is to be drawn
  110. /// @param in_rSubRegion portion of the texture to be drawn
  111. /// @param in_flip enumerated constant representing any flipping to be done about the x and/or y axis
  112. void dglDrawBitmapSR(TextureObject* texObject,
  113. const Point2I& in_rAt,
  114. const RectI& in_rSubRegion,
  115. const U32 in_flip = GFlip_None);
  116. /// Draws a stretched sub region of a texture
  117. /// @param texObject texture object to be drawn
  118. /// @param in_rStretch rectangle where the texture object will be drawn
  119. /// @param in_rSubRegion sub region of the texture that will be applied over the stretch region of the screen
  120. /// @param in_flip enumerated constant representing any flipping to be done about the x and/or y axis
  121. void dglDrawBitmapStretchSR(TextureObject* texObject,
  122. const RectI& in_rStretch,
  123. const RectI& in_rSubRegion,
  124. const U32 in_flip = GFlip_None,
  125. F32 fSpin = 0.0f,
  126. bool bSilhouette = false);
  127. /// @}
  128. /// @defgroup dgl_text Text Functions
  129. /// These functions draw a string on the string with a given font
  130. /// @{
  131. /// Draws text at a location in 2d gui coordinates
  132. /// Also supports color tags to modulate the given text
  133. /// @returns the number of x-pixels traversed
  134. /// @param font font to draw with, usually found in the profile
  135. /// @param ptDraw point where to start drawing text
  136. /// @param in_string string to be drawn
  137. /// @param colorTable lookups for the color tags
  138. /// @param maxColorIndex size of the colorTable
  139. /// @param rot rotation of text
  140. U32 dglDrawText(GFont *font, const Point2I &ptDraw, const UTF16 *in_string, const ColorI *colorTable = NULL, const U32 maxColorIndex = 9, F32 rot = 0.f);
  141. /// Converts UTF8 text to UTF16, and calls the UTF16 version of dglDrawText
  142. U32 dglDrawText(GFont *font, const Point2I &ptDraw, const UTF8 *in_string, const ColorI *colorTable = NULL, const U32 maxColorIndex = 9, F32 rot = 0.f);
  143. /// Draws "n" number of characters from the string, in_string
  144. /// @returns the number of x-pixels traversed
  145. /// @see dglDrawText
  146. U32 dglDrawTextN(GFont *font, const Point2I &ptDraw, const UTF16 *in_string, U32 n, const ColorI *colorTable = NULL, const U32 maxColorIndex = 9, F32 rot = 0.f);
  147. /// Converts UTF8 text to UTF16, and calls the UTF16 version of dglDrawTextN
  148. U32 dglDrawTextN(GFont *font, const Point2I &ptDraw, const UTF8 *in_string, U32 n, const ColorI *colorTable = NULL, const U32 maxColorIndex = 9, F32 rot = 0.f);
  149. /// @}
  150. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  151. // Drawing primitives
  152. /// @defgroup dgl_primitive Primitive Drawing
  153. /// Easy functions for drawing lines and (un)textured rectangles in 2d or 3d space
  154. /// @{
  155. /// draws a line from x1,y1 to x2,y2 in the specified color
  156. void dglDrawLine(S32 x1, S32 y1, S32 x2, S32 y2, const ColorI &color);
  157. /// draws a line from startPt to endPt in specified color
  158. void dglDrawLine(const Point2I &startPt, const Point2I &endPt, const ColorI &color);
  159. /// draws a wireframe rectangle from upperL to lowerR in specified color and optional line width
  160. void dglDrawRect(const Point2I &upperL, const Point2I &lowerR, const ColorI &color, const float &lineWidth = 1.0f);
  161. /// draws a wireframe rectangle in "rect" in specified color and optional line width
  162. void dglDrawRect(const RectI &rect, const ColorI &color, const float &lineWidth = 1.0f);
  163. /// draws an UNTEXTURED filled rectangle from upperL to lowerR in specified color
  164. void dglDrawRectFill(const Point2I &upperL, const Point2I &lowerR, const ColorI &color);
  165. /// draws an UNTEXTURED filled rectangle in "rect" in specified color
  166. void dglDrawRectFill(const RectI &rect, const ColorI &color);
  167. /// draws a square, with center point "screenPoint", width of "width" on an angle of "spinAngle" in 2d
  168. void dglDraw2DSquare( const Point2F &screenPoint, F32 width, F32 spinAngle );
  169. /// draws a square, with center point "position", width of "width" on an angle of "spinAngle" in 3d
  170. void dglDrawBillboard( const Point3F &position, F32 width, F32 spinAngle );
  171. /// Draws a wireframe cube around "center" with size "extent"
  172. void dglWireCube(const Point3F &extent, const Point3F &center);
  173. /// Draws a solid cube around "center" with size "extent"
  174. void dglSolidCube(const Point3F &extent, const Point3F & enter);
  175. /// @}
  176. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  177. // Matrix functions
  178. /// @defgroup dgl_matrix Matrix Functions
  179. /// These functions manipulate the current matrix. The current matrix could be modelivew, projection, or texture
  180. /// @note Make sure you specify which matrix you want to manipulate with a call to glMatrixMode(enum matrix); before calling dglLoadMatrix() or dglMultMatrix()
  181. /// @{
  182. /// loads matrix "m" into the current matrix mode
  183. void dglLoadMatrix(const MatrixF *m);
  184. /// multiplies the current transformation matrix by matrix "m"
  185. void dglMultMatrix(const MatrixF *m);
  186. /// returns the current modelview matrix
  187. void dglGetModelview(MatrixF *m);
  188. /// returns the current projection matrix
  189. void dglGetProjection(MatrixF *m);
  190. /// @}
  191. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  192. // Camera functions
  193. /// @defgroup dgl_camera_func Camera functions
  194. /// @{
  195. /// Returns the pixel scale, namely:
  196. ///
  197. /// viewPort.extent.x / 640.0
  198. F32 dglGetPixelScale();
  199. /// The scale factor of a world unit to a screen unit
  200. F32 dglGetWorldToScreenScale();
  201. /// Returns the screen length of a line of distance "radius" that is "dist" units away from the camera that is perpendicular to the line of sight, namely:
  202. ///
  203. /// (radius / dist) * worldToScreenScale
  204. F32 dglProjectRadius(F32 dist, F32 radius);
  205. /// @}
  206. /// @defgroup dgl_view Viewing Volume Functions
  207. /// These functions set up the view cube of the window.
  208. /// @{
  209. /// sets the viewport for the window
  210. void dglSetViewport(const RectI &aViewPort);
  211. /// gets the current viewport of the window
  212. void dglGetViewport(RectI* outViewport);
  213. /// Sets the viewing frustrum. This effectively creates the view volume and sets up the 6 clipping planes (near, far, left, right, top, bottom)
  214. /// @param left This is the position of the left vertical clipping plane
  215. /// @param right This is the position of the right vertical clipping plane
  216. /// @param top This is the position of the top horizontal clipping plane
  217. /// @param bottom This is the position of the bottom horizontal clipping plane
  218. /// @param nearDist This is the distance between the eye and the near clipping plane
  219. /// @param farDist This is the distance between the eye and the far clipping plane
  220. /// @param ortho (optional, default is false) If left false, calling this function will create a projection viewing volume. If true, it will be orthographic
  221. void dglSetFrustum(F64 left, F64 right, F64 bottom, F64 top, F64 nearDist, F64 farDist, bool ortho = false);
  222. /// Returns the parameters for the current viewing frustrum
  223. /// @see dglSetFrustrum
  224. void dglGetFrustum(F64 *left, F64 *right, F64 *bottom, F64 *top, F64 *nearDist, F64 *farDist);
  225. /// returns whether or not the coordinate system is orthographic (if it is not projected)
  226. bool dglIsOrtho();
  227. /// Sets up an orthographical viewport and clipping region. This is best used for guis
  228. /// @param clipRect The bounds of the coordinate system
  229. void dglSetClipRect(const RectI &clipRect);
  230. /// Gets the last clip rect specified by a call to dglSetClipRect
  231. /// @see dglSetClipRect
  232. const RectI& dglGetClipRect();
  233. /// @}
  234. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  235. // Misc
  236. /// Projects a point on the screen in 3d space into a point on the screen
  237. /// @returns true if the point is on the screen, false if not and screenPoint will be (0,0,0)
  238. bool dglPointToScreen( Point3F &point3D, Point3F &screenPoint );
  239. //--------------------------------------------------------------------------
  240. // Debug functions
  241. /// Checks to see if all states are "canonical"
  242. /// @see dglSetCanonicalState
  243. bool dglIsInCanonicalState();
  244. /// Sets states to a "canonical" state
  245. /// @note a "canonical" state is described as:
  246. ///
  247. /// BLEND disabled
  248. ///
  249. /// TEXTURE_2D disabled on both texture units.
  250. ///
  251. /// ActiveTexture set to 0
  252. ///
  253. /// LIGHTING off
  254. ///
  255. /// winding : clockwise ?
  256. ///
  257. /// cullface : disabled
  258. void dglSetCanonicalState();
  259. /// Gets the current state of all transformation matrices
  260. /// @param mvDepth Number of "pushes" made to the modelview matrix without balancing "pops"
  261. /// @param pDepth Number of "pushes" made to the projection matrix without balancing "pops"
  262. /// @param t0Depth Number of "pushes" made to the texture 0 matrix without balancing "pops"
  263. /// @param t0Matrix The current texture 0 matrix, should be a 4-element array
  264. /// @param t1Depth Number of "pushes" made to the texture 1 matrix without balancing "pops"
  265. /// @param t1Matrix The current texture 1 matrix, should be a 4-element array
  266. /// @param vp The current viewport, should be a 4-element array
  267. /// @see dglCheckState
  268. void dglGetTransformState(S32* mvDepth,
  269. S32* pDepth,
  270. S32* t0Depth,
  271. F32* t0Matrix,
  272. S32* t1Depth,
  273. F32* t1Matrix,
  274. S32* vp);
  275. /// Checks to see that the given information matches the current transform state
  276. /// @see dglGetTransformState
  277. bool dglCheckState(const S32 mvDepth, const S32 pDepth,
  278. const S32 t0Depth, const F32* t0Matrix,
  279. const S32 t1Depth, const F32* t1Matrix,
  280. const S32* vp);
  281. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  282. // Advanced hardware functionality.
  283. /// Number of full screen anti-aliasting samples
  284. extern signed int gFSAASamples;
  285. #ifdef TORQUE_OS_IOS
  286. //Luma: Add some additional commands here so that it works in all cases
  287. extern GLfloat gVertexFloats[8];
  288. extern GLfloat gTextureVerts[8];
  289. // uses memory insted of heap memory. PUAP optimization
  290. #define dglDrawTextureQuadiPhone( vX1, vY1, vX2, vY2, vX3, vY3, vX4, vY4, tX1, tY1, tX2, tY2, tX3, tY3, tX4, tY4 ) \
  291. gVertexFloats[0] = (GLfloat)(vX1); \
  292. gVertexFloats[1] = (GLfloat)(vY1); \
  293. gVertexFloats[2] = (GLfloat)(vX2); \
  294. gVertexFloats[3] = (GLfloat)(vY2); \
  295. gVertexFloats[4] = (GLfloat)(vX3); \
  296. gVertexFloats[5] = (GLfloat)(vY3); \
  297. gVertexFloats[6] = (GLfloat)(vX4); \
  298. gVertexFloats[7] = (GLfloat)(vY4); \
  299. gTextureVerts[0] = (GLfloat)(tX1); \
  300. gTextureVerts[1] = (GLfloat)(tY1); \
  301. gTextureVerts[2] = (GLfloat)(tX2); \
  302. gTextureVerts[3] = (GLfloat)(tY2); \
  303. gTextureVerts[4] = (GLfloat)(tX3); \
  304. gTextureVerts[5] = (GLfloat)(tY3); \
  305. gTextureVerts[6] = (GLfloat)(tX4); \
  306. gTextureVerts[7] = (GLfloat)(tY4); \
  307. glDisableClientState(GL_COLOR_ARRAY); \
  308. glDisableClientState(GL_POINT_SIZE_ARRAY_OES); \
  309. glEnableClientState(GL_VERTEX_ARRAY); \
  310. glEnableClientState(GL_TEXTURE_COORD_ARRAY); \
  311. glVertexPointer(2, GL_FLOAT, 0, gVertexFloats); \
  312. glTexCoordPointer(2, GL_FLOAT, 0, gTextureVerts); \
  313. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  314. #endif
  315. #endif // _H_DGL