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