OpenGL.cpp 8.2 KB

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