diff options
author | Chuyan Zhang <me@zcy.moe> | 2024-10-06 11:43:15 -0700 |
---|---|---|
committer | Chuyan Zhang <me@zcy.moe> | 2024-10-06 11:43:15 -0700 |
commit | 60b9692af28a353c4e5813d1723422477e31f433 (patch) | |
tree | e776799b962d3ce179abab5f751904a46e1439c4 /src | |
parent | 9ed211d1ca084b25d1780da3bde19e9da64d4a4a (diff) | |
download | iris-glTF-load.tar.gz iris-glTF-load.zip |
Add material texture loadglTF-load
Diffstat (limited to 'src')
-rw-r--r-- | src/gltf_loader.cpp | 52 | ||||
-rw-r--r-- | src/render_assets.h | 23 |
2 files changed, 72 insertions, 3 deletions
diff --git a/src/gltf_loader.cpp b/src/gltf_loader.cpp index 684fa7f..a692e93 100644 --- a/src/gltf_loader.cpp +++ b/src/gltf_loader.cpp @@ -1,6 +1,7 @@ #include "gltf_loader.h" #include "render_assets.h" #include "spdlog/spdlog.h" +#include "tiny_gltf.h" #include <cstdint> enum class SceneFileType { @@ -214,6 +215,47 @@ bool load_gltf(const std::string_view path, iris::Scene &scene) { } } + std::vector<iris::Image> images; + images.reserve(model.images.size()); + for (const tinygltf::Image &image : model.images) { + images.emplace_back(iris::Image { + .data = image.image, + .extent = glm::uvec3(image.width, image.height, image.component), + .bits_per_channel = image.bits, + }); + } + + std::vector<iris::Sampler2D> samplers; + samplers.reserve(model.samplers.size()); + for (const tinygltf::Sampler &sampler : model.samplers) { + samplers.emplace_back(iris::Sampler2D { + .min_filter = sampler.minFilter, + .mag_filter = sampler.magFilter, + .wrap_s = sampler.wrapS, + .wrap_t = sampler.wrapT, + }); + } + + // load textures + for (const tinygltf::Texture &texture : model.textures) { + const std::string texture_name = texture.name == "" ? "texture" : texture.name; + if (texture.source < 0 || unsigned(texture.source) >= images.size()) { + spdlog::error("Invalid texture source index: {}", texture.source); + return false; + } + if (texture.sampler < 0 || unsigned(texture.sampler) >= samplers.size()) { + spdlog::error("Invalid texture sampler index: {}", texture.sampler); + return false; + } + + iris::Texture iris_texture { + .name = texture_name, + .image = images[texture.source], + .sampler = samplers[texture.sampler], + }; + spdlog::info("Texture: {}", texture_name); + } + for (const tinygltf::Material &material : model.materials) { const std::string material_name = material.name == "" ? "material" : material.name; const auto &pbr = material.pbrMetallicRoughness; @@ -227,13 +269,17 @@ bool load_gltf(const std::string_view path, iris::Scene &scene) { .metallic = float(pbr.metallicFactor), .roughness = float(pbr.roughnessFactor), }; - + // Where to detect if the texture is valid? Can we early intercept? + iris_material.base_color_texture = pbr.baseColorTexture.index; + iris_material.metallic_roughness_texture = pbr.metallicRoughnessTexture.index; + iris_material.normal_texture = material.normalTexture.index; + iris_material.occulsion_texture = material.occlusionTexture.index; + iris_material.emissive_texture = material.emissiveTexture.index; + // TODO: load texture information, skip for now spdlog::info("Material: {}", material_name); } - // TODO load materials and textures - for (const tinygltf::Camera &camera : model.cameras) { iris::Camera iris_camera { .position = glm::vec3(0.0f), diff --git a/src/render_assets.h b/src/render_assets.h index 113084a..1819271 100644 --- a/src/render_assets.h +++ b/src/render_assets.h @@ -27,9 +27,32 @@ struct Material { glm::vec4 base_color; float metallic; float roughness; + + int32_t base_color_texture; + int32_t metallic_roughness_texture; + int32_t normal_texture; + int32_t occulsion_texture; + int32_t emissive_texture; +}; + + +struct Image { + std::vector<uint8_t> data; + glm::uvec3 extent; + int32_t bits_per_channel; +}; + +struct Sampler2D { + int32_t min_filter; + int32_t mag_filter; + int32_t wrap_s; + int32_t wrap_t; }; struct Texture { + std::string name; + Image image; + Sampler2D sampler; }; struct Camera { |