OpenGL.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * Copyright (c) 2006-2013 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/Image.h"
  26. // STL
  27. #include <vector>
  28. namespace love
  29. {
  30. namespace graphics
  31. {
  32. namespace opengl
  33. {
  34. /**
  35. * Thin layer between OpenGL and the rest of the program.
  36. * Internally shadows some OpenGL context state for improved efficiency and
  37. * accuracy (compared to glGet etc.)
  38. * A class is more convenient and readable than plain namespaced functions, but
  39. * typically only one OpenGL object should be used (singleton.)
  40. **/
  41. class OpenGL
  42. {
  43. public:
  44. // A rectangle representing an OpenGL viewport or a scissor box.
  45. struct Viewport
  46. {
  47. int x, y;
  48. int w, h;
  49. Viewport()
  50. : x(0), y(0), w(0), h(0)
  51. {}
  52. Viewport(int _x, int _y, int _w, int _h)
  53. : x(_x), y(_y), w(_w), h(_h)
  54. {}
  55. };
  56. OpenGL();
  57. /**
  58. * Initializes some required context state based on current and default
  59. * OpenGL state. Call this directly after creating an OpenGL context!
  60. **/
  61. void initContext();
  62. /**
  63. * Marks current context state as invalid and deletes OpenGL objects owned
  64. * by this class instance. Call this directly before potentially deleting
  65. * an OpenGL context!
  66. **/
  67. void deInitContext();
  68. /**
  69. * Sets the current constant color.
  70. **/
  71. void setColor(const Color &c);
  72. /**
  73. * Gets the current constant color.
  74. **/
  75. Color getColor() const;
  76. /**
  77. * Sets the current clear color for all framebuffer objects.
  78. **/
  79. void setClearColor(const Color &c);
  80. /**
  81. * Gets the current clear color.
  82. **/
  83. Color getClearColor() const;
  84. /**
  85. * Sets the OpenGL rendering viewport to the specified rectangle.
  86. * The y-coordinate starts at the top.
  87. **/
  88. void setViewport(const Viewport &v);
  89. /**
  90. * Gets the current OpenGL rendering viewport rectangle.
  91. **/
  92. Viewport getViewport() const;
  93. /**
  94. * Sets the scissor box to the specified rectangle.
  95. * The y-coordinate starts at the top and is flipped internally.
  96. **/
  97. void setScissor(const Viewport &v);
  98. /**
  99. * Gets the current scissor box (regardless of whether scissoring is enabled.)
  100. **/
  101. Viewport getScissor() const;
  102. /**
  103. * Helper for setting the active texture unit.
  104. *
  105. * @param textureunit Index in the range of [0, maxtextureunits-1]
  106. **/
  107. void setActiveTextureUnit(int textureunit);
  108. /**
  109. * Helper for binding an OpenGL texture.
  110. * Makes sure we aren't redundantly binding textures.
  111. **/
  112. void bindTexture(GLuint texture);
  113. /**
  114. * Helper for binding a texture to a specific texture unit.
  115. *
  116. * @param textureunit Index in the range of [0, maxtextureunits-1]
  117. * @param resoreprev Restore previously bound texture unit when done.
  118. **/
  119. void bindTextureToUnit(GLuint texture, int textureunit, bool restoreprev);
  120. /**
  121. * Helper for deleting an OpenGL texture.
  122. * Cleans up if the texture is currently bound.
  123. **/
  124. void deleteTexture(GLuint texture);
  125. /**
  126. * Sets the image filter mode for the currently bound texture.
  127. * Returns the actual amount of anisotropic filtering set.
  128. **/
  129. float setTextureFilter(const graphics::Image::Filter &f);
  130. /**
  131. * Returns the image filter mode for the currently bound texture.
  132. **/
  133. graphics::Image::Filter getTextureFilter();
  134. /**
  135. * Sets the image wrap mode for the currently bound texture.
  136. **/
  137. void setTextureWrap(const graphics::Image::Wrap &w);
  138. /**
  139. * Returns the image wrap mode for the currently bound texture.
  140. **/
  141. graphics::Image::Wrap getTextureWrap();
  142. /**
  143. * Returns the maximum supported width or height of a texture.
  144. **/
  145. int getMaxTextureSize() const;
  146. private:
  147. void initOpenGLFunctions();
  148. void initMaxValues();
  149. void createDefaultTexture();
  150. bool contextInitialized;
  151. float maxAnisotropy;
  152. int maxTextureSize;
  153. // Tracked OpenGL state.
  154. struct
  155. {
  156. // Current constant color.
  157. Color color;
  158. Color clearColor;
  159. // Texture unit state (currently bound texture for each texture unit.)
  160. std::vector<GLuint> textureUnits;
  161. // Currently active texture unit.
  162. int curTextureUnit;
  163. Viewport viewport;
  164. Viewport scissor;
  165. } state;
  166. }; // OpenGL
  167. // OpenGL class instance singleton.
  168. extern OpenGL gl;
  169. } // opengl
  170. } // graphics
  171. } // love
  172. #endif // LOVE_GRAPHICS_OPENGL_OPENGL_H