df shadows
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / alphatest.h
1 #ifndef SHADER_alphatest_H
2 #define SHADER_alphatest_H
3 static void shader_alphatest_link(void);
4 static void shader_alphatest_register(void);
5 static struct vg_shader _shader_alphatest = {
6 .name = "alphatest",
7 .link = shader_alphatest_link,
8 .vs =
9 {
10 .static_src =
11 "layout (location=0) in vec3 a_co;\n"
12 "layout (location=1) in vec3 a_norm;\n"
13 "layout (location=2) in vec2 a_uv;\n"
14 "layout (location=3) in vec4 a_colour;\n"
15 "layout (location=4) in vec4 a_weights;\n"
16 "layout (location=5) in ivec4 a_groups;\n"
17 "\n"
18 "#line 2 0 \n"
19 "#line 1 2 \n"
20 "const float k_motion_lerp_amount = 0.01;\n"
21 "\n"
22 "#line 2 0 \n"
23 "\n"
24 "out vec3 aMotionVec0;\n"
25 "out vec3 aMotionVec1;\n"
26 "\n"
27 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
28 "{\n"
29 " // This magically solves some artifacting errors!\n"
30 " //\n"
31 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
32 "\n"
33 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
34 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
35 "}\n"
36 "\n"
37 "#line 3 0 \n"
38 "\n"
39 "uniform mat4x3 uMdl;\n"
40 "uniform mat4 uPv;\n"
41 "uniform mat4 uPvmPrev;\n"
42 "\n"
43 "out vec4 aColour;\n"
44 "out vec2 aUv;\n"
45 "out vec3 aNorm;\n"
46 "out vec3 aCo;\n"
47 "out vec3 aWorldCo;\n"
48 "\n"
49 "void main()\n"
50 "{\n"
51 " vec3 world_pos0 = uMdl * vec4( a_co, 1.0 );\n"
52 " vec4 vproj0 = uPv * vec4( world_pos0, 1.0 );\n"
53 " vec4 vproj1 = uPvmPrev * vec4( a_co, 1.0 );\n"
54 "\n"
55 " vs_motion_out( vproj0, vproj1 );\n"
56 "\n"
57 " gl_Position = vproj0;\n"
58 " aWorldCo = world_pos0;\n"
59 " aColour = a_colour;\n"
60 " aUv = a_uv;\n"
61 " aNorm = mat3(uMdl) * a_norm;\n"
62 " aCo = a_co;\n"
63 "}\n"
64 ""},
65 .fs =
66 {
67 .static_src =
68 "uniform sampler2D uTexGarbage;\n"
69 "uniform sampler2D uTexMain;\n"
70 "uniform vec3 uBoard0;\n"
71 "uniform vec3 uBoard1;\n"
72 "uniform vec3 uCamera;\n"
73 "uniform vec4 uPlane;\n"
74 "\n"
75 "in vec4 aColour;\n"
76 "in vec2 aUv;\n"
77 "in vec3 aNorm;\n"
78 "in vec3 aCo;\n"
79 "in vec3 aWorldCo;\n"
80 "\n"
81 "#line 1 1 \n"
82 "layout (location = 0) out vec4 oColour;\n"
83 "\n"
84 "layout (std140) uniform ub_world_lighting\n"
85 "{\n"
86 " vec4 g_light_colours[3];\n"
87 " vec4 g_light_directions[3];\n"
88 " vec4 g_ambient_colour;\n"
89 "\n"
90 " vec4 g_water_plane;\n"
91 " vec4 g_depth_bounds;\n"
92 " float g_water_fog;\n"
93 " int g_light_count;\n"
94 " int g_light_preview;\n"
95 " int g_shadow_samples;\n"
96 "};\n"
97 "\n"
98 "uniform sampler2D g_world_depth;\n"
99 "\n"
100 "// Standard diffuse + spec models\n"
101 "// ==============================\n"
102 "\n"
103 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
104 "{\n"
105 " vec3 vtotal = g_ambient_colour.rgb;\n"
106 "\n"
107 " for( int i=0; i<g_light_count; i++ )\n"
108 " {\n"
109 " vec3 vcolour = g_light_colours[i].rgb;\n"
110 " vec3 vdir = g_light_directions[i].xyz;\n"
111 "\n"
112 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
113 " vtotal += vcolour*flight;\n"
114 " }\n"
115 "\n"
116 " return vfrag * vtotal;\n"
117 "}\n"
118 "\n"
119 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
120 "{\n"
121 " vec3 vcolour = g_light_colours[0].rgb;\n"
122 " vec3 vdir = g_light_directions[0].xyz;\n"
123 "\n"
124 " vec3 specdir = reflect( -vdir, wnormal );\n"
125 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
126 " return vfrag + vcolour*spec*fintensity;\n"
127 "}\n"
128 "\n"
129 "float world_depth_sample( vec3 pos )\n"
130 "{\n"
131 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
132 " return texture( g_world_depth, depth_coord ).r;\n"
133 "}\n"
134 "\n"
135 "float shadow_sample( vec3 vdir )\n"
136 "{\n"
137 " vec3 sample_pos = aWorldCo + vdir;\n"
138 " float height_sample = world_depth_sample( sample_pos );\n"
139 "\n"
140 " float fdelta = height_sample - sample_pos.y;\n"
141 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
142 "}\n"
143 "\n"
144 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
145 "{\n"
146 " float faccum = 0.0;\n"
147 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
148 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
149 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
150 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
151 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
152 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
153 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
154 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
155 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
156 "}\n"
157 "\n"
158 "// FIXME\n"
159 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
160 "{\n"
161 " vec3 pa = p - a;\n"
162 " vec3 ba = b - a;\n"
163 "\n"
164 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
165 " return length( pa - ba*h );\n"
166 "}\n"
167 "\n"
168 "vec3 do_light_shadowing( vec3 vfrag )\n"
169 "{\n"
170 " if( g_shadow_samples == 0 )\n"
171 " {\n"
172 " return vfrag;\n"
173 " }\n"
174 "\n"
175 " float fspread = g_light_colours[0].w;\n"
176 " vec3 vdir = g_light_directions[0].xyz;\n"
177 " float flength = g_light_directions[0].w;\n"
178 "\n"
179 " float famt = 0.0;\n"
180 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
181 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
182 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
183 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
184 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
185 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
186 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
187 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
188 "\n"
189 " // player shadow\n"
190 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.03 );\n"
191 " float player_shadow = max( 1.0-dist_to_player*1.7, 0.0 );\n"
192 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
193 "\n"
194 " famt = max( player_shadow*0.6, famt );\n"
195 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
196 "}\n"
197 "\n"
198 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
199 "{\n"
200 " float dist = pow(fdist*0.0008,1.2);\n"
201 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
202 "}\n"
203 "\n"
204 "#line 15 0 \n"
205 "#line 1 2 \n"
206 "const float k_motion_lerp_amount = 0.01;\n"
207 "\n"
208 "#line 2 0 \n"
209 "\n"
210 "layout (location = 1) out vec2 oMotionVec;\n"
211 "\n"
212 "in vec3 aMotionVec0;\n"
213 "in vec3 aMotionVec1;\n"
214 "\n"
215 "void compute_motion_vectors()\n"
216 "{\n"
217 " // Write motion vectors\n"
218 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
219 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
220 "\n"
221 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
222 "}\n"
223 "\n"
224 "#line 16 0 \n"
225 "\n"
226 "void main()\n"
227 "{\n"
228 " compute_motion_vectors();\n"
229 "\n"
230 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
231 " vec4 vsamplemain = texture( uTexMain, aUv );\n"
232 " vec3 qnorm = normalize(aNorm);\n"
233 "\n"
234 " if( vsamplemain.a < 0.15 )\n"
235 " discard;\n"
236 "\n"
237 " vfrag = vsamplemain.rgb;\n"
238 "\n"
239 " if( g_light_preview == 1 )\n"
240 " {\n"
241 " vfrag = vec3(0.5);\n"
242 " }\n"
243 "\n"
244 " // Lighting\n"
245 " vec3 halfview = uCamera - aWorldCo;\n"
246 " float fdist = length( halfview );\n"
247 " halfview /= fdist;\n"
248 "\n"
249 " vfrag = do_light_diffuse( vfrag, qnorm );\n"
250 " vfrag = do_light_spec( vfrag, qnorm, halfview, 0.1 );\n"
251 " vfrag = do_light_shadowing( vfrag );\n"
252 " vfrag = apply_fog( vfrag, fdist );\n"
253 "\n"
254 " oColour = vec4(vfrag, 1.0);\n"
255 "}\n"
256 ""},
257 };
258
259 static GLuint _uniform_alphatest_uMdl;
260 static GLuint _uniform_alphatest_uPv;
261 static GLuint _uniform_alphatest_uPvmPrev;
262 static GLuint _uniform_alphatest_uTexGarbage;
263 static GLuint _uniform_alphatest_uTexMain;
264 static GLuint _uniform_alphatest_uBoard0;
265 static GLuint _uniform_alphatest_uBoard1;
266 static GLuint _uniform_alphatest_uCamera;
267 static GLuint _uniform_alphatest_uPlane;
268 static GLuint _uniform_alphatest_g_world_depth;
269 static void shader_alphatest_uMdl(m4x3f m){
270 glUniformMatrix4x3fv(_uniform_alphatest_uMdl,1,GL_FALSE,(float*)m);
271 }
272 static void shader_alphatest_uPv(m4x4f m){
273 glUniformMatrix4fv(_uniform_alphatest_uPv,1,GL_FALSE,(float*)m);
274 }
275 static void shader_alphatest_uPvmPrev(m4x4f m){
276 glUniformMatrix4fv(_uniform_alphatest_uPvmPrev,1,GL_FALSE,(float*)m);
277 }
278 static void shader_alphatest_uTexGarbage(int i){
279 glUniform1i(_uniform_alphatest_uTexGarbage,i);
280 }
281 static void shader_alphatest_uTexMain(int i){
282 glUniform1i(_uniform_alphatest_uTexMain,i);
283 }
284 static void shader_alphatest_uBoard0(v3f v){
285 glUniform3fv(_uniform_alphatest_uBoard0,1,v);
286 }
287 static void shader_alphatest_uBoard1(v3f v){
288 glUniform3fv(_uniform_alphatest_uBoard1,1,v);
289 }
290 static void shader_alphatest_uCamera(v3f v){
291 glUniform3fv(_uniform_alphatest_uCamera,1,v);
292 }
293 static void shader_alphatest_uPlane(v4f v){
294 glUniform4fv(_uniform_alphatest_uPlane,1,v);
295 }
296 static void shader_alphatest_g_world_depth(int i){
297 glUniform1i(_uniform_alphatest_g_world_depth,i);
298 }
299 static void shader_alphatest_register(void){
300 vg_shader_register( &_shader_alphatest );
301 }
302 static void shader_alphatest_use(void){ glUseProgram(_shader_alphatest.id); }
303 static void shader_alphatest_link(void){
304 _uniform_alphatest_uMdl = glGetUniformLocation( _shader_alphatest.id, "uMdl" );
305 _uniform_alphatest_uPv = glGetUniformLocation( _shader_alphatest.id, "uPv" );
306 _uniform_alphatest_uPvmPrev = glGetUniformLocation( _shader_alphatest.id, "uPvmPrev" );
307 _uniform_alphatest_uTexGarbage = glGetUniformLocation( _shader_alphatest.id, "uTexGarbage" );
308 _uniform_alphatest_uTexMain = glGetUniformLocation( _shader_alphatest.id, "uTexMain" );
309 _uniform_alphatest_uBoard0 = glGetUniformLocation( _shader_alphatest.id, "uBoard0" );
310 _uniform_alphatest_uBoard1 = glGetUniformLocation( _shader_alphatest.id, "uBoard1" );
311 _uniform_alphatest_uCamera = glGetUniformLocation( _shader_alphatest.id, "uCamera" );
312 _uniform_alphatest_uPlane = glGetUniformLocation( _shader_alphatest.id, "uPlane" );
313 _uniform_alphatest_g_world_depth = glGetUniformLocation( _shader_alphatest.id, "g_world_depth" );
314 }
315 #endif /* SHADER_alphatest_H */