|
@@ -18,7 +18,9 @@
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
|
**/
|
|
**/
|
|
|
|
|
|
|
|
+#include <vector>
|
|
#include "OpenGL.h"
|
|
#include "OpenGL.h"
|
|
|
|
+#include "common/Exception.h"
|
|
|
|
|
|
namespace love
|
|
namespace love
|
|
{
|
|
{
|
|
@@ -27,27 +29,95 @@ namespace graphics
|
|
namespace opengl
|
|
namespace opengl
|
|
{
|
|
{
|
|
|
|
|
|
-static GLuint boundTexture = 0;
|
|
|
|
|
|
+static bool contextInitialized = false;
|
|
|
|
|
|
-void resetBoundTexture()
|
|
|
|
|
|
+static int curTextureUnitIndex = 0;
|
|
|
|
+static std::vector<GLuint> textureUnits;
|
|
|
|
+
|
|
|
|
+void initializeContext()
|
|
|
|
+{
|
|
|
|
+ if (contextInitialized)
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ contextInitialized = true;
|
|
|
|
+
|
|
|
|
+ if (GLEE_ARB_multitexture || GLEE_VERSION_1_3)
|
|
|
|
+ {
|
|
|
|
+ GLint maxtextureunits;
|
|
|
|
+ glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxtextureunits);
|
|
|
|
+
|
|
|
|
+ if (GLEE_VERSION_2_0 || GLEE_ARB_vertex_shader)
|
|
|
|
+ {
|
|
|
|
+ GLint maxtextureimageunits;
|
|
|
|
+ glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxtextureimageunits);
|
|
|
|
+
|
|
|
|
+ if (maxtextureimageunits > maxtextureunits)
|
|
|
|
+ maxtextureunits = maxtextureimageunits;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ textureUnits.resize(maxtextureunits, 0);
|
|
|
|
+
|
|
|
|
+ GLenum activeTextureUnit;
|
|
|
|
+ glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint *)&activeTextureUnit);
|
|
|
|
+
|
|
|
|
+ curTextureUnitIndex = activeTextureUnit - GL_TEXTURE0;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ // multitexturing not supported so we only have 1 texture unit
|
|
|
|
+ textureUnits.resize(1, 0);
|
|
|
|
+ curTextureUnitIndex = 0;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void uninitializeContext()
|
|
{
|
|
{
|
|
- // OpenGL might not be initialized yet, so we can't do a real reset
|
|
|
|
- boundTexture = 0;
|
|
|
|
|
|
+ contextInitialized = false;
|
|
}
|
|
}
|
|
|
|
|
|
-void bindTexture(GLuint texture, bool override)
|
|
|
|
|
|
+void setActiveTextureUnit(GLenum textureunit)
|
|
{
|
|
{
|
|
- if (texture != boundTexture || texture == 0 || override)
|
|
|
|
|
|
+ initializeContext();
|
|
|
|
+
|
|
|
|
+ int textureunitindex = textureunit - GL_TEXTURE0;
|
|
|
|
+
|
|
|
|
+ if (textureunitindex < 0 || (size_t) textureunitindex >= textureUnits.size())
|
|
|
|
+ throw love::Exception("Invalid texture unit index.");
|
|
|
|
+
|
|
|
|
+ if (textureunitindex != curTextureUnitIndex)
|
|
{
|
|
{
|
|
- boundTexture = texture;
|
|
|
|
|
|
+ if (GLEE_VERSION_1_3)
|
|
|
|
+ glActiveTexture(textureunit);
|
|
|
|
+ else if (GLEE_ARB_multitexture)
|
|
|
|
+ glActiveTextureARB(textureunit);
|
|
|
|
+ else
|
|
|
|
+ throw love::Exception("Multitexturing not supported.");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ curTextureUnitIndex = textureunitindex;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void bindTexture(GLuint texture)
|
|
|
|
+{
|
|
|
|
+ initializeContext();
|
|
|
|
+
|
|
|
|
+ if (texture != textureUnits[curTextureUnitIndex] || texture == 0)
|
|
|
|
+ {
|
|
|
|
+ textureUnits[curTextureUnitIndex] = texture;
|
|
glBindTexture(GL_TEXTURE_2D, texture);
|
|
glBindTexture(GL_TEXTURE_2D, texture);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void deleteTexture(GLuint texture)
|
|
void deleteTexture(GLuint texture)
|
|
{
|
|
{
|
|
- if (texture == boundTexture)
|
|
|
|
- boundTexture = 0;
|
|
|
|
|
|
+ initializeContext();
|
|
|
|
+
|
|
|
|
+ std::vector<GLuint>::iterator it;
|
|
|
|
+ for (it = textureUnits.begin(); it != textureUnits.end(); ++it)
|
|
|
|
+ {
|
|
|
|
+ if (*it == texture)
|
|
|
|
+ *it = 0;
|
|
|
|
+ }
|
|
|
|
|
|
glDeleteTextures(1, &texture);
|
|
glDeleteTextures(1, &texture);
|
|
}
|
|
}
|