summaryrefslogtreecommitdiff
path: root/src/gltf_loader.cpp
diff options
context:
space:
mode:
authorChuyan Zhang <me@zcy.moe>2024-10-06 11:43:15 -0700
committerChuyan Zhang <me@zcy.moe>2024-10-06 11:43:15 -0700
commit60b9692af28a353c4e5813d1723422477e31f433 (patch)
treee776799b962d3ce179abab5f751904a46e1439c4 /src/gltf_loader.cpp
parent9ed211d1ca084b25d1780da3bde19e9da64d4a4a (diff)
downloadiris-60b9692af28a353c4e5813d1723422477e31f433.tar.gz
iris-60b9692af28a353c4e5813d1723422477e31f433.zip
Add material texture loadglTF-load
Diffstat (limited to 'src/gltf_loader.cpp')
-rw-r--r--src/gltf_loader.cpp52
1 files changed, 49 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),