fixes some artifacting
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / alphatest.h
1 #ifndef SHADER_alphatest_H
2 #define SHADER_alphatest_H
3 static void shader_alphatest_link(void);
4 static void shader_alphatest_register(void);
5 static struct vg_shader _shader_alphatest = {
6 .name = "alphatest",
7 .link = shader_alphatest_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.05;\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 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
30 "\n"
31 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
32 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
33 "}\n"
34 "\n"
35 "#line 3 0 \n"
36 "\n"
37 "uniform mat4x3 uMdl;\n"
38 "uniform mat4 uPv;\n"
39 "uniform mat4 uPvmPrev;\n"
40 "\n"
41 "out vec4 aColour;\n"
42 "out vec2 aUv;\n"
43 "out vec3 aNorm;\n"
44 "out vec3 aCo;\n"
45 "out vec3 aWorldCo;\n"
46 "\n"
47 "void main()\n"
48 "{\n"
49 " vec3 world_pos0 = uMdl * vec4( a_co, 1.0 );\n"
50 " vec4 vproj0 = uPv * vec4( world_pos0, 1.0 );\n"
51 " vec4 vproj1 = uPvmPrev * vec4( a_co, 1.0 );\n"
52 "\n"
53 " vs_motion_out( vproj0, vproj1 );\n"
54 "\n"
55 " gl_Position = vproj0;\n"
56 " aWorldCo = world_pos0;\n"
57 " aColour = a_colour;\n"
58 " aUv = a_uv;\n"
59 " aNorm = mat3(uMdl) * a_norm;\n"
60 " aCo = a_co;\n"
61 "}\n"
62 ""},
63 .fs =
64 {
65 .static_src =
66 "uniform sampler2D uTexGarbage;\n"
67 "uniform sampler2D uTexMain;\n"
68 "uniform vec3 uCamera;\n"
69 "uniform vec4 uPlane;\n"
70 "\n"
71 "in vec4 aColour;\n"
72 "in vec2 aUv;\n"
73 "in vec3 aNorm;\n"
74 "in vec3 aCo;\n"
75 "in vec3 aWorldCo;\n"
76 "\n"
77 "#line 1 1 \n"
78 "layout (location = 0) out vec4 oColour;\n"
79 "\n"
80 "layout (std140) uniform ub_world_lighting\n"
81 "{\n"
82 " vec4 g_light_colours[3];\n"
83 " vec4 g_light_directions[3];\n"
84 " vec4 g_ambient_colour;\n"
85 "\n"
86 " vec4 g_water_plane;\n"
87 " vec4 g_depth_bounds;\n"
88 " float g_water_fog;\n"
89 " int g_light_count;\n"
90 " int g_light_preview;\n"
91 " int g_shadow_samples;\n"
92 "};\n"
93 "\n"
94 "uniform sampler2D g_world_depth;\n"
95 "\n"
96 "// Standard diffuse + spec models\n"
97 "// ==============================\n"
98 "\n"
99 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
100 "{\n"
101 " vec3 vtotal = g_ambient_colour.rgb;\n"
102 "\n"
103 " for( int i=0; i<g_light_count; i++ )\n"
104 " {\n"
105 " vec3 vcolour = g_light_colours[i].rgb;\n"
106 " vec3 vdir = g_light_directions[i].xyz;\n"
107 "\n"
108 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
109 " vtotal += vcolour*flight;\n"
110 " }\n"
111 "\n"
112 " return vfrag * vtotal;\n"
113 "}\n"
114 "\n"
115 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
116 "{\n"
117 " vec3 vcolour = g_light_colours[0].rgb;\n"
118 " vec3 vdir = g_light_directions[0].xyz;\n"
119 "\n"
120 " vec3 specdir = reflect( -vdir, wnormal );\n"
121 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
122 " return vfrag + vcolour*spec*fintensity;\n"
123 "}\n"
124 "\n"
125 "float world_depth_sample( vec3 pos )\n"
126 "{\n"
127 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
128 " return texture( g_world_depth, depth_coord ).r;\n"
129 "}\n"
130 "\n"
131 "float shadow_sample( vec3 vdir )\n"
132 "{\n"
133 " vec3 sample_pos = aWorldCo + vdir;\n"
134 " float height_sample = world_depth_sample( sample_pos );\n"
135 "\n"
136 " float fdelta = height_sample - sample_pos.y;\n"
137 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
138 "}\n"
139 "\n"
140 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
141 "{\n"
142 " float faccum = 0.0;\n"
143 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
144 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
145 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
146 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
147 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
148 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
149 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
150 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
151 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
152 "}\n"
153 "\n"
154 "vec3 do_light_shadowing( vec3 vfrag )\n"
155 "{\n"
156 " if( g_shadow_samples == 0 )\n"
157 " {\n"
158 " return vfrag;\n"
159 " }\n"
160 "\n"
161 " float fspread = g_light_colours[0].w;\n"
162 " vec3 vdir = g_light_directions[0].xyz;\n"
163 " float flength = g_light_directions[0].w;\n"
164 "\n"
165 " float famt = 0.0;\n"
166 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
167 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
168 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
169 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
170 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
171 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
172 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
173 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
174 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
175 "}\n"
176 "\n"
177 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
178 "{\n"
179 " float dist = pow(fdist*0.0008,1.2);\n"
180 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
181 "}\n"
182 "\n"
183 "#line 13 0 \n"
184 "#line 1 2 \n"
185 "const float k_motion_lerp_amount = 0.05;\n"
186 "\n"
187 "#line 2 0 \n"
188 "\n"
189 "layout (location = 1) out vec2 oMotionVec;\n"
190 "\n"
191 "in vec3 aMotionVec0;\n"
192 "in vec3 aMotionVec1;\n"
193 "\n"
194 "void compute_motion_vectors()\n"
195 "{\n"
196 " // Write motion vectors\n"
197 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
198 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
199 "\n"
200 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
201 "}\n"
202 "\n"
203 "#line 14 0 \n"
204 "\n"
205 "void main()\n"
206 "{\n"
207 " compute_motion_vectors();\n"
208 "\n"
209 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
210 " vec4 vsamplemain = texture( uTexMain, aUv );\n"
211 " vec3 qnorm = normalize(aNorm);\n"
212 "\n"
213 " if( vsamplemain.a < 0.15 )\n"
214 " discard;\n"
215 "\n"
216 " vfrag = vsamplemain.rgb;\n"
217 "\n"
218 " if( g_light_preview == 1 )\n"
219 " {\n"
220 " vfrag = vec3(0.5);\n"
221 " }\n"
222 "\n"
223 " // Lighting\n"
224 " vec3 halfview = uCamera - aWorldCo;\n"
225 " float fdist = length( halfview );\n"
226 " halfview /= fdist;\n"
227 "\n"
228 " vfrag = do_light_diffuse( vfrag, qnorm );\n"
229 " vfrag = do_light_spec( vfrag, qnorm, halfview, 0.1 );\n"
230 " vfrag = do_light_shadowing( vfrag );\n"
231 " vfrag = apply_fog( vfrag, fdist );\n"
232 "\n"
233 " oColour = vec4(vfrag, 1.0);\n"
234 "}\n"
235 ""},
236 };
237
238 static GLuint _uniform_alphatest_uMdl;
239 static GLuint _uniform_alphatest_uPv;
240 static GLuint _uniform_alphatest_uPvmPrev;
241 static GLuint _uniform_alphatest_uTexGarbage;
242 static GLuint _uniform_alphatest_uTexMain;
243 static GLuint _uniform_alphatest_uCamera;
244 static GLuint _uniform_alphatest_uPlane;
245 static GLuint _uniform_alphatest_g_world_depth;
246 static void shader_alphatest_uMdl(m4x3f m){
247 glUniformMatrix4x3fv(_uniform_alphatest_uMdl,1,GL_FALSE,(float*)m);
248 }
249 static void shader_alphatest_uPv(m4x4f m){
250 glUniformMatrix4fv(_uniform_alphatest_uPv,1,GL_FALSE,(float*)m);
251 }
252 static void shader_alphatest_uPvmPrev(m4x4f m){
253 glUniformMatrix4fv(_uniform_alphatest_uPvmPrev,1,GL_FALSE,(float*)m);
254 }
255 static void shader_alphatest_uTexGarbage(int i){
256 glUniform1i(_uniform_alphatest_uTexGarbage,i);
257 }
258 static void shader_alphatest_uTexMain(int i){
259 glUniform1i(_uniform_alphatest_uTexMain,i);
260 }
261 static void shader_alphatest_uCamera(v3f v){
262 glUniform3fv(_uniform_alphatest_uCamera,1,v);
263 }
264 static void shader_alphatest_uPlane(v4f v){
265 glUniform4fv(_uniform_alphatest_uPlane,1,v);
266 }
267 static void shader_alphatest_g_world_depth(int i){
268 glUniform1i(_uniform_alphatest_g_world_depth,i);
269 }
270 static void shader_alphatest_register(void){
271 vg_shader_register( &_shader_alphatest );
272 }
273 static void shader_alphatest_use(void){ glUseProgram(_shader_alphatest.id); }
274 static void shader_alphatest_link(void){
275 _uniform_alphatest_uMdl = glGetUniformLocation( _shader_alphatest.id, "uMdl" );
276 _uniform_alphatest_uPv = glGetUniformLocation( _shader_alphatest.id, "uPv" );
277 _uniform_alphatest_uPvmPrev = glGetUniformLocation( _shader_alphatest.id, "uPvmPrev" );
278 _uniform_alphatest_uTexGarbage = glGetUniformLocation( _shader_alphatest.id, "uTexGarbage" );
279 _uniform_alphatest_uTexMain = glGetUniformLocation( _shader_alphatest.id, "uTexMain" );
280 _uniform_alphatest_uCamera = glGetUniformLocation( _shader_alphatest.id, "uCamera" );
281 _uniform_alphatest_uPlane = glGetUniformLocation( _shader_alphatest.id, "uPlane" );
282 _uniform_alphatest_g_world_depth = glGetUniformLocation( _shader_alphatest.id, "g_world_depth" );
283 }
284 #endif /* SHADER_alphatest_H */