df shadows
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / viewchar.h
1 #ifndef SHADER_viewchar_H
2 #define SHADER_viewchar_H
3 static void shader_viewchar_link(void);
4 static void shader_viewchar_register(void);
5 static struct vg_shader _shader_viewchar = {
6 .name = "viewchar",
7 .link = shader_viewchar_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 mat4 uPv;\n"
40 "\n"
41 "// TODO: Send a previous transform matrix stack\n"
42 "//\n"
43 "uniform mat4x3 uTransforms[32];\n"
44 "\n"
45 "out vec4 aColour;\n"
46 "out vec2 aUv;\n"
47 "out vec3 aNorm;\n"
48 "out vec3 aCo;\n"
49 "out vec3 aWorldCo;\n"
50 "\n"
51 "void main()\n"
52 "{\n"
53 " vec4 co_local = vec4( a_co, 1.0 );\n"
54 " vec3 co0 = uTransforms[ a_groups[0] ] * co_local;\n"
55 " vec3 co1 = uTransforms[ a_groups[1] ] * co_local;\n"
56 " vec3 co2 = uTransforms[ a_groups[2] ] * co_local;\n"
57 " vec3 n0 = mat3(uTransforms[ a_groups[0] ]) * a_norm;\n"
58 " vec3 n1 = mat3(uTransforms[ a_groups[1] ]) * a_norm;\n"
59 " vec3 n2 = mat3(uTransforms[ a_groups[2] ]) * a_norm;\n"
60 "\n"
61 " vec3 world_pos = co0*a_weights[0] + co1*a_weights[1] + co2*a_weights[2];\n"
62 " vec3 world_normal = n0*a_weights[0] + n1*a_weights[1] + n2*a_weights[2];\n"
63 " \n"
64 " gl_Position = uPv * vec4( world_pos, 1.0 );\n"
65 " aColour = a_colour;\n"
66 " aUv = a_uv;\n"
67 " aNorm = world_normal;\n"
68 " aCo = a_co;\n"
69 " aWorldCo = world_pos;\n"
70 "\n"
71 " // TODO:\n"
72 " aMotionVec0 = vec3(1.0);\n"
73 " aMotionVec1 = vec3(1.0);\n"
74 "}\n"
75 ""},
76 .fs =
77 {
78 .static_src =
79 "uniform sampler2D uTexMain;\n"
80 "uniform vec3 uCamera;\n"
81 "uniform vec3 uBoard0;\n"
82 "uniform vec3 uBoard1;\n"
83 "\n"
84 "in vec4 aColour;\n"
85 "in vec2 aUv;\n"
86 "in vec3 aNorm;\n"
87 "in vec3 aCo;\n"
88 "in vec3 aWorldCo;\n"
89 "\n"
90 "#line 1 1 \n"
91 "layout (location = 0) out vec4 oColour;\n"
92 "\n"
93 "layout (std140) uniform ub_world_lighting\n"
94 "{\n"
95 " vec4 g_light_colours[3];\n"
96 " vec4 g_light_directions[3];\n"
97 " vec4 g_ambient_colour;\n"
98 "\n"
99 " vec4 g_water_plane;\n"
100 " vec4 g_depth_bounds;\n"
101 " float g_water_fog;\n"
102 " int g_light_count;\n"
103 " int g_light_preview;\n"
104 " int g_shadow_samples;\n"
105 "};\n"
106 "\n"
107 "uniform sampler2D g_world_depth;\n"
108 "\n"
109 "// Standard diffuse + spec models\n"
110 "// ==============================\n"
111 "\n"
112 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
113 "{\n"
114 " vec3 vtotal = g_ambient_colour.rgb;\n"
115 "\n"
116 " for( int i=0; i<g_light_count; i++ )\n"
117 " {\n"
118 " vec3 vcolour = g_light_colours[i].rgb;\n"
119 " vec3 vdir = g_light_directions[i].xyz;\n"
120 "\n"
121 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
122 " vtotal += vcolour*flight;\n"
123 " }\n"
124 "\n"
125 " return vfrag * vtotal;\n"
126 "}\n"
127 "\n"
128 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
129 "{\n"
130 " vec3 vcolour = g_light_colours[0].rgb;\n"
131 " vec3 vdir = g_light_directions[0].xyz;\n"
132 "\n"
133 " vec3 specdir = reflect( -vdir, wnormal );\n"
134 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
135 " return vfrag + vcolour*spec*fintensity;\n"
136 "}\n"
137 "\n"
138 "float world_depth_sample( vec3 pos )\n"
139 "{\n"
140 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
141 " return texture( g_world_depth, depth_coord ).r;\n"
142 "}\n"
143 "\n"
144 "float shadow_sample( vec3 vdir )\n"
145 "{\n"
146 " vec3 sample_pos = aWorldCo + vdir;\n"
147 " float height_sample = world_depth_sample( sample_pos );\n"
148 "\n"
149 " float fdelta = height_sample - sample_pos.y;\n"
150 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
151 "}\n"
152 "\n"
153 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
154 "{\n"
155 " float faccum = 0.0;\n"
156 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
157 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
158 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
159 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
160 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
161 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
162 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
163 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
164 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
165 "}\n"
166 "\n"
167 "// FIXME\n"
168 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
169 "{\n"
170 " vec3 pa = p - a;\n"
171 " vec3 ba = b - a;\n"
172 "\n"
173 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
174 " return length( pa - ba*h );\n"
175 "}\n"
176 "\n"
177 "vec3 do_light_shadowing( vec3 vfrag )\n"
178 "{\n"
179 " if( g_shadow_samples == 0 )\n"
180 " {\n"
181 " return vfrag;\n"
182 " }\n"
183 "\n"
184 " float fspread = g_light_colours[0].w;\n"
185 " vec3 vdir = g_light_directions[0].xyz;\n"
186 " float flength = g_light_directions[0].w;\n"
187 "\n"
188 " float famt = 0.0;\n"
189 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
190 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
191 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
192 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
193 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
194 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
195 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
196 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
197 "\n"
198 " // player shadow\n"
199 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.03 );\n"
200 " float player_shadow = max( 1.0-dist_to_player*1.7, 0.0 );\n"
201 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
202 "\n"
203 " famt = max( player_shadow*0.6, famt );\n"
204 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
205 "}\n"
206 "\n"
207 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
208 "{\n"
209 " float dist = pow(fdist*0.0008,1.2);\n"
210 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
211 "}\n"
212 "\n"
213 "#line 13 0 \n"
214 "#line 1 2 \n"
215 "const float k_motion_lerp_amount = 0.01;\n"
216 "\n"
217 "#line 2 0 \n"
218 "\n"
219 "layout (location = 1) out vec2 oMotionVec;\n"
220 "\n"
221 "in vec3 aMotionVec0;\n"
222 "in vec3 aMotionVec1;\n"
223 "\n"
224 "void compute_motion_vectors()\n"
225 "{\n"
226 " // Write motion vectors\n"
227 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
228 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
229 "\n"
230 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
231 "}\n"
232 "\n"
233 "#line 14 0 \n"
234 "\n"
235 "void main()\n"
236 "{\n"
237 " compute_motion_vectors();\n"
238 "\n"
239 " vec3 vfrag = texture( uTexMain, aUv ).rgb;\n"
240 "\n"
241 " // Lighting\n"
242 " vec3 halfview = uCamera - aWorldCo;\n"
243 " float fdist = length( halfview );\n"
244 " halfview /= fdist;\n"
245 " fdist -= 0.08;\n"
246 "\n"
247 " vec3 qnorm = normalize(floor(aNorm*2.0)*0.5) + vec3(0.001,0.0,0.0);\n"
248 "\n"
249 " vfrag = mix( vfrag, do_light_diffuse( vfrag, qnorm ), 0.5 );\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 " float opacity = clamp( fdist*fdist, 0.0, 1.0 );\n"
255 " oColour = vec4(vfrag,opacity);\n"
256 "}\n"
257 ""},
258 };
259
260 static GLuint _uniform_viewchar_uPv;
261 static GLuint _uniform_viewchar_uTransforms;
262 static GLuint _uniform_viewchar_uTexMain;
263 static GLuint _uniform_viewchar_uCamera;
264 static GLuint _uniform_viewchar_uBoard0;
265 static GLuint _uniform_viewchar_uBoard1;
266 static GLuint _uniform_viewchar_g_world_depth;
267 static void shader_viewchar_uPv(m4x4f m){
268 glUniformMatrix4fv(_uniform_viewchar_uPv,1,GL_FALSE,(float*)m);
269 }
270 static void shader_viewchar_uTexMain(int i){
271 glUniform1i(_uniform_viewchar_uTexMain,i);
272 }
273 static void shader_viewchar_uCamera(v3f v){
274 glUniform3fv(_uniform_viewchar_uCamera,1,v);
275 }
276 static void shader_viewchar_uBoard0(v3f v){
277 glUniform3fv(_uniform_viewchar_uBoard0,1,v);
278 }
279 static void shader_viewchar_uBoard1(v3f v){
280 glUniform3fv(_uniform_viewchar_uBoard1,1,v);
281 }
282 static void shader_viewchar_g_world_depth(int i){
283 glUniform1i(_uniform_viewchar_g_world_depth,i);
284 }
285 static void shader_viewchar_register(void){
286 vg_shader_register( &_shader_viewchar );
287 }
288 static void shader_viewchar_use(void){ glUseProgram(_shader_viewchar.id); }
289 static void shader_viewchar_link(void){
290 _uniform_viewchar_uPv = glGetUniformLocation( _shader_viewchar.id, "uPv" );
291 _uniform_viewchar_uTransforms = glGetUniformLocation( _shader_viewchar.id, "uTransforms" );
292 _uniform_viewchar_uTexMain = glGetUniformLocation( _shader_viewchar.id, "uTexMain" );
293 _uniform_viewchar_uCamera = glGetUniformLocation( _shader_viewchar.id, "uCamera" );
294 _uniform_viewchar_uBoard0 = glGetUniformLocation( _shader_viewchar.id, "uBoard0" );
295 _uniform_viewchar_uBoard1 = glGetUniformLocation( _shader_viewchar.id, "uBoard1" );
296 _uniform_viewchar_g_world_depth = glGetUniformLocation( _shader_viewchar.id, "g_world_depth" );
297 }
298 #endif /* SHADER_viewchar_H */