Browse Source

Convert double to float in gltf model and json util

master
Riyyi 3 years ago
parent
commit
aa9403dde0
  1. 18
      inferno/src/inferno/render/gltf.cpp
  2. 16
      inferno/src/inferno/render/gltf.h
  3. 48
      inferno/src/inferno/util/json.h

18
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);

16
inferno/src/inferno/render/gltf.h

@ -35,12 +35,12 @@ namespace Inferno {
uint32_t camera;
std::vector<uint32_t> children;
uint32_t skin;
std::array<double, 16> matrix { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }; // Identity matrix
std::array<float, 16> matrix { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }; // Identity matrix
uint32_t mesh;
std::array<double, 4> rotation { 0, 0, 0, 1 };
std::array<double, 3> scale { 1, 1, 1 };
std::array<double, 3> translation { 0, 0, 0 };
std::vector<double> weights;
std::array<float, 4> rotation { 0, 0, 0, 1 };
std::array<float, 3> scale { 1, 1, 1 };
std::array<float, 3> translation { 0, 0, 0 };
std::vector<float> 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<Primitive> primitives; // Required
std::vector<double> weights;
std::vector<float> weights;
std::string name;
};
@ -67,8 +67,8 @@ namespace Inferno {
bool normalized { false };
uint32_t count; // Required
std::string type; // Required
std::vector<double> max;
std::vector<double> min;
std::vector<float> max;
std::vector<float> min;
std::string name;
};

48
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<typename T>
@ -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<bool> parseBoolProperty(const json& json, const char* property, bool required)
@ -173,19 +175,25 @@ namespace Inferno {
return parseProperty<bool>(json, property, required, json::value_t::boolean);
}
static std::optional<double> parseDoubleProperty(const json& json, const char* property, bool required)
static std::optional<float> parseFloatProperty(const json& json, const char* property, bool required)
{
return parseProperty<double>(json, property, required, std::vector { json::value_t::number_integer, json::value_t::number_unsigned, json::value_t::number_float });
auto result = parseProperty<double>(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<float>(result.value());
return {};
}
static std::optional<int32_t> parseIntegerProperty(const json& json, const char* property, bool required)
{
return parseProperty<int32_t>(json, property, required, json::value_t::number_integer);
auto result = parseProperty<int64_t>(json, property, required, json::value_t::number_integer);
if (result) return static_cast<int32_t>(result.value());
return {};
}
static std::optional<uint32_t> parseUnsignedProperty(const json& json, const char* property, bool required)
{
return parseProperty<uint32_t>(json, property, required, json::value_t::number_unsigned);
auto result = parseProperty<uint64_t>(json, property, required, json::value_t::number_unsigned);
if (result) return static_cast<uint32_t>(result.value());
return {};
}
static std::optional<std::string> parseStringProperty(const json& json, const char* property, bool required)
@ -193,34 +201,46 @@ namespace Inferno {
return parseProperty<std::string>(json, property, required, json::value_t::string);
}
static std::optional<std::vector<double>> parseDoubleArrayProperty(const json& json, const char* property, bool required)
static std::optional<std::vector<float>> parseFloatArrayProperty(const json& json, const char* property, bool required)
{
return parseArrayProperty<double>(json, property, required, std::vector { json::value_t::number_integer, json::value_t::number_unsigned, json::value_t::number_float });
auto result = parseArrayProperty<double>(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<float>(result.value().begin(), result.value().end());
return {};
}
static std::optional<std::vector<int32_t>> parseIntegerArrayProperty(const json& json, const char* property, bool required)
{
return parseArrayProperty<int32_t>(json, property, required, json::value_t::number_integer);
auto result = parseArrayProperty<int64_t>(json, property, required, json::value_t::number_integer);
if (result) return std::vector<int32_t>(result.value().begin(), result.value().end());
return {};
}
static std::optional<std::vector<uint32_t>> parseUnsignedArrayProperty(const json& json, const char* property, bool required)
{
return parseArrayProperty<uint32_t>(json, property, required, json::value_t::number_unsigned);
auto result = parseArrayProperty<uint64_t>(json, property, required, json::value_t::number_unsigned);
if (result) return std::vector<uint32_t>(result.value().begin(), result.value().end());
return {};
}
static std::optional<std::map<std::string, double>> parseDoubleObjectProperty(const json& json, const char* property, bool required)
static std::optional<std::map<std::string, float>> parseFloatObjectProperty(const json& json, const char* property, bool required)
{
return parseObjectProperty<double>(json, property, required, std::vector { json::value_t::number_integer, json::value_t::number_unsigned, json::value_t::number_float });
auto result = parseObjectProperty<double>(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<std::string, float>(result.value().begin(), result.value().end());
return {};
}
static std::optional<std::map<std::string, int32_t>> parseIntegerObjectProperty(const json& json, const char* property, bool required)
{
return parseObjectProperty<int32_t>(json, property, required, json::value_t::number_integer);
auto result = parseObjectProperty<int64_t>(json, property, required, json::value_t::number_integer);
if (result) return std::map<std::string, int32_t>(result.value().begin(), result.value().end());
return {};
}
static std::optional<std::map<std::string, uint32_t>> parseUnsignedObjectProperty(const json& json, const char* property, bool required)
{
return parseObjectProperty<uint32_t>(json, property, required, json::value_t::number_unsigned);
auto result = parseObjectProperty<uint64_t>(json, property, required, json::value_t::number_unsigned);
if (result) return std::map<std::string, uint32_t>(result.value().begin(), result.value().end());
return {};
}
};

Loading…
Cancel
Save