needs a lot of cleaning but lights are OK
[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 .orig_file = "shaders/scene.vs",
11 .static_src =
12 "layout (location=0) in vec3 a_co;\n"
13 "layout (location=1) in vec4 a_norm;\n"
14 "layout (location=2) in vec2 a_uv;\n"
15 "layout (location=3) in ivec4 a_lights;\n"
16 "\n"
17 "#line 1 1 \n"
18 "const float k_motion_lerp_amount = 0.01;\n"
19 "\n"
20 "#line 2 0 \n"
21 "\n"
22 "out vec3 aMotionVec0;\n"
23 "out vec3 aMotionVec1;\n"
24 "\n"
25 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
26 "{\n"
27 " // This magically solves some artifacting errors!\n"
28 " //\n"
29 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
30 "\n"
31 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
32 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
33 "}\n"
34 "\n"
35 "#line 7 0 \n"
36 "\n"
37 "uniform mat4x3 uMdl;\n"
38 "uniform mat4 uPv;\n"
39 "uniform mat4 uPvmPrev;\n"
40 "\n"
41 "out vec2 aUv;\n"
42 "out vec4 aNorm;\n"
43 "out vec3 aCo;\n"
44 "out vec3 aWorldCo;\n"
45 "\n"
46 "flat out ivec4 light_indices;\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 " light_indices = a_lights;\n"
64 "}\n"
65 ""},
66 .fs =
67 {
68 .orig_file = "shaders/scene_terrain.fs",
69 .static_src =
70 "uniform sampler2D uTexGarbage;\n"
71 "uniform sampler2D uTexGradients;\n"
72 "uniform vec3 uCamera;\n"
73 "uniform vec3 uSandColour;\n"
74 "uniform vec2 uBlendOffset;\n"
75 "uniform vec3 uBoard0;\n"
76 "uniform vec3 uBoard1;\n"
77 "\n"
78 "#line 1 1 \n"
79 "// :D\n"
80 "\n"
81 "in vec2 aUv;\n"
82 "in vec4 aNorm;\n"
83 "in vec3 aCo;\n"
84 "in vec3 aWorldCo;\n"
85 "flat in ivec4 light_indices;\n"
86 "\n"
87 "uniform samplerBuffer uLightsArray;\n"
88 "\n"
89 "#line 1 1 \n"
90 "layout (location = 0) out vec4 oColour;\n"
91 "\n"
92 "layout (std140) uniform ub_world_lighting\n"
93 "{\n"
94 " vec4 g_light_colours[3];\n"
95 " vec4 g_light_directions[3];\n"
96 " vec4 g_ambient_colour;\n"
97 "\n"
98 " vec4 g_water_plane;\n"
99 " vec4 g_depth_bounds;\n"
100 " float g_water_fog;\n"
101 " float g_time;\n"
102 " int g_light_count;\n"
103 " int g_light_preview;\n"
104 " int g_shadow_samples;\n"
105 "\n"
106 " // g_time ?\n"
107 "\n"
108 " //vec4 g_point_light_positions[32];\n"
109 " //vec4 g_point_light_colours[32];\n"
110 "};\n"
111 "\n"
112 "uniform sampler2D g_world_depth;\n"
113 "\n"
114 "float world_depth_sample( vec3 pos )\n"
115 "{\n"
116 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
117 " return texture( g_world_depth, depth_coord ).r;\n"
118 "}\n"
119 "\n"
120 "float world_water_depth( vec3 pos )\n"
121 "{\n"
122 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
123 " float ref_depth = g_water_plane.y*g_water_plane.w;\n"
124 " return texture( g_world_depth, depth_coord ).g - ref_depth;\n"
125 "}\n"
126 "\n"
127 "float shadow_sample( vec3 vdir )\n"
128 "{\n"
129 " vec3 sample_pos = aWorldCo + vdir;\n"
130 " float height_sample = world_depth_sample( sample_pos );\n"
131 "\n"
132 " float fdelta = height_sample - sample_pos.y;\n"
133 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
134 "}\n"
135 "\n"
136 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
137 "{\n"
138 " float dist = pow(fdist*0.0008,1.2);\n"
139 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
140 "}\n"
141 "\n"
142 "\n"
143 "// New lighting model\n"
144 "\n"
145 "vec3 newlight_compute_ambient()\n"
146 "{\n"
147 " return g_ambient_colour.rgb;\n"
148 "}\n"
149 "\n"
150 "float newlight_compute_sun_shadow( vec3 dir )\n"
151 "{\n"
152 " if( g_shadow_samples == 0 )\n"
153 " {\n"
154 " return 1.0;\n"
155 " }\n"
156 "\n"
157 " float fspread = g_light_colours[0].w;\n"
158 " vec3 vdir = dir;\n"
159 " float flength = g_light_directions[0].w;\n"
160 "\n"
161 " float famt = 0.0;\n"
162 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
163 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
164 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
165 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
166 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
167 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
168 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
169 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
170 "\n"
171 " return 1.0 - famt;\n"
172 "}\n"
173 "\n"
174 "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n"
175 "{\n"
176 " vec3 vtotal = g_ambient_colour.rgb;\n"
177 "\n"
178 " for( int i=0; i<g_light_count; i++ )\n"
179 " {\n"
180 " vec3 vcolour = g_light_colours[i].rgb;\n"
181 " vec3 vdir = g_light_directions[i].xyz;\n"
182 "\n"
183 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
184 " vtotal += vcolour*flight;\n"
185 " }\n"
186 "\n"
187 " return vtotal;\n"
188 "}\n"
189 "\n"
190 "vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )\n"
191 "{\n"
192 " vec3 vcolour = g_light_colours[0].rgb;\n"
193 " vec3 vdir = g_light_directions[0].xyz;\n"
194 "\n"
195 " vec3 specdir = reflect( -vdir, wnormal );\n"
196 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
197 " return vcolour*spec*fintensity;\n"
198 "}\n"
199 "\n"
200 "float newlight_specular( vec3 wnormal, vec3 dir, vec3 halfview, float exponent )\n"
201 "{\n"
202 " vec3 specdir = reflect( -dir, wnormal );\n"
203 " return pow(max(dot( halfview, specdir ), 0.0), exponent);\n"
204 "}\n"
205 "\n"
206 "vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, \n"
207 " vec3 light_colour, vec3 light_pos )\n"
208 "{\n"
209 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
210 "\n"
211 " float quadratic = dot(light_delta,light_delta);\n"
212 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
213 " attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );\n"
214 "\n"
215 " return light_colour*attenuation;\n"
216 "}\n"
217 "\n"
218 "vec3 newlight_compute_spot( vec3 wnormal, vec3 halfview, \n"
219 " vec3 light_colour, vec3 light_pos,\n"
220 " vec4 light_dir )\n"
221 "{\n"
222 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
223 "\n"
224 " float quadratic = dot(light_delta,light_delta);\n"
225 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
226 "\n"
227 " light_delta = normalize( light_delta );\n"
228 " attenuation *= max( 0.0, dot( light_delta, wnormal ) );\n"
229 "\n"
230 " float spot_theta = max( 0.0, dot( light_delta, -light_dir.xyz ) ),\n"
231 " falloff = max( 0.0,( spot_theta - light_dir.w ) / (1.0-light_dir.w) );\n"
232 "\n"
233 " return light_colour*attenuation*falloff;\n"
234 "}\n"
235 "\n"
236 "#line 12 0 \n"
237 "#line 1 2 \n"
238 "const vec3 DAYSKY_COLOUR = vec3( 0.37, 0.54, 0.97 );\n"
239 "const vec3 NIGHTSKY_COLOUR = vec3( 0.03, 0.05, 0.20 );\n"
240 "const vec3 SUNSET_COLOUR = vec3( 1.00, 0.32, 0.01 );\n"
241 "const vec3 AMBIENT_COLOUR = vec3( 0.13, 0.17, 0.35 );\n"
242 "const vec3 SUNSET_AMBIENT = vec3( 0.25, 0.17, 0.51 );\n"
243 "const vec3 SUN_COLOUR = vec3( 1.10, 0.89, 0.35 ) * 1.125;\n"
244 "\n"
245 "const float SUN_ANGLE = 0.0001;\n"
246 "const float TIME_RATE = 0.025;\n"
247 "\n"
248 "const float PI = 3.14159265;\n"
249 "\n"
250 "struct world_info\n"
251 "{\n"
252 " float time,\n"
253 " time_of_day,\n"
254 " day_phase,\n"
255 " sunset_phase;\n"
256 " \n"
257 " vec3 sun_dir;\n"
258 "};\n"
259 "\n"
260 "float luminance( vec3 v )\n"
261 "{\n"
262 " return dot( v, vec3(0.2126, 0.7152, 0.0722) );\n"
263 "}\n"
264 "\n"
265 "vec3 scene_ambient( vec3 dir, const world_info w )\n"
266 "{\n"
267 " float sun_azimuth = dot( dir.xz, w.sun_dir.xz ) * 0.4 + 0.6;\n"
268 " float sky_gradient = dir.y;\n"
269 " \n"
270 " /* Blend phase colours */\n"
271 " vec3 ambient = DAYSKY_COLOUR * (w.day_phase-w.sunset_phase*0.1);\n"
272 " ambient += SUNSET_COLOUR * (1.0-dir.y*0.5) * w.sunset_phase * sun_azimuth;\n"
273 " ambient += NIGHTSKY_COLOUR * (1.0-w.day_phase);\n"
274 " \n"
275 " /* Add gradient */\n"
276 " ambient -= sky_gradient * luminance(ambient);\n"
277 " \n"
278 " return ambient;\n"
279 "}\n"
280 "\n"
281 "vec3 scene_sky( vec3 ray_dir, const world_info w )\n"
282 "{\n"
283 " ray_dir.y = abs( ray_dir.y );\n"
284 " vec3 sky_colour = scene_ambient( ray_dir, w );\n"
285 " \n"
286 " /* Sun */\n"
287 " float sun_theta = dot( ray_dir, w.sun_dir );\n"
288 " float sun_size = max( 0.0, sun_theta * 0.5 + 0.5 + SUN_ANGLE );\n"
289 " float sun_shape = pow( sun_size, 2000.0 );\n"
290 " sun_shape += sun_size * max(w.sun_dir.y,0.0) * 0.5;\n"
291 " \n"
292 " vec3 sun_colour = mix( vec3(1.0), SUNSET_COLOUR, w.sunset_phase*0.5 );\n"
293 " sun_colour *= sun_shape;\n"
294 " \n"
295 " vec3 composite = sky_colour + sun_colour;\n"
296 " return composite;\n"
297 "}\n"
298 "\n"
299 "vec3 scene_compute_ambient( vec3 normal, const world_info w )\n"
300 "{\n"
301 " return scene_ambient( (normal * vec3(1.0,-1.0,1.0)) * 0.5 + 0.5, w );\n"
302 "}\n"
303 "\n"
304 "vec3 SR_LIGHT( vec3 normal, vec2 dir, vec3 colour )\n"
305 "{\n"
306 " vec3 dir3 = vec3\n"
307 " (\n"
308 " cos(dir.y) * cos(dir.x),\n"
309 " sin(dir.x),\n"
310 " sin(dir.y) * cos(dir.x)\n"
311 " );\n"
312 "\n"
313 " float flight = max( dot( normal, dir3 ) * 0.75 + 0.25, 0.0 );\n"
314 " \n"
315 " return flight * colour;\n"
316 "}\n"
317 "\n"
318 "vec3 scene_lighting_old( vec3 normal, const world_info w )\n"
319 "{\n"
320 " vec3 SR_COLOUR_SUN = vec3( 1.36, 1.35, 1.01 );\n"
321 " vec3 SR_COLOUR_FILL = vec3( 0.33, 0.56, 0.64 );\n"
322 " vec3 SR_COLOUR_RIM = vec3( 0.05, 0.05, 0.23 );\n"
323 " \n"
324 " return SR_LIGHT( normal, vec2( 0.63, -0.08 ), SR_COLOUR_SUN ) +\n"
325 " SR_LIGHT( normal, vec2( -2.60, -0.13 ), SR_COLOUR_FILL ) + \n"
326 " SR_LIGHT( normal, vec2( 2.60, -0.84 ), SR_COLOUR_RIM ) ;\n"
327 "}\n"
328 "\n"
329 "vec3 scene_lighting( vec3 normal, float shadow, vec3 halfview, const world_info w )\n"
330 "{\n"
331 " float fresnel = 1.0 - abs(dot(normal,halfview));\n"
332 "\n"
333 " vec3 sky_reflection = 0.5 * fresnel * mix( DAYSKY_COLOUR, SUNSET_COLOUR, w.sunset_phase );\n"
334 " vec3 light_sun = max(0.0,dot(normal,w.sun_dir)*0.75+0.25) * SUN_COLOUR \n"
335 " * w.day_phase;\n"
336 "\n"
337 " float scaled_shadow = max( shadow, 1.0 - max(w.sun_dir.y,0.0) );\n"
338 "\n"
339 " vec3 ambient = mix( AMBIENT_COLOUR, SUNSET_AMBIENT, w.sunset_phase );\n"
340 "\n"
341 " return ambient + (light_sun + sky_reflection) * shadow;\n"
342 "\n"
343 "\n"
344 "\n"
345 "\n"
346 "\n"
347 "\n"
348 " float sun_theta = dot( normal, w.sun_dir );\n"
349 "\n"
350 " float softness_min = 0.5;\n"
351 " float softness = softness_min + w.sunset_phase * (1.0-softness_min);\n"
352 " float light_min = 0.0 * w.day_phase;\n"
353 " float light_direct = light_min + smoothstep( -softness, softness, sun_theta ) * (1.0-light_min);\n"
354 " light_direct *= clamp(w.sun_dir.y * 4.0 + 1.0,0.0,1.0) * shadow;\n"
355 " \n"
356 " float light_bounce = 0.5 + 0.5 * dot( -normal, w.sun_dir );\n"
357 " light_bounce *= light_bounce * max( w.sun_dir.y, 0.0 );\n"
358 " \n"
359 " vec3 light_colour = SUN_COLOUR*w.day_phase + (SUNSET_COLOUR*w.sunset_phase + 0.1);\n"
360 " vec3 dark_colour = mix( AMBIENT_COLOUR, SUNSET_AMBIENT, w.sunset_phase );\n"
361 " \n"
362 " float spec = newlight_specular( normal, w.sun_dir, halfview, 2.0 ) \n"
363 " * 0.2 * shadow * w.day_phase;\n"
364 " \n"
365 " return mix(dark_colour, light_colour, light_direct) + \n"
366 " spec +\n"
367 " dark_colour * light_bounce;\n"
368 "}\n"
369 "\n"
370 "void scene_state( float world_time, out world_info w )\n"
371 "{\n"
372 " w.time = world_time;\n"
373 " w.time_of_day = fract( w.time );\n"
374 " w.day_phase = cos( w.time_of_day * PI * 2.0 ) * 0.5 + 0.5;\n"
375 " w.sunset_phase = cos( w.time_of_day * PI * 4.0 + PI ) * 0.5 + 0.5;\n"
376 " w.sunset_phase = pow( w.sunset_phase, 6.0 );\n"
377 " \n"
378 " float a = w.time_of_day * PI * 2.0;\n"
379 " w.sun_dir = normalize( vec3( sin( a ), cos( a ), 0.2) );\n"
380 "}\n"
381 "\n"
382 "\n"
383 "#line 13 0 \n"
384 "\n"
385 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
386 "{\n"
387 " vec3 pa = p - a;\n"
388 " vec3 ba = b - a;\n"
389 "\n"
390 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
391 " return length( pa - ba*h );\n"
392 "}\n"
393 "\n"
394 "float compute_board_shadow()\n"
395 "{\n"
396 " // player shadow\n"
397 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 );\n"
398 " float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );\n"
399 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
400 "\n"
401 " return 1.0 - player_shadow*0.8;\n"
402 "}\n"
403 "\n"
404 "vec3 scene_apply_fog( vec3 vfrag, vec3 colour, float fdist )\n"
405 "{\n"
406 " float dist = pow(fdist*0.0010,0.78);\n"
407 " return mix( vfrag, colour, min( 1.0, dist ) );\n"
408 "}\n"
409 "\n"
410 "vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )\n"
411 "{\n"
412 " world_info world;\n"
413 " scene_state( g_time, world );\n"
414 "\n"
415 " // Lighting\n"
416 " vec3 halfview = uCamera - aWorldCo;\n"
417 " float fdist = length(halfview);\n"
418 " halfview /= fdist;\n"
419 "\n"
420 " vec3 total_light = vec3(0.0);\n"
421 "\n"
422 "\n"
423 " float world_shadow = newlight_compute_sun_shadow( world.sun_dir \n"
424 " * (1.0/(max(world.sun_dir.y,0.0)+0.2)) );\n"
425 " float board_shadow = compute_board_shadow();\n"
426 "\n"
427 " total_light += scene_lighting( wnormal, min( board_shadow, world_shadow ), \n"
428 " halfview, world );\n"
429 "\n"
430 " //total_light += scene_lighting_old( wnormal, world );\n"
431 "\n"
432 " // Compute the other lights that exist in the map, not effected by the sun\n"
433 " // shadow\n"
434 "\n"
435 " // read lights\n"
436 " vec4 light_colour_0 = texelFetch( uLightsArray, light_indices.x*3+0 );\n"
437 " vec4 light_colour_1 = texelFetch( uLightsArray, light_indices.y*3+0 );\n"
438 " vec4 light_colour_2 = texelFetch( uLightsArray, light_indices.z*3+0 );\n"
439 " vec4 light_co_0 = texelFetch( uLightsArray, light_indices.x*3+1 );\n"
440 " vec4 light_co_1 = texelFetch( uLightsArray, light_indices.y*3+1 );\n"
441 " vec4 light_co_2 = texelFetch( uLightsArray, light_indices.z*3+1 );\n"
442 " vec4 light_dir_0 = texelFetch( uLightsArray, light_indices.x*3+2 );\n"
443 " vec4 light_dir_1 = texelFetch( uLightsArray, light_indices.y*3+2 );\n"
444 " vec4 light_dir_2 = texelFetch( uLightsArray, light_indices.z*3+2 );\n"
445 "\n"
446 " //return vec3(fract(distance(light_co_0.xyz,aWorldCo)),\n"
447 " // fract(distance(light_co_1.xyz,aWorldCo)),\n"
448 " // fract(distance(light_co_2.xyz,aWorldCo)));\n"
449 "\n"
450 " // return vec3(fract(light_indices.x * 0.125), fract(light_indices.y*0.125),\n"
451 " // fract(light_indices.z * 0.125 ));\n"
452 "\n"
453 " total_light += newlight_compute_spot\n"
454 " ( \n"
455 " wnormal, halfview,\n"
456 " light_colour_0.rgb,\n"
457 " light_co_0.xyz,\n"
458 " light_dir_0\n"
459 " ) * board_shadow;\n"
460 "\n"
461 " total_light += newlight_compute_spot\n"
462 " ( \n"
463 " wnormal, halfview,\n"
464 " light_colour_1.rgb,\n"
465 " light_co_1.xyz,\n"
466 " light_dir_1\n"
467 " ) * board_shadow;\n"
468 " total_light += newlight_compute_spot\n"
469 " ( \n"
470 " wnormal, halfview,\n"
471 " light_colour_2.rgb,\n"
472 " light_co_2.xyz,\n"
473 " light_dir_2\n"
474 " ) * board_shadow;\n"
475 "\n"
476 " vec3 fog_colour = scene_sky( -halfview, world );\n"
477 " \n"
478 " return scene_apply_fog( diffuse * total_light, fog_colour, fdist );\n"
479 "}\n"
480 "\n"
481 "#line 10 0 \n"
482 "#line 1 2 \n"
483 "const float k_motion_lerp_amount = 0.01;\n"
484 "\n"
485 "#line 2 0 \n"
486 "\n"
487 "layout (location = 1) out vec2 oMotionVec;\n"
488 "\n"
489 "in vec3 aMotionVec0;\n"
490 "in vec3 aMotionVec1;\n"
491 "\n"
492 "void compute_motion_vectors()\n"
493 "{\n"
494 " // Write motion vectors\n"
495 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
496 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
497 "\n"
498 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
499 "}\n"
500 "\n"
501 "#line 11 0 \n"
502 "\n"
503 "void main()\n"
504 "{\n"
505 " compute_motion_vectors();\n"
506 "\n"
507 " // Colour\n"
508 " // ------\n"
509 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
510 "\n"
511 " // ws modulation\n"
512 " vec4 wgarbage = texture( uTexGarbage, aCo.xz * 0.015 );\n"
513 " \n"
514 " // Creating normal patches\n"
515 " vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;\n"
516 " vec3 qnorm = normalize(floor(aNorm.xyz*4.0+modnorm)*0.25);\n"
517 " qnorm += vec3(0.001,0.0,0.0);\n"
518 "\n"
519 " vec2 dir = normalize(qnorm.xz);\n"
520 " vec2 uvdiffuse = aCo.xz * 0.02;\n"
521 " uvdiffuse = mat2(dir.y, dir.x, -dir.x, dir.y) * uvdiffuse;\n"
522 " \n"
523 " // Patch local noise\n"
524 " vec4 rgarbage = texture( uTexGarbage, uvdiffuse );\n"
525 "\n"
526 " // Colour blending\n"
527 " float amtgrass = step(qnorm.y,0.6);\n"
528 " float amtsand = min(max((aCo.y - 10.0) * -0.1,0.0)*qnorm.y,1.0);\n"
529 " vec2 uvgradients = aUv + vec2( amtgrass + rgarbage.a*0.8 )*uBlendOffset;\n"
530 " vfrag = texture( uTexGradients, uvgradients ).rgb;\n"
531 " vfrag = mix( vfrag, uSandColour, amtsand );\n"
532 "\n"
533 " qnorm = mix( qnorm, aNorm.xyz, amtsand );\n"
534 " \n"
535 " if( g_light_preview == 1 )\n"
536 " {\n"
537 " vfrag = vec3(0.5);\n"
538 " }\n"
539 "\n"
540 " vfrag = scene_do_lighting( vfrag, qnorm );\n"
541 " oColour = vec4(vfrag, 1.0);\n"
542 "}\n"
543 ""},
544 };
545
546 static GLuint _uniform_scene_terrain_uMdl;
547 static GLuint _uniform_scene_terrain_uPv;
548 static GLuint _uniform_scene_terrain_uPvmPrev;
549 static GLuint _uniform_scene_terrain_uTexGarbage;
550 static GLuint _uniform_scene_terrain_uTexGradients;
551 static GLuint _uniform_scene_terrain_uCamera;
552 static GLuint _uniform_scene_terrain_uSandColour;
553 static GLuint _uniform_scene_terrain_uBlendOffset;
554 static GLuint _uniform_scene_terrain_uBoard0;
555 static GLuint _uniform_scene_terrain_uBoard1;
556 static GLuint _uniform_scene_terrain_uLightsArray;
557 static GLuint _uniform_scene_terrain_g_world_depth;
558 static void shader_scene_terrain_uMdl(m4x3f m){
559 glUniformMatrix4x3fv(_uniform_scene_terrain_uMdl,1,GL_FALSE,(float*)m);
560 }
561 static void shader_scene_terrain_uPv(m4x4f m){
562 glUniformMatrix4fv(_uniform_scene_terrain_uPv,1,GL_FALSE,(float*)m);
563 }
564 static void shader_scene_terrain_uPvmPrev(m4x4f m){
565 glUniformMatrix4fv(_uniform_scene_terrain_uPvmPrev,1,GL_FALSE,(float*)m);
566 }
567 static void shader_scene_terrain_uTexGarbage(int i){
568 glUniform1i(_uniform_scene_terrain_uTexGarbage,i);
569 }
570 static void shader_scene_terrain_uTexGradients(int i){
571 glUniform1i(_uniform_scene_terrain_uTexGradients,i);
572 }
573 static void shader_scene_terrain_uCamera(v3f v){
574 glUniform3fv(_uniform_scene_terrain_uCamera,1,v);
575 }
576 static void shader_scene_terrain_uSandColour(v3f v){
577 glUniform3fv(_uniform_scene_terrain_uSandColour,1,v);
578 }
579 static void shader_scene_terrain_uBlendOffset(v2f v){
580 glUniform2fv(_uniform_scene_terrain_uBlendOffset,1,v);
581 }
582 static void shader_scene_terrain_uBoard0(v3f v){
583 glUniform3fv(_uniform_scene_terrain_uBoard0,1,v);
584 }
585 static void shader_scene_terrain_uBoard1(v3f v){
586 glUniform3fv(_uniform_scene_terrain_uBoard1,1,v);
587 }
588 static void shader_scene_terrain_g_world_depth(int i){
589 glUniform1i(_uniform_scene_terrain_g_world_depth,i);
590 }
591 static void shader_scene_terrain_register(void){
592 vg_shader_register( &_shader_scene_terrain );
593 }
594 static void shader_scene_terrain_use(void){ glUseProgram(_shader_scene_terrain.id); }
595 static void shader_scene_terrain_link(void){
596 _uniform_scene_terrain_uMdl = glGetUniformLocation( _shader_scene_terrain.id, "uMdl" );
597 _uniform_scene_terrain_uPv = glGetUniformLocation( _shader_scene_terrain.id, "uPv" );
598 _uniform_scene_terrain_uPvmPrev = glGetUniformLocation( _shader_scene_terrain.id, "uPvmPrev" );
599 _uniform_scene_terrain_uTexGarbage = glGetUniformLocation( _shader_scene_terrain.id, "uTexGarbage" );
600 _uniform_scene_terrain_uTexGradients = glGetUniformLocation( _shader_scene_terrain.id, "uTexGradients" );
601 _uniform_scene_terrain_uCamera = glGetUniformLocation( _shader_scene_terrain.id, "uCamera" );
602 _uniform_scene_terrain_uSandColour = glGetUniformLocation( _shader_scene_terrain.id, "uSandColour" );
603 _uniform_scene_terrain_uBlendOffset = glGetUniformLocation( _shader_scene_terrain.id, "uBlendOffset" );
604 _uniform_scene_terrain_uBoard0 = glGetUniformLocation( _shader_scene_terrain.id, "uBoard0" );
605 _uniform_scene_terrain_uBoard1 = glGetUniformLocation( _shader_scene_terrain.id, "uBoard1" );
606 _uniform_scene_terrain_uLightsArray = glGetUniformLocation( _shader_scene_terrain.id, "uLightsArray" );
607 _uniform_scene_terrain_g_world_depth = glGetUniformLocation( _shader_scene_terrain.id, "g_world_depth" );
608 }
609 #endif /* SHADER_scene_terrain_H */