Space Sim
Loading...
Searching...
No Matches
mesh.frag
Go to the documentation of this file.
1#version 430 core
7
11in float vDist;
13layout(location = 0)out vec4 fragColor;
14layout(std140) uniform CameraData {
15 vec4 cam_pos;
16 vec4 cam_vel;
18 mat4 view;
19 mat4 inv_projection;
20 mat4 inv_view;
21};
22// uniform vec3 cam_pos;
23
24
25uniform int uSelected;
26uniform float uTime;
27#define BODY_COUNT 30000
28#define MAX_NODES 100000
29struct Particle
30{
31 vec4 p_m;
32 vec4 mat;
33 vec4 vel;
34 vec4 acc;
35};
36layout(std430, binding=1) buffer ParticleData {
37
38 Particle p_spawn[BODY_COUNT];
39 vec4 p_colors[BODY_COUNT];
40 uint p_dead[BODY_COUNT];
41 uint claimed[BODY_COUNT];
42 int nodes_visited[BODY_COUNT];
43 uint star_particles[BODY_COUNT];
44 Particle p[];
45};
46layout(std430, binding=6) buffer NodeCounter {
47 uint queue_out;
48 int p_dead_cnt;
49 int p_spawn_cnt;
50 uint star_pcnt;
51};
52
53#define LODMAX 4
54#define MAX_CHUNKS 1000
55layout(std430, binding=12) buffer ChunkData {
56 float lod_dist[3];
57 float cpad0;
58 int vertex_counts[];
59};
60
61uniform sampler2D normal_map;
62uniform sampler2D diffuse_tex;
63uniform sampler2D spec_tex;
64uniform sampler2D ao_tex;
65uniform sampler2D shadow_map;
66
67uniform int uType;
68uniform int lod_level;
69uniform int cid;
70#define MAX_LAYERS 24
71
72struct WarpLayer
73{
75 float freq0;
77 float freq1;
79 float freq2;
80 float amp;
81 float pad0;
82 float pad1;
83 float pad3;
84};
85
86struct PerlinLayer
87{
88 float freq;
89 float amp;
90 float pad0;
91 float pad1;
92};
93
94struct FBMLayer
95{
96 float freq;
97 float amp;
98 float pad0;
99 float pad1;
100};
101
102layout(std430, binding=13) buffer PlanetData {
103
104 int warp_count;
105 int perlin_count;
106 int fbm_count;
107 int pad0;
110 WarpLayer warp_layers[MAX_LAYERS];
111 PerlinLayer perlin_layers[MAX_LAYERS];
112 FBMLayer fbm_layers[MAX_LAYERS];
113
114};
115
116// const vec3 light_dir[2] = {vec3(0.1, 0.2, -0.8), vec3(-0.3, 0.2, 0.5)};
117// const vec3 light[2] = {vec3(1.0, 0.7, 0.5), vec3(0.4, 0.1, 0.6)};
118
120 0.0, 0.5, 0.125, 0.625,
121 0.75, 0.25, 0.875, 0.375,
122 0.1875, 0.6875, 0.0625, 0.5625,
123 0.9375, 0.4375, 0.8125, 0.3125
124);
125const int bayer16x16[16][16] = {{0, 128, 32, 160, 8, 136, 40, 168, 2, 130, 34, 162, 10, 138, 42, 170},
126{192, 64, 224, 96, 200, 72, 232, 104, 194, 66, 226, 98, 202, 74, 234, 106},
127{48, 176, 16, 144, 56, 184, 24, 152, 50, 178, 18, 146, 58, 186, 26, 154},
128{240, 112, 208, 80, 248, 120, 216, 88, 242, 114, 210, 82, 250, 122, 218, 90},
129{12, 140, 44, 172, 4, 132, 36, 164, 14, 142, 46, 174, 6, 134, 38, 166},
130{204, 76, 236, 108, 196, 68, 228, 100, 206, 78, 238, 110, 198, 70, 230, 102},
131{60, 188, 28, 156, 52, 180, 20, 148, 62, 190, 30, 158, 54, 182, 22, 150},
132{252, 124, 220, 92, 244, 116, 212, 84, 254, 126, 222, 94, 246, 118, 214, 86},
133{3, 131, 35, 163, 11, 139, 43, 171, 1, 129, 33, 161, 9, 137, 41, 169},
134{195, 67, 227, 99, 203, 75, 235, 107, 193, 65, 225, 97, 201, 73, 233, 105},
135{51, 179, 19, 147, 59, 187, 27, 155, 49, 177, 17, 145, 57, 185, 25, 153},
136{243, 115, 211, 83, 251, 123, 219, 91, 241, 113, 209, 81, 249, 121, 217, 89},
137{15, 143, 47, 175, 7, 135, 39, 167, 13, 141, 45, 173, 5, 133, 37, 165},
138{207, 79, 239, 111, 199, 71, 231, 103, 205, 77, 237, 109, 197, 69, 229, 101},
139{63, 191, 31, 159, 55, 183, 23, 151, 61, 189, 29, 157, 53, 181, 21, 149},
140{255, 127, 223, 95, 247, 119, 215, 87, 253, 125, 221, 93, 245, 117, 213, 85}};
141
142// const vec2 screen_res = vec2(1280, 960);
143float hash(vec2 p)
144{
145 return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
146}
147
148uniform float uChunkDist;
149const vec3 tX = vec3(0, 0, 1);
150const vec3 bX = vec3(0, 1, 0);
151const vec3 nXw = vec3(1, 0, 0);
152
153const vec3 tY = vec3(1, 0, 0);
154const vec3 bY = vec3(0, 0, 1);
155const vec3 nYw = vec3(0, 1, 0);
156
157const vec3 tZ = vec3(1, 0, 0);
158const vec3 bZ = vec3(0, 1, 0);
159const vec3 nZw = vec3(0, 0, 1);
160
161
162float shadow_calculation(vec4 pos, float diff)
163{
164 vec3 proj_coords = pos.xyz / pos.w;
165 proj_coords = proj_coords * 0.5 + 0.5;
166 float closest_depth = texture(shadow_map, proj_coords.xy).r;
167 float current_depth = proj_coords.z;
168
169
170 float bias = max(0.05f * (diff), 0.005f);
171 float shadow = 0.0f;
172 vec2 texel_size = 1.0 / textureSize(shadow_map, 0);
173 for (int y = -1; y <= 1; y++)
174 {
175
176 for (int x = -1; x <= 1; x++)
177 {
178
179 float pcfDepth = texture(shadow_map, proj_coords.xy + vec2(x, y) * texel_size).r;
180
181 shadow += (current_depth - bias > pcfDepth ? 1.0 : 0.0);
182 }
183 }
184 // float shadow = (current_depth - bias > closest_depth ? 1.0 : 0.0);
185 shadow /= 9;
186 if (proj_coords.z > 1.0) shadow = 0.0;
187
188 return shadow;
189}
190
191float linearize(float z)
192{
193 float near = 0.01;
194 float far = 100000.0;
195 return (2.0 * near) / (far + near - z * (far - near));
196}
197layout(std140, binding = 2) uniform RenderingData {
198 ivec4 resolution;
199};
200void main()
201{
202
203
204
205
206 int lod = lod_level;
207 float dist = vDist;
208
209
210 float quantized_dist = floor(dist / 20);
211
212 float debug_color = quantized_dist / 10;
213
214 float bias = 0.05;
215
216
217
218 float s = sin(uTime * 0.01);
219 float c = cos(uTime * 0.01);
220
221 vec3 n = normalize(vec3(vNormal.x, vNormal.y, vNormal.z));
222 vec3 adiff;
223 vec3 blending = abs(n);
224 blending /= (blending.x + blending.y + blending.z);
225 float scale = 0.1;
226 vec2 uvX = FragPos.yz * scale;
227 vec2 uvY = FragPos.xz * scale;
228 vec2 uvZ = FragPos.xy * scale;
229 vec3 texX = texture(diffuse_tex, uvX).rgb;
230 vec3 texY = texture(diffuse_tex, uvY).rgb;
231 vec3 texZ = texture(diffuse_tex, uvZ).rgb;
232
233 adiff = texX * blending.x +
234 texY * blending.y +
235 texZ * blending.z;
236 vec3 viewDir = normalize(FragPos.xyz - cam_pos.xyz);
237
238
239 vec3 color = vec3(0.1);
240 vec3 L = normalize(sun_dir.xyz);
241 float diffuse = max(dot(n, L), 0.0);
242 float shadow = shadow_calculation(vFragPosLightSpace, diffuse);
243
244 vec3 halfway_dir = normalize(viewDir + L);
245 color += diffuse * sun_color.rgb * 2 * (1 - shadow) + sun_color.rgb * 0.3;
246 color *= adiff;
247 fragColor = vec4(color.rgb, linearize(gl_FragCoord.z));
248}
out vec4 fragColor
in vec3 vNormal
float scale(float fCos)
layout(local_size_x=16, local_size_y=16, local_size_z=1) in
in vec3 vColor
Definition clouds.frag:4
uniform int lod
Definition density.comp:154
vec3 sun_dir
Definition main.c:237
vec3 sun_color
Definition main.c:238
int main(int argc, char *argv[])
Definition main.c:1726
vec4 mat
Definition main.c:261
float uTime
Definition main.c:775
float hash(uint x)
Definition main.comp:91
in float vDist
Definition main.frag:14
float linearize(float z)
Definition main.frag:138
bool s
Definition menu.c:6
uniform sampler2D normal_map
Definition mesh.frag:61
uniform int uType
Definition mesh.frag:67
uniform sampler2D ao_tex
Definition mesh.frag:64
uniform int lod_level
Definition mesh.frag:68
const int bayer16x16[16][16]
Definition mesh.frag:125
in vec4 vFragPosLightSpace
Definition mesh.frag:12
float shadow_calculation(vec4 pos, float diff)
Definition mesh.frag:162
const vec3 tX
Definition mesh.frag:149
const mat4 ditherMatrix
Definition mesh.frag:119
const vec3 tY
Definition mesh.frag:153
const vec3 bX
Definition mesh.frag:150
const vec3 nZw
Definition mesh.frag:159
const vec3 nYw
Definition mesh.frag:155
const vec3 bY
Definition mesh.frag:154
uniform sampler2D spec_tex
Definition mesh.frag:63
uniform int cid
Definition mesh.frag:69
uniform int uSelected
Definition mesh.frag:25
uniform sampler2D diffuse_tex
Definition mesh.frag:62
const vec3 tZ
Definition mesh.frag:157
uniform sampler2D shadow_map
Definition mesh.frag:65
in vec4 FragPos
Definition mesh.frag:10
const vec3 bZ
Definition mesh.frag:158
uniform float uChunkDist
Definition mesh.frag:148
const vec3 nXw
Definition mesh.frag:151
vec4 acc
Definition types.h:97
vec4 p_m
Definition types.h:94
vec3 vel
Definition main.h:10
Definition main.h:8
uniform mat4 projection
Definition shadow.vert:8
uniform mat4 view
Definition shadow.vert:9
float w
Definition types.h:47
float x
Definition main.h:5
#define BODY_COUNT
Definition types.h:39
float y
Definition main.h:5
float z
Definition main.h:5
#define MAX_LAYERS
Definition types.h:407
Definition types.h:49
Definition types.h:51
Definition types.h:45
Definition main.h:5
Definition types.h:47
#define L
Definition water.comp:15
vec3 offset2
Definition types.h:378
float pad1
Definition types.h:403
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 pad3
Definition types.h:383
float freq1
Definition types.h:377
float pad1
Definition types.h:382
float pad0
Definition types.h:392
float freq2
Definition types.h:379
vec3 offset0
Definition types.h:374
float pad1
Definition types.h:393
float pad0
Definition types.h:381
float amp
Definition types.h:380
float freq
Definition types.h:390
float pad0
Definition types.h:402