From aa9403dde0bfd864ef004a9b24c5dfc6a8d2e6c0 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sun, 7 Feb 2021 16:48:38 +0100 Subject: [PATCH] Convert double to float in gltf model and json util --- inferno/src/inferno/render/gltf.cpp | 18 +++++------ inferno/src/inferno/render/gltf.h | 16 +++++----- inferno/src/inferno/util/json.h | 48 ++++++++++++++++++++--------- 3 files changed, 51 insertions(+), 31 deletions(-) diff --git a/inferno/src/inferno/render/gltf.cpp b/inferno/src/inferno/render/gltf.cpp index 30411e4..85d41df 100644 --- a/inferno/src/inferno/render/gltf.cpp +++ b/inferno/src/inferno/render/gltf.cpp @@ -143,7 +143,7 @@ namespace Inferno { void Gltf::parseScene(glTF::Scene* scene, const std::string& key, const json& object) { - auto nodes = Json::parseDoubleArrayProperty(object, "nodes", false); + auto nodes = Json::parseFloatArrayProperty(object, "nodes", false); ASSERT(!nodes || nodes.value().size() > 0, "Gltf scene '{}' empty 'nodes' property", key); auto name = Json::parseStringProperty(object, "name", false); @@ -160,21 +160,21 @@ namespace Inferno { auto skin = Json::parseUnsignedProperty(object, "skin", false); - auto matrix = Json::parseDoubleArrayProperty(object, "matrix", false); + auto matrix = Json::parseFloatArrayProperty(object, "matrix", false); ASSERT(!matrix || matrix.value().size() == 16, "Gltf node '{}' property 'matrix' invalid size", key); auto mesh = Json::parseUnsignedProperty(object, "mesh", false); - auto rotation = Json::parseDoubleArrayProperty(object, "rotation", false); + auto rotation = Json::parseFloatArrayProperty(object, "rotation", false); ASSERT(!rotation || rotation.value().size() == 4, "Gltf node '{}' property 'rotation' invalid size", key); - auto scale = Json::parseDoubleArrayProperty(object, "scale", false); + auto scale = Json::parseFloatArrayProperty(object, "scale", false); ASSERT(!scale || scale.value().size() == 3, "Gltf node '{}' property 'scale' invalid size", key); - auto translation = Json::parseDoubleArrayProperty(object, "translation", false); + auto translation = Json::parseFloatArrayProperty(object, "translation", false); ASSERT(!translation || translation.value().size() == 3, "Gltf node '{}' property 'translation' invalid size", key); - auto weights = Json::parseDoubleArrayProperty(object, "weights", false); + auto weights = Json::parseFloatArrayProperty(object, "weights", false); ASSERT(!weights || weights.value().size() > 0, "Gltf node '{}' empty property 'weights'", key); auto name = Json::parseStringProperty(object, "name", false); @@ -239,7 +239,7 @@ namespace Inferno { mesh->primitives.emplace_back(std::move(primitive)); } - auto weights = Json::parseDoubleArrayProperty(object, "weights", false); + auto weights = Json::parseFloatArrayProperty(object, "weights", false); ASSERT(!weights || weights.value().size() > 0, "Gltf mesh '{}' empty property 'weights'", key); auto name = Json::parseStringProperty(object, "name", false); @@ -265,10 +265,10 @@ namespace Inferno { auto type = Json::parseStringProperty(object, "type", true); ASSERT(type, "Gltf accessor '{}' missing required property 'type'", key); - auto max = Json::parseDoubleArrayProperty(object, "max", false); + auto max = Json::parseFloatArrayProperty(object, "max", false); ASSERT(!max || max.value().size() > 0, "Gltf accessor '{}' empty property 'max'", key); - auto min = Json::parseDoubleArrayProperty(object, "min", false); + auto min = Json::parseFloatArrayProperty(object, "min", false); ASSERT(!min || min.value().size() > 0, "Gltf accessor '{}' empty property 'min'", key); auto name = Json::parseStringProperty(object, "name", false); diff --git a/inferno/src/inferno/render/gltf.h b/inferno/src/inferno/render/gltf.h index ac56053..27d742e 100644 --- a/inferno/src/inferno/render/gltf.h +++ b/inferno/src/inferno/render/gltf.h @@ -35,12 +35,12 @@ namespace Inferno { uint32_t camera; std::vector children; uint32_t skin; - std::array matrix { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }; // Identity matrix + std::array matrix { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }; // Identity matrix uint32_t mesh; - std::array rotation { 0, 0, 0, 1 }; - std::array scale { 1, 1, 1 }; - std::array translation { 0, 0, 0 }; - std::vector weights; + std::array rotation { 0, 0, 0, 1 }; + std::array scale { 1, 1, 1 }; + std::array translation { 0, 0, 0 }; + std::vector weights; std::string name; }; @@ -55,7 +55,7 @@ namespace Inferno { // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#meshes struct Mesh { std::vector primitives; // Required - std::vector weights; + std::vector weights; std::string name; }; @@ -67,8 +67,8 @@ namespace Inferno { bool normalized { false }; uint32_t count; // Required std::string type; // Required - std::vector max; - std::vector min; + std::vector max; + std::vector min; std::string name; }; diff --git a/inferno/src/inferno/util/json.h b/inferno/src/inferno/util/json.h index 3842b23..f122042 100644 --- a/inferno/src/inferno/util/json.h +++ b/inferno/src/inferno/util/json.h @@ -127,7 +127,8 @@ namespace Inferno { } // Return vector if it has any items, uninitialized optional otherwise - return values.size() > 0 ? std::optional { values } : std::nullopt; + if (values.size() > 0) return values; + return {}; } template @@ -165,7 +166,8 @@ namespace Inferno { } // Return map if it has any items, uninitialized optional otherwise - return values.size() > 0 ? std::optional { values } : std::nullopt; + if (values.size() > 0) return values; + return {}; } static std::optional parseBoolProperty(const json& json, const char* property, bool required) @@ -173,19 +175,25 @@ namespace Inferno { return parseProperty(json, property, required, json::value_t::boolean); } - static std::optional parseDoubleProperty(const json& json, const char* property, bool required) + static std::optional parseFloatProperty(const json& json, const char* property, bool required) { - return parseProperty(json, property, required, std::vector { json::value_t::number_integer, json::value_t::number_unsigned, json::value_t::number_float }); + auto result = parseProperty(json, property, required, std::vector { json::value_t::number_integer, json::value_t::number_unsigned, json::value_t::number_float }); + if (result) return static_cast(result.value()); + return {}; } static std::optional parseIntegerProperty(const json& json, const char* property, bool required) { - return parseProperty(json, property, required, json::value_t::number_integer); + auto result = parseProperty(json, property, required, json::value_t::number_integer); + if (result) return static_cast(result.value()); + return {}; } static std::optional parseUnsignedProperty(const json& json, const char* property, bool required) { - return parseProperty(json, property, required, json::value_t::number_unsigned); + auto result = parseProperty(json, property, required, json::value_t::number_unsigned); + if (result) return static_cast(result.value()); + return {}; } static std::optional parseStringProperty(const json& json, const char* property, bool required) @@ -193,34 +201,46 @@ namespace Inferno { return parseProperty(json, property, required, json::value_t::string); } - static std::optional> parseDoubleArrayProperty(const json& json, const char* property, bool required) + static std::optional> parseFloatArrayProperty(const json& json, const char* property, bool required) { - return parseArrayProperty(json, property, required, std::vector { json::value_t::number_integer, json::value_t::number_unsigned, json::value_t::number_float }); + auto result = parseArrayProperty(json, property, required, std::vector { json::value_t::number_integer, json::value_t::number_unsigned, json::value_t::number_float }); + if (result) return std::vector(result.value().begin(), result.value().end()); + return {}; } static std::optional> parseIntegerArrayProperty(const json& json, const char* property, bool required) { - return parseArrayProperty(json, property, required, json::value_t::number_integer); + auto result = parseArrayProperty(json, property, required, json::value_t::number_integer); + if (result) return std::vector(result.value().begin(), result.value().end()); + return {}; } static std::optional> parseUnsignedArrayProperty(const json& json, const char* property, bool required) { - return parseArrayProperty(json, property, required, json::value_t::number_unsigned); + auto result = parseArrayProperty(json, property, required, json::value_t::number_unsigned); + if (result) return std::vector(result.value().begin(), result.value().end()); + return {}; } - static std::optional> parseDoubleObjectProperty(const json& json, const char* property, bool required) + static std::optional> parseFloatObjectProperty(const json& json, const char* property, bool required) { - return parseObjectProperty(json, property, required, std::vector { json::value_t::number_integer, json::value_t::number_unsigned, json::value_t::number_float }); + auto result = parseObjectProperty(json, property, required, std::vector { json::value_t::number_integer, json::value_t::number_unsigned, json::value_t::number_float }); + if (result) return std::map(result.value().begin(), result.value().end()); + return {}; } static std::optional> parseIntegerObjectProperty(const json& json, const char* property, bool required) { - return parseObjectProperty(json, property, required, json::value_t::number_integer); + auto result = parseObjectProperty(json, property, required, json::value_t::number_integer); + if (result) return std::map(result.value().begin(), result.value().end()); + return {}; } static std::optional> parseUnsignedObjectProperty(const json& json, const char* property, bool required) { - return parseObjectProperty(json, property, required, json::value_t::number_unsigned); + auto result = parseObjectProperty(json, property, required, json::value_t::number_unsigned); + if (result) return std::map(result.value().begin(), result.value().end()); + return {}; } };