checkin
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / model_sky.h
1 #ifndef SHADER_model_sky_H
2 #define SHADER_model_sky_H
3 static void shader_model_sky_link(void);
4 static void shader_model_sky_register(void);
5 static struct vg_shader _shader_model_sky = {
6 .name = "model_sky",
7 .link = shader_model_sky_link,
8 .vs =
9 {
10 .orig_file = "shaders/model.vs",
11 .static_src =
12 "layout (location=0) in vec3 a_co;\n"
13 "layout (location=1) in vec3 a_norm;\n"
14 "layout (location=2) in vec2 a_uv;\n"
15 "layout (location=3) in vec4 a_colour;\n"
16 "layout (location=4) in vec4 a_weights;\n"
17 "layout (location=5) in ivec4 a_groups;\n"
18 "\n"
19 "#line 1 1 \n"
20 "const float k_motion_lerp_amount = 0.01;\n"
21 "\n"
22 "#line 2 0 \n"
23 "\n"
24 "out vec3 aMotionVec0;\n"
25 "out vec3 aMotionVec1;\n"
26 "\n"
27 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
28 "{\n"
29 " // This magically solves some artifacting errors!\n"
30 " //\n"
31 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
32 "\n"
33 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
34 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
35 "}\n"
36 "\n"
37 "#line 9 0 \n"
38 "\n"
39 "uniform mat4x3 uMdl;\n"
40 "uniform mat4 uPv;\n"
41 "uniform mat4 uPvmPrev;\n"
42 "\n"
43 "out vec4 aColour;\n"
44 "out vec2 aUv;\n"
45 "out vec3 aNorm;\n"
46 "out vec3 aCo;\n"
47 "out vec3 aWorldCo;\n"
48 "\n"
49 "void main()\n"
50 "{\n"
51 " vec3 world_pos0 = uMdl * vec4( a_co, 1.0 );\n"
52 " vec4 vproj0 = uPv * vec4( world_pos0, 1.0 );\n"
53 " vec4 vproj1 = uPvmPrev * vec4( a_co, 1.0 );\n"
54 "\n"
55 " vs_motion_out( vproj0, vproj1 );\n"
56 "\n"
57 " gl_Position = vproj0;\n"
58 " aWorldCo = world_pos0;\n"
59 " aColour = a_colour;\n"
60 " aUv = a_uv;\n"
61 " aNorm = mat3(uMdl) * a_norm;\n"
62 " aCo = a_co;\n"
63 "}\n"
64 ""},
65 .fs =
66 {
67 .orig_file = "shaders/model_sky.fs",
68 .static_src =
69 "uniform sampler2D uTexGarbage;\n"
70 "\n"
71 "in vec4 aColour;\n"
72 "in vec2 aUv;\n"
73 "in vec3 aNorm;\n"
74 "in vec3 aCo;\n"
75 "in vec3 aWorldCo;\n"
76 "\n"
77 "#line 1 1 \n"
78 "layout (location = 0) out vec4 oColour;\n"
79 "\n"
80 "layout (std140) uniform ub_world_lighting\n"
81 "{\n"
82 " vec4 g_light_colours[3];\n"
83 " vec4 g_light_directions[3];\n"
84 " vec4 g_ambient_colour;\n"
85 "\n"
86 " vec4 g_water_plane;\n"
87 " vec4 g_depth_bounds;\n"
88 " float g_water_fog;\n"
89 " float g_time;\n"
90 " int g_light_count;\n"
91 " int g_light_preview;\n"
92 " int g_shadow_samples;\n"
93 "\n"
94 " int g_debug_indices;\n"
95 " int g_debug_complexity;\n"
96 "\n"
97 " // g_time ?\n"
98 "\n"
99 " //vec4 g_point_light_positions[32];\n"
100 " //vec4 g_point_light_colours[32];\n"
101 "};\n"
102 "\n"
103 "uniform sampler2D g_world_depth;\n"
104 "\n"
105 "float world_depth_sample( vec3 pos )\n"
106 "{\n"
107 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
108 " return texture( g_world_depth, depth_coord ).r;\n"
109 "}\n"
110 "\n"
111 "float world_water_depth( vec3 pos )\n"
112 "{\n"
113 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
114 " float ref_depth = g_water_plane.y*g_water_plane.w;\n"
115 " return texture( g_world_depth, depth_coord ).g - ref_depth;\n"
116 "}\n"
117 "\n"
118 "float shadow_sample( vec3 vdir )\n"
119 "{\n"
120 " vec3 sample_pos = aWorldCo + vdir;\n"
121 " float height_sample = world_depth_sample( sample_pos );\n"
122 "\n"
123 " float fdelta = height_sample - sample_pos.y;\n"
124 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
125 "}\n"
126 "\n"
127 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
128 "{\n"
129 " float dist = pow(fdist*0.0008,1.2);\n"
130 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
131 "}\n"
132 "\n"
133 "\n"
134 "// New lighting model\n"
135 "\n"
136 "vec3 newlight_compute_ambient()\n"
137 "{\n"
138 " return g_ambient_colour.rgb;\n"
139 "}\n"
140 "\n"
141 "float newlight_compute_sun_shadow( vec3 dir )\n"
142 "{\n"
143 " if( g_shadow_samples == 0 )\n"
144 " {\n"
145 " return 1.0;\n"
146 " }\n"
147 "\n"
148 " float fspread = g_light_colours[0].w;\n"
149 " vec3 vdir = dir;\n"
150 " float flength = g_light_directions[0].w;\n"
151 "\n"
152 " float famt = 0.0;\n"
153 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
154 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
155 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
156 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
157 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
158 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
159 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
160 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
161 "\n"
162 " return 1.0 - famt;\n"
163 "}\n"
164 "\n"
165 "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n"
166 "{\n"
167 " vec3 vtotal = g_ambient_colour.rgb;\n"
168 "\n"
169 " for( int i=0; i<g_light_count; i++ )\n"
170 " {\n"
171 " vec3 vcolour = g_light_colours[i].rgb;\n"
172 " vec3 vdir = g_light_directions[i].xyz;\n"
173 "\n"
174 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
175 " vtotal += vcolour*flight;\n"
176 " }\n"
177 "\n"
178 " return vtotal;\n"
179 "}\n"
180 "\n"
181 "vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )\n"
182 "{\n"
183 " vec3 vcolour = g_light_colours[0].rgb;\n"
184 " vec3 vdir = g_light_directions[0].xyz;\n"
185 "\n"
186 " vec3 specdir = reflect( -vdir, wnormal );\n"
187 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
188 " return vcolour*spec*fintensity;\n"
189 "}\n"
190 "\n"
191 "float newlight_specular( vec3 wnormal, vec3 dir, vec3 halfview, float exponent )\n"
192 "{\n"
193 " vec3 specdir = reflect( -dir, wnormal );\n"
194 " return pow(max(dot( halfview, specdir ), 0.0), exponent);\n"
195 "}\n"
196 "\n"
197 "vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, \n"
198 " vec3 light_colour, vec3 light_pos )\n"
199 "{\n"
200 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
201 "\n"
202 " float quadratic = dot(light_delta,light_delta);\n"
203 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
204 " attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );\n"
205 "\n"
206 " return light_colour*attenuation;\n"
207 "}\n"
208 "\n"
209 "vec3 newlight_compute_spot( vec3 wnormal, vec3 halfview, \n"
210 " vec3 light_colour, vec3 light_pos,\n"
211 " vec4 light_dir )\n"
212 "{\n"
213 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
214 "\n"
215 " float quadratic = dot(light_delta,light_delta);\n"
216 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
217 "\n"
218 " light_delta = normalize( light_delta );\n"
219 " attenuation *= max( 0.0, dot( light_delta, wnormal ) );\n"
220 "\n"
221 " float spot_theta = max( 0.0, dot( light_delta, -light_dir.xyz ) ),\n"
222 " falloff = max( 0.0,( spot_theta - light_dir.w ) / (1.0-light_dir.w) );\n"
223 "\n"
224 " return light_colour*attenuation*falloff;\n"
225 "}\n"
226 "\n"
227 "#line 10 0 \n"
228 "#line 1 2 \n"
229 "const float k_motion_lerp_amount = 0.01;\n"
230 "\n"
231 "#line 2 0 \n"
232 "\n"
233 "layout (location = 1) out vec2 oMotionVec;\n"
234 "\n"
235 "in vec3 aMotionVec0;\n"
236 "in vec3 aMotionVec1;\n"
237 "\n"
238 "void compute_motion_vectors()\n"
239 "{\n"
240 " // Write motion vectors\n"
241 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
242 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
243 "\n"
244 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
245 "}\n"
246 "\n"
247 "#line 11 0 \n"
248 "#line 1 3 \n"
249 "const vec3 DAYSKY_COLOUR = vec3( 0.37, 0.54, 0.97 );\n"
250 "const vec3 NIGHTSKY_COLOUR = vec3( 0.03, 0.05, 0.20 );\n"
251 "const vec3 SUNSET_COLOUR = vec3( 1.00, 0.32, 0.01 );\n"
252 "const vec3 AMBIENT_COLOUR = vec3( 0.13, 0.17, 0.35 );\n"
253 "const vec3 SUNSET_AMBIENT = vec3( 0.25, 0.17, 0.51 );\n"
254 "const vec3 SUN_COLOUR = vec3( 1.10, 0.89, 0.35 ) * 1.125;\n"
255 "\n"
256 "const float SUN_ANGLE = 0.0001;\n"
257 "const float TIME_RATE = 0.025;\n"
258 "\n"
259 "const float PI = 3.14159265;\n"
260 "\n"
261 "struct world_info\n"
262 "{\n"
263 " float time,\n"
264 " time_of_day,\n"
265 " day_phase,\n"
266 " sunset_phase;\n"
267 " \n"
268 " vec3 sun_dir;\n"
269 "};\n"
270 "\n"
271 "float luminance( vec3 v )\n"
272 "{\n"
273 " return dot( v, vec3(0.2126, 0.7152, 0.0722) );\n"
274 "}\n"
275 "\n"
276 "vec3 scene_ambient( vec3 dir, const world_info w )\n"
277 "{\n"
278 " float sun_azimuth = dot( dir.xz, w.sun_dir.xz ) * 0.4 + 0.6;\n"
279 " float sky_gradient = dir.y;\n"
280 " \n"
281 " /* Blend phase colours */\n"
282 " vec3 ambient = DAYSKY_COLOUR * (w.day_phase-w.sunset_phase*0.1);\n"
283 " ambient += SUNSET_COLOUR * (1.0-dir.y*0.5) * w.sunset_phase * sun_azimuth;\n"
284 " ambient += NIGHTSKY_COLOUR * (1.0-w.day_phase);\n"
285 " \n"
286 " /* Add gradient */\n"
287 " ambient -= sky_gradient * luminance(ambient);\n"
288 " \n"
289 " return ambient;\n"
290 "}\n"
291 "\n"
292 "vec3 scene_sky( vec3 ray_dir, const world_info w )\n"
293 "{\n"
294 " ray_dir.y = abs( ray_dir.y );\n"
295 " vec3 sky_colour = scene_ambient( ray_dir, w );\n"
296 " \n"
297 " /* Sun */\n"
298 " float sun_theta = dot( ray_dir, w.sun_dir );\n"
299 " float sun_size = max( 0.0, sun_theta * 0.5 + 0.5 + SUN_ANGLE );\n"
300 " float sun_shape = pow( sun_size, 2000.0 );\n"
301 " sun_shape += sun_size * max(w.sun_dir.y,0.0) * 0.5;\n"
302 " \n"
303 " vec3 sun_colour = mix( vec3(1.0), SUNSET_COLOUR, w.sunset_phase*0.5 );\n"
304 " sun_colour *= sun_shape;\n"
305 " \n"
306 " vec3 composite = sky_colour + sun_colour;\n"
307 " return composite;\n"
308 "}\n"
309 "\n"
310 "vec3 scene_compute_ambient( vec3 normal, const world_info w )\n"
311 "{\n"
312 " return scene_ambient( (normal * vec3(1.0,-1.0,1.0)) * 0.5 + 0.5, w );\n"
313 "}\n"
314 "\n"
315 "vec3 SR_LIGHT( vec3 normal, vec2 dir, vec3 colour )\n"
316 "{\n"
317 " vec3 dir3 = vec3\n"
318 " (\n"
319 " cos(dir.y) * cos(dir.x),\n"
320 " sin(dir.x),\n"
321 " sin(dir.y) * cos(dir.x)\n"
322 " );\n"
323 "\n"
324 " float flight = max( dot( normal, dir3 ) * 0.75 + 0.25, 0.0 );\n"
325 " \n"
326 " return flight * colour;\n"
327 "}\n"
328 "\n"
329 "vec3 scene_lighting_old( vec3 normal, const world_info w )\n"
330 "{\n"
331 " vec3 SR_COLOUR_SUN = vec3( 1.36, 1.35, 1.01 );\n"
332 " vec3 SR_COLOUR_FILL = vec3( 0.33, 0.56, 0.64 );\n"
333 " vec3 SR_COLOUR_RIM = vec3( 0.05, 0.05, 0.23 );\n"
334 " \n"
335 " return SR_LIGHT( normal, vec2( 0.63, -0.08 ), SR_COLOUR_SUN ) +\n"
336 " SR_LIGHT( normal, vec2( -2.60, -0.13 ), SR_COLOUR_FILL ) + \n"
337 " SR_LIGHT( normal, vec2( 2.60, -0.84 ), SR_COLOUR_RIM ) ;\n"
338 "}\n"
339 "\n"
340 "vec3 scene_lighting( vec3 normal, float shadow, vec3 halfview, const world_info w )\n"
341 "{\n"
342 " float fresnel = 1.0 - abs(dot(normal,halfview));\n"
343 "\n"
344 " vec3 sky_reflection = 0.5 * fresnel * mix( DAYSKY_COLOUR, SUNSET_COLOUR, w.sunset_phase );\n"
345 " vec3 light_sun = max(0.0,dot(normal,w.sun_dir)*0.75+0.25) * SUN_COLOUR \n"
346 " * w.day_phase;\n"
347 "\n"
348 " float scaled_shadow = max( shadow, 1.0 - max(w.sun_dir.y,0.0) );\n"
349 "\n"
350 " vec3 ambient = mix( AMBIENT_COLOUR, SUNSET_AMBIENT, w.sunset_phase );\n"
351 "\n"
352 " return ambient + (light_sun + sky_reflection) * shadow;\n"
353 "\n"
354 "\n"
355 "\n"
356 "\n"
357 "\n"
358 "\n"
359 " float sun_theta = dot( normal, w.sun_dir );\n"
360 "\n"
361 " float softness_min = 0.5;\n"
362 " float softness = softness_min + w.sunset_phase * (1.0-softness_min);\n"
363 " float light_min = 0.0 * w.day_phase;\n"
364 " float light_direct = light_min + smoothstep( -softness, softness, sun_theta ) * (1.0-light_min);\n"
365 " light_direct *= clamp(w.sun_dir.y * 4.0 + 1.0,0.0,1.0) * shadow;\n"
366 " \n"
367 " float light_bounce = 0.5 + 0.5 * dot( -normal, w.sun_dir );\n"
368 " light_bounce *= light_bounce * max( w.sun_dir.y, 0.0 );\n"
369 " \n"
370 " vec3 light_colour = SUN_COLOUR*w.day_phase + (SUNSET_COLOUR*w.sunset_phase + 0.1);\n"
371 " vec3 dark_colour = mix( AMBIENT_COLOUR, SUNSET_AMBIENT, w.sunset_phase );\n"
372 " \n"
373 " float spec = newlight_specular( normal, w.sun_dir, halfview, 2.0 ) \n"
374 " * 0.2 * shadow * w.day_phase;\n"
375 " \n"
376 " return mix(dark_colour, light_colour, light_direct) + \n"
377 " spec +\n"
378 " dark_colour * light_bounce;\n"
379 "}\n"
380 "\n"
381 "void scene_state( float world_time, out world_info w )\n"
382 "{\n"
383 " w.time = world_time;\n"
384 " w.time_of_day = fract( w.time );\n"
385 " w.day_phase = cos( w.time_of_day * PI * 2.0 ) * 0.5 + 0.5;\n"
386 " w.sunset_phase = cos( w.time_of_day * PI * 4.0 + PI ) * 0.5 + 0.5;\n"
387 " w.sunset_phase = pow( w.sunset_phase, 6.0 );\n"
388 " \n"
389 " float a = w.time_of_day * PI * 2.0;\n"
390 " w.sun_dir = normalize( vec3( sin( a ), cos( a ), 0.2) );\n"
391 "}\n"
392 "\n"
393 "\n"
394 "#line 12 0 \n"
395 "\n"
396 "void main()\n"
397 "{\n"
398 " compute_motion_vectors();\n"
399 "\n"
400 " world_info world;\n"
401 " scene_state( g_time, world );\n"
402 "\n"
403 " vec3 rd = normalize(aNorm);\n"
404 "\n"
405 " float fmove = g_time * 0.004;\n"
406 " vec2 cloudplane = (rd.xz / (rd.y*sign(rd.y))) * 0.025;\n"
407 " vec4 clouds1 = texture( uTexGarbage, cloudplane + vec2(0.1,0.4)*fmove*2.0 );\n"
408 " vec4 clouds2 = texture( uTexGarbage, cloudplane*2.0 + vec2(0.3,0.1)*fmove );\n"
409 "\n"
410 " float cloud_d = max(clouds1.b*clouds2.r -0.2 - clouds2.g*0.4,0.0);\n"
411 " float cloud_e = pow(cloud_d,1.5)*pow(abs(rd.y),0.3)*2.0;\n"
412 "\n"
413 " oColour = vec4( scene_sky( -rd, world ) ,1.0);\n"
414 "\n"
415 " vec3 cloud_colour = mix( mix(NIGHTSKY_COLOUR,vec3(1.0),world.day_phase), \n"
416 " SUNSET_COLOUR, world.sunset_phase );\n"
417 " oColour.rgb = mix( oColour.rgb, cloud_colour, cloud_e );\n"
418 "}\n"
419 ""},
420 };
421
422 static GLuint _uniform_model_sky_uMdl;
423 static GLuint _uniform_model_sky_uPv;
424 static GLuint _uniform_model_sky_uPvmPrev;
425 static GLuint _uniform_model_sky_uTexGarbage;
426 static GLuint _uniform_model_sky_g_world_depth;
427 static void shader_model_sky_uMdl(m4x3f m){
428 glUniformMatrix4x3fv(_uniform_model_sky_uMdl,1,GL_FALSE,(float*)m);
429 }
430 static void shader_model_sky_uPv(m4x4f m){
431 glUniformMatrix4fv(_uniform_model_sky_uPv,1,GL_FALSE,(float*)m);
432 }
433 static void shader_model_sky_uPvmPrev(m4x4f m){
434 glUniformMatrix4fv(_uniform_model_sky_uPvmPrev,1,GL_FALSE,(float*)m);
435 }
436 static void shader_model_sky_uTexGarbage(int i){
437 glUniform1i(_uniform_model_sky_uTexGarbage,i);
438 }
439 static void shader_model_sky_g_world_depth(int i){
440 glUniform1i(_uniform_model_sky_g_world_depth,i);
441 }
442 static void shader_model_sky_register(void){
443 vg_shader_register( &_shader_model_sky );
444 }
445 static void shader_model_sky_use(void){ glUseProgram(_shader_model_sky.id); }
446 static void shader_model_sky_link(void){
447 _uniform_model_sky_uMdl = glGetUniformLocation( _shader_model_sky.id, "uMdl" );
448 _uniform_model_sky_uPv = glGetUniformLocation( _shader_model_sky.id, "uPv" );
449 _uniform_model_sky_uPvmPrev = glGetUniformLocation( _shader_model_sky.id, "uPvmPrev" );
450 _uniform_model_sky_uTexGarbage = glGetUniformLocation( _shader_model_sky.id, "uTexGarbage" );
451 _uniform_model_sky_g_world_depth = glGetUniformLocation( _shader_model_sky.id, "g_world_depth" );
452 }
453 #endif /* SHADER_model_sky_H */