-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgrid.cpp
More file actions
471 lines (371 loc) · 12.9 KB
/
grid.cpp
File metadata and controls
471 lines (371 loc) · 12.9 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//
// grid.cpp
// Initial sample grid code from http://www.gamedev.net/topic/392830-mass-spring-grid-problem/#entry3611422
// Greatly expanded upon (added attractors, non-rectangular grids, etc) by Peter Hirschberg
//
#include "attractor.hpp"
#include "game.hpp"
#include "grid.hpp"
#include "mathutils.hpp"
#include "point3d.hpp"
#include "profiler.hpp"
#include "settings.hpp"
#include <atomic>
//#include <mutex>
#include <vector>
#include <cstdio>
#include <SDL3/SDL_opengl.h>
// The Grid
// GW is 33w x 22h
const int grid::resolution_x = ((33 * 4) + 1);
const int grid::resolution_y = ((22 * 4) + 1);
// Stuff for our OpenGL grid arrays
struct Vertex
{
GLfloat x = 0.0f;
GLfloat y = 0.0f;
GLfloat r = 0.0f;
GLfloat g = 0.0f;
GLfloat b = 0.0f;
GLfloat a = 0.0f;
};
static std::vector<Vertex> gridVertices;
static std::vector<GLushort> gridElements;
static const unsigned int numGridLinesX = grid::resolution_x * grid::resolution_y;
static const unsigned int numGridLinesY = numGridLinesX;
/*
static SDL_mutex* game::mAttractors.mMutex;
static SDL_mutex* mDrawMutex;
*/
static SDL_Thread* mRunThread = nullptr;
static std::atomic_bool mRunFlag { false };
static std::atomic_bool quitFlag { false };
static float q;
static float damping;
static const float dt = .3;
struct GridPoint
{
Point3d pos;
Point3d vel;
};
static std::vector<GridPoint> mGrid;
// static std::mutex m;
// #define GRID_GLOW // PERFORMANCE: Making the grid glow causes us to have to draw it twice, which is slower. This is also defined in game.cpp!
static int runThread(void* /*ptr*/)
{
profiler prof("grid");
printf("Grid thread running\n");
while (!quitFlag) {
while (!mRunFlag && !quitFlag) {
SDL_Delay(1);
}
mRunFlag = false;
prof.start();
// TODO: fix data races
// std::unique_lock<std::mutex> lock(m);
// Apply attractors
for (attractor::Attractor& att: theGame->mAttractors->mAttractors) {
if (att.enabled) {
// Evaluate every point on the grid for this attractor
const int xstart = 1;
const int ystart = 1;
const int xend = grid::resolution_x - 2;
const int yend = grid::resolution_y - 2;
const Point3d& apoint = att.pos;
const float arSquared = att.radius * att.radius;
for (int y = ystart; y <= yend; ++y) {
GridPoint* p = &mGrid[y * grid::resolution_x];
for (int x = xstart; x <= xend; ++x) {
// Point3d gpoint = (gridxy(x,y).pos + gridxy(x,y).center) * .5;
p++;
const Point3d& gpoint = p->pos;
const float distance = mathutils::calculate2dDistanceSquared(gpoint, apoint);
if (distance < arSquared && distance > 0.0f) {
// distance = (distance*distance); // Simulate gravity with distance squared
const float angle = mathutils::calculate2dAngle(gpoint, apoint);
const float strength = att.strength;
const Point3d gravityVector(-distance * strength, 0.0f, 0.0f);
const Point3d g = mathutils::rotate2dPoint(gravityVector, angle);
p->vel.x += g.x * .005f;
p->vel.y += g.y * .005f;
}
}
}
// Now that we've processed this attractor we can clear it out
att.enabled = false;
}
}
const float accel_c = -q * dt;
const float damping_multiplier = exp(-dt * damping);
// Run the grid
for (int pass = 0; pass < settings::get().mGridPasses; pass++) {
for (int y = 1; y < grid::resolution_y - 1; ++y) {
GridPoint* p = &mGrid[y * grid::resolution_x];
for (int x = 1; x < grid::resolution_x - 1; ++x) {
p++;
// Weigh against neighbors
const Point3d& p1 = (p - 1)->pos;
const Point3d& p2 = (p + 1)->pos;
const Point3d& p3 = (p - grid::resolution_x)->pos;
const Point3d& p4 = (p + grid::resolution_x)->pos;
// Average the point
// avg_pos = (p1+p2+p3+p4) * .25;
const Point3d avg_pos((p1.x + p2.x + p3.x + p4.x) * 0.25f,
(p1.y + p2.y + p3.y + p4.y) * 0.25f,
0.0f);
p->vel += (p->pos - avg_pos) * accel_c;
p->vel *= damping_multiplier;
p->pos += p->vel * dt;
// Keep the points in bounds
if (p->pos.x < 0)
p->pos.x = 0;
else if (p->pos.x > grid::resolution_x - 1)
p->pos.x = grid::resolution_x - 1;
if (p->pos.y < 0)
p->pos.y = 0;
else if (p->pos.y > grid::resolution_y - 1)
p->pos.y = grid::resolution_y - 1;
}
}
}
prof.stop();
}
printf("Grid thread exiting\n");
return 0;
}
grid::grid()
{
mGrid.resize(resolution_x * resolution_y);
q = 12;
damping = 1.5f;
// Create our array of grid points
for (int y = 0; y < resolution_y; ++y) {
for (int x = 0; x < resolution_x; ++x) {
GridPoint& p = mGrid[x + y * grid::resolution_x];
p.pos = Point3d(x, y, 0);
p.vel = Point3d(0, 0, 0);
}
}
// Create our OpenGL vertex and color array
gridVertices.reserve(resolution_x * resolution_y);
gridElements.reserve(numGridLinesX + numGridLinesY);
initializeVertices();
initializeElements();
// Thread stuff
mRunThread = SDL_CreateThread(runThread, "grid", nullptr);
if (!mRunThread) {
printf("Couldn't create grid run thread: %s\n", SDL_GetError());
}
}
grid::~grid()
{
quitFlag = true;
int status = 0;
SDL_WaitThread(mRunThread, &status);
printf("Grid thread exited with status %d\n", status);
}
void grid::initializeVertices()
{
// Init the grid line colors up front so it only has to happen once
#ifdef GRID_GLOW
const vector::pen darkColor(0.2f, 0.2f, 1.0f, (0.15f * ((scene::mPass == scene::RENDERPASS_PRIMARY) ? 0.75f : 0.25f)) * brightness, 0);
const vector::pen lightColor(0.2f, 0.2f, 1.0f, (0.4f * ((scene::mPass == scene::RENDERPASS_PRIMARY) ? 0.75f : 0.25f)) * brightness, 0);
#else
const vector::pen darkColor(0.4f, 0.4f, 1.0f, 0.15f * brightness, 0);
const vector::pen lightColor(0.4f, 0.4f, 1.0f, 0.4f * brightness, 0);
#endif
// Vertices for dark lines
for (int i = 0; i < resolution_x * resolution_y; i++) {
Vertex v {};
v.r = darkColor.r;
v.g = darkColor.g;
v.b = darkColor.b;
v.a = darkColor.a;
gridVertices.push_back(v);
}
lightStartHorizontal = gridVertices.size();
// Horizontal light lines
for (int y = 0; y < resolution_y; y += 4) {
for (int x = 0; x < resolution_x; x++) {
Vertex v {};
v.r = lightColor.r;
v.g = lightColor.g;
v.b = lightColor.b;
v.a = lightColor.a;
gridVertices.push_back(v);
}
}
lightStartVertical = gridVertices.size();
// Vertical light lines
for (int x = 0; x < resolution_x; x+= 4) {
for (int y = 0; y < resolution_y; y++) {
Vertex v {};
v.r = lightColor.r;
v.g = lightColor.g;
v.b = lightColor.b;
v.a = lightColor.a;
gridVertices.push_back(v);
}
}
}
void grid::initializeElements()
{
// Horizontal dark
for (int y = 0; y < resolution_y; y++) {
GLuint vertex = y * resolution_x;
for (int x = 0; x < resolution_x - 1; x++) {
if (y % 4 > 0) {
gridElements.push_back(vertex++);
gridElements.push_back(vertex);
}
}
}
// Vertical dark
for (int x = 0; x < resolution_x; x++) {
GLuint vertex = x;
for (int y = 0; y < resolution_y - 1; y++) {
if (x % 4 > 0) {
gridElements.push_back(vertex);
vertex += resolution_x;
gridElements.push_back(vertex);
}
}
}
// Horizontal light
for (int y = 0; y < resolution_y; y++) {
GLuint vertex = lightStartHorizontal + y * resolution_x / 4;
for (int x = 0; x < resolution_x - 1; x++) {
if (y % 4 == 0) {
gridElements.push_back(vertex++);
gridElements.push_back(vertex);
}
}
}
// Vertical light
for (int x = 0; x < resolution_x; x++) {
GLuint vertex = lightStartVertical + x * resolution_y / 4;
for (int y = 0; y < resolution_y - 1; y++) {
if (x % 4 == 0) {
gridElements.push_back(vertex++);
gridElements.push_back(vertex);
}
}
}
}
void grid::run()
{
mRunFlag = true;
}
static profiler prof1("grid_draw_update");
static profiler prof2("grid_draw_opengl");
void grid::draw()
{
if (brightness <= 0.05f)
return;
// std::unique_lock<std::mutex> lock(m);
prof1.start();
glLineWidth(5.0f);
std::size_t vertex = 0;
// Update dark lines positions
for (int y = 0; y < resolution_y; ++y) {
GridPoint* p = &mGrid[y * resolution_x];
for (int x = 0; x < resolution_x; ++x) {
gridVertices[vertex].x = p->pos.x;
gridVertices[vertex].y = p->pos.y;
vertex++;
p++;
}
}
vertex = lightStartHorizontal;
// Update horizontal light lines
for (int y = 0; y < resolution_y; y += 4) {
GridPoint* p = &mGrid[y * resolution_x];
for (int x = 0; x < resolution_x; x++) {
gridVertices[vertex].x = p->pos.x;
gridVertices[vertex].y = p->pos.y;
vertex++;
p++;
}
}
vertex = lightStartVertical;
// Update vertical light lines
for (int x = 0; x < resolution_x; x += 4) {
GridPoint* p = &mGrid[x];
for (int y = 0; y < resolution_y; y++) {
gridVertices[vertex].x = p->pos.x;
gridVertices[vertex].y = p->pos.y;
vertex++;
p += resolution_x;
}
}
prof1.stop();
prof2.start();
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
// Draw lines
glVertexPointer(2, GL_FLOAT, sizeof(Vertex), gridVertices.data());
glColorPointer(4, GL_FLOAT, sizeof(Vertex), ((char*)gridVertices.data() + 2 * sizeof(GLfloat)));
glDrawElements(GL_LINES, gridElements.size(), GL_UNSIGNED_SHORT, gridElements.data());
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
prof2.stop();
// Grid outline
glColor4f(1.0f, 1.0f, 1.0f, 1.0f); // RGBA
glLineWidth(4.0f);
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(grid::resolution_x - 1, 0.0f, 0.0f);
glVertex3f(grid::resolution_x - 1, grid::resolution_y - 1, 0.0f);
glVertex3f(0.0f, grid::resolution_y - 1, 0.0f);
glEnd();
// If the brightness has been lowered, cover the grid with a semi transparent scrim
// since all our grid colors are locked.
if (brightness < 0.99f) {
glColor4f(0.0f, 0.0f, 0.0f, 1 - brightness);
glBlendFunc(GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_QUADS);
glVertex3f(-10.0f, -10.0f, 0.0f);
glVertex3f(grid::resolution_x + 10, -10.0f, 0.0f);
glVertex3f(grid::resolution_x + 10, grid::resolution_y + 10, 0.0f);
glVertex3f(-10.0f, grid::resolution_y + 10, 0.0f);
glEnd();
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
}
}
bool grid::hitTest(const Point3d& pos, float radius, Point3d* hitPos, Point3d* speed)
{
bool hit = false;
if (hitPos)
*hitPos = pos;
const float left = 0 + radius;
const float bottom = 0 + radius;
const float right = resolution_x - radius;
const float top = resolution_y - radius;
if (pos.x < left) {
if (hitPos)
hitPos->x = left;
if (speed)
speed->x = -speed->x;
hit = true;
} else if (pos.x > right - 1) {
if (hitPos)
hitPos->x = right - 1;
if (speed)
speed->x = -speed->x;
hit = true;
}
if (pos.y < bottom) {
if (hitPos)
hitPos->y = bottom;
if (speed)
speed->y = -speed->y;
hit = true;
} else if (pos.y > top - 1) {
if (hitPos)
hitPos->y = top - 1;
if (speed)
speed->y = -speed->y;
hit = true;
}
return hit;
}