diff --git a/korman/exporter/explosions.py b/korman/exporter/explosions.py index 16f1ee6..0b06071 100644 --- a/korman/exporter/explosions.py +++ b/korman/exporter/explosions.py @@ -23,11 +23,6 @@ class BlenderOptionNotSupportedError(ExportError): super(ExportError, self).__init__("Unsupported Blender Option: '{}'".format(opt)) -class GLLoadError(ExportError): - def __init__(self, image): - super(ExportError, self).__init__("Failed to load '{}' into OpenGL".format(image.name)) - - class TooManyUVChannelsError(ExportError): def __init__(self, obj, mat): msg = "There are too many UV Textures on the material '{}' associated with object '{}'.".format( diff --git a/korman/exporter/material.py b/korman/exporter/material.py index 351f503..e4ac737 100644 --- a/korman/exporter/material.py +++ b/korman/exporter/material.py @@ -488,7 +488,8 @@ class MaterialConverter: numLevels = max(numLevels - 2, 2) # Grab the image data from OpenGL and stuff it into the plBitmap - with korlib.GLTexture(image) as glimage: + helper = korlib.GLTexture(image) + with helper as glimage: if key.mipmap: print(" Generating mip levels") glimage.generate_mipmap() @@ -523,9 +524,7 @@ class MaterialConverter: if page not in pages: mipmap = plMipmap(name=name, width=eWidth, height=eHeight, numLevels=numLevels, compType=compression, format=plBitmap.kRGB8888, dxtLevel=dxt) - func = mipmap.CompressImage if compression == plBitmap.kDirectXCompression else mipmap.setLevel - for i, level in enumerate(data): - func(i, level) + helper.store_in_mipmap(mipmap, data, compression) mgr.AddObject(page, mipmap) pages[page] = mipmap else: diff --git a/korman/korlib/texture.py b/korman/korlib/texture.py index 16ac20d..7480174 100644 --- a/korman/korlib/texture.py +++ b/korman/korlib/texture.py @@ -14,6 +14,7 @@ # along with Korman. If not, see . import bgl +from PyHSPlasma import plBitmap # BGL doesn't know about this as of Blender 2.74 bgl.GL_GENERATE_MIPMAP = 0x8191 @@ -22,17 +23,14 @@ bgl.GL_BGRA = 0x80E1 class GLTexture: def __init__(self, blimg): self._ownit = (blimg.bindcode == 0) - if self._ownit: - if blimg.gl_load() != 0: - raise explosions.GLLoadError(blimg) self._blimg = blimg - def __del__(self): - if self._ownit: - self._blimg.gl_free() - def __enter__(self): """Sets the Blender Image as the active OpenGL texture""" + if self._ownit: + if self._blimg.gl_load() != 0: + raise RuntimeError("failed to load image") + self._previous_texture = self._get_integer(bgl.GL_TEXTURE_BINDING_2D) self._changed_state = (self._previous_texture != self._blimg.bindcode) if self._changed_state: @@ -43,9 +41,10 @@ class GLTexture: mipmap_state = getattr(self, "_mipmap_state", None) if mipmap_state is not None: bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_GENERATE_MIPMAP, mipmap_state) - if self._changed_state: bgl.glBindTexture(bgl.GL_TEXTURE_2D, self._previous_texture) + if self._ownit: + self._blimg.gl_free() def generate_mipmap(self): """Generates all mip levels for this texture""" @@ -103,3 +102,8 @@ class GLTexture: if data[i] != 255: return True return False + + def store_in_mipmap(self, mipmap, data, compression): + func = mipmap.CompressImage if compression == plBitmap.kDirectXCompression else mipmap.setLevel + for i, level in enumerate(data): + func(i, level)