OpenGL.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /**
  2. * Copyright (c) 2006-2014 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #ifndef LOVE_GRAPHICS_OPENGL_OPENGL_H
  21. #define LOVE_GRAPHICS_OPENGL_OPENGL_H
  22. #include "GLee.h"
  23. // LOVE
  24. #include "graphics/Color.h"
  25. #include "graphics/Texture.h"
  26. #include "common/Matrix.h"
  27. // C++
  28. #include <vector>
  29. #include <stack>
  30. // The last argument to AttribPointer takes a buffer offset casted to a pointer.
  31. #define BUFFER_OFFSET(i) ((char *) NULL + (i))
  32. namespace love
  33. {
  34. namespace graphics
  35. {
  36. namespace opengl
  37. {
  38. /**
  39. * Thin layer between OpenGL and the rest of the program.
  40. * Internally shadows some OpenGL context state for improved efficiency and
  41. * accuracy (compared to glGet etc.)
  42. * A class is more convenient and readable than plain namespaced functions, but
  43. * typically only one OpenGL object should be used (singleton.)
  44. **/
  45. class OpenGL
  46. {
  47. public:
  48. // OpenGL GPU vendors.
  49. enum Vendor
  50. {
  51. VENDOR_ATI_AMD,
  52. VENDOR_NVIDIA,
  53. VENDOR_INTEL,
  54. VENDOR_MESA_SOFT, // Software renderer.
  55. VENDOR_APPLE, // Software renderer.
  56. VENDOR_MICROSOFT, // Software renderer.
  57. VENDOR_UNKNOWN
  58. };
  59. // Vertex attributes used in shaders by LOVE. The values map to OpenGL
  60. // generic vertex attribute indices, when applicable.
  61. // LOVE uses the old hard-coded attribute APIs for positions, colors, etc.
  62. // (for now.)
  63. enum VertexAttrib
  64. {
  65. // Instance ID when pseudo-instancing is used.
  66. ATTRIB_PSEUDO_INSTANCE_ID = 1,
  67. ATTRIB_MAX_ENUM
  68. };
  69. // A rectangle representing an OpenGL viewport or a scissor box.
  70. struct Viewport
  71. {
  72. int x, y;
  73. int w, h;
  74. Viewport()
  75. : x(0), y(0), w(0), h(0)
  76. {}
  77. Viewport(int _x, int _y, int _w, int _h)
  78. : x(_x), y(_y), w(_w), h(_h)
  79. {}
  80. bool operator == (const Viewport &rhs) const
  81. {
  82. return x == rhs.x && y == rhs.y && w == rhs.w && h == rhs.h;
  83. }
  84. };
  85. struct BlendState
  86. {
  87. GLenum srcRGB, srcA;
  88. GLenum dstRGB, dstA;
  89. GLenum func;
  90. };
  91. struct
  92. {
  93. std::vector<Matrix> transform;
  94. std::vector<Matrix> projection;
  95. } matrices;
  96. class TempTransform
  97. {
  98. public:
  99. TempTransform(OpenGL &gl)
  100. : gl(gl)
  101. {
  102. gl.pushTransform();
  103. }
  104. ~TempTransform()
  105. {
  106. gl.popTransform();
  107. }
  108. Matrix &get()
  109. {
  110. return gl.getTransform();
  111. }
  112. private:
  113. OpenGL &gl;
  114. };
  115. OpenGL();
  116. /**
  117. * Initializes some required context state based on current and default
  118. * OpenGL state. Call this directly after creating an OpenGL context!
  119. **/
  120. void initContext();
  121. /**
  122. * Marks current context state as invalid and deletes OpenGL objects owned
  123. * by this class instance. Call this directly before potentially deleting
  124. * an OpenGL context!
  125. **/
  126. void deInitContext();
  127. void pushTransform();
  128. void popTransform();
  129. Matrix &getTransform();
  130. /**
  131. * Set up necessary state (LOVE-provided shader uniforms, etc.) for drawing.
  132. * This *MUST* be called directly before OpenGL drawing functions.
  133. **/
  134. void prepareDraw();
  135. /**
  136. * glDrawArraysInstanced with a pseudo-instancing fallback.
  137. **/
  138. void drawArraysInstanced(GLenum mode, GLint first, GLsizei count, GLsizei primcount);
  139. /**
  140. * glDrawElementsInstanced with a pseudo-instancing fallback.
  141. **/
  142. void drawElementsInstanced(GLenum mode, GLsizei count, GLenum type, const void *indices, GLsizei primcount);
  143. /**
  144. * Sets the current constant color.
  145. **/
  146. void setColor(const Color &c);
  147. /**
  148. * Gets the current constant color.
  149. **/
  150. Color getColor() const;
  151. /**
  152. * Sets the current clear color for all framebuffer objects.
  153. **/
  154. void setClearColor(const Color &c);
  155. /**
  156. * Gets the current clear color.
  157. **/
  158. Color getClearColor() const;
  159. /**
  160. * Sets the OpenGL rendering viewport to the specified rectangle.
  161. * The y-coordinate starts at the top.
  162. **/
  163. void setViewport(const Viewport &v);
  164. /**
  165. * Gets the current OpenGL rendering viewport rectangle.
  166. **/
  167. Viewport getViewport() const;
  168. /**
  169. * Sets the scissor box to the specified rectangle.
  170. * The y-coordinate starts at the top and is flipped internally.
  171. **/
  172. void setScissor(const Viewport &v);
  173. /**
  174. * Gets the current scissor box (regardless of whether scissoring is enabled.)
  175. **/
  176. Viewport getScissor() const;
  177. /**
  178. * Sets blending functionality.
  179. * Note: This does not globally enable or disable blending.
  180. **/
  181. void setBlendState(const BlendState &blend);
  182. /**
  183. * Gets the currently set blending functionality.
  184. **/
  185. BlendState getBlendState() const;
  186. /**
  187. * Helper for setting the active texture unit.
  188. *
  189. * @param textureunit Index in the range of [0, maxtextureunits-1]
  190. **/
  191. void setTextureUnit(int textureunit);
  192. /**
  193. * Helper for binding an OpenGL texture.
  194. * Makes sure we aren't redundantly binding textures.
  195. **/
  196. void bindTexture(GLuint texture);
  197. /**
  198. * Helper for binding a texture to a specific texture unit.
  199. *
  200. * @param textureunit Index in the range of [0, maxtextureunits-1]
  201. * @param restoreprev Restore previously bound texture unit when done.
  202. **/
  203. void bindTextureToUnit(GLuint texture, int textureunit, bool restoreprev);
  204. /**
  205. * Helper for deleting an OpenGL texture.
  206. * Cleans up if the texture is currently bound.
  207. **/
  208. void deleteTexture(GLuint texture);
  209. /**
  210. * Sets the texture filter mode for the currently bound texture.
  211. * Returns the actual amount of anisotropic filtering set.
  212. **/
  213. float setTextureFilter(graphics::Texture::Filter &f);
  214. /**
  215. * Returns the texture filter mode for the currently bound texture.
  216. **/
  217. graphics::Texture::Filter getTextureFilter();
  218. /**
  219. * Sets the texture wrap mode for the currently bound texture.
  220. **/
  221. void setTextureWrap(const graphics::Texture::Wrap &w);
  222. /**
  223. * Returns the texture wrap mode for the currently bound texture.
  224. **/
  225. graphics::Texture::Wrap getTextureWrap();
  226. /**
  227. * Returns the maximum supported width or height of a texture.
  228. **/
  229. int getMaxTextureSize() const;
  230. /**
  231. * Returns the maximum supported number of simultaneous render targets.
  232. **/
  233. int getMaxRenderTargets() const;
  234. /**
  235. * Get the GPU vendor of this OpenGL context.
  236. **/
  237. Vendor getVendor() const;
  238. // Get human-readable strings for debug info.
  239. static const char *debugSeverityString(GLenum severity);
  240. static const char *debugSourceString(GLenum source);
  241. static const char *debugTypeString(GLenum type);
  242. private:
  243. void initVendor();
  244. void initOpenGLFunctions();
  245. void initMaxValues();
  246. void initMatrices();
  247. void createDefaultTexture();
  248. bool contextInitialized;
  249. float maxAnisotropy;
  250. int maxTextureSize;
  251. int maxRenderTargets;
  252. Vendor vendor;
  253. // Tracked OpenGL state.
  254. struct
  255. {
  256. // Current constant color.
  257. Color color;
  258. Color clearColor;
  259. // Texture unit state (currently bound texture for each texture unit.)
  260. std::vector<GLuint> textureUnits;
  261. // Currently active texture unit.
  262. int curTextureUnit;
  263. Viewport viewport;
  264. Viewport scissor;
  265. BlendState blend;
  266. // The last ID value used for pseudo-instancing.
  267. int lastPseudoInstanceID;
  268. Matrix lastProjectionMatrix;
  269. Matrix lastTransformMatrix;
  270. } state;
  271. }; // OpenGL
  272. // OpenGL class instance singleton.
  273. extern OpenGL gl;
  274. } // opengl
  275. } // graphics
  276. } // love
  277. #endif // LOVE_GRAPHICS_OPENGL_OPENGL_H