Space Sim
Loading...
Searching...
No Matches
mesh.h
Go to the documentation of this file.
1#ifndef MESH_H
2#define MESH_H
3
4#include "core/types.h"
5#include "uthash/uthash.h"
6#include <stdio.h>
7
8
9typedef struct {
10 int x, y, z;
11} CenterKey;
12
13typedef struct {
15 int lod;
16 UT_hash_handle hh;
18
19
20
21typedef struct ChunkKey
22{
23
24 int x, y, z, lod;
25
26} ChunkKey;
27
28typedef struct ChunkHash
29{
30
32 int id;
33 UT_hash_handle hh;
34
35} ChunkHash;
37
38void init_chunk_data();
39bool free_parent(int lod, int i);
40bool free_child(int lod, int i);
41bool free_current_parent(int lod, int i);
42
43float calc_density(vec3 p);
45void create_new_planet_mesh(int lod, ivec3 chunk_center, int chunk_id, bool is_parent);
46float fbm(vec3 p);
47
49extern ChunkHash * hash_table;
50
51static inline int get_current_lod(ivec3 center)
52{
54 CenterKey k = {center.x, center.y, center.z};
55
56 HASH_FIND(hh, authority_map, &k, sizeof(k), e);
57 if (!e) return -1;
58
59 return e->lod;
60}
61
62static inline void set_current_lod(ivec3 center, int lod)
63{
64 AuthorityEntry* e = NULL;
65 CenterKey k = {center.x, center.y, center.z};
66
67 HASH_FIND(hh, authority_map, &k, sizeof(k), e);
68
69 if (!e) {
70 e = (AuthorityEntry*)malloc(sizeof(*e));
71 e->key = k;
72 HASH_ADD(hh, authority_map, key, sizeof(k), e);
73 }
74
75 e->lod = lod;
76}
77
78// find by center, return even if lod does not match
79static inline ChunkHash * find_chunk_hash(ivec3 center, int lod)
80{
81 ChunkHash * f = NULL;
82
83 ChunkKey k = {(center.x), (center.y), (center.z), lod};
84 HASH_FIND(hh, hash_table, &k, sizeof(ChunkKey), f);
85
86 return f;
87}
88static inline void add_chunk_hash(ivec3 center, int lod, int id)
89{
90
91 ChunkKey k = {(center.x), (center.y), (center.z), lod};
92 ChunkHash *a = (ChunkHash*)malloc(sizeof(ChunkHash));
93 memset(a, 0, sizeof(ChunkHash));
94 a->key = k;
95 a->id = id;
96 // a->hh = hh;
97 HASH_ADD(hh, hash_table, key, sizeof(ChunkKey), a);
98
99}
100
101static inline void replace_chunk_hash(ivec3 center, int lod, int id)
102{
103
104 ChunkKey k = {(center.x), (center.y), (center.z), lod};
105 // a->hh = hh;
106 ChunkHash * r = NULL;
107 // HASH_REPLACE(hh, hash_table, key, sizeof(ChunkKey), a, r);
108 HASH_FIND(hh, hash_table, &k, sizeof(k), r);
109
110 if (!r) {
111 ChunkHash *a = (ChunkHash*)malloc(sizeof(ChunkHash));
112 memset(a, 0, sizeof(ChunkHash));
113 a->key = k;
114 a->id = id;
115 HASH_ADD(hh, hash_table, key, sizeof(ChunkKey), a);
116 }
117 else
118 {
119 r->id = id;
120 r->key = k;
121 }
122
123
124}
125
126static inline void delete_chunk_hash(ivec3 center, int lod)
127{
128
129 ChunkHash * p = NULL;
130 ChunkKey k = {(center.x), (center.y), (center.z), lod};
131 HASH_FIND(hh, hash_table, &k, sizeof(ChunkKey), p);
132 if (p)
133 {
134 HASH_DEL(hash_table, p);
135 free(p);
136 }
137 else
138 {
139 // printf("NO DELETION HASH FOUND %d %d %d\n", center.x, center.y, center.z);
140 // exit(0);
141 }
142}
143#endif
uniform int chunk_id
Definition density.comp:159
uniform int lod
Definition density.comp:154
float fbm(vec3 x)
Definition density.comp:139
void mesh_atmosphere_sphere()
Creates an atmosphere sphere for rendering.
Definition mesh.c:1741
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
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
uniform bool is_parent
Definition indirect.comp:55
vec3 planet_origin
Definition main.c:260
float planet_radius
Definition main.c:262
AuthorityEntry * authority_map
Definition mesh.c:617
ChunkHash * hash_table
Definition mesh.c:618
CenterKey key
Definition mesh.h:14
UT_hash_handle hh
Definition mesh.h:16
ChunkHash * find_chunk_hash(ivec3 center, int lod)
Definition mesh.h:79
int lod
Definition mesh.h:24
ChunkKey key
Definition mesh.h:31
int x
Definition mesh.h:24
void delete_chunk_hash(ivec3 center, int lod)
Definition mesh.h:126
int z
Definition mesh.h:24
int x
Definition mesh.h:10
int get_current_lod(ivec3 center)
Definition mesh.h:51
int y
Definition mesh.h:24
UT_hash_handle hh
Definition mesh.h:33
int z
Definition mesh.h:10
void add_chunk_hash(ivec3 center, int lod, int id)
Definition mesh.h:88
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
int y
Definition mesh.h:10
Definition mesh.h:13
int y
Definition types.h:48
int z
Definition types.h:48
int x
Definition types.h:48
Definition types.h:48
Definition main.h:5