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