-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
344 lines (273 loc) · 10 KB
/
main.cpp
File metadata and controls
344 lines (273 loc) · 10 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <algorithm>
#include <iostream>
#include <cstdint>
#include <cmath>
// #include <thread>
// #include <mutex>
#include "BWindow/GDIWindow.h"
#include "windows.h"
struct vec2 {
float x, y;
};
enum class Field : uint8_t {
VEL_X,
VEL_Y,
SMOKE
};
template<size_t NUM_CELLS_X, size_t NUM_CELLS_Y>
struct FluidGrid {
float hor [NUM_CELLS_Y + 1][NUM_CELLS_X + 1]{}; // horizontal velocities
float vert [NUM_CELLS_Y + 1][NUM_CELLS_X + 1]{}; // vertical velocities
float s [NUM_CELLS_Y + 1][NUM_CELLS_X + 1]{}; // scaling (0 = wall, 1 = fluid)
float smoke[NUM_CELLS_Y + 1][NUM_CELLS_X + 1]{};
// float pressure[NUM_CELLS_Y][NUM_CELLS_X]{};
inline FluidGrid() {
for(size_t y = 0; y < NUM_CELLS_Y + 1; y++)
for(size_t x = 0; x < NUM_CELLS_X + 1; x++)
s[y][x] = 1.f;
}
inline void addVel(const float vHor, const float vVert) {
for(size_t y = 1; y < NUM_CELLS_Y; y++)
for(size_t x = 1; x < NUM_CELLS_X + 1; x++)
hor[y][x] += vHor;
for(size_t y = 1; y < NUM_CELLS_Y + 1; y++)
for(size_t x = 1; x < NUM_CELLS_X; x++)
vert[y][x] += vVert;
}
inline float divergence(const size_t x, const size_t y) const { // calculate divergence (outflow at specified cell)
return hor[y][x + 1] - hor[y][x] // horizontal divergence
+ vert[y + 1][x] - vert[y][x]; // vertical divergence
}
inline void solveDivergence(const size_t num_iter = 100, const float overRelaxation = 1.9f) { // change velocities so divergence becomes 0
for(size_t i = 0; i < num_iter; i++) {
for(size_t y = 1; y < NUM_CELLS_Y; y++) {
for(size_t x = 1; x < NUM_CELLS_X; x++) {
if(s[y][x] == 0.f) continue; // inside wall
const float sx0 = s[y][x-1];
const float sx1 = s[y][x+1];
const float sy0 = s[y-1][x];
const float sy1 = s[y+1][x];
const float s = sx0 + sx1 + sy0 + sy1;
if (s == 0.f) continue; // surrounded by walls
const float currentDivergence = divergence(x, y);
const float correctionFactor = -currentDivergence / s * overRelaxation; // TODO: adapt for walls (replace 4)
hor[y][x ] -= correctionFactor * sx0;
hor[y][x+1] += correctionFactor * sx1;
vert[y ][x] -= correctionFactor * sy0;
vert[y+1][x] += correctionFactor * sy1;
}
}
}
}
inline void extrapolate() {
for (size_t x = 0; x < NUM_CELLS_X; x++) {
hor[0][x] = hor[1][x];
hor[NUM_CELLS_Y][x] = hor[NUM_CELLS_Y - 1][x];
}
for (size_t y = 0; y < NUM_CELLS_Y; y++) {
vert[y][0] = vert[y][1];
vert[y][NUM_CELLS_X] = vert[y][NUM_CELLS_X - 1];
}
}
inline float sample(const Field field, float x, float y) const {
const float h = 1.f;
const float h1 = 1.f / h;
const float h2 = .5f * h;
x = std::max<float>(std::min<float>(x, NUM_CELLS_X + 1), 1);
y = std::max<float>(std::min<float>(y, NUM_CELLS_Y + 1), 1);
float dx = 0.f;
float dy = 0.f;
// const float (*f)[NUM_CELLS_Y + 1][NUM_CELLS_X + 1];
const float (*f)[NUM_CELLS_X + 1];
switch (field) {
case Field::VEL_X: f = hor; dy = h2; break;
case Field::VEL_Y: f = vert; dx = h2; break;
case Field::SMOKE: f = smoke; dx = h2; dy = h2; break;
}
const size_t x0 = std::min<size_t>(std::floor((x-dx)*h1), NUM_CELLS_X-1);
const float tx = ((x-dx) - x0*h) * h1;
const size_t x1 = std::min<size_t>(x0 + 1, NUM_CELLS_X-1);
const size_t y0 = std::min<size_t>(std::floor((y-dy)*h1), NUM_CELLS_Y-1);
const float ty = ((y-dy) - y0*h) * h1;
const size_t y1 = std::min<size_t>(y0 + 1, NUM_CELLS_Y-1);
const float sx = 1.0 - tx;
const float sy = 1.0 - ty;
const float val =
sx*sy * f[y0][x0] +
tx*sy * f[y0][x1] +
tx*ty * f[y1][x1] +
sx*ty * f[y1][x0];
return val;
}
inline float avgVelX(const size_t x, const size_t y) const {
return (hor[y][x] + hor[y][x + 1]
+ hor[y + 1][x] + hor[y + 1][x + 1]) * .25f;
}
inline float avgVelY(const size_t x, const size_t y) const {
return (vert[y][x] + vert[y + 1][x]
+ vert[y][x + 1] + vert[y + 1][x + 1]) * .25f;
}
static inline void update(FluidGrid& prev, FluidGrid& next, const float dt) {
for(size_t y = 1; y < NUM_CELLS_Y; y++) {
for(size_t x = 1; x < NUM_CELLS_X; x++) {
// update horizontal velocities:
{
float px = x;
float py = y + .5f;
const float velX = prev.hor[y][x];
const float velY = prev.avgVelY(x, y);
px -= dt*velX;
py -= dt*velY;
const float newVelX = prev.sample(Field::VEL_X, px, py);
next.hor[y][x] = newVelX;
}
// update vertical velocities:
{
float px = x + .5f;
float py = y;
const float velX = prev.avgVelX(x, y);
const float velY = prev.vert[y][x];
px -= dt*velX;
py -= dt*velY;
const float newVelY = prev.sample(Field::VEL_Y, px, py);
next.vert[y][x] = newVelY;
}
}
}
// prev = next;
memcpy(prev.hor, next.hor, sizeof(float) * (NUM_CELLS_X + 1) * (NUM_CELLS_Y + 1));
memcpy(prev.vert, next.vert, sizeof(float) * (NUM_CELLS_X + 1) * (NUM_CELLS_Y + 1));
}
static inline void updateSmoke(FluidGrid& prev, FluidGrid& next, const float dt) {
for(size_t y = 1; y < NUM_CELLS_Y; y++) {
for(size_t x = 1; x < NUM_CELLS_X; x++) {
if(prev.s[y][x] == 0) continue;
const float velX = (prev.hor[y][x] + prev.hor[y][x + 1]) * .5f;
const float velY = (prev.vert[y][x] + prev.vert[y + 1][x]) * .5f;
const float sourceX = x + .5f - velX * dt;
const float sourceY = y + .5f - velY * dt;
next.smoke[y][x] = prev.sample(Field::SMOKE, sourceX, sourceY);
}
}
memcpy(prev.smoke, next.smoke, sizeof(float) * (NUM_CELLS_X + 1) * (NUM_CELLS_Y + 1));
}
};
int main() {
GDIWindow win(800, 800);
const auto drawDir = [&win](const vec2& pos, const vec2& dir, const float size){
win.graphics.line((int)pos.x, (int)pos.y, (int)(pos.x + dir.x * size), (int)(pos.y + dir.y * size), 0xFFFFFFFF);
win.graphics.setPixel((int)pos.x, (int)pos.y, 0xFF0000FF);
};
static constexpr size_t CELLS_X = 400;
static constexpr size_t CELLS_Y = 400;
// TODO: no memory leak :)
FluidGrid<CELLS_X, CELLS_Y>* vCurrent = new FluidGrid<CELLS_X, CELLS_Y>;
FluidGrid<CELLS_X, CELLS_Y>* vNext = new FluidGrid<CELLS_X, CELLS_Y>;
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq); // TODO
LARGE_INTEGER lastTime;
QueryPerformanceCounter(&lastTime);
for(size_t x = 0; x < CELLS_X + 1; x++) {
vCurrent->s[0][x] = 0;
vCurrent->s[CELLS_Y][x] = 0;
}
for(size_t y = 0; y < CELLS_Y + 1; y++)
vCurrent->s[y][0] = 0;
for(size_t y = 0; y < CELLS_Y / 8; y++) {
for(size_t x = 0; x < CELLS_X / 8; x++) {
vCurrent->s[CELLS_Y / 3 * 1 + y][CELLS_X / 5 + x] = 0;
vCurrent->s[CELLS_Y / 3 * 2 - y][CELLS_X / 5 + x] = 0;
}
}
// float density = 1000.f;
while(!win.shouldClose()) {
LARGE_INTEGER currentTime;
QueryPerformanceCounter(¤tTime);
const double dt = (currentTime.QuadPart - lastTime.QuadPart) * 1. / freq.QuadPart;
lastTime = currentTime;
std::cout << "dt: " << dt << "s\n";
const size_t NUM_SMOKE_TRAILS = 10;
for(size_t y = CELLS_Y / NUM_SMOKE_TRAILS / 2; y < CELLS_Y; y += CELLS_Y / NUM_SMOKE_TRAILS)
vCurrent->smoke[y][0] = 3.f;
for(size_t y = 1; y < CELLS_Y - 3; y++)
vCurrent->hor[y][1] = 30;
// for(size_t y = 1; y < CELLS_Y - 2; y++)
// vCurrent->vert[y][1] = 0;
// vCurrent->hor[54][5] = 50;
// vCurrent->addVel(0, 9.81f);
vCurrent->solveDivergence(10, 1.9f);
// const size_t iter = 1;
// for(size_t i = 0; i < iter; i++)
// FluidGrid<CELLS_X, CELLS_Y>::update(*vCurrent, *vNext, dt / iter);
vCurrent->extrapolate();
FluidGrid<CELLS_X, CELLS_Y>::update(*vCurrent, *vNext, dt);
FluidGrid<CELLS_X, CELLS_Y>::updateSmoke(*vCurrent, *vNext, dt);
// --- graphics:
for(size_t y = 0; y < win.height; y++) {
for(size_t x = 0; x < win.width; x++) {
const size_t cellX = x * (CELLS_X + 1) / win.width;
const size_t cellY = y * (CELLS_Y + 1) / win.height;
static constexpr auto clamp = [](float v, float min, float max){
return (v < min) ? min : (v > max) ? max : v;
};
const float wall = vCurrent->s[cellY][cellX];
const float smoke = 1.f - clamp(vCurrent->sample(Field::SMOKE, cellX, cellY), 0.f, 1.f);
const uint8_t vX = smoke * wall * (clamp(vCurrent->sample(Field::VEL_X, cellX, cellY) * .01f, -.5f, .5f) * 255 + 128);
const uint8_t vY = smoke * wall * (clamp(vCurrent->sample(Field::VEL_Y, cellX, cellY) * .01f, -.5f, .5f) * 255 + 128);
win.graphics.setPixel(x, y,
0xFF << 24
| vX << 16
| vY << 8
);
// win.graphics.setPixel(x, y, (x % 100 < 50) ? 0xFF00FF00 : 0xFFFF0000);
}
}
if(GetAsyncKeyState('L') & 0x8000) {
for(size_t y = 0; y < CELLS_Y; y++) {
for(size_t x = 0; x < CELLS_X; x++) {
drawDir(
vec2 {
(x + .5f) * win.width / CELLS_X,
(y + .5f) * win.height / CELLS_Y
},
vec2 {
vCurrent->sample(Field::VEL_X, x, y),
vCurrent->sample(Field::VEL_Y, x, y)
},
5.f);
}
}
// for(size_t y = 0; y < CELLS_Y; y++) {
// for(size_t x = 0; x < CELLS_X + 1; x++) {
// const int startX = x * win.width / CELLS_X;
// const int startY = (y + .5f) * win.height / CELLS_Y;
// win.graphics.line(
// startX,
// startY,
// startX + std::min<float>(vCurrent->hor[y][x] * 5.f, 15),
// startY,
// 0xFFFFFFFF
// );
// win.graphics.fillCircle(startX, startY, 1, 0xFFFF00FF);
// }
// }
// for(size_t y = 0; y < CELLS_Y + 1; y++) {
// for(size_t x = 0; x < CELLS_X; x++) {
// const int startX = (x + .5f) * win.width / CELLS_X;
// const int startY = y * win.height / CELLS_Y;
// win.graphics.line(
// startX,
// startY,
// startX,
// startY + std::min<float>(vCurrent->vert[y][x] * 5.f, 15),
// 0xFFFFFFFF
// );
// win.graphics.fillCircle(startX, startY, 1, 0xFFFF00FF);
// }
// }
}
win.updateScreen();
win.pollMsg();
}
return 0;
}