-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
1268 lines (1110 loc) · 41.6 KB
/
main.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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
James William Fletcher (github.com/mrbid)
November 2022
!! this was an attempt to add extrapolation
but unless the server tells you client update
latencies there is only so much you can infer
from their position change during your update
latency with the server. It's not very good.
I = Toggle Extrapolation, default: off
To reduce file size the icosphere could be
generated on program execution by subdividing
a icosahedron and then snapping the points to
a unit sphere; and producing an index buffer
for each triangle from each subdivision.
Get current epoch: date +%s
Start online game: ./fat <msaa> <future epoch time>
Every player will start at the same epoch and the
game simulation should run exactly the same across
all systems with a small amount of deviation over
time due to the accuracy of dt (unavoidable) and
the start time accuracy that hangs on the precision
of the microtime function.
Players only transmit a single registration and then
their positions as a vec3 in byte format.
Very simple, low bandwidth, requires only a central
server, no port forwarding required other than port 80
on the server or port 443 for https, but this is set
to use http (80) as default.
I was tempted to go for an IPv4 P2P model, but with the
state of the web these days, most small games are expected
to utilise http if they want to eventually run in a
browser, and you skip all the IPv4/IPv6 mess.
Game is limited to 60 FPS because it seemed like a nice
median for all players to keep up with while keeping a
smooth experience and not thrashing the http protocol
too badly. This could be decoupled from the rendering
but there's not a lot of point other than increasing
the smoothness of view panning and that's not bad at
60 FPS anyway. It's more efficient to keep them coupled.
But the latency of the rendering functions could have an
impact on the deviation of the simulation over time.
All players have to stick to the same FPS to minimise the
deviance of the simulation over time.
gcc main.c glad_gl.c -I inc -Ofast -lglfw -lpthread -lcurl -lm -o fat
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/file.h>
#include <unistd.h>
#include <curl/curl.h>
CURL *curl;
#pragma GCC diagnostic ignored "-Wunused-result"
#define uint GLuint
#define sint GLint
#define f32 GLfloat
#include "inc/gl.h"
#define GLFW_INCLUDE_NONE
#include "inc/glfw3.h"
#ifndef __x86_64__
#define NOSSE
#endif
#define SEIR_RAND
#include "inc/esAux2.h"
#include "inc/res.h"
#include "assets/exo.h"
#include "assets/rocks.h"
//*************************************
// globals
//*************************************
GLFWwindow* window;
uint winw = 1024;
uint winh = 768;
double t = 0; // time
f32 dt = 0; // delta time
double fc = 0; // frame count
double lfct = 0;// last frame count time
f32 aspect;
double rww, ww, rwh, wh, ww2, wh2;
double uw, uh, uw2, uh2; // normalised pixel dpi
double x,y;
// render state id's
GLint projection_id;
GLint modelview_id;
GLint position_id;
GLint lightpos_id;
GLint color_id;
GLint opacity_id;
GLint normal_id; //
// render state matrices
mat projection;
mat view;
mat model;
mat modelview;
// models
ESModel mdlMenger;
ESModel mdlExo;
ESModel mdlInner;
ESModel mdlRock[9];
// camera vars
#define FAR_DISTANCE 10000.f
vec lightpos = {0.f, 0.f, 0.f};
uint focus_cursor = 0;
double sens = 0.001;
f32 xrot = 0.f, yrot = 0.f;
// game vars
#define GFX_SCALE 0.01f
#define MOVE_SPEED 0.5f
#define MIN_UPDATE_TIME_US 10000
#define UPDATE_TIMEOUT_MS 1000
uint keystate[8] = {0};
vec pp = {0.f, 0.f, 0.f};
vec ppr = {0.f, 0.f, -2.3f};
uint hits = 0;
uint popped = 0;
uint brake = 0;
uint damage = 0;
time_t sepoch = 0;
unsigned short uid = 0;
uint autoroll = 1;
#define MAX_PLAYERS 31
float players[MAX_PLAYERS*3] = {0};
float players_vel[MAX_PLAYERS*3] = {0};
uint64_t interp_et = 0;
float interp_rdt = 0;
uint interp = 0;
typedef struct
{
vec dir, pos;
f32 rot, scale, speed;
} comet;
#define NUM_COMETS 64
comet comets[NUM_COMETS];
//*************************************
// utility functions
//*************************************
void timestamp(char* ts)
{
const time_t tt = time(0);
strftime(ts, 16, "%H:%M:%S", localtime(&tt));
}
uint64_t microtime()
{
struct timeval tv;
struct timezone tz;
memset(&tz, 0, sizeof(struct timezone));
gettimeofday(&tv, &tz);
return 1000000 * tv.tv_sec + tv.tv_usec;
}
unsigned short urand16()
{
int f = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
unsigned short s = 0;
read(f, &s, sizeof(unsigned short));
close(f);
return s;
}
void scaleBuffer(GLfloat* b, GLsizeiptr s)
{
for(GLsizeiptr i = 0; i < s; i++)
b[i] *= GFX_SCALE;
}
void doExoImpact(vec p, float f)
{
//if(f < 0.003793040058F){return;}
const GLsizeiptr s = exo_numvert*3;
for(GLsizeiptr i = 0; i < s; i+=3)
{
vec v = {exo_vertices[i], exo_vertices[i+1], exo_vertices[i+2]};
f32 ds = vDistSq(v, p);
if(ds < f*f)
{
ds = vDist(v, p);
vNorm(&v);
const f32 sr = f-ds;
vMulS(&v, v, sr);
if(sr > 0.03f){damage++;}
exo_vertices[i] -= v.x;
exo_vertices[i+1] -= v.y;
exo_vertices[i+2] -= v.z;
exo_colors[i] -= 0.2f;
exo_colors[i+1] -= 0.2f;
exo_colors[i+2] -= 0.2f;
}
}
esRebind(GL_ARRAY_BUFFER, &mdlExo.vid, exo_vertices, exo_vertices_size, GL_STATIC_DRAW);
esRebind(GL_ARRAY_BUFFER, &mdlExo.cid, exo_colors, exo_colors_size, GL_STATIC_DRAW);
}
void randComet(uint i)
{
vRuvBT(&comets[i].pos);
vMulS(&comets[i].pos, comets[i].pos, 10.f);
vec dp;
vRuvTA(&dp);
vSub(&comets[i].dir, comets[i].pos, dp);
vNorm(&comets[i].dir);
vInv(&comets[i].dir);
vec of = comets[i].dir;
vInv(&of);
vMulS(&of, of, randf()*6.f);
vAdd(&comets[i].pos, comets[i].pos, of);
comets[i].rot = randf()*300.f;
comets[i].scale = 0.01f+(randf()*0.07f);
comets[i].speed = 0.16f+(randf()*0.08f);
}
void randComets()
{
for(uint i = 0; i < NUM_COMETS; i++)
randComet(i);
}
void incrementHits()
{
hits++;
char title[256];
const uint max_damage = exo_numvert/2;
sprintf(title, "Online Fractal Attack Lite | %u/%u | %.2f%% | %.2f mins", hits, popped, (100.f/(float)max_damage)*(float)damage, (time(0)-sepoch)/60.0);
glfwSetWindowTitle(window, title);
if(damage >= max_damage)
{
for(uint i = 0; i < NUM_COMETS; i++)
{
comets[i].speed = -1.f;
comets[i].rot = 0.f;
}
sprintf(title, "Online Fractal Attack Lite | %u/%u | 100%% | %.2f mins | GAME END", hits, popped, (time(0)-sepoch)/60.0);
glfwSetWindowTitle(window, title);
}
}
static size_t cb(void *data, size_t size, size_t nmemb, void *p)
{
//if(nmemb > 372){nmemb = 372;}
if(nmemb > 11 && nmemb <= 372){memcpy(&players, data, nmemb);}
return 0;
}
void curlUpdateGame(const time_t sepoch, const unsigned short uid)
{
// might want to add a cache buster to the url
unsigned char d[12];
memcpy(&d, (unsigned char*)&ppr, 12);
char url[256];
sprintf(url, "https://fractalattack.repl.co/?r=%lu&u=%hu&p=%%%02X%%%02X%%%02X%%%02X%%%02X%%%02X%%%02X%%%02X%%%02X%%%02X%%%02X%%%02X", sepoch, uid, d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], d[8], d[9], d[10], d[11]);
//printf("%s\n", url);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, UPDATE_TIMEOUT_MS);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb);
curl_easy_perform(curl);
}
void curlRegisterGame(const time_t sepoch, const unsigned short uid)
{
// might want to add a cache buster to the url
char url[256];
sprintf(url, "https://fractalattack.repl.co/?r=%lu&u=%hu", sepoch, uid);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, time(0)-sepoch);
FILE *devnull = fopen("/dev/null", "w+");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, devnull);
curl_easy_perform(curl);
fclose(devnull);
}
void *netThread(void *arg)
{
float prevel[MAX_PLAYERS*3] = {0};
while(1)
{
if(comets[0].speed == -1.f)
{
printf("netThread: quit, end game.\n");
return 0;
}
if(interp == 1)
{
for(uint i = 0; i < MAX_PLAYERS; i++)
{
const uint j = i*3;
prevel[j] = players[j];
prevel[j+1] = players[j+1];
prevel[j+2] = players[j+2];
}
}
const uint64_t last_update = microtime();
curlUpdateGame(sepoch, uid);
const uint64_t this_time = microtime();
const uint64_t delta_time = this_time-last_update;
if(interp == 1)
{
interp_rdt = 1.f/(float)delta_time;
interp_et = microtime()+delta_time;
for(uint i = 0; i < MAX_PLAYERS; i++)
{
const uint j = i*3;
players_vel[j] = prevel[j] - players[j];
players_vel[j+1] = prevel[j+1] - players[j+1];
players_vel[j+2] = prevel[j+2] - players[j+2];
vec v = (vec){players_vel[j], players_vel[j+1], players_vel[j+2]};
if(vMod(v) > 0.0001f)
{
vInv(&v);
players_vel[j] = v.x;
players_vel[j+1] = v.y;
players_vel[j+2] = v.z;
}
else
{
players_vel[j] = 0.f;
players_vel[j+1] = 0.f;
players_vel[j+2] = 0.f;
}
}
}
if(interp == 0 && delta_time < MIN_UPDATE_TIME_US) // was delta time faster than MIN_UPDATE_TIME?
usleep(MIN_UPDATE_TIME_US - delta_time); // it was so sleep for the remainder of MIN_UPDATE_TIME
}
}
//*************************************
// update & render
//*************************************
void main_loop()
{
//*************************************
// time delta for frame interpolation
//*************************************
static double lt = 0;
if(lt == 0){lt = t;}
dt = t-lt;
lt = t;
//*************************************
// keystates
//*************************************
static f32 zrot = 0.f;
if(keystate[2] == 1) // W
{
vec vdc = (vec){view.m[0][2], view.m[1][2], view.m[2][2]};
vec m;
vMulS(&m, vdc, MOVE_SPEED * dt);
vAdd(&pp, pp, m);
}
else if(keystate[3] == 1) // S
{
vec vdc = (vec){view.m[0][2], view.m[1][2], view.m[2][2]};
vec m;
vMulS(&m, vdc, MOVE_SPEED * dt);
vSub(&pp, pp, m);
}
if(keystate[0] == 1) // A
{
vec vdc = (vec){view.m[0][0], view.m[1][0], view.m[2][0]};
vec m;
vMulS(&m, vdc, MOVE_SPEED * dt);
vAdd(&pp, pp, m);
}
else if(keystate[1] == 1) // D
{
vec vdc = (vec){view.m[0][0], view.m[1][0], view.m[2][0]};
vec m;
vMulS(&m, vdc, MOVE_SPEED * dt);
vSub(&pp, pp, m);
}
if(keystate[4] == 1) // SPACE
{
vec vdc = (vec){view.m[0][1], view.m[1][1], view.m[2][1]};
vec m;
vMulS(&m, vdc, MOVE_SPEED * dt);
vSub(&pp, pp, m);
}
else if(keystate[5] == 1) // SHIFT
{
vec vdc = (vec){view.m[0][1], view.m[1][1], view.m[2][1]};
vec m;
vMulS(&m, vdc, MOVE_SPEED * dt);
vAdd(&pp, pp, m);
}
if(brake == 1)
vMulS(&pp, pp, 0.99f*(1.f-dt));
vec ppi = pp;
vMulS(&ppi, ppi, dt);
vAdd(&ppr, ppr, ppi);
const f32 pmod = vMod(ppr);
if(autoroll == 1 && pmod < 2.f) // self-righting
{
vec dve = (vec){view.m[0][0], view.m[1][0], view.m[2][0]};
vec dvp = ppr;
vInv(&dvp);
vNorm(&dvp);
const f32 ta = vDot(dve, dvp);
if(ta < -0.03f || ta > 0.03f)
{
// const f32 ia = ta*0.01f;
const f32 ia = (ta*0.03f) * ( 1.f - ((pmod - 1.14f) * 1.162790656f) ); // 0.86f
zrot -= ia*dt;
//printf("%f %f %f\n", zrot, ta, pmod);
}
else
zrot = 0.f;
}
else // roll inputs
{
if(brake == 1)
zrot *= 0.99f*(1.f-dt);
if(keystate[6] && !keystate[7]) {
if(zrot > 0.f) {
zrot += dt*sens*10.f;
} else {
zrot *= 1.f-(dt*0.02f);
zrot += dt*sens*10.f;
}
} else if(keystate[7] && !keystate[6]) {
if(zrot < 0.f) {
zrot -= dt*sens*10.f;
} else {
zrot *= 1.f-(dt*0.02f);
zrot -= dt*sens*10.f;
}
} else if(keystate[6] && keystate[7]) {
zrot = 0.f;
} else {
if (zrot > 0.09f || zrot < -0.09f)
zrot *= 1.f-(dt*0.91f);
else if (zrot > 0.001f)
zrot -= 0.001f*dt;
else if (zrot < -0.001f)
zrot += 0.001f*dt;
else
zrot = 0.f;
}
}
if(pmod < 1.13f) // exo collision
{
const f32 hf = vMod(pp)*10.f;
vec n = ppr;
vNorm(&n);
vReflect(&pp, pp, (vec){-n.x, -n.y, -n.z}); // better if I don't normalise pp
vMulS(&pp, pp, 0.3f);
vMulS(&n, n, 1.13f - pmod);
vAdd(&ppr, ppr, n);
}
//*************************************
// camera
//*************************************
// mouse delta to rot
f32 xrot = 0.f, yrot = 0.f;
if(focus_cursor == 1)
{
glfwGetCursorPos(window, &x, &y);
if(x != ww2 || y != wh2)
{
xrot = (ww2-x)*sens;
yrot = (wh2-y)*sens;
glfwSetCursorPos(window, ww2, wh2);
}
}
// Test_User angle-axis rotation
// https://en.wikipedia.org/wiki/Axis%E2%80%93angle_representation
// https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula
vec tmp0, tmp1;
vec vecview[3] = {
{view.m[0][0], view.m[1][0], view.m[2][0]}, // r
{view.m[0][1], view.m[1][1], view.m[2][1]}, // u
{view.m[0][2], view.m[1][2], view.m[2][2]} // f
};
// left/right
vMulS(&tmp0, vecview[0], cosf(xrot));
vCross(&tmp1, vecview[1], vecview[0]);
vMulS(&tmp1, tmp1, sinf(xrot));
vMulS(&vecview[0], vecview[1], vDot(vecview[1], vecview[0]) * (1.f - cosf(xrot)));
vAdd(&vecview[0], vecview[0], tmp0);
vAdd(&vecview[0], vecview[0], tmp1);
// up/down
vMulS(&tmp0, vecview[1], cosf(yrot));
vCross(&tmp1, vecview[0], vecview[1]);
vMulS(&tmp1, tmp1, sinf(yrot));
vMulS(&vecview[1], vecview[0], vDot(vecview[0], vecview[1]) * (1.f - cosf(yrot)));
vAdd(&vecview[1], vecview[1], tmp0);
vAdd(&vecview[1], vecview[1], tmp1);
vCross(&vecview[2], vecview[0], vecview[1]);
vCross(&vecview[1], vecview[2], vecview[0]);
// roll
vMulS(&tmp0, vecview[0], cosf(zrot));
vCross(&tmp1, vecview[2], vecview[0]);
vMulS(&tmp1, tmp1, sinf(zrot));
vMulS(&vecview[0], vecview[2], vDot(vecview[2], vecview[0]) * (1.f - cosf(zrot)));
vAdd(&vecview[0], vecview[0], tmp0);
vAdd(&vecview[0], vecview[0], tmp1);
vCross(&vecview[1], vecview[2], vecview[0]);
vNorm(&vecview[0]);
vNorm(&vecview[1]);
vNorm(&vecview[2]);
view = (mat){
vecview[0].x, vecview[1].x, vecview[2].x, view.m[0][3],
vecview[0].y, vecview[1].y, vecview[2].y, view.m[1][3],
vecview[0].z, vecview[1].z, vecview[2].z, view.m[2][3],
0.f, 0.f, 0.f, view.m[3][3]
};
// translate
mTranslate(&view, ppr.x, ppr.y, ppr.z);
static f32 ft = 0.f;
ft += dt*0.03f;
lightpos.x = sinf(ft) * 6.3f;
lightpos.y = cosf(ft) * 6.3f;
lightpos.z = sinf(ft) * 6.3f;
//*************************************
// render
//*************************************
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
///
shadeLambert2(&position_id, &projection_id, &modelview_id, &lightpos_id, &color_id, &opacity_id);
glUniformMatrix4fv(projection_id, 1, GL_FALSE, (GLfloat*) &projection.m[0][0]);
glUniform3f(lightpos_id, lightpos.x, lightpos.y, lightpos.z);
glUniform1f(opacity_id, 1.f);
///
glBindBuffer(GL_ARRAY_BUFFER, mdlExo.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlExo.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlExo.iid);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (GLfloat*) &view.m[0][0]);
glDrawElements(GL_TRIANGLES, exo_numind, GL_UNSIGNED_INT, 0);
/// the inner wont draw now if occluded by the exo due to depth buffer
glBindBuffer(GL_ARRAY_BUFFER, mdlInner.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlInner.cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlExo.iid);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (GLfloat*) &view.m[0][0]);
glDrawElements(GL_TRIANGLES, exo_numind, GL_UNSIGNED_INT, 0);
///
// lambert
shadeLambert(&position_id, &projection_id, &modelview_id, &lightpos_id, &color_id, &opacity_id);
glUniformMatrix4fv(projection_id, 1, GL_FALSE, (GLfloat*) &projection.m[0][0]);
glUniform3f(lightpos_id, 0.f, 0.f, 0.f);
glUniform1f(opacity_id, 1.f);
// bind menger
glBindBuffer(GL_ARRAY_BUFFER, mdlMenger.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlMenger.iid);
// "light source" dummy object
mIdent(&model);
mTranslate(&model, lightpos.x, lightpos.y, lightpos.z);
mScale(&model, 3.4f, 3.4f, 3.4f);
glUniform3f(color_id, 1.f, 1.f, 0.f);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
glDrawElements(GL_TRIANGLES, ncube_numind, GL_UNSIGNED_INT, 0);
// lambert
shadeLambert3(&position_id, &projection_id, &modelview_id, &lightpos_id, &normal_id, &color_id, &opacity_id);
glUniformMatrix4fv(projection_id, 1, GL_FALSE, (GLfloat*) &projection.m[0][0]);
glUniform3f(lightpos_id, 0.f, 0.f, 0.f);
// comets
static const uint rcs = NUM_COMETS / 9;
static const f32 rrcs = 1.f / (f32)rcs;
int bindstate = -1;
int cbs = -1;
for(uint i = 0; i < NUM_COMETS; i++)
{
// simulation
if(comets[i].speed == 0.f) // explode
{
if(cbs != 0)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlRock[1].cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
cbs = 0;
}
comets[i].dir.x -= 0.3f*dt;
comets[i].scale -= 0.03f*dt;
if(comets[i].dir.x <= 0.f || comets[i].scale <= 0.f)
{
randComet(i);
continue;
}
glUniform1f(opacity_id, comets[i].dir.x);
}
else if(comets[i].speed != -1.f) // detect impacts
{
// increment position
const f32 pi = comets[i].speed*dt;
vAdd(&comets[i].pos, comets[i].pos, (vec){comets[i].dir.x*pi, comets[i].dir.y*pi, comets[i].dir.z*pi});
// planet impact
if(vMod(comets[i].pos) < 1.14f)
{
doExoImpact((vec){comets[i].pos.x, comets[i].pos.y, comets[i].pos.z}, (comets[i].scale+(comets[i].speed*0.1f))*1.2f);
vec fwd;
vMulS(&fwd, comets[i].dir, 0.03f);
vAdd(&comets[i].pos, comets[i].pos, fwd);
comets[i].speed = 0.f;
comets[i].dir.x = 1.f;
comets[i].scale *= 2.f;
incrementHits();
continue;
}
// player impact
const f32 cd = vDistSq((vec){-ppr.x, -ppr.y, -ppr.z}, comets[i].pos);
const f32 cs = comets[i].scale+0.06f;
if(cd < cs*cs)
{
popped++;
char title[256];
const uint max_damage = exo_numvert/2;
sprintf(title, "Online Fractal Attack Lite | %u/%u | %.2f%% | %.2f mins", hits, popped, (100.f/(float)max_damage)*(float)damage, (time(0)-sepoch)/60.0);
glfwSetWindowTitle(window, title);
comets[i].speed = 0.f;
comets[i].dir.x = 1.f;
//comets[i].scale *= 2.f;
}
// comet impact
for(uint k = 0; k < NUM_COMETS; k++)
{
if(k == i){continue;}
const f32 cd = vDistSq(comets[i].pos, comets[k].pos);
if(cd < comets[i].scale*comets[i].scale)
{
comets[i].speed = 0.f;
comets[i].dir.x = 1.f;
comets[k].speed = 0.f;
comets[k].dir.x = 1.f;
}
}
// flip to grey if red
if(cbs != 1)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlRock[0].cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
cbs = 1;
}
}
else
{
if(cbs != 1)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlRock[0].cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
cbs = 1;
}
}
// translate comet
mIdent(&model);
mTranslate(&model, comets[i].pos.x, comets[i].pos.y, comets[i].pos.z);
// rotate comet
const f32 mag = comets[i].rot*0.01f*t;
if(comets[i].rot < 100.f)
mRotY(&model, mag);
if(comets[i].rot < 200.f)
mRotZ(&model, mag);
if(comets[i].rot < 300.f)
mRotX(&model, mag);
// scale comet
mScale(&model, comets[i].scale, comets[i].scale, comets[i].scale);
// make modelview
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
// bind one of the 8 rock models
uint nbs = i * rrcs;
if(nbs > 8){nbs = 8;}
if(nbs != bindstate)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlRock[nbs].vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlRock[nbs].nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlRock[nbs].iid);
bindstate = nbs;
}
// draw it
if(comets[i].speed == 0.f)
{
glEnable(GL_BLEND);
glDrawElements(GL_TRIANGLES, rock1_numind, GL_UNSIGNED_BYTE, 0);
glDisable(GL_BLEND);
}
else
glDrawElements(GL_TRIANGLES, rock1_numind, GL_UNSIGNED_BYTE, 0);
}
// players
glBindBuffer(GL_ARRAY_BUFFER, mdlRock[2].cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
for(uint i = 0; i < MAX_PLAYERS; i++)
{
const uint j = i*3;
if(players[j] != 0.f || players[j+1] != 0.f || players[j+2] != 0.f)
{
for(uint k = 0; k < NUM_COMETS; k++)
{
const f32 cd = vDistSq((vec){-players[j], -players[j+1], -players[j+2]}, comets[k].pos);
const f32 cs = comets[i].scale+0.06f;
if(cd < cs*cs)
{
comets[k].speed = 0.f;
comets[k].dir.x = 1.f;
}
}
if(interp == 1 && ((players_vel[j]+players_vel[j+1]+players_vel[j+2] != 0.f) && microtime() < interp_et))
{
float s = 1.f - (interp_rdt * (float)(interp_et - microtime()));
if(s < 0.f){s = 0.f;}
else if(s > 1.f){s = 1.f;}
players[j] += players_vel[j]*s;
players[j+1] += players_vel[j+1]*s;
players[j+2] += players_vel[j+2]*s;
//printf("[%u] (%f) %f %f %f\n", i, s, players_vel[j], players_vel[j+1], players_vel[j+2]);
}
mIdent(&model);
mTranslate(&model, -players[j], -players[j+1], -players[j+2]);
mScale(&model, 0.01f, 0.01f, 0.01f);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
glDrawElements(GL_TRIANGLES, rock1_numind, GL_UNSIGNED_BYTE, 0);
}
}
// swap
glfwSwapBuffers(window);
}
//*************************************
// Input Handelling
//*************************************
void window_size_callback(GLFWwindow* window, int width, int height)
{
winw = width;
winh = height;
glViewport(0, 0, winw, winh);
aspect = (f32)winw / (f32)winh;
ww = (double)winw;
wh = (double)winh;
rww = 1.0/ww;
rwh = 1.0/wh;
ww2 = ww/2.0;
wh2 = wh/2.0;
uw = (double)aspect/ww;
uh = 1.0/wh;
uw2 = (double)aspect/ww2;
uh2 = 1.0/wh2;
mIdent(&projection);
mPerspective(&projection, 60.0f, aspect, 0.01f, FAR_DISTANCE);
glUniformMatrix4fv(projection_id, 1, GL_FALSE, (GLfloat*) &projection.m[0][0]);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if(action == GLFW_PRESS)
{
if(key == GLFW_KEY_A){ keystate[0] = 1; }
else if(key == GLFW_KEY_D){ keystate[1] = 1; }
else if(key == GLFW_KEY_W){ keystate[2] = 1; }
else if(key == GLFW_KEY_S){ keystate[3] = 1; }
else if(key == GLFW_KEY_SPACE){ keystate[4] = 1; }
else if(key == GLFW_KEY_LEFT_SHIFT){ keystate[5] = 1; }
else if(key == GLFW_KEY_Q){ keystate[6] = 1; }
else if(key == GLFW_KEY_E){ keystate[7] = 1; }
else if(key == GLFW_KEY_LEFT_CONTROL){ brake = 1; }
else if(key == GLFW_KEY_F)
{
if(t-lfct > 2.0)
{
char strts[16];
timestamp(&strts[0]);
printf("[%s] FPS: %g\n", strts, fc/(t-lfct));
lfct = t;
fc = 0;
}
}
else if(key == GLFW_KEY_ESCAPE)
{
focus_cursor = 1 - focus_cursor;
if(focus_cursor == 0)
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
else
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
window_size_callback(window, winw, winh);
glfwSetCursorPos(window, ww2, wh2);
glfwGetCursorPos(window, &ww2, &wh2);
}
else if(key == GLFW_KEY_V)
{
// view inversion
printf(":: %+.2f\n", view.m[1][1]);
// dump view matrix
printf("%+.2f %+.2f %+.2f %+.2f\n", view.m[0][0], view.m[0][1], view.m[0][2], view.m[0][3]);
printf("%+.2f %+.2f %+.2f %+.2f\n", view.m[1][0], view.m[1][1], view.m[1][2], view.m[1][3]);
printf("%+.2f %+.2f %+.2f %+.2f\n", view.m[2][0], view.m[2][1], view.m[2][2], view.m[2][3]);
printf("%+.2f %+.2f %+.2f %+.2f\n", view.m[3][0], view.m[3][1], view.m[3][2], view.m[3][3]);
printf("---\n");
// randomly dump an online player at your position
// const uint j = esRand(0, 6)*3;
// players[j] = ppr.x;
// players[j+1] = ppr.y;
// players[j+2] = ppr.z;
}
else if(key == GLFW_KEY_I)
{
interp = 1 - interp;
if(interp == 1)
printf("Player extrapolation on.\n");
else
printf("Player extrapolation off.\n");
}
else if(key == GLFW_KEY_R)
{
autoroll = 1 - autoroll;
printf("autoroll: %u\n", autoroll);
}
}
else if(action == GLFW_RELEASE)
{
if(key == GLFW_KEY_A){ keystate[0] = 0; }
else if(key == GLFW_KEY_D){ keystate[1] = 0; }
else if(key == GLFW_KEY_W){ keystate[2] = 0; }
else if(key == GLFW_KEY_S){ keystate[3] = 0; }
else if(key == GLFW_KEY_SPACE){ keystate[4] = 0; }
else if(key == GLFW_KEY_LEFT_SHIFT){ keystate[5] = 0; }
else if(key == GLFW_KEY_Q){ keystate[6] = 0; }
else if(key == GLFW_KEY_E){ keystate[7] = 0; }
else if(key == GLFW_KEY_LEFT_CONTROL){ brake = 0; }
}
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
if(action == GLFW_PRESS)
{
if(button == GLFW_MOUSE_BUTTON_LEFT)
{
focus_cursor = 1 - focus_cursor;
if(focus_cursor == 0)
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
else
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
window_size_callback(window, winw, winh);
glfwSetCursorPos(window, ww2, wh2);
glfwGetCursorPos(window, &ww2, &wh2);
}
else if(button == GLFW_MOUSE_BUTTON_RIGHT)
brake = 1;
}
else if(action == GLFW_RELEASE)
brake = 0;
}
//*************************************
// Process Entry Point
//*************************************
int main(int argc, char** argv)
{
// gen client UID
uid = urand16();
// epoch
sepoch = time(0);
sepoch = ((((time_t)(((double)sepoch / 60.0) / 3.0)+1)*3)*60); // next 20th of an hour
// help
printf("----\n");
printf("Online Fractal Attack Lite\n");
printf("----\n");
printf("James William Fletcher (github.com/mrbid)\n");
printf("----\n");
printf("Argv(2): start epoch, msaa 0-16\n");
printf("F = FPS to console.\n");
printf("I = Toggle player lag extrapolation.\n");
printf("R = Toggle auto-tilt around planet.\n");
printf("W, A, S, D, Q, E, SPACE, LEFT SHIFT\n");
printf("L-CTRL / Right Click to Brake.\n");
printf("Escape / Left Click to free mouse focus.\n");
printf("----\n");
printf("current epoch: %lu\n", time(0));
if(argc >= 2)
{
sepoch = atoll(argv[1]);
if(sepoch != 0 && sepoch-time(0) < 3)
{
printf("suggested epoch: %lu\n----\nYour epoch should be at minimum 3 seconds into the future.\n", time(0)+16);
return 0;