Space Sim
Loading...
Searching...
No Matches
main.frag
Go to the documentation of this file.
1#version 430 core
13in float vSize;
14in float vDist;
16flat in uint vPID;
17
18layout(location = 0)out vec4 fragColor;
19layout(location = 1)out int idColor;
20
21uniform sampler2D uSphere;
22uniform samplerCube uSky;
23// uniform vec3 cam_pos;
26uniform vec3 cam_up;
27vec3 blue = vec3(0.4, 0.6, 0.7);
28vec4 white = vec4(1.0, 1.0, 1.0, 1.0);
29#define BODY_COUNT 15000
30#define MAX_NODES 45000
31layout(std140, binding = 1) uniform CameraData {
32 vec4 cam_pos;
33 vec4 cam_vel;
35 mat4 view;
36 mat4 inv_projection;
37 mat4 inv_view;
38};
39layout(std140, binding = 2) uniform RenderingData {
40 ivec4 resolution;
41};
42struct Particle
43{
44 vec4 p_m;
45 vec4 mat;
46 vec4 vel;
47 vec4 acc;
48};
49layout(std430, binding=1) buffer ParticleData {
50
51 Particle p_spawn[BODY_COUNT];
52 vec4 p_colors[BODY_COUNT];
53 uint p_dead[BODY_COUNT];
54 uint claimed[BODY_COUNT];
55 int nodes_visited[BODY_COUNT];
56 uint star_particles[BODY_COUNT];
57 Particle p[];
58};
59struct Node {
60 vec4 com_m;
61 // min and size
62 vec4 min;
63 // max and next
64 vec3 max;
65 int next;
66 // left child, right child, parent, depth
67 ivec4 lrpd;
68 // ivec4 next;
69};
70#define MAX_DEPTH 64
71layout(std430, binding=4) buffer NodeData {
72 uint ready_queue[MAX_NODES];
73 uint children_remaining[MAX_NODES];
74 uint depth_offsets[MAX_DEPTH+1];
75 uint pad0;
76 uint pad1;
77 uint pad2;
78 vec4 com_m[MAX_NODES];
79 vec4 lrsn[MAX_NODES];
80 // min and parent
81 vec4 nmin[MAX_NODES];
82 // max and depth
83 vec4 nmax[MAX_NODES];
84};
85layout(std430, binding=6) buffer NodeCounter {
86 uint queue_out;
87 int p_dead_cnt;
88 int p_spawn_cnt;
89 uint star_pcnt;
90};
91
92uniform sampler3D perlin_tex;
93uniform sampler3D fbm_tex;
94
95#define NUM_OCTAVES 5
96
97float mod289(float x){return x - floor(x * (1.0 / 289.0)) * 289.0;}
98vec4 mod289(vec4 x){return x - floor(x * (1.0 / 289.0)) * 289.0;}
99vec4 perm(vec4 x){return mod289(((x * 34.0) + 1.0) * x);}
100
101float noise(vec3 p){
102 vec3 a = floor(p);
103 vec3 d = p - a;
104 d = d * d * (3.0 - 2.0 * d);
105
106 vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0);
107 vec4 k1 = perm(b.xyxy);
108 vec4 k2 = perm(k1.xyxy + b.zzww);
109
110 vec4 c = k2 + a.zzzz;
111 vec4 k3 = perm(c);
112 vec4 k4 = perm(c + 1.0);
113
114 vec4 o1 = fract(k3 * (1.0 / 41.0));
115 vec4 o2 = fract(k4 * (1.0 / 41.0));
116
117 vec4 o3 = o2 * d.z + o1 * (1.0 - d.z);
118 vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x);
119
120 return o4.y * d.y + o4.x * (1.0 - d.y);
121}
122
123
124float fbm(vec3 x) {
125 float v = 0.0;
126 float a = 0.5;
127 float f = 1.0;
128 vec3 shift = vec3(100);
129 for (int i = 0; i < NUM_OCTAVES; ++i) {
130 v += a * noise(x * f);
131 x = x * 2.0 + shift;
132 f *= 2.5;
133 a *= 0.45;
134 }
135 return v;
136}
137
138float linearize(float z)
139{
140 float near = 0.01;
141 float far = 100000.0;
142 return (2.0 * near) / (far + near - z * (far - near));
143}
144void main()
145{
146 vec2 uv = vTexCoord;
147 float d = dot(uv, uv);
148 if(d > 1.0) discard;
149 float alpha = 1.6 - d;
150 alpha = smoothstep(0.0, 1.0, alpha);
151 uint ppid = vPID;
152 if (length(vColor) == 0) discard;
153 float z = sqrt(max(1.0 - d, 0.0001));
154 Particle u = p[ppid];
155 vec3 center = u.p_m.xyz;
156 vec3 pos = vWorldPos;
157 float size = vSize;
158 vec3 normal = normalize(vec3(uv, z));
159 vec3 fragWorldPos = vCenter + normal * size;
160 float radius = size;
161
162 vec3 forward = normalize(u.vel.xyz);
163 vec3 right = normalize(cross(vec3(0,0,1), forward));
164 vec3 up = normalize(cross(forward, right));
165
166
167 vec3 fragPos =
168 center +
169 cam_right * uv.x * radius +
170 cam_up * uv.y * radius;
171
172 vec3 rayDir = normalize(fragPos - cam_pos.xyz);
173
174
175 vec3 oc = cam_pos.xyz - center;
176
177 float b = dot(oc, rayDir);
178 float c = dot(oc, oc) - radius * radius;
179
180 float h = b*b - c;
181 if (h < 0.0) discard;
182
183 float t = -b - sqrt(h);
184 t = max(t, 0.1);
185
186 vec3 hit = cam_pos.xyz + rayDir * t;
187
188 vec3 worldPos = normalize(hit - center);
189 vec3 nn =
190 vec3(dot(forward, worldPos),
191 dot(right, worldPos),
192 dot(up, worldPos));
193
194 vec4 color = vColor;
195 vec4 adiff = vec4(0.1, 0.1, 0.1, 1);
196 vec4 mdiff = vec4(0.0, 0.0, 0.0, 1);
197 vec4 sdiff = vec4(0.0, 0.0, 0.0, 1);
198 vec3 viewDir = normalize(-fragWorldPos);
199 for (int i = 0; i < 1; i++)
200 {
201 uint pid = star_particles[i];
202 Particle e = p[pid];
203 if (pid == ppid) {
204 float mt = clamp(log(e.p_m.w) * 0.2, 0.0, 1.0);
205
206 vec3 star_color = mix(
207 vec3(1.0, 0.3, 0.2),
208 vec3(0.6, 0.8, 1.0),
209 mt
210 );
211 mdiff.rgb += p_colors[pid].rgb * star_color;
212 continue;
213 }
214 vec3 lightVS = (view * vec4(e.p_m.xyz,1)).xyz;
215 vec3 dist = lightVS - fragWorldPos;
216 float r2 = dot(dist, dist) + 0.01;
217 float attenuation = min(500 / r2, 1.0);
218 vec3 L = normalize(dist);
219 float diff = max(dot(normal, L), 0.0);
220 adiff += p_colors[pid] /* * attenuation */ * 0.05;
221 mdiff += p_colors[pid] /* * attenuation */ * diff * 0.9;
222 }
223 color.rgb *= mdiff.rgb + sdiff.rgb + adiff.rgb;
224
225 int out_pid = int(ppid);
226
227 float depth = linearize(gl_FragCoord.z);
228 fragColor = vec4(color.rgb, depth);
229}
out vec4 fragColor
in vec3 vNormal
layout(local_size_x=16, local_size_y=16, local_size_z=1) in
const float radius
Definition camera.c:580
in vec3 vColor
Definition clouds.frag:4
in vec3 vWorldPos
Definition clouds.frag:9
uniform int depth
Definition com22.comp:76
#define NUM_OCTAVES
Definition density.comp:111
float mod289(float x)
Definition density.comp:112
vec4 perm(vec4 x)
Definition density.comp:114
float fbm(vec3 x)
Definition density.comp:139
float * perlin_tex
Definition init.c:610
float * fbm_tex
Definition init.c:346
int main(int argc, char *argv[])
Definition main.c:1726
vec3 blue
Definition main.frag:27
in vec2 vTexCoord
Definition main.frag:12
uniform sampler2D uSphere
Definition main.frag:21
in vec3 vVelocity
Definition main.frag:8
vec4 white
Definition main.frag:28
in float vDist
Definition main.frag:14
in vec3 vViewVelocity
Definition main.frag:10
uniform vec3 cam_right
Definition main.frag:24
uniform vec3 cam_up
Definition main.frag:26
uniform samplerCube uSky
Definition main.frag:22
float linearize(float z)
Definition main.frag:138
in vec3 vCenter
Definition main.frag:15
uniform vec3 cam_forward
Definition main.frag:25
flat in uint vPID
Definition main.frag:16
in float vSize
Definition main.frag:13
vec3 max
Definition com22.comp:49
vec4 com_m
Definition com22.comp:45
int next
Definition com22.comp:50
ivec4 lrpd
Definition com22.comp:52
vec4 min
Definition com22.comp:47
Definition types.h:109
float clamp(float a, float b, float c)
Definition mathf.h:147
vec4 acc
Definition types.h:97
vec4 p_m
Definition types.h:94
vec4 mat
Definition types.h:95
vec3 vel
Definition main.h:10
Definition main.h:8
uniform mat4 projection
Definition shadow.vert:8
uniform mat4 view
Definition shadow.vert:9
const int size
Definition sky.frag:9
float x
Definition types.h:45
float w
Definition types.h:47
float x
Definition main.h:5
#define MAX_DEPTH
Definition types.h:154
float noise(vec3 p)
Definition density.comp:116
#define BODY_COUNT
Definition types.h:39
float y
Definition types.h:45
float y
Definition types.h:47
float y
Definition main.h:5
float z
Definition main.h:5
#define MAX_NODES
Definition types.h:40
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
uniform float t
Definition water.comp:23