From 5845319b1aca6c6a949a1ac1e4109c785fabfec9 Mon Sep 17 00:00:00 2001 From: Arthur Brainville Date: Mon, 8 Jan 2018 18:25:52 +0100 Subject: [PATCH 1/3] Add little utility methods to access the content of a Parameter object Signed-off-by: Arthur Brainville (Ybalrid) --- tiny_gltf.h | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/tiny_gltf.h b/tiny_gltf.h index 114211d..9a4d721 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -42,6 +42,7 @@ #include #include #include +#include namespace tinygltf { @@ -325,12 +326,48 @@ TINYGLTF_VALUE_GET(Value::Array, array_value_) TINYGLTF_VALUE_GET(Value::Object, object_value_) #undef TINYGLTF_VALUE_GET -typedef struct { +///Agregate object for representing a color +using ColorValue = std::array; + + struct Parameter { bool bool_value; std::string string_value; std::vector number_array; std::map json_double_value; -} Parameter; + + //context sensitive methods. depending the type of the Parameter you are accessing, these are either valid or not + //If this parameter represent a texture map in a material, will return the texture index + + ///Return the index of a texture if this Parameter is a texture map. + ///Returned value is only valid if the parameter represent a texture from a material + int TextureIndex() { + const auto it = json_double_value.find("index"); + if (it != std::end(json_double_value)) + { + return int(it->second); + } + return -1; + } + + ///Material factor, like the roughness or metalness of a material + ///Returned value is only valid if the parameter represent a texture from a material + double Factor() { + return number_array[0]; + } + + ///Return the color of a material + ///Returned value is only valid if the parameter represent a texture from a material + ColorValue Color() { + return { + { // this agregate intialize the std::array object, and uses C++11 RVO. + number_array[0], + number_array[1], + number_array[2], + (number_array.size() > 3 ? number_array[3] : 1.0) + } + }; + } +}; typedef std::map ParameterMap; From 41fe77205884009f63c5a38f924ad3a401936b47 Mon Sep 17 00:00:00 2001 From: Arthur Brainville Date: Mon, 8 Jan 2018 18:32:48 +0100 Subject: [PATCH 2/3] Const qualify const methods Signed-off-by: Arthur Brainville (Ybalrid) --- tiny_gltf.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tiny_gltf.h b/tiny_gltf.h index 9a4d721..2accbd0 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -340,7 +340,7 @@ using ColorValue = std::array; ///Return the index of a texture if this Parameter is a texture map. ///Returned value is only valid if the parameter represent a texture from a material - int TextureIndex() { + int TextureIndex() const { const auto it = json_double_value.find("index"); if (it != std::end(json_double_value)) { @@ -351,13 +351,13 @@ using ColorValue = std::array; ///Material factor, like the roughness or metalness of a material ///Returned value is only valid if the parameter represent a texture from a material - double Factor() { + double Factor() const { return number_array[0]; } ///Return the color of a material ///Returned value is only valid if the parameter represent a texture from a material - ColorValue Color() { + ColorValue Color() const { return { { // this agregate intialize the std::array object, and uses C++11 RVO. number_array[0], From 9585391311ce2138dda42e4aeabc1674c27f9aca Mon Sep 17 00:00:00 2001 From: Arthur Brainville Date: Mon, 8 Jan 2018 18:37:44 +0100 Subject: [PATCH 3/3] rename Color to ColorFactor Signed-off-by: Arthur Brainville (Ybalrid) --- tiny_gltf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tiny_gltf.h b/tiny_gltf.h index 2accbd0..bf316c4 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -357,7 +357,7 @@ using ColorValue = std::array; ///Return the color of a material ///Returned value is only valid if the parameter represent a texture from a material - ColorValue Color() const { + ColorValue ColorFactor() const { return { { // this agregate intialize the std::array object, and uses C++11 RVO. number_array[0],