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