467cd91979c369de44f23795da8a16e7bea9eead
[carveJwlIkooP6JGAAIwe30JlM.git] / 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 shadow_sample( vec3 vdir )\n"
112 "{\n"
113 " vec3 sample_pos = aWorldCo + vdir;\n"
114 " float height_sample = world_depth_sample( sample_pos );\n"
115 "\n"
116 " float fdelta = height_sample - sample_pos.y;\n"
117 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
118 "}\n"
119 "\n"
120 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
121 "{\n"
122 " vec3 pa = p - a;\n"
123 " vec3 ba = b - a;\n"
124 "\n"
125 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
126 " return length( pa - ba*h );\n"
127 "}\n"
128 "\n"
129 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
130 "{\n"
131 " float dist = pow(fdist*0.0008,1.2);\n"
132 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
133 "}\n"
134 "\n"
135 "\n"
136 "// New lighting model\n"
137 "\n"
138 "vec3 newlight_compute_ambient()\n"
139 "{\n"
140 " return g_ambient_colour.rgb;\n"
141 "}\n"
142 "\n"
143 "float newlight_compute_sun_shadow()\n"
144 "{\n"
145 " if( g_shadow_samples == 0 )\n"
146 " {\n"
147 " return 1.0;\n"
148 " }\n"
149 "\n"
150 " float fspread = g_light_colours[0].w;\n"
151 " vec3 vdir = g_light_directions[0].xyz;\n"
152 " float flength = g_light_directions[0].w;\n"
153 "\n"
154 " float famt = 0.0;\n"
155 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
156 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
157 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
158 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
159 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
160 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
161 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
162 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
163 "\n"
164 " // player shadow\n"
165 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 );\n"
166 " float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );\n"
167 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
168 "\n"
169 " return 1.0 - max( player_shadow*0.8, famt );\n"
170 "}\n"
171 "\n"
172 "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n"
173 "{\n"
174 " vec3 vtotal = g_ambient_colour.rgb;\n"
175 "\n"
176 " for( int i=0; i<g_light_count; i++ )\n"
177 " {\n"
178 " vec3 vcolour = g_light_colours[i].rgb;\n"
179 " vec3 vdir = g_light_directions[i].xyz;\n"
180 "\n"
181 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
182 " vtotal += vcolour*flight;\n"
183 " }\n"
184 "\n"
185 " return vtotal;\n"
186 "}\n"
187 "\n"
188 "vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )\n"
189 "{\n"
190 " vec3 vcolour = g_light_colours[0].rgb;\n"
191 " vec3 vdir = g_light_directions[0].xyz;\n"
192 "\n"
193 " vec3 specdir = reflect( -vdir, wnormal );\n"
194 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
195 " return vcolour*spec*fintensity;\n"
196 "}\n"
197 "\n"
198 "vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, \n"
199 " vec3 light_pos, vec3 light_colour )\n"
200 "{\n"
201 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
202 "\n"
203 " float quadratic = dot(light_delta,light_delta);\n"
204 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
205 " attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );\n"
206 "\n"
207 " return light_colour*attenuation;\n"
208 "}\n"
209 "\n"
210 "#line 4 0 \n"
211 "\n"
212 "vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )\n"
213 "{\n"
214 " // Lighting\n"
215 " vec3 halfview = uCamera - aWorldCo;\n"
216 " float fdist = length(halfview);\n"
217 " halfview /= fdist;\n"
218 "\n"
219 " vec3 total_light = newlight_compute_ambient();\n"
220 " \n"
221 " // Compute world lighting contribution and apply it according to the\n"
222 " // shadow map\n"
223 " //\n"
224 " vec3 world_light = newlight_compute_world_diffuse( wnormal );\n"
225 " world_light += newlight_compute_sun_spec( wnormal, halfview, 0.1 );\n"
226 "\n"
227 " float world_shadow = newlight_compute_sun_shadow();\n"
228 "\n"
229 " total_light += world_light * world_shadow;\n"
230 "\n"
231 " // Compute the other lights that exist in the map, not effected by the sun\n"
232 " // shadow\n"
233 " total_light += newlight_compute_quadratic\n"
234 " ( \n"
235 " wnormal, halfview,\n"
236 " g_point_light_positions[ aLights.x ].xyz,\n"
237 " g_point_light_colours[ aLights.x ].rgb \n"
238 " );\n"
239 " total_light += newlight_compute_quadratic\n"
240 " ( \n"
241 " wnormal, halfview,\n"
242 " g_point_light_positions[ aLights.y ].xyz,\n"
243 " g_point_light_colours[ aLights.y ].rgb \n"
244 " );\n"
245 " total_light += newlight_compute_quadratic\n"
246 " ( \n"
247 " wnormal, halfview,\n"
248 " g_point_light_positions[ aLights.z ].xyz,\n"
249 " g_point_light_colours[ aLights.z ].rgb \n"
250 " );\n"
251 "\n"
252 " return apply_fog( diffuse * total_light, fdist );\n"
253 "}\n"
254 "\n"
255 "#line 16 0 \n"
256 "#line 1 2 \n"
257 "const float k_motion_lerp_amount = 0.01;\n"
258 "\n"
259 "#line 2 0 \n"
260 "\n"
261 "layout (location = 1) out vec2 oMotionVec;\n"
262 "\n"
263 "in vec3 aMotionVec0;\n"
264 "in vec3 aMotionVec1;\n"
265 "\n"
266 "void compute_motion_vectors()\n"
267 "{\n"
268 " // Write motion vectors\n"
269 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
270 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
271 "\n"
272 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
273 "}\n"
274 "\n"
275 "#line 17 0 \n"
276 "\n"
277 "void main()\n"
278 "{\n"
279 " compute_motion_vectors();\n"
280 "\n"
281 " // Colour\n"
282 " // ------\n"
283 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
284 "\n"
285 " // ws modulation\n"
286 " vec4 wgarbage = texture( uTexGarbage, aCo.xz * 0.015 );\n"
287 " \n"
288 " // Creating normal patches\n"
289 " vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;\n"
290 " vec3 qnorm = normalize(floor(aNorm.xyz*4.0+modnorm)*0.25);\n"
291 " qnorm += vec3(0.001,0.0,0.0);\n"
292 "\n"
293 " vec2 dir = normalize(qnorm.xz);\n"
294 " vec2 uvdiffuse = aCo.xz * 0.02;\n"
295 " uvdiffuse = mat2(dir.y, dir.x, -dir.x, dir.y) * uvdiffuse;\n"
296 " \n"
297 " // Patch local noise\n"
298 " vec4 rgarbage = texture( uTexGarbage, uvdiffuse );\n"
299 "\n"
300 " // Colour blending\n"
301 " float amtgrass = step(qnorm.y,0.6);\n"
302 " float amtsand = min(max((aCo.y - 10.0) * -0.1,0.0)*qnorm.y,1.0);\n"
303 " vec2 uvgradients = aUv + vec2( amtgrass + rgarbage.a*0.8 )*uBlendOffset;\n"
304 " vfrag = texture( uTexGradients, uvgradients ).rgb;\n"
305 " vfrag = mix( vfrag, uSandColour, amtsand );\n"
306 "\n"
307 " qnorm = mix( qnorm, aNorm.xyz, amtsand );\n"
308 " \n"
309 " if( g_light_preview == 1 )\n"
310 " {\n"
311 " vfrag = vec3(0.5);\n"
312 " }\n"
313 "\n"
314 " vfrag = scene_do_lighting( vfrag, qnorm );\n"
315 " oColour = vec4(vfrag, 1.0);\n"
316 "}\n"
317 ""},
318 };
319
320 static GLuint _uniform_scene_terrain_uMdl;
321 static GLuint _uniform_scene_terrain_uPv;
322 static GLuint _uniform_scene_terrain_uPvmPrev;
323 static GLuint _uniform_scene_terrain_uTexGarbage;
324 static GLuint _uniform_scene_terrain_uTexGradients;
325 static GLuint _uniform_scene_terrain_uCamera;
326 static GLuint _uniform_scene_terrain_uSandColour;
327 static GLuint _uniform_scene_terrain_uBlendOffset;
328 static GLuint _uniform_scene_terrain_uBoard0;
329 static GLuint _uniform_scene_terrain_uBoard1;
330 static GLuint _uniform_scene_terrain_g_world_depth;
331 static void shader_scene_terrain_uMdl(m4x3f m){
332 glUniformMatrix4x3fv(_uniform_scene_terrain_uMdl,1,GL_FALSE,(float*)m);
333 }
334 static void shader_scene_terrain_uPv(m4x4f m){
335 glUniformMatrix4fv(_uniform_scene_terrain_uPv,1,GL_FALSE,(float*)m);
336 }
337 static void shader_scene_terrain_uPvmPrev(m4x4f m){
338 glUniformMatrix4fv(_uniform_scene_terrain_uPvmPrev,1,GL_FALSE,(float*)m);
339 }
340 static void shader_scene_terrain_uTexGarbage(int i){
341 glUniform1i(_uniform_scene_terrain_uTexGarbage,i);
342 }
343 static void shader_scene_terrain_uTexGradients(int i){
344 glUniform1i(_uniform_scene_terrain_uTexGradients,i);
345 }
346 static void shader_scene_terrain_uCamera(v3f v){
347 glUniform3fv(_uniform_scene_terrain_uCamera,1,v);
348 }
349 static void shader_scene_terrain_uSandColour(v3f v){
350 glUniform3fv(_uniform_scene_terrain_uSandColour,1,v);
351 }
352 static void shader_scene_terrain_uBlendOffset(v2f v){
353 glUniform2fv(_uniform_scene_terrain_uBlendOffset,1,v);
354 }
355 static void shader_scene_terrain_uBoard0(v3f v){
356 glUniform3fv(_uniform_scene_terrain_uBoard0,1,v);
357 }
358 static void shader_scene_terrain_uBoard1(v3f v){
359 glUniform3fv(_uniform_scene_terrain_uBoard1,1,v);
360 }
361 static void shader_scene_terrain_g_world_depth(int i){
362 glUniform1i(_uniform_scene_terrain_g_world_depth,i);
363 }
364 static void shader_scene_terrain_register(void){
365 vg_shader_register( &_shader_scene_terrain );
366 }
367 static void shader_scene_terrain_use(void){ glUseProgram(_shader_scene_terrain.id); }
368 static void shader_scene_terrain_link(void){
369 _uniform_scene_terrain_uMdl = glGetUniformLocation( _shader_scene_terrain.id, "uMdl" );
370 _uniform_scene_terrain_uPv = glGetUniformLocation( _shader_scene_terrain.id, "uPv" );
371 _uniform_scene_terrain_uPvmPrev = glGetUniformLocation( _shader_scene_terrain.id, "uPvmPrev" );
372 _uniform_scene_terrain_uTexGarbage = glGetUniformLocation( _shader_scene_terrain.id, "uTexGarbage" );
373 _uniform_scene_terrain_uTexGradients = glGetUniformLocation( _shader_scene_terrain.id, "uTexGradients" );
374 _uniform_scene_terrain_uCamera = glGetUniformLocation( _shader_scene_terrain.id, "uCamera" );
375 _uniform_scene_terrain_uSandColour = glGetUniformLocation( _shader_scene_terrain.id, "uSandColour" );
376 _uniform_scene_terrain_uBlendOffset = glGetUniformLocation( _shader_scene_terrain.id, "uBlendOffset" );
377 _uniform_scene_terrain_uBoard0 = glGetUniformLocation( _shader_scene_terrain.id, "uBoard0" );
378 _uniform_scene_terrain_uBoard1 = glGetUniformLocation( _shader_scene_terrain.id, "uBoard1" );
379 _uniform_scene_terrain_g_world_depth = glGetUniformLocation( _shader_scene_terrain.id, "g_world_depth" );
380 }
381 #endif /* SHADER_scene_terrain_H */