fixes some artifacting
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / gpos.h
1 #ifndef SHADER_gpos_H
2 #define SHADER_gpos_H
3 static void shader_gpos_link(void);
4 static void shader_gpos_register(void);
5 static struct vg_shader _shader_gpos = {
6 .name = "gpos",
7 .link = shader_gpos_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 "out vec4 FragColor;\n"
67 "\n"
68 "uniform vec3 uCamera;\n"
69 "\n"
70 "in vec4 aColour;\n"
71 "in vec2 aUv;\n"
72 "in vec3 aNorm;\n"
73 "in vec3 aCo;\n"
74 "in vec3 aWorldCo;\n"
75 "\n"
76 "#line 1 1 \n"
77 "layout (location = 0) out vec4 oColour;\n"
78 "\n"
79 "layout (std140) uniform ub_world_lighting\n"
80 "{\n"
81 " vec4 g_light_colours[3];\n"
82 " vec4 g_light_directions[3];\n"
83 " vec4 g_ambient_colour;\n"
84 "\n"
85 " vec4 g_water_plane;\n"
86 " vec4 g_depth_bounds;\n"
87 " float g_water_fog;\n"
88 " int g_light_count;\n"
89 " int g_light_preview;\n"
90 " int g_shadow_samples;\n"
91 "};\n"
92 "\n"
93 "uniform sampler2D g_world_depth;\n"
94 "\n"
95 "// Standard diffuse + spec models\n"
96 "// ==============================\n"
97 "\n"
98 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
99 "{\n"
100 " vec3 vtotal = g_ambient_colour.rgb;\n"
101 "\n"
102 " for( int i=0; i<g_light_count; i++ )\n"
103 " {\n"
104 " vec3 vcolour = g_light_colours[i].rgb;\n"
105 " vec3 vdir = g_light_directions[i].xyz;\n"
106 "\n"
107 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
108 " vtotal += vcolour*flight;\n"
109 " }\n"
110 "\n"
111 " return vfrag * vtotal;\n"
112 "}\n"
113 "\n"
114 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
115 "{\n"
116 " vec3 vcolour = g_light_colours[0].rgb;\n"
117 " vec3 vdir = g_light_directions[0].xyz;\n"
118 "\n"
119 " vec3 specdir = reflect( -vdir, wnormal );\n"
120 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
121 " return vfrag + vcolour*spec*fintensity;\n"
122 "}\n"
123 "\n"
124 "float world_depth_sample( vec3 pos )\n"
125 "{\n"
126 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
127 " return texture( g_world_depth, depth_coord ).r;\n"
128 "}\n"
129 "\n"
130 "float shadow_sample( vec3 vdir )\n"
131 "{\n"
132 " vec3 sample_pos = aWorldCo + vdir;\n"
133 " float height_sample = world_depth_sample( sample_pos );\n"
134 "\n"
135 " float fdelta = height_sample - sample_pos.y;\n"
136 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
137 "}\n"
138 "\n"
139 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
140 "{\n"
141 " float faccum = 0.0;\n"
142 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
143 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
144 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
145 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
146 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
147 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
148 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
149 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
150 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
151 "}\n"
152 "\n"
153 "vec3 do_light_shadowing( vec3 vfrag )\n"
154 "{\n"
155 " if( g_shadow_samples == 0 )\n"
156 " {\n"
157 " return vfrag;\n"
158 " }\n"
159 "\n"
160 " float fspread = g_light_colours[0].w;\n"
161 " vec3 vdir = g_light_directions[0].xyz;\n"
162 " float flength = g_light_directions[0].w;\n"
163 "\n"
164 " float famt = 0.0;\n"
165 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
166 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
167 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
168 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
169 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
170 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
171 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
172 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
173 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
174 "}\n"
175 "\n"
176 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
177 "{\n"
178 " float dist = pow(fdist*0.0008,1.2);\n"
179 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
180 "}\n"
181 "\n"
182 "#line 12 0 \n"
183 "\n"
184 "// Water blending\n"
185 "// ==============\n"
186 "\n"
187 "float water_depth( vec3 pos, vec3 halfview )\n"
188 "{\n"
189 " vec3 pnorm = g_water_plane.xyz;\n"
190 " float pdist = g_water_plane.w;\n"
191 "\n"
192 " float d = dot( pnorm, halfview );\n"
193 " float t = dot((pnorm*pdist - pos), pnorm) / d;\n"
194 " return t * g_water_fog;\n"
195 "}\n"
196 "\n"
197 "void main()\n"
198 "{\n"
199 " vec3 halfview = normalize( uCamera - aCo );\n"
200 " vec3 world_pos = vec3( aCo.y, aCo.x, aCo.z );\n"
201 " FragColor = vec4( world_pos, water_depth( aCo, halfview ) );\n"
202 "}\n"
203 ""},
204 };
205
206 static GLuint _uniform_gpos_uMdl;
207 static GLuint _uniform_gpos_uPv;
208 static GLuint _uniform_gpos_uPvmPrev;
209 static GLuint _uniform_gpos_uCamera;
210 static GLuint _uniform_gpos_g_world_depth;
211 static void shader_gpos_uMdl(m4x3f m){
212 glUniformMatrix4x3fv(_uniform_gpos_uMdl,1,GL_FALSE,(float*)m);
213 }
214 static void shader_gpos_uPv(m4x4f m){
215 glUniformMatrix4fv(_uniform_gpos_uPv,1,GL_FALSE,(float*)m);
216 }
217 static void shader_gpos_uPvmPrev(m4x4f m){
218 glUniformMatrix4fv(_uniform_gpos_uPvmPrev,1,GL_FALSE,(float*)m);
219 }
220 static void shader_gpos_uCamera(v3f v){
221 glUniform3fv(_uniform_gpos_uCamera,1,v);
222 }
223 static void shader_gpos_g_world_depth(int i){
224 glUniform1i(_uniform_gpos_g_world_depth,i);
225 }
226 static void shader_gpos_register(void){
227 vg_shader_register( &_shader_gpos );
228 }
229 static void shader_gpos_use(void){ glUseProgram(_shader_gpos.id); }
230 static void shader_gpos_link(void){
231 _uniform_gpos_uMdl = glGetUniformLocation( _shader_gpos.id, "uMdl" );
232 _uniform_gpos_uPv = glGetUniformLocation( _shader_gpos.id, "uPv" );
233 _uniform_gpos_uPvmPrev = glGetUniformLocation( _shader_gpos.id, "uPvmPrev" );
234 _uniform_gpos_uCamera = glGetUniformLocation( _shader_gpos.id, "uCamera" );
235 _uniform_gpos_g_world_depth = glGetUniformLocation( _shader_gpos.id, "g_world_depth" );
236 }
237 #endif /* SHADER_gpos_H */