From 8c0163bcb48df56112a625550d411741c20c5846 Mon Sep 17 00:00:00 2001 From: Dave Houlton Date: Wed, 13 Apr 2022 12:32:58 -0600 Subject: SL-17214 initial loader class skeleton --- indra/llprimitive/llgltfloader.cpp | 336 +++++++++++++++++++++++++++++++++++++ 1 file changed, 336 insertions(+) create mode 100644 indra/llprimitive/llgltfloader.cpp (limited to 'indra/llprimitive/llgltfloader.cpp') diff --git a/indra/llprimitive/llgltfloader.cpp b/indra/llprimitive/llgltfloader.cpp new file mode 100644 index 0000000000..001623ac9e --- /dev/null +++ b/indra/llprimitive/llgltfloader.cpp @@ -0,0 +1,336 @@ +/** + * @file LLGLTFLoader.cpp + * @brief LLGLTFLoader class implementation + * + * $LicenseInfo:firstyear=2022&license=viewerlgpl$ + * Second Life Viewer Source Code + * Copyright (C) 2022, Linden Research, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License only. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA + * $/LicenseInfo$ + */ + +#include "LLGLTFLoader.h" + +// Import & define single-header gltf import/export lib +#define TINYGLTF_IMPLEMENTATION +#define TINYGLTF_USE_CPP14 // default is C++ 11 + +// tinygltf by default loads image files using STB +#define STB_IMAGE_IMPLEMENTATION +// to use our own image loading: +// 1. replace this definition with TINYGLTF_NO_STB_IMAGE +// 2. provide image loader callback with TinyGLTF::SetImageLoader(LoadimageDataFunction LoadImageData, void *user_data) + +// tinygltf saves image files using STB +#define STB_IMAGE_WRITE_IMPLEMENTATION +// similarly, can override with TINYGLTF_NO_STB_IMAGE_WRITE and TinyGLTF::SetImageWriter(fxn, data) + +// Additionally, disable inclusion of STB header files entirely with +// TINYGLTF_NO_INCLUDE_STB_IMAGE +// TINYGLTF_NO_INCLUDE_STB_IMAGE_WRITE +#include "tinygltf\tiny_gltf.h" + +#include + +#include "llsdserialize.h" +#include "lljoint.h" + +#include "glh/glh_linear.h" +#include "llmatrix4a.h" + +#include +#include + +static const std::string lod_suffix[LLModel::NUM_LODS] = +{ + "_LOD0", + "_LOD1", + "_LOD2", + "", + "_PHYS", +}; + +const U32 LIMIT_MATERIALS_OUTPUT = 12; + +LLGLTFLoader::LLGLTFLoader(std::string filename, + S32 lod, + LLModelLoader::load_callback_t load_cb, + LLModelLoader::joint_lookup_func_t joint_lookup_func, + LLModelLoader::texture_load_func_t texture_load_func, + LLModelLoader::state_callback_t state_cb, + void * opaque_userdata, + JointTransformMap & jointTransformMap, + JointNameSet & jointsFromNodes, + std::map &jointAliasMap, + U32 maxJointsPerMesh, + U32 modelLimit) //, + //bool preprocess) + : LLModelLoader( filename, + lod, + load_cb, + joint_lookup_func, + texture_load_func, + state_cb, + opaque_userdata, + jointTransformMap, + jointsFromNodes, + jointAliasMap, + maxJointsPerMesh ), + mGeneratedModelLimit(modelLimit), + //mPreprocessGLTF(preprocess), + mMeshesLoaded(false), + mMaterialsLoaded(false) +{ +} + +LLGLTFLoader::~LLGLTFLoader() {} + +bool LLGLTFLoader::OpenFile(const std::string &filename) +{ + tinygltf::TinyGLTF loader; + std::string error_msg; + std::string warn_msg; + + // Load a tinygltf model fom a file. Assumes that the input filename has already been + // been sanitized to one of (.gltf , .glb) extensions, so does a simple find to distinguish. + if (std::string::npos == filename.rfind(".gltf")) + { // file is binary + mGltfLoaded = loader.LoadBinaryFromFile(&mGltfModel, &error_msg, &warn_msg, filename); + } + else + { // file is ascii + mGltfLoaded = loader.LoadASCIIFromFile(&mGltfModel, &error_msg, &warn_msg, filename); + } + + if (!mGltfLoaded) + { + if (!warn_msg.empty()) + LL_WARNS() << "gltf load warning: " << warn_msg.c_str() << LL_ENDL; + if (!error_msg.empty()) + LL_WARNS() << "gltf load error: " << error_msg.c_str() << LL_ENDL; + return false; + } + + mMeshesLoaded = parseMeshes(); + if (mMeshesLoaded) uploadMeshes(); + + mMaterialsLoaded = parseMaterials(); + if (mMaterialsLoaded) uploadMaterials(); + + return (mMeshesLoaded || mMaterialsLoaded); +} + +bool LLGLTFLoader::parseMeshes() +{ + if (!mGltfLoaded) return false; + + // 2022-04 DJH Volume params from dae example. TODO understand PCODE + LLVolumeParams volume_params; + volume_params.setType(LL_PCODE_PROFILE_SQUARE, LL_PCODE_PATH_LINE); + + for (tinygltf::Mesh mesh : mGltfModel.meshes) + { + LLModel *pModel = new LLModel(volume_params, 0.f); + + if (populateModelFromMesh(pModel, mesh) && + (LLModel::NO_ERRORS == pModel->getStatus()) && + validate_model(pModel)) + { + mModelList.push_back(pModel); + } + else + { + setLoadState(ERROR_MODEL + pModel->getStatus()); + delete(pModel); + return false; + } + } + return true; +} + +bool LLGLTFLoader::populateModelFromMesh(LLModel* pModel, const tinygltf::Mesh &mesh) +{ + pModel->mLabel = mesh.name; + int pos_idx, norm_idx, tan_idx, uv0_idx, uv1_idx, color0_idx, color1_idx; + tinygltf::Accessor indices_a, positions_a, normals_a, uv0_a, color0_a; + + auto prims = mesh.primitives; + for (auto prim : prims) + { + if (prim.indices >= 0) indices_a = mGltfModel.accessors[prim.indices]; + + pos_idx = (prim.attributes.count("POSITION") > 0) ? prim.attributes.at("POSITION") : -1; + if (pos_idx >= 0) + { + positions_a = mGltfModel.accessors[pos_idx]; + if (TINYGLTF_COMPONENT_TYPE_FLOAT != positions_a.componentType) + continue; + auto positions_bv = mGltfModel.bufferViews[positions_a.bufferView]; + auto positions_buf = mGltfModel.buffers[positions_bv.buffer]; + //auto type = positions_vb. + //if (positions_buf.name + } + + norm_idx = (prim.attributes.count("NORMAL") > 0) ? prim.attributes.at("NORMAL") : -1; + tan_idx = (prim.attributes.count("TANGENT") > 0) ? prim.attributes.at("TANGENT") : -1; + uv0_idx = (prim.attributes.count("TEXCOORDS_0") > 0) ? prim.attributes.at("TEXCOORDS_0") : -1; + uv1_idx = (prim.attributes.count("TEXCOORDS_1") > 0) ? prim.attributes.at("TEXCOORDS_1") : -1; + color0_idx = (prim.attributes.count("COLOR_0") > 0) ? prim.attributes.at("COLOR_0") : -1; + color1_idx = (prim.attributes.count("COLOR_1") > 0) ? prim.attributes.at("COLOR_1") : -1; + + if (prim.mode == TINYGLTF_MODE_TRIANGLES) + { + //auto pos = mesh. TODO resume here DJH 2022-04 + } + } + + //pModel->addFace() + return false; +} + +bool LLGLTFLoader::parseMaterials() +{ + if (!mGltfLoaded) return false; + + // fill local texture data structures + mSamplers.clear(); + for (auto in_sampler : mGltfModel.samplers) + { + gltf_sampler sampler{ 0 }; + sampler.magFilter = in_sampler.magFilter; + sampler.minFilter = in_sampler.minFilter; + sampler.wrapS = in_sampler.wrapS; + sampler.wrapT = in_sampler.wrapT; + sampler.name = in_sampler.name; // unused + mSamplers.push_back(sampler); + } + + mImages.clear(); + for (auto in_image : mGltfModel.images) + { + gltf_image image{ 0 }; + image.numChannels = in_image.component; + image.bytesPerChannel = in_image.bits >> 3; + image.pixelType = in_image.pixel_type; + image.size = in_image.image.size(); + image.height = in_image.height; + image.width = in_image.width; + image.data = in_image.image.data(); + + if (in_image.as_is) + { + LL_WARNS("GLTF_IMPORT") << "Unsupported image encoding" << LL_ENDL; + return false; + } + + if (image.size != image.height * image.width * image.numChannels * image.bytesPerChannel) + { + LL_WARNS("GLTF_IMPORT") << "Image size error" << LL_ENDL; + return false; + } + + mImages.push_back(image); + } + + mTextures.clear(); + for (auto in_tex : mGltfModel.textures) + { + gltf_texture tex{ 0 }; + tex.image_idx = in_tex.source; + tex.sampler_idx = in_tex.sampler; + + if (tex.image_idx >= mImages.size() || tex.sampler_idx >= mSamplers.size()) + { + LL_WARNS("GLTF_IMPORT") << "Texture sampler/image index error" << LL_ENDL; + return false; + } + + mTextures.push_back(tex); + } + + // parse each material + for (tinygltf::Material gltf_material : mGltfModel.materials) + { + gltf_render_material mat{ 0 }; + mat.name = gltf_material.name; + + mat.normalScale = gltf_material.normalTexture.scale; + mat.normalTexIdx = gltf_material.normalTexture.index; + mat.normalTexCoordIdx = gltf_material.normalTexture.texCoord; + + mat.occlusionScale = gltf_material.occlusionTexture.strength; + mat.occlusionTexIdx = gltf_material.occlusionTexture.index; + mat.occlusionTexCoordIdx = gltf_material.occlusionTexture.texCoord; + + mat.emissiveColor.set(gltf_material.emissiveFactor.data()); + mat.emissiveColorTexIdx = gltf_material.emissiveTexture.index; + mat.emissiveColorTexCoordIdx = gltf_material.emissiveTexture.texCoord; + + mat.alphaMode = gltf_material.alphaMode; + mat.alphaMask = gltf_material.alphaCutoff; + + tinygltf::PbrMetallicRoughness& pbr = gltf_material.pbrMetallicRoughness; + mat.hasPBR = true; + + mat.pbr.baseColor.set(pbr.baseColorFactor.data()); + mat.pbr.baseColorTexIdx = pbr.baseColorTexture.index; + mat.pbr.baseColorTexCoordIdx = pbr.baseColorTexture.texCoord; + + mat.pbr.metalness = pbr.metallicFactor; + mat.pbr.roughness = pbr.roughnessFactor; + mat.pbr.metalRoughTexIdx = pbr.metallicRoughnessTexture.index; + mat.pbr.metalRoughTexCoordIdx = pbr.metallicRoughnessTexture.texCoord; + + if (mat.normalTexIdx >= mTextures.size() || + mat.occlusionTexIdx >= mTextures.size() || + mat.emissiveColorTexIdx >= mTextures.size() || + mat.pbr.baseColorTexIdx >= mTextures.size() || + mat.pbr.metalRoughTexIdx >= mTextures.size()) + { + LL_WARNS("GLTF_IMPORT") << "Texture resource index error" << LL_ENDL; + return false; + } + + if (mat.normalTexCoordIdx > 0 || // May have to loosen this condition + mat.occlusionTexCoordIdx > 0 || + mat.emissiveColorTexCoordIdx > 0 || + mat.pbr.baseColorTexCoordIdx > 0 || + mat.pbr.metalRoughTexCoordIdx > 0) + { + LL_WARNS("GLTF_IMPORT") << "Image texcoord index error" << LL_ENDL; + return false; + } + + mMaterials.push_back(mat); + } + + return true; +} + +// TODO: convert raw vertex buffers to UUIDs +void LLGLTFLoader::uploadMeshes() +{ + llassert(0); +} + +// TODO: convert raw index buffers to UUIDs +void LLGLTFLoader::uploadMaterials() +{ + llassert(0); +} + -- cgit v1.2.3 From adaaccd3d74dd05b596693ef7de90aeef20b5f9d Mon Sep 17 00:00:00 2001 From: Dave Houlton Date: Tue, 17 May 2022 15:54:00 -0600 Subject: SL-17214 additional glTF validation, remove dead code from DAE loader --- indra/llprimitive/llgltfloader.cpp | 44 ++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 19 deletions(-) (limited to 'indra/llprimitive/llgltfloader.cpp') diff --git a/indra/llprimitive/llgltfloader.cpp b/indra/llprimitive/llgltfloader.cpp index 001623ac9e..bc9c4760f7 100644 --- a/indra/llprimitive/llgltfloader.cpp +++ b/indra/llprimitive/llgltfloader.cpp @@ -211,9 +211,9 @@ bool LLGLTFLoader::parseMaterials() mSamplers.clear(); for (auto in_sampler : mGltfModel.samplers) { - gltf_sampler sampler{ 0 }; - sampler.magFilter = in_sampler.magFilter; - sampler.minFilter = in_sampler.minFilter; + gltf_sampler sampler; + sampler.magFilter = in_sampler.magFilter > 0 ? in_sampler.magFilter : GL_LINEAR; + sampler.minFilter = in_sampler.minFilter > 0 ? in_sampler.minFilter : GL_LINEAR;; sampler.wrapS = in_sampler.wrapS; sampler.wrapT = in_sampler.wrapT; sampler.name = in_sampler.name; // unused @@ -223,10 +223,10 @@ bool LLGLTFLoader::parseMaterials() mImages.clear(); for (auto in_image : mGltfModel.images) { - gltf_image image{ 0 }; + gltf_image image; image.numChannels = in_image.component; - image.bytesPerChannel = in_image.bits >> 3; - image.pixelType = in_image.pixel_type; + image.bytesPerChannel = in_image.bits >> 3; // Convert bits to bytes + image.pixelType = in_image.pixel_type; // Maps exactly, i.e. TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE == GL_UNSIGNED_BYTE, etc image.size = in_image.image.size(); image.height = in_image.height; image.width = in_image.width; @@ -250,7 +250,7 @@ bool LLGLTFLoader::parseMaterials() mTextures.clear(); for (auto in_tex : mGltfModel.textures) { - gltf_texture tex{ 0 }; + gltf_texture tex; tex.image_idx = in_tex.source; tex.sampler_idx = in_tex.sampler; @@ -266,18 +266,21 @@ bool LLGLTFLoader::parseMaterials() // parse each material for (tinygltf::Material gltf_material : mGltfModel.materials) { - gltf_render_material mat{ 0 }; + gltf_render_material mat; mat.name = gltf_material.name; mat.normalScale = gltf_material.normalTexture.scale; + mat.hasNormalTex = gltf_material.normalTexture.index > 0; mat.normalTexIdx = gltf_material.normalTexture.index; mat.normalTexCoordIdx = gltf_material.normalTexture.texCoord; mat.occlusionScale = gltf_material.occlusionTexture.strength; + mat.hasOcclusionTex = gltf_material.occlusionTexture.index > 0; mat.occlusionTexIdx = gltf_material.occlusionTexture.index; mat.occlusionTexCoordIdx = gltf_material.occlusionTexture.texCoord; mat.emissiveColor.set(gltf_material.emissiveFactor.data()); + mat.hasEmissiveTex = gltf_material.emissiveTexture.index > 0; mat.emissiveColorTexIdx = gltf_material.emissiveTexture.index; mat.emissiveColorTexCoordIdx = gltf_material.emissiveTexture.texCoord; @@ -288,29 +291,31 @@ bool LLGLTFLoader::parseMaterials() mat.hasPBR = true; mat.pbr.baseColor.set(pbr.baseColorFactor.data()); + mat.pbr.hasBaseTex = pbr.baseColorTexture.index > 0; mat.pbr.baseColorTexIdx = pbr.baseColorTexture.index; mat.pbr.baseColorTexCoordIdx = pbr.baseColorTexture.texCoord; mat.pbr.metalness = pbr.metallicFactor; mat.pbr.roughness = pbr.roughnessFactor; + mat.pbr.hasMRTex = pbr.metallicRoughnessTexture.index > 0; mat.pbr.metalRoughTexIdx = pbr.metallicRoughnessTexture.index; mat.pbr.metalRoughTexCoordIdx = pbr.metallicRoughnessTexture.texCoord; - if (mat.normalTexIdx >= mTextures.size() || - mat.occlusionTexIdx >= mTextures.size() || - mat.emissiveColorTexIdx >= mTextures.size() || - mat.pbr.baseColorTexIdx >= mTextures.size() || - mat.pbr.metalRoughTexIdx >= mTextures.size()) + if ((mat.hasNormalTex && (mat.normalTexIdx >= mTextures.size())) || + (mat.hasOcclusionTex && (mat.occlusionTexIdx >= mTextures.size())) || + (mat.hasEmissiveTex && (mat.emissiveColorTexIdx >= mTextures.size())) || + (mat.pbr.hasBaseTex && (mat.pbr.baseColorTexIdx >= mTextures.size())) || + (mat.pbr.hasMRTex && (mat.pbr.metalRoughTexIdx >= mTextures.size()))) { LL_WARNS("GLTF_IMPORT") << "Texture resource index error" << LL_ENDL; return false; } - if (mat.normalTexCoordIdx > 0 || // May have to loosen this condition - mat.occlusionTexCoordIdx > 0 || - mat.emissiveColorTexCoordIdx > 0 || - mat.pbr.baseColorTexCoordIdx > 0 || - mat.pbr.metalRoughTexCoordIdx > 0) + if ((mat.hasNormalTex && (mat.normalTexCoordIdx > 2)) || // mesh can have up to 3 sets of UV + (mat.hasOcclusionTex && (mat.occlusionTexCoordIdx > 2)) || + (mat.hasEmissiveTex && (mat.emissiveColorTexCoordIdx > 2)) || + (mat.pbr.hasBaseTex && (mat.pbr.baseColorTexCoordIdx > 2)) || + (mat.pbr.hasMRTex && (mat.pbr.metalRoughTexCoordIdx > 2))) { LL_WARNS("GLTF_IMPORT") << "Image texcoord index error" << LL_ENDL; return false; @@ -331,6 +336,7 @@ void LLGLTFLoader::uploadMeshes() // TODO: convert raw index buffers to UUIDs void LLGLTFLoader::uploadMaterials() { - llassert(0); + //llassert(0); + } -- cgit v1.2.3 From c9ebb970ee916ace16e88508dc1a178336a91d52 Mon Sep 17 00:00:00 2001 From: Dave Houlton Date: Wed, 1 Jun 2022 14:37:20 -0600 Subject: SL-17214 re-work gltf data organization --- indra/llprimitive/llgltfloader.cpp | 138 +++++++++++++++++++++++++++---------- 1 file changed, 100 insertions(+), 38 deletions(-) (limited to 'indra/llprimitive/llgltfloader.cpp') diff --git a/indra/llprimitive/llgltfloader.cpp b/indra/llprimitive/llgltfloader.cpp index bc9c4760f7..3ec11f70c6 100644 --- a/indra/llprimitive/llgltfloader.cpp +++ b/indra/llprimitive/llgltfloader.cpp @@ -45,6 +45,9 @@ // TINYGLTF_NO_INCLUDE_STB_IMAGE_WRITE #include "tinygltf\tiny_gltf.h" + +// TODO: includes inherited from dae loader. Validate / prune + #include #include "llsdserialize.h" @@ -120,9 +123,9 @@ bool LLGLTFLoader::OpenFile(const std::string &filename) if (!mGltfLoaded) { if (!warn_msg.empty()) - LL_WARNS() << "gltf load warning: " << warn_msg.c_str() << LL_ENDL; + LL_WARNS("GLTF_IMPORT") << "gltf load warning: " << warn_msg.c_str() << LL_ENDL; if (!error_msg.empty()) - LL_WARNS() << "gltf load error: " << error_msg.c_str() << LL_ENDL; + LL_WARNS("GLTF_IMPORT") << "gltf load error: " << error_msg.c_str() << LL_ENDL; return false; } @@ -251,10 +254,11 @@ bool LLGLTFLoader::parseMaterials() for (auto in_tex : mGltfModel.textures) { gltf_texture tex; - tex.image_idx = in_tex.source; - tex.sampler_idx = in_tex.sampler; + tex.imageIdx = in_tex.source; + tex.samplerIdx = in_tex.sampler; + tex.imageUuid.setNull(); - if (tex.image_idx >= mImages.size() || tex.sampler_idx >= mSamplers.size()) + if (tex.imageIdx >= mImages.size() || tex.samplerIdx >= mSamplers.size()) { LL_WARNS("GLTF_IMPORT") << "Texture sampler/image index error" << LL_ENDL; return false; @@ -269,53 +273,53 @@ bool LLGLTFLoader::parseMaterials() gltf_render_material mat; mat.name = gltf_material.name; + tinygltf::PbrMetallicRoughness& pbr = gltf_material.pbrMetallicRoughness; + mat.hasPBR = true; // Always true, for now + + mat.baseColor.set(pbr.baseColorFactor.data()); + mat.hasBaseTex = pbr.baseColorTexture.index >= 0; + mat.baseColorTexIdx = pbr.baseColorTexture.index; + mat.baseColorTexCoords = pbr.baseColorTexture.texCoord; + + mat.metalness = pbr.metallicFactor; + mat.roughness = pbr.roughnessFactor; + mat.hasMRTex = pbr.metallicRoughnessTexture.index >= 0; + mat.metalRoughTexIdx = pbr.metallicRoughnessTexture.index; + mat.metalRoughTexCoords = pbr.metallicRoughnessTexture.texCoord; + mat.normalScale = gltf_material.normalTexture.scale; - mat.hasNormalTex = gltf_material.normalTexture.index > 0; + mat.hasNormalTex = gltf_material.normalTexture.index >= 0; mat.normalTexIdx = gltf_material.normalTexture.index; - mat.normalTexCoordIdx = gltf_material.normalTexture.texCoord; + mat.normalTexCoords = gltf_material.normalTexture.texCoord; mat.occlusionScale = gltf_material.occlusionTexture.strength; - mat.hasOcclusionTex = gltf_material.occlusionTexture.index > 0; + mat.hasOcclusionTex = gltf_material.occlusionTexture.index >= 0; mat.occlusionTexIdx = gltf_material.occlusionTexture.index; - mat.occlusionTexCoordIdx = gltf_material.occlusionTexture.texCoord; + mat.occlusionTexCoords = gltf_material.occlusionTexture.texCoord; mat.emissiveColor.set(gltf_material.emissiveFactor.data()); - mat.hasEmissiveTex = gltf_material.emissiveTexture.index > 0; - mat.emissiveColorTexIdx = gltf_material.emissiveTexture.index; - mat.emissiveColorTexCoordIdx = gltf_material.emissiveTexture.texCoord; + mat.hasEmissiveTex = gltf_material.emissiveTexture.index >= 0; + mat.emissiveTexIdx = gltf_material.emissiveTexture.index; + mat.emissiveTexCoords = gltf_material.emissiveTexture.texCoord; mat.alphaMode = gltf_material.alphaMode; mat.alphaMask = gltf_material.alphaCutoff; - tinygltf::PbrMetallicRoughness& pbr = gltf_material.pbrMetallicRoughness; - mat.hasPBR = true; - - mat.pbr.baseColor.set(pbr.baseColorFactor.data()); - mat.pbr.hasBaseTex = pbr.baseColorTexture.index > 0; - mat.pbr.baseColorTexIdx = pbr.baseColorTexture.index; - mat.pbr.baseColorTexCoordIdx = pbr.baseColorTexture.texCoord; - - mat.pbr.metalness = pbr.metallicFactor; - mat.pbr.roughness = pbr.roughnessFactor; - mat.pbr.hasMRTex = pbr.metallicRoughnessTexture.index > 0; - mat.pbr.metalRoughTexIdx = pbr.metallicRoughnessTexture.index; - mat.pbr.metalRoughTexCoordIdx = pbr.metallicRoughnessTexture.texCoord; - - if ((mat.hasNormalTex && (mat.normalTexIdx >= mTextures.size())) || - (mat.hasOcclusionTex && (mat.occlusionTexIdx >= mTextures.size())) || - (mat.hasEmissiveTex && (mat.emissiveColorTexIdx >= mTextures.size())) || - (mat.pbr.hasBaseTex && (mat.pbr.baseColorTexIdx >= mTextures.size())) || - (mat.pbr.hasMRTex && (mat.pbr.metalRoughTexIdx >= mTextures.size()))) + if ((mat.hasNormalTex && (mat.normalTexIdx >= mTextures.size())) || + (mat.hasOcclusionTex && (mat.occlusionTexIdx >= mTextures.size())) || + (mat.hasEmissiveTex && (mat.emissiveTexIdx >= mTextures.size())) || + (mat.hasBaseTex && (mat.baseColorTexIdx >= mTextures.size())) || + (mat.hasMRTex && (mat.metalRoughTexIdx >= mTextures.size()))) { LL_WARNS("GLTF_IMPORT") << "Texture resource index error" << LL_ENDL; return false; } - if ((mat.hasNormalTex && (mat.normalTexCoordIdx > 2)) || // mesh can have up to 3 sets of UV - (mat.hasOcclusionTex && (mat.occlusionTexCoordIdx > 2)) || - (mat.hasEmissiveTex && (mat.emissiveColorTexCoordIdx > 2)) || - (mat.pbr.hasBaseTex && (mat.pbr.baseColorTexCoordIdx > 2)) || - (mat.pbr.hasMRTex && (mat.pbr.metalRoughTexCoordIdx > 2))) + if ((mat.hasNormalTex && (mat.normalTexCoords > 2)) || // mesh can have up to 3 sets of UV + (mat.hasOcclusionTex && (mat.occlusionTexCoords > 2)) || + (mat.hasEmissiveTex && (mat.emissiveTexCoords > 2)) || + (mat.hasBaseTex && (mat.baseColorTexCoords > 2)) || + (mat.hasMRTex && (mat.metalRoughTexCoords > 2))) { LL_WARNS("GLTF_IMPORT") << "Image texcoord index error" << LL_ENDL; return false; @@ -333,10 +337,68 @@ void LLGLTFLoader::uploadMeshes() llassert(0); } -// TODO: convert raw index buffers to UUIDs +// convert raw image buffers to texture UUIDs & assemble into a render material void LLGLTFLoader::uploadMaterials() { - //llassert(0); + for (gltf_render_material mat : mMaterials) // Initially 1 material per gltf file, but design for multiple + { + if (mat.hasBaseTex) + { + gltf_texture& gtex = mTextures[mat.baseColorTexIdx]; + if (gtex.imageUuid.isNull()) + { + gtex.imageUuid = imageBufferToTextureUUID(gtex); + } + } + + if (mat.hasMRTex) + { + gltf_texture& gtex = mTextures[mat.metalRoughTexIdx]; + if (gtex.imageUuid.isNull()) + { + gtex.imageUuid = imageBufferToTextureUUID(gtex); + } + } + + if (mat.hasNormalTex) + { + gltf_texture& gtex = mTextures[mat.normalTexIdx]; + if (gtex.imageUuid.isNull()) + { + gtex.imageUuid = imageBufferToTextureUUID(gtex); + } + } + if (mat.hasOcclusionTex) + { + gltf_texture& gtex = mTextures[mat.occlusionTexIdx]; + if (gtex.imageUuid.isNull()) + { + gtex.imageUuid = imageBufferToTextureUUID(gtex); + } + } + + if (mat.hasEmissiveTex) + { + gltf_texture& gtex = mTextures[mat.emissiveTexIdx]; + if (gtex.imageUuid.isNull()) + { + gtex.imageUuid = imageBufferToTextureUUID(gtex); + } + } + } } +LLUUID LLGLTFLoader::imageBufferToTextureUUID(const gltf_texture& tex) +{ + //gltf_image& image = mImages[tex.imageIdx]; + //gltf_sampler& sampler = mSamplers[tex.samplerIdx]; + + // fill an LLSD container with image+sampler data + + // upload texture + + // retrieve UUID + + return LLUUID::null; +} -- cgit v1.2.3