-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClothModel.cpp
More file actions
329 lines (277 loc) · 12.1 KB
/
Copy pathClothModel.cpp
File metadata and controls
329 lines (277 loc) · 12.1 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <algorithm>
#include "TriangleIntersection.h"
#include "ClothSample.h"
#include "ClothModel.h"
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#pragma optimize("", off)
const char *ClothModel::ClothVS = "Cloth.vs.hlsl";
const char *ClothModel::ClothPS = "Cloth.ps.hlsl";
GraphicsProgram::SharedPtr ClothModel::spClothProgram = nullptr;
GraphicsVars::SharedPtr ClothModel::spClothVars = nullptr;
VertexLayout::SharedPtr ClothModel::spVertexLayout = nullptr;
std::vector<ClothModel::TTextureSet> ClothModel::sTextureSets;
int32 ClothModel::createRectMesh(const vec2 &size, const ivec2 &tessellation, std::vector<float3> &positions, std::vector<float2> &texCoords, std::vector<trimesh::triangle_t> &triangles, trimesh::trimesh_t &mesh)
{
const auto vertexCount = tessellation.x * tessellation.y;
const auto gridStep = size / vec2(tessellation - 1);
for (int32 y = 0; y < tessellation.y; ++y)
{
for (int32 x = 0; x < tessellation.x; ++x)
{
positions.emplace_back(float32(x) * gridStep.x - size.x * 0.5f, size.y * 0.5f - float32(y) * gridStep.y, 0.0f);
texCoords.emplace_back(float2(x, y) / float2(tessellation - 1));
}
}
const auto EmitTriangle = [&](const auto &v0, const auto &v1, const auto &v2) -> void
{
trimesh::triangle_t triangle;
triangle.v[0] = v0;
triangle.v[1] = v1;
triangle.v[2] = v2;
triangles.push_back(triangle);
};
for (int32 y = 0; y < tessellation.y - 1; ++y)
{
for (int32 x = 0; x < tessellation.x - 1; ++x)
{
EmitTriangle(
(y + 1) * tessellation.x + x,
y * tessellation.x + (x + 1),
y * tessellation.x + x
);
EmitTriangle(
(y + 1) * tessellation.x + (x + 1),
y * tessellation.x + (x + 1),
(y + 1) * tessellation.x + x
);
}
}
std::vector<trimesh::edge_t> edges;
trimesh::unordered_edges_from_triangles(int(triangles.size()), &triangles[0], edges);
mesh.build(vertexCount, int(triangles.size()), &triangles[0], int(edges.size()), &edges[0]);
return vertexCount;
}
void ClothModel::init()
{
spVertexLayout = VertexLayout::create();
VertexBufferLayout::SharedPtr pPositionsLayout = VertexBufferLayout::create();
pPositionsLayout->addElement("POSITION", 0, ResourceFormat::RGB32Float, 1, 0);
spVertexLayout->addBufferLayout(0, pPositionsLayout);
VertexBufferLayout::SharedPtr pNormalsLayout = VertexBufferLayout::create();
pNormalsLayout->addElement("NORMAL", 0, ResourceFormat::RGB32Float, 1, 1);
spVertexLayout->addBufferLayout(1, pNormalsLayout);
VertexBufferLayout::SharedPtr pColorsLayout = VertexBufferLayout::create();
pColorsLayout->addElement("COLOR", 0, ResourceFormat::RGB32Float, 1, 2);
spVertexLayout->addBufferLayout(2, pColorsLayout);
VertexBufferLayout::SharedPtr pTexCoordsLayout = VertexBufferLayout::create();
pTexCoordsLayout->addElement("TEXCOORD", 0, ResourceFormat::RG16Unorm, 1, 3);
spVertexLayout->addBufferLayout(3, pTexCoordsLayout);
for (auto& file : fs::directory_iterator("Data/Materials/"))
{
const auto &ReadTexture = [=](const char *name, bool generateMipLevels, bool loadAsSrgb) -> Texture::SharedPtr
{
auto path = file.path().string() + "/" + name;
return createTextureFromFile(path, generateMipLevels, loadAsSrgb, Texture::BindFlags::ShaderResource);
};
std::string filename = file.path().filename().string();
TTextureSet textureSet;
textureSet.Name = file.path().filename().string();
textureSet.BaseColorMap = ReadTexture("BaseColor.png", true, true);
textureSet.RoughnessMap = ReadTexture("Roughness.png", true, false);
textureSet.NormalMap = ReadTexture("Normal.png", true, false);
sTextureSets.emplace_back(std::move(textureSet));
}
Program::DefineList defineList;
GraphicsProgram::Desc clothProgramDesc;
clothProgramDesc.addShaderLibrary(ClothVS).vsEntry("main");
clothProgramDesc.addShaderLibrary(ClothPS).psEntry("main");
spClothProgram = GraphicsProgram::create(clothProgramDesc, defineList);
spClothVars = GraphicsVars::create(spClothProgram->getReflector());
}
void ClothModel::sharedInit(const std::vector<float2> &texCoords)
{
std::vector<uint16> vIndices;
for (const auto &triangle : mTriangles)
{
vIndices.emplace_back(uint16(triangle.v[0]));
vIndices.emplace_back(uint16(triangle.v[1]));
vIndices.emplace_back(uint16(triangle.v[2]));
}
std::vector<uint16> unormTexCoords;
for (const auto &uv : texCoords)
{
unormTexCoords.push_back(uint16_t(uv.x * std::numeric_limits<uint16_t>::max()));
unormTexCoords.push_back(uint16_t(uv.y * std::numeric_limits<uint16_t>::max()));
}
const auto vertexCount = mTriangles.size() * 3;
mpIndexBuffer = Buffer::create(vIndices.size() * sizeof(uint16_t), ResourceBindFlags::Index, Buffer::CpuAccess::None, vIndices.data());
mpVBPositions = Buffer::create(vertexCount * sizeof(float32_t[3]), ResourceBindFlags::Vertex, Buffer::CpuAccess::Write, nullptr);
mpVBNormals = Buffer::create(vertexCount * sizeof(float32_t[3]), ResourceBindFlags::Vertex, Buffer::CpuAccess::Write, nullptr);
mpVBColors = Buffer::create(vertexCount * sizeof(float32_t[3]), ResourceBindFlags::Vertex, Buffer::CpuAccess::Write, nullptr);
mpVBTexCoords = Buffer::create(vertexCount * sizeof(uint16_t[2]), ResourceBindFlags::Vertex, Buffer::CpuAccess::Write, unormTexCoords.data());
Vao::BufferVec buffers{ mpVBPositions, mpVBNormals, mpVBColors, mpVBTexCoords };
mpVao = Vao::create(Vao::Topology::TriangleList, spVertexLayout, buffers, mpIndexBuffer, ResourceFormat::R16Uint);
mpClothState = GraphicsState::create();
RasterizerState::Desc rasterizerDesc;
rasterizerDesc.setCullMode(RasterizerState::CullMode::None);
rasterizerDesc.setFillMode(RasterizerState::FillMode::Solid);
mpRasterizeNormal = RasterizerState::create(rasterizerDesc);
rasterizerDesc.setFillMode(RasterizerState::FillMode::Wireframe);
mpRasterizeWireframe = RasterizerState::create(rasterizerDesc);
mpClothState->setProgram(spClothProgram);
mpClothState->setVao(mpVao);
}
void ClothModel::getTriangleVertices(const trimesh::triangle_t &triangle, vec3 vertices[3]) const
{
vertices[0] = getVertexPosition(triangle[0]);
vertices[1] = getVertexPosition(triangle[1]);
vertices[2] = getVertexPosition(triangle[2]);
}
vec3 ClothModel::getVertexNormal(int32 index) const
{
return computeVertexNormal(index);
}
vec3 ClothModel::computeVertexNormal(int32 index) const
{
auto faces = mMesh.vertex_face_neighbors(index);
float3 normal(0.0f);
const auto &GetPlaneNormal = [](const float3 &A, const float3 &B, const float3 &C) -> float3 {
const auto result = normalize(cross(C - B, A - B));
return any(isnan(result)) ? float3(0.0f) : result;
};
for (const auto &face : faces)
{
const auto &triangle = mTriangles[face];
normal += GetPlaneNormal(
getVertexPosition(triangle[0]),
getVertexPosition(triangle[1]),
getVertexPosition(triangle[2])
);
}
return normalize(normal);
}
void ClothModel::render(ClothSample *pClothSample, SampleCallbacks *pSample)
{
const auto currTime = pSample->getCurrentTime();
auto *pRenderContext = pSample->getRenderContext();
const auto &pTargetFbo = pSample->getCurrentFbo();
const int32 vertexCount = int32(mMesh.get_num_vertices());
float32_t *pPositions = reinterpret_cast<float32_t*>(mpVBPositions->map(Buffer::MapType::WriteDiscard));
float32_t *pNormals = reinterpret_cast<float32_t*>(mpVBNormals->map(Buffer::MapType::WriteDiscard));
for (int32 vertex = 0; vertex < vertexCount; ++vertex)
{
const auto position = getVertexPosition(vertex);
std::copy_n(position.data.data, 3, pPositions + vertex * 3);
const auto normal = getVertexNormal(vertex);
std::copy_n(normal.data.data, 3, pNormals + vertex * 3);
if (mUserParams.bShowNormals)
{
pClothSample->drawVector(normal, position);
}
else if (mUserParams.bShowParticles)
{
auto unitSphere = Scene::ModelInstance::create(pClothSample->mpDbgUnitSphere, position, vec3(0.0f), vec3(0.0005f));
unitSphere->move(vec3(0.0f), vec3(0.0f, 0.0f, 1.0f), vec3(0.0f, 1.0f, 0.0f));
pClothSample->mpScene->addModelInstance(unitSphere);
}
}
mpVBPositions->unmap();
mpVBNormals->unmap();
auto *pColors = reinterpret_cast<vec3*>(mpVBColors->map(Buffer::MapType::WriteDiscard));
std::fill_n(pColors, vertexCount, mUserParams.baseColor);
if (mUserParams.bShowSelfCollisions)
{
const auto &HandleCollision = [&](const trimesh::triangle_t &triangle) -> void
{
pColors[triangle[0]] = vec3(1.0f, 0.0f, 0.0f);
pColors[triangle[1]] = vec3(1.0f, 0.0f, 0.0f);
pColors[triangle[2]] = vec3(1.0f, 0.0f, 0.0f);
// getParticle(x, y)->mPosition = getParticle(x, y)->mPrevPosition;
};
for (int32 t0 = 0; t0 < mTriangles.size(); ++t0)
{
vec3 t0_vertices[3];
const auto &triangle0 = mTriangles[t0];
getTriangleVertices(triangle0, t0_vertices);
for (int32 t1 = t0 + 1; t1 < mTriangles.size(); ++t1)
{
vec3 t1_vertices[3];
const auto &triangle1 = mTriangles[t1];
getTriangleVertices(triangle1, t1_vertices);
vec3 response; // TODO(mitko): Remove extra "responce" code from triangle intersect implementation
bool collision = intersectTriangles(t0_vertices, t1_vertices, response);
if (!collision) continue;
for (const auto vertex : triangle0.v)
{
const auto faces = mMesh.vertex_face_neighbors(vertex);
for (const auto face : faces)
{
collision &= triangle1[0] != vertex;
collision &= triangle1[1] != vertex;
collision &= triangle1[2] != vertex;
}
}
if (!collision) continue;
HandleCollision(triangle0);
HandleCollision(triangle1);
}
}
}
mpVBColors->unmap();
if (mUserParams.bShowWireframe)
mpClothState->setRasterizerState(mpRasterizeWireframe);
else
mpClothState->setRasterizerState(mpRasterizeNormal);
pClothSample->mpCamera->setIntoConstantBuffer(spClothVars->getConstantBuffer("InternalPerFrameCB").get(), "gCamera");
pClothSample->mpDirLight->setIntoProgramVars(spClothVars.get(), spClothVars["PerFrameCB"].get(), "gDirLight");
if (!sTextureSets.empty())
{
const auto &textureSet = sTextureSets[mUserParams.textureSet];
spClothVars->setTexture("gBaseColorMap", textureSet.BaseColorMap);
spClothVars->setTexture("gRoughnessMap", textureSet.RoughnessMap);
spClothVars->setTexture("gNormalMap", textureSet.NormalMap);
}
pRenderContext->setGraphicsVars(spClothVars);
pRenderContext->setGraphicsState(mpClothState);
mpClothState->pushFbo(pTargetFbo);
pRenderContext->drawIndexed(uint32(mTriangles.size()) * 3u, 0, 0);
mpClothState->popFbo();
}
void ClothModel::onGuiRender(ClothSample *pClothSample, SampleCallbacks *pSample)
{
auto *pGui = pSample->getGui();
pGui->addFloatSlider("Drag", mUserParams.windDragCoefficient, 0.0f, 1.0f);
pGui->addFloatSlider("Lift", mUserParams.windLiftCoefficient, 0.0f, 1.0f);
pGui->addCheckBox("Show Particles", mUserParams.bShowParticles);
pGui->addCheckBox("Show Wireframe", mUserParams.bShowWireframe);
pGui->addCheckBox("Show Surface Normals", mUserParams.bShowNormals);
pGui->addCheckBox("Show Wind Effect", mUserParams.bShowWindEffect);
pGui->addCheckBox("Show Self Collisions", mUserParams.bShowSelfCollisions);
if (!sTextureSets.empty())
{
Gui::DropdownList textureSetDropdown;
for (uint32 textureSet = 0u; textureSet < sTextureSets.size(); ++textureSet)
textureSetDropdown.push_back({ textureSet, sTextureSets[textureSet].Name });
pGui->addDropdown("Texture Set", textureSetDropdown, reinterpret_cast<uint32&>(mUserParams.textureSet));
}
pGui->addRgbColor("Base Color", mUserParams.baseColor);
}
auto ClothModel::createClothModel(EType type, const ClothModel *parent) -> SharedPtr
{
const auto *parentToPass = parent && parent->getType() == type ? parent : nullptr;
SharedPtr result;
switch (type)
{
case ParticleSpringModel: result = createClothModel<ParticleSpringModel>(parentToPass); break;
case FiniteElementsMethod: result = createClothModel<FiniteElementsMethod>(parentToPass); break;
default:
should_not_get_here();
return nullptr;
}
if (parent != nullptr)
{
result->mUserParams = parent->mUserParams;
}
return result;
}