4
4
mirror of https://github.com/H-uru/korman.git synced 2025-07-14 22:36:52 +00:00

Pure Python korlib

Yeah, korlib was a bit faster than this implementation, but, honestly,
having a second python module made life more difficult than it needed to
be. This fixes that.
This commit is contained in:
2014-07-11 22:33:07 -04:00
parent a2bde5ff28
commit f3b7ae3ad5
8 changed files with 118 additions and 403 deletions

View File

@ -18,6 +18,11 @@ class ExportError(Exception):
super(Exception, self).__init__(value)
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(

View File

@ -14,13 +14,91 @@
# along with Korman. If not, see <http://www.gnu.org/licenses/>.
import bpy
import korlib
import bgl
import math
from PyHSPlasma import *
import weakref
from . import explosions
from . import utils
# BGL doesn't know about this as of Blender 2.71
bgl.GL_GENERATE_MIPMAP = 0x8191
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"""
self._previous_texture = self._get_integer(bgl.GL_TEXTURE_BINDING_2D)
self._changed_state = (self._previous_texture != self._blimg.bindcode)
if self._changed_state:
bgl.glBindTexture(bgl.GL_TEXTURE_2D, self._blimg.bindcode)
return self
def __exit__(self, type, value, traceback):
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)
def generate_mipmap(self):
"""Generates all mip levels for this texture"""
self._mipmap_state = self._get_tex_param(bgl.GL_GENERATE_MIPMAP)
# Note that this is a very old feature from OpenGL 1.x -- it's new enough that Windows (and
# Blender apparently) don't support it natively and yet old enough that it was thrown away
# in OpenGL 3.0. The new way is glGenerateMipmap, but Blender likes oldgl, so we don't have that
# function available to us in BGL. I don't want to deal with loading the GL dll in ctypes on
# many platforms right now (or context headaches). If someone wants to fix this, be my guest!
# It will simplify our state tracking a bit.
bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_GENERATE_MIPMAP, 1)
def get_level_data(self, level, calc_alpha=False):
"""Gets the uncompressed pixel data for a requested mip level, optionally calculating the alpha
channel from the image color data
"""
width = self._get_tex_param(bgl.GL_TEXTURE_WIDTH, level)
height = self._get_tex_param(bgl.GL_TEXTURE_HEIGHT, level)
# Grab the image data
size = width * height * 4
buf = bgl.Buffer(bgl.GL_BYTE, size)
bgl.glGetTexImage(bgl.GL_TEXTURE_2D, level, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, buf);
# Calculate le alphas
if calc_alpha:
for i in range(size, 4):
base = i*4
r, g, b = buf[base:base+2]
buf[base+3] = int((r + g + b) / 3)
return bytes(buf)
def _get_integer(self, arg):
buf = bgl.Buffer(bgl.GL_INT, 1)
bgl.glGetIntegerv(arg, buf)
return int(buf[0])
def _get_tex_param(self, param, level=None):
buf = bgl.Buffer(bgl.GL_INT, 1)
if level is None:
bgl.glGetTexParameteriv(bgl.GL_TEXTURE_2D, param, buf)
else:
bgl.glGetTexLevelParameteriv(bgl.GL_TEXTURE_2D, level, param, buf)
return int(buf[0])
class MaterialConverter:
def __init__(self, exporter):
self._exporter = weakref.ref(exporter)
@ -102,13 +180,45 @@ class MaterialConverter:
return
else:
location = self._mgr.get_textures_page(bo)
bitmap = self._mgr.add_object(plMipmap, name=name, loc=location)
korlib.generate_mipmap(texture, bitmap)
bitmap = self._TEMP_export_image(bo, name, texture)
# Store the created plBitmap and toss onto the layer
self._hsbitmaps[name] = bitmap
layer.texture = bitmap.key
def _TEMP_export_image(self, bo, name, texture):
print(" Exporting {}".format(name))
image = texture.image
oWidth, oHeight = image.size
eWidth = int(round(pow(2, math.log(oWidth, 2))))
eHeight = int(round(pow(2, math.log(oHeight, 2))))
if (eWidth != oWidth) or (eHeight != oHeight):
print(" Image is not a POT ({}x{}) resizing to {}x{}".format(oWidth, oHeight, eWidth, eHeight))
image.scale(eWidth, eHeight)
# Basic things
levelHint = 0 if texture.use_mipmap else 1
compression = plBitmap.kDirectXCompression if texture.use_mipmap else plBitmap.kUncompressed
dxt = plBitmap.kDXT5 if texture.use_alpha or texture.use_calculate_alpha else plBitmap.kDXT1
# This wraps the call to plMipmap::Create
mipmap = plMipmap(name=name, width=eWidth, height=eHeight, numLevels=levelHint,
compType=compression, format=plBitmap.kRGB8888, dxtLevel=dxt)
page = self._mgr.get_textures_page(bo)
self._mgr.AddObject(page, mipmap)
with _GLTexture(image) as glimage:
if texture.use_mipmap:
glimage.generate_mipmap()
stuff_func = mipmap.CompressImage if compression == plBitmap.kDirectXCompression else mipmap.setLevel
for i in range(mipmap.numLevels):
data = glimage.get_level_data(i, texture.use_calculate_alpha)
stuff_func(i, data)
return mipmap
def _export_texture_type_none(self, bo, hsgmat, layer, texture):
# We'll allow this, just for sanity's sake...
pass