-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcf_scrap_code.txt
7182 lines (5167 loc) · 209 KB
/
cf_scrap_code.txt
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
const int res = 1024;
const float units_per_texel = 2.0f * glm_aabb_radius(camera_)
camera_sub_frustum_center[0] = floorf(camera_sub_frustum_center[0] / units_per_texel;
camera_sub_frustum_center[1] = floorf(camera_sub_frustum_center[1] / units_per_texel;
camera_sub_frustum_center[2] = floorf(camera_sub_frustum_center[2] / units_per_texel;
_____
// return float(UV.z - 0.0005f < texture(shadow_cascade_sampler, vec3(UV.xy, layer)).r);
/*
float bias = 0.0002f / (layer + 1);
return texture(shadow_cascade_sampler, vec4(UV.xy, layer, UV.z - bias));
*/
_____
// const float esm_constant = 80.0f;
/*
const float esm_constants[NUM_CASCADES] = float[NUM_CASCADES](
// 100.0f, 200.0f, 80.0f, 80.0f, 80.0f, 80.0f, 8.0f, 8.0f
80.0f, 80.0f, 80.0f, 80.0f, 80.0f, 80.0f, 80.0f, 80.0f
);
*/
_____
/*
const float esm_constants[NUM_CASCADES] = float[NUM_CASCADES](
// 100.0f, 200.0f, 80.0f, 80.0f, 80.0f, 80.0f, 8.0f, 8.0f
80.0f, 80.0f, 80.0f, 80.0f, 80.0f, 80.0f, 80.0f, 80.0f
);
_____
float shadow = csm_shadow(world_depth_value, fragment_pos_world_space);
// texture_color = layer_color;
_____
camera_sub_frustum_center[0] = floorf(camera_sub_frustum_center[0]);
camera_sub_frustum_center[1] = floorf(camera_sub_frustum_center[1]);
camera_sub_frustum_center[2] = floorf(camera_sub_frustum_center[2]);
camera_sub_frustum_center[3] = floorf(camera_sub_frustum_center[3]);
_____ Non-working texel snapping:
static void texel_snapping(const GLsizei resolution, const mat4 view_projection, const vec4 center, vec4 final_center) {
const GLfloat texel_scale = 2.0f / resolution;
const GLfloat inv_texel_scale = 1.0f / texel_scale;
vec4 projected_center;
glm_mat4_mulv((vec4*) view_projection, (vec4) {center[0], center[1], center[2], 1.0f}, projected_center);
/////////
const GLfloat w = projected_center[3];
const GLfloat x = floorf((projected_center[0] / w) * inv_texel_scale) * texel_scale;
const GLfloat y = floorf((projected_center[1] / w) * inv_texel_scale) * texel_scale;
const GLfloat z = projected_center[2] / w;
//////////
mat4 inv_view_projection;
glm_mat4_inv((vec4*) view_projection, inv_view_projection);
vec4 corrected_center;
glm_mat4_mulv(inv_view_projection, (vec4) {x, y, z, 1.0f}, corrected_center);
glm_vec4_scale(corrected_center, 1.0f / corrected_center[3], final_center);
DEBUG_VEC3(final_center);
/*
texel scale = 2 / resolution // TODO: possibly make this a 2D vector at some point
inv texel scale = 1 / texel scale // TODO: remove this division in some way
// is center the center of the sub frustum in camera space? probably
// and it may actually be the case that light_view_projection is camera_sub_frustum_view_projection
projected center = XMVector4Transform(vec4(center, 1.0f), light_view_projection) // TODO: use glm_mat4_mulv3
w = projected center.w
x = floor((projected_center.x / w) * inv texel scale) * texel_scale
y = floor((projected_center.y / w) * inv texel scale) * texel scale
z = projected_center.z / w
corrected_center = XMVector4Transform(
vec4(x, y, z, 1.0f),
mat4_inverse(light_view_projection)
)
final_center = vec3_scale(corrected center, 1.0f / corrected_center.w);
*/
}
/*
const GLfloat texel_scale = 2.0f / resolution;
const GLfloat inv_texel_scale = 1.0f / texel_scale;
vec4 projected_center;
glm_mat4_mulv((vec4*) view_projection, (vec4) {center[0], center[1], center[2], 1.0f}, projected_center);
/////////
const GLfloat w = projected_center[3];
const GLfloat x = floorf((projected_center[0] / w) * inv_texel_scale) * texel_scale;
const GLfloat y = floorf((projected_center[1] / w) * inv_texel_scale) * texel_scale;
const GLfloat z = projected_center[2] / w;
//////////
mat4 inv_view_projection;
glm_mat4_inv((vec4*) view_projection, inv_view_projection);
vec4 corrected_center;
glm_mat4_mulv(inv_view_projection, (vec4) {x, y, z, 1.0f}, corrected_center);
glm_vec4_scale(corrected_center, 1.0f / corrected_center[3], final_center);
DEBUG_VEC3(final_center);
*/
/*
texel scale = 2 / resolution // TODO: possibly make this a 2D vector at some point
inv texel scale = 1 / texel scale // TODO: remove this division in some way
// is center the center of the sub frustum in camera space? probably
// and it may actually be the case that light_view_projection is camera_sub_frustum_view_projection
projected center = XMVector4Transform(vec4(center, 1.0f), light_view_projection) // TODO: use glm_mat4_mulv3
w = projected center.w
x = floor((projected_center.x / w) * inv texel scale) * texel_scale
y = floor((projected_center.y / w) * inv texel scale) * texel scale
z = projected_center.z / w
corrected_center = XMVector4Transform(
vec4(x, y, z, 1.0f),
mat4_inverse(light_view_projection)
)
final_center = vec3_scale(corrected center, 1.0f / corrected_center.w);
*/
_____
// glm_vec2_copy((GLfloat*) light_view_projection[3], shadow_origin);
_____
glm_vec2_copy((GLfloat*) light_view_projection[3], shadow_origin);
glm_vec2_scale(shadow_origin, resolution / 2.0f, shadow_origin);
_____
if (keys[SDL_SCANCODE_G]) {
puts("Texel snapping");
apply_texel_snapping(light_projection, light_view_projection);
glm_mul(light_projection, light_view, light_view_projection);
}
else puts("No texel snapping");
_____ From https://dev.theomader.com/stable-csm/ (didn't work):
const GLsizei size = 1024;
const GLfloat min_x = light_view_frustum_box[0][0], min_y = light_view_frustum_box[0][1];
const GLfloat quant_step = 1.0f / size;
const GLfloat qx = remainderf(min_x, quant_step);
const GLfloat qy = remainderf(min_y, quant_step);
light_view_frustum_box[0][0] -= qx;
light_view_frustum_box[0][1] -= qy;
light_view_frustum_box[1][0] += size;
light_view_frustum_box[1][1] += size;
glm_ortho_aabb(light_view_frustum_box, light_projection);
glm_mul(light_projection, light_view, light_view_projection);
_____
DEBUG_VEC4(light_projection[3]);
DEBUG_VEC4(light_view[3]);
DEBUG_VEC4(light_view_projection[3]);
puts("---");
_____
// only need [3][0] and [3][1] for the math
#define PRMAT(m) do {\
puts(#m " = ");\
for (int y = 0; y < 4; y++) {\
for (int x = 0; x < 4; x++) {\
printf("%lf ", (double) m[y][x]);\
}\
puts("");\
}\
puts("---");\
} while (0)
PRMAT(light_projection);
PRMAT(light_view);
DEBUG_VEC4(light_view_projection[3]);
puts("----------");
_____
// int layer_before = (layer - 1) * int(layer == 0); // If the last layer is 0, yields 0
_____
// How the hell
float lin_pct = world_depth_value / far_clip_dist;
float log_pct = log2(world_depth_value);
// Try with a smaller world
return log_pct * NUM_CASCADES;
/*
float lin = world_depth_value;
float lin_pct = near_clip_dist + lin / clip_dist_diff;
float lg = near_clip_dist * pow(far_clip_dist / near_clip_dist, lin_pct);
float lg_pct = lg / far_clip_dist;
layer = int(mix(lg_pct, lin_pct, linear_split_weight) * NUM_CASCADES);
*/
_____
const GLfloat a = 125.0f, b = 5.0f;
const GLfloat c = logf(a) / logf(b);
DEBUG_FLOAT(c);
_____
GLfloat get_layer(const GLfloat world_depth_value, const GLfloat far_clip_dist, const GLsizei num_layers) {
/*
linear:
linear_dist = near + ((i + 1) / num_layers) * (far - near))
linear_dist - near = ((i + 1) / num_layers * (far - near))
(linear_dist - near) / (far - near) = (i + 1) / num_layers
(linear_dist - near) / (far - near) * num_layers = i + 1
(linear_dist - near) / (far - near) * num_layers - 1 = i
// how to get linear dist from the start then?
logarithmic:
log_dist = near * ((far / near) ^ ((i + 1) / num_layers))
log_dist / near = (far / near) ^ ((i + 1) / num_layers)
a = b ^ c
goal: extract c
c = log(a) / log(b)
so, log(log_dist / near) / log(far / near) = ((i + 1) / num_layers)
log(log_dist / near) / log(far / near) * num_layers = i + 1
log(log_dist / near) / log(far / near) * num_layers - 1 = i
_____
in fragment shader, I have access to the world depth value, near clip dist, the far clip dist,
and the number of layers. The world depth value is essentially the linear dist.
I can get the linear layer without too much difficulty.
But I will need to think more about how to get the logarithmic dist, since i and the logarithmic dist are both unknown.
Perhaps I can use the i value gotten from the linear layer calculation?
And am I really looking for i with the log dist calculation, since i will be the same?
Try the equation first.
*/
const GLfloat near_clip_dist = constants.camera.near_clip_dist;
const GLfloat linear_layer = (world_depth_value - near_clip_dist) / (far_clip_dist - near_clip_dist) * num_layers - 1.0f;
const GLfloat log_layer = (logf(world_depth_value / near_clip_dist) / logf(far_clip_dist / near_clip_dist) * num_layers);
DEBUG_FLOAT(log_layer);
return linear_layer;
}
_____
const vec3
red = vec3(1.0f, 0.0f, 0.0f), green = vec3(0.0f, 1.0f, 0.0f), blue = vec3(0.0f, 0.0f, 1.0f),
yellow = vec3(1.0f, 1.0f, 0.0f), teal = vec3(0.0f, 1.0f, 1.0f), orange = vec3(1.0f, 0.27f, 0.0f),
pink = vec3(1.0f, 0.03f, 0.5f), gray = vec3(0.86f, 0.86f, 0.86f);
const vec3 colors[NUM_CASCADES] = vec3[NUM_CASCADES](red, green, blue, yellow, teal, orange, pink, gray);
layer_color = colors[layer];
// TODO: a `blend_between_layers` function
//////////
// The constant-time solution would make things so much easier. First find a hacky way to figure out the last layer though.
/*
if (layer != 0) {
if (layer == num_splits_between_cascades) {
layer_color = vec3(0.0f, 0.0f, 0.0f);
}
else {
int layer_before = layer - 1;
float
plane_dist = cascade_plane_distances[layer],
plane_dist_behind = cascade_plane_distances[layer_before];
float
dist_ahead_of_last_plane = world_depth_value - plane_dist_behind,
depth_range = plane_dist - plane_dist_behind;
float percent_between = dist_ahead_of_last_plane / depth_range;
float
curr_layer_shadow = get_csm_shadow_from_layer(layer, fragment_pos_world_space),
last_layer_shadow = get_csm_shadow_from_layer(layer_before, fragment_pos_world_space);
layer_color = mix(colors[layer_before], colors[layer], percent_between);
return mix(last_layer_shadow, curr_layer_shadow, percent_between);
}
}
*/
_____
const vec3
red = vec3(1.0f, 0.0f, 0.0f), green = vec3(0.0f, 1.0f, 0.0f), blue = vec3(0.0f, 0.0f, 1.0f),
yellow = vec3(1.0f, 1.0f, 0.0f), teal = vec3(0.0f, 1.0f, 1.0f), orange = vec3(1.0f, 0.27f, 0.0f),
black = vec3(0.0f, 0.0f, 0.0f), white = vec3(1.0f, 1.0f, 1.0f);
const vec3 colors[NUM_CASCADES] = vec3[NUM_CASCADES](red, green, blue, yellow, teal, orange, black, white);
layer_color = mix(colors[prev_layer_index], colors[layer_index], percent_between);
_____
const struct {const GLuint vertex_buffer, vertex_spec;} world_corners;
_____
if (branch && UV.x < 0.0f || UV.y < 0.0f || UV.x > 1.0f || UV.y > 1.0f) {
return 0.0f;
}
_____
/* (triangle counts, 12 vs 17):
palace: 1466 vs 1130. tpt: 232 vs 150.
pyramid: 816 vs 542. maze: 5796 vs 6114.
terrain: 150620 vs 86588. */
_____
////////////////////
// TWEAK_REALTIME_VALUE(zs, 0.0f, 0.0f, 100.0f, 0.1f, T, Y, U);
////////////////////
_____
// TODO: test shadows while rotating the camera
mat4 light_view;
if (keys[SDL_SCANCODE_C]) {
vec3 light_eye;
glm_vec3_add(camera_sub_frustum_center, (GLfloat*) shadow_context -> light_dir, light_eye);
glm_lookat(light_eye, camera_sub_frustum_center, (vec3) {0.0f, 1.0f, 0.0f}, light_view);
}
else {
vec3 a;
// glm_vec3_sub(camera_sub_frustum_center, (GLfloat*) shadow_context -> light_dir, a);
glm_vec3_copy((GLfloat*) shadow_context -> light_dir, a);
glm_lookat(camera_sub_frustum_center, a, (vec3) {0.0f, 1.0f, 0.0f}, light_view);
/*
vec3 d;
glm_vec3_copy((GLfloat*) shadow_context -> light_dir, d);
glm_vec3_negate(d);
// glm_look(camera_sub_frustum_center, (GLfloat*) shadow_context -> light_dir, (vec3) {0.0f, 1.0f, 0.0f}, light_view);
*/
}
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
printf("%lf ", light_view[y][x]);
}
puts("");
}
puts("---");
_____
/*
const GLfloat z_scale = shadow_context -> z_scale;
const GLfloat one_over_z_scale = 1.0f / z_scale;
GLfloat
*const min_z = &light_view_frustum_box[0][2],
*const max_z = &light_view_frustum_box[1][2];
*min_z *= (*min_z < 0.0f) ? z_scale : one_over_z_scale;
*max_z *= (*max_z < 0.0f) ? one_over_z_scale : z_scale;
if (keys[SDL_SCANCODE_C]) {
GLfloat s = 2.0f;
GLfloat one_over_s = 1.0f / s;
GLfloat
*const min_x = &light_view_frustum_box[0][1],
*const max_x = &light_view_frustum_box[1][1];
*min_x *= (*min_x < 0.0f) ? s : one_over_s;
*max_x *= (*max_x < 0.0f) ? one_over_s : s;
}
*/
_____
////
ON_FIRST_CALL(puts("x = TYU, y = GHJ, z = BNM"););
TWEAK_REALTIME_VALUE(x, 1.0f, 1.0f, 50.0f, 0.005f, T, Y, U);
TWEAK_REALTIME_VALUE(z, 5.0f, 1.0f, 20.0f, 0.005f, B, N, M);
vec3 light_view_frustum_box_scale = {x, x, z};
////
_____
/* To try:
- With and without anisotropic filtering (done)
- Different combos of border modes
- Setting the border mode for R
- A static ESM exponent (edge problem still persists, done)
Notes:
- Anisotropic filtering causes the little stitch
*/
_____
/*
const GLfloat cx = billboard.pos[0], cy = billboard.pos[1], cz = billboard.pos[2];
const GLfloat half_height = billboard.size[0] * 0.5f, half_xz_extent = billboard.size[1] * 0.5f;
*/
_____
typedef struct {
const vec3 center;
const GLfloat radius;
} Sphere;
// https://stackoverflow.com/questions/25572337/frustum-and-sphere-intersection
static bool is_inside_plane(const Sphere sphere, const vec4 plane) {
const GLfloat dist_btwn_plane_and_sphere = glm_vec3_dot((GLfloat*) sphere.center, (GLfloat*) plane) + plane[3];
return dist_btwn_plane_and_sphere > -sphere.radius;
}
_____
if (billboard == (Billboard*) cpu_billboards.data + 6) found_needed = true;
_____
const Billboard* const billboards = billboard_context -> draw_context.buffers.cpu.data;
_____
/*
const bool chosen = glm_vec3_eqv((GLfloat*) billboard.pos, (vec3) {4.5f, 4.0f, 12.5f});
puts(chosen ? "Yah" : "Nah");
*/
_____
////////// This part concerns computing the convex hull of a sector list (for determining the far clip distance)
static GLfloat convex_hull_of_sectors(const List* const sectors) {
}
_____
// Returns the number of visible faces
static buffer_size_t fill_sector_vertex_buffer_with_visible_faces(
const BatchDrawContext* const draw_context,
const List sectors, const Camera* const camera) {
use_vertex_buffer(draw_context -> buffers.gpu);
const Sector* const out_of_bounds_sector = (Sector*) sectors.data + sectors.length;
/* Each vec4 plane in `frustum_planes` is composed of a vec3 surface
normal and the closest distance to the origin in the fourth component */
const vec4* const frustum_planes = camera -> frustum_planes;
const face_mesh_t* const face_meshes_cpu = draw_context -> buffers.cpu.data;
face_mesh_t* const face_meshes_gpu = init_mapping_for_culled_batching(draw_context);
buffer_size_t num_visible_faces = 0;
for (const Sector* sector = sectors.data; sector < out_of_bounds_sector; sector++) {
buffer_size_t num_visible_faces_in_group = 0;
const buffer_size_t initial_face_index = sector -> face_range.start;
while (sector < out_of_bounds_sector && sector_in_view_frustum(*sector, frustum_planes))
num_visible_faces_in_group += sector++ -> face_range.length;
if (num_visible_faces_in_group != 0) {
memcpy(face_meshes_gpu + num_visible_faces,
face_meshes_cpu + initial_face_index,
num_visible_faces_in_group * sizeof(face_mesh_t));
num_visible_faces += num_visible_faces_in_group;
}
}
deinit_current_mapping_for_culled_batching();
return num_visible_faces;
}
_____
static bool sector_in_view_frustum(const Sector sector, const vec4 frustum_planes[planes_per_frustum]) {
// First corner is bottom left (if looking top-down, top left), and second is top right
vec3 aabb_corners[2] = {{sector.origin[0], sector.visible_heights.min, sector.origin[1]}};
aabb_corners[1][0] = aabb_corners[0][0] + sector.size[0];
aabb_corners[1][1] = aabb_corners[0][1] + sector.visible_heights.max - sector.visible_heights.min;
aabb_corners[1][2] = aabb_corners[0][2] + sector.size[1];
return glm_aabb_frustum(aabb_corners, (vec4*) frustum_planes);
}
_____
/*
-1, 1, -1,
-1, -1, -1,
1, -1, -1,
1, -1, -1,
1, 1, -1,
-1, 1, -1,
-1, -1, 1,
-1, -1, -1,
-1, 1, -1,
-1, 1, -1,
-1, 1, 1,
-1, -1, 1,
1, -1, -1,
1, -1, 1,
1, 1, 1,
1, 1, 1,
1, 1, -1,
1, -1, -1,
-1, -1, 1,
-1, 1, 1,
1, 1, 1,
1, 1, 1,
1, -1, 1,
-1, -1, 1,
-1, 1, -1,
1, 1, -1,
1, 1, 1,
1, 1, 1,
-1, 1, 1,
-1, 1, -1,
-1, -1, -1,
-1, -1, 1,
1, -1, -1,
1, -1, -1,
-1, -1, 1,
1, -1, 1
*/
_____
GLint bytes_for_vertices;
glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &bytes_for_vertices);
_____
/*
const vec4 projectedCenter = XMVector4Transform(XMVectorSetW(center, 1.0f), cascadeViewProjection);
const float w = proj_center.w;
const float x = floor((proj_center.x) / w) * inv_texel_scale) * texel_scale;
const float y = floor((proj_center.y) / w) * inv_texel_scale) * texel_scale;
const float z = proj_center.z / w;
vec4 correctedCenter = vec4(x, y, x, 1.0f) * inverse(view_projection);
vec4 finalCenter = correctedCenter / XMVectorGetW(correctedCente
*/
_____
GLfloat size = 1024.0f;
GLfloat *const min = light_view_frustum_box[0], *const max = light_view_frustum_box[1];
GLfloat quant_step = 2.0f / size;
GLfloat qx = remainderf(min[0], quant_step);
GLfloat qy = remainderf(min[1], quant_step);
min[0] += qx;
min[1] += qy;
/*
max[0] += size;
max[1] += size;
*/
/*
var quantizationStep = 1.0f / shadowMapSize;
var qx = (float)Math.IEEERemainder(min.X, quantizationStep);
var qy = (float)Math.IEEERemainder(min.Y, quantizationStep);
min.X -= qx;
min.Y -= qy;
max.X += shadowMapSize;
max.Y += shadowMapSize;
*/
_____
for (byte i = 0; i < 4; i++) column[i] = roundf(column[i]);
_____
static void model_stuff(mat4 model, vec4 world_corners[corners_per_quad]) {
////////// Scale, then translate, then rotate
glm_mat4_copy(GLM_MAT4_IDENTITY, model);
// This moves the weapon in the positive x direction
// TWEAK_REALTIME_VALUE(o, 0.0f, 0.0f, 2.0f, 0.01f, T, Y, U); glm_translate_z(model, o);
// Try to tilt the front of the weapon up when the player tilts down
// TWEAK_REALTIME_VALUE(o, 0.0f, 0.0f, 2.0f, 0.01f, T, Y, U); glm_rotate_z(GLM_MAT4_IDENTITY, glm_rad(o), model);
// TWEAK_REALTIME_VALUE(o, 0.0f, 0.0f, 2.0f, 0.01f, T, Y, U); glm_rotate_x(GLM_MAT4_IDENTITY, glm_rad(o), model);
// incr/scale front world corners
}
_____
// GLfloat* const third = world_corners[2];
// third[0] += o;
// glm_vec3_copy(third, (vec3) {0.0f, 0.0f, 0.0f});
// glm_vec3_copy((vec3) {2.320565f, 0.500023f, 2.041909f}, third);
// TWEAK_REALTIME_VALUE(o, 0.0f, 0.0f, 2.0f, 0.01f, T, Y, U); glm_rotate_y(GLM_MAT4_IDENTITY, glm_rad(o), m);
// vec3 axis = {1.0f, 0.0f, 0.0f};
// TWEAK_REALTIME_VALUE(o, 0.0f, 0.0f, 2.0f, 0.05f, T, Y, U);
glm_mat4_copy(GLM_MAT4_IDENTITY, m);
// glm_rotate_at(m, axis, glm_rad(o), center);
_____
vec3 in_front;
glm_vec3_add(camera -> pos, dir, in_front);
glm_vec3_copy(in_front, top_left);
glm_vec3_copy(in_front, top_left);
_____
change[0] = TWO_PI - change[0];
change[1] = TWO_PI - change[1];
change[2] = TWO_PI - change[2];
_____
// Corner order: bl, br, tl, tr
_____
if (!keys[SDL_SCANCODE_G]) {
glm_vec3_scale((GLfloat*) camera -> right, -tilt_amount, change);
glm_vec3_abs(change, change);
// Perhaps tilt by the up vector?
GLfloat *const bl = world_corners[0], *const br = world_corners[1];
glm_vec3_add(bl, change, bl);
glm_vec3_add(br, change, br);
}
_____
glm_quat_rotatev
_____
TWEAK_REALTIME_VALUE(o, 0.0f, -2.0f, 2.0f, 0.005f, T, Y, U); camera -> angles.tilt = o;
_____ An attempt at texel snapping:
const GLfloat texel_scale = 2.0f / 1024.0f;
const GLfloat inv_texel_scale = 1.0f / texel_scale;
vec4 projected_center;
glm_mat4_mulv(light_view_projection, camera_sub_frustum_center, projected_center);
const GLfloat w = projected_center[3];
DEBUG_VEC4(projected_center);
const vec4 projected_corrected_center = {
floorf((projected_center[0] / w) * inv_texel_scale) * texel_scale,
floorf((projected_center[1] / w) * inv_texel_scale) * texel_scale,
projected_center[2] / w, 1.0f
};
DEBUG_VEC4(projected_corrected_center);
mat4 inv_light_view_projection;
glm_mat4_inv(light_view_projection, inv_light_view_projection);
vec4 corrected_world_space_center;
glm_mat4_mulv(inv_light_view_projection, (GLfloat*) projected_corrected_center, corrected_world_space_center);
glm_vec4_scale(corrected_world_space_center, 1.0f / corrected_world_space_center[3], corrected_world_space_center);
DEBUG_VEC4(camera_sub_frustum_center);
DEBUG_VEC4(corrected_world_space_center);
puts("---");
vec4 c[8];
glm_frustum_corners(inv_light_view_projection, c);
////////// Now this
get_light_view(corrected_world_space_center, shadow_context -> light_dir, light_view);
get_light_projection(camera_sub_frustum_corners, shadow_context -> sub_frustum_scale, light_view, light_projection);
glm_mul(light_projection, light_view, light_view_projection);
_____
typedef struct {
GLuint face_normal_map_set;
BatchDrawContext draw_context;
List sectors;
} SectorContext;
_____
static void erase_version_strings_from_dependency_list(const List* const dependency_list) {
const GLchar *const base_version_string = "#version", *const full_version_string = "#version ___ core\n";
const GLsizei full_version_string_length = strlen(full_version_string);
// Not erasing the version string from the first one because it's the only one that should keep #version in it
LIST_FOR_EACH(1, dependency_list, untyped_dependency, _,
const GLchar* const dependency = *((GLchar**) untyped_dependency);
/*
ON_FIRST_CALL(
DEBUG(dependency, s);
DEBUG(count_num_lines_in_string(dependency), u);
);
*/
GLchar* const version_string_pos = strstr(dependency, base_version_string);
if (version_string_pos != NULL) {
memset(version_string_pos, ' ', full_version_string_length);
// in w/ a #line stmt
// TODO: avoid some of the unnecessary memset
// Only need to set the line number for the include string
/*
const buffer_size_t dependency_line_count = count_num_lines_in_string(dependency) + 1;
char line_stmt[100];
sprintf(line_stmt, "#line %d", dependency_line_count);
strncpy(version_string_pos, line_stmt, strlen(line_stmt) - 1);
ON_FIRST_CALL(
puts("----------");
DEBUG(dependency, s);
puts("----------");
DEBUG(dependency_line_count, u);
DEBUG(line_stmt, s);
puts("<---------->");
DEBUG(version_string_pos, s);
puts("<---------->");
);
*/
}
);
}
_____
/*
if (*string == '\0') return 0;
buffer_size_t num_lines = 0;
while (true) {
string = strchr(string + 1, '\n');
if (string == NULL) break;
num_lines++;
}
return num_lines;
*/
_____
/*
- perhaps before each include, get the so-far line count, and then specify the line number after
- or put #line 0 at the end of each shader - but no that wouldn't work b/c don't want to fully reset it then
*/
_____
// This is done so that the GLSL compiler can report errors with correct line numbers
static void replace_include_directive_with_line_directive( // The `consts` in front here indicate read-only; not that of `restrict`
const GLchar* const sub_shader_path, const GLchar* const sub_shader_start,
GLchar* const include_string_start, const GLchar* const after_include_string) {
////////// Calculating the number of lines before + with the include string
buffer_size_t line_number_after_include_string = 2;
const GLchar* copy_sub_shader_start = sub_shader_start;
for (GLchar c = *copy_sub_shader_start; c != '\0'; c = *(++copy_sub_shader_start)) {
if (c == '\n') line_number_after_include_string++;
}
////////// Calculating the number of bytes needed for the include string
const buffer_size_t num_bytes_for_include_string = (buffer_size_t) (after_include_string - include_string_start);
////////// Calculating the number of bytes needed for the line directive
const GLchar* const line_directive_format = "#line %u";
// One more byte for the null terminator
const buffer_size_t num_bytes_for_line_directive = (buffer_size_t) snprintf(NULL, 0,
line_directive_format, line_number_after_include_string) + 1;
if (num_bytes_for_line_directive > num_bytes_for_include_string) // Too many bytes for the line number then
FAIL(CreateShader, "The shader '%s' is too long", sub_shader_path);
////////// Replacing the bytes of the include string with the line directive, and filling the rest with blank spaces
sprintf(include_string_start, line_directive_format, line_number_after_include_string);
const buffer_size_t num_remaining_bytes = num_bytes_for_include_string - num_bytes_for_line_directive;
/* - 1 for the dest because the early null terminator from `sprintf` should be erased too.
+ 1 for the amount to erase the null terminator from the previous #include. */
memset(include_string_start + num_bytes_for_line_directive - 1, ' ', num_remaining_bytes + 1);
// TODO: make line numbers correct in included files
}
_____
////////// Fetching the included code, and replacing the #include region with whitespace
GLchar* const included_code = get_source_for_included_file(dependency_list, sub_shader_path, after_include_string + 1);
push_ptr_to_list(dependency_list, &included_code); // The included code is freed by `init_shader`
replace_include_directive_with_line_directive(sub_shader_path, sub_shader_code, include_string, curr_path_substring);
return true;
}
_____
if (!strcmp(vertex_shader_path, "../assets/shaders/weapon.vert")) {
List dep = dependency_lists[0];
char* first = ((GLchar**) dep.data)[dep.length - 1];
DEBUG(first, s);
}
_____
typedef struct {
struct {
List cpu; - done
GLuint gpu; - done
} buffers;
GLuint
vertex_spec, - done
texture_set, - done
shader; - done
} BatchDrawContext;
typedef struct {
GLuint face_normal_map_set; - done
BatchDrawContext draw_context; - done
List sectors; - done
} SectorContext;
_____
GLfloat compute_world_far_clip_dist(const byte* const heightmap, const byte map_size[2]) {
/* The far clip distance, ideally, would be equal to the diameter of
the convex hull of all points in the heightmap. If I had more time,
I would implement that, but a simple method that works reasonably well is this:
- First, find the smallest and tallest points in the map.
- Then, the far clip distance equals the length of
the `<map_width, map_height, (tallest_point - smallest_point) + additional_camera_height>` vector.
To compute the maximum jump height, use the kinematics equation `v^2 = v0^2 + 2aΔy`.
Given that `v` equals 0, rearrange the equation like this:
0 = v0^2 + 2aΔy
-(v0^2) = 2aΔy
-(v0^2)/2a = Δy
And since downward acceleration is positive in `constants`, to not get a negative result,
remove the negative sign of the left term.
Then, `additional_camera_height` equals `max_jump_height + eye_height`. */
const byte map_size_x = map_size[0], map_size_z = map_size[1];
byte min_point_height = constants.max_byte_value, max_point_height = 0;
for (byte y = 0; y < map_size_z; y++) {
for (byte x = 0; x < map_size_x; x++) {
const byte height = sample_map_point(heightmap, x, y, map_size_x);
if (height < min_point_height) min_point_height = height;
if (height > max_point_height) max_point_height = height;
}
}
const GLfloat max_jump_height = (constants.speeds.jump * constants.speeds.jump) / (2.0f * constants.accel.g);
const GLfloat additional_camera_height = max_jump_height + constants.camera.eye_height;
const GLfloat max_z_difference = (max_point_height - min_point_height) + additional_camera_height;
return glm_vec3_norm((vec3) {map_size_x, map_size_z, max_z_difference});
}
_____
const byte map_size_x = map_size[0], map_size_z = map_size[1];
GLfloat max_dist_between_points = 0.0f;
vec3 src, dest;
for (src[2] = 0.0f; src[2] < map_size_z; src[2]++) {
for (src[0] = 0.0f; src[0] < map_size_x; src[0]++) {
src[1] = sample_map_point(heightmap, src[0], src[1], map_size_x);
for (dest[2] = 0.0f; dest[2] < map_size_z; dest[2]++) {
for (dest[0] = 0.0f; dest[0] < map_size_x; dest[0]++) {
dest[1] = sample_map_point(heightmap, dest[0], dest[1], map_size_x);
const GLfloat dist = glm_vec3_distance(src, dest); // TODO: no sqrt
if (dist > max_dist_between_points) {
printf("Set to %lf\n", dist);
max_dist_between_points = dist;
}
}
}
}
}
return max_dist_between_points;