diff --git a/korlib/texture.cpp b/korlib/texture.cpp index af4f0f6..54a5eca 100644 --- a/korlib/texture.cpp +++ b/korlib/texture.cpp @@ -361,6 +361,90 @@ static PyObject* pyGLTexture_store_in_mipmap(pyGLTexture* self, PyObject* args) Py_RETURN_NONE; } + +static uint32_t MakeUInt32Color(float r, float g, float b, float a) +{ + return (uint32_t(a * 255.9f) << 24) + |(uint32_t(r * 255.9f) << 16) + |(uint32_t(g * 255.9f) << 8) + |(uint32_t(b * 255.9f) << 0); +} + +static PyObject* pyGLTexture_create_bump_LUT(pyGLTexture* self, PyObject* args) { + const int kLUTHeight = 16; + const int kLUTWidth = 16; + + pyMipmap* pymipmap; + if (!PyArg_ParseTuple(args, "O", &pymipmap)) { + PyErr_SetString(PyExc_TypeError, "create_bump_LUT expects a plMipmap"); + return NULL; + } + + plMipmap* texture = plMipmap::Convert(pymipmap->fThis, false); + if (!texture) { + PyErr_SetString(PyExc_TypeError, "create_bump_LUT expects a plMipmap"); + return NULL; + } + + texture->Create(kLUTWidth, kLUTHeight, 1, plBitmap::kUncompressed, plBitmap::kRGB8888); + + int delH = (kLUTHeight - 1) / 5; + int startH = delH / 2 + 1; + int doneH = 0; + + uint32_t* pix = (uint32_t*)const_cast(texture->getImageData()); + int i; + + // Red ramps, one with G,B = 0,0, one with G,B = 127,127 + for (i = 0; i < startH; i++) { + for(int j = 0; j < kLUTWidth; j++) { + float x = float(j) / (kLUTWidth - 1); + *pix++ = MakeUInt32Color(x, 0.0f, 0.0f, 1.0f); + } + } + doneH = i; + for (i = i; i < doneH + delH; i++) { + for (int j = 0; j < kLUTWidth; j++) { + float x = float(j) / (kLUTWidth - 1); + *pix++ = MakeUInt32Color(x, 0.5f, 0.5f, 1.0f); + } + } + doneH = i; + + // Green ramps, one with R,B = 0,0, one with R,B = 127,127 + for (i = i; i < doneH + delH; i++) { + for (int j = 0; j < kLUTWidth; j++) { + float x = float(j) / (kLUTWidth - 1); + *pix++ = MakeUInt32Color(0.0f, x, 0.0f, 1.0f); + } + } + doneH = i; + for (i = i; i < doneH + delH; i++) { + for (int j = 0; j < kLUTWidth; j++) { + float x = float(j) / (kLUTWidth - 1); + *pix++ = MakeUInt32Color(0.5f, x, 0.5f, 1.0f); + } + } + doneH = i; + + // Blue ramps, one with R,G = 0,0, one with R,G = 127,127 + for (i = i; i < doneH + delH; i++) { + for (int j = 0; j < kLUTWidth; j++) { + float x = float(j) / (kLUTWidth - 1); + *pix++ = MakeUInt32Color(0.0f, 0.0f, x, 1.0f); + } + } + doneH = i; + for (i = i; i < kLUTHeight; i++) { + for (int j = 0; j < kLUTWidth; j++) { + float x = float(j) / (kLUTWidth - 1); + *pix++ = MakeUInt32Color(0.5f, 0.5f, x, 1.0f); + } + } + + Py_RETURN_NONE; +} + static PyMethodDef pyGLTexture_Methods[] = { { _pycs("__enter__"), (PyCFunction)pyGLTexture__enter__, METH_NOARGS, NULL }, { _pycs("__exit__"), (PyCFunction)pyGLTexture__enter__, METH_VARARGS, NULL }, @@ -368,6 +452,7 @@ static PyMethodDef pyGLTexture_Methods[] = { { _pycs("generate_mipmap"), (PyCFunction)pyGLTexture_generate_mipmap, METH_NOARGS, NULL }, { _pycs("get_level_data"), (PyCFunction)pyGLTexture_get_level_data, METH_KEYWORDS | METH_VARARGS, NULL }, { _pycs("store_in_mipmap"), (PyCFunction)pyGLTexture_store_in_mipmap, METH_VARARGS, NULL }, + { _pycs("create_bump_LUT"), (PyCFunction)pyGLTexture_create_bump_LUT, METH_STATIC | METH_VARARGS, NULL }, { NULL, NULL, 0, NULL } }; diff --git a/korman/exporter/material.py b/korman/exporter/material.py index c252bc3..d2a4b09 100644 --- a/korman/exporter/material.py +++ b/korman/exporter/material.py @@ -110,6 +110,7 @@ class MaterialConverter: def __init__(self, exporter): self._obj2mat = {} self._bumpMats = {} + self._bumpLUT = None self._exporter = weakref.ref(exporter) self._pending = {} self._alphatest = {} @@ -274,8 +275,18 @@ class MaterialConverter: dw_layer.UVWSrc = du_uv | plLayerInterface.kUVWNormal dv_layer.UVWSrc = du_uv + 1 - return (du_layer, dw_layer, dv_layer, nm_layer) + if self._bumpLUT is None: + self._bumpLUT = plMipmap("BumpLutTexture") + GLTexture.create_bump_LUT(self._bumpLUT) + + page = self._mgr.get_textures_page(du_layer.key) + self._mgr.AddObject(page, self._bumpLUT) + du_layer.texture = self._bumpLUT.key + dw_layer.texture = self._bumpLUT.key + dv_layer.texture = self._bumpLUT.key + + return (du_layer, dw_layer, dv_layer, nm_layer) def export_texture_slot(self, bo, bm, hsgmat, slot, idx, name=None, blend_flags=True): if name is None: