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