Space Sim
Loading...
Searching...
No Matches
mesh.c
Go to the documentation of this file.
1
2#include "core/init.h"
3#include "utilities/mathf.h"
4#include "core/types.h"
5#include "mesh/mesh.h"
6#include "uthash/uthash.h"
7#include "core/camera.h"
8#include <assert.h>
9#include <stdint.h>
10#include <stdio.h>
11#include <float.h>
12
13
14
15
16typedef enum FACE
17{
24
25} FACE;
26
27
31#define ATMOSPHERE_IDX(x, y, o) ((o) + (y) * ATMOSPHERE_RES + (x))
32
33
34
35vec3
37 (
38 FACE face,
39 vec2 uv
40 )
41
42{
43
44 switch(face) {
45
46 case XPOS: return (vec3){ 1, uv.y, -uv.x};
47 case XNEG: return (vec3){-1, uv.y, uv.x};
48 case YPOS: return (vec3){ uv.x, 1, -uv.y};
49 case YNEG: return (vec3){ uv.x, -1, uv.y};
50 case ZPOS: return (vec3){ uv.x, uv.y, 1};
51 case ZNEG: return (vec3){-uv.x, uv.y, -1};
52
53 }
54
55}
56
57vec3
59 (
60 vec3 a,
61 vec3 b,
62 vec3 c
63 )
64
65{
66
67 vec3 u = (vec3){b.x - a.x, b.y - a.y, b.z - a.z};
68 vec3 v = (vec3){c.x - a.x, c.y - a.y, c.z - a.z};
69 return (vec3){u.y * v.z - u.z * v.y, u.z * v.x - u.x * v.z, u.x * v.y - u.y * v.x};
70
71}
72
73float
75 ( float x )
76{ return x - floorf(x * (1.0f / 289.0f)) * 289.0f; }
77
78vec4
80 ( vec4 x )
81
82{
83
84 vec4 p = vec4_mul(
86 vec4_scale(x, 34.0f),
87 (vec4){ 1.0f, 1.0f, 1.0f, 1.0f }
88 ),
89 x
90 );
91
92 p.x = mod289(p.x);
93 p.y = mod289(p.y);
94 p.z = mod289(p.z);
95 p.w = mod289(p.w);
96 return p;
97
98}
99
100vec3
102 (vec3 x)
103
104{
105
106 vec3 p;
107 p.x = x.x - floorf(x.x);
108 p.y = x.y - floorf(x.y);
109 p.z = x.z - floorf(x.z);
110
111 return p;
112
113}
114
115vec4
117 ( vec4 x )
118
119{
120
121 vec4 p;
122 p.x = x.x - floorf(x.x);
123 p.y = x.y - floorf(x.y);
124 p.z = x.z - floorf(x.z);
125 p.w = x.w - floorf(x.w);
126
127 return p;
128
129}
130
131
132
133float
135 ( vec3 p )
136
137{
138
139 vec3 a = (vec3){floorf(p.x), floorf(p.y), floorf(p.z)};
140 vec3 d = vec3_sub(p, a);
141
142 d = vec3_mul(vec3_mul(d, d), vec3_sub((vec3){3.0f, 3.0f, 3.0f}, vec3_scale(d, 2.0f)));
143
144 vec4 b = vec4_add((vec4){a.x, a.x, a.y, a.y}, (vec4){0.0f, 1.0f, 0.0f, 1.0f});
145 vec4 k1 = perm((vec4){b.x, b.y, b.x, b.y});
146 vec4 k2 = perm(vec4_add((vec4){k1.x, k1.y, k1.x, k1.y}, (vec4){b.z, b.z, b.w, b.w}));
147
148 vec4 c = vec4_add(k2, (vec4){a.z, a.z, a.z, a.z});
149 vec4 k3 = perm(c);
150 vec4 k4 = perm(vec4_add(c, (vec4){1.0f, 1.0f, 1.0f, 1.0f}));
151
152 vec4 o1 = vec4_fract(vec4_scale(k3, (1.0f / 41.0f)));
153 vec4 o2 = vec4_fract(vec4_scale(k4, (1.0f / 41.0f)));
154
155 vec4 o3 = vec4_add(vec4_scale(o2, d.z), vec4_scale(o1, (1.0f - d.z)));
156 vec2 o4 = vec2_add(vec2_scale((vec2){o3.y, o3.w}, d.x), vec2_scale((vec2){o3.x, o3.z}, (1.0f - d.x)));
157
158 return o4.y * d.y + o4.x * (1.0 - d.y);
159
160}
161
162float
164 ( vec3 p )
165
166{
167
168 float v = 0.0;
169 float a = 0.5;
170 float f = 1.0;
171
172 vec3 shift = (vec3){100, 100, 100};
173 for (int i = 0; i < 5; ++i) {
174
175 v += a * fbm_noise(vec3_scale(p, f));
176
177 p = vec3_add(vec3_scale(p, 2.0), shift);
178 f *= 2.5;
179 a *= 0.45;
180
181 }
182
183 return v;
184
185}
186
187
188
189float
191 (
192 float x,
193 float size
194 )
195
196{
197 float r = fmodf(x, size);
198 if (r < 0) r += size;
199 return r;
200}
201
203{
204 a.x = wrap(a.x, size);
205 a.y = wrap(a.y, size);
206 a.z = wrap(a.z, size);
207 return a;
208}
209
210
216float
218 ( vec3 p )
219
220{
221
222 ivec3 pf = {floorf(p.x), floorf(p.y), floorf(p.z)};
223 int pm1 = PERLIN_TEXTURE_SIZE - 1;
224
225 pf.x &= pm1;
226 pf.y &= pm1;
227 pf.z &= pm1;
228 int px1 = (int)(pf.x + 1) & pm1;
229 int py1 = (int)(pf.y + 1) & pm1;
230 int pz1 = (int)(pf.z + 1) & pm1;
231 ivec3 pf1 = {px1, py1, pz1};
232
233
234 float fx = p.x - floorf(p.x);
235 float fy = p.y - floorf(p.y);
236 float fz = p.z - floorf(p.z);
237
238
242 float e3 = perlin_tex[ pf.z * PERLIN_TEXTURE_SIZE * PERLIN_TEXTURE_SIZE + pf1.y * PERLIN_TEXTURE_SIZE + pf1.x ];
244 float e5 = perlin_tex[ pf1.z * PERLIN_TEXTURE_SIZE * PERLIN_TEXTURE_SIZE + pf.y * PERLIN_TEXTURE_SIZE + pf1.x ];
245 float e6 = perlin_tex[ pf1.z * PERLIN_TEXTURE_SIZE * PERLIN_TEXTURE_SIZE + pf1.y * PERLIN_TEXTURE_SIZE + pf.x ];
246 float e7 = perlin_tex[ pf1.z * PERLIN_TEXTURE_SIZE * PERLIN_TEXTURE_SIZE + pf1.y * PERLIN_TEXTURE_SIZE + pf1.x ];
247
248 float c00 = lerp(e0, e1, fx);
249 float c10 = lerp(e2, e3, fx);
250 float c01 = lerp(e4, e5, fx);
251 float c11 = lerp(e6, e7, fx);
252
253 float c0 = lerp(c00, c10, fy);
254 float c1 = lerp(c01, c11, fy);
255
256 float out = lerp(c0, c1, fz);
257
258 return out * 2.0f - 1.0f;
259
260}
261
262
268float
270 ( vec3 p )
271
272{
273
274 ivec3 pf = {floorf(p.x), floorf(p.y), floorf(p.z)};
275
276
277 pf.x &= FBM_TEXTURE_SIZE-1;
278 pf.y &= FBM_TEXTURE_SIZE-1;
279 pf.z &= FBM_TEXTURE_SIZE-1;
280 int SIZE = FBM_TEXTURE_SIZE;
281 int px1 = (int)(pf.x + 1) & (FBM_TEXTURE_SIZE-1);
282 int py1 = (int)(pf.y + 1) & (FBM_TEXTURE_SIZE-1);
283 int pz1 = (int)(pf.z + 1) & (FBM_TEXTURE_SIZE-1);
284 ivec3 pf1 = {px1, py1, pz1};
285
286
287 float fx = p.x - floorf(p.x);
288 float fy = p.y - floorf(p.y);
289 float fz = p.z - floorf(p.z);
290
291
292 float e0 = fbm_tex[ pf.z * SIZE * SIZE + pf.y * SIZE + pf.x ];
293 float e1 = fbm_tex[ pf.z * SIZE * SIZE + pf.y * SIZE + pf1.x ];
294 float e2 = fbm_tex[ pf.z * SIZE * SIZE + pf1.y * SIZE + pf.x ];
295 float e3 = fbm_tex[ pf.z * SIZE * SIZE + pf1.y * SIZE + pf1.x ];
296 float e4 = fbm_tex[ pf1.z * SIZE * SIZE + pf.y * SIZE + pf.x ];
297 float e5 = fbm_tex[ pf1.z * SIZE * SIZE + pf.y * SIZE + pf1.x ];
298 float e6 = fbm_tex[ pf1.z * SIZE * SIZE + pf1.y * SIZE + pf.x ];
299 float e7 = fbm_tex[ pf1.z * SIZE * SIZE + pf1.y * SIZE + pf1.x ];
300
301 float c00 = lerp(e0, e1, fx);
302 float c10 = lerp(e2, e3, fx);
303 float c01 = lerp(e4, e5, fx);
304 float c11 = lerp(e6, e7, fx);
305
306 float c0 = lerp(c00, c10, fy);
307 float c1 = lerp(c01, c11, fy);
308
309 float out = lerp(c0, c1, fz);
310
311 return out * 2.0f - 1.0f;
312
313}
314
315
322float
324 ( vec3 p )
325
326{
327
328 float d = p.z;
329
330 vec3 chunk_origin = vec3_scale(p, (float) 1 / CHUNK_WORLD_SIZE);
331 chunk_origin.x = floorf(chunk_origin.x);
332 chunk_origin.y = floorf(chunk_origin.y);
333 chunk_origin.z = floorf(chunk_origin.z);
334 chunk_origin = vec3_scale(chunk_origin, CHUNK_WORLD_SIZE);
335
336
337 // Apply equal density & noise calculations to density.comp
338 // UV has to be adjusted to wrap the noise textures and match opengl wrapping
339 for (int i = 0; i < PLANET_DATA.warp_count; i++)
340 {
341
343 vec3 uvw = vec3_add(vec3_scale(p, wl->freq0), wl->offset0);
344 uvw.x -= floorf(uvw.x);
345 uvw.y -= floorf(uvw.y);
346 uvw.z -= floorf(uvw.z);
347 vec3 w1 = vec3_add(vec3_scale(uvw, PERLIN_TEXTURE_SIZE), (vec3){-0.5f, -0.5f, -0.5f});
348
349 uvw = vec3_add(vec3_scale(p, wl->freq1), wl->offset1);
350 uvw.x -= floorf(uvw.x);
351 uvw.y -= floorf(uvw.y);
352 uvw.z -= floorf(uvw.z);
353 vec3 w2 = vec3_add(vec3_scale(uvw, PERLIN_TEXTURE_SIZE), (vec3){-0.5f, -0.5f, -0.5f});
354
355 uvw = vec3_add(vec3_scale(p, wl->freq2), wl->offset2);
356 uvw.x -= floorf(uvw.x);
357 uvw.y -= floorf(uvw.y);
358 uvw.z -= floorf(uvw.z);
359 vec3 w3 = vec3_add(vec3_scale(uvw, PERLIN_TEXTURE_SIZE), (vec3){-0.5f, -0.5f, -0.5f});
360
361 vec3 warp = {sample_density(w1), sample_density(w2), sample_density(w3)};
362 p = vec3_add(p, vec3_scale(warp, wl->amp));
363
364 }
365
366 for (int i = 0; i < PLANET_DATA.perlin_count; i++)
367 {
368
370 vec3 uvw = vec3_scale(p, pl->freq);
371 uvw.x -= floorf(uvw.x);
372 uvw.y -= floorf(uvw.y);
373 uvw.z -= floorf(uvw.z);
374
375 vec3 t = vec3_add(vec3_scale(uvw, PERLIN_TEXTURE_SIZE), (vec3){-0.5f, -0.5f, -0.5f});
376 float d0 = sample_density(t) * pl->amp;
377 d += d0;
378
379 }
380
381 for (int i = 0; i < PLANET_DATA.fbm_count; i++)
382 {
383
385 vec3 uvw = vec3_scale(p, fl->freq);
386 uvw.x -= floorf(uvw.x);
387 uvw.y -= floorf(uvw.y);
388 uvw.z -= floorf(uvw.z);
389 vec3 t = vec3_add(vec3_scale(uvw, FBM_TEXTURE_SIZE), (vec3){-0.5f, -0.5f, -0.5f});
390 float d0 = sample_fbm_density(t) * fl->amp;
391 d += d0;
392
393 }
394
395 return d;
396
397}
398
399// for each voxel 0.. 33^3
400// scale by voxel size and add to chunk offset
401// calculate density
402// slot density into 33^3 grid
403//
404// marching cubes per 0.. 32
405// append to vertex buffer
406// atomically increment count by 3
407// append tri verts
408//
409// glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT |
410// GL_ATOMIC_COUNTER_BARRIER_BIT);
411//
412// glMemoryBarrier(GL_COMMAND_BARRIER_BIT);
413// draw
414
415
416// skirts
417//
418// vec3 v0 = original_vertex;
419// vec3 v1 = v0 - normal * skirt_depth;
420// (v0, v0_next, v1)
421// (v1, v0_next, v1_next)
422
423// Create a mesh for a certain chunk
432void
434 (
435 int lod,
436 ivec3 chunk_center,
437 int chunk_id,
438 bool is_parent
439 )
440
441{
442
443 int size = LOD_RES[lod];
444 uint32_t uid = CHUNK_DATA[lod][chunk_id].uID;
445
446 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 9, DENSITY_SSBO[lod]);
447 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 10, DENSITY_VERTEX_SSBO[lod]);
448 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 11, INDIRECT_BUFFER[lod][chunk_id]);
449
450 glActiveTexture(GL_TEXTURE0);
451 glBindTexture(GL_TEXTURE_3D, PERLIN_TEXTURE);
452 glActiveTexture(GL_TEXTURE1);
453 glBindTexture(GL_TEXTURE_3D, FBM_TEXTURE);
454
455 // Density function computes the SDF
456 glUseProgram(DENSITY_COMPUTE_SHADER.program);
459 glUniform3f(DENSITY_COMPUTE_SHADER.uniforms[2].location, chunk_center.x, chunk_center.y, chunk_center.z);
461 glUniform1i(DENSITY_COMPUTE_SHADER.uniforms[4].location, 0);
462 glUniform1i(DENSITY_COMPUTE_SHADER.uniforms[5].location, 1);
463 glDispatchCompute(size+1, size+1, size+1);
464 glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
465
466 // Marching cubes the mesh
470 glUniform3f(MARCHING_CUBES_COMPUTE_SHADER.uniforms[2].location, chunk_center.x, chunk_center.y, chunk_center.z);
473 glDispatchCompute(size, size, size);
474 glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
475 glMemoryBarrier(GL_COMMAND_BARRIER_BIT);
476
477 // Generates an indirect draw command
478 glUseProgram(INDIRECT_COMPUTE_SHADER.program);
482 glUniform1i(glGetUniformLocation(INDIRECT_COMPUTE_SHADER.program, "is_parent"), is_parent);
483 glUniform1i(glGetUniformLocation(INDIRECT_COMPUTE_SHADER.program, "chunk_uid"), uid);
484 glDispatchCompute(1, 1, 1);
485 glMemoryBarrier(GL_COMMAND_BARRIER_BIT);
486
487
488
489}
490
491bool
493 (
494 ivec3 min,
495 ivec3 max,
496 vec4 planes[6]
497 )
498
499{
500
501 for (int i = 0; i < 6; i++)
502 {
503 vec3 p = {
504 planes[i].x > 0 ? max.x : min.x,
505 planes[i].y > 0 ? max.y : min.y,
506 planes[i].z > 0 ? max.z : min.z
507 };
508
509 if (vec3_dot((vec3){planes[i].x, planes[i].y, planes[i].z}, p) + planes[i].w < 0)
510 {
511 return false;
512 }
513 }
514
515 return true;
516
517}
518
519
520// Struct for the scores that determines how valuable a chunk is to render
521
522
526void
528 (
529 vec4 planes[6],
530 Camera * cam,
531 vec3 planet_offset,
532 float planet_radius
533 )
534
535{
536
537 vec3 cam_pos = (vec3){
538 cam->cam_data.cam_pos.x - planet_offset.x,
539 cam->cam_data.cam_pos.y - planet_offset.y,
540 cam->cam_data.cam_pos.z - planet_offset.z - planet_radius
541 };
542
543 Camera fake_cam;
544 fake_cam.cam_data.cam_pos = (vec4){cam_pos.x, cam_pos.y, cam_pos.z, 0};
545 fake_cam.pitch = cam->pitch;
546 fake_cam.yaw = cam->yaw;
547
548 update_camera(&fake_cam);
549
550 mat4 VP = m4_mul(fake_cam.cam_data.proj, fake_cam.cam_data.view);
551
552 vec4 r0 = (vec4){VP.a0, VP.b0, VP.c0, VP.d0};
553 vec4 r1 = (vec4){VP.a1, VP.b1, VP.c1, VP.d1};
554 vec4 r2 = (vec4){VP.a2, VP.b2, VP.c2, VP.d2};
555 vec4 r3 = (vec4){VP.a3, VP.b3, VP.c3, VP.d3};
556
557 planes[0] = vec4_add(r3, r0);
558 planes[1] = vec4_sub(r3, r0);
559 planes[2] = vec4_add(r3, r1);
560 planes[3] = vec4_sub(r3, r1);
561 planes[4] = vec4_add(r3, r2);
562 planes[5] = vec4_sub(r3, r2);
563
564}
565
566
567// qsort functions for sorting chunk jobs
568int
570 (
571 const void * a,
572 const void * b
573 )
574
575{
576
577 ChunkScore * a0 = (ChunkScore*)a;
578 ChunkScore * b0 = (ChunkScore*)b;
579
580 if (b0->score > a0->score) return 1;
581 if (b0->score < a0->score) return -1;
582 return 0;
583
584}
585
586int
588 (
589 const void * a,
590 const void * b
591 )
592
593{
594
595 ChunkScore * a0 = (ChunkScore*)a;
596 ChunkScore * b0 = (ChunkScore*)b;
597
598 if (b0->score < a0->score) return 1;
599 if (b0->score > a0->score) return -1;
600 return 0;
601
602}
603
604
605
606// this is how we do LODS:
607// calculate density
608// calculate MC at low res
609// present
610// LOD increase
611// calculate density
612// calculate MC at high res
613// store separately
614// interpolate based on distance
615
616
619
620// The chunks that have already been emitted this frame
621// Used so that we don't touch chunks that we just made.
622// Copied over on each new frame so that we can have a list of chunks we made during the last frame. Used for the zero list.
624
629int
631 (
632 ivec3 center,
633 ChunkHash ** h
634 )
635
636{
637
638 ChunkHash* best = NULL;
639 int best_lod = -1;
640
641 for (int l = 0; l < LODMAX; l++) {
642
643 ChunkHash* h = find_chunk_hash(center, l);
644 if (h && l > best_lod) {
645 best = h;
646 best_lod = l;
647 }
648
649 }
650
651 *h = best;
652 return best_lod;
653
654}
655
660bool
662 (
663 int lod,
664 int i
665 )
666
667{
668
669 if (lod < 0 || lod >= LODMAX) return false;
670
671 if (!CHUNK_DATA[lod][i].free)
672 {
673
674 delete_chunk_hash(CHUNK_DATA[lod][i].center, lod);
675 CHUNK_DATA[lod][i].free = true;
676 CHUNK_DATA[lod][i].is_child = false;
677 return true;
678
679 }
680
681 return false;
682
683}
684
689bool
691 (
692 int lod,
693 int i
694 )
695
696{
697
698 if (lod < 0 || lod >= LODMAX) return false;
699 int i1 = CHUNK_DATA[lod][i].id1;
700
701 if (CHUNK_DATA[lod][i].id1 != -1 && lod - 1 >= 0)
702 {
703
704 if (CHUNK_DATA[lod-1][i1].id0 == i && !already_emitted[lod-1][i1] && !CHUNK_DATA[lod-1][i1].free)
705 {
706
707 delete_chunk_hash(CHUNK_DATA[lod-1][i1].center, lod-1);
708 CHUNK_DATA[lod-1][i1].free = true;
709 CHUNK_DATA[lod-1][i1].is_child = false;
710 return true;
711
712 }
713
714 }
715 return false;
716
717}
718
723bool
725 (
726 int lod,
727 int i
728 )
729
730{
731
732 if (lod < 0 || lod >= LODMAX) return false;
733 int i1 = CHUNK_DATA[lod][i].id0;
734
735 if (CHUNK_DATA[lod][i].id0 != -1 && lod + 1 < LODMAX)
736 {
737
738 if (CHUNK_DATA[lod+1][i1].id1 == i && !already_emitted[lod+1][i1] && !CHUNK_DATA[lod+1][i1].free)
739 {
740
741 delete_chunk_hash(CHUNK_DATA[lod+1][i1].center, lod+1);
742 CHUNK_DATA[lod+1][i1].free = true;
743 CHUNK_DATA[lod+1][i1].is_child = false;
744 return true;
745
746 }
747
748 }
749 return false;
750
751}
752
753
754uint32_t chunk_uID = 0;
755
756uint32_t
758{ chunk_uID = (chunk_uID + 1) % UINT32_MAX; return chunk_uID; }
759
760
761// The zero cache is a hash table of known empty chunks so that we don't keep generating them at higher LODs and skip over them.
762// AKA below ground or above ground chunks with nothing inside of them.
763#define MAX_ZERO_LIST 100000
764
765typedef struct ZeroKey
766{
767 int x, y, z, lod;
768
769} ZeroKey;
770
771typedef struct ZeroEntry
772{
774 UT_hash_handle hh;
775
776} ZeroEntry;
777
780
781
785// Mask of chunks that are going to be replaced during the frame
787
788
789
790// Inits the zero list to -1 (empty value)
795void
797
798{
799 {
800 ZeroEntry * s;
801 ZeroEntry * tmp;
802 HASH_ITER(hh, zero_hash_table, s, tmp){
803 HASH_DEL(zero_hash_table, s);
804 free(s);
805
806 }
807 }
808 {
809
810 ChunkHash * s;
811 ChunkHash * tmp;
812 HASH_ITER(hh, hash_table, s, tmp){
813 HASH_DEL(hash_table, s);
814 free(s);
815
816 }
817 }
818 {
819
821 AuthorityEntry * tmp;
822 HASH_ITER(hh, authority_map, s, tmp){
823 HASH_DEL(authority_map, s);
824 free(s);
825
826 }
827 }
828 if (jobs)
829 {
830 free(jobs);
831 jobs = NULL;
832 }
833 if (job_data)
834 {
835 free(job_data);
836 job_data = NULL;
837 }
838 if (frees)
839 {
840 free(frees);
841 frees = NULL;
842 }
843
844 jobs = calloc(MAX_CHUNK_LIST * LODMAX, sizeof(ChunkScore));
845 job_data = calloc(MAX_CHUNK_LIST * LODMAX, sizeof(ChunkData));
846 frees = calloc(MAX_CHUNKS, sizeof(ChunkScore));
847 memset(CHUNK_DATA, 0, sizeof(ChunkData) * LODMAX * MAX_CHUNKS);
848 for (int l = 0; l < LODMAX; l++)
849 {
850 for (int i = 0; i < MAX_CHUNKS_LOD[l]; i++)
851 {
852 glGenBuffers(1, &INDIRECT_BUFFER[l][i]);
853
854 glBindBuffer(GL_DRAW_INDIRECT_BUFFER, INDIRECT_BUFFER[l][i]);
855 glBufferData(GL_DRAW_INDIRECT_BUFFER,
857 NULL,
858 GL_DYNAMIC_DRAW);
859 }
860
861 }
862 for (int l = 0; l < LODMAX; l++)
863 {
864 for (int i = 0; i < MAX_CHUNKS; i++)
865 {
866 CHUNK_DATA[l][i].free = true;
867 CHUNK_DATA[l][i].is_child = false;
868 CHUNK_DATA[l][i].uID = -1;
869 CHUNK_DATA[l][i].id0 = -1;
870 CHUNK_DATA[l][i].id1 = -1;
871
872 }
873 }
874
875}
876
878
882
883
885
892void
894 (
896 float planet_radius
897 )
898
899{
900
902
903 Timer start, big_loop, execute;
904
905 start_timer(&start);
906
907 // Copies old chunk emissions into a back buffer
908 bool old_emissions[LODMAX][MAX_CHUNKS];
909 memcpy(old_emissions, already_emitted, sizeof(already_emitted));
910 memset(already_emitted, 0, sizeof(already_emitted));
911 memset(replacement_mask, 0, sizeof(replacement_mask));
912
913 // Swaps the chunk data to the back buffer
914 int read_flag = CHUNK_DATA_FLAG;
915
916 GLenum res = glClientWaitSync(zero_fence, GL_SYNC_FLUSH_COMMANDS_BIT, 0);
917
918 if (res == GL_CONDITION_SATISFIED || res == GL_ALREADY_SIGNALED || emitted_chunks >= VERTEX_COUNT_MAX - LODMAX * MAX_CHUNKS)
919 {
921 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 12, CHUNK_DATA_SSBO[CHUNK_DATA_FLAG]);
922
923 glBindBuffer(GL_SHADER_STORAGE_BUFFER, CHUNK_DATA_SSBO[read_flag]);
924 // Maps the last frame's chunk data vertex counts
925 VertexInfo * ptr = glMapBufferRange(
926 GL_SHADER_STORAGE_BUFFER,
927 offsetof(GPUChunk, vertex_counts),
928 sizeof(VertexInfo) * emitted_chunks,
929 GL_MAP_READ_BIT
930 );
931
933 memcpy(new_list, ptr, sizeof(VertexInfo) * emitted_chunks);
934 glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
935
936 // Zeroes the back buffer
937 glUseProgram(CHUNK_ZERO_COMPUTE_SHADER);
938 glDispatchCompute(1, 1, 1);
939 glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
940
941 // If an emitted chunk has 0 vertices, free it and add it to the zero hash table.
942 for (int c = 0; c < emitted_chunks; c++)
943 {
944
945 int l = emitted_chunk_lods[c];
946 int i = emitted_chunk_ids[c];
947
948 if (new_list[c].vcnt == 0 && new_list[c].uid == CHUNK_DATA[l][i].uID)
949 {
950
951 free_child(l, i);
953
954 ZeroKey k = {CHUNK_DATA[l][i].center.x, CHUNK_DATA[l][i].center.y, CHUNK_DATA[l][i].center.z, l};
955 ZeroEntry * m = NULL;
956 HASH_FIND(hh, zero_hash_table, &k, sizeof(ZeroKey), m);
957 if (!m)
958 {
959
960 ZeroEntry * z = malloc(sizeof(ZeroEntry));
961 memset(z, 0, sizeof(ZeroEntry));
962 z->key = k;
963 HASH_ADD(hh, zero_hash_table, key, sizeof(ZeroKey), z);
964
965 }
966
967
969 }
970
971 }
972 emitted_chunks = 0;
973 }
974
975
976 const float lookahead = 0.25f;
977 // have to move cam pos into planet coords and applies lookahead based on camera velocity
978 ivec3 cam_pos = (ivec3){
982 };
983 cam_pos.x -= planet_origin.x;
984 cam_pos.y -= planet_origin.y;
985 cam_pos.z -= planet_origin.z;
986 cam_pos.z -= planet_radius;
987
988
990 {
991 zero_list_count = 0;
992
993 ZeroEntry * s;
994 ZeroEntry * tmp;
995 HASH_ITER(hh, zero_hash_table, s, tmp){
996 ivec3 center = (ivec3){s->key.x, s->key.y, s->key.z};
997 float dist = ivec3_dist(center, cam_pos);
998 if (dist > CHUNK_RADIUS * CHUNK_WORLD_SIZE)
999 {
1000 HASH_DEL(zero_hash_table, s);
1001 free(s);
1002 }
1003
1004 }
1005 }
1006
1007 // Set up view planes for frustum culling.
1008 vec4 planes[6];
1009 vec4 light_planes[6];
1012
1013
1014 // This particle struct was supposed to change the terrain based on material makeup.
1015
1016
1017 int free_count = 0;
1018 float frustum_weight = 100;
1019 int cam_chunk = -1;
1020 int cam_chunk_lod = 0;
1021
1022 ivec3 MIN_OFFSET = {
1023 -CHUNK_WORLD_SIZE / 2,
1024 -CHUNK_WORLD_SIZE / 2,
1025 -CHUNK_WORLD_SIZE / 2
1026 };
1027
1028 ivec3 MAX_OFFSET = {
1029 CHUNK_WORLD_SIZE / 2,
1030 CHUNK_WORLD_SIZE / 2,
1032 };
1033
1034
1035 vec3 pos = ivec3_scale(cam_pos, 1.0f/CHUNK_WORLD_SIZE);
1036 pos.x = floorf(pos.x);
1037 pos.y = floorf(pos.y);
1038 pos.z = floorf(pos.z);
1039 pos = vec3_scale(pos, CHUNK_WORLD_SIZE);
1040
1041 // Check for if we have a chunk that houses the camera.
1042 ivec3 cam_center_chunk = {pos.x, pos.y, pos.z};
1043 int cam_clod = get_current_lod(cam_center_chunk);
1044 ChunkHash * h = NULL;
1045 h = find_chunk_hash(cam_center_chunk, cam_clod);
1046 if (h)
1047 {
1048
1049 cam_chunk = h->id;
1050 cam_chunk_lod = cam_clod;
1051 }
1052
1053 // Start is the bottom left chunk in the cube that we are iterating through. Center is the center.
1054 ivec3 start_pos;
1055 ivec3 center_pos;
1056
1057 // If our camera isn't found within a chunk we have generated, we start at the bottom left voxel in the cube and center based on our camera position.
1058 if (cam_chunk == -1 /* || closest_dist > CHUNK_WORLD_SIZE * CHUNK_RADIUS */)
1059 {
1060
1061 start_pos = cam_pos;
1062 center_pos = cam_center_chunk;
1064
1065 }
1066 else
1067 {
1068
1069 start_pos = ivec3_add(
1070 CHUNK_DATA[cam_chunk_lod][cam_chunk].center,
1072 );
1073 center_pos = CHUNK_DATA[cam_chunk_lod][cam_chunk].center;
1074
1075 }
1076
1077
1078 double start_time = end_timer(&start);
1079
1080 start_timer(&big_loop);
1081 // The current chunk position
1082 ivec3 chunk_pos = start_pos;
1083
1084 int job_count = 0;
1085
1086 // Iterate through the cube volume and push potential chunks that need to be updated in LOD or brand new chunks.
1087 for (int z = 0; z < CHUNK_RADIUS * 2 + 1; z++)
1088 {
1089
1090 for (int y = 0; y < CHUNK_RADIUS * 2 + 1; y++)
1091 {
1092
1093 for (int x = 0; x < CHUNK_RADIUS * 2 + 1; x++)
1094 {
1095
1096 ivec3 min = ivec3_add(chunk_pos, MIN_OFFSET);
1097 ivec3 max = ivec3_add(chunk_pos, MAX_OFFSET);
1098
1099 // There was an idea to not push chunks based on direction but not sure if it works or not.
1100 // ivec3 dir = ivec3_sub(cam_pos, chunk_pos);
1101 // vec3 ndir = ivec3_normalize(dir);
1102 // float dot = ndir.x*cam.forward.x + ndir.y*cam.forward.y + ndir.z*cam.forward.z;
1103
1104 // if (dot > 0)
1105 // {
1106 // chunk_pos = ivec3_add(start_pos, (ivec3){x * CHUNK_WORLD_SIZE, y * CHUNK_WORLD_SIZE, z * CHUNK_WORLD_SIZE});
1107 // continue;
1108 // }
1109
1110 // Don't push jobs outside of frustum.
1111 // bool within_frustum = aabb_in_frustum(min, max, planes) || aabb_in_frustum(min, max, light_planes);
1112 bool within_frustum = aabb_in_frustum(min, max, planes);
1113 if (!within_frustum)
1114 {
1115
1116 chunk_pos = ivec3_add(start_pos, (ivec3){x * CHUNK_WORLD_SIZE, y * CHUNK_WORLD_SIZE, z * CHUNK_WORLD_SIZE});
1117 continue;
1118 }
1119
1120 // Determine LOD level based on distance
1121 int lod_level = 0;
1122 float dist_to_center = ivec3_dist(chunk_pos, cam_pos);
1123
1124 if (dist_to_center < CHUNK_WORLD_SIZE * 2 - 1e-4) { lod_level = 0; }
1125 else if (dist_to_center < CHUNK_WORLD_SIZE * 4 - 1e-4) { lod_level = 1; }
1126 else if (dist_to_center < CHUNK_WORLD_SIZE * 6 - 1e-4) { lod_level = 2; }
1127 else { lod_level = 3; }
1128
1129 ZeroEntry * ze = NULL;
1130 ZeroKey key = {chunk_pos.x, chunk_pos.y, chunk_pos.z, lod_level};
1131 HASH_FIND(hh, zero_hash_table, &key, sizeof(ZeroKey), ze);
1132 if (ze)
1133 {
1134
1135 chunk_pos = ivec3_add(
1136 start_pos,
1138 );
1139 continue;
1140
1141 }
1142
1143 bool already_done = false;
1144 int updating_lod_level = -1;
1145 int updating_lod_index = -1;
1146
1147 // Get current highest LOD cached for a chunk.
1148 int clod = get_current_lod(chunk_pos);
1149 ChunkHash * h = NULL;
1150 h = find_chunk_hash(chunk_pos, clod);
1151
1152 // Process which LOD level we will propose.
1153 if (h)
1154 {
1155
1156 if (clod == lod_level) { already_done = true; }
1157 else
1158 {
1159
1160 updating_lod_level = clod;
1161 updating_lod_index = h->id;
1162 lod_level = MAX(MIN(lod_level, clod + 1), clod - 1);
1163 if (lod_level == clod)
1164 {
1165 already_done = true;
1166 }
1167 }
1168
1169 }
1170
1171
1172 // If chunk is already finished or we were going to process it at the same LOD, skip.
1173 if (already_done)
1174 {
1175 chunk_pos = ivec3_add(start_pos, (ivec3){x * CHUNK_WORLD_SIZE, y * CHUNK_WORLD_SIZE, z * CHUNK_WORLD_SIZE});
1176 continue;
1177 }
1178
1179 // If we are updating, set replacement values.
1180 if (updating_lod_level != -1)
1181 {
1182 job_data[job_count].replacement_lod = updating_lod_level;
1183 job_data[job_count].replacement_index = updating_lod_index;
1184 replacement_mask[updating_lod_level][updating_lod_index] = true;
1185 }
1186 else
1187 {
1188 job_data[job_count].replacement_lod = -1;
1189 job_data[job_count].replacement_index = -1;
1190 }
1191
1192
1193 // Job data is different than jobs because it is a bigger struct and qsort is smaller.
1194 job_data[job_count].min = min;
1195 job_data[job_count].max = max;
1196 job_data[job_count].center = chunk_pos;
1197 job_data[job_count].lod = lod_level;
1198 job_data[job_count].free = false;
1199
1200 float weight = (1 - (dist_to_center / (float)(CHUNK_WORLD_SIZE * 20)) + 1) * (within_frustum * frustum_weight + 1);
1201
1202 jobs[job_count].score = weight;
1203 jobs[job_count].index = job_count;
1204 jobs[job_count].lod = lod_level;
1205 jobs[job_count].active = true;
1206
1207 job_count++;
1208
1209 chunk_pos = ivec3_add(start_pos, (ivec3){x * CHUNK_WORLD_SIZE, y * CHUNK_WORLD_SIZE, z * CHUNK_WORLD_SIZE});
1210
1211 }
1212 }
1213 }
1214
1215 // Score all current chunks based on importance and skip over children or empty chunks.
1216 float min_score = FLT_MAX;
1217 for (int l = 0; l < LODMAX; l++)
1218 {
1219
1220 for (int i = 0; i < MAX_CHUNKS_LOD[l]; i++)
1221 {
1222
1223 if (CHUNK_DATA[l][i].is_child && !CHUNK_DATA[l][i].free){
1224
1225 CHUNK_DATA[l][i].within_frustum = false;
1226 continue;
1227
1228 }
1229
1230 if (CHUNK_DATA[l][i].free)
1231 {
1232
1233 frees[free_count].index = i;
1234 frees[free_count].lod = l;
1235 frees[free_count].active = true;
1236 frees[free_count].score = -99;
1237 CHUNK_DATA[l][i].within_frustum = false;
1238 free_count++;
1239 min_score = -99;
1240 continue;
1241
1242 }
1243 else
1244 {
1245
1246 if (replacement_mask[l][i]) continue;
1247 frees[free_count].index = i;
1248 frees[free_count].lod = l;
1249 frees[free_count].active = true;
1250
1251 ivec3 min = CHUNK_DATA[l][i].min;
1252 ivec3 max = CHUNK_DATA[l][i].max;
1253
1254 bool within_frustum = aabb_in_frustum(min, max, planes);
1255 CHUNK_DATA[l][i].within_frustum = within_frustum;
1256
1257 float dist = ivec3_dist(CHUNK_DATA[l][i].center, cam_pos);
1258
1259 float weight = (1 - (dist / (float)(CHUNK_WORLD_SIZE * 20)) + 1) * (within_frustum * frustum_weight + 1);
1260
1261 frees[free_count].score = weight;
1262 if (weight < min_score) { min_score = weight; }
1263
1264 free_count++;
1265 }
1266 }
1267 }
1268
1269 // Sort both jobs and current free chunks.
1270 qsort(frees, free_count, sizeof(ChunkScore), compareFrees);
1271 qsort(jobs, job_count, sizeof(ChunkScore), compareJobs);
1272
1273 double big_loop_time = end_timer(&big_loop);
1274
1275 start_timer(&execute);
1276
1277
1278 // Execute jobs that have higher importance over least important free chunks.
1279 for (int i = 0; i < job_count; i++)
1280 {
1281
1282 int j = jobs[i].index;
1283 if (!jobs[i].active || jobs[i].score <= min_score) continue;
1284 int lod = job_data[j].lod;
1285
1286 ivec3 center = job_data[j].center;
1287
1288 if (job_data[j].replacement_index != -1)
1289 {
1290
1291 if (already_emitted[job_data[j].replacement_lod][job_data[j].replacement_index]) continue;
1292 if (lod > job_data[j].replacement_lod)
1293 {
1294 lod += 1;
1295 }
1296
1297 }
1298
1299 int f = -1;
1300 bool out_of_space = false;
1301 bool skip_free = false;
1302 if (lod >= LODMAX) skip_free = true;
1303 int cf = -1;
1304 int k0 = -1;
1305 int k1 = -1;
1306 bool valid_child = false;
1307 while (f == -1 && !skip_free)
1308 {
1309
1310 bool found_k = false;
1311 for (int k = 0; k < free_count; k++)
1312 {
1313
1314 if
1315 (
1316 frees[k].active &&
1317 frees[k].lod == lod &&
1318 frees[k].score < jobs[i].score
1319 )
1320 {
1321
1322 if (!already_emitted[frees[k].lod][frees[k].index])
1323 {
1324
1325 f = k;
1326 found_k = true;
1327 k0 = k;
1328
1329 int child = CHUNK_DATA[frees[k].lod][frees[k].index].id0;
1330
1331 if
1332 (
1333 child != -1 &&
1334 frees[k].lod + 1 < LODMAX &&
1335 !CHUNK_DATA[lod][frees[k].index].free &&
1336 !already_emitted[frees[k].lod+1][child]
1337 )
1338 { valid_child = true; }
1339
1340 break;
1341 }
1342 }
1343 }
1344
1345 if
1346 (
1347 job_data[j].replacement_index == -1 &&
1348 f != -1 &&
1349 lod + 1 < LODMAX && !valid_child
1350 )
1351 {
1352
1353 found_k = false;
1354 for (int k = 0; k < free_count; k++)
1355 {
1356
1357 if
1358 (
1359 frees[k].active &&
1360 frees[k].lod == lod + 1 &&
1361 frees[k].score == -99
1362 )
1363 {
1364
1365 if (!already_emitted[frees[k].lod][frees[k].index]){
1366
1367 found_k = true;
1368 cf = frees[k].index;
1369 frees[k].active = false;
1370 k1 = k;
1371 break;
1372
1373 }
1374 }
1375 }
1376 }
1377
1378 if (found_k)
1379 {
1380
1381 frees[f].active = false;
1382 break;
1383 } else { f = -1; }
1384
1385 if (f == -1){
1386
1387 // special case: replacement with no space
1388 if (job_data[j].replacement_index != -1)
1389 {
1390
1391 out_of_space = true;
1392 break;
1393
1394 }
1395 lod++;
1396 if (lod >= LODMAX)
1397 {
1398 out_of_space = true;
1399 break;
1400 }
1401 }
1402 else
1403 {
1404 break;
1405 }
1406
1407 }
1408
1409 if (out_of_space) continue;
1410 if
1411 (
1412 job_data[j].replacement_index == -1 &&
1413 cf == -1 &&
1414 lod < LODMAX - 1 &&
1415 !valid_child
1416 )
1417 continue;
1418
1419 int k = (f != -1) ? frees[f].index : -1;
1420
1421 if (valid_child) { cf = CHUNK_DATA[lod][k].id0; }
1422
1423 // Check the zero list to make sure we don't emit a duplicate empty chunk.
1424 bool match_found = false;
1425 if (job_data[j].replacement_index == -1)
1426 {
1427
1428 ZeroEntry * ze = NULL;
1429 ZeroKey key =
1430 {
1431 job_data[j].center.x,
1432 job_data[j].center.y,
1433 job_data[j].center.z,
1434 lod
1435 };
1436
1437 HASH_FIND(hh, zero_hash_table, &key, sizeof(ZeroKey), ze);
1438 if (ze)
1439 {
1440
1441 if (k0 != -1) frees[k0].active = true;
1442 if (k1 != -1) frees[k1].active = true;
1443 continue;
1444
1445 }
1446
1447 }
1448
1449 bool new_chunk = false;
1450 bool is_parent = false;
1451
1452
1453 // If we are replacing a chunk.
1454 if (job_data[j].replacement_index != -1)
1455 {
1456
1457 // If going down in LOD.
1458 if (lod < job_data[j].replacement_lod)
1459 {
1460
1461 assert(lod == job_data[j].replacement_lod - 1);
1462 assert(k != -1);
1464 free_child(lod, k);
1465 free_parent(lod, k);
1467 assert(!already_emitted[lod][k]);
1468 free_child(job_data[j].replacement_lod, job_data[j].replacement_index);
1469 assert(!already_emitted[job_data[j].replacement_lod][job_data[j].replacement_index]);
1470
1471 CHUNK_DATA[lod][k].uID = uID;
1473 CHUNK_DATA[lod][k].min = job_data[j].min;
1474 CHUNK_DATA[lod][k].max = job_data[j].max;
1475 CHUNK_DATA[lod][k].free = false;
1476 already_emitted[lod][k] = true;
1477
1479 CHUNK_DATA[lod][k].id1 = -1;
1480 CHUNK_DATA[lod][k].is_child = false;
1481 is_parent = true;
1483
1491
1492 replace_chunk_hash(job_data[j].center, job_data[j].replacement_lod, job_data[j].replacement_index);
1493 replace_chunk_hash(job_data[j].center, lod, k);
1494 set_current_lod(job_data[j].center, lod);
1495
1496 }
1497 // If going up in LOD.
1498 else
1499 {
1500
1503
1504 assert(lod != job_data[j].replacement_lod);
1505 assert(lod == job_data[j].replacement_lod + 2);
1506 assert(child != -1);
1507 free_current_parent(job_data[j].replacement_lod, job_data[j].replacement_index);
1508 assert(!already_emitted[job_data[j].replacement_lod+1][child]);
1509
1510 if (k != -1)
1511 {
1512
1513 free_child(lod, k);
1514 free_parent(lod, k);
1516 assert(!already_emitted[lod][k]);
1517
1518 }
1519
1520
1521 assert(k != -1 || lod >= LODMAX);
1522
1523 already_emitted[job_data[j].replacement_lod+1][child] = true;
1524 CHUNK_DATA[job_data[j].replacement_lod+1][child].id0 = -1;
1525 CHUNK_DATA[job_data[j].replacement_lod+1][child].id1 = -1;
1526 CHUNK_DATA[job_data[j].replacement_lod+1][child].uID = uID;
1530 CHUNK_DATA[job_data[j].replacement_lod+1][child].is_child = false;
1531 CHUNK_DATA[job_data[j].replacement_lod+1][child].free = false;
1532
1533 if (k != -1)
1534 {
1535
1536 assert(lod != job_data[j].replacement_lod);
1537 CHUNK_DATA[lod][k].uID = uID;
1538 CHUNK_DATA[lod][k].id0 = -1;
1539 CHUNK_DATA[lod][k].id1 = child;
1540 CHUNK_DATA[lod][k].is_child = true;
1542 CHUNK_DATA[lod][k].min = job_data[j].min;
1543 CHUNK_DATA[lod][k].max = job_data[j].max;
1544 CHUNK_DATA[lod][k].free = false;
1545 already_emitted[lod][k] = true;
1546 CHUNK_DATA[job_data[j].replacement_lod+1][child].id0 = k;
1547 replace_chunk_hash(job_data[j].center, lod, k);
1548
1549 }
1550
1551 replace_chunk_hash(job_data[j].center, job_data[j].replacement_lod + 1, child);
1552 set_current_lod(job_data[j].center, job_data[j].replacement_lod + 1);
1553
1554 }
1555
1556 }
1557 // Brand new chunk.
1558 else
1559 {
1560
1561 uint32_t uid = assign_new_uID();
1562
1563 assert(k != -1);
1564 free_child(lod, k);
1565 free_parent(lod, k);
1567 assert(!already_emitted[lod][k]);
1568
1569 if (lod + 1 < LODMAX)
1570 {
1571
1572 free_child(lod+1, cf);
1573 free_parent(lod+1, cf);
1574 free_current_parent(lod+1, cf);
1575 assert(!already_emitted[lod+1][cf]);
1576
1577 }
1578
1579 int current_chunk = uid;
1580 CHUNK_DATA[lod][k].uID = current_chunk;
1581 CHUNK_DATA[lod][k].is_child = false;
1582 CHUNK_DATA[lod][k].id1 = -1;
1583 CHUNK_DATA[lod][k].id0 = -1;
1585 CHUNK_DATA[lod][k].min = job_data[j].min;
1586 CHUNK_DATA[lod][k].max = job_data[j].max;
1587 CHUNK_DATA[lod][k].free = false;
1588 CHUNK_DATA[lod][k].uID = uid;
1589
1590 already_emitted[lod][k] = true;
1591
1592 if (lod + 1 < LODMAX) {
1593
1594 assert(cf >= 0);
1595 CHUNK_DATA[lod][k].id0 = cf;
1596 int nc = CHUNK_DATA[lod + 1][cf].id0;
1597 int pc = CHUNK_DATA[lod + 1][cf].id1;
1598 CHUNK_DATA[lod + 1][cf].uID = current_chunk;
1599 CHUNK_DATA[lod + 1][cf].center = job_data[j].center;
1600 CHUNK_DATA[lod + 1][cf].min = job_data[j].min;
1601 CHUNK_DATA[lod + 1][cf].max = job_data[j].max;
1602 CHUNK_DATA[lod + 1][cf].is_child = true;
1603
1604 CHUNK_DATA[lod + 1][cf].id0 = -1;
1605 CHUNK_DATA[lod + 1][cf].id1 = k;
1606 CHUNK_DATA[lod + 1][cf].uID = uid;
1607 CHUNK_DATA[lod + 1][cf].free = false;
1608 already_emitted[lod + 1][cf] = true;
1609
1610 // Since it's a brand new chunk, make a child mesh at higher LOD.
1611 create_new_planet_mesh(lod+1, job_data[j].center, cf, false);
1612 replace_chunk_hash(job_data[j].center, lod+1, cf);
1613
1614 }
1615
1616 replace_chunk_hash(job_data[j].center, lod, k);
1617 set_current_lod(job_data[j].center, lod);
1618 is_parent = true;
1619 chunk_uID++;
1620
1621 }
1622
1623 // Create new mesh for given chunk.
1624 if (k != -1)
1625 {
1626
1628
1629 if (is_parent)
1630 {
1634 }
1635 }
1636 }
1637
1638 double execute_time = end_timer(&execute);
1639 zero_fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
1640
1641}
1642
1643
1651void
1653 (
1654 FACE face,
1655 int * vcnt,
1656 int * icnt
1657 )
1658
1659{
1660
1661 int c = *vcnt;
1662 int o = ATMOSPHERE_VCNT;
1663 int ic = *icnt;
1664
1665 Timer check;
1666 start_timer(&check);
1667 float scale = 1.0;
1668 // float size = pow(particle.p_m.w, 1.0/3.0) * scale;
1669
1670 // float size = 600;
1671 float radius = (float)(CHUNK_WORLD_SIZE * (CHUNK_RADIUS * 2 + 1) * 2) / 2;
1672 // float size = radius + radius * 0.2f;
1673 float size = 1;
1674
1675 for (float y = 0; y < ATMOSPHERE_RES; y+=1.0f)
1676 {
1677
1678 for (float x = 0; x < ATMOSPHERE_RES; x+=1.0f)
1679 {
1680
1681 vec2 uv = {x / (ATMOSPHERE_RES-1), y /(ATMOSPHERE_RES-1)};
1682 uv.x = uv.x * 2.0f - 1.0f;
1683 uv.y = uv.y * 2.0f - 1.0f;
1684 vec3 p = cube_pos(face, uv);
1685
1686 p = vec3_normalize(p);
1687
1688
1689 p = vec3_scale(p, size);
1690
1692
1693 }
1694 }
1695
1696 for (int y = 0; y < ATMOSPHERE_RES-1; y+=1)
1697 {
1698
1699 for (int x = 0; x < ATMOSPHERE_RES-1; x+=1)
1700 {
1701
1702 uint32_t a0 = ATMOSPHERE_IDX(x, y, o);
1703 uint32_t b0 = ATMOSPHERE_IDX(x+1, y, o);
1704 uint32_t c0 = ATMOSPHERE_IDX(x, y+1, o);
1708
1710
1711 ATMOSPHERE_VERTS[a0].normal = vec3_add(ATMOSPHERE_VERTS[a0].normal, n0);
1712 ATMOSPHERE_VERTS[b0].normal = vec3_add(ATMOSPHERE_VERTS[b0].normal, n0);
1714
1715 uint32_t a1 = ATMOSPHERE_IDX(x, y+1, o);
1716 uint32_t b1 = ATMOSPHERE_IDX(x+1, y, o);
1717 uint32_t c1 = ATMOSPHERE_IDX(x+1, y+1, o);
1721
1723
1724 ATMOSPHERE_VERTS[a1].normal = vec3_add(ATMOSPHERE_VERTS[a1].normal, n1);
1725 ATMOSPHERE_VERTS[b1].normal = vec3_add(ATMOSPHERE_VERTS[b1].normal, n1);
1727
1728 }
1729 }
1730
1731 double end = end_timer(&check);
1732
1733
1734}
1735
1740void
1742
1743{
1744
1745 for (int face = 0; face < 6; face++) { make_atmosphere_sphere_face(face, &ATMOSPHERE_VCNT, &ATMOSPHERE_ICNT); }
1746
1747 for (int i = 0; i < ATMOSPHERE_VCNT; i++) { ATMOSPHERE_VERTS[i].normal = vec3_normalize(ATMOSPHERE_VERTS[i].normal); }
1748
1749}
1750
1751
1752
in vec3 c0
in vec3 c1
float scale(float fCos)
const float radius
Definition camera.c:580
void update_camera(Camera *cam)
Definition camera.c:223
Camera cam
Definition main.c:222
#define LODMAX
float mod289(float x)
Definition density.comp:112
uniform int chunk_id
Definition density.comp:159
vec4 perm(vec4 x)
Definition density.comp:114
uniform int lod
Definition density.comp:154
float fbm(vec3 x)
Definition density.comp:139
#define ATMOSPHERE_IDX(x, y, o)
Definition mesh.c:31
void make_atmosphere_sphere_face(FACE face, int *vcnt, int *icnt)
Creats a sphere face to be used for atmosphere rendering.
Definition mesh.c:1653
void mesh_atmosphere_sphere()
Creates an atmosphere sphere for rendering.
Definition mesh.c:1741
int ATMOSPHERE_VCNT
Definition main.c:188
AtmosphereVertex * ATMOSPHERE_VERTS
Definition main.c:186
uint32_t * ATMOSPHERE_INDICES
Definition main.c:187
int ATMOSPHERE_ICNT
Definition main.c:189
void init_chunk_data()
Initializes and frees old ChunkData and hash tables.
Definition mesh.c:796
bool free_parent(int lod, int i)
Frees the parent of given LOD.
Definition mesh.c:691
void load_chunks(vec3 planet_origin, float planet_radius)
Checks all chunks based on new Camera position, building a job list and then executing them based on ...
Definition mesh.c:894
float calc_density(vec3 p)
Copies the exact density function from the GPU to the CPU.
Definition mesh.c:324
bool free_current_parent(int lod, int i)
Frees the given LOD (as a parent).
Definition mesh.c:662
bool free_child(int lod, int i)
Frees the child of given LOD.
Definition mesh.c:725
int find_current_lod(ivec3 center, ChunkHash **h)
Finds the highest LOD that we have cached from the current chunk.
Definition mesh.c:631
void create_new_planet_mesh(int lod, ivec3 chunk_center, int chunk_id, bool is_parent)
Creates a mesh for a certain chunk.
Definition mesh.c:434
float sample_fbm_density(vec3 p)
Samples a precomputed fbm volume.
Definition mesh.c:270
uniform bool is_parent
Definition indirect.comp:55
float * perlin_tex
Definition init.c:610
float * fbm_tex
Definition init.c:346
GLuint PERLIN_TEXTURE
Definition main.c:92
int CHUNK_DATA_FLAG
Definition main.c:69
ChunkData * job_data
Definition mesh.c:783
vec3 planet_origin
Definition main.c:260
GLuint DENSITY_SSBO[LODMAX]
Definition main.c:209
ChunkData CHUNK_DATA[LODMAX][MAX_CHUNKS]
Definition main.c:67
GLuint CHUNK_ZERO_COMPUTE_SHADER
Definition main.c:141
ComputeShaderProgram DENSITY_COMPUTE_SHADER
Definition main.c:138
GLuint DENSITY_VERTEX_SSBO[LODMAX]
Definition main.c:210
ChunkScore * jobs
Definition mesh.c:782
ChunkScore * frees
Definition mesh.c:784
GLuint FBM_TEXTURE
Definition main.c:93
GLuint CHUNK_DATA_SSBO[3]
Definition main.c:215
GLuint INDIRECT_BUFFER[LODMAX][MAX_CHUNKS]
Definition main.c:176
PlanetData PLANET_DATA
Definition main.c:194
ComputeShaderProgram INDIRECT_COMPUTE_SHADER
Definition main.c:140
ComputeShaderProgram MARCHING_CUBES_COMPUTE_SHADER
Definition main.c:139
float planet_radius
Definition main.c:262
Camera * current_cam
Definition main.c:225
Camera LIGHT_CAM
Definition main.c:224
float sample_density(vec3 p, vec3 chunk_offset, vec3 center_offset, float voxel_size, uint padded)
float vec3_dot(vec3 a, vec3 b)
Definition mathf.h:101
vec3 ivec3_scale(ivec3 a, float b)
Definition mathf.h:69
vec4 vec4_add(vec4 a, vec4 b)
Definition mathf.h:77
#define MIN(a, b)
Definition mathf.h:193
vec3 vec3_normalize(vec3 v)
Definition mathf.h:7
mat4 m4_mul(mat4 a, mat4 b)
Definition mathf.h:159
float ivec3_dist(ivec3 a, ivec3 b)
Definition mathf.h:48
vec3 vec3_scale(vec3 a, float b)
Definition mathf.h:39
vec3 vec3_mul(vec3 a, vec3 b)
Definition mathf.h:85
vec4 vec4_mul(vec4 a, vec4 b)
Definition mathf.h:89
vec2 vec2_add(vec2 a, vec2 b)
Definition mathf.h:61
vec2 vec2_scale(vec2 a, float b)
Definition mathf.h:35
vec4 vec4_sub(vec4 a, vec4 b)
Definition mathf.h:81
vec3 vec3_sub(vec3 a, vec3 b)
Definition mathf.h:57
vec3 vec3_add(vec3 a, vec3 b)
Definition mathf.h:73
float lerp(float a, float b, float t)
Definition mathf.h:143
vec4 vec4_scale(vec4 a, float b)
Definition mathf.h:43
ivec3 ivec3_add(ivec3 a, ivec3 b)
Definition mathf.h:65
#define MAX(a, b)
Definition mathf.h:191
bool s
Definition menu.c:6
UT_hash_handle hh
Definition mesh.c:774
bool aabb_in_frustum(ivec3 min, ivec3 max, vec4 planes[6])
Definition mesh.c:493
float fbm_noise(vec3 p)
Definition mesh.c:135
AuthorityEntry * authority_map
Definition mesh.c:617
uint32_t assign_new_uID()
Definition mesh.c:757
int compareFrees(const void *a, const void *b)
Definition mesh.c:588
int emitted_chunk_ids[VERTEX_COUNT_MAX]
Definition mesh.c:880
void get_view_planes(vec4 planes[6], Camera *cam, vec3 planet_offset, float planet_radius)
Returns view planes from a camera, used for frustum culling.
Definition mesh.c:528
int zero_list_count
Definition mesh.c:779
GLsync zero_fence
Definition mesh.c:884
bool already_emitted[LODMAX][MAX_CHUNKS]
Definition mesh.c:623
int x
Definition mesh.c:767
FACE
Definition mesh.c:17
@ ZPOS
Definition mesh.c:22
@ ZNEG
Definition mesh.c:23
@ XNEG
Definition mesh.c:19
@ YPOS
Definition mesh.c:20
@ XPOS
Definition mesh.c:18
@ YNEG
Definition mesh.c:21
int y
Definition mesh.c:767
int emitted_chunks
Definition mesh.c:879
vec3 cube_pos(FACE face, vec2 uv)
Definition mesh.c:37
int generation_id
Definition mesh.c:877
vec3 calc_normal(vec3 a, vec3 b, vec3 c)
Definition mesh.c:59
int lod
Definition mesh.c:767
vec3 vec3_fract(vec3 x)
Definition mesh.c:102
int compareJobs(const void *a, const void *b)
Definition mesh.c:570
float wrap(float x, float size)
Definition mesh.c:191
int z
Definition mesh.c:767
#define MAX_ZERO_LIST
Definition mesh.c:763
ZeroKey key
Definition mesh.c:773
ChunkHash * hash_table
Definition mesh.c:618
vec3 vec3_wrap(vec3 a, float size)
Definition mesh.c:202
bool replacement_mask[LODMAX][MAX_CHUNKS]
Definition mesh.c:786
uint32_t chunk_uID
Definition mesh.c:754
ZeroEntry * zero_hash_table
Definition mesh.c:778
vec4 vec4_fract(vec4 x)
Definition mesh.c:117
int emitted_chunk_lods[VERTEX_COUNT_MAX]
Definition mesh.c:881
Definition mesh.c:772
uniform int lod_level
Definition mesh.frag:68
ChunkHash * find_chunk_hash(ivec3 center, int lod)
Definition mesh.h:79
void delete_chunk_hash(ivec3 center, int lod)
Definition mesh.h:126
int get_current_lod(ivec3 center)
Definition mesh.h:51
void set_current_lod(ivec3 center, int lod)
Definition mesh.h:62
void replace_chunk_hash(ivec3 center, int lod, int id)
Definition mesh.h:101
int id
Definition mesh.h:32
Definition mesh.h:13
const vec2 res
uniform isampler2D uID
const int size
Definition sky.frag:9
float x
Definition types.h:45
bool active
Definition types.h:248
float a1
Definition types.h:51
float w
Definition types.h:47
PerlinLayer perlin_layers[MAX_LAYERS]
Definition types.h:418
float z
Definition types.h:47
const int LOD_RES[LODMAX]
Definition types.h:299
Uniform uniforms[32]
Definition types.h:444
float d3
Definition types.h:51
float c2
Definition types.h:51
float score
Definition types.h:247
#define VERTEX_COUNT_MAX
Definition types.h:483
float b0
Definition types.h:51
WarpLayer warp_layers[MAX_LAYERS]
Definition types.h:417
float c1
Definition types.h:51
float x
Definition types.h:47
int replacement_lod
Definition types.h:331
float a2
Definition types.h:51
#define MAX_CHUNK_LIST
Definition types.h:184
#define FBM_TEXTURE_SIZE
Definition types.h:510
ivec3 min
Definition types.h:327
double end_timer(Timer *t)
Definition types.h:63
int lod
Definition types.h:324
float x
Definition main.h:5
ivec3 center
Definition types.h:329
int id0
Definition types.h:322
uint32_t uID
Definition types.h:326
int y
Definition types.h:48
#define CHUNK_WORLD_SIZE
Definition types.h:318
ivec3 max
Definition types.h:328
int fbm_count
Definition types.h:413
float b3
Definition types.h:51
float y
Definition types.h:45
float y
Definition types.h:47
int warp_count
Definition types.h:411
int perlin_count
Definition types.h:412
int index
Definition types.h:245
float y
Definition main.h:5
int id1
Definition types.h:323
void start_timer(Timer *t)
Definition types.h:58
const int MAX_CHUNKS_LOD[LODMAX]
Definition types.h:309
float d1
Definition types.h:51
float a3
Definition types.h:51
int z
Definition types.h:48
#define MAX_CHUNKS
Definition types.h:182
float c0
Definition types.h:51
float z
Definition main.h:5
mat4 view
Definition types.h:225
float pitch
Definition types.h:234
#define PERLIN_TEXTURE_SIZE
Definition types.h:508
int x
Definition types.h:48
float b2
Definition types.h:51
float d0
Definition types.h:51
bool free
Definition types.h:333
bool is_child
Definition types.h:332
vec4 cam_vel
Definition types.h:224
bool within_frustum
Definition types.h:325
FBMLayer fbm_layers[MAX_LAYERS]
Definition types.h:419
float yaw
Definition types.h:234
GLuint location
Definition types.h:435
mat4 proj
Definition types.h:225
#define CHUNK_RADIUS
Definition types.h:505
#define ATMOSPHERE_RES
Definition types.h:503
CamData cam_data
Definition types.h:235
float a0
Definition types.h:51
float c3
Definition types.h:51
vec4 cam_pos
Definition types.h:224
float b1
Definition types.h:51
float d2
Definition types.h:51
int replacement_index
Definition types.h:330
int lod
Definition types.h:246
Definition types.h:53
Definition types.h:48
Definition types.h:51
Definition types.h:45
Definition main.h:5
Definition types.h:47
uniform float t
Definition water.comp:23
vec3 offset2
Definition types.h:378
float amp
Definition types.h:401
float freq0
Definition types.h:375
vec3 offset1
Definition types.h:376
float freq
Definition types.h:400
float amp
Definition types.h:391
float freq1
Definition types.h:377
float freq2
Definition types.h:379
vec3 offset0
Definition types.h:374
float amp
Definition types.h:380
float freq
Definition types.h:390