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. // initialize multiple texture unit support, if available
  38. if (GLEE_ARB_multitexture || GLEE_VERSION_1_3)
  39. {
  40. GLint maxtextureunits;
  41. glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxtextureunits);
  42. // shaders/GL2.0 added "Texture Image Units." Total max texture units is the greater of the two
  43. if (GLEE_VERSION_2_0 || GLEE_ARB_vertex_shader)
  44. {
  45. GLint maxtextureimageunits;
  46. glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxtextureimageunits);
  47. if (maxtextureimageunits > maxtextureunits)
  48. maxtextureunits = maxtextureimageunits;
  49. }
  50. textureUnits.clear();
  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.clear();
  72. textureUnits.resize(1, 0);
  73. curTextureUnitIndex = 0;
  74. glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint *) &textureUnits[0]);
  75. }
  76. }
  77. void uninitializeContext()
  78. {
  79. contextInitialized = false;
  80. }
  81. void setActiveTextureUnit(GLenum textureunit)
  82. {
  83. initializeContext();
  84. int textureunitindex = textureunit - GL_TEXTURE0;
  85. if (textureunitindex < 0 || (size_t) textureunitindex >= textureUnits.size())
  86. throw love::Exception("Invalid texture unit index.");
  87. if (textureunitindex != curTextureUnitIndex)
  88. {
  89. if (GLEE_VERSION_1_3)
  90. glActiveTexture(textureunit);
  91. else if (GLEE_ARB_multitexture)
  92. glActiveTextureARB(textureunit);
  93. else
  94. throw love::Exception("Multitexturing not supported.");
  95. }
  96. curTextureUnitIndex = textureunitindex;
  97. }
  98. void bindTexture(GLuint texture)
  99. {
  100. initializeContext();
  101. if (texture != textureUnits[curTextureUnitIndex])
  102. {
  103. textureUnits[curTextureUnitIndex] = texture;
  104. glBindTexture(GL_TEXTURE_2D, texture);
  105. }
  106. }
  107. void bindTextureToUnit(GLuint texture, GLenum textureunit, bool restoreprev)
  108. {
  109. initializeContext();
  110. int textureunitindex = textureunit - GL_TEXTURE0;
  111. if (textureunitindex < 0 || (size_t) textureunitindex >= textureUnits.size())
  112. throw love::Exception("Invalid texture unit index.");
  113. if (texture != textureUnits[textureunitindex])
  114. {
  115. int oldtexunitindex = curTextureUnitIndex;
  116. setActiveTextureUnit(textureunit);
  117. textureUnits[textureunitindex] = texture;
  118. glBindTexture(GL_TEXTURE_2D, texture);
  119. if (restoreprev)
  120. setActiveTextureUnit(GL_TEXTURE0 + oldtexunitindex);
  121. }
  122. }
  123. void deleteTexture(GLuint texture)
  124. {
  125. initializeContext();
  126. std::vector<GLuint>::iterator it;
  127. for (it = textureUnits.begin(); it != textureUnits.end(); ++it)
  128. {
  129. if (*it == texture)
  130. *it = 0;
  131. }
  132. glDeleteTextures(1, &texture);
  133. }
  134. } // opengl
  135. } // graphics
  136. } // love