Space Sim
Toggle main menu visibility
Loading...
Searching...
No Matches
src
render
mesh.vert
Go to the documentation of this file.
1
2
#version 460
3
9
layout
(location = 0) in
vec3
aPosition;
10
layout
(location = 1) in
vec3
aNormal;
11
layout
(location = 2) in
vec3
aColor;
12
out
vec3
vNormal
;
13
out
vec4
FragPos
;
14
out
vec4
vColor
;
15
out
float
vDist
;
16
out
vec4
vFragPosLightSpace
;
17
uniform
mat4
light_view
;
18
uniform
mat4
light_proj
;
19
uniform
float
uPlanetRadius
;
20
layout
(std140) uniform CameraData {
21
vec4
cam_pos;
22
vec4
cam_vel;
23
mat4
projection
;
24
mat4
view
;
25
mat4
inv_projection;
26
mat4
inv_view;
27
};
28
29
30
#define BODY_COUNT 30000
31
#define MAX_NODES 100000
32
struct
Particle
33
{
34
vec4
p_m
;
35
vec4
mat
;
36
vec4
vel
;
37
vec4
acc
;
38
};
39
layout
(std430, binding=1) buffer
ParticleData
{
40
41
Particle
p_spawn[
BODY_COUNT
];
42
vec4
p_colors[
BODY_COUNT
];
43
uint p_dead[
BODY_COUNT
];
44
uint claimed[
BODY_COUNT
];
45
int
nodes_visited[
BODY_COUNT
];
46
uint star_particles[
BODY_COUNT
];
47
Particle
p[];
48
};
49
50
51
52
layout
(std430, binding=8) buffer
MarchingCubesData
53
{
54
int
EDGE_TO_VERTICES
[12][2];
55
int
EDGE_TABLE
[256];
56
int
TRIANGLE_TABLE
[256][16];
57
};
58
layout
(std430, binding=9) buffer
DensityData
59
{
60
float
density[];
61
};
62
struct
Vertex
63
{
64
vec3
pos
;
65
float
pad0
;
66
vec3
normal
;
67
float
pad1
;
68
vec3
color
;
69
float
pad2
;
70
};
71
#define LODMAX 4
72
#define MAX_CHUNKS 1000
73
layout
(std430, binding=10) buffer DensityVertexData
74
{
75
int
chunk_vertex_offset[
MAX_CHUNKS
];
76
Vertex
vertices[];
77
};
78
layout
(std430, binding=6) buffer
NodeCounter
{
79
uint queue_out;
80
int
p_dead_cnt;
81
int
p_spawn_cnt;
82
uint star_pcnt;
83
};
84
85
uniform
int
uType
;
86
// uniform vec3 cam_pos;
87
88
#define NUM_OCTAVES 5
89
float
mod289
(
float
x){
return
x - floor(x * (1.0 / 289.0)) * 289.0;}
90
vec4
mod289
(
vec4
x){
return
x - floor(x * (1.0 / 289.0)) * 289.0;}
91
vec4
perm
(
vec4
x){
return
mod289
(((x * 34.0) + 1.0) * x);}
92
93
float
noise
(
vec3
p){
94
vec3
a = floor(p);
95
vec3
d = p - a;
96
d = d * d * (3.0 - 2.0 * d);
97
98
vec4
b = a.xxyy +
vec4
(0.0, 1.0, 0.0, 1.0);
99
vec4
k1 =
perm
(b.xyxy);
100
vec4
k2 =
perm
(k1.xyxy + b.zzww);
101
102
vec4
c = k2 + a.zzzz;
103
vec4
k3 =
perm
(c);
104
vec4
k4 =
perm
(c + 1.0);
105
106
vec4
o1 = fract(k3 * (1.0 / 41.0));
107
vec4
o2 = fract(k4 * (1.0 / 41.0));
108
109
vec4
o3 = o2 * d.
z
+ o1 * (1.0 - d.
z
);
110
vec2
o4 = o3.yw * d.
x
+ o3.xz * (1.0 - d.
x
);
111
112
return
o4.
y
* d.
y
+ o4.x * (1.0 - d.
y
);
113
}
114
115
116
float
fbm
(
vec3
x) {
117
float
v = 0.0;
118
float
a = 0.5;
119
// float f = 1.0;
120
vec3
shift =
vec3
(100);
121
for
(
int
i = 0; i <
NUM_OCTAVES
; ++i) {
122
v += a *
noise
(x);
123
x = x * 2.0 + shift;
124
// f *= 2.5;
125
a *= 0.45;
126
}
127
return
v;
128
}
129
vec3
unpack_normal
(uint p)
130
{
131
float
x = float(p & 0xFF) / 255.0;
132
float
y = float((p >> 8) & 0xFF) / 255.0;
133
134
vec2
f =
vec2
(x, y) * 2.0 - 1.0;
135
136
vec3
n =
vec3
(f.
x
, f.
y
, 1.0 - abs(f.
x
) - abs(f.
y
));
137
138
float
t
=
clamp
(-n.
z
, 0.0, 1.0);
139
n.xy +=
vec2
(n.
x
>= 0.0 ? -
t
:
t
, n.
y
>= 0.0 ? -
t
:
t
);
140
141
return
normalize(n);
142
}
143
uniform
int
uSelected
;
144
uniform sampler3D
perlin_tex
;
145
146
147
148
149
float
density_fn
(
vec3
p)
150
{
151
152
float
d = (p.
z
);
153
154
vec3
warp =
155
vec3
(texture(
perlin_tex
, p * 0.001).r * 2.0f - 1.0f,
156
texture(
perlin_tex
, p* 0.001 +
vec3
(1.0, 1.0, 1.0)).r * 2.0f - 1.0f,
157
texture(
perlin_tex
, p* 0.001 +
vec3
(5.0, 5.0, 5.0)).r * 2.0f - 1.0f);
158
p += warp * 2.0f;
159
d += (float(texture(
perlin_tex
, p * 0.001).r) * 2.0f - 1.0f) * 1.55;
160
d += (float(texture(
perlin_tex
, p * 0.0025).r) * 2.0f - 1.0f) * 1.0;
161
d += (float(texture(
perlin_tex
, p * 0.007).r) * 2.0f - 1.0f) * 0.5;
162
163
d +=
fbm
(p * 0.5) * 8;
164
return
d;
165
}
166
void
main
()
167
{
168
169
170
uint
id
= gl_VertexID;
171
Vertex
v = vertices[id];
172
vec3
pos = v.
pos
;
173
pos.
z
+=
uPlanetRadius
;
174
175
float
height = pos.
z
;
176
float
slope = 1.0 - abs(dot(-v.
normal
,
vec3
(0,0,1)));
177
178
179
vec3
base = mix(
vec3
(0.2,0.35,0.2),
vec3
(0.4,0.25,0.2), slope);
180
base = mix(base,
vec3
(0.4, 0.4, 0.5), max(pos.
z
, 0.0) / 3);
181
182
vColor
=
vec4
(base, 1.0);
183
FragPos
=
vec4
(pos, 1.0);
184
185
vDist
= length(pos - cam_pos.xyz);
186
187
vNormal
= normalize(-v.
normal
);
188
189
190
191
vFragPosLightSpace
=
light_proj
*
light_view
*
vec4
(pos, 1.0);
192
gl_Position =
projection
*
view
*
vec4
(pos , 1.0);
193
}
vNormal
in vec3 vNormal
Definition
atmosphere.frag:11
layout
layout(local_size_x=16, local_size_y=16, local_size_z=1) in
vColor
in vec3 vColor
Definition
clouds.frag:4
uPlanetRadius
uniform float uPlanetRadius
Definition
clouds.frag:15
NUM_OCTAVES
#define NUM_OCTAVES
Definition
density.comp:111
mod289
float mod289(float x)
Definition
density.comp:112
perm
vec4 perm(vec4 x)
Definition
density.comp:114
fbm
float fbm(vec3 x)
Definition
density.comp:139
perlin_tex
float * perlin_tex
Definition
init.c:610
main
int main(int argc, char *argv[])
Definition
main.c:1726
mat
vec4 mat
Definition
main.c:261
vDist
in float vDist
Definition
main.frag:14
density_fn
float density_fn(vec3 p)
Definition
marching_cubes.comp:152
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
clamp
float clamp(float a, float b, float c)
Definition
mathf.h:147
uType
uniform int uType
Definition
mesh.frag:67
vFragPosLightSpace
in vec4 vFragPosLightSpace
Definition
mesh.frag:12
uSelected
uniform int uSelected
Definition
mesh.frag:25
FragPos
in vec4 FragPos
Definition
mesh.frag:10
light_proj
uniform mat4 light_proj
Definition
mesh.vert:18
light_view
uniform mat4 light_view
Definition
mesh.vert:17
unpack_normal
vec3 unpack_normal(uint p)
Definition
mesh.vert:129
Particle::acc
vec4 acc
Definition
types.h:97
Particle::p_m
vec4 p_m
Definition
types.h:94
Particle::vel
vec3 vel
Definition
main.h:10
Particle
Definition
main.h:8
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
vec2::x
float x
Definition
types.h:45
vec3::x
float x
Definition
main.h:5
noise
float noise(vec3 p)
Definition
density.comp:116
BODY_COUNT
#define BODY_COUNT
Definition
types.h:39
vec2::y
float y
Definition
types.h:45
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
MarchingCubesData
Definition
types.h:339
NodeCounter
Definition
types.h:204
ParticleData
Definition
types.h:129
mat4
Definition
types.h:51
vec2
Definition
types.h:45
vec3
Definition
main.h:5
vec4
Definition
types.h:47
t
uniform float t
Definition
water.comp:23
Generated by
1.18.0