diff --git a/korlib/CMakeLists.txt b/korlib/CMakeLists.txt index 784bd23..6cbcc30 100644 --- a/korlib/CMakeLists.txt +++ b/korlib/CMakeLists.txt @@ -19,6 +19,7 @@ find_package(Vorbis REQUIRED) # Da files set(korlib_HEADERS buffer.h + bumpmap.h korlib.h sound.h texture.h @@ -26,6 +27,7 @@ set(korlib_HEADERS set(korlib_SOURCES buffer.cpp + bumpmap.cpp module.cpp sound.cpp texture.cpp diff --git a/korlib/bumpmap.cpp b/korlib/bumpmap.cpp new file mode 100644 index 0000000..00ca20f --- /dev/null +++ b/korlib/bumpmap.cpp @@ -0,0 +1,113 @@ +/* This file is part of Korman. + * + * Korman is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Korman is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Korman. If not, see . + */ + +#include "bumpmap.h" +#include + +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); +} + +typedef struct { + PyObject_HEAD + plMipmap* fThis; + bool fPyOwned; +} pyMipmap; + +extern "C" { + +PyObject* create_bump_LUT(PyObject*, 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; + + uint8_t* data = new uint8_t[texture->getTotalSize()]; + uint32_t* pix = (uint32_t*)data; + 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); + } + } + + texture->setImageData(data, texture->getTotalSize()); + + Py_RETURN_NONE; +} + +}; diff --git a/korlib/bumpmap.h b/korlib/bumpmap.h new file mode 100644 index 0000000..13a2d9a --- /dev/null +++ b/korlib/bumpmap.h @@ -0,0 +1,29 @@ +/* This file is part of Korman. + * + * Korman is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Korman is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Korman. If not, see . + */ + +#ifndef _KORLIB_BUMPMAP_H +#define _KORLIB_BUMPMAP_H + +#include "korlib.h" + +extern "C" { + +PyObject* create_bump_LUT(PyObject*, PyObject* args); + +}; + +#endif // _KORLIB_BUMPMAP_H + diff --git a/korlib/module.cpp b/korlib/module.cpp index 6ff987c..8850715 100644 --- a/korlib/module.cpp +++ b/korlib/module.cpp @@ -15,12 +15,14 @@ */ #include "buffer.h" +#include "bumpmap.h" #include "sound.h" #include "texture.h" extern "C" { static PyMethodDef korlib_Methods[] = { + { _pycs("create_bump_LUT"), (PyCFunction)create_bump_LUT, METH_VARARGS, NULL }, { _pycs("inspect_vorbisfile"), (PyCFunction)inspect_vorbisfile, METH_VARARGS, NULL }, { NULL, NULL, 0, NULL }, diff --git a/korlib/texture.cpp b/korlib/texture.cpp index fa3cd4d..af4f0f6 100644 --- a/korlib/texture.cpp +++ b/korlib/texture.cpp @@ -361,98 +361,10 @@ 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; - - uint8_t* data = new uint8_t[texture->getTotalSize()]; - uint32_t* pix = (uint32_t*)data; - 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); - } - } - - texture->setImageData(data, texture->getTotalSize()); - - Py_RETURN_NONE; -} - static PyMethodDef pyGLTexture_Methods[] = { { _pycs("__enter__"), (PyCFunction)pyGLTexture__enter__, METH_NOARGS, NULL }, { _pycs("__exit__"), (PyCFunction)pyGLTexture__enter__, METH_VARARGS, NULL }, - { _pycs("create_bump_LUT"), (PyCFunction)pyGLTexture_create_bump_LUT, METH_STATIC | METH_VARARGS, NULL }, { _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 }, diff --git a/korman/exporter/material.py b/korman/exporter/material.py index 0dba72e..5ab8a2c 100644 --- a/korman/exporter/material.py +++ b/korman/exporter/material.py @@ -244,7 +244,7 @@ class MaterialConverter: if LUT_key is None: bumpLUT = plMipmap("BumpLutTexture", 16, 16, 1, plBitmap.kUncompressed, plBitmap.kRGB8888) - GLTexture.create_bump_LUT(bumpLUT) + create_bump_LUT(bumpLUT) self._mgr.AddObject(page, bumpLUT) LUT_key = bumpLUT.key diff --git a/korman/korlib/__init__.py b/korman/korlib/__init__.py index 9a4c22d..77b9457 100644 --- a/korman/korlib/__init__.py +++ b/korman/korlib/__init__.py @@ -18,6 +18,42 @@ try: except ImportError: from .texture import * + def create_bump_LUT(mipmap): + kLUTHeight = 16 + kLUTWidth = 16 + + buf = bytearray(kLUTHeight * kLUTWidth * 4) + + denom = kLUTWidth - 1 + delH = (kLUTHeight - 1) // 5 + startH = delH // 2 + 1 + doneH = 0 + + doneH = startH * kLUTWidth * 4 + buf[0:doneH] = [b for x in range(kLUTWidth) for b in (0, 0, int((x / denom) * 255.9), 255)] * startH + + startH = doneH + doneH += delH * kLUTWidth * 4 + buf[startH:doneH] = [b for x in range(kLUTWidth) for b in (127, 127, int((x / denom) * 255.9), 255)] * delH + + startH = doneH + doneH += delH * kLUTWidth * 4 + buf[startH:doneH] = [b for x in range(kLUTWidth) for b in (0, int((x / denom) * 255.9), 0, 255)] * delH + + startH = doneH + doneH += delH * kLUTWidth * 4 + buf[startH:doneH] = [b for x in range(kLUTWidth) for b in (127, int((x / denom) * 255.9), 127, 255)] * delH + + startH = doneH + doneH += delH * kLUTWidth * 4 + buf[startH:doneH] = [b for x in range(kLUTWidth) for b in (int((x / denom) * 255.9), 0, 0, 255)] * delH + + startH = doneH + doneH += delH * kLUTWidth * 4 + buf[startH:doneH] = [b for x in range(kLUTWidth) for b in (int((x / denom) * 255.9), 127, 127, 255)] * startH + + mipmap.setRawImage(bytes(buf)) + def inspect_voribsfile(stream, header): raise NotImplementedError("Ogg Vorbis not supported unless _korlib is compiled") diff --git a/korman/korlib/texture.py b/korman/korlib/texture.py index 2d4e4f7..11a289e 100644 --- a/korman/korlib/texture.py +++ b/korman/korlib/texture.py @@ -186,41 +186,3 @@ class GLTexture: func = mipmap.CompressImage if compression == plBitmap.kDirectXCompression else mipmap.setLevel for i, level in enumerate(data): func(i, level) - - - @staticmethod - def create_bump_LUT(mipmap): - kLUTHeight = 16 - kLUTWidth = 16 - - buf = bytearray(kLUTHeight * kLUTWidth * 4) - - denom = kLUTWidth - 1 - delH = (kLUTHeight - 1) // 5 - startH = delH // 2 + 1 - doneH = 0 - - doneH = startH * kLUTWidth * 4 - buf[0:doneH] = [b for x in range(kLUTWidth) for b in (0, 0, int((x / denom) * 255.9), 255)] * startH - - startH = doneH - doneH += delH * kLUTWidth * 4 - buf[startH:doneH] = [b for x in range(kLUTWidth) for b in (127, 127, int((x / denom) * 255.9), 255)] * delH - - startH = doneH - doneH += delH * kLUTWidth * 4 - buf[startH:doneH] = [b for x in range(kLUTWidth) for b in (0, int((x / denom) * 255.9), 0, 255)] * delH - - startH = doneH - doneH += delH * kLUTWidth * 4 - buf[startH:doneH] = [b for x in range(kLUTWidth) for b in (127, int((x / denom) * 255.9), 127, 255)] * delH - - startH = doneH - doneH += delH * kLUTWidth * 4 - buf[startH:doneH] = [b for x in range(kLUTWidth) for b in (int((x / denom) * 255.9), 0, 0, 255)] * delH - - startH = doneH - doneH += delH * kLUTWidth * 4 - buf[startH:doneH] = [b for x in range(kLUTWidth) for b in (int((x / denom) * 255.9), 127, 127, 255)] * startH - - mipmap.setRawImage(bytes(buf))