5913d2c0d30d7e992be7a282ee37d8a8e6ee03fc
[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 "\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 uSandColour;\n"
70 "uniform vec2 uBlendOffset;\n"
71 "uniform vec3 uBoard0;\n"
72 "uniform vec3 uBoard1;\n"
73 "\n"
74 "in vec2 aUv;\n"
75 "in vec4 aNorm;\n"
76 "in vec3 aCo;\n"
77 "in vec3 aWorldCo;\n"
78 "flat in ivec4 aLights;\n"
79 "\n"
80 "#line 1 1 \n"
81 "// :D\n"
82 "\n"
83 "#line 1 1 \n"
84 "layout (location = 0) out vec4 oColour;\n"
85 "\n"
86 "layout (std140) uniform ub_world_lighting\n"
87 "{\n"
88 " vec4 g_light_colours[3];\n"
89 " vec4 g_light_directions[3];\n"
90 " vec4 g_ambient_colour;\n"
91 "\n"
92 " vec4 g_water_plane;\n"
93 " vec4 g_depth_bounds;\n"
94 " float g_water_fog;\n"
95 " int g_light_count;\n"
96 " int g_light_preview;\n"
97 " int g_shadow_samples;\n"
98 "\n"
99 " vec4 g_point_light_positions[32];\n"
100 " vec4 g_point_light_colours[32];\n"
101 "};\n"
102 "\n"
103 "uniform sampler2D g_world_depth;\n"
104 "\n"
105 "float world_depth_sample( vec3 pos )\n"
106 "{\n"
107 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
108 " return texture( g_world_depth, depth_coord ).r;\n"
109 "}\n"
110 "\n"
111 "float world_water_depth( vec3 pos )\n"
112 "{\n"
113 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
114 " float ref_depth = g_water_plane.y*g_water_plane.w;\n"
115 " return texture( g_world_depth, depth_coord ).g - ref_depth;\n"
116 "}\n"
117 "\n"
118 "float shadow_sample( vec3 vdir )\n"
119 "{\n"
120 " vec3 sample_pos = aWorldCo + vdir;\n"
121 " float height_sample = world_depth_sample( sample_pos );\n"
122 "\n"
123 " float fdelta = height_sample - sample_pos.y;\n"
124 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
125 "}\n"
126 "\n"
127 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
128 "{\n"
129 " vec3 pa = p - a;\n"
130 " vec3 ba = b - a;\n"
131 "\n"
132 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
133 " return length( pa - ba*h );\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()\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 = g_light_directions[0].xyz;\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 " // player shadow\n"
172 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 );\n"
173 " float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );\n"
174 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
175 "\n"
176 " return 1.0 - max( player_shadow*0.8, famt );\n"
177 "}\n"
178 "\n"
179 "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n"
180 "{\n"
181 " vec3 vtotal = g_ambient_colour.rgb;\n"
182 "\n"
183 " for( int i=0; i<g_light_count; i++ )\n"
184 " {\n"
185 " vec3 vcolour = g_light_colours[i].rgb;\n"
186 " vec3 vdir = g_light_directions[i].xyz;\n"
187 "\n"
188 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
189 " vtotal += vcolour*flight;\n"
190 " }\n"
191 "\n"
192 " return vtotal;\n"
193 "}\n"
194 "\n"
195 "vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )\n"
196 "{\n"
197 " vec3 vcolour = g_light_colours[0].rgb;\n"
198 " vec3 vdir = g_light_directions[0].xyz;\n"
199 "\n"
200 " vec3 specdir = reflect( -vdir, wnormal );\n"
201 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
202 " return vcolour*spec*fintensity;\n"
203 "}\n"
204 "\n"
205 "vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, \n"
206 " vec3 light_pos, vec3 light_colour )\n"
207 "{\n"
208 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
209 "\n"
210 " float quadratic = dot(light_delta,light_delta);\n"
211 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
212 " attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );\n"
213 "\n"
214 " return light_colour*attenuation;\n"
215 "}\n"
216 "\n"
217 "#line 4 0 \n"
218 "\n"
219 "vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )\n"
220 "{\n"
221 " // Lighting\n"
222 " vec3 halfview = uCamera - aWorldCo;\n"
223 " float fdist = length(halfview);\n"
224 " halfview /= fdist;\n"
225 "\n"
226 " vec3 total_light = newlight_compute_ambient();\n"
227 " \n"
228 " // Compute world lighting contribution and apply it according to the\n"
229 " // shadow map\n"
230 " //\n"
231 " vec3 world_light = newlight_compute_world_diffuse( wnormal );\n"
232 " world_light += newlight_compute_sun_spec( wnormal, halfview, 0.1 );\n"
233 "\n"
234 " float world_shadow = newlight_compute_sun_shadow();\n"
235 "\n"
236 " total_light += world_light * world_shadow;\n"
237 "\n"
238 " // Compute the other lights that exist in the map, not effected by the sun\n"
239 " // shadow\n"
240 " total_light += newlight_compute_quadratic\n"
241 " ( \n"
242 " wnormal, halfview,\n"
243 " g_point_light_positions[ aLights.x ].xyz,\n"
244 " g_point_light_colours[ aLights.x ].rgb \n"
245 " );\n"
246 " total_light += newlight_compute_quadratic\n"
247 " ( \n"
248 " wnormal, halfview,\n"
249 " g_point_light_positions[ aLights.y ].xyz,\n"
250 " g_point_light_colours[ aLights.y ].rgb \n"
251 " );\n"
252 " total_light += newlight_compute_quadratic\n"
253 " ( \n"
254 " wnormal, halfview,\n"
255 " g_point_light_positions[ aLights.z ].xyz,\n"
256 " g_point_light_colours[ aLights.z ].rgb \n"
257 " );\n"
258 "\n"
259 " return apply_fog( diffuse * total_light, fdist );\n"
260 "}\n"
261 "\n"
262 "#line 16 0 \n"
263 "#line 1 2 \n"
264 "const float k_motion_lerp_amount = 0.01;\n"
265 "\n"
266 "#line 2 0 \n"
267 "\n"
268 "layout (location = 1) out vec2 oMotionVec;\n"
269 "\n"
270 "in vec3 aMotionVec0;\n"
271 "in vec3 aMotionVec1;\n"
272 "\n"
273 "void compute_motion_vectors()\n"
274 "{\n"
275 " // Write motion vectors\n"
276 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
277 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
278 "\n"
279 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
280 "}\n"
281 "\n"
282 "#line 17 0 \n"
283 "\n"
284 "void main()\n"
285 "{\n"
286 " compute_motion_vectors();\n"
287 "\n"
288 " // Colour\n"
289 " // ------\n"
290 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
291 "\n"
292 " // ws modulation\n"
293 " vec4 wgarbage = texture( uTexGarbage, aCo.xz * 0.015 );\n"
294 " \n"
295 " // Creating normal patches\n"
296 " vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;\n"
297 " vec3 qnorm = normalize(floor(aNorm.xyz*4.0+modnorm)*0.25);\n"
298 " qnorm += vec3(0.001,0.0,0.0);\n"
299 "\n"
300 " vec2 dir = normalize(qnorm.xz);\n"
301 " vec2 uvdiffuse = aCo.xz * 0.02;\n"
302 " uvdiffuse = mat2(dir.y, dir.x, -dir.x, dir.y) * uvdiffuse;\n"
303 " \n"
304 " // Patch local noise\n"
305 " vec4 rgarbage = texture( uTexGarbage, uvdiffuse );\n"
306 "\n"
307 " // Colour blending\n"
308 " float amtgrass = step(qnorm.y,0.6);\n"
309 " float amtsand = min(max((aCo.y - 10.0) * -0.1,0.0)*qnorm.y,1.0);\n"
310 " vec2 uvgradients = aUv + vec2( amtgrass + rgarbage.a*0.8 )*uBlendOffset;\n"
311 " vfrag = texture( uTexGradients, uvgradients ).rgb;\n"
312 " vfrag = mix( vfrag, uSandColour, amtsand );\n"
313 "\n"
314 " qnorm = mix( qnorm, aNorm.xyz, amtsand );\n"
315 " \n"
316 " if( g_light_preview == 1 )\n"
317 " {\n"
318 " vfrag = vec3(0.5);\n"
319 " }\n"
320 "\n"
321 " vfrag = scene_do_lighting( vfrag, qnorm );\n"
322 " oColour = vec4(vfrag, 1.0);\n"
323 "}\n"
324 ""},
325 };
326
327 static GLuint _uniform_scene_terrain_uMdl;
328 static GLuint _uniform_scene_terrain_uPv;
329 static GLuint _uniform_scene_terrain_uPvmPrev;
330 static GLuint _uniform_scene_terrain_uTexGarbage;
331 static GLuint _uniform_scene_terrain_uTexGradients;
332 static GLuint _uniform_scene_terrain_uCamera;
333 static GLuint _uniform_scene_terrain_uSandColour;
334 static GLuint _uniform_scene_terrain_uBlendOffset;
335 static GLuint _uniform_scene_terrain_uBoard0;
336 static GLuint _uniform_scene_terrain_uBoard1;
337 static GLuint _uniform_scene_terrain_g_world_depth;
338 static void shader_scene_terrain_uMdl(m4x3f m){
339 glUniformMatrix4x3fv(_uniform_scene_terrain_uMdl,1,GL_FALSE,(float*)m);
340 }
341 static void shader_scene_terrain_uPv(m4x4f m){
342 glUniformMatrix4fv(_uniform_scene_terrain_uPv,1,GL_FALSE,(float*)m);
343 }
344 static void shader_scene_terrain_uPvmPrev(m4x4f m){
345 glUniformMatrix4fv(_uniform_scene_terrain_uPvmPrev,1,GL_FALSE,(float*)m);
346 }
347 static void shader_scene_terrain_uTexGarbage(int i){
348 glUniform1i(_uniform_scene_terrain_uTexGarbage,i);
349 }
350 static void shader_scene_terrain_uTexGradients(int i){
351 glUniform1i(_uniform_scene_terrain_uTexGradients,i);
352 }
353 static void shader_scene_terrain_uCamera(v3f v){
354 glUniform3fv(_uniform_scene_terrain_uCamera,1,v);
355 }
356 static void shader_scene_terrain_uSandColour(v3f v){
357 glUniform3fv(_uniform_scene_terrain_uSandColour,1,v);
358 }
359 static void shader_scene_terrain_uBlendOffset(v2f v){
360 glUniform2fv(_uniform_scene_terrain_uBlendOffset,1,v);
361 }
362 static void shader_scene_terrain_uBoard0(v3f v){
363 glUniform3fv(_uniform_scene_terrain_uBoard0,1,v);
364 }
365 static void shader_scene_terrain_uBoard1(v3f v){
366 glUniform3fv(_uniform_scene_terrain_uBoard1,1,v);
367 }
368 static void shader_scene_terrain_g_world_depth(int i){
369 glUniform1i(_uniform_scene_terrain_g_world_depth,i);
370 }
371 static void shader_scene_terrain_register(void){
372 vg_shader_register( &_shader_scene_terrain );
373 }
374 static void shader_scene_terrain_use(void){ glUseProgram(_shader_scene_terrain.id); }
375 static void shader_scene_terrain_link(void){
376 _uniform_scene_terrain_uMdl = glGetUniformLocation( _shader_scene_terrain.id, "uMdl" );
377 _uniform_scene_terrain_uPv = glGetUniformLocation( _shader_scene_terrain.id, "uPv" );
378 _uniform_scene_terrain_uPvmPrev = glGetUniformLocation( _shader_scene_terrain.id, "uPvmPrev" );
379 _uniform_scene_terrain_uTexGarbage = glGetUniformLocation( _shader_scene_terrain.id, "uTexGarbage" );
380 _uniform_scene_terrain_uTexGradients = glGetUniformLocation( _shader_scene_terrain.id, "uTexGradients" );
381 _uniform_scene_terrain_uCamera = glGetUniformLocation( _shader_scene_terrain.id, "uCamera" );
382 _uniform_scene_terrain_uSandColour = glGetUniformLocation( _shader_scene_terrain.id, "uSandColour" );
383 _uniform_scene_terrain_uBlendOffset = glGetUniformLocation( _shader_scene_terrain.id, "uBlendOffset" );
384 _uniform_scene_terrain_uBoard0 = glGetUniformLocation( _shader_scene_terrain.id, "uBoard0" );
385 _uniform_scene_terrain_uBoard1 = glGetUniformLocation( _shader_scene_terrain.id, "uBoard1" );
386 _uniform_scene_terrain_g_world_depth = glGetUniformLocation( _shader_scene_terrain.id, "g_world_depth" );
387 }
388 #endif /* SHADER_scene_terrain_H */