better low qual mode
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / route.h
1 #ifndef SHADER_route_H
2 #define SHADER_route_H
3 static void shader_route_link(void);
4 static void shader_route_register(void);
5 static struct vg_shader _shader_route = {
6 .name = "route",
7 .link = shader_route_link,
8 .vs =
9 {
10 .orig_file = "../../shaders/standard.vs",
11 .static_src =
12 "layout (location=0) in vec3 a_co;\n"
13 "layout (location=1) in vec3 a_norm;\n"
14 "layout (location=2) in vec2 a_uv;\n"
15 "layout (location=3) in vec4 a_colour;\n"
16 "layout (location=4) in vec4 a_weights;\n"
17 "layout (location=5) in ivec4 a_groups;\n"
18 "\n"
19 "#line 2 0 \n"
20 "\n"
21 "uniform mat4x3 uMdl;\n"
22 "uniform mat4 uPv;\n"
23 "\n"
24 "out vec4 aColour;\n"
25 "out vec2 aUv;\n"
26 "out vec3 aNorm;\n"
27 "out vec3 aCo;\n"
28 "out vec3 aWorldCo;\n"
29 "\n"
30 "void main()\n"
31 "{\n"
32 " vec3 world_pos = uMdl * vec4(a_co,1.0);\n"
33 " gl_Position = uPv * vec4( world_pos, 1.0 );\n"
34 " aColour = a_colour;\n"
35 " aUv = a_uv;\n"
36 " aNorm = mat3(uMdl) * a_norm;\n"
37 " aCo = a_co;\n"
38 " aWorldCo = world_pos;\n"
39 "}\n"
40 ""},
41 .fs =
42 {
43 .orig_file = "../../shaders/route.fs",
44 .static_src =
45 "out vec4 FragColor;\n"
46 "\n"
47 "uniform sampler2D uTexGarbage;\n"
48 "uniform sampler2D uTexGradients;\n"
49 "uniform vec3 uCamera;\n"
50 "uniform vec4 uColour;\n"
51 "\n"
52 "in vec4 aColour;\n"
53 "in vec2 aUv;\n"
54 "in vec3 aNorm;\n"
55 "in vec3 aCo;\n"
56 "in vec3 aWorldCo;\n"
57 "\n"
58 "#line 1 1 \n"
59 "layout (std140) uniform ub_world_lighting\n"
60 "{\n"
61 " vec4 g_light_colours[3];\n"
62 " vec4 g_light_directions[3];\n"
63 " vec4 g_ambient_colour;\n"
64 "\n"
65 " vec4 g_water_plane;\n"
66 " vec4 g_depth_bounds;\n"
67 " float g_water_fog;\n"
68 " int g_light_count;\n"
69 " int g_light_preview;\n"
70 " int g_shadow_samples;\n"
71 "};\n"
72 "\n"
73 "uniform sampler2D g_world_depth;\n"
74 "\n"
75 "// Standard diffuse + spec models\n"
76 "// ==============================\n"
77 "\n"
78 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
79 "{\n"
80 " vec3 vtotal = g_ambient_colour.rgb;\n"
81 "\n"
82 " for( int i=0; i<g_light_count; i++ )\n"
83 " {\n"
84 " vec3 vcolour = g_light_colours[i].rgb;\n"
85 " vec3 vdir = g_light_directions[i].xyz;\n"
86 "\n"
87 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
88 " vtotal += vcolour*flight;\n"
89 " }\n"
90 "\n"
91 " return vfrag * vtotal;\n"
92 "}\n"
93 "\n"
94 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
95 "{\n"
96 " vec3 vcolour = g_light_colours[0].rgb;\n"
97 " vec3 vdir = g_light_directions[0].xyz;\n"
98 "\n"
99 " vec3 specdir = reflect( -vdir, wnormal );\n"
100 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
101 " return vfrag + vcolour*spec*fintensity;\n"
102 "}\n"
103 "\n"
104 "float world_depth_sample( vec3 pos )\n"
105 "{\n"
106 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
107 " return texture( g_world_depth, depth_coord ).r;\n"
108 "}\n"
109 "\n"
110 "float shadow_sample( vec3 vdir )\n"
111 "{\n"
112 " vec3 sample_pos = aWorldCo + vdir;\n"
113 " float height_sample = world_depth_sample( sample_pos );\n"
114 "\n"
115 " float fdelta = height_sample - sample_pos.y;\n"
116 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
117 "}\n"
118 "\n"
119 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
120 "{\n"
121 " float faccum = 0.0;\n"
122 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
123 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
124 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
125 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
126 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
127 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
128 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
129 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
130 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
131 "}\n"
132 "\n"
133 "vec3 do_light_shadowing( vec3 vfrag )\n"
134 "{\n"
135 " if( g_shadow_samples == 0 )\n"
136 " {\n"
137 " return vfrag;\n"
138 " }\n"
139 "\n"
140 " float fspread = g_light_colours[0].w;\n"
141 " vec3 vdir = g_light_directions[0].xyz;\n"
142 " float flength = g_light_directions[0].w;\n"
143 "\n"
144 " float famt = 0.0;\n"
145 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
146 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
147 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
148 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
149 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
150 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
151 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
152 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
153 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
154 "}\n"
155 "\n"
156 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
157 "{\n"
158 " float dist = pow(fdist*0.0008,1.2);\n"
159 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
160 "}\n"
161 "\n"
162 "#line 15 0 \n"
163 "\n"
164 "void main()\n"
165 "{\n"
166 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
167 "\n"
168 " // ws modulation\n"
169 " vec4 wgarbage = texture( uTexGarbage, aCo.xz * 0.015 );\n"
170 "\n"
171 " // Creating normal patches\n"
172 " vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;\n"
173 " vec3 qnorm = normalize(floor(aNorm*4.0+modnorm)*0.25) + vec3(0.001,0.0,0.0);\n"
174 "\n"
175 " vec3 tangent0 = normalize(cross(qnorm,vec3(0.0,1.0,0.0)));\n"
176 " vec3 tangent1 = cross(qnorm,tangent0);\n"
177 " vec2 uvdiffuse = vec2( dot(tangent0,aCo), dot(tangent1,aCo) ) * 0.035;\n"
178 " \n"
179 " // Patch local noise\n"
180 " vec4 rgarbage = texture( uTexGarbage, uvdiffuse );\n"
181 "\n"
182 " vfrag = pow(uColour.rgb,vec3(1.0/2.2));\n"
183 " vfrag -= rgarbage.a*0.1;\n"
184 "\n"
185 " if( wgarbage.g < 0.3 )\n"
186 " discard;\n"
187 "\n"
188 " if( g_light_preview == 1 )\n"
189 " {\n"
190 " vfrag = vec3(0.5);\n"
191 " }\n"
192 "\n"
193 " // Lighting\n"
194 " vec3 halfview = uCamera - aWorldCo;\n"
195 " float fdist = length( halfview );\n"
196 " halfview /= fdist;\n"
197 "\n"
198 " vfrag = do_light_diffuse( vfrag, qnorm );\n"
199 " vfrag = do_light_spec( vfrag, qnorm, halfview, 0.1 );\n"
200 " vfrag = do_light_shadowing( vfrag );\n"
201 " vfrag = apply_fog( vfrag, fdist );\n"
202 "\n"
203 " FragColor = vec4(vfrag, 1.0 );\n"
204 "}\n"
205 ""},
206 };
207
208 static GLuint _uniform_route_uMdl;
209 static GLuint _uniform_route_uPv;
210 static GLuint _uniform_route_uTexGarbage;
211 static GLuint _uniform_route_uTexGradients;
212 static GLuint _uniform_route_uCamera;
213 static GLuint _uniform_route_uColour;
214 static GLuint _uniform_route_g_world_depth;
215 static void shader_route_uMdl(m4x3f m){
216 glUniformMatrix4x3fv( _uniform_route_uMdl, 1, GL_FALSE, (float *)m );
217 }
218 static void shader_route_uPv(m4x4f m){
219 glUniformMatrix4fv( _uniform_route_uPv, 1, GL_FALSE, (float *)m );
220 }
221 static void shader_route_uTexGarbage(int i){
222 glUniform1i( _uniform_route_uTexGarbage, i );
223 }
224 static void shader_route_uTexGradients(int i){
225 glUniform1i( _uniform_route_uTexGradients, i );
226 }
227 static void shader_route_uCamera(v3f v){
228 glUniform3fv( _uniform_route_uCamera, 1, v );
229 }
230 static void shader_route_uColour(v4f v){
231 glUniform4fv( _uniform_route_uColour, 1, v );
232 }
233 static void shader_route_g_world_depth(int i){
234 glUniform1i( _uniform_route_g_world_depth, i );
235 }
236 static void shader_route_register(void){
237 vg_shader_register( &_shader_route );
238 }
239 static void shader_route_use(void){ glUseProgram(_shader_route.id); }
240 static void shader_route_link(void){
241 _uniform_route_uMdl = glGetUniformLocation( _shader_route.id, "uMdl" );
242 _uniform_route_uPv = glGetUniformLocation( _shader_route.id, "uPv" );
243 _uniform_route_uTexGarbage = glGetUniformLocation( _shader_route.id, "uTexGarbage" );
244 _uniform_route_uTexGradients = glGetUniformLocation( _shader_route.id, "uTexGradients" );
245 _uniform_route_uCamera = glGetUniformLocation( _shader_route.id, "uCamera" );
246 _uniform_route_uColour = glGetUniformLocation( _shader_route.id, "uColour" );
247 _uniform_route_g_world_depth = glGetUniformLocation( _shader_route.id, "g_world_depth" );
248 }
249 #endif /* SHADER_route_H */