fix texture/material methods

This commit is contained in:
Nick Fisher
2025-03-03 15:52:03 +08:00
parent 2a8931e1ac
commit 8d52690c97
10 changed files with 742 additions and 163 deletions

View File

@@ -19,10 +19,11 @@ namespace thermion
extern "C"
{
using namespace filament::backend;
#endif
EMSCRIPTEN_KEEPALIVE TLinearImage* Image_decode(uint8_t* data, size_t length, const char* name = "image") {
EMSCRIPTEN_KEEPALIVE TLinearImage *Image_decode(uint8_t *data, size_t length, const char *name = "image")
{
std::istringstream stream(std::string(reinterpret_cast<const char *>(data), length));
auto *linearImage = new image::LinearImage(::image::ImageDecoder::decode(stream, name, ::image::ImageDecoder::ColorSpace::SRGB));
@@ -32,57 +33,60 @@ namespace thermion
Log("Failed to decode image.");
return nullptr;
}
return reinterpret_cast<TLinearImage*>(linearImage);
return reinterpret_cast<TLinearImage *>(linearImage);
}
EMSCRIPTEN_KEEPALIVE uint32_t Image_getWidth(TLinearImage* tLinearImage) {
auto *linearImage = reinterpret_cast<::image::LinearImage*>(tLinearImage);
EMSCRIPTEN_KEEPALIVE uint32_t Image_getWidth(TLinearImage *tLinearImage)
{
auto *linearImage = reinterpret_cast<::image::LinearImage *>(tLinearImage);
return linearImage->getWidth();
}
EMSCRIPTEN_KEEPALIVE uint32_t Image_getHeight(TLinearImage* tLinearImage) {
auto *linearImage = reinterpret_cast<::image::LinearImage*>(tLinearImage);
EMSCRIPTEN_KEEPALIVE uint32_t Image_getHeight(TLinearImage *tLinearImage)
{
auto *linearImage = reinterpret_cast<::image::LinearImage *>(tLinearImage);
return linearImage->getHeight();
}
EMSCRIPTEN_KEEPALIVE uint32_t Image_getChannels(TLinearImage* tLinearImage) {
auto *linearImage = reinterpret_cast<::image::LinearImage*>(tLinearImage);
EMSCRIPTEN_KEEPALIVE uint32_t Image_getChannels(TLinearImage *tLinearImage)
{
auto *linearImage = reinterpret_cast<::image::LinearImage *>(tLinearImage);
return linearImage->getChannels();
}
EMSCRIPTEN_KEEPALIVE void Image_destroy(TLinearImage* tLinearImage) {
auto *linearImage = reinterpret_cast<::image::LinearImage*>(tLinearImage);
EMSCRIPTEN_KEEPALIVE void Image_destroy(TLinearImage *tLinearImage)
{
auto *linearImage = reinterpret_cast<::image::LinearImage *>(tLinearImage);
delete linearImage;
}
EMSCRIPTEN_KEEPALIVE bool Texture_loadImage(TEngine *tEngine, TTexture *tTexture, TLinearImage *tImage, TPixelDataFormat tBufferFormat, TPixelDataType tPixelDataType)
{
auto engine = reinterpret_cast<filament::Engine*>(tEngine);
auto image = reinterpret_cast<::image::LinearImage*>(tImage);
auto texture = reinterpret_cast<filament::Texture*>(tTexture);
auto engine = reinterpret_cast<filament::Engine *>(tEngine);
auto image = reinterpret_cast<::image::LinearImage *>(tImage);
auto texture = reinterpret_cast<filament::Texture *>(tTexture);
auto bufferFormat = static_cast<PixelBufferDescriptor::PixelDataFormat>(static_cast<int>(tBufferFormat));
auto pixelDataType = static_cast<PixelBufferDescriptor::PixelDataType>(static_cast<int>(tPixelDataType));
uint32_t w = image->getWidth();
uint32_t h = image->getHeight();
uint32_t channels = image->getChannels();
size_t size;
switch(bufferFormat) {
case PixelBufferDescriptor::PixelDataFormat::RGB:
case PixelBufferDescriptor::PixelDataFormat::RGBA:
size = w * h * channels * sizeof(float);
break;
case PixelBufferDescriptor::PixelDataFormat::RGB_INTEGER:
case PixelBufferDescriptor::PixelDataFormat::RGBA_INTEGER:
size = w * h * channels * sizeof(uint8_t);
break;
default:
Log("Unsupported buffer format type : %d", bufferFormat);
return false;
switch (bufferFormat)
{
case PixelBufferDescriptor::PixelDataFormat::RGB:
case PixelBufferDescriptor::PixelDataFormat::RGBA:
size = w * h * channels * sizeof(float);
break;
case PixelBufferDescriptor::PixelDataFormat::RGB_INTEGER:
case PixelBufferDescriptor::PixelDataFormat::RGBA_INTEGER:
size = w * h * channels * sizeof(uint8_t);
break;
default:
Log("Unsupported buffer format type : %d", bufferFormat);
return false;
}
Log("Dimensions %d x %d, channels %d, size %d, buffer format %d and pixel data type %d", w, h,channels, size, bufferFormat, pixelDataType);
Log("Dimensions %d x %d, channels %d, size %d, buffer format %d and pixel data type %d", w, h, channels, size, bufferFormat, pixelDataType);
filament::Texture::PixelBufferDescriptor buffer(
image->getPixelRef(),
@@ -92,7 +96,126 @@ namespace thermion
texture->setImage(*engine, 0, std::move(buffer));
return true;
}
EMSCRIPTEN_KEEPALIVE TTextureSampler *TextureSampler_create()
{
auto *sampler = new filament::TextureSampler();
return reinterpret_cast<TTextureSampler *>(sampler);
}
EMSCRIPTEN_KEEPALIVE TTextureSampler *TextureSampler_createWithFiltering(
TSamplerMinFilter minFilter,
TSamplerMagFilter magFilter,
TSamplerWrapMode wrapS,
TSamplerWrapMode wrapT,
TSamplerWrapMode wrapR)
{
filament::TextureSampler::MinFilter min = static_cast<filament::TextureSampler::MinFilter>(minFilter);
filament::TextureSampler::MagFilter mag = static_cast<filament::TextureSampler::MagFilter>(magFilter);
filament::TextureSampler::WrapMode s = static_cast<filament::TextureSampler::WrapMode>(wrapS);
filament::TextureSampler::WrapMode t = static_cast<filament::TextureSampler::WrapMode>(wrapT);
filament::TextureSampler::WrapMode r = static_cast<filament::TextureSampler::WrapMode>(wrapR);
auto *sampler = new filament::TextureSampler(min, mag, s, t, r);
return reinterpret_cast<TTextureSampler *>(sampler);
}
EMSCRIPTEN_KEEPALIVE TTextureSampler *TextureSampler_createWithComparison(
TSamplerCompareMode compareMode,
TSamplerCompareFunc compareFunc)
{
filament::TextureSampler::CompareMode mode = static_cast<filament::TextureSampler::CompareMode>(compareMode);
filament::TextureSampler::CompareFunc func = static_cast<filament::TextureSampler::CompareFunc>(compareFunc);
auto *sampler = new filament::TextureSampler(mode, func);
return reinterpret_cast<TTextureSampler *>(sampler);
}
EMSCRIPTEN_KEEPALIVE void TextureSampler_setMinFilter(
TTextureSampler *sampler,
TSamplerMinFilter filter)
{
if (sampler)
{
auto *textureSampler = reinterpret_cast<filament::TextureSampler *>(sampler);
textureSampler->setMinFilter(static_cast<filament::TextureSampler::MinFilter>(filter));
}
}
EMSCRIPTEN_KEEPALIVE void TextureSampler_setMagFilter(
TTextureSampler *sampler,
TSamplerMagFilter filter)
{
if (sampler)
{
auto *textureSampler = reinterpret_cast<filament::TextureSampler *>(sampler);
textureSampler->setMagFilter(static_cast<filament::TextureSampler::MagFilter>(filter));
}
}
EMSCRIPTEN_KEEPALIVE void TextureSampler_setWrapModeS(
TTextureSampler *sampler,
TSamplerWrapMode mode)
{
if (sampler)
{
auto *textureSampler = reinterpret_cast<filament::TextureSampler *>(sampler);
textureSampler->setWrapModeS(static_cast<filament::TextureSampler::WrapMode>(mode));
}
}
EMSCRIPTEN_KEEPALIVE void TextureSampler_setWrapModeT(
TTextureSampler *sampler,
TSamplerWrapMode mode)
{
if (sampler)
{
auto *textureSampler = reinterpret_cast<filament::TextureSampler *>(sampler);
textureSampler->setWrapModeT(static_cast<filament::TextureSampler::WrapMode>(mode));
}
}
EMSCRIPTEN_KEEPALIVE void TextureSampler_setWrapModeR(
TTextureSampler *sampler,
TSamplerWrapMode mode)
{
if (sampler)
{
auto *textureSampler = reinterpret_cast<filament::TextureSampler *>(sampler);
textureSampler->setWrapModeR(static_cast<filament::TextureSampler::WrapMode>(mode));
}
}
EMSCRIPTEN_KEEPALIVE void TextureSampler_setCompareMode(
TTextureSampler *sampler,
TSamplerCompareMode mode,
TSamplerCompareFunc func)
{
if (sampler)
{
auto *textureSampler = reinterpret_cast<filament::TextureSampler *>(sampler);
textureSampler->setCompareMode(
static_cast<filament::TextureSampler::CompareMode>(mode),
static_cast<filament::TextureSampler::CompareFunc>(func));
}
}
EMSCRIPTEN_KEEPALIVE void TextureSampler_destroy(TTextureSampler *sampler)
{
if (sampler)
{
auto *textureSampler = reinterpret_cast<filament::TextureSampler *>(sampler);
delete textureSampler;
}
}
#ifdef __cplusplus