From a18ad02b33480f8d0168e069438738d249bd8484 Mon Sep 17 00:00:00 2001
From: Trevor Hollmann <thollmann@uni-koblenz.de>
Date: Mon, 12 Jul 2021 11:20:46 +0200
Subject: [PATCH] [#79] Make hasTexture() a method of Material.

---
 .../include/vkcv/asset/asset_loader.hpp       | 49 +++++++++----------
 .../src/vkcv/asset/asset_loader.cpp           |  5 +-
 2 files changed, 26 insertions(+), 28 deletions(-)

diff --git a/modules/asset_loader/include/vkcv/asset/asset_loader.hpp b/modules/asset_loader/include/vkcv/asset/asset_loader.hpp
index 775e90d5..c194dcfb 100644
--- a/modules/asset_loader/include/vkcv/asset/asset_loader.hpp
+++ b/modules/asset_loader/include/vkcv/asset/asset_loader.hpp
@@ -84,22 +84,6 @@ typedef struct {
 	std::vector<uint8_t> data;	// binary data of the decoded texture
 } Texture;
 
-/**
- * The asset loader module only supports the PBR-MetallicRoughness model for
- * materials.
- */
-typedef struct {
-	uint16_t textureMask;	// bit mask with active texture targets
-	// Indices into the Scene.textures vector
-	int baseColor, metalRough, normal, occlusion, emissive;
-	// Scaling factors for each texture target
-	struct { float r, g, b, a; } baseColorFactor;
-	float metallicFactor, roughnessFactor;
-	float normalScale;
-	float occlusionStrength;
-	struct { float r, g, b; } emissiveFactor;
-} Material;
-
 /**
  * Flags for the bit-mask in the Material struct. To check if a material has a
  * certain texture target, you can use the hasTexture() function below, passing
@@ -122,16 +106,31 @@ enum class PBRTextureTarget {
 #define bitflag(ENUM) (0x1u << ((unsigned)(ENUM)))
 
 /**
- * To signal that a certain texture target is active in a Material struct, its
- * bit is set in the textureMask. You can use this function to check that:
- * Material mat = ...;
- * if (materialHasTexture(&mat, baseColor)) {...}
- * @param m The material to query
- * @param t The target to query for
- * @return Boolean to signal whether the texture target is active in the
- * material.
+ * The asset loader module only supports the PBR-MetallicRoughness model for
+ * materials.
  */
-bool materialHasTexture(const Material *const m, const PBRTextureTarget t);
+typedef struct {
+	uint16_t textureMask;	// bit mask with active texture targets
+	// Indices into the Scene.textures vector
+	int baseColor, metalRough, normal, occlusion, emissive;
+	// Scaling factors for each texture target
+	struct { float r, g, b, a; } baseColorFactor;
+	float metallicFactor, roughnessFactor;
+	float normalScale;
+	float occlusionStrength;
+	struct { float r, g, b; } emissiveFactor;
+
+	/**
+	 * To signal that a certain texture target is active in this Material
+	 * struct, its bit is set in the textureMask. You can use this function
+	 * to check that:
+	 * if (myMaterial.hasTexture(baseColor)) {...}
+	 * @param t The target to query for
+	 * @return Boolean to signal whether the texture target is active in
+	 * the material.
+	 */
+	bool hasTexture(const PBRTextureTarget t) const;
+} Material;
 
 /* With these enums, 0 is reserved to signal uninitialized or invalid data. */
 enum class PrimitiveType : uint32_t {
diff --git a/modules/asset_loader/src/vkcv/asset/asset_loader.cpp b/modules/asset_loader/src/vkcv/asset/asset_loader.cpp
index 8ab97dab..bffde8f0 100644
--- a/modules/asset_loader/src/vkcv/asset/asset_loader.cpp
+++ b/modules/asset_loader/src/vkcv/asset/asset_loader.cpp
@@ -246,10 +246,9 @@ std::array<float, 16> computeModelMatrix(std::array<float, 3> translation, std::
 
 }
 
-/* TODO Should probably be a member function of vkcv::asset::Material */
-bool materialHasTexture(const Material *const m, const PBRTextureTarget t)
+bool Material::hasTexture(const PBRTextureTarget t) const
 {
-	return m->textureMask & bitflag(t);
+	return textureMask & bitflag(t);
 }
 
  /**
-- 
GitLab