-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
1691 lines (1413 loc) · 52.1 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)
December 2021
Info:
Space Miner, a simple 3D space mining game.
Keyboard:
F = FPS to console
P = Player stats to console
N = New Game
Q = Break Asteroid
E = Stop all nearby Asteroids
R = Repel all nearby Asteroids
W = Thrust Forward
A = Turn Left
S = Thrust Backward
D = Turn Right
Shift = Thrust Down
Space = Thrust Up
Mouse:
Left Click = Break Asteroid
Right Click = Repel Asteroid
Mouse 4 Click = Stop all Asteroids nearby
Scroll = Zoom in/out
Notes:
Frustum Culling:
In a game like this by introducing frustum culling all you are doing is introducing
frame rate instability, the fewer asteroids on-screen with frustum culling the more frames
per second the game will get, and vice-versa. But in a game like this, you could get all
asteroids in the frame at once, or even just very varied amounts. Frame rate instability is
always worse than having a stable, but lower, frame rate.
I've never been a great fan of frustum culling, mainly because it only makes sense in a
very niche avenue of 3D games, albeit the most popular niche, FPS games.
The popularity of First-Person can hamper creativity, other 3D camera systems are rarely
ever explored and partly this is due to there only being four main 3D camera systems;
Fixed Camera, Ghost/Chase Camera, Orbit Camera, and First Person Camera.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define uint GLushort
#define sint GLshort
#define f32 GLfloat
#include "gl.h"
#define GLFW_INCLUDE_NONE
#include "glfw3.h"
#include "esAux4.h"
#include "res.h"
#include "assets/rock1.h"
#include "assets/rock2.h"
#include "assets/rock3.h"
#include "assets/rock4.h"
#include "assets/rock5.h"
#include "assets/rock6.h"
#include "assets/rock7.h"
#include "assets/rock8.h"
#include "assets/rock9.h"
#include "assets/face.h"
#include "assets/body.h"
#include "assets/arms.h"
#include "assets/left_flame.h"
#include "assets/right_flame.h"
#include "assets/legs.h"
#include "assets/fuel.h"
#include "assets/shield.h"
#include "assets/pbreak.h"
#include "assets/pshield.h"
#include "assets/pslow.h"
#include "assets/prepel.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 x,y,lx,ly;
double rww, ww, rwh, wh, ww2, wh2;
double uw, uh, uw2, uh2; // normalised pixel dpi
// 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;
// render state inputs
vec lightpos = {0.f, 0.f, 0.f};
// models
sint bindstate = -1;
sint bindstate2 = -1;
uint keystate[6] = {0};
ESModel mdlRock[9];
ESModel mdlFace;
ESModel mdlBody;
ESModel mdlArms;
ESModel mdlLeftFlame;
ESModel mdlRightFlame;
ESModel mdlLegs;
ESModel mdlFuel;
ESModel mdlShield;
ESModel mdlPbreak;
ESModel mdlPshield;
ESModel mdlPslow;
ESModel mdlPrepel;
// game vars
#define NEWGAME_SEED 1337
#define THRUST_POWER 0.09f
#define NECK_ANGLE 0.6f
#define ROCK_DARKNESS 0.412f
#define MAX_ROCK_SCALE 12.f
const f32 RECIP_MAX_ROCK_SCALE = 1.f/(MAX_ROCK_SCALE+10.f);
#define FUEL_DRAIN_RATE 0.03f
#define SHIELD_DRAIN_RATE 0.06f
#define REFINARY_YEILD 0.13f
#ifdef __arm__
#define ARRAY_MAX 2048 // 8 Megabytes of Asteroids
const f32 FAR_DISTANCE = (float)ARRAY_MAX / 2.f;
#else
#define ARRAY_MAX 16384 // 64 Megabytes of Asteroids
f32 FAR_DISTANCE = (float)ARRAY_MAX / 2.f;
#endif
typedef struct
{
// since we have the room
int free; // fast free checking
int nores;// no mineral resources
// rock vars
f32 scale;
vec pos;
vec vel;
// +6 bytes
uint rnd;
f32 rndf;
// rock colour array
f32 colors[720];
// mineral amounts
f32 qshield;
f32 qbreak;
f32 qslow;
f32 qrepel;
f32 qfuel;
} gi; // 4+4+4+16+16+2+4+2880+4+4+4+4+4 = 2950 bytes = 4096 padded (4 kilobyte)
gi array_rocks[ARRAY_MAX] = {0};
// gets a free/unused rock
/*
// the original idea was to dynamically pop out ores when rocks are mined
// which then you need to manually float to and collect but, I went off the idea.
sint freeRock()
{
for(sint i = 0; i < ARRAY_MAX; i++)
if(array_rocks[i].free == 1)
return i;
return -1;
}
*/
// camera vars
uint focus_cursor = 0;
double sens = 0.003f;
f32 xrot = 0.f;
f32 yrot = 0.f;
f32 zoom = -25.f;
// player vars
f32 so; // shield on (closest distance)
uint ct;// thrust signal
f32 pr; // rotation
vec pp; // position
vec pv; // velocity
vec pd; // thust direction
f32 lgr = 0.f; // last good head rotation
vec pld;// look direction
vec pfd;// face direction
f32 pf; // fuel
f32 pb; // break
f32 ps; // shield
f32 psp;// speed
f32 psl;// slow
f32 pre;// repel
uint lf;// last fuel
uint pm;// mined asteroid count
double st=0; // start time
char tts[32];// time taken string
//*************************************
// utility functions
//*************************************
void timestamp(char* ts)
{
const time_t tt = time(0);
strftime(ts, 16, "%H:%M:%S", localtime(&tt));
}
static inline f32 fzero(f32 f)
{
if(f < 0.f){f = 0.f;}
return f;
}
static inline f32 fone(f32 f)
{
if(f > 1.f){f = 1.f;}
return f;
}
static inline f32 fsat(f32 f)
{
if(f < 0.f){f = 0.f;}
if(f > 1.f){f = 1.f;}
return f;
}
void timeTaken(uint ss)
{
if(ss == 1)
{
const double tt = t-st;
if(tt < 60.0)
sprintf(tts, "%.2f Sec", tt);
else if(tt < 3600.0)
sprintf(tts, "%.2f Min", tt * 0.016666667);
else if(tt < 216000.0)
sprintf(tts, "%.2f Hr", tt * 0.000277778);
else if(tt < 12960000.0)
sprintf(tts, "%.2f Days", tt * 0.00000463);
}
else
{
const double tt = t-st;
if(tt < 60.0)
sprintf(tts, "%.2f Seconds", tt);
else if(tt < 3600.0)
sprintf(tts, "%.2f Minutes", tt * 0.016666667);
else if(tt < 216000.0)
sprintf(tts, "%.2f Hours", tt * 0.000277778);
else if(tt < 12960000.0)
sprintf(tts, "%.2f Days", tt * 0.00000463);
}
}
//*************************************
// render functions
//*************************************
void rRock(uint i, f32 dist)
{
static const uint rcs = ARRAY_MAX / 9;
static const f32 rrcs = 1.f / (f32)rcs;
mIdent(&model);
mTranslate(&model, array_rocks[i].pos.x, array_rocks[i].pos.y, array_rocks[i].pos.z);
if(array_rocks[i].rnd < 500)
{
f32 mag = vMag(array_rocks[i].vel)*array_rocks[i].rndf*t;
if(array_rocks[i].rnd < 100)
mRotY(&model, mag);
if(array_rocks[i].rnd < 200)
mRotZ(&model, mag);
if(array_rocks[i].rnd < 300)
mRotX(&model, mag);
}
if(array_rocks[i].free == 2)
{
array_rocks[i].scale -= 32.f*dt;
if(array_rocks[i].scale <= 0.f)
array_rocks[i].free = 1;
mScale(&model, array_rocks[i].scale, array_rocks[i].scale, array_rocks[i].scale);
}
else
mScale(&model, array_rocks[i].scale, array_rocks[i].scale, array_rocks[i].scale);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*)&modelview.m[0][0]);
glUniform1f(opacity_id, 1.0f);
// unique colour arrays for each rock within visible distance
if(array_rocks[i].nores == 0 && dist < 333.f)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlRock[0].cid);
glBufferData(GL_ARRAY_BUFFER, sizeof(rock1_colors), array_rocks[i].colors, GL_STATIC_DRAW);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
bindstate2 = 0;
}
else
{
if(bindstate2 != 1)
{
glBindBuffer(GL_ARRAY_BUFFER, mdlRock[1].cid);
glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(color_id);
bindstate2 = 1;
}
}
// this is a super efficient way to render 9 different types of asteroid
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;
}
glDrawElements(GL_TRIANGLES, rock1_numind, GL_UNSIGNED_SHORT, 0);
}
void rLegs(f32 x, f32 y, f32 z, f32 rx)
{
bindstate = -1;
mIdent(&model);
mTranslate(&model, x, y, z);
mRotX(&model, -rx);
f32 mag = psp*32.f;
if(mag > 0.4f)
mag = 0.4f;
mRotY(&model, mag);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*)&modelview.m[0][0]);
glUniform1f(opacity_id, 1.0f);
glUniform3f(color_id, 1.f, 1.f, 1.f);
glBindBuffer(GL_ARRAY_BUFFER, mdlLegs.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlLegs.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlLegs.iid);
glDrawElements(GL_TRIANGLES, legs_numind, GL_UNSIGNED_SHORT, 0);
}
void rBody(f32 x, f32 y, f32 z, f32 rx)
{
bindstate = -1;
mIdent(&model);
mTranslate(&model, x, y, z);
mRotX(&model, -rx);
// returns new player direction
mGetDirZ(&pld, model);
vInv(&pld);
if(ct > 0)
{
mGetDirZ(&pd, model);
vInv(&pd);
ct = 0;
}
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*)&modelview.m[0][0]);
glUniform1f(opacity_id, 1.0f);
glUniform3f(color_id, 1.f, 1.f, 1.f);
glBindBuffer(GL_ARRAY_BUFFER, mdlBody.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlBody.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlBody.iid);
glDrawElements(GL_TRIANGLES, body_numind, GL_UNSIGNED_SHORT, 0);
}
void rFuel(f32 x, f32 y, f32 z, f32 rx)
{
bindstate = -1;
mIdent(&model);
mTranslate(&model, x, y, z);
mRotX(&model, -rx);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*)&modelview.m[0][0]);
glUniform1f(opacity_id, 1.0f);
glUniform3f(color_id, fone(0.062f+(1.f-pf)), fone(1.f+(1.f-pf)), fone(0.873f+(1.f-pf)));
glBindBuffer(GL_ARRAY_BUFFER, mdlFuel.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlFuel.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlFuel.iid);
glDrawElements(GL_TRIANGLES, fuel_numind, GL_UNSIGNED_SHORT, 0);
}
void rArms(f32 x, f32 y, f32 z, f32 rx)
{
bindstate = -1;
mIdent(&model);
mTranslate(&model, x, y, z);
mRotX(&model, -rx);
f32 mag = psp*32.f;
if(mag > 0.4f)
mag = 0.4f;
mRotY(&model, mag);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*)&modelview.m[0][0]);
glUniform1f(opacity_id, 1.0f);
glUniform3f(color_id, 1.f, 1.f, 1.f);
glBindBuffer(GL_ARRAY_BUFFER, mdlArms.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlArms.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlArms.iid);
glDrawElements(GL_TRIANGLES, arms_numind, GL_UNSIGNED_SHORT, 0);
}
void rLeftFlame(f32 x, f32 y, f32 z, f32 rx)
{
bindstate = -1;
mIdent(&model);
mTranslate(&model, x, y, z);
mRotX(&model, -rx);
f32 mag = psp*32.f;
if(mag > 0.4f)
mag = 0.4f;
mRotY(&model, mag);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*)&modelview.m[0][0]);
glUniform1f(opacity_id, 1.0f);
//glUniform3f(color_id, 1.f, 0.f, 0.f);
glUniform3f(color_id, 0.062f, 1.f, 0.873f);
glBindBuffer(GL_ARRAY_BUFFER, mdlLeftFlame.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlLeftFlame.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlLeftFlame.iid);
glDrawElements(GL_TRIANGLES, left_flame_numind, GL_UNSIGNED_SHORT, 0);
}
void rRightFlame(f32 x, f32 y, f32 z, f32 rx)
{
bindstate = -1;
mIdent(&model);
mTranslate(&model, x, y, z);
mRotX(&model, -rx);
f32 mag = psp*32.f;
if(mag > 0.4f)
mag = 0.4f;
mRotY(&model, mag);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*)&modelview.m[0][0]);
glUniform1f(opacity_id, 1.0f);
glUniform3f(color_id, 0.062f, 1.f, 0.873f);
glBindBuffer(GL_ARRAY_BUFFER, mdlRightFlame.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlRightFlame.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlRightFlame.iid);
glDrawElements(GL_TRIANGLES, right_flame_numind, GL_UNSIGNED_SHORT, 0);
}
void rFace(f32 x, f32 y, f32 z, f32 rx)
{
bindstate = -1;
mIdent(&model);
mTranslate(&model, x, y, z);
mRotX(&model, -xrot);
mGetDirZ(&pfd, model);
vInv(&pfd);
const f32 dot = vDot(pfd, pld);
if(dot < NECK_ANGLE)
{
mIdent(&model);
mTranslate(&model, x, y, z);
mRotX(&model, -lgr);
}
else
{
lgr = xrot;
}
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*)&modelview.m[0][0]);
glUniform1f(opacity_id, 1.0f);
glUniform3f(color_id, 1.f, 1.f, 1.f);
glBindBuffer(GL_ARRAY_BUFFER, mdlFace.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlFace.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlFace.iid);
glDrawElements(GL_TRIANGLES, face_numind, GL_UNSIGNED_SHORT, 0);
}
void rBreak(f32 x, f32 y, f32 z, f32 rx)
{
bindstate = -1;
mIdent(&model);
mTranslate(&model, x, y, z);
mRotX(&model, -xrot);
vec dir;
mGetDirZ(&dir, model);
vInv(&dir);
const f32 dot = vDot(dir, pld);
if(dot < NECK_ANGLE)
{
mIdent(&model);
mTranslate(&model, x, y, z);
mRotX(&model, -lgr);
}
else
{
lgr = xrot;
}
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*)&modelview.m[0][0]);
glUniform1f(opacity_id, 1.0f);
glUniform3f(color_id, fone(0.644f+(1.f-pb)), fone(0.209f+(1.f-pb)), fone(0.f+(1.f-pb)));
glBindBuffer(GL_ARRAY_BUFFER, mdlPbreak.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlPbreak.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlPbreak.iid);
glDrawElements(GL_TRIANGLES, pbreak_numind, GL_UNSIGNED_SHORT, 0);
}
void rShield(f32 x, f32 y, f32 z, f32 rx)
{
bindstate = -1;
mIdent(&model);
mTranslate(&model, x, y, z);
mRotX(&model, -xrot);
vec dir;
mGetDirZ(&dir, model);
vInv(&dir);
const f32 dot = vDot(dir, pld);
if(dot < NECK_ANGLE)
{
mIdent(&model);
mTranslate(&model, x, y, z);
mRotX(&model, -lgr);
}
else
{
lgr = xrot;
}
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*)&modelview.m[0][0]);
glUniform1f(opacity_id, 1.0f);
glUniform3f(color_id, fone(0.f+(1.f-ps)), fone(0.8f+(1.f-ps)), fone(0.28f+(1.f-ps)));
glBindBuffer(GL_ARRAY_BUFFER, mdlPshield.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlPshield.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlPshield.iid);
glDrawElements(GL_TRIANGLES, pshield_numind, GL_UNSIGNED_SHORT, 0);
}
void rSlow(f32 x, f32 y, f32 z, f32 rx)
{
bindstate = -1;
mIdent(&model);
mTranslate(&model, x, y, z);
mRotX(&model, -xrot);
vec dir;
mGetDirZ(&dir, model);
vInv(&dir);
const f32 dot = vDot(dir, pld);
if(dot < NECK_ANGLE)
{
mIdent(&model);
mTranslate(&model, x, y, z);
mRotX(&model, -lgr);
}
else
{
lgr = xrot;
}
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*)&modelview.m[0][0]);
glUniform1f(opacity_id, 1.0f);
glUniform3f(color_id, fone(0.429f+(1.f-psl)), fone(0.f+(1.f-psl)), fone(0.8f+(1.f-psl)));
glBindBuffer(GL_ARRAY_BUFFER, mdlPslow.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlPslow.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlPslow.iid);
glDrawElements(GL_TRIANGLES, pslow_numind, GL_UNSIGNED_SHORT, 0);
}
void rRepel(f32 x, f32 y, f32 z, f32 rx)
{
bindstate = -1;
mIdent(&model);
mTranslate(&model, x, y, z);
mRotX(&model, -xrot);
vec dir;
mGetDirZ(&dir, model);
vInv(&dir);
const f32 dot = vDot(dir, pld);
if(dot < NECK_ANGLE)
{
mIdent(&model);
mTranslate(&model, x, y, z);
mRotX(&model, -lgr);
}
else
{
lgr = xrot;
}
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*)&modelview.m[0][0]);
glUniform1f(opacity_id, 1.0f);
glUniform3f(color_id, fone(0.095f+(1.f-pre)), fone(0.069f+(1.f-pre)), fone(0.041f+(1.f-pre)));
glBindBuffer(GL_ARRAY_BUFFER, mdlPrepel.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlPrepel.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlPrepel.iid);
glDrawElements(GL_TRIANGLES, prepel_numind, GL_UNSIGNED_SHORT, 0);
}
void rShieldElipse(f32 x, f32 y, f32 z, f32 rx, f32 opacity)
{
bindstate = -1;
mIdent(&model);
mTranslate(&model, x, y, z);
mRotX(&model, -rx);
mMul(&modelview, &model, &view);
glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*)&modelview.m[0][0]);
glUniform1f(opacity_id, opacity);
glUniform3f(color_id, 0.f, 0.717, 0.8f);
glBindBuffer(GL_ARRAY_BUFFER, mdlShield.vid);
glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(position_id);
glBindBuffer(GL_ARRAY_BUFFER, mdlShield.nid);
glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(normal_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlShield.iid);
glEnable(GL_BLEND);
glDrawElements(GL_TRIANGLES, shield_numind, GL_UNSIGNED_SHORT, 0);
glDisable(GL_BLEND);
}
void rPlayer(f32 x, f32 y, f32 z, f32 rx)
{
psp = vMag(pv);
rLegs(x, y, z, rx);
rBody(x, y, z, rx);
rFuel(x, y, z, rx);
rArms(x, y+2.6f, z, rx);
uint lf=0, rf=0;
if(keystate[0] == 1)
rf = 1;
if(keystate[1] == 1)
lf = 1;
if(keystate[2] == 1)
rf = 1, lf = 1;
if(keystate[3] == 1)
rf = 1, lf = 1;
if(keystate[4] == 1)
rf = 1, lf = 1;
if(keystate[5] == 1)
rf = 1, lf = 1;
if(lf == 1)
rLeftFlame(x, y+2.6f, z, rx);
if(rf == 1)
rRightFlame(x, y+2.6f, z, rx);
rFace(x, y+3.4f, z, rx);
rBreak(x, y+3.4f, z, rx);
rShield(x, y+3.4f, z, rx);
rSlow(x, y+3.4f, z, rx);
rRepel(x, y+3.4f, z, rx);
if(so > 0.f)
{
const f32 ss = 1.f-(so*RECIP_MAX_ROCK_SCALE);
if(ps == 0.f)
{
pf -= FUEL_DRAIN_RATE * ss * dt;
pf = fzero(pf);
}
else
{
ps -= SHIELD_DRAIN_RATE * ss * dt;
ps = fzero(ps);
//pv = (vec){0.f,0.f,0.f};
//vReflect(&pv, pv, pd);
rShieldElipse(x, y+1.f, z, rx, fsat(ss));
}
//printf("s: %g\n", ps);
}
}
//*************************************
// game functions
//*************************************
void newGame(unsigned int seed)
{
srand(seed);
char strts[16];
timestamp(&strts[0]);
printf("\n[%s] Game Start [%u].\n", strts, seed);
glfwSetWindowTitle(window, "Space Miner");
f32 FDGD = FAR_DISTANCE;
#ifndef __arm__
const f32 scalar = esRandFloat(8.f, 12.f);
FDGD = (float)ARRAY_MAX / scalar;
//printf("Far Distance Divisor: %g\n", scalar);
#endif
pp = (vec){0.f, 0.f, 0.f};
pv = (vec){0.f, 0.f, 0.f};
pd = (vec){0.f, 0.f, 0.f};
pld = (vec){0.f, 0.f, 0.f};
st = 0;
lf = 100;
ct = 0;
pm = 0;
so = 0.f;
pr = 0.f;
lgr = 0.f;
pf = 1.f;
pb = 1.f;
ps = 1.f;
psl = 0.f;
pre = 0.f;
psp = 0.f;
for(uint i = 0; i < ARRAY_MAX; i++)
{
array_rocks[i].free = 0;
array_rocks[i].scale = esRandFloat(0.1f, MAX_ROCK_SCALE);
array_rocks[i].pos.x = esRandFloat(-FDGD, FDGD);
array_rocks[i].pos.y = esRandFloat(-FDGD, FDGD);
array_rocks[i].pos.z = esRandFloat(-FDGD, FDGD);
array_rocks[i].rnd = esRand(0, 1000);
array_rocks[i].rndf = esRandFloat(0.05f, 0.3f);
if(esRand(0, 1000) < 500)
{
array_rocks[i].qshield = esRandFloat(0.f, 1.f);
array_rocks[i].qbreak = esRandFloat(0.f, 1.f);
array_rocks[i].qslow = esRandFloat(0.f, 1.f);
array_rocks[i].qrepel = esRandFloat(0.f, 1.f);
array_rocks[i].qfuel = esRandFloat(0.f, 1.f);
}
else
{
array_rocks[i].qshield = 0.f;
array_rocks[i].qbreak = 0.f;
array_rocks[i].qslow = 0.f;
array_rocks[i].qrepel = 0.f;
array_rocks[i].qfuel = 0.f;
array_rocks[i].nores = 1;
}
for(uint j = 0; j < 717; j += 3)
{
uint set = 0;
#define CLR_CHANCE 0.01f
// break
if(esRandFloat(0.f, 1.f) < array_rocks[i].qbreak*CLR_CHANCE)
{
array_rocks[i].colors[j] = 0.644f;
array_rocks[i].colors[j+1] = 0.209f;
array_rocks[i].colors[j+2] = 0.f;
set = 1;
}
// shield
if(set == 0 && esRandFloat(0.f, 1.f) < array_rocks[i].qshield*CLR_CHANCE)
{
array_rocks[i].colors[j] = 0.f;
array_rocks[i].colors[j+1] = 0.8f;
array_rocks[i].colors[j+2] = 0.28f;
set = 1;
}
// slow
if(set == 0 && esRandFloat(0.f, 1.f) < array_rocks[i].qslow*CLR_CHANCE)
{
array_rocks[i].colors[j] = 0.429f;
array_rocks[i].colors[j+1] = 0.f;
array_rocks[i].colors[j+2] = 0.8f;
set = 1;
}
// repel
if(set == 0 && esRandFloat(0.f, 1.f) < array_rocks[i].qrepel*CLR_CHANCE)
{
array_rocks[i].colors[j] = 0.095f;
array_rocks[i].colors[j+1] = 0.069f;
array_rocks[i].colors[j+2] = 0.041f;
set = 1;
}
// fuel
if(set == 0 && esRandFloat(0.f, 1.f) < array_rocks[i].qfuel*CLR_CHANCE)
{
array_rocks[i].colors[j] = 0.062f;
array_rocks[i].colors[j+1] = 1.f;
array_rocks[i].colors[j+2] = 0.873f;
set = 1;
}
// else
if(set == 0)
{
array_rocks[i].colors[j] = ROCK_DARKNESS;
array_rocks[i].colors[j+1] = ROCK_DARKNESS;
array_rocks[i].colors[j+2] = ROCK_DARKNESS;
}
}
vRuv(&array_rocks[i].vel);
}
st = t;
}
//*************************************
// update & render
//*************************************
void main_loop()
{
//*************************************
// time delta for interpolation
//*************************************
static double lt = 0;
dt = t-lt;
lt = t;
//*************************************
// keystates
//*************************************
if(pf == 0.f) // disable thrust control on fuel empty