-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathBulletPhysicsCloth.cc
323 lines (294 loc) · 13.2 KB
/
BulletPhysicsCloth.cc
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
//------------------------------------------------------------------------------
// BulletPhysicsCloth.cc
//------------------------------------------------------------------------------
#include "Pre.h"
#include "Core/Main.h"
#include "Gfx/Gfx.h"
#include "Dbg/Dbg.h"
#include "Input/Input.h"
#include "Core/Time/Clock.h"
#include "Common/CameraHelper.h"
#include "PhysicsCommon/Physics.h"
#include "PhysicsCommon/ShapeRenderer.h"
#include "shaders.h"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/random.hpp"
#include <string.h>
using namespace Oryol;
static const float SphereRadius = 1.0f;
static const float BoxSize = 1.5f;
static const int NumClothSegments = 25;
static const int NumClothQuads = (NumClothSegments-1)*(NumClothSegments-1);
static const int NumClothTriangles = NumClothQuads*2;
static const int NumClothVertices = NumClothTriangles*3;
class BulletPhysicsClothApp : public App {
public:
AppState::Code OnInit();
AppState::Code OnRunning();
AppState::Code OnCleanup();
Duration updatePhysics();
Duration updateInstanceData();
Duration updateClothData();
bool clothFlatShading = false;
int frameIndex = 0;
TimePoint lapTimePoint;
CameraHelper camera;
ShapeRenderer shapeRenderer;
Id clothMesh;
DrawState clothColorDrawState;
DrawState clothShadowDrawState;
ColorShader::vsParams colorVSParams;
ColorShader::fsParams colorFSParams;
ShadowShader::vsParams shadowVSParams;
glm::mat4 lightProjView;
Id planeShape;
Id boxShape;
Id sphereShape;
Id groundRigidBody;
Id clothSoftBody;
static const int MaxNumBodies = 256;
int numBodies = 0;
StaticArray<Id, MaxNumBodies> bodies;
struct {
float pos[3] = { 0, 0, 0 };
float nrm[3] = { 0, 0, 0 };
} clothVertices[NumClothVertices];
};
OryolMain(BulletPhysicsClothApp);
//------------------------------------------------------------------------------
AppState::Code
BulletPhysicsClothApp::OnInit() {
auto gfxSetup = GfxSetup::WindowMSAA4(800, 600, "BulletPhysicsCloth");
gfxSetup.DefaultPassAction = PassAction::Clear(glm::vec4(0.2f, 0.4f, 0.8f, 1.0f));
gfxSetup.HtmlTrackElementSize = true;
Gfx::Setup(gfxSetup);
this->colorFSParams.shadowMapSize = glm::vec2(float(this->shapeRenderer.ShadowMapSize));
// instanced shape rendering helper class
const Id colorShader = Gfx::CreateResource(ColorShader::Setup());
const Id shadowShader = Gfx::CreateResource(ShadowShader::Setup());
this->shapeRenderer.ColorShader = colorShader;
this->shapeRenderer.ColorShaderInstanced = Gfx::CreateResource(ColorShaderInstanced::Setup());
this->shapeRenderer.ShadowShader = shadowShader;
this->shapeRenderer.ShadowShaderInstanced = Gfx::CreateResource(ShadowShaderInstanced::Setup());
this->shapeRenderer.SphereRadius = SphereRadius;
this->shapeRenderer.BoxSize = BoxSize;
this->shapeRenderer.Setup(gfxSetup);
// setup a mesh with dynamic vertex buffer and no indices
// FIXME: use index rendering later
auto meshSetup = MeshSetup::Empty(NumClothVertices, Usage::Stream);
meshSetup.Layout
.Add(VertexAttr::Position, VertexFormat::Float3)
.Add(VertexAttr::Normal, VertexFormat::Float3);
meshSetup.AddPrimitiveGroup(PrimitiveGroup(0, NumClothTriangles*3));
this->clothMesh = Gfx::CreateResource(meshSetup);
this->clothColorDrawState.Mesh[0] = this->clothMesh;
this->clothShadowDrawState.Mesh[0] = this->clothMesh;
// setup pipeline states (color and shadow pass) for cloth rendering
auto pipSetup = PipelineSetup::FromLayoutAndShader(meshSetup.Layout, colorShader);
pipSetup.DepthStencilState.DepthWriteEnabled = true;
pipSetup.DepthStencilState.DepthCmpFunc = CompareFunc::LessEqual;
pipSetup.RasterizerState.CullFaceEnabled = false;
pipSetup.RasterizerState.SampleCount = gfxSetup.SampleCount;
this->clothColorDrawState.Pipeline = Gfx::CreateResource(pipSetup);
pipSetup = PipelineSetup::FromLayoutAndShader(meshSetup.Layout, shadowShader);
pipSetup.DepthStencilState.DepthWriteEnabled = true;
pipSetup.DepthStencilState.DepthCmpFunc = CompareFunc::LessEqual;
pipSetup.RasterizerState.CullFaceEnabled = false;
pipSetup.RasterizerState.SampleCount = 1;
pipSetup.BlendState.ColorFormat = PixelFormat::RGBA8;
pipSetup.BlendState.DepthFormat = PixelFormat::DEPTH;
this->clothShadowDrawState.Pipeline = Gfx::CreateResource(pipSetup);
// setup directional light (for lighting and shadow rendering)
glm::mat4 lightView = glm::lookAt(glm::vec3(50.0f, 50.0f, -50.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
// this shifts the post-projection Z coordinate into the range 0..1 (like D3D),
// instead of -1..+1 (like OpenGL), which makes sure that objects in the
// range -1.0 to +1.0 are not clipped away in D3D, the shadow map lookup in D3D
// also needs to invert the Y coordinate (that's handled in the pixel shader
// where the lookup happens)
glm::mat4 lightProj = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, 1.0f));
lightProj = glm::scale(lightProj, glm::vec3(1.0f, 1.0f, 0.5f));
lightProj = lightProj * glm::ortho(-75.0f, +75.0f, -75.0f, +75.0f, 1.0f, 400.0f);
this->lightProjView = lightProj * lightView;
this->colorFSParams.lightDir = glm::vec3(glm::column(glm::inverse(lightView), 2));
// setup the initial physics world
Physics::Setup();
this->boxShape = Physics::Create(CollideShapeSetup::Box(glm::vec3(BoxSize)));
this->sphereShape = Physics::Create(CollideShapeSetup::Sphere(SphereRadius));
this->planeShape = Physics::Create(CollideShapeSetup::Plane(glm::vec4(0, 1, 0, 0)));
// create a fixed ground rigid body
this->groundRigidBody = Physics::Create(RigidBodySetup::FromShape(this->planeShape, glm::mat4(1.0f), 0.0f, 0.25f));
Physics::Add(this->groundRigidBody);
// create cloth shape
auto clothSetup = SoftBodySetup::Patch();
clothSetup.NumSegments = NumClothSegments;
clothSetup.Points[0] = glm::vec3(-10.0f, 10.0f, -10.0f);
clothSetup.Points[1] = glm::vec3(+10.0f, 10.0f, -10.0f);
clothSetup.Points[2] = glm::vec3(-10.0f, 10.0f, +10.0f);
clothSetup.Points[3] = glm::vec3(+10.0f, 10.0f, +10.0f);
this->clothSoftBody = Physics::Create(clothSetup);
Physics::Add(this->clothSoftBody);
Input::Setup();
Dbg::Setup();
this->camera.Setup();
camera.Orbital = glm::vec2(glm::radians(40.0f), glm::radians(180.0f));
camera.Center = glm::vec3(0.0f, 7.0f, 0.0f);
this->lapTimePoint = Clock::Now();
return App::OnInit();
}
//------------------------------------------------------------------------------
AppState::Code
BulletPhysicsClothApp::OnRunning() {
Duration frameTime = Clock::LapTime(this->lapTimePoint);
Duration physicsTime = this->updatePhysics();
Duration instUpdTime = this->updateInstanceData();
Duration clothUpdTime = this->updateClothData();
this->camera.Update();
if (Input::KeyboardAttached() && Input::KeyDown(Key::F)) {
this->clothFlatShading = !this->clothFlatShading;
}
// the shadow pass
Gfx::BeginPass(this->shapeRenderer.ShadowPass);
this->shadowVSParams.mvp = this->lightProjView;
this->shapeRenderer.DrawShadows(this->shadowVSParams);
Gfx::ApplyDrawState(this->clothShadowDrawState);
Gfx::ApplyUniformBlock(this->shadowVSParams);
Gfx::Draw();
Gfx::EndPass();
// begin color pass rendering
Gfx::BeginPass();
// draw ground
const glm::mat4 model = Physics::Transform(this->groundRigidBody);
this->colorVSParams.model = model;
this->colorVSParams.mvp = this->camera.ViewProj * model;
this->colorVSParams.lightMVP = lightProjView * model;
this->colorVSParams.diffColor = glm::vec3(0.5, 0.5, 0.5);
this->colorFSParams.eyePos = this->camera.EyePos;
this->colorFSParams.specIntensity = 1.0f;
this->shapeRenderer.DrawGround(this->colorVSParams, this->colorFSParams);
// draw the dynamic shapes
this->colorVSParams.model = glm::mat4();
this->colorVSParams.mvp = this->camera.ViewProj;
this->colorVSParams.lightMVP = lightProjView;
this->colorVSParams.diffColor = glm::vec3(1.0f, 1.0f, 1.0f);
this->shapeRenderer.DrawShapes(this->colorVSParams, this->colorFSParams);
// draw the cloth patch
this->colorVSParams.diffColor = glm::vec3(0.6f, 0.0f, 0.05f);
this->colorFSParams.specIntensity = 0.15f;
Gfx::ApplyDrawState(this->clothColorDrawState);
Gfx::ApplyUniformBlock(this->colorVSParams);
Gfx::ApplyUniformBlock(this->colorFSParams);
Gfx::Draw();
Dbg::PrintF("\n\r"
" Mouse left click + drag: rotate camera\n\r"
" Mouse wheel: zoom camera\n\r"
" P: pause/continue\n\r"
" F: toggle cloth flat shading\n\n\r"
" Frame time: %.4f ms\n\r"
" Physics time: %.4f ms\n\r"
" Instance buffer upd: %.4f ms\n\r"
" Cloth buffer upd: %.4f ms\n\r"
" Num Rigid Bodies: %d\n\r",
frameTime.AsMilliSeconds(),
physicsTime.AsMilliSeconds(),
instUpdTime.AsMilliSeconds(),
clothUpdTime.AsMilliSeconds(),
this->numBodies);
Dbg::DrawTextBuffer();
Gfx::EndPass();
Gfx::CommitFrame();
return Gfx::QuitRequested() ? AppState::Cleanup : AppState::Running;
}
//------------------------------------------------------------------------------
AppState::Code
BulletPhysicsClothApp::OnCleanup() {
// FIXME: Physics should just cleanup this stuff on shutdown!
Physics::Remove(this->clothSoftBody);
Physics::Destroy(this->clothSoftBody);
Physics::Remove(this->groundRigidBody);
Physics::Destroy(this->groundRigidBody);
for (int i = 0; i < this->numBodies; i++) {
Physics::Remove(this->bodies[i]);
Physics::Destroy(this->bodies[i]);
}
Physics::Destroy(this->sphereShape);
Physics::Destroy(this->boxShape);
Physics::Destroy(this->planeShape);
Physics::Discard();
Dbg::Discard();
Input::Discard();
Gfx::Discard();
return App::OnCleanup();
}
//------------------------------------------------------------------------------
Duration
BulletPhysicsClothApp::updatePhysics() {
TimePoint physStartTime = Clock::Now();
if (!this->camera.Paused) {
// emit new rigid bodies
this->frameIndex++;
if ((this->frameIndex % 60) == 0) {
if (this->numBodies < MaxNumBodies) {
float x = this->numBodies & 1 ? -1.0f : 1.0f;
const glm::mat4 tform = glm::translate(glm::mat4(), glm::vec3(20*x, 3, 0));
Id newObj = Physics::Create(RigidBodySetup::FromShape(this->sphereShape, tform, 25.0f, 0.5f));
Physics::Add(newObj);
this->bodies[this->numBodies] = newObj;
this->numBodies++;
btRigidBody* body = Physics::RigidBody(newObj);
glm::vec3 vel = glm::vec3(-6*x, 30, 0);
glm::vec3 ang = glm::ballRand(10.0f);
body->setLinearVelocity(btVector3(vel.x, vel.y, vel.z));
body->setAngularVelocity(btVector3(ang.x, ang.y, ang.z));
body->setDamping(0.1f, 0.1f);
}
}
Physics::Update(1.0f/60.0f);
}
return Clock::Since(physStartTime);
}
//------------------------------------------------------------------------------
Duration
BulletPhysicsClothApp::updateInstanceData() {
TimePoint startTime = Clock::Now();
this->shapeRenderer.BeginTransforms();
for (int i = 0; i < numBodies; i++) {
CollideShapeSetup::ShapeType type = Physics::RigidBodyShapeType(this->bodies[i]);
if (CollideShapeSetup::SphereShape == type) {
this->shapeRenderer.UpdateSphereTransform(Physics::Transform(this->bodies[i]));
}
else {
this->shapeRenderer.UpdateBoxTransform(Physics::Transform(this->bodies[i]));
}
}
this->shapeRenderer.EndTransforms();
return Clock::Since(startTime);
}
//------------------------------------------------------------------------------
Duration
BulletPhysicsClothApp::updateClothData() {
TimePoint startTime = Clock::Now();
btSoftBody* body = Physics::SoftBody(this->clothSoftBody);
const int numTris = body->m_faces.size();
o_assert(numTris == NumClothTriangles);
for (int triIndex = 0, vtxIndex=0; triIndex < numTris; triIndex++) {
const auto& face = body->m_faces[triIndex];
for (int i = 0; i < 3; i++, vtxIndex++) {
auto& vtx = this->clothVertices[vtxIndex];
vtx.pos[0] = face.m_n[i]->m_x.m_floats[0];
vtx.pos[1] = face.m_n[i]->m_x.m_floats[1];
vtx.pos[2] = face.m_n[i]->m_x.m_floats[2];
if (this->clothFlatShading) {
vtx.nrm[0] = -face.m_normal.m_floats[0];
vtx.nrm[1] = -face.m_normal.m_floats[1];
vtx.nrm[2] = -face.m_normal.m_floats[2];
}
else {
vtx.nrm[0] = -face.m_n[i]->m_n.m_floats[0];
vtx.nrm[1] = -face.m_n[i]->m_n.m_floats[1];
vtx.nrm[2] = -face.m_n[i]->m_n.m_floats[2];
}
}
}
Gfx::UpdateVertices(this->clothMesh, this->clothVertices, sizeof(this->clothVertices));
return Clock::Since(startTime);
}