OpenGL.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /**
  2. * Copyright (c) 2006-2012 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. #include <vector>
  21. #include <algorithm>
  22. #include "OpenGL.h"
  23. #include "common/Exception.h"
  24. namespace love
  25. {
  26. namespace graphics
  27. {
  28. namespace opengl
  29. {
  30. static bool contextInitialized = false;
  31. static int curTextureUnit = 0;
  32. static std::vector<GLuint> textureUnits;
  33. void initializeContext()
  34. {
  35. if (contextInitialized)
  36. return;
  37. contextInitialized = true;
  38. textureUnits.clear();
  39. // initialize multiple texture unit support, if available
  40. if (GLEE_VERSION_1_3 || GLEE_ARB_multitexture)
  41. {
  42. GLint maxtextureunits;
  43. glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxtextureunits);
  44. // shaders/GL2.0 added "Texture Image Units." Total max texture units is the greater of the two
  45. if (GLEE_VERSION_2_0 || GLEE_ARB_vertex_shader)
  46. {
  47. GLint maxtextureimageunits;
  48. glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxtextureimageunits);
  49. maxtextureunits = std::max(maxtextureunits, maxtextureimageunits);
  50. }
  51. textureUnits.resize(maxtextureunits, 0);
  52. GLenum curgltextureunit;
  53. glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint *)&curgltextureunit);
  54. curTextureUnit = curgltextureunit - GL_TEXTURE0;
  55. // retrieve currently bound textures for each texture unit
  56. for (size_t i = 0; i < textureUnits.size(); ++i)
  57. {
  58. if (GLEE_VERSION_1_3)
  59. glActiveTexture(GL_TEXTURE0 + i);
  60. else
  61. glActiveTextureARB(GL_TEXTURE0 + i);
  62. glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint *) &textureUnits[i]);
  63. }
  64. if (GLEE_VERSION_1_3)
  65. glActiveTexture(curgltextureunit);
  66. else
  67. glActiveTextureARB(curgltextureunit);
  68. }
  69. else
  70. {
  71. // multitexturing not supported, so we only have 1 texture unit
  72. textureUnits.resize(1, 0);
  73. curTextureUnit = 0;
  74. glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint *) &textureUnits[0]);
  75. }
  76. // Set the 'default' texture (id 0) as a repeating white pixel.
  77. // Otherwise, texture2D inside a shader would return black when drawing graphics primitives,
  78. // which would create the need to use different "passthrough" shaders for untextured primitives vs images.
  79. GLuint curtexture = textureUnits[curTextureUnit];
  80. bindTexture(0);
  81. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  82. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  83. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  84. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  85. GLubyte pixel = 255;
  86. glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE8, 1, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, &pixel);
  87. bindTexture(curtexture);
  88. }
  89. void uninitializeContext()
  90. {
  91. contextInitialized = false;
  92. }
  93. void setActiveTextureUnit(int textureunit)
  94. {
  95. initializeContext();
  96. if (textureunit < 0 || (size_t) textureunit >= textureUnits.size())
  97. throw love::Exception("Invalid texture unit index (%d).", textureunit);
  98. if (textureunit != curTextureUnit)
  99. {
  100. if (GLEE_VERSION_1_3)
  101. glActiveTexture(GL_TEXTURE0 + textureunit);
  102. else if (GLEE_ARB_multitexture)
  103. glActiveTextureARB(GL_TEXTURE0 + textureunit);
  104. else
  105. throw love::Exception("Multitexturing not supported.");
  106. }
  107. curTextureUnit = textureunit;
  108. }
  109. void bindTexture(GLuint texture)
  110. {
  111. initializeContext();
  112. if (texture != textureUnits[curTextureUnit])
  113. {
  114. textureUnits[curTextureUnit] = texture;
  115. glBindTexture(GL_TEXTURE_2D, texture);
  116. }
  117. }
  118. void bindTextureToUnit(GLuint texture, int textureunit, bool restoreprev)
  119. {
  120. initializeContext();
  121. if (textureunit < 0 || (size_t) textureunit >= textureUnits.size())
  122. throw love::Exception("Invalid texture unit index.");
  123. if (texture != textureUnits[textureunit])
  124. {
  125. int oldtextureunit = curTextureUnit;
  126. setActiveTextureUnit(textureunit);
  127. textureUnits[textureunit] = texture;
  128. glBindTexture(GL_TEXTURE_2D, texture);
  129. if (restoreprev)
  130. setActiveTextureUnit(oldtextureunit);
  131. }
  132. }
  133. void deleteTexture(GLuint texture)
  134. {
  135. initializeContext();
  136. // glDeleteTextures binds texture 0 to all texture units the deleted texture was bound to
  137. std::vector<GLuint>::iterator it;
  138. for (it = textureUnits.begin(); it != textureUnits.end(); ++it)
  139. {
  140. if (*it == texture)
  141. *it = 0;
  142. }
  143. glDeleteTextures(1, &texture);
  144. }
  145. void setTextureFilter(const graphics::Image::Filter &f)
  146. {
  147. initializeContext();
  148. GLint gmin, gmag;
  149. if (f.mipmap == Image::FILTER_NONE)
  150. {
  151. if (f.min == Image::FILTER_NEAREST)
  152. gmin = GL_NEAREST;
  153. else // f.min == Image::FILTER_LINEAR
  154. gmin = GL_LINEAR;
  155. }
  156. else
  157. {
  158. if (f.min == Image::FILTER_NEAREST && f.mipmap == Image::FILTER_NEAREST)
  159. gmin = GL_NEAREST_MIPMAP_NEAREST;
  160. else if (f.min == Image::FILTER_NEAREST && f.mipmap == Image::FILTER_LINEAR)
  161. gmin = GL_NEAREST_MIPMAP_LINEAR;
  162. else if (f.min == Image::FILTER_LINEAR && f.mipmap == Image::FILTER_NEAREST)
  163. gmin = GL_LINEAR_MIPMAP_NEAREST;
  164. else if (f.min == Image::FILTER_LINEAR && f.mipmap == Image::FILTER_LINEAR)
  165. gmin = GL_LINEAR_MIPMAP_LINEAR;
  166. else
  167. gmin = GL_LINEAR;
  168. }
  169. switch (f.mag)
  170. {
  171. case Image::FILTER_NEAREST:
  172. gmag = GL_NEAREST;
  173. break;
  174. case Image::FILTER_LINEAR:
  175. default:
  176. gmag = GL_LINEAR;
  177. break;
  178. }
  179. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gmin);
  180. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gmag);
  181. }
  182. graphics::Image::Filter getTextureFilter()
  183. {
  184. initializeContext();
  185. GLint gmin, gmag;
  186. glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, &gmin);
  187. glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, &gmag);
  188. Image::Filter f;
  189. switch (gmin)
  190. {
  191. case GL_NEAREST:
  192. f.min = Image::FILTER_NEAREST;
  193. f.mipmap = Image::FILTER_NONE;
  194. break;
  195. case GL_NEAREST_MIPMAP_NEAREST:
  196. f.min = f.mipmap = Image::FILTER_NEAREST;
  197. break;
  198. case GL_NEAREST_MIPMAP_LINEAR:
  199. f.min = Image::FILTER_NEAREST;
  200. f.mipmap = Image::FILTER_LINEAR;
  201. break;
  202. case GL_LINEAR_MIPMAP_NEAREST:
  203. f.min = Image::FILTER_LINEAR;
  204. f.mipmap = Image::FILTER_NEAREST;
  205. break;
  206. case GL_LINEAR_MIPMAP_LINEAR:
  207. f.min = f.mipmap = Image::FILTER_LINEAR;
  208. break;
  209. case GL_LINEAR:
  210. default:
  211. f.min = Image::FILTER_LINEAR;
  212. f.mipmap = Image::FILTER_NONE;
  213. break;
  214. }
  215. switch (gmag)
  216. {
  217. case GL_NEAREST:
  218. f.mag = Image::FILTER_NEAREST;
  219. break;
  220. case GL_LINEAR:
  221. default:
  222. f.mag = Image::FILTER_LINEAR;
  223. break;
  224. }
  225. return f;
  226. }
  227. void setTextureWrap(const graphics::Image::Wrap &w)
  228. {
  229. initializeContext();
  230. GLint gs, gt;
  231. switch (w.s)
  232. {
  233. case Image::WRAP_CLAMP:
  234. gs = GL_CLAMP_TO_EDGE;
  235. break;
  236. case Image::WRAP_REPEAT:
  237. default:
  238. gs = GL_REPEAT;
  239. break;
  240. }
  241. switch (w.t)
  242. {
  243. case Image::WRAP_CLAMP:
  244. gt = GL_CLAMP_TO_EDGE;
  245. break;
  246. case Image::WRAP_REPEAT:
  247. default:
  248. gt = GL_REPEAT;
  249. break;
  250. }
  251. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, gs);
  252. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, gt);
  253. }
  254. graphics::Image::Wrap getTextureWrap()
  255. {
  256. initializeContext();
  257. GLint gs, gt;
  258. glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, &gs);
  259. glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, &gt);
  260. Image::Wrap w;
  261. switch (gs)
  262. {
  263. case GL_CLAMP_TO_EDGE:
  264. w.s = Image::WRAP_CLAMP;
  265. break;
  266. case GL_REPEAT:
  267. default:
  268. w.s = Image::WRAP_REPEAT;
  269. break;
  270. }
  271. switch (gt)
  272. {
  273. case GL_CLAMP_TO_EDGE:
  274. w.t = Image::WRAP_CLAMP;
  275. break;
  276. case GL_REPEAT:
  277. default:
  278. w.t = Image::WRAP_REPEAT;
  279. break;
  280. }
  281. return w;
  282. }
  283. } // opengl
  284. } // graphics
  285. } // love