-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.cpp
473 lines (456 loc) · 12.9 KB
/
scene.cpp
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
472
473
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include "cg2demo.h"
#include "scene.h"
#include "math3d.h"
/*
Actual parsing functions.
The pointers will be moved!
if shader is nullptr, nothing is written, but shadersz is still updated.
Use that to validate input and calculate size.
*/
static inline uint8_t consume1(const uint8_t **scene, size_t *scenesz) {
(*scenesz)--;
return *(*scene)++;
}
#define CONSUME1 consume1(scene, scenesz)
static inline void appendstr(const char *str, size_t strsz, char **shader, size_t *shadersz) {
if (shader && *shader) {
memcpy(*shader, str, strsz);
*shader += strsz;
}
*shadersz += strsz;
}
#define APPENDSTR(str) appendstr(str, strlen(str), shader, shadersz)
static inline int parse_num(const uint8_t **scene, size_t *scenesz, char **shader, size_t *shadersz) {
if (*scenesz < 2) {
LOG("No bytes left for NUM");
return 0;
}
uint16_t x = 0;
x |= ((uint16_t)CONSUME1) << 8;
x |= ((uint16_t)CONSUME1) & 0xFF;
// have to clear high bit if not set
// the number is technically a 15-bit number
if (!(x & 0x4000)) {
x = x & 0x7fff;
}
float f = (float)(int16_t)x;
f /= 256.0;
// haha, no snprintf and no ftoa in MSVS
// sure, why not
char strf[1024];
size_t strfsz;
strfsz = sprintf(strf, "%f", f);
appendstr(strf, strfsz, shader, shadersz);
return 1;
}
#define PARSE_NUM do { if (!parse_num(scene, scenesz, shader, shadersz)) return 0; } while (0)
#define PARSE_DF do { if (!parse_df(scene, scenesz, shader, shadersz)) return 0; } while (0)
#define PARSE_GF do { if (!parse_gf(scene, scenesz, shader, shadersz)) return 0; } while (0)
#define PARSE_VF do { if (!parse_vf(scene, scenesz, shader, shadersz)) return 0; } while (0)
static int parse_df(const uint8_t **scene, size_t *scenesz, char **shader, size_t *shadersz);
static int parse_gf(const uint8_t **scene, size_t *scenesz, char **shader, size_t *shadersz);
static int parse_vf(const uint8_t **scene, size_t *scenesz, char **shader, size_t *shadersz);
static const char _comma[] = ", ";
static const char __p[] = "p";
// used for indirection when tiled
static const char *_p = __p;
static const char _dot[] = ".";
static const char _cube[] = "cube";
static const char _tile[] = "tile";
static const char _normalize[] = "normalize";
static const char _lparen[] = "(";
static const char _rparen[] = ")";
static const char _timing[] = "timing";
static const char _int[] = "int";
static const char _multhousand[] = " * 1000";
static int parse_df(const uint8_t **scene, size_t *scenesz, char **shader, size_t *shadersz) {
if (!scenesz || !*scenesz) {
LOG("No bytes left for DF");
return 0;
}
enum distance_func df = (enum distance_func) CONSUME1;
switch (df) {
case DF_CUBE:
APPENDSTR(_cube);
APPENDSTR(_lparen);
APPENDSTR(_p);
APPENDSTR(_comma);
PARSE_VF;
APPENDSTR(_comma);
APPENDSTR("float");
APPENDSTR(_lparen);
PARSE_GF;
APPENDSTR(_rparen);
APPENDSTR(_rparen);
return 1;
case DF_CUBE3:
APPENDSTR(_cube);
APPENDSTR(_lparen);
APPENDSTR(_p);
APPENDSTR(_comma);
PARSE_VF;
APPENDSTR(_comma);
PARSE_VF;
APPENDSTR(_rparen);
return 1;
case DF_CYLINDER_CAP:
APPENDSTR("cylinder_caps");
APPENDSTR(_lparen);
APPENDSTR(_p);
APPENDSTR(_comma);
PARSE_VF;
APPENDSTR(_comma);
PARSE_VF;
APPENDSTR(_comma);
PARSE_GF;
APPENDSTR(_rparen);
return 1;
case DF_TORUS:
APPENDSTR("torus");
APPENDSTR(_lparen);
APPENDSTR(_p);
APPENDSTR(_comma);
PARSE_VF;
APPENDSTR(_comma);
APPENDSTR(_normalize);
APPENDSTR(_lparen);
PARSE_VF;
APPENDSTR(_rparen);
APPENDSTR(_comma);
PARSE_GF;
APPENDSTR(_comma);
PARSE_GF;
APPENDSTR(_rparen);
return 1;
case DF_PLANE:
APPENDSTR("plane");
APPENDSTR(_lparen);
APPENDSTR(_p);
APPENDSTR(_comma);
PARSE_VF;
APPENDSTR(_comma);
APPENDSTR(_normalize);
APPENDSTR(_lparen);
PARSE_VF;
APPENDSTR("))");
return 1;
case DF_SPHERE:
APPENDSTR("sphere");
APPENDSTR(_lparen);
APPENDSTR(_p);
APPENDSTR(_comma);
PARSE_VF;
APPENDSTR(_comma);
PARSE_GF;
APPENDSTR(_rparen);
return 1;
case DF_TILED:
{
// have to change _p to
// tile(_p, VF)
// and call parse_df
const char *oldp = _p;
char *tiledp;
size_t tiledpsz;
const uint8_t *scene_in = *scene;
size_t scenesz_in = *scenesz;
tiledpsz = 0;
if (!parse_vf(&scene_in, &scenesz_in, nullptr, &tiledpsz)) {
return 0;
}
tiledpsz = strlen(_tile) + strlen(_lparen) + strlen(oldp) + strlen(_comma) + tiledpsz + strlen(_rparen);
tiledp = (char *)malloc(tiledpsz + 1);
if (!tiledp) {
LOG("Error malloc");
return 0;
}
_p = tiledp;
const char *parts[] = {_tile, _lparen, oldp, _comma, nullptr};
tiledpsz = 0;
for (int i = 0; parts[i]; i++) {
memcpy(tiledp + tiledpsz, parts[i], strlen(parts[i]));
tiledpsz += strlen(parts[i]);
}
char *tiledp_in = tiledp + tiledpsz;
if (!parse_vf(scene, scenesz, &tiledp_in, &tiledpsz)) {
free(tiledp);
return 0;
}
memcpy(tiledp + tiledpsz, _rparen, strlen(_rparen));
tiledpsz += strlen(_rparen);
tiledp[tiledpsz] = '\0';
LOGF("tiled: %s", _p);
if (!parse_df(scene, scenesz, shader, shadersz)) {
free(tiledp);
return 0;
}
_p = oldp;
free(tiledp);
return 1;
}
case DF_MAX:
APPENDSTR("max");
APPENDSTR(_lparen);
PARSE_DF;
APPENDSTR(_comma);
PARSE_DF;
APPENDSTR(_rparen);
return 1;
case DF_MIN:
APPENDSTR("min");
APPENDSTR(_lparen);
PARSE_DF;
APPENDSTR(_comma);
PARSE_DF;
APPENDSTR(_rparen);
return 1;
case DF_MIX:
APPENDSTR("mixfix");
APPENDSTR(_lparen);
PARSE_DF;
APPENDSTR(_comma);
PARSE_DF;
APPENDSTR(_comma);
PARSE_GF;
APPENDSTR(_rparen);
return 1;
case DF_ORIGINAL_MIX:
APPENDSTR("mix");
APPENDSTR(_lparen);
PARSE_DF;
APPENDSTR(_comma);
PARSE_DF;
APPENDSTR(_comma);
PARSE_GF;
APPENDSTR(_rparen);
return 1;
case DF_NEGATIVE:
APPENDSTR(_lparen);
APPENDSTR("-");
APPENDSTR(_lparen);
PARSE_DF;
APPENDSTR(_rparen);
APPENDSTR(_rparen);
return 1;
}
LOGF("Unknown DF: %x", (unsigned)df);
return 0;
}
static int parse_gf(const uint8_t **scene, size_t *scenesz, char **shader, size_t *shadersz) {
if (!scenesz || !*scenesz) {
LOG("No bytes left for GF");
return 0;
}
uint8_t u8gf = CONSUME1;
// if number bit set, set token to number
enum generic_func gf = (enum generic_func) (u8gf & GF_NUMBER ? GF_NUMBER : u8gf);
switch (gf) {
case GF_NUMBER:
// put back
(*scene)--;
(*scenesz)++;
PARSE_NUM;
return 1;
case GF_CLAMP:
APPENDSTR("clamp");
APPENDSTR(_lparen);
PARSE_GF;
APPENDSTR(_comma);
PARSE_GF;
APPENDSTR(_comma);
PARSE_GF;
APPENDSTR(_rparen);
return 1;
case GF_MAX:
APPENDSTR("max");
APPENDSTR(_lparen);
PARSE_GF;
APPENDSTR(_comma);
PARSE_GF;
APPENDSTR(_rparen);
return 1;
case GF_MIN:
APPENDSTR("min");
APPENDSTR(_lparen);
PARSE_GF;
APPENDSTR(_comma);
PARSE_GF;
APPENDSTR(_rparen);
return 1;
case GF_MIX:
APPENDSTR("mixfix");
APPENDSTR(_lparen);
PARSE_GF;
APPENDSTR(_comma);
PARSE_GF;
APPENDSTR(_comma);
PARSE_GF;
APPENDSTR(_rparen);
return 1;
case GF_ORIGINAL_MIX:
APPENDSTR("mix");
APPENDSTR(_lparen);
PARSE_GF;
APPENDSTR(_comma);
PARSE_GF;
APPENDSTR(_comma);
PARSE_GF;
APPENDSTR(_rparen);
return 1;
case GF_SMOOTH:
APPENDSTR("smoothstep");
APPENDSTR(_lparen);
PARSE_GF;
APPENDSTR(_comma);
PARSE_GF;
APPENDSTR(_comma);
PARSE_GF;
APPENDSTR(_rparen);
return 1;
case GF_TIME:
APPENDSTR(_timing);
APPENDSTR(_lparen);
APPENDSTR(_int);
APPENDSTR(_lparen);
PARSE_GF;
APPENDSTR(_multhousand);
APPENDSTR(_rparen);
APPENDSTR(_rparen);
return 1;
case GF_TIME2:
APPENDSTR(_timing);
APPENDSTR("2");
APPENDSTR(_lparen);
APPENDSTR(_int);
APPENDSTR(_lparen);
PARSE_GF;
APPENDSTR(_multhousand);
APPENDSTR(_rparen);
APPENDSTR(_rparen);
return 1;
case GF_PDOTX:
APPENDSTR(_p);
APPENDSTR(_dot);
APPENDSTR("x");
return 1;
case GF_PDOTY:
APPENDSTR(_p);
APPENDSTR(_dot);
APPENDSTR("y");
return 1;
case GF_PDOTZ:
APPENDSTR(_p);
APPENDSTR(_dot);
APPENDSTR("z");
return 1;
}
LOGF("Unknown GF: %x", (unsigned)gf);
return 0;
}
static int parse_vf(const uint8_t **scene, size_t *scenesz, char **shader, size_t *shadersz) {
if (!scenesz || !*scenesz) {
LOG("No bytes left for VF");
return 0;
}
enum vec_func vf = (enum vec_func) CONSUME1;
switch (vf) {
case VF_VECTOR:
APPENDSTR("vec3");
APPENDSTR(_lparen);
PARSE_GF;
APPENDSTR(_comma);
PARSE_GF;
APPENDSTR(_comma);
PARSE_GF;
APPENDSTR(_rparen);
return 1;
}
LOGF("Unknown VF: %x", (unsigned)vf);
return 0;
}
static const char prologue[] = "float dist_object(vec3 p){return ";
static const char epilogue[] = ";}";
size_t parse_scene(const uint8_t *scene, size_t scenesz, char **shader, size_t *shadersz) {
size_t needed = sizeof(prologue) + sizeof(epilogue) - 2;
char *parsed = nullptr;
const uint8_t *scene_in = scene;
size_t scenesz_in = scenesz;
if (!parse_df(&scene_in, &scenesz_in, nullptr, &needed)) {
LOGF("error parsing scene at byte %d", (int)(scenesz - scenesz_in) - 1);
return 0;
}
if (!shader) {
*shadersz = needed;
return scene - scene_in;
}
scene_in = scene;
scenesz_in = scenesz;
parsed = (char *)malloc(needed);
if (!parsed) {
LOGF("error malloc: %s", strerror(errno));
return 0;
}
needed = sizeof(prologue) + sizeof(epilogue) - 2;
memcpy(parsed, prologue, sizeof(prologue) - 1);
char *parsed_in = parsed + sizeof(prologue) - 1;
if (!parse_df(&scene_in, &scenesz_in, &parsed_in, &needed)) {
LOG("error parsing scene");
return 0;
}
memcpy(parsed_in, epilogue, sizeof(epilogue) - 1);
*shader = parsed;
*shadersz = needed;
return scene - scene_in;
}
int split_scenes(const uint8_t *scenes, size_t scenessz, struct scene **parsed, size_t *nparsed) {
const uint8_t *scene_in = scenes;
size_t scenesz_in = scenessz;
size_t n = 0;
struct scene *p;
size_t tmp;
while (scenesz_in > 8) {
scenesz_in -= 8;
scene_in += 8;
if (!parse_df(&scene_in, &scenesz_in, nullptr, &tmp)) {
LOGF("Couldn't parse scene %d", (int)n);
LOGF("At byte %d", (int)(scenessz - scenesz_in) - 1);
return 0;
}
n++;
}
p = (struct scene *)malloc(n * sizeof(struct scene));
if (!p) {
LOG("error malloc");
return 0;
}
scene_in = scenes;
scenesz_in = scenessz;
n = 0;
while (scenesz_in > 8) {
float tx = (int16_t)(scene_in[0] * 256 + scene_in[1]) / 256.0f;
float ty = (int16_t)(scene_in[2] * 256 + scene_in[3]) / 256.0f;
float tz = (int16_t)(scene_in[4] * 256 + scene_in[5]) / 256.0f;
unsigned duration = (unsigned)(scene_in[6] * 256 + scene_in[7]);
LOGF("Scene %d %f %f %f %d", (int)n, tx, ty, tz, (int)duration);
p[n].camera_translation = mkv3(tx, ty, tz);
p[n].duration = duration;
scenesz_in -= 8;
scene_in += 8;
p[n].data = scene_in;
size_t oldsz = scenesz_in;
if (!parse_df(&scene_in, &scenesz_in, nullptr, &tmp)) {
LOGF("Couldn't parse scene %d", (int)n);
free(p);
return 0;
}
p[n].datasz = oldsz - scenesz_in;
++n;
}
*parsed = p;
*nparsed = n;
return 1;
}