fix da bugs
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / scene_depth.h
1 #ifndef SHADER_scene_depth_H
2 #define SHADER_scene_depth_H
3 static void shader_scene_depth_link(void);
4 static void shader_scene_depth_register(void);
5 static struct vg_shader _shader_scene_depth = {
6 .name = "scene_depth",
7 .link = shader_scene_depth_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 "out vec4 FragColor;\n"
67 "\n"
68 "uniform vec3 uCamera;\n"
69 "uniform vec3 uBoard0;\n"
70 "uniform vec3 uBoard1;\n"
71 "\n"
72 "in vec2 aUv;\n"
73 "in vec4 aNorm;\n"
74 "in vec3 aCo;\n"
75 "in vec3 aWorldCo;\n"
76 "flat in ivec4 aLights;\n"
77 "\n"
78 "#line 1 1 \n"
79 "// :D\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 " vec4 g_point_light_positions[32];\n"
98 " vec4 g_point_light_colours[32];\n"
99 "};\n"
100 "\n"
101 "uniform sampler2D g_world_depth;\n"
102 "\n"
103 "float world_depth_sample( vec3 pos )\n"
104 "{\n"
105 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
106 " return texture( g_world_depth, depth_coord ).r;\n"
107 "}\n"
108 "\n"
109 "float shadow_sample( vec3 vdir )\n"
110 "{\n"
111 " vec3 sample_pos = aWorldCo + vdir;\n"
112 " float height_sample = world_depth_sample( sample_pos );\n"
113 "\n"
114 " float fdelta = height_sample - sample_pos.y;\n"
115 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
116 "}\n"
117 "\n"
118 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
119 "{\n"
120 " vec3 pa = p - a;\n"
121 " vec3 ba = b - a;\n"
122 "\n"
123 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
124 " return length( pa - ba*h );\n"
125 "}\n"
126 "\n"
127 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
128 "{\n"
129 " float dist = pow(fdist*0.0008,1.2);\n"
130 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
131 "}\n"
132 "\n"
133 "\n"
134 "// New lighting model\n"
135 "\n"
136 "vec3 newlight_compute_ambient()\n"
137 "{\n"
138 " return g_ambient_colour.rgb;\n"
139 "}\n"
140 "\n"
141 "float newlight_compute_sun_shadow()\n"
142 "{\n"
143 " if( g_shadow_samples == 0 )\n"
144 " {\n"
145 " return 1.0;\n"
146 " }\n"
147 "\n"
148 " float fspread = g_light_colours[0].w;\n"
149 " vec3 vdir = g_light_directions[0].xyz;\n"
150 " float flength = g_light_directions[0].w;\n"
151 "\n"
152 " float famt = 0.0;\n"
153 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
154 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
155 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
156 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
157 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
158 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
159 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
160 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
161 "\n"
162 " // player shadow\n"
163 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 );\n"
164 " float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );\n"
165 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
166 "\n"
167 " return 1.0 - max( player_shadow*0.8, famt );\n"
168 "}\n"
169 "\n"
170 "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n"
171 "{\n"
172 " vec3 vtotal = g_ambient_colour.rgb;\n"
173 "\n"
174 " for( int i=0; i<g_light_count; i++ )\n"
175 " {\n"
176 " vec3 vcolour = g_light_colours[i].rgb;\n"
177 " vec3 vdir = g_light_directions[i].xyz;\n"
178 "\n"
179 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
180 " vtotal += vcolour*flight;\n"
181 " }\n"
182 "\n"
183 " return vtotal;\n"
184 "}\n"
185 "\n"
186 "vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )\n"
187 "{\n"
188 " vec3 vcolour = g_light_colours[0].rgb;\n"
189 " vec3 vdir = g_light_directions[0].xyz;\n"
190 "\n"
191 " vec3 specdir = reflect( -vdir, wnormal );\n"
192 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
193 " return vcolour*spec*fintensity;\n"
194 "}\n"
195 "\n"
196 "vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, \n"
197 " vec3 light_pos, vec3 light_colour )\n"
198 "{\n"
199 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
200 "\n"
201 " float quadratic = dot(light_delta,light_delta);\n"
202 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
203 " attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );\n"
204 "\n"
205 " return light_colour*attenuation;\n"
206 "}\n"
207 "\n"
208 "#line 4 0 \n"
209 "\n"
210 "vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )\n"
211 "{\n"
212 " // Lighting\n"
213 " vec3 halfview = uCamera - aWorldCo;\n"
214 " float fdist = length(halfview);\n"
215 " halfview /= fdist;\n"
216 "\n"
217 " vec3 total_light = newlight_compute_ambient();\n"
218 " \n"
219 " // Compute world lighting contribution and apply it according to the\n"
220 " // shadow map\n"
221 " //\n"
222 " vec3 world_light = newlight_compute_world_diffuse( wnormal );\n"
223 " world_light += newlight_compute_sun_spec( wnormal, halfview, 0.1 );\n"
224 "\n"
225 " float world_shadow = newlight_compute_sun_shadow();\n"
226 "\n"
227 " total_light += world_light * world_shadow;\n"
228 "\n"
229 " // Compute the other lights that exist in the map, not effected by the sun\n"
230 " // shadow\n"
231 " total_light += newlight_compute_quadratic\n"
232 " ( \n"
233 " wnormal, halfview,\n"
234 " g_point_light_positions[ aLights.x ].xyz,\n"
235 " g_point_light_colours[ aLights.x ].rgb \n"
236 " );\n"
237 " total_light += newlight_compute_quadratic\n"
238 " ( \n"
239 " wnormal, halfview,\n"
240 " g_point_light_positions[ aLights.y ].xyz,\n"
241 " g_point_light_colours[ aLights.y ].rgb \n"
242 " );\n"
243 " total_light += newlight_compute_quadratic\n"
244 " ( \n"
245 " wnormal, halfview,\n"
246 " g_point_light_positions[ aLights.z ].xyz,\n"
247 " g_point_light_colours[ aLights.z ].rgb \n"
248 " );\n"
249 "\n"
250 " return apply_fog( diffuse * total_light, fdist );\n"
251 "}\n"
252 "\n"
253 "#line 14 0 \n"
254 "\n"
255 "// Water blending\n"
256 "// ==============\n"
257 "\n"
258 "float water_depth( vec3 pos, vec3 halfview )\n"
259 "{\n"
260 " vec3 pnorm = g_water_plane.xyz;\n"
261 " float pdist = g_water_plane.w;\n"
262 "\n"
263 " float d = dot( pnorm, halfview );\n"
264 " float t = dot((pnorm*pdist - pos), pnorm) / d;\n"
265 " return t * g_water_fog;\n"
266 "}\n"
267 "\n"
268 "void main()\n"
269 "{\n"
270 " vec3 halfview = normalize( uCamera - aWorldCo );\n"
271 " float depth = water_depth( aWorldCo, halfview );\n"
272 " FragColor = vec4( aWorldCo.y, 0.0, 0.0,depth );\n"
273 "}\n"
274 ""},
275 };
276
277 static GLuint _uniform_scene_depth_uMdl;
278 static GLuint _uniform_scene_depth_uPv;
279 static GLuint _uniform_scene_depth_uPvmPrev;
280 static GLuint _uniform_scene_depth_uCamera;
281 static GLuint _uniform_scene_depth_uBoard0;
282 static GLuint _uniform_scene_depth_uBoard1;
283 static GLuint _uniform_scene_depth_g_world_depth;
284 static void shader_scene_depth_uMdl(m4x3f m){
285 glUniformMatrix4x3fv(_uniform_scene_depth_uMdl,1,GL_FALSE,(float*)m);
286 }
287 static void shader_scene_depth_uPv(m4x4f m){
288 glUniformMatrix4fv(_uniform_scene_depth_uPv,1,GL_FALSE,(float*)m);
289 }
290 static void shader_scene_depth_uPvmPrev(m4x4f m){
291 glUniformMatrix4fv(_uniform_scene_depth_uPvmPrev,1,GL_FALSE,(float*)m);
292 }
293 static void shader_scene_depth_uCamera(v3f v){
294 glUniform3fv(_uniform_scene_depth_uCamera,1,v);
295 }
296 static void shader_scene_depth_uBoard0(v3f v){
297 glUniform3fv(_uniform_scene_depth_uBoard0,1,v);
298 }
299 static void shader_scene_depth_uBoard1(v3f v){
300 glUniform3fv(_uniform_scene_depth_uBoard1,1,v);
301 }
302 static void shader_scene_depth_g_world_depth(int i){
303 glUniform1i(_uniform_scene_depth_g_world_depth,i);
304 }
305 static void shader_scene_depth_register(void){
306 vg_shader_register( &_shader_scene_depth );
307 }
308 static void shader_scene_depth_use(void){ glUseProgram(_shader_scene_depth.id); }
309 static void shader_scene_depth_link(void){
310 _uniform_scene_depth_uMdl = glGetUniformLocation( _shader_scene_depth.id, "uMdl" );
311 _uniform_scene_depth_uPv = glGetUniformLocation( _shader_scene_depth.id, "uPv" );
312 _uniform_scene_depth_uPvmPrev = glGetUniformLocation( _shader_scene_depth.id, "uPvmPrev" );
313 _uniform_scene_depth_uCamera = glGetUniformLocation( _shader_scene_depth.id, "uCamera" );
314 _uniform_scene_depth_uBoard0 = glGetUniformLocation( _shader_scene_depth.id, "uBoard0" );
315 _uniform_scene_depth_uBoard1 = glGetUniformLocation( _shader_scene_depth.id, "uBoard1" );
316 _uniform_scene_depth_g_world_depth = glGetUniformLocation( _shader_scene_depth.id, "g_world_depth" );
317 }
318 #endif /* SHADER_scene_depth_H */