OpenGL.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "OpenGL.h"
  22. #include "common/Exception.h"
  23. namespace love
  24. {
  25. namespace graphics
  26. {
  27. namespace opengl
  28. {
  29. static bool contextInitialized = false;
  30. static int curTextureUnitIndex = 0;
  31. static std::vector<GLuint> textureUnits;
  32. void initializeContext()
  33. {
  34. if (contextInitialized)
  35. return;
  36. contextInitialized = true;
  37. textureUnits.clear();
  38. // initialize multiple texture unit support, if available
  39. if (GLEE_ARB_multitexture || GLEE_VERSION_1_3)
  40. {
  41. GLint maxtextureunits;
  42. glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxtextureunits);
  43. // shaders/GL2.0 added "Texture Image Units." Total max texture units is the greater of the two
  44. if (GLEE_VERSION_2_0 || GLEE_ARB_vertex_shader)
  45. {
  46. GLint maxtextureimageunits;
  47. glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxtextureimageunits);
  48. if (maxtextureimageunits > maxtextureunits)
  49. maxtextureunits = maxtextureimageunits;
  50. }
  51. textureUnits.resize(maxtextureunits, 0);
  52. GLenum activetextureunit;
  53. glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint *)&activetextureunit);
  54. curTextureUnitIndex = activetextureunit - GL_TEXTURE0;
  55. for (size_t i = 0; i < textureUnits.size(); ++i)
  56. {
  57. if (GLEE_VERSION_1_3)
  58. glActiveTexture(GL_TEXTURE0 + i);
  59. else
  60. glActiveTextureARB(GL_TEXTURE0 + i);
  61. glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint *) &textureUnits[i]);
  62. }
  63. if (GLEE_VERSION_1_3)
  64. glActiveTexture(activetextureunit);
  65. else
  66. glActiveTextureARB(activetextureunit);
  67. }
  68. else
  69. {
  70. // multitexturing not supported, so we only have 1 texture unit
  71. textureUnits.resize(1, 0);
  72. curTextureUnitIndex = 0;
  73. glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint *) &textureUnits[0]);
  74. }
  75. }
  76. void uninitializeContext()
  77. {
  78. contextInitialized = false;
  79. }
  80. void setActiveTextureUnit(GLenum textureunit)
  81. {
  82. initializeContext();
  83. int textureunitindex = textureunit - GL_TEXTURE0;
  84. if (textureunitindex < 0 || (size_t) textureunitindex >= textureUnits.size())
  85. throw love::Exception("Invalid texture unit index.");
  86. if (textureunitindex != curTextureUnitIndex)
  87. {
  88. if (GLEE_VERSION_1_3)
  89. glActiveTexture(textureunit);
  90. else if (GLEE_ARB_multitexture)
  91. glActiveTextureARB(textureunit);
  92. else
  93. throw love::Exception("Multitexturing not supported.");
  94. }
  95. curTextureUnitIndex = textureunitindex;
  96. }
  97. void bindTexture(GLuint texture)
  98. {
  99. initializeContext();
  100. if (texture != textureUnits[curTextureUnitIndex])
  101. {
  102. textureUnits[curTextureUnitIndex] = texture;
  103. glBindTexture(GL_TEXTURE_2D, texture);
  104. }
  105. }
  106. void bindTextureToUnit(GLuint texture, GLenum textureunit, bool restoreprev)
  107. {
  108. initializeContext();
  109. int textureunitindex = textureunit - GL_TEXTURE0;
  110. if (textureunitindex < 0 || (size_t) textureunitindex >= textureUnits.size())
  111. throw love::Exception("Invalid texture unit index.");
  112. if (texture != textureUnits[textureunitindex])
  113. {
  114. int oldtexunitindex = curTextureUnitIndex;
  115. setActiveTextureUnit(textureunit);
  116. textureUnits[textureunitindex] = texture;
  117. glBindTexture(GL_TEXTURE_2D, texture);
  118. if (restoreprev)
  119. setActiveTextureUnit(GL_TEXTURE0 + oldtexunitindex);
  120. }
  121. }
  122. void deleteTexture(GLuint texture)
  123. {
  124. initializeContext();
  125. std::vector<GLuint>::iterator it;
  126. for (it = textureUnits.begin(); it != textureUnits.end(); ++it)
  127. {
  128. if (*it == texture)
  129. *it = 0;
  130. }
  131. glDeleteTextures(1, &texture);
  132. }
  133. } // opengl
  134. } // graphics
  135. } // love