Space Sim
Toggle main menu visibility
Loading...
Searching...
No Matches
src
mesh
density.comp
Go to the documentation of this file.
1
#version 460
2
8
9
layout
(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
10
// layout (local_size_y = 64) in;
11
// layout (local_size_z = 64) in;
12
layout
(std140) uniform CameraData {
13
vec4
cam_pos;
14
vec4
cam_vel;
15
mat4
projection
;
16
mat4
view
;
17
mat4
inv_projection;
18
mat4
inv_view;
19
};
20
layout
(std430, binding=8) buffer
MarchingCubesData
21
{
22
int
EDGE_TO_VERTICES
[12][2];
23
int
EDGE_TABLE
[256];
24
int
TRIANGLE_TABLE
[256][16];
25
};
26
layout
(std430, binding=9) buffer
DensityData
27
{
28
float
density[];
29
};
30
struct
Vertex
31
{
32
vec3
pos
;
33
float
pad0
;
34
vec3
normal
;
35
float
pad1
;
36
vec3
color
;
37
float
pad2
;
38
};
39
#define LODMAX 4
40
#define MAX_CHUNKS 400
41
42
struct
VertexInfo
43
{
44
int
vcnt
;
45
uint
uid
;
46
uint
pad0
;
47
uint
pad1
;
48
};
49
layout
(std430, binding=12) buffer
ChunkData
{
50
float
lod_dist[3];
51
uint
emitted_chunks
;
52
VertexInfo
vertex_counts[];
53
};
54
#define MAX_LAYERS 24
55
56
struct
WarpLayer
57
{
58
vec3
offset0
;
59
float
freq0
;
60
vec3
offset1
;
61
float
freq1
;
62
vec3
offset2
;
63
float
freq2
;
64
float
amp
;
65
float
pad0
;
66
float
pad1
;
67
float
pad3
;
68
};
69
70
struct
PerlinLayer
71
{
72
float
freq
;
73
float
amp
;
74
float
pad0
;
75
float
pad1
;
76
};
77
78
struct
FBMLayer
79
{
80
float
freq
;
81
float
amp
;
82
float
pad0
;
83
float
pad1
;
84
};
85
86
layout
(std430, binding=13) buffer
PlanetData
{
87
88
int
warp_count;
89
int
perlin_count;
90
int
fbm_count;
91
int
pad0;
92
vec4
sun_dir
;
93
vec4
sun_color
;
94
WarpLayer
warp_layers[
MAX_LAYERS
];
95
PerlinLayer
perlin_layers[
MAX_LAYERS
];
96
FBMLayer
fbm_layers[
MAX_LAYERS
];
97
98
};
99
layout
(std430, binding=10) buffer DensityVertexData
100
{
101
int
chunk_vertex_offset[
MAX_CHUNKS
];
102
Vertex
vertices[];
103
};
104
layout
(std430, binding=6) buffer
NodeCounter
{
105
uint queue_out;
106
int
p_dead_cnt;
107
int
p_spawn_cnt;
108
uint star_pcnt;
109
};
110
111
#define NUM_OCTAVES 5
112
float
mod289
(
float
x){
return
x - floor(x * (1.0 / 289.0)) * 289.0;}
113
vec4
mod289
(
vec4
x){
return
x - floor(x * (1.0 / 289.0)) * 289.0;}
114
vec4
perm
(
vec4
x){
return
mod289
(((x * 34.0) + 1.0) * x);}
115
116
float
noise
(
vec3
p){
117
vec3
a = floor(p);
118
vec3
d = p - a;
119
d = d * d * (3.0 - 2.0 * d);
120
121
vec4
b = a.xxyy +
vec4
(0.0, 1.0, 0.0, 1.0);
122
vec4
k1 =
perm
(b.xyxy);
123
vec4
k2 =
perm
(k1.xyxy + b.zzww);
124
125
vec4
c = k2 + a.zzzz;
126
vec4
k3 =
perm
(c);
127
vec4
k4 =
perm
(c + 1.0);
128
129
vec4
o1 = fract(k3 * (1.0 / 41.0));
130
vec4
o2 = fract(k4 * (1.0 / 41.0));
131
132
vec4
o3 = o2 * d.
z
+ o1 * (1.0 - d.
z
);
133
vec2
o4 = o3.yw * d.
x
+ o3.xz * (1.0 - d.
x
);
134
135
return
o4.
y
* d.
y
+ o4.x * (1.0 - d.
y
);
136
}
137
138
139
float
fbm
(
vec3
x) {
140
float
v = 0.0;
141
float
a = 0.5;
142
vec3
shift =
vec3
(100);
143
for
(
int
i = 0; i <
NUM_OCTAVES
; ++i) {
144
v += a *
noise
(x);
145
x = x * 2.0 + shift;
146
a *= 0.45;
147
}
148
return
v;
149
}
150
151
const
float
radius
= 1000.0;
152
153
uniform
int
uSize
;
154
uniform
int
lod
;
155
uniform
vec3
chunk_offset
;
156
uniform sampler3D
perlin_tex
;
157
uniform sampler3D
fbm_tex
;
158
159
uniform
int
chunk_id
;
161
void
main
()
162
{
163
164
uint x = gl_GlobalInvocationID.x;
165
uint y = gl_GlobalInvocationID.y;
166
uint z = gl_GlobalInvocationID.z;
167
if
(x >=
uSize
+1 || y >=
uSize
+1 || z >=
uSize
+1)
return
;
168
if
(x == 0 && y == 0 && z == 0)
169
{
170
chunk_vertex_offset[
chunk_id
] = 0;
171
}
172
float
chunk_world_size = 20;
173
174
float
voxel_size = chunk_world_size /
uSize
;
175
176
vec3
center_offset =
vec3
(-0.5 * chunk_world_size);
177
vec3
p = center_offset +
vec3
(x,y,z) * voxel_size +
chunk_offset
;
178
float
d = (p.
z
);
179
vec3
nn = p;
180
for
(
int
i = 0; i < warp_count; i++)
181
{
182
183
WarpLayer
wl = warp_layers[i];
184
vec3
warp =
185
vec3
(texture(
perlin_tex
, p * wl.
freq0
+ wl.
offset0
).r * 2.0f - 1.0f,
186
texture(
perlin_tex
, p * wl.
freq1
+ wl.
offset1
).r * 2.0f - 1.0f,
187
texture(
perlin_tex
, p * wl.
freq2
+ wl.
offset2
).r * 2.0f - 1.0f);
188
p += warp * wl.
amp
;
189
}
190
191
for
(
int
i = 0; i < perlin_count; i++)
192
{
193
PerlinLayer
pl = perlin_layers[i];
194
d += (float(texture(
perlin_tex
, p * pl.
freq
).r) * 2.0f - 1.0f) * pl.
amp
;
195
}
196
for
(
int
i = 0; i < fbm_count; i++)
197
{
198
FBMLayer
fl = fbm_layers[i];
199
d += (float(texture(
fbm_tex
, p * fl.
freq
).r) * 2.0f - 1.0f) * fl.
amp
;
200
}
201
density[z * (
uSize
+ 1) * (
uSize
+ 1) + y * (
uSize
+1) + x +
202
(
uSize
+1) * (
uSize
+1) * (
uSize
+1) *
chunk_id
] = d;
203
204
}
layout
layout(local_size_x=16, local_size_y=16, local_size_z=1) in
radius
const float radius
Definition
camera.c:580
NUM_OCTAVES
#define NUM_OCTAVES
Definition
density.comp:111
mod289
float mod289(float x)
Definition
density.comp:112
chunk_id
uniform int chunk_id
Definition
density.comp:159
perm
vec4 perm(vec4 x)
Definition
density.comp:114
lod
uniform int lod
Definition
density.comp:154
uSize
uniform int uSize
Definition
density.comp:153
chunk_offset
uniform vec3 chunk_offset
Definition
density.comp:155
fbm
float fbm(vec3 x)
Definition
density.comp:139
sun_dir
vec3 sun_dir
Definition
main.c:237
sun_color
vec3 sun_color
Definition
main.c:238
VertexInfo::uid
uint32_t uid
Definition
types.h:487
VertexInfo::vcnt
int vcnt
Definition
types.h:486
VertexInfo::pad1
uint32_t pad1
Definition
types.h:489
VertexInfo::pad0
uint32_t pad0
Definition
types.h:488
VertexInfo
Definition
types.h:485
perlin_tex
float * perlin_tex
Definition
init.c:610
fbm_tex
float * fbm_tex
Definition
init.c:346
main
int main(int argc, char *argv[])
Definition
main.c:1726
TRIANGLE_TABLE
const int TRIANGLE_TABLE[256][16]
Definition
marching_cubes.h:57
EDGE_TO_VERTICES
const int EDGE_TO_VERTICES[12][2]
Definition
marching_cubes.h:13
EDGE_TABLE
const int EDGE_TABLE[256]
Definition
marching_cubes.h:20
emitted_chunks
int emitted_chunks
Definition
mesh.c:879
Vertex::color
vec3 color
Definition
types.h:353
Vertex::pad1
float pad1
Definition
types.h:352
Vertex::pad0
float pad0
Definition
types.h:350
projection
uniform mat4 projection
Definition
shadow.vert:8
view
uniform mat4 view
Definition
shadow.vert:9
Vertex::pos
vec3 pos
Definition
types.h:349
Vertex::pad2
float pad2
Definition
types.h:354
Vertex::normal
vec3 normal
Definition
types.h:351
Vertex
Definition
types.h:348
DensityData
Definition
types.h:475
vec3::x
float x
Definition
main.h:5
noise
float noise(vec3 p)
Definition
density.comp:116
vec4::y
float y
Definition
types.h:47
vec3::y
float y
Definition
main.h:5
MAX_CHUNKS
#define MAX_CHUNKS
Definition
types.h:182
vec3::z
float z
Definition
main.h:5
MAX_LAYERS
#define MAX_LAYERS
Definition
types.h:407
ChunkData
Definition
types.h:320
MarchingCubesData
Definition
types.h:339
NodeCounter
Definition
types.h:204
PlanetData
Definition
types.h:409
mat4
Definition
types.h:51
vec2
Definition
types.h:45
vec3
Definition
main.h:5
vec4
Definition
types.h:47
WarpLayer::offset2
vec3 offset2
Definition
types.h:378
FBMLayer::pad1
float pad1
Definition
types.h:403
FBMLayer::amp
float amp
Definition
types.h:401
WarpLayer::freq0
float freq0
Definition
types.h:375
WarpLayer::offset1
vec3 offset1
Definition
types.h:376
FBMLayer::freq
float freq
Definition
types.h:400
PerlinLayer::amp
float amp
Definition
types.h:391
WarpLayer::pad3
float pad3
Definition
types.h:383
WarpLayer::freq1
float freq1
Definition
types.h:377
WarpLayer::pad1
float pad1
Definition
types.h:382
PerlinLayer::pad0
float pad0
Definition
types.h:392
WarpLayer::freq2
float freq2
Definition
types.h:379
WarpLayer::offset0
vec3 offset0
Definition
types.h:374
PerlinLayer::pad1
float pad1
Definition
types.h:393
WarpLayer::pad0
float pad0
Definition
types.h:381
WarpLayer::amp
float amp
Definition
types.h:380
PerlinLayer::freq
float freq
Definition
types.h:390
FBMLayer::pad0
float pad0
Definition
types.h:402
FBMLayer
Definition
types.h:398
PerlinLayer
Definition
types.h:388
WarpLayer
Definition
types.h:372
Generated by
1.18.0