From 40b590aca6fcc7693614eb5726710fad97f7ae04 Mon Sep 17 00:00:00 2001
From: Lars Hoerttrich <larshoerttrich@uni-koblenz.de>
Date: Sun, 20 Jun 2021 15:35:37 +0200
Subject: [PATCH] [#57] Creation of default-textures

---
 include/vkcv/Core.hpp                         |  5 +++
 modules/CMakeLists.txt                        |  1 +
 .../vkcv/material/pbrMetallicRoughness.cpp    | 45 ++++++++++++++-----
 src/vkcv/Core.cpp                             | 10 +++++
 4 files changed, 51 insertions(+), 10 deletions(-)

diff --git a/include/vkcv/Core.hpp b/include/vkcv/Core.hpp
index bf9514d5..b97c707d 100644
--- a/include/vkcv/Core.hpp
+++ b/include/vkcv/Core.hpp
@@ -222,6 +222,11 @@ namespace vkcv
         [[nodiscard]]
         Image createImage(vk::Format format, uint32_t width, uint32_t height, uint32_t depth = 1);
 
+        [[nodiscard]]
+        const uint32_t getImageWidth(ImageHandle imageHandle);
+        [[nodiscard]]
+        const uint32_t getImageHeight(ImageHandle imageHandle);
+
         /** TODO:
          *   @param setDescriptions
          *   @return
diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt
index 5edb802b..28b2184b 100644
--- a/modules/CMakeLists.txt
+++ b/modules/CMakeLists.txt
@@ -1,6 +1,7 @@
 
 # Add new modules here:
 add_subdirectory(asset_loader)
+add_subdirectory(material)
 add_subdirectory(camera)
 add_subdirectory(gui)
 add_subdirectory(shader_compiler)
diff --git a/modules/material/src/vkcv/material/pbrMetallicRoughness.cpp b/modules/material/src/vkcv/material/pbrMetallicRoughness.cpp
index 03470236..98a30f99 100644
--- a/modules/material/src/vkcv/material/pbrMetallicRoughness.cpp
+++ b/modules/material/src/vkcv/material/pbrMetallicRoughness.cpp
@@ -40,16 +40,41 @@ namespace vkcv::material
         SamplerHandle& metRoughSmp)
     {
         //Test if Images and samplers valid, if not use default
-        if (colorImg) {
-            //TODO
+        uint32_t width  = core->getImageWidth(colorImg); //use colorImg size as default
+        uint32_t height = core->getImageHeight(colorImg);
+        uint32_t n = width * height;
+        struct vec3 {
+            float x, y, z;
+        };
+        struct vec4 {
+            float x, y, z, a;
+        }; 
+
+        if (!colorImg) {
+            width = core->getImageWidth(metRoughImg); // if colorImg has no size
+            height = core->getImageHeight(metRoughImg);
+            n = width * height;
+            vkcv::Image defaultColor = core->createImage(vk::Format::eR8G8B8A8Srgb, width, height);
+            std::vector<vec4> colorData(n);
+            std::fill(colorData.begin(), colorData.end(), vec4{ 228, 51 , 255, 1 });
+            defaultColor.fill(colorData.data());
+            colorImg = defaultColor.getHandle();
         }
-        if (normalImg) {
-            //TODO
+        if (!normalImg || (core->getImageWidth(normalImg)!=width)|| (core->getImageHeight(normalImg) != height)) {
+            vkcv::Image defaultNormal = core->createImage(vk::Format::eR8G8B8A8Srgb, width, height);
+            std::vector<vec4> normalData(n);
+            std::fill(normalData.begin(), normalData.end(), vec4{ 228, 51 , 255, 1 });
+            defaultNormal.fill(normalData.data());
+            normalImg = defaultNormal.getHandle();
         }
-        if (metRoughImg) {
-            //TODO
+        if (!metRoughImg || (core->getImageWidth(metRoughImg) != width) || (core->getImageHeight(metRoughImg) != height)) {
+            vkcv::Image defaultRough = core->createImage(vk::Format::eR8G8B8A8Srgb, width, height);
+            std::vector<vec4> roughData(n);
+            std::fill(roughData.begin(), roughData.end(), vec4{ 228, 51 , 255, 1 });
+            defaultRough.fill(roughData.data());
+            metRoughImg = defaultRough.getHandle();
         }
-        if (colorSmp) {            
+        if (!colorSmp) {            
             colorSmp = core->createSampler(
                 vkcv::SamplerFilterType::LINEAR,
                 vkcv::SamplerFilterType::LINEAR,
@@ -57,7 +82,7 @@ namespace vkcv::material
                 vkcv::SamplerAddressMode::REPEAT
             );            
         }
-        if (normalSmp) {            
+        if (!normalSmp) {            
             normalSmp = core->createSampler(
                 vkcv::SamplerFilterType::LINEAR,
                 vkcv::SamplerFilterType::LINEAR,
@@ -65,7 +90,7 @@ namespace vkcv::material
                 vkcv::SamplerAddressMode::REPEAT
             );            
         }
-        if (metRoughSmp) {
+        if (!metRoughSmp) {
             metRoughSmp = core->createSampler(
                 vkcv::SamplerFilterType::LINEAR,
                 vkcv::SamplerFilterType::LINEAR,
@@ -87,7 +112,7 @@ namespace vkcv::material
             vkcv::SamplerDescriptorWrite(1, colorSmp),
             vkcv::SamplerDescriptorWrite(3, normalSmp),
             vkcv::SamplerDescriptorWrite(5, metRoughSmp) };
-        core->writeResourceDescription(descriptorSetHandle, 0, setWrites);
+        core->writeDescriptorSet(descriptorSetHandle, setWrites);
 
         return PBRMaterial(colorImg, colorSmp, normalImg, normalSmp, metRoughImg, metRoughSmp, descriptorSetHandle);        
     }
diff --git a/src/vkcv/Core.cpp b/src/vkcv/Core.cpp
index 49707d4c..3d8e6188 100644
--- a/src/vkcv/Core.cpp
+++ b/src/vkcv/Core.cpp
@@ -456,6 +456,16 @@ namespace vkcv
     	return Image::create(m_ImageManager.get(), format, width, height, depth);
 	}
 
+	const uint32_t Core::getImageWidth(ImageHandle imageHandle)
+	{
+		return m_ImageManager->getImageWidth(imageHandle);
+	}
+
+	const uint32_t Core::getImageHeight(ImageHandle imageHandle)
+	{
+		return m_ImageManager->getImageHeight(imageHandle);
+	}
+
     DescriptorSetHandle Core::createDescriptorSet(const std::vector<DescriptorBinding>& bindings)
     {
         return m_DescriptorManager->createDescriptorSet(bindings);
-- 
GitLab