-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2019_12_vis.c
executable file
·309 lines (268 loc) · 8.58 KB
/
aoc2019_12_vis.c
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
#if 0 // self-compiling code
gcc -std=c99 -Wall -Wextra -pedantic -Wno-missing-field-initializers -Werror -g -O2 $0 -lglut -lGLU -lGL -lm || exit 1
exec ./a.out
#endif
// n-body trajectory visualization modeled after AoC 2019 day 12
// - uses old-school OpenGL 1.0 and GLUT
// - rotate view by clicking and dragging with the mouse
// - zoom in/out with the mouse wheel
// - press 1,2,3,...,9,0 to select speed and trail length
// - press space to pause
#include <stdio.h>
#include <math.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>
#define NBODY 4
typedef struct _body {
int p[3]; // position
int v[3]; // velocity
} body_t;
body_t bodies[NBODY] = {
#if 0 // first example from puzzle description
// (the one that repeats at t=2772)
{{ -1, 0, 2 }},
{{ 2, -10, -7 }},
{{ 4, -8, 8 }},
{{ 3, 5, -1 }},
#endif
#if 1 // second example from puzzle description
{{ -8, -10, 0 }},
{{ 5, 5, 10 }},
{{ 2, -7, 3 }},
{{ 9, -8, -3 }},
#endif
};
#define START_PAUSED 0
#define WIDE_LINES 0
const float bodyColors[NBODY][3] = {
{ 1.00f, 0.00f, 0.00f },
{ 0.25f, 0.75f, 0.00f },
{ 0.00f, 0.50f, 1.00f },
{ 1.00f, 0.75f, 0.00f },
};
double t = 0.0;
double speed = START_PAUSED ? 0.0 : 32.0;
double camDist = 100.0;
double camAngle = 0.75;
double camHeight = 0.375;
double radius = 1.0;
double trailLength = 96.0;
#define MAX_TRAIL_LENGTH 256
#if MAX_TRAIL_LENGTH & (MAX_TRAIL_LENGTH - 1)
#error MAX_TRAIL_LENGTH must be a power of two
#endif
static int trail[NBODY][MAX_TRAIL_LENGTH][3];
static int trailIndex = 0;
#define TM(x) ((x) & (MAX_TRAIL_LENGTH - 1))
static int screenWidth, screenHeight;
void onInit(void) {
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glClearColor(0.0625f, 0.0625f, 0.0625f, 0.0f);
glLineWidth(1.0 + WIDE_LINES);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_LIGHT0);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, (GLfloat[4]){0.25f, 0.25, 0.25f, 1.0f});
glLightfv(GL_LIGHT0, GL_DIFFUSE, (GLfloat[4]){1.0f, 1.0f, 1.0f, 1.0f});
glLightfv(GL_LIGHT0, GL_SPECULAR, (GLfloat[4]){1.0f, 1.0f, 1.0f, 1.0f});
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (GLfloat[4]){1.0f, 1.0f, 1.0f, 1.0f});
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64.0f);
for (int n = 0; n < NBODY; ++n) {
for (int c = 0; c < 3; ++c) {
trail[n][0][c] = bodies[n].p[c];
}
}
}
static void drawText(int x, int y, const char *str) {
glRasterPos2i(x, y);
while (*str) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *str++);
}
}
void onDraw(void) {
while (t > trailIndex) {
trailIndex++;
for (int a = 0; a < NBODY; ++a) {
for (int b = 0; b < NBODY; ++b) {
for (int c = 0; c < 3; ++c) {
int pa = bodies[a].p[c];
int pb = bodies[b].p[c];
bodies[a].v[c] += (pa < pb) - (pa > pb);
}
}
}
for (int n = 0; n < NBODY; ++n) {
for (int c = 0; c < 3; ++c) {
trail[n][TM(trailIndex)][c] =
(bodies[n].p[c] += bodies[n].v[c]);
}
}
}
static GLUquadricObj* quadric = NULL;
if (!quadric) {
quadric = gluNewQuadric();
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)screenWidth / (double)screenHeight, 1.0, 1024.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(
camDist * cos(camHeight) * sin(camAngle),
camDist * cos(camHeight) * cos(camAngle),
camDist * sin(camHeight),
0.0, 0.0, 0.0,
0.0, 0.0, 1.0
);
glLightfv(GL_LIGHT0, GL_POSITION, (GLfloat[4]){1024.0f, -1024.0f, 2048.0f, 1.0f});
glDisable(GL_DEPTH_TEST);
glBegin(GL_LINES);
#define A 96
#define B 64
glColor4ub(0, 0, 0, 255); glVertex3i(-1024, 0, 0);
glColor4ub(A, B, B, 255); glVertex3i(0, 0, 0); glVertex3i(0, 0, 0);
glColor4ub(0, 0, 0, 255); glVertex3i( 1024, 0, 0);
glColor4ub(0, 0, 0, 255); glVertex3i(0, -1024, 0);
glColor4ub(B, A, B, 255); glVertex3i(0, 0, 0); glVertex3i(0, 0, 0);
glColor4ub(0, 0, 0, 255); glVertex3i(0, 1024, 0);
glColor4ub(0, 0, 0, 255); glVertex3i(0, 0, -1024);
glColor4ub(B, B, A, 255); glVertex3i(0, 0, 0); glVertex3i(0, 0, 0);
glColor4ub(0, 0, 0, 255); glVertex3i(0, 0, 1024);
glEnd();
glEnable(GL_DEPTH_TEST);
#if 0
glEnable(GL_LIGHTING);
glColor4ub(255, 255, 240, 255);
glRotated(90.0, 1.0, 0.0, 0.0);
glutSolidTeapot(20.0);
glDisable(GL_LIGHTING);
#endif
float tFrac = (float)(trailIndex - t);
float alphaScale = (float) (1.0 / (trailLength - 1.0));
for (int n = 0; n < NBODY; ++n) {
float pos[3];
const float *col = &bodyColors[n][0];
for (int c = 0; c < 3; ++c) {
float a = (float)trail[n][TM(trailIndex)][c];
float b = (float)trail[n][TM(trailIndex - 1)][c];
pos[c] = a + tFrac * (b - a);
}
glColor4f(col[0], col[1], col[2], 1.0f);
glEnable(GL_LIGHTING);
glPushMatrix();
glTranslatef(pos[0], pos[1], pos[2]);
gluSphere(quadric, radius, 16, 8);
glPopMatrix();
glDisable(GL_LIGHTING);
glEnable(GL_BLEND);
glBegin(GL_LINE_STRIP);
glVertex3fv(pos);
for (int i = 1; (i <= trailIndex) && (i < MAX_TRAIL_LENGTH); ++i) {
float trailPos = i - tFrac;
float alpha = 1.0f - trailPos * alphaScale;
if (alpha < 0.0f) { alpha = 0.0f; }
alpha *= alpha * alpha;
glColor4f(col[0], col[1], col[2], alpha);
glVertex3iv(&trail[n][TM(trailIndex - i)][0]);
if (trailPos > (float)trailLength) { break; }
}
glEnd();
glDisable(GL_BLEND);
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, screenWidth, screenHeight, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor4ub(255, 255, 255, 255);
char s[40];
sprintf(s, "speed: %.0f/s", speed);
drawText(10, 20, s);
sprintf(s, "t=%.2f", t);
drawText(10, 40, s);
glutSwapBuffers();
}
void onIdle(void) {
static double tLast = -1.0;
double tNow = glutGet(GLUT_ELAPSED_TIME) * 0.001;
if (tLast >= 0.0) {
t += (tNow - tLast) * speed;
}
tLast = tNow;
glutPostRedisplay();
}
void onResize(int w, int h) {
glViewport(0, 0, w, h);
screenWidth = w;
screenHeight = h;
}
void onKey(unsigned char c, int x, int y) {
(void)x,(void)y;
switch (c) {
case 'q':
glutLeaveMainLoop();
break;
case ' ': {
static double pausedSpeed = 0.0;
if (speed > 0.0) {
pausedSpeed = speed;
speed = 0.0;
} else {
speed = pausedSpeed;
}
break; }
default:
if ((c >= '0') && (c <= '9')) {
int digit = c - '0';
if (!digit) { digit = 10; }
speed = exp2(digit - 3);
trailLength = speed * 3.0 + 2.0;
}
break;
}
}
static int m_x0, m_y0;
static double m_a0, m_h0;
void onMouseWheel(int wheel, int direction, int x, int y) {
(void)wheel, (void)x, (void)y;
camDist += direction * 10.0;
if (camDist < 1.0) { camDist = 1.0; }
}
void onMouseClick(int button, int state, int x, int y) {
if (state != GLUT_DOWN) { return; }
switch (button) {
case GLUT_LEFT_BUTTON:
m_x0 = x; m_a0 = camAngle;
m_y0 = y; m_h0 = camHeight;
break;
case 3: onMouseWheel(0, -1, x, y); break;
case 4: onMouseWheel(0, +1, x, y); break;
default:
break;
}
}
void onMouseMove(int x, int y) {
camAngle = m_a0 + (x - m_x0) * 0.01;
camHeight = m_h0 + (y - m_y0) * 0.01;
if (camHeight < -1.5) { camHeight = -1.5; }
if (camHeight > 1.5) { camHeight = 1.5; }
}
int main(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowSize(1280, 720);
glutCreateWindow("AoC 2019 day 12");
glutReshapeFunc(onResize);
glutDisplayFunc(onDraw);
glutIdleFunc(onIdle);
glutKeyboardFunc(onKey);
glutMouseFunc(onMouseClick);
glutMotionFunc(onMouseMove);
glutMouseWheelFunc(onMouseWheel);
onInit();
glutMainLoop();
return 0;
}