53b2bfafaf22ebf33896abb1e4cc4dbef2050fcf
[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 "layout (location = 0) out vec4 oColour;\n"
80 "\n"
81 "layout (std140) uniform ub_world_lighting\n"
82 "{\n"
83 " vec4 g_light_colours[3];\n"
84 " vec4 g_light_directions[3];\n"
85 " vec4 g_ambient_colour;\n"
86 "\n"
87 " vec4 g_water_plane;\n"
88 " vec4 g_depth_bounds;\n"
89 " float g_water_fog;\n"
90 " int g_light_count;\n"
91 " int g_light_preview;\n"
92 " int g_shadow_samples;\n"
93 "\n"
94 " vec4 g_point_light_positions[32];\n"
95 " vec4 g_point_light_colours[32];\n"
96 "};\n"
97 "\n"
98 "uniform sampler2D g_world_depth;\n"
99 "\n"
100 "float world_depth_sample( vec3 pos )\n"
101 "{\n"
102 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
103 " return texture( g_world_depth, depth_coord ).r;\n"
104 "}\n"
105 "\n"
106 "float shadow_sample( vec3 vdir )\n"
107 "{\n"
108 " vec3 sample_pos = aWorldCo + vdir;\n"
109 " float height_sample = world_depth_sample( sample_pos );\n"
110 "\n"
111 " float fdelta = height_sample - sample_pos.y;\n"
112 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
113 "}\n"
114 "\n"
115 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
116 "{\n"
117 " vec3 pa = p - a;\n"
118 " vec3 ba = b - a;\n"
119 "\n"
120 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
121 " return length( pa - ba*h );\n"
122 "}\n"
123 "\n"
124 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
125 "{\n"
126 " float dist = pow(fdist*0.0008,1.2);\n"
127 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
128 "}\n"
129 "\n"
130 "\n"
131 "// New lighting model\n"
132 "\n"
133 "vec3 newlight_compute_ambient()\n"
134 "{\n"
135 " return g_ambient_colour.rgb;\n"
136 "}\n"
137 "\n"
138 "float newlight_compute_sun_shadow()\n"
139 "{\n"
140 " if( g_shadow_samples == 0 )\n"
141 " {\n"
142 " return 1.0;\n"
143 " }\n"
144 "\n"
145 " float fspread = g_light_colours[0].w;\n"
146 " vec3 vdir = g_light_directions[0].xyz;\n"
147 " float flength = g_light_directions[0].w;\n"
148 "\n"
149 " float famt = 0.0;\n"
150 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
151 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
152 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
153 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
154 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
155 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
156 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
157 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
158 "\n"
159 " // player shadow\n"
160 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 );\n"
161 " float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );\n"
162 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
163 "\n"
164 " return 1.0 - max( player_shadow*0.8, famt );\n"
165 "}\n"
166 "\n"
167 "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n"
168 "{\n"
169 " vec3 vtotal = g_ambient_colour.rgb;\n"
170 "\n"
171 " for( int i=0; i<g_light_count; i++ )\n"
172 " {\n"
173 " vec3 vcolour = g_light_colours[i].rgb;\n"
174 " vec3 vdir = g_light_directions[i].xyz;\n"
175 "\n"
176 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
177 " vtotal += vcolour*flight;\n"
178 " }\n"
179 "\n"
180 " return vtotal;\n"
181 "}\n"
182 "\n"
183 "vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )\n"
184 "{\n"
185 " vec3 vcolour = g_light_colours[0].rgb;\n"
186 " vec3 vdir = g_light_directions[0].xyz;\n"
187 "\n"
188 " vec3 specdir = reflect( -vdir, wnormal );\n"
189 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
190 " return vcolour*spec*fintensity;\n"
191 "}\n"
192 "\n"
193 "vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, \n"
194 " vec3 light_pos, vec3 light_colour )\n"
195 "{\n"
196 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
197 "\n"
198 " float quadratic = dot(light_delta,light_delta);\n"
199 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
200 " attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );\n"
201 "\n"
202 " return light_colour*attenuation;\n"
203 "}\n"
204 "\n"
205 "#line 14 0 \n"
206 "\n"
207 "// Water blending\n"
208 "// ==============\n"
209 "\n"
210 "float water_depth( vec3 pos, vec3 halfview )\n"
211 "{\n"
212 " vec3 pnorm = g_water_plane.xyz;\n"
213 " float pdist = g_water_plane.w;\n"
214 "\n"
215 " float d = dot( pnorm, halfview );\n"
216 " float t = dot((pnorm*pdist - pos), pnorm) / d;\n"
217 " return t * g_water_fog;\n"
218 "}\n"
219 "\n"
220 "void main()\n"
221 "{\n"
222 " vec3 halfview = normalize( uCamera - aCo );\n"
223 " vec3 world_pos = vec3( aCo.y, aCo.x, aCo.z );\n"
224 " FragColor = vec4( world_pos, water_depth( aCo, halfview ) );\n"
225 "}\n"
226 ""},
227 };
228
229 static GLuint _uniform_scene_depth_uMdl;
230 static GLuint _uniform_scene_depth_uPv;
231 static GLuint _uniform_scene_depth_uPvmPrev;
232 static GLuint _uniform_scene_depth_uCamera;
233 static GLuint _uniform_scene_depth_uBoard0;
234 static GLuint _uniform_scene_depth_uBoard1;
235 static GLuint _uniform_scene_depth_g_world_depth;
236 static void shader_scene_depth_uMdl(m4x3f m){
237 glUniformMatrix4x3fv(_uniform_scene_depth_uMdl,1,GL_FALSE,(float*)m);
238 }
239 static void shader_scene_depth_uPv(m4x4f m){
240 glUniformMatrix4fv(_uniform_scene_depth_uPv,1,GL_FALSE,(float*)m);
241 }
242 static void shader_scene_depth_uPvmPrev(m4x4f m){
243 glUniformMatrix4fv(_uniform_scene_depth_uPvmPrev,1,GL_FALSE,(float*)m);
244 }
245 static void shader_scene_depth_uCamera(v3f v){
246 glUniform3fv(_uniform_scene_depth_uCamera,1,v);
247 }
248 static void shader_scene_depth_uBoard0(v3f v){
249 glUniform3fv(_uniform_scene_depth_uBoard0,1,v);
250 }
251 static void shader_scene_depth_uBoard1(v3f v){
252 glUniform3fv(_uniform_scene_depth_uBoard1,1,v);
253 }
254 static void shader_scene_depth_g_world_depth(int i){
255 glUniform1i(_uniform_scene_depth_g_world_depth,i);
256 }
257 static void shader_scene_depth_register(void){
258 vg_shader_register( &_shader_scene_depth );
259 }
260 static void shader_scene_depth_use(void){ glUseProgram(_shader_scene_depth.id); }
261 static void shader_scene_depth_link(void){
262 _uniform_scene_depth_uMdl = glGetUniformLocation( _shader_scene_depth.id, "uMdl" );
263 _uniform_scene_depth_uPv = glGetUniformLocation( _shader_scene_depth.id, "uPv" );
264 _uniform_scene_depth_uPvmPrev = glGetUniformLocation( _shader_scene_depth.id, "uPvmPrev" );
265 _uniform_scene_depth_uCamera = glGetUniformLocation( _shader_scene_depth.id, "uCamera" );
266 _uniform_scene_depth_uBoard0 = glGetUniformLocation( _shader_scene_depth.id, "uBoard0" );
267 _uniform_scene_depth_uBoard1 = glGetUniformLocation( _shader_scene_depth.id, "uBoard1" );
268 _uniform_scene_depth_g_world_depth = glGetUniformLocation( _shader_scene_depth.id, "g_world_depth" );
269 }
270 #endif /* SHADER_scene_depth_H */