df shadows
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / gpos.h
1 #ifndef SHADER_gpos_H
2 #define SHADER_gpos_H
3 static void shader_gpos_link(void);
4 static void shader_gpos_register(void);
5 static struct vg_shader _shader_gpos = {
6 .name = "gpos",
7 .link = shader_gpos_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 "out vec4 FragColor;\n"
69 "\n"
70 "uniform vec3 uCamera;\n"
71 "uniform vec3 uBoard0;\n"
72 "uniform vec3 uBoard1;\n"
73 "\n"
74 "in vec4 aColour;\n"
75 "in vec2 aUv;\n"
76 "in vec3 aNorm;\n"
77 "in vec3 aCo;\n"
78 "in vec3 aWorldCo;\n"
79 "\n"
80 "#line 1 1 \n"
81 "layout (location = 0) out vec4 oColour;\n"
82 "\n"
83 "layout (std140) uniform ub_world_lighting\n"
84 "{\n"
85 " vec4 g_light_colours[3];\n"
86 " vec4 g_light_directions[3];\n"
87 " vec4 g_ambient_colour;\n"
88 "\n"
89 " vec4 g_water_plane;\n"
90 " vec4 g_depth_bounds;\n"
91 " float g_water_fog;\n"
92 " int g_light_count;\n"
93 " int g_light_preview;\n"
94 " int g_shadow_samples;\n"
95 "};\n"
96 "\n"
97 "uniform sampler2D g_world_depth;\n"
98 "\n"
99 "// Standard diffuse + spec models\n"
100 "// ==============================\n"
101 "\n"
102 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
103 "{\n"
104 " vec3 vtotal = g_ambient_colour.rgb;\n"
105 "\n"
106 " for( int i=0; i<g_light_count; i++ )\n"
107 " {\n"
108 " vec3 vcolour = g_light_colours[i].rgb;\n"
109 " vec3 vdir = g_light_directions[i].xyz;\n"
110 "\n"
111 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
112 " vtotal += vcolour*flight;\n"
113 " }\n"
114 "\n"
115 " return vfrag * vtotal;\n"
116 "}\n"
117 "\n"
118 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
119 "{\n"
120 " vec3 vcolour = g_light_colours[0].rgb;\n"
121 " vec3 vdir = g_light_directions[0].xyz;\n"
122 "\n"
123 " vec3 specdir = reflect( -vdir, wnormal );\n"
124 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
125 " return vfrag + vcolour*spec*fintensity;\n"
126 "}\n"
127 "\n"
128 "float world_depth_sample( vec3 pos )\n"
129 "{\n"
130 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
131 " return texture( g_world_depth, depth_coord ).r;\n"
132 "}\n"
133 "\n"
134 "float shadow_sample( vec3 vdir )\n"
135 "{\n"
136 " vec3 sample_pos = aWorldCo + vdir;\n"
137 " float height_sample = world_depth_sample( sample_pos );\n"
138 "\n"
139 " float fdelta = height_sample - sample_pos.y;\n"
140 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
141 "}\n"
142 "\n"
143 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
144 "{\n"
145 " float faccum = 0.0;\n"
146 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
147 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
148 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
149 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
150 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
151 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
152 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
153 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
154 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
155 "}\n"
156 "\n"
157 "// FIXME\n"
158 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
159 "{\n"
160 " vec3 pa = p - a;\n"
161 " vec3 ba = b - a;\n"
162 "\n"
163 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
164 " return length( pa - ba*h );\n"
165 "}\n"
166 "\n"
167 "vec3 do_light_shadowing( vec3 vfrag )\n"
168 "{\n"
169 " if( g_shadow_samples == 0 )\n"
170 " {\n"
171 " return vfrag;\n"
172 " }\n"
173 "\n"
174 " float fspread = g_light_colours[0].w;\n"
175 " vec3 vdir = g_light_directions[0].xyz;\n"
176 " float flength = g_light_directions[0].w;\n"
177 "\n"
178 " float famt = 0.0;\n"
179 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
180 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
181 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
182 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
183 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
184 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
185 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
186 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
187 "\n"
188 " // player shadow\n"
189 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.03 );\n"
190 " float player_shadow = max( 1.0-dist_to_player*1.7, 0.0 );\n"
191 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
192 "\n"
193 " famt = max( player_shadow*0.6, famt );\n"
194 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
195 "}\n"
196 "\n"
197 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
198 "{\n"
199 " float dist = pow(fdist*0.0008,1.2);\n"
200 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
201 "}\n"
202 "\n"
203 "#line 14 0 \n"
204 "\n"
205 "// Water blending\n"
206 "// ==============\n"
207 "\n"
208 "float water_depth( vec3 pos, vec3 halfview )\n"
209 "{\n"
210 " vec3 pnorm = g_water_plane.xyz;\n"
211 " float pdist = g_water_plane.w;\n"
212 "\n"
213 " float d = dot( pnorm, halfview );\n"
214 " float t = dot((pnorm*pdist - pos), pnorm) / d;\n"
215 " return t * g_water_fog;\n"
216 "}\n"
217 "\n"
218 "void main()\n"
219 "{\n"
220 " vec3 halfview = normalize( uCamera - aCo );\n"
221 " vec3 world_pos = vec3( aCo.y, aCo.x, aCo.z );\n"
222 " FragColor = vec4( world_pos, water_depth( aCo, halfview ) );\n"
223 "}\n"
224 ""},
225 };
226
227 static GLuint _uniform_gpos_uMdl;
228 static GLuint _uniform_gpos_uPv;
229 static GLuint _uniform_gpos_uPvmPrev;
230 static GLuint _uniform_gpos_uCamera;
231 static GLuint _uniform_gpos_uBoard0;
232 static GLuint _uniform_gpos_uBoard1;
233 static GLuint _uniform_gpos_g_world_depth;
234 static void shader_gpos_uMdl(m4x3f m){
235 glUniformMatrix4x3fv(_uniform_gpos_uMdl,1,GL_FALSE,(float*)m);
236 }
237 static void shader_gpos_uPv(m4x4f m){
238 glUniformMatrix4fv(_uniform_gpos_uPv,1,GL_FALSE,(float*)m);
239 }
240 static void shader_gpos_uPvmPrev(m4x4f m){
241 glUniformMatrix4fv(_uniform_gpos_uPvmPrev,1,GL_FALSE,(float*)m);
242 }
243 static void shader_gpos_uCamera(v3f v){
244 glUniform3fv(_uniform_gpos_uCamera,1,v);
245 }
246 static void shader_gpos_uBoard0(v3f v){
247 glUniform3fv(_uniform_gpos_uBoard0,1,v);
248 }
249 static void shader_gpos_uBoard1(v3f v){
250 glUniform3fv(_uniform_gpos_uBoard1,1,v);
251 }
252 static void shader_gpos_g_world_depth(int i){
253 glUniform1i(_uniform_gpos_g_world_depth,i);
254 }
255 static void shader_gpos_register(void){
256 vg_shader_register( &_shader_gpos );
257 }
258 static void shader_gpos_use(void){ glUseProgram(_shader_gpos.id); }
259 static void shader_gpos_link(void){
260 _uniform_gpos_uMdl = glGetUniformLocation( _shader_gpos.id, "uMdl" );
261 _uniform_gpos_uPv = glGetUniformLocation( _shader_gpos.id, "uPv" );
262 _uniform_gpos_uPvmPrev = glGetUniformLocation( _shader_gpos.id, "uPvmPrev" );
263 _uniform_gpos_uCamera = glGetUniformLocation( _shader_gpos.id, "uCamera" );
264 _uniform_gpos_uBoard0 = glGetUniformLocation( _shader_gpos.id, "uBoard0" );
265 _uniform_gpos_uBoard1 = glGetUniformLocation( _shader_gpos.id, "uBoard1" );
266 _uniform_gpos_g_world_depth = glGetUniformLocation( _shader_gpos.id, "g_world_depth" );
267 }
268 #endif /* SHADER_gpos_H */