dgl.h 18 KB

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