5bd8fd574623f83364071ea140c9013113a6336d
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / scene_route.h
1 #ifndef SHADER_scene_route_H
2 #define SHADER_scene_route_H
3 static void shader_scene_route_link(void);
4 static void shader_scene_route_register(void);
5 static struct vg_shader _shader_scene_route = {
6 .name = "scene_route",
7 .link = shader_scene_route_link,
8 .vs =
9 {
10 .static_src =
11 "layout (location=0) in vec3 a_co;\n"
12 "layout (location=1) in vec4 a_norm;\n"
13 "layout (location=2) in vec2 a_uv;\n"
14 "layout (location=3) in ivec4 a_lights;\n"
15 "\n"
16 "#line 1 1 \n"
17 "const float k_motion_lerp_amount = 0.01;\n"
18 "\n"
19 "#line 2 0 \n"
20 "\n"
21 "out vec3 aMotionVec0;\n"
22 "out vec3 aMotionVec1;\n"
23 "\n"
24 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
25 "{\n"
26 " // This magically solves some artifacting errors!\n"
27 " //\n"
28 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
29 "\n"
30 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
31 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
32 "}\n"
33 "\n"
34 "#line 7 0 \n"
35 "\n"
36 "uniform mat4x3 uMdl;\n"
37 "uniform mat4 uPv;\n"
38 "uniform mat4 uPvmPrev;\n"
39 "\n"
40 "out vec2 aUv;\n"
41 "out vec4 aNorm;\n"
42 "out vec3 aCo;\n"
43 "out vec3 aWorldCo;\n"
44 "flat out ivec4 aLights;\n"
45 "\n"
46 "void main()\n"
47 "{\n"
48 " vec3 world_pos0 = uMdl * vec4( a_co, 1.0 );\n"
49 " vec4 vproj0 = uPv * vec4( world_pos0, 1.0 );\n"
50 " vec4 vproj1 = uPvmPrev * vec4( a_co, 1.0 );\n"
51 "\n"
52 " vs_motion_out( vproj0, vproj1 );\n"
53 "\n"
54 " gl_Position = vproj0;\n"
55 "\n"
56 " aUv = a_uv;\n"
57 " aNorm = vec4( mat3(uMdl) * a_norm.xyz, a_norm.w );\n"
58 " aCo = a_co;\n"
59 " aWorldCo = world_pos0;\n"
60 " aLights = a_lights;\n"
61 "}\n"
62 ""},
63 .fs =
64 {
65 .static_src =
66 "uniform sampler2D uTexGarbage;\n"
67 "uniform sampler2D uTexGradients;\n"
68 "uniform vec3 uCamera;\n"
69 "uniform vec4 uColour;\n"
70 "uniform vec3 uBoard0;\n"
71 "uniform vec3 uBoard1;\n"
72 "\n"
73 "in vec2 aUv;\n"
74 "in vec4 aNorm;\n"
75 "in vec3 aCo;\n"
76 "in vec3 aWorldCo;\n"
77 "flat in ivec4 aLights;\n"
78 "\n"
79 "#line 1 1 \n"
80 "// :D\n"
81 "\n"
82 "#line 1 1 \n"
83 "layout (location = 0) out vec4 oColour;\n"
84 "\n"
85 "layout (std140) uniform ub_world_lighting\n"
86 "{\n"
87 " vec4 g_light_colours[3];\n"
88 " vec4 g_light_directions[3];\n"
89 " vec4 g_ambient_colour;\n"
90 "\n"
91 " vec4 g_water_plane;\n"
92 " vec4 g_depth_bounds;\n"
93 " float g_water_fog;\n"
94 " int g_light_count;\n"
95 " int g_light_preview;\n"
96 " int g_shadow_samples;\n"
97 "\n"
98 " vec4 g_point_light_positions[32];\n"
99 " vec4 g_point_light_colours[32];\n"
100 "};\n"
101 "\n"
102 "uniform sampler2D g_world_depth;\n"
103 "\n"
104 "float world_depth_sample( vec3 pos )\n"
105 "{\n"
106 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
107 " return texture( g_world_depth, depth_coord ).r;\n"
108 "}\n"
109 "\n"
110 "float world_water_depth( vec3 pos )\n"
111 "{\n"
112 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
113 " float ref_depth = g_water_plane.y*g_water_plane.w;\n"
114 " return texture( g_world_depth, depth_coord ).g - ref_depth;\n"
115 "}\n"
116 "\n"
117 "float shadow_sample( vec3 vdir )\n"
118 "{\n"
119 " vec3 sample_pos = aWorldCo + vdir;\n"
120 " float height_sample = world_depth_sample( sample_pos );\n"
121 "\n"
122 " float fdelta = height_sample - sample_pos.y;\n"
123 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
124 "}\n"
125 "\n"
126 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
127 "{\n"
128 " vec3 pa = p - a;\n"
129 " vec3 ba = b - a;\n"
130 "\n"
131 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
132 " return length( pa - ba*h );\n"
133 "}\n"
134 "\n"
135 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
136 "{\n"
137 " float dist = pow(fdist*0.0008,1.2);\n"
138 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
139 "}\n"
140 "\n"
141 "\n"
142 "// New lighting model\n"
143 "\n"
144 "vec3 newlight_compute_ambient()\n"
145 "{\n"
146 " return g_ambient_colour.rgb;\n"
147 "}\n"
148 "\n"
149 "float newlight_compute_sun_shadow()\n"
150 "{\n"
151 " if( g_shadow_samples == 0 )\n"
152 " {\n"
153 " return 1.0;\n"
154 " }\n"
155 "\n"
156 " float fspread = g_light_colours[0].w;\n"
157 " vec3 vdir = g_light_directions[0].xyz;\n"
158 " float flength = g_light_directions[0].w;\n"
159 "\n"
160 " float famt = 0.0;\n"
161 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
162 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
163 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
164 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
165 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
166 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
167 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
168 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
169 "\n"
170 " // player shadow\n"
171 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 );\n"
172 " float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );\n"
173 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
174 "\n"
175 " return 1.0 - max( player_shadow*0.8, famt );\n"
176 "}\n"
177 "\n"
178 "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n"
179 "{\n"
180 " vec3 vtotal = g_ambient_colour.rgb;\n"
181 "\n"
182 " for( int i=0; i<g_light_count; i++ )\n"
183 " {\n"
184 " vec3 vcolour = g_light_colours[i].rgb;\n"
185 " vec3 vdir = g_light_directions[i].xyz;\n"
186 "\n"
187 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
188 " vtotal += vcolour*flight;\n"
189 " }\n"
190 "\n"
191 " return vtotal;\n"
192 "}\n"
193 "\n"
194 "vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )\n"
195 "{\n"
196 " vec3 vcolour = g_light_colours[0].rgb;\n"
197 " vec3 vdir = g_light_directions[0].xyz;\n"
198 "\n"
199 " vec3 specdir = reflect( -vdir, wnormal );\n"
200 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
201 " return vcolour*spec*fintensity;\n"
202 "}\n"
203 "\n"
204 "vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, \n"
205 " vec3 light_pos, vec3 light_colour )\n"
206 "{\n"
207 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
208 "\n"
209 " float quadratic = dot(light_delta,light_delta);\n"
210 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
211 " attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );\n"
212 "\n"
213 " return light_colour*attenuation;\n"
214 "}\n"
215 "\n"
216 "#line 4 0 \n"
217 "\n"
218 "vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )\n"
219 "{\n"
220 " // Lighting\n"
221 " vec3 halfview = uCamera - aWorldCo;\n"
222 " float fdist = length(halfview);\n"
223 " halfview /= fdist;\n"
224 "\n"
225 " vec3 total_light = newlight_compute_ambient();\n"
226 " \n"
227 " // Compute world lighting contribution and apply it according to the\n"
228 " // shadow map\n"
229 " //\n"
230 " vec3 world_light = newlight_compute_world_diffuse( wnormal );\n"
231 " world_light += newlight_compute_sun_spec( wnormal, halfview, 0.1 );\n"
232 "\n"
233 " float world_shadow = newlight_compute_sun_shadow();\n"
234 "\n"
235 " total_light += world_light * world_shadow;\n"
236 "\n"
237 " // Compute the other lights that exist in the map, not effected by the sun\n"
238 " // shadow\n"
239 " total_light += newlight_compute_quadratic\n"
240 " ( \n"
241 " wnormal, halfview,\n"
242 " g_point_light_positions[ aLights.x ].xyz,\n"
243 " g_point_light_colours[ aLights.x ].rgb \n"
244 " );\n"
245 " total_light += newlight_compute_quadratic\n"
246 " ( \n"
247 " wnormal, halfview,\n"
248 " g_point_light_positions[ aLights.y ].xyz,\n"
249 " g_point_light_colours[ aLights.y ].rgb \n"
250 " );\n"
251 " total_light += newlight_compute_quadratic\n"
252 " ( \n"
253 " wnormal, halfview,\n"
254 " g_point_light_positions[ aLights.z ].xyz,\n"
255 " g_point_light_colours[ aLights.z ].rgb \n"
256 " );\n"
257 "\n"
258 " return apply_fog( diffuse * total_light, fdist );\n"
259 "}\n"
260 "\n"
261 "#line 15 0 \n"
262 "#line 1 2 \n"
263 "const float k_motion_lerp_amount = 0.01;\n"
264 "\n"
265 "#line 2 0 \n"
266 "\n"
267 "layout (location = 1) out vec2 oMotionVec;\n"
268 "\n"
269 "in vec3 aMotionVec0;\n"
270 "in vec3 aMotionVec1;\n"
271 "\n"
272 "void compute_motion_vectors()\n"
273 "{\n"
274 " // Write motion vectors\n"
275 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
276 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
277 "\n"
278 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
279 "}\n"
280 "\n"
281 "#line 16 0 \n"
282 "\n"
283 "void main()\n"
284 "{\n"
285 " compute_motion_vectors();\n"
286 "\n"
287 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
288 "\n"
289 " // ws modulation\n"
290 " vec4 wgarbage = texture( uTexGarbage, aCo.xz * 0.015 );\n"
291 "\n"
292 " // Creating normal patches\n"
293 " vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;\n"
294 " vec3 qnorm = normalize(floor(aNorm.xyz*4.0+modnorm)*0.25);\n"
295 " qnorm += vec3(0.001,0.0,0.0);\n"
296 "\n"
297 " vec3 tangent0 = normalize(cross(qnorm,vec3(0.0,1.0,0.0)));\n"
298 " vec3 tangent1 = cross(qnorm,tangent0);\n"
299 " vec2 uvdiffuse = vec2( dot(tangent0,aCo), dot(tangent1,aCo) ) * 0.035;\n"
300 " \n"
301 " // Patch local noise\n"
302 " vec4 rgarbage = texture( uTexGarbage, uvdiffuse );\n"
303 "\n"
304 " vfrag = pow(uColour.rgb,vec3(1.0/2.2));\n"
305 " vfrag -= rgarbage.a*0.1;\n"
306 "\n"
307 " if( wgarbage.g < 0.3 )\n"
308 " discard;\n"
309 "\n"
310 " if( g_light_preview == 1 )\n"
311 " {\n"
312 " vfrag = vec3(0.5);\n"
313 " }\n"
314 "\n"
315 " // Lighting\n"
316 "\n"
317 " vfrag = scene_do_lighting( vfrag, qnorm );\n"
318 " oColour = vec4(vfrag, 1.0);\n"
319 "}\n"
320 ""},
321 };
322
323 static GLuint _uniform_scene_route_uMdl;
324 static GLuint _uniform_scene_route_uPv;
325 static GLuint _uniform_scene_route_uPvmPrev;
326 static GLuint _uniform_scene_route_uTexGarbage;
327 static GLuint _uniform_scene_route_uTexGradients;
328 static GLuint _uniform_scene_route_uCamera;
329 static GLuint _uniform_scene_route_uColour;
330 static GLuint _uniform_scene_route_uBoard0;
331 static GLuint _uniform_scene_route_uBoard1;
332 static GLuint _uniform_scene_route_g_world_depth;
333 static void shader_scene_route_uMdl(m4x3f m){
334 glUniformMatrix4x3fv(_uniform_scene_route_uMdl,1,GL_FALSE,(float*)m);
335 }
336 static void shader_scene_route_uPv(m4x4f m){
337 glUniformMatrix4fv(_uniform_scene_route_uPv,1,GL_FALSE,(float*)m);
338 }
339 static void shader_scene_route_uPvmPrev(m4x4f m){
340 glUniformMatrix4fv(_uniform_scene_route_uPvmPrev,1,GL_FALSE,(float*)m);
341 }
342 static void shader_scene_route_uTexGarbage(int i){
343 glUniform1i(_uniform_scene_route_uTexGarbage,i);
344 }
345 static void shader_scene_route_uTexGradients(int i){
346 glUniform1i(_uniform_scene_route_uTexGradients,i);
347 }
348 static void shader_scene_route_uCamera(v3f v){
349 glUniform3fv(_uniform_scene_route_uCamera,1,v);
350 }
351 static void shader_scene_route_uColour(v4f v){
352 glUniform4fv(_uniform_scene_route_uColour,1,v);
353 }
354 static void shader_scene_route_uBoard0(v3f v){
355 glUniform3fv(_uniform_scene_route_uBoard0,1,v);
356 }
357 static void shader_scene_route_uBoard1(v3f v){
358 glUniform3fv(_uniform_scene_route_uBoard1,1,v);
359 }
360 static void shader_scene_route_g_world_depth(int i){
361 glUniform1i(_uniform_scene_route_g_world_depth,i);
362 }
363 static void shader_scene_route_register(void){
364 vg_shader_register( &_shader_scene_route );
365 }
366 static void shader_scene_route_use(void){ glUseProgram(_shader_scene_route.id); }
367 static void shader_scene_route_link(void){
368 _uniform_scene_route_uMdl = glGetUniformLocation( _shader_scene_route.id, "uMdl" );
369 _uniform_scene_route_uPv = glGetUniformLocation( _shader_scene_route.id, "uPv" );
370 _uniform_scene_route_uPvmPrev = glGetUniformLocation( _shader_scene_route.id, "uPvmPrev" );
371 _uniform_scene_route_uTexGarbage = glGetUniformLocation( _shader_scene_route.id, "uTexGarbage" );
372 _uniform_scene_route_uTexGradients = glGetUniformLocation( _shader_scene_route.id, "uTexGradients" );
373 _uniform_scene_route_uCamera = glGetUniformLocation( _shader_scene_route.id, "uCamera" );
374 _uniform_scene_route_uColour = glGetUniformLocation( _shader_scene_route.id, "uColour" );
375 _uniform_scene_route_uBoard0 = glGetUniformLocation( _shader_scene_route.id, "uBoard0" );
376 _uniform_scene_route_uBoard1 = glGetUniformLocation( _shader_scene_route.id, "uBoard1" );
377 _uniform_scene_route_g_world_depth = glGetUniformLocation( _shader_scene_route.id, "g_world_depth" );
378 }
379 #endif /* SHADER_scene_route_H */