dgl.h 17 KB

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