-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFlag.h
More file actions
137 lines (112 loc) · 4.02 KB
/
Copy pathFlag.h
File metadata and controls
137 lines (112 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* SimShip by Edouard Halbert
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License
http://creativecommons.org/licenses/by-nc-nd/4.0/ */
#pragma once
// Simulation Verlet avec des springs structuraux, de flexion et diagonaux pour simuler un tissu
// 1. PROJECT
#include "Utility.h"
#include "Camera.h"
#include "Sky.h"
#include "vulkan_device.hpp"
#include "vulkan_pipeline.hpp"
#include "vulkan_ubo.hpp"
#include "vulkan_texture.hpp"
// 2. LIB
#include <vulkan/vulkan.h>
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
using namespace glm;
// 3. WIN
#define NOMINMAX
#include <vector>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <math.h>
#include <algorithm>
#include <numeric>
#include <random>
using namespace std;
using namespace std;
using namespace glm;
struct sClothPt {
vec3 pos;
vec3 prevPos;
vec3 acceleration;
bool fixed;
};
class Flag
{
public:
Flag(shared_ptr<VulkanDevice> vulkanDevice, VkRenderPass renderPass, VkExtent2D extent, int w, int h, float s, const char* path);
~Flag();
void Update(uint32_t frame, float deltaTime, const vec2& baseWind);
void Render(VkCommandBuffer commandBuffer, uint32_t frame, const mat4& model, const mat4& view, const mat4& projection, float exposure);
void RecreatePipelines(VkRenderPass renderPass, VkExtent2D newExtent);
bool bVisible = true;
private:
void initParticles();
void initBuffers();
void loadTexture(const char* path);
void CreatePipeline(VkRenderPass renderPass, VkExtent2D extent);
void CreateDescriptors();
void UpdateDescriptors();
void updateVerticesBuffer(uint32_t frame);
vec3 computeTriangleNormal(int idx0, int idx1, int idx2) const;
void applyForces(float time, const vec2& wind);
void integrate(float deltaTime);
uint32_t getIndex(int x, int y) const { return static_cast<uint32_t>(y * mWidth + x); }
void applyConstraint(int idx1, int idx2, float restLength, float rigidity);
void satisfyConstraints();
void applyWindAlignmentConstraint(const vec3& windDir, float strength);
struct sVertexFlag
{
float pos[3]; // x, y, z
float uv[2]; // s, t
static VkVertexInputBindingDescription getBindingDescription()
{
VkVertexInputBindingDescription binding{};
binding.binding = 0;
binding.stride = sizeof(sVertexFlag);
binding.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
return binding;
}
static array<VkVertexInputAttributeDescription, 2> getAttributeDescriptions()
{
array<VkVertexInputAttributeDescription, 2> attrs{};
// layout(location = 0) vec3 aPos;
attrs[0].location = 0;
attrs[0].binding = 0;
attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT;
attrs[0].offset = offsetof(sVertexFlag, pos);
// layout(location = 1) vec3 aColor;
attrs[1].location = 1;
attrs[1].binding = 0;
attrs[1].format = VK_FORMAT_R32G32B32_SFLOAT;
attrs[1].offset = offsetof(sVertexFlag, uv);
return attrs;
}
};
struct sFlagUBO {
mat4 model;
mat4 view;
mat4 proj;
float time;
float exposure;
float pad[2];
};
shared_ptr<VulkanDevice> mVulkanDevice;
sPipeline_x mPipeline;
unique_ptr<VulkanTexture> mTexture;
vector<unique_ptr<VulkanUBO>>mVertexBuffers;
uint32_t mVertexCount;
unique_ptr<VulkanUBO> mIndiceBuffer;
uint32_t mIndiceCount;
int mWidth, mHeight;
float mSpacing;
vector<sClothPt> vClothPts;
vector<uint32_t> mIndices;
};