now we're doing a bunch of them
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / scene_terrain.h
1 #ifndef SHADER_scene_terrain_H
2 #define SHADER_scene_terrain_H
3 static void shader_scene_terrain_link(void);
4 static void shader_scene_terrain_register(void);
5 static struct vg_shader _shader_scene_terrain = {
6 .name = "scene_terrain",
7 .link = shader_scene_terrain_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 uSandColour;\n"
79 "uniform vec2 uBlendOffset;\n"
80 "uniform vec3 uBoard0;\n"
81 "uniform vec3 uBoard1;\n"
82 "\n"
83 "#line 1 1 \n"
84 "// :D\n"
85 "\n"
86 "in vec2 aUv;\n"
87 "in vec4 aNorm;\n"
88 "in vec3 aCo;\n"
89 "in vec3 aWorldCo;\n"
90 "flat in vec4 light_colours[3];\n"
91 "flat in vec4 light_positions[3];\n"
92 "\n"
93 "#line 1 1 \n"
94 "layout (location = 0) out vec4 oColour;\n"
95 "\n"
96 "layout (std140) uniform ub_world_lighting\n"
97 "{\n"
98 " vec4 g_light_colours[3];\n"
99 " vec4 g_light_directions[3];\n"
100 " vec4 g_ambient_colour;\n"
101 "\n"
102 " vec4 g_water_plane;\n"
103 " vec4 g_depth_bounds;\n"
104 " float g_water_fog;\n"
105 " int g_light_count;\n"
106 " int g_light_preview;\n"
107 " int g_shadow_samples;\n"
108 "\n"
109 " // g_time ?\n"
110 "\n"
111 " //vec4 g_point_light_positions[32];\n"
112 " //vec4 g_point_light_colours[32];\n"
113 "};\n"
114 "\n"
115 "uniform sampler2D g_world_depth;\n"
116 "\n"
117 "float world_depth_sample( vec3 pos )\n"
118 "{\n"
119 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
120 " return texture( g_world_depth, depth_coord ).r;\n"
121 "}\n"
122 "\n"
123 "float world_water_depth( vec3 pos )\n"
124 "{\n"
125 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
126 " float ref_depth = g_water_plane.y*g_water_plane.w;\n"
127 " return texture( g_world_depth, depth_coord ).g - ref_depth;\n"
128 "}\n"
129 "\n"
130 "float shadow_sample( vec3 vdir )\n"
131 "{\n"
132 " vec3 sample_pos = aWorldCo + vdir;\n"
133 " float height_sample = world_depth_sample( sample_pos );\n"
134 "\n"
135 " float fdelta = height_sample - sample_pos.y;\n"
136 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
137 "}\n"
138 "\n"
139 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
140 "{\n"
141 " float dist = pow(fdist*0.0008,1.2);\n"
142 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
143 "}\n"
144 "\n"
145 "\n"
146 "// New lighting model\n"
147 "\n"
148 "vec3 newlight_compute_ambient()\n"
149 "{\n"
150 " return g_ambient_colour.rgb;\n"
151 "}\n"
152 "\n"
153 "float newlight_compute_sun_shadow()\n"
154 "{\n"
155 " if( g_shadow_samples == 0 )\n"
156 " {\n"
157 " return 1.0;\n"
158 " }\n"
159 "\n"
160 " float fspread = g_light_colours[0].w;\n"
161 " vec3 vdir = g_light_directions[0].xyz;\n"
162 " float flength = g_light_directions[0].w;\n"
163 "\n"
164 " float famt = 0.0;\n"
165 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
166 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
167 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
168 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
169 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
170 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
171 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
172 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
173 "\n"
174 " return 1.0 - 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 11 0 \n"
216 "\n"
217 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
218 "{\n"
219 " vec3 pa = p - a;\n"
220 " vec3 ba = b - a;\n"
221 "\n"
222 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
223 " return length( pa - ba*h );\n"
224 "}\n"
225 "\n"
226 "float compute_board_shadow()\n"
227 "{\n"
228 " // player shadow\n"
229 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 );\n"
230 " float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );\n"
231 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
232 "\n"
233 " return 1.0 - player_shadow*0.8;\n"
234 "}\n"
235 "\n"
236 "vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )\n"
237 "{\n"
238 " // Lighting\n"
239 " vec3 halfview = uCamera - aWorldCo;\n"
240 " float fdist = length(halfview);\n"
241 " halfview /= fdist;\n"
242 "\n"
243 " vec3 total_light = newlight_compute_ambient();\n"
244 " \n"
245 " // Compute world lighting contribution and apply it according to the\n"
246 " // shadow map\n"
247 " //\n"
248 " vec3 world_light = newlight_compute_world_diffuse( wnormal );\n"
249 " world_light += newlight_compute_sun_spec( wnormal, halfview, 0.1 );\n"
250 "\n"
251 " float world_shadow = newlight_compute_sun_shadow();\n"
252 " float board_shadow = compute_board_shadow();\n"
253 "\n"
254 " total_light += world_light * min( board_shadow, world_shadow );\n"
255 "\n"
256 " // Compute the other lights that exist in the map, not effected by the sun\n"
257 " // shadow\n"
258 "\n"
259 " total_light += newlight_compute_quadratic\n"
260 " ( \n"
261 " wnormal, halfview,\n"
262 " light_positions[0].xyz,\n"
263 " light_colours[0].rgb \n"
264 " ) * board_shadow;\n"
265 " total_light += newlight_compute_quadratic\n"
266 " ( \n"
267 " wnormal, halfview,\n"
268 " light_positions[1].xyz,\n"
269 " light_colours[1].rgb \n"
270 " ) * board_shadow;\n"
271 " total_light += newlight_compute_quadratic\n"
272 " ( \n"
273 " wnormal, halfview,\n"
274 " light_positions[2].xyz,\n"
275 " light_colours[2].rgb \n"
276 " ) * board_shadow;\n"
277 "\n"
278 " return apply_fog( diffuse * total_light, fdist );\n"
279 "}\n"
280 "\n"
281 "#line 10 0 \n"
282 "#line 1 2 \n"
283 "const float k_motion_lerp_amount = 0.01;\n"
284 "\n"
285 "#line 2 0 \n"
286 "\n"
287 "layout (location = 1) out vec2 oMotionVec;\n"
288 "\n"
289 "in vec3 aMotionVec0;\n"
290 "in vec3 aMotionVec1;\n"
291 "\n"
292 "void compute_motion_vectors()\n"
293 "{\n"
294 " // Write motion vectors\n"
295 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
296 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
297 "\n"
298 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
299 "}\n"
300 "\n"
301 "#line 11 0 \n"
302 "\n"
303 "void main()\n"
304 "{\n"
305 " compute_motion_vectors();\n"
306 "\n"
307 " // Colour\n"
308 " // ------\n"
309 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
310 "\n"
311 " // ws modulation\n"
312 " vec4 wgarbage = texture( uTexGarbage, aCo.xz * 0.015 );\n"
313 " \n"
314 " // Creating normal patches\n"
315 " vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;\n"
316 " vec3 qnorm = normalize(floor(aNorm.xyz*4.0+modnorm)*0.25);\n"
317 " qnorm += vec3(0.001,0.0,0.0);\n"
318 "\n"
319 " vec2 dir = normalize(qnorm.xz);\n"
320 " vec2 uvdiffuse = aCo.xz * 0.02;\n"
321 " uvdiffuse = mat2(dir.y, dir.x, -dir.x, dir.y) * uvdiffuse;\n"
322 " \n"
323 " // Patch local noise\n"
324 " vec4 rgarbage = texture( uTexGarbage, uvdiffuse );\n"
325 "\n"
326 " // Colour blending\n"
327 " float amtgrass = step(qnorm.y,0.6);\n"
328 " float amtsand = min(max((aCo.y - 10.0) * -0.1,0.0)*qnorm.y,1.0);\n"
329 " vec2 uvgradients = aUv + vec2( amtgrass + rgarbage.a*0.8 )*uBlendOffset;\n"
330 " vfrag = texture( uTexGradients, uvgradients ).rgb;\n"
331 " vfrag = mix( vfrag, uSandColour, amtsand );\n"
332 "\n"
333 " qnorm = mix( qnorm, aNorm.xyz, amtsand );\n"
334 " \n"
335 " if( g_light_preview == 1 )\n"
336 " {\n"
337 " vfrag = vec3(0.5);\n"
338 " }\n"
339 "\n"
340 " vfrag = scene_do_lighting( vfrag, qnorm );\n"
341 " oColour = vec4(vfrag, 1.0);\n"
342 "}\n"
343 ""},
344 };
345
346 static GLuint _uniform_scene_terrain_uMdl;
347 static GLuint _uniform_scene_terrain_uPv;
348 static GLuint _uniform_scene_terrain_uPvmPrev;
349 static GLuint _uniform_scene_terrain_uLightsArray;
350 static GLuint _uniform_scene_terrain_uTexGarbage;
351 static GLuint _uniform_scene_terrain_uTexGradients;
352 static GLuint _uniform_scene_terrain_uCamera;
353 static GLuint _uniform_scene_terrain_uSandColour;
354 static GLuint _uniform_scene_terrain_uBlendOffset;
355 static GLuint _uniform_scene_terrain_uBoard0;
356 static GLuint _uniform_scene_terrain_uBoard1;
357 static GLuint _uniform_scene_terrain_g_world_depth;
358 static void shader_scene_terrain_uMdl(m4x3f m){
359 glUniformMatrix4x3fv(_uniform_scene_terrain_uMdl,1,GL_FALSE,(float*)m);
360 }
361 static void shader_scene_terrain_uPv(m4x4f m){
362 glUniformMatrix4fv(_uniform_scene_terrain_uPv,1,GL_FALSE,(float*)m);
363 }
364 static void shader_scene_terrain_uPvmPrev(m4x4f m){
365 glUniformMatrix4fv(_uniform_scene_terrain_uPvmPrev,1,GL_FALSE,(float*)m);
366 }
367 static void shader_scene_terrain_uTexGarbage(int i){
368 glUniform1i(_uniform_scene_terrain_uTexGarbage,i);
369 }
370 static void shader_scene_terrain_uTexGradients(int i){
371 glUniform1i(_uniform_scene_terrain_uTexGradients,i);
372 }
373 static void shader_scene_terrain_uCamera(v3f v){
374 glUniform3fv(_uniform_scene_terrain_uCamera,1,v);
375 }
376 static void shader_scene_terrain_uSandColour(v3f v){
377 glUniform3fv(_uniform_scene_terrain_uSandColour,1,v);
378 }
379 static void shader_scene_terrain_uBlendOffset(v2f v){
380 glUniform2fv(_uniform_scene_terrain_uBlendOffset,1,v);
381 }
382 static void shader_scene_terrain_uBoard0(v3f v){
383 glUniform3fv(_uniform_scene_terrain_uBoard0,1,v);
384 }
385 static void shader_scene_terrain_uBoard1(v3f v){
386 glUniform3fv(_uniform_scene_terrain_uBoard1,1,v);
387 }
388 static void shader_scene_terrain_g_world_depth(int i){
389 glUniform1i(_uniform_scene_terrain_g_world_depth,i);
390 }
391 static void shader_scene_terrain_register(void){
392 vg_shader_register( &_shader_scene_terrain );
393 }
394 static void shader_scene_terrain_use(void){ glUseProgram(_shader_scene_terrain.id); }
395 static void shader_scene_terrain_link(void){
396 _uniform_scene_terrain_uMdl = glGetUniformLocation( _shader_scene_terrain.id, "uMdl" );
397 _uniform_scene_terrain_uPv = glGetUniformLocation( _shader_scene_terrain.id, "uPv" );
398 _uniform_scene_terrain_uPvmPrev = glGetUniformLocation( _shader_scene_terrain.id, "uPvmPrev" );
399 _uniform_scene_terrain_uLightsArray = glGetUniformLocation( _shader_scene_terrain.id, "uLightsArray" );
400 _uniform_scene_terrain_uTexGarbage = glGetUniformLocation( _shader_scene_terrain.id, "uTexGarbage" );
401 _uniform_scene_terrain_uTexGradients = glGetUniformLocation( _shader_scene_terrain.id, "uTexGradients" );
402 _uniform_scene_terrain_uCamera = glGetUniformLocation( _shader_scene_terrain.id, "uCamera" );
403 _uniform_scene_terrain_uSandColour = glGetUniformLocation( _shader_scene_terrain.id, "uSandColour" );
404 _uniform_scene_terrain_uBlendOffset = glGetUniformLocation( _shader_scene_terrain.id, "uBlendOffset" );
405 _uniform_scene_terrain_uBoard0 = glGetUniformLocation( _shader_scene_terrain.id, "uBoard0" );
406 _uniform_scene_terrain_uBoard1 = glGetUniformLocation( _shader_scene_terrain.id, "uBoard1" );
407 _uniform_scene_terrain_g_world_depth = glGetUniformLocation( _shader_scene_terrain.id, "g_world_depth" );
408 }
409 #endif /* SHADER_scene_terrain_H */