From cb8ee6a962d22771a7e67221aa0f56e1431d0c69 Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Mon, 30 Sep 2024 16:53:28 +0200 Subject: [PATCH] can read uv coords --- include/vulkan/vertex.hpp | 8 +++++--- shaders/normal.comp | 1 + shaders/pbd.comp | 1 + shaders/shader.frag | 7 +++++-- shaders/shader.vert | 11 +++++++---- src/mesh.cpp | 13 +++++++++---- src/soft_body.cpp | 22 +++++++++++++--------- src/vulkan/image.cpp | 1 - src/vulkan/vertex.cpp | 11 ++++++++--- 9 files changed, 49 insertions(+), 26 deletions(-) diff --git a/include/vulkan/vertex.hpp b/include/vulkan/vertex.hpp index 21a591d..dba3086 100644 --- a/include/vulkan/vertex.hpp +++ b/include/vulkan/vertex.hpp @@ -2,17 +2,19 @@ #include #include -#include +#include +#include struct Vertex { alignas(16) glm::vec3 position; alignas(16) glm::vec3 color; + alignas(16) glm::vec2 uv; alignas(16) glm::vec3 normal; alignas(16) glm::vec3 velocity; alignas(16) glm::vec3 prevPosition; - float inverseMass; + alignas(4) float inverseMass; static VkVertexInputBindingDescription getBindingDescription(); - static std::array getAttributeDescriptions(); + static std::vector getAttributeDescriptions(); }; \ No newline at end of file diff --git a/shaders/normal.comp b/shaders/normal.comp index 17aea95..4b580e1 100644 --- a/shaders/normal.comp +++ b/shaders/normal.comp @@ -5,6 +5,7 @@ layout (local_size_x = 32) in; struct Vertex { vec3 position; vec3 color; + vec2 uv; uvec3 normal; vec3 velocity; vec3 prevPosition; diff --git a/shaders/pbd.comp b/shaders/pbd.comp index 35e858c..543f8ae 100644 --- a/shaders/pbd.comp +++ b/shaders/pbd.comp @@ -5,6 +5,7 @@ layout (local_size_x = 32) in; struct Vertex { vec3 position; vec3 color; + vec2 uv; vec3 normal; vec3 velocity; vec3 prevPosition; diff --git a/shaders/shader.frag b/shaders/shader.frag index 84c14e8..e6776be 100644 --- a/shaders/shader.frag +++ b/shaders/shader.frag @@ -1,9 +1,12 @@ #version 450 layout (location = 0) out vec4 outColor; -layout (location = 0) in vec3 fragColor; -layout (location = 1) in vec3 normal; + +layout (location = 0) in vec3 color; +layout (location = 1) in vec2 uv; +layout (location = 2) in vec3 normal; void main() { outColor = vec4((normal + vec3(1, 1, 1)) / 2, 1.0); + outColor = vec4(uv.x, uv.y, 0.0, 1.0); } \ No newline at end of file diff --git a/shaders/shader.vert b/shaders/shader.vert index 12bebbd..17746dd 100644 --- a/shaders/shader.vert +++ b/shaders/shader.vert @@ -2,10 +2,12 @@ layout (location = 0) in vec3 inPosition; layout (location = 1) in vec3 inColor; -layout (location = 2) in vec3 inNormal; +layout (location = 2) in vec2 inUV; +layout (location = 3) in vec3 inNormal; -layout (location = 0) out vec3 fragColor; -layout (location = 1) out vec3 normal; +layout (location = 0) out vec3 color; +layout (location = 1) out vec2 uv; +layout (location = 2) out vec3 normal; layout (set = 0, binding = 0) uniform UniformBufferObject { mat4 model; @@ -15,6 +17,7 @@ layout (set = 0, binding = 0) uniform UniformBufferObject { void main() { gl_Position = ubo.projection * ubo.view * ubo.model * vec4(inPosition, 1.0); - fragColor = inColor; + color = inColor; normal = inNormal; + uv = inUV; } \ No newline at end of file diff --git a/src/mesh.cpp b/src/mesh.cpp index 2801d49..0ce3062 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -12,10 +12,15 @@ Mesh::Mesh(const std::string &fileName) { auto mesh = scene->mMeshes[0]; for (size_t i = 0; i < mesh->mNumVertices; i++){ - vertices.push_back({ - *reinterpret_cast(&mesh->mVertices[i]), - glm::vec3(1, 0, 0) - }); + glm::vec3 pos = *reinterpret_cast(&mesh->mVertices[i]); + glm::vec3 color = {0, 0, 0}; + glm::vec2 uv = {0, 0}; + if (mesh->mNumUVComponents[0] == 2) + uv = *reinterpret_cast(&mesh->mTextureCoords[0][i]); + glm::vec3 normal = {0, 0, 0}; + if (mesh->mNormals != nullptr) + normal = *reinterpret_cast(&mesh->mNormals[i]); + vertices.push_back({pos, color, uv, normal}); } for (size_t i = 0; i < mesh->mNumFaces; i++){ diff --git a/src/soft_body.cpp b/src/soft_body.cpp index 4cad556..d251103 100644 --- a/src/soft_body.cpp +++ b/src/soft_body.cpp @@ -16,16 +16,18 @@ SoftBody::SoftBody(Mesh* mesh, float edgeCompliance, float triangleCompliance, f in.pointlist = new REAL[mesh->vertices.size() * 3]; in.numberoffacets = static_cast(mesh->faces.size()); in.facetlist = new tetgenio::facet[mesh->faces.size()]; - in.numberofpointattributes = 3; - in.pointattributelist = new REAL[mesh->vertices.size() * 3]; + in.numberofpointattributes = 5; // color, uv + in.pointattributelist = new REAL[mesh->vertices.size() * 5]; for (size_t i = 0; i < mesh->vertices.size(); i++){ in.pointlist[i * 3 + 0] = mesh->vertices[i].position.x; in.pointlist[i * 3 + 1] = mesh->vertices[i].position.y; in.pointlist[i * 3 + 2] = mesh->vertices[i].position.z; - in.pointattributelist[i * 3 + 0] = (mesh->vertices[i].position.x + 1) / 2; - in.pointattributelist[i * 3 + 1] = (mesh->vertices[i].position.y + 1) / 2; - in.pointattributelist[i * 3 + 2] = (mesh->vertices[i].position.z + 1) / 2; + in.pointattributelist[i * 5 + 0] = mesh->vertices[i].color.r; + in.pointattributelist[i * 5 + 1] = mesh->vertices[i].color.g; + in.pointattributelist[i * 5 + 2] = mesh->vertices[i].color.b; + in.pointattributelist[i * 5 + 3] = mesh->vertices[i].uv.s; + in.pointattributelist[i * 5 + 4] = mesh->vertices[i].uv.t; } for (size_t i = 0; i < mesh->faces.size(); i++){ @@ -53,10 +55,12 @@ SoftBody::SoftBody(Mesh* mesh, float edgeCompliance, float triangleCompliance, f float x = static_cast(out.pointlist[i * 3 + 0]); float y = static_cast(out.pointlist[i * 3 + 1]); float z = static_cast(out.pointlist[i * 3 + 2]); - float r = static_cast(out.pointattributelist[i * 3 + 0]); - float g = static_cast(out.pointattributelist[i * 3 + 1]); - float b = static_cast(out.pointattributelist[i * 3 + 2]); - vertices.emplace_back(Vertex({x, y, z}, {r, g, b})); + float r = static_cast(out.pointattributelist[i * 5 + 0]); + float g = static_cast(out.pointattributelist[i * 5 + 1]); + float b = static_cast(out.pointattributelist[i * 5 + 2]); + float s = static_cast(out.pointattributelist[i * 5 + 3]); + float t = static_cast(out.pointattributelist[i * 5 + 4]); + vertices.emplace_back(Vertex({x, y, z}, {r, g, b}, {s, t})); } faces.reserve(out.numberoftrifaces); diff --git a/src/vulkan/image.cpp b/src/vulkan/image.cpp index f902bc8..f3c83e1 100644 --- a/src/vulkan/image.cpp +++ b/src/vulkan/image.cpp @@ -1,4 +1,3 @@ -#include #include "vulkan/image.hpp" #include "vulkan/instance.hpp" diff --git a/src/vulkan/vertex.cpp b/src/vulkan/vertex.cpp index 5cb8869..b840521 100644 --- a/src/vulkan/vertex.cpp +++ b/src/vulkan/vertex.cpp @@ -8,8 +8,8 @@ VkVertexInputBindingDescription Vertex::getBindingDescription() { return bindingDescription; } -std::array Vertex::getAttributeDescriptions() { - std::array attributeDescriptions {}; +std::vector Vertex::getAttributeDescriptions() { + std::vector attributeDescriptions(4); attributeDescriptions[0].binding = 0; attributeDescriptions[0].location = 0; @@ -24,7 +24,12 @@ std::array Vertex::getAttributeDescription attributeDescriptions[2].binding = 0; attributeDescriptions[2].location = 2; attributeDescriptions[2].format = VK_FORMAT_R32G32B32_SFLOAT; - attributeDescriptions[2].offset = offsetof(Vertex, normal); + attributeDescriptions[2].offset = offsetof(Vertex, uv); + + attributeDescriptions[3].binding = 0; + attributeDescriptions[3].location = 3; + attributeDescriptions[3].format = VK_FORMAT_R32G32B32_SFLOAT; + attributeDescriptions[3].offset = offsetof(Vertex, normal); return attributeDescriptions; }