Skip to content

Commit

Permalink
Add support for exr files in gltf_Vvewer
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelflinger committed Sep 16, 2024
1 parent 0fd2436 commit d16fc01
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
1 change: 1 addition & 0 deletions libs/filamentapp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ set(LIBS
geometry
getopt
image
imageio
imgui
ktxreader
math
Expand Down
1 change: 1 addition & 0 deletions libs/filamentapp/include/filamentapp/IBL.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <math/vec3.h>

#include <string>
#include <sstream>

namespace filament {
class Engine;
Expand Down
53 changes: 41 additions & 12 deletions libs/filamentapp/src/IBL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#include <ktxreader/Ktx1Reader.h>

#include <imageio/ImageDecoder.h>

#include <filament-iblprefilter/IBLPrefilterContext.h>

#include <stb_image.h>
Expand All @@ -37,6 +39,8 @@

#include <string.h>

#include <utils/Log.h>

using namespace filament;
using namespace filament::math;
using namespace ktxreader;
Expand All @@ -60,26 +64,51 @@ bool IBL::loadFromEquirect(Path const& path) {
return false;
}

int w, h;
stbi_info(path.getAbsolutePath().c_str(), &w, &h, nullptr);
if (w != h * 2) {
std::cerr << "not an equirectangular image!" << std::endl;
return false;
int w = 0, h = 0;
int n = 0;
size_t size = 0;
void* data = nullptr;
void* user = nullptr;
Texture::PixelBufferDescriptor::Callback destroyer{};

if (path.getExtension() == "exr") {
std::ifstream in_stream(path.getAbsolutePath().c_str(), std::ios::binary);
image::LinearImage* image = new image::LinearImage(
image::ImageDecoder::decode(in_stream, path.getAbsolutePath().c_str()));
w = image->getWidth();
h = image->getHeight();
n = image->getChannels();
size = w * h * n * sizeof(float);
data = image->getPixelRef();
user = image;
destroyer = [](void*, size_t, void* user) {
delete reinterpret_cast<image::LinearImage*>(user);
};
} else {
stbi_info(path.getAbsolutePath().c_str(), &w, &h, nullptr);
// load image as float
size = w * h * sizeof(float3);
data = (float3*)stbi_loadf(path.getAbsolutePath().c_str(), &w, &h, &n, 3);
destroyer = [](void* data, size_t, void*) {
stbi_image_free(data);
};
}

// load image as float
int n;
const size_t size = w * h * sizeof(float3);
float3* const data = (float3*)stbi_loadf(path.getAbsolutePath().c_str(), &w, &h, &n, 3);
if (data == nullptr || n != 3) {
std::cerr << "Could not decode image " << std::endl;
destroyer(data, size, user);
return false;
}

if (w != h * 2) {
std::cerr << "not an equirectangular image!" << std::endl;
destroyer(data, size, user);
return false;
}

// now load texture
Texture::PixelBufferDescriptor buffer(
data, size,Texture::Format::RGB, Texture::Type::FLOAT,
[](void* buffer, size_t size, void* user) { stbi_image_free(buffer); });
data, size,Texture::Format::RGB, Texture::Type::FLOAT, destroyer, user);

Texture* const equirect = Texture::Builder()
.width((uint32_t)w)
Expand All @@ -102,7 +131,7 @@ bool IBL::loadFromEquirect(Path const& path) {

mTexture = specularFilter(mSkyboxTexture);

mFogTexture = irradianceFilter({ .generateMipmap=false }, mSkyboxTexture);
mFogTexture = irradianceFilter({ .generateMipmap = false }, mSkyboxTexture);
mFogTexture->generateMipmaps(mEngine);

mIndirectLight = IndirectLight::Builder()
Expand Down
4 changes: 3 additions & 1 deletion samples/gltf_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,9 @@ static void onClick(App& app, View* view, ImVec2 pos) {

static utils::Path getPathForIBLAsset(std::string_view string) {
auto isIBL = [] (utils::Path file) -> bool {
return file.getExtension() == "ktx" || file.getExtension() == "hdr";
return file.getExtension() == "ktx" || file.getExtension() == "hdr" ||
file.getExtension() == "exr";

};

utils::Path filename{ string };
Expand Down

0 comments on commit d16fc01

Please sign in to comment.