can read uv coords

feature/softbody-runtime-control
Benjamin Kraft 4 months ago
parent f9329cb84d
commit cb8ee6a962
  1. 8
      include/vulkan/vertex.hpp
  2. 1
      shaders/normal.comp
  3. 1
      shaders/pbd.comp
  4. 7
      shaders/shader.frag
  5. 11
      shaders/shader.vert
  6. 13
      src/mesh.cpp
  7. 22
      src/soft_body.cpp
  8. 1
      src/vulkan/image.cpp
  9. 11
      src/vulkan/vertex.cpp

@ -2,17 +2,19 @@
#include <glm/vec3.hpp> #include <glm/vec3.hpp>
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
#include <array> #include <vector>
#include <glm/vec2.hpp>
struct Vertex { struct Vertex {
alignas(16) glm::vec3 position; alignas(16) glm::vec3 position;
alignas(16) glm::vec3 color; alignas(16) glm::vec3 color;
alignas(16) glm::vec2 uv;
alignas(16) glm::vec3 normal; alignas(16) glm::vec3 normal;
alignas(16) glm::vec3 velocity; alignas(16) glm::vec3 velocity;
alignas(16) glm::vec3 prevPosition; alignas(16) glm::vec3 prevPosition;
float inverseMass; alignas(4) float inverseMass;
static VkVertexInputBindingDescription getBindingDescription(); static VkVertexInputBindingDescription getBindingDescription();
static std::array<VkVertexInputAttributeDescription, 3> getAttributeDescriptions(); static std::vector<VkVertexInputAttributeDescription> getAttributeDescriptions();
}; };

@ -5,6 +5,7 @@ layout (local_size_x = 32) in;
struct Vertex { struct Vertex {
vec3 position; vec3 position;
vec3 color; vec3 color;
vec2 uv;
uvec3 normal; uvec3 normal;
vec3 velocity; vec3 velocity;
vec3 prevPosition; vec3 prevPosition;

@ -5,6 +5,7 @@ layout (local_size_x = 32) in;
struct Vertex { struct Vertex {
vec3 position; vec3 position;
vec3 color; vec3 color;
vec2 uv;
vec3 normal; vec3 normal;
vec3 velocity; vec3 velocity;
vec3 prevPosition; vec3 prevPosition;

@ -1,9 +1,12 @@
#version 450 #version 450
layout (location = 0) out vec4 outColor; 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() { void main() {
outColor = vec4((normal + vec3(1, 1, 1)) / 2, 1.0); outColor = vec4((normal + vec3(1, 1, 1)) / 2, 1.0);
outColor = vec4(uv.x, uv.y, 0.0, 1.0);
} }

@ -2,10 +2,12 @@
layout (location = 0) in vec3 inPosition; layout (location = 0) in vec3 inPosition;
layout (location = 1) in vec3 inColor; 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 = 0) out vec3 color;
layout (location = 1) out vec3 normal; layout (location = 1) out vec2 uv;
layout (location = 2) out vec3 normal;
layout (set = 0, binding = 0) uniform UniformBufferObject { layout (set = 0, binding = 0) uniform UniformBufferObject {
mat4 model; mat4 model;
@ -15,6 +17,7 @@ layout (set = 0, binding = 0) uniform UniformBufferObject {
void main() { void main() {
gl_Position = ubo.projection * ubo.view * ubo.model * vec4(inPosition, 1.0); gl_Position = ubo.projection * ubo.view * ubo.model * vec4(inPosition, 1.0);
fragColor = inColor; color = inColor;
normal = inNormal; normal = inNormal;
uv = inUV;
} }

@ -12,10 +12,15 @@ Mesh::Mesh(const std::string &fileName) {
auto mesh = scene->mMeshes[0]; auto mesh = scene->mMeshes[0];
for (size_t i = 0; i < mesh->mNumVertices; i++){ for (size_t i = 0; i < mesh->mNumVertices; i++){
vertices.push_back({ glm::vec3 pos = *reinterpret_cast<glm::vec3*>(&mesh->mVertices[i]);
*reinterpret_cast<glm::vec3*>(&mesh->mVertices[i]), glm::vec3 color = {0, 0, 0};
glm::vec3(1, 0, 0) glm::vec2 uv = {0, 0};
}); if (mesh->mNumUVComponents[0] == 2)
uv = *reinterpret_cast<glm::vec2*>(&mesh->mTextureCoords[0][i]);
glm::vec3 normal = {0, 0, 0};
if (mesh->mNormals != nullptr)
normal = *reinterpret_cast<glm::vec3*>(&mesh->mNormals[i]);
vertices.push_back({pos, color, uv, normal});
} }
for (size_t i = 0; i < mesh->mNumFaces; i++){ for (size_t i = 0; i < mesh->mNumFaces; i++){

@ -16,16 +16,18 @@ SoftBody::SoftBody(Mesh* mesh, float edgeCompliance, float triangleCompliance, f
in.pointlist = new REAL[mesh->vertices.size() * 3]; in.pointlist = new REAL[mesh->vertices.size() * 3];
in.numberoffacets = static_cast<int>(mesh->faces.size()); in.numberoffacets = static_cast<int>(mesh->faces.size());
in.facetlist = new tetgenio::facet[mesh->faces.size()]; in.facetlist = new tetgenio::facet[mesh->faces.size()];
in.numberofpointattributes = 3; in.numberofpointattributes = 5; // color, uv
in.pointattributelist = new REAL[mesh->vertices.size() * 3]; in.pointattributelist = new REAL[mesh->vertices.size() * 5];
for (size_t i = 0; i < mesh->vertices.size(); i++){ for (size_t i = 0; i < mesh->vertices.size(); i++){
in.pointlist[i * 3 + 0] = mesh->vertices[i].position.x; in.pointlist[i * 3 + 0] = mesh->vertices[i].position.x;
in.pointlist[i * 3 + 1] = mesh->vertices[i].position.y; in.pointlist[i * 3 + 1] = mesh->vertices[i].position.y;
in.pointlist[i * 3 + 2] = mesh->vertices[i].position.z; 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 * 5 + 0] = mesh->vertices[i].color.r;
in.pointattributelist[i * 3 + 1] = (mesh->vertices[i].position.y + 1) / 2; in.pointattributelist[i * 5 + 1] = mesh->vertices[i].color.g;
in.pointattributelist[i * 3 + 2] = (mesh->vertices[i].position.z + 1) / 2; 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++){ 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<float>(out.pointlist[i * 3 + 0]); float x = static_cast<float>(out.pointlist[i * 3 + 0]);
float y = static_cast<float>(out.pointlist[i * 3 + 1]); float y = static_cast<float>(out.pointlist[i * 3 + 1]);
float z = static_cast<float>(out.pointlist[i * 3 + 2]); float z = static_cast<float>(out.pointlist[i * 3 + 2]);
float r = static_cast<float>(out.pointattributelist[i * 3 + 0]); float r = static_cast<float>(out.pointattributelist[i * 5 + 0]);
float g = static_cast<float>(out.pointattributelist[i * 3 + 1]); float g = static_cast<float>(out.pointattributelist[i * 5 + 1]);
float b = static_cast<float>(out.pointattributelist[i * 3 + 2]); float b = static_cast<float>(out.pointattributelist[i * 5 + 2]);
vertices.emplace_back(Vertex({x, y, z}, {r, g, b})); float s = static_cast<float>(out.pointattributelist[i * 5 + 3]);
float t = static_cast<float>(out.pointattributelist[i * 5 + 4]);
vertices.emplace_back(Vertex({x, y, z}, {r, g, b}, {s, t}));
} }
faces.reserve(out.numberoftrifaces); faces.reserve(out.numberoftrifaces);

@ -1,4 +1,3 @@
#include <cstdio>
#include "vulkan/image.hpp" #include "vulkan/image.hpp"
#include "vulkan/instance.hpp" #include "vulkan/instance.hpp"

@ -8,8 +8,8 @@ VkVertexInputBindingDescription Vertex::getBindingDescription() {
return bindingDescription; return bindingDescription;
} }
std::array<VkVertexInputAttributeDescription, 3> Vertex::getAttributeDescriptions() { std::vector<VkVertexInputAttributeDescription> Vertex::getAttributeDescriptions() {
std::array<VkVertexInputAttributeDescription, 3> attributeDescriptions {}; std::vector<VkVertexInputAttributeDescription> attributeDescriptions(4);
attributeDescriptions[0].binding = 0; attributeDescriptions[0].binding = 0;
attributeDescriptions[0].location = 0; attributeDescriptions[0].location = 0;
@ -24,7 +24,12 @@ std::array<VkVertexInputAttributeDescription, 3> Vertex::getAttributeDescription
attributeDescriptions[2].binding = 0; attributeDescriptions[2].binding = 0;
attributeDescriptions[2].location = 2; attributeDescriptions[2].location = 2;
attributeDescriptions[2].format = VK_FORMAT_R32G32B32_SFLOAT; 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; return attributeDescriptions;
} }

Loading…
Cancel
Save