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