now we're doing a bunch of them
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / scene_standard.h
1 #ifndef SHADER_scene_standard_H
2 #define SHADER_scene_standard_H
3 static void shader_scene_standard_link(void);
4 static void shader_scene_standard_register(void);
5 static struct vg_shader _shader_scene_standard = {
6 .name = "scene_standard",
7 .link = shader_scene_standard_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 uTexMain;\n"
77 "uniform vec3 uCamera;\n"
78 "uniform vec4 uPlane;\n"
79 "uniform vec3 uBoard0;\n"
80 "uniform vec3 uBoard1;\n"
81 "\n"
82 "#line 1 1 \n"
83 "// :D\n"
84 "\n"
85 "in vec2 aUv;\n"
86 "in vec4 aNorm;\n"
87 "in vec3 aCo;\n"
88 "in vec3 aWorldCo;\n"
89 "flat in vec4 light_colours[3];\n"
90 "flat in vec4 light_positions[3];\n"
91 "\n"
92 "#line 1 1 \n"
93 "layout (location = 0) out vec4 oColour;\n"
94 "\n"
95 "layout (std140) uniform ub_world_lighting\n"
96 "{\n"
97 " vec4 g_light_colours[3];\n"
98 " vec4 g_light_directions[3];\n"
99 " vec4 g_ambient_colour;\n"
100 "\n"
101 " vec4 g_water_plane;\n"
102 " vec4 g_depth_bounds;\n"
103 " float g_water_fog;\n"
104 " int g_light_count;\n"
105 " int g_light_preview;\n"
106 " int g_shadow_samples;\n"
107 "\n"
108 " // g_time ?\n"
109 "\n"
110 " //vec4 g_point_light_positions[32];\n"
111 " //vec4 g_point_light_colours[32];\n"
112 "};\n"
113 "\n"
114 "uniform sampler2D g_world_depth;\n"
115 "\n"
116 "float world_depth_sample( vec3 pos )\n"
117 "{\n"
118 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
119 " return texture( g_world_depth, depth_coord ).r;\n"
120 "}\n"
121 "\n"
122 "float world_water_depth( vec3 pos )\n"
123 "{\n"
124 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
125 " float ref_depth = g_water_plane.y*g_water_plane.w;\n"
126 " return texture( g_world_depth, depth_coord ).g - ref_depth;\n"
127 "}\n"
128 "\n"
129 "float shadow_sample( vec3 vdir )\n"
130 "{\n"
131 " vec3 sample_pos = aWorldCo + vdir;\n"
132 " float height_sample = world_depth_sample( sample_pos );\n"
133 "\n"
134 " float fdelta = height_sample - sample_pos.y;\n"
135 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
136 "}\n"
137 "\n"
138 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
139 "{\n"
140 " float dist = pow(fdist*0.0008,1.2);\n"
141 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
142 "}\n"
143 "\n"
144 "\n"
145 "// New lighting model\n"
146 "\n"
147 "vec3 newlight_compute_ambient()\n"
148 "{\n"
149 " return g_ambient_colour.rgb;\n"
150 "}\n"
151 "\n"
152 "float newlight_compute_sun_shadow()\n"
153 "{\n"
154 " if( g_shadow_samples == 0 )\n"
155 " {\n"
156 " return 1.0;\n"
157 " }\n"
158 "\n"
159 " float fspread = g_light_colours[0].w;\n"
160 " vec3 vdir = g_light_directions[0].xyz;\n"
161 " float flength = g_light_directions[0].w;\n"
162 "\n"
163 " float famt = 0.0;\n"
164 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
165 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
166 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
167 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
168 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
169 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
170 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
171 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
172 "\n"
173 " return 1.0 - famt;\n"
174 "}\n"
175 "\n"
176 "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n"
177 "{\n"
178 " vec3 vtotal = g_ambient_colour.rgb;\n"
179 "\n"
180 " for( int i=0; i<g_light_count; i++ )\n"
181 " {\n"
182 " vec3 vcolour = g_light_colours[i].rgb;\n"
183 " vec3 vdir = g_light_directions[i].xyz;\n"
184 "\n"
185 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
186 " vtotal += vcolour*flight;\n"
187 " }\n"
188 "\n"
189 " return vtotal;\n"
190 "}\n"
191 "\n"
192 "vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )\n"
193 "{\n"
194 " vec3 vcolour = g_light_colours[0].rgb;\n"
195 " vec3 vdir = g_light_directions[0].xyz;\n"
196 "\n"
197 " vec3 specdir = reflect( -vdir, wnormal );\n"
198 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
199 " return vcolour*spec*fintensity;\n"
200 "}\n"
201 "\n"
202 "vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, \n"
203 " vec3 light_pos, vec3 light_colour )\n"
204 "{\n"
205 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
206 "\n"
207 " float quadratic = dot(light_delta,light_delta);\n"
208 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
209 " attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );\n"
210 "\n"
211 " return light_colour*attenuation;\n"
212 "}\n"
213 "\n"
214 "#line 11 0 \n"
215 "\n"
216 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
217 "{\n"
218 " vec3 pa = p - a;\n"
219 " vec3 ba = b - a;\n"
220 "\n"
221 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
222 " return length( pa - ba*h );\n"
223 "}\n"
224 "\n"
225 "float compute_board_shadow()\n"
226 "{\n"
227 " // player shadow\n"
228 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 );\n"
229 " float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );\n"
230 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
231 "\n"
232 " return 1.0 - player_shadow*0.8;\n"
233 "}\n"
234 "\n"
235 "vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )\n"
236 "{\n"
237 " // Lighting\n"
238 " vec3 halfview = uCamera - aWorldCo;\n"
239 " float fdist = length(halfview);\n"
240 " halfview /= fdist;\n"
241 "\n"
242 " vec3 total_light = newlight_compute_ambient();\n"
243 " \n"
244 " // Compute world lighting contribution and apply it according to the\n"
245 " // shadow map\n"
246 " //\n"
247 " vec3 world_light = newlight_compute_world_diffuse( wnormal );\n"
248 " world_light += newlight_compute_sun_spec( wnormal, halfview, 0.1 );\n"
249 "\n"
250 " float world_shadow = newlight_compute_sun_shadow();\n"
251 " float board_shadow = compute_board_shadow();\n"
252 "\n"
253 " total_light += world_light * min( board_shadow, world_shadow );\n"
254 "\n"
255 " // Compute the other lights that exist in the map, not effected by the sun\n"
256 " // shadow\n"
257 "\n"
258 " total_light += newlight_compute_quadratic\n"
259 " ( \n"
260 " wnormal, halfview,\n"
261 " light_positions[0].xyz,\n"
262 " light_colours[0].rgb \n"
263 " ) * board_shadow;\n"
264 " total_light += newlight_compute_quadratic\n"
265 " ( \n"
266 " wnormal, halfview,\n"
267 " light_positions[1].xyz,\n"
268 " light_colours[1].rgb \n"
269 " ) * board_shadow;\n"
270 " total_light += newlight_compute_quadratic\n"
271 " ( \n"
272 " wnormal, halfview,\n"
273 " light_positions[2].xyz,\n"
274 " light_colours[2].rgb \n"
275 " ) * board_shadow;\n"
276 "\n"
277 " return apply_fog( diffuse * total_light, fdist );\n"
278 "}\n"
279 "\n"
280 "#line 9 0 \n"
281 "#line 1 2 \n"
282 "const float k_motion_lerp_amount = 0.01;\n"
283 "\n"
284 "#line 2 0 \n"
285 "\n"
286 "layout (location = 1) out vec2 oMotionVec;\n"
287 "\n"
288 "in vec3 aMotionVec0;\n"
289 "in vec3 aMotionVec1;\n"
290 "\n"
291 "void compute_motion_vectors()\n"
292 "{\n"
293 " // Write motion vectors\n"
294 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
295 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
296 "\n"
297 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
298 "}\n"
299 "\n"
300 "#line 10 0 \n"
301 "\n"
302 "void main()\n"
303 "{\n"
304 " compute_motion_vectors();\n"
305 "\n"
306 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
307 " vec4 vsamplemain = texture( uTexMain, aUv );\n"
308 " vec3 qnorm = aNorm.xyz;\n"
309 "\n"
310 " vfrag = vsamplemain.rgb;\n"
311 "\n"
312 " if( g_light_preview == 1 )\n"
313 " {\n"
314 " vfrag = vec3(0.5);\n"
315 " }\n"
316 "\n"
317 " vfrag = scene_do_lighting( vfrag, qnorm );\n"
318 " oColour = vec4( vfrag, 1.0 );\n"
319 "}\n"
320 ""},
321 };
322
323 static GLuint _uniform_scene_standard_uMdl;
324 static GLuint _uniform_scene_standard_uPv;
325 static GLuint _uniform_scene_standard_uPvmPrev;
326 static GLuint _uniform_scene_standard_uLightsArray;
327 static GLuint _uniform_scene_standard_uTexGarbage;
328 static GLuint _uniform_scene_standard_uTexMain;
329 static GLuint _uniform_scene_standard_uCamera;
330 static GLuint _uniform_scene_standard_uPlane;
331 static GLuint _uniform_scene_standard_uBoard0;
332 static GLuint _uniform_scene_standard_uBoard1;
333 static GLuint _uniform_scene_standard_g_world_depth;
334 static void shader_scene_standard_uMdl(m4x3f m){
335 glUniformMatrix4x3fv(_uniform_scene_standard_uMdl,1,GL_FALSE,(float*)m);
336 }
337 static void shader_scene_standard_uPv(m4x4f m){
338 glUniformMatrix4fv(_uniform_scene_standard_uPv,1,GL_FALSE,(float*)m);
339 }
340 static void shader_scene_standard_uPvmPrev(m4x4f m){
341 glUniformMatrix4fv(_uniform_scene_standard_uPvmPrev,1,GL_FALSE,(float*)m);
342 }
343 static void shader_scene_standard_uTexGarbage(int i){
344 glUniform1i(_uniform_scene_standard_uTexGarbage,i);
345 }
346 static void shader_scene_standard_uTexMain(int i){
347 glUniform1i(_uniform_scene_standard_uTexMain,i);
348 }
349 static void shader_scene_standard_uCamera(v3f v){
350 glUniform3fv(_uniform_scene_standard_uCamera,1,v);
351 }
352 static void shader_scene_standard_uPlane(v4f v){
353 glUniform4fv(_uniform_scene_standard_uPlane,1,v);
354 }
355 static void shader_scene_standard_uBoard0(v3f v){
356 glUniform3fv(_uniform_scene_standard_uBoard0,1,v);
357 }
358 static void shader_scene_standard_uBoard1(v3f v){
359 glUniform3fv(_uniform_scene_standard_uBoard1,1,v);
360 }
361 static void shader_scene_standard_g_world_depth(int i){
362 glUniform1i(_uniform_scene_standard_g_world_depth,i);
363 }
364 static void shader_scene_standard_register(void){
365 vg_shader_register( &_shader_scene_standard );
366 }
367 static void shader_scene_standard_use(void){ glUseProgram(_shader_scene_standard.id); }
368 static void shader_scene_standard_link(void){
369 _uniform_scene_standard_uMdl = glGetUniformLocation( _shader_scene_standard.id, "uMdl" );
370 _uniform_scene_standard_uPv = glGetUniformLocation( _shader_scene_standard.id, "uPv" );
371 _uniform_scene_standard_uPvmPrev = glGetUniformLocation( _shader_scene_standard.id, "uPvmPrev" );
372 _uniform_scene_standard_uLightsArray = glGetUniformLocation( _shader_scene_standard.id, "uLightsArray" );
373 _uniform_scene_standard_uTexGarbage = glGetUniformLocation( _shader_scene_standard.id, "uTexGarbage" );
374 _uniform_scene_standard_uTexMain = glGetUniformLocation( _shader_scene_standard.id, "uTexMain" );
375 _uniform_scene_standard_uCamera = glGetUniformLocation( _shader_scene_standard.id, "uCamera" );
376 _uniform_scene_standard_uPlane = glGetUniformLocation( _shader_scene_standard.id, "uPlane" );
377 _uniform_scene_standard_uBoard0 = glGetUniformLocation( _shader_scene_standard.id, "uBoard0" );
378 _uniform_scene_standard_uBoard1 = glGetUniformLocation( _shader_scene_standard.id, "uBoard1" );
379 _uniform_scene_standard_g_world_depth = glGetUniformLocation( _shader_scene_standard.id, "g_world_depth" );
380 }
381 #endif /* SHADER_scene_standard_H */