1 #ifndef SHADER_terrain_H
2 #define SHADER_terrain_H
3 static void shader_terrain_link(void);
4 static void shader_terrain_register(void);
5 static struct vg_shader _shader_terrain
= {
7 .link
= shader_terrain_link
,
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"
20 "out vec3 aMotionVec0;\n"
21 "out vec3 aMotionVec1;\n"
23 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
25 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
26 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
31 "uniform mat4x3 uMdl;\n"
33 "uniform mat4 uPvmPrev;\n"
39 "out vec3 aWorldCo;\n"
43 " vec3 world_pos0 = uMdl * vec4( a_co, 1.0 );\n"
44 " vec4 vproj0 = uPv * vec4( world_pos0, 1.0 );\n"
45 " vec4 vproj1 = uPvmPrev * vec4( a_co, 1.0 );\n"
47 " vs_motion_out( vproj0, vproj1 );\n"
49 " gl_Position = vproj0;\n"
50 " aWorldCo = world_pos0;\n"
51 " aColour = a_colour;\n"
53 " aNorm = mat3(uMdl) * a_norm;\n"
60 "uniform sampler2D uTexGarbage;\n"
61 "uniform sampler2D uTexGradients;\n"
62 "uniform vec3 uCamera;\n"
63 "uniform vec3 uSandColour;\n"
64 "uniform vec2 uBlendOffset;\n"
73 "layout (location = 0) out vec4 oColour;\n"
75 "layout (std140) uniform ub_world_lighting\n"
77 " vec4 g_light_colours[3];\n"
78 " vec4 g_light_directions[3];\n"
79 " vec4 g_ambient_colour;\n"
81 " vec4 g_water_plane;\n"
82 " vec4 g_depth_bounds;\n"
83 " float g_water_fog;\n"
84 " int g_light_count;\n"
85 " int g_light_preview;\n"
86 " int g_shadow_samples;\n"
89 "uniform sampler2D g_world_depth;\n"
91 "// Standard diffuse + spec models\n"
92 "// ==============================\n"
94 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
96 " vec3 vtotal = g_ambient_colour.rgb;\n"
98 " for( int i=0; i<g_light_count; i++ )\n"
100 " vec3 vcolour = g_light_colours[i].rgb;\n"
101 " vec3 vdir = g_light_directions[i].xyz;\n"
103 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
104 " vtotal += vcolour*flight;\n"
107 " return vfrag * vtotal;\n"
110 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
112 " vec3 vcolour = g_light_colours[0].rgb;\n"
113 " vec3 vdir = g_light_directions[0].xyz;\n"
115 " vec3 specdir = reflect( -vdir, wnormal );\n"
116 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
117 " return vfrag + vcolour*spec*fintensity;\n"
120 "float world_depth_sample( vec3 pos )\n"
122 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
123 " return texture( g_world_depth, depth_coord ).r;\n"
126 "float shadow_sample( vec3 vdir )\n"
128 " vec3 sample_pos = aWorldCo + vdir;\n"
129 " float height_sample = world_depth_sample( sample_pos );\n"
131 " float fdelta = height_sample - sample_pos.y;\n"
132 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
135 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
137 " float faccum = 0.0;\n"
138 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
139 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
140 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
141 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
142 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
143 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
144 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
145 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
146 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
149 "vec3 do_light_shadowing( vec3 vfrag )\n"
151 " if( g_shadow_samples == 0 )\n"
156 " float fspread = g_light_colours[0].w;\n"
157 " vec3 vdir = g_light_directions[0].xyz;\n"
158 " float flength = g_light_directions[0].w;\n"
160 " float famt = 0.0;\n"
161 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
162 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
163 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
164 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
165 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
166 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
167 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
168 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
169 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
172 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
174 " float dist = pow(fdist*0.0008,1.2);\n"
175 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
180 "layout (location = 1) out vec2 oMotionVec;\n"
182 "in vec3 aMotionVec0;\n"
183 "in vec3 aMotionVec1;\n"
185 "void compute_motion_vectors()\n"
187 " // Write motion vectors\n"
188 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
189 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
190 " oMotionVec = vmotion1-vmotion0;\n"
197 " compute_motion_vectors();\n"
201 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
203 " // ws modulation\n"
204 " vec4 wgarbage = texture( uTexGarbage, aCo.xz * 0.015 );\n"
206 " // Creating normal patches\n"
207 " vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;\n"
208 " vec3 qnorm = normalize(floor(aNorm*4.0+modnorm)*0.25) + vec3(0.001,0.0,0.0);\n"
209 " vec2 dir = normalize(qnorm.xz);\n"
210 " vec2 uvdiffuse = aCo.xz * 0.02;\n"
211 " uvdiffuse = mat2(dir.y, dir.x, -dir.x, dir.y) * uvdiffuse;\n"
213 " // Patch local noise\n"
214 " vec4 rgarbage = texture( uTexGarbage, uvdiffuse );\n"
216 " // Colour blending\n"
217 " float amtgrass = step(qnorm.y,0.6);\n"
218 " float amtsand = min(max((aCo.y - 10.0) * -0.1,0.0)*qnorm.y,1.0);\n"
219 " vec2 uvgradients = aUv + vec2( amtgrass + rgarbage.a*0.8 )*uBlendOffset;\n"
220 " vfrag = texture( uTexGradients, uvgradients ).rgb;\n"
221 " vfrag = mix( vfrag, uSandColour, amtsand );\n"
223 " qnorm = mix( qnorm, aNorm, amtsand );\n"
225 " if( g_light_preview == 1 )\n"
227 " vfrag = vec3(0.5);\n"
231 " vec3 halfview = uCamera - aCo;\n"
232 " float fdist = length( halfview );\n"
233 " halfview /= fdist;\n"
235 " vfrag = do_light_diffuse( vfrag, qnorm );\n"
236 " vfrag = do_light_spec( vfrag, qnorm, halfview, 0.1 );\n"
237 " vfrag = do_light_shadowing( vfrag );\n"
238 " vfrag = apply_fog( vfrag, fdist );\n"
240 " oColour = vec4(vfrag, 1.0 );\n"
245 static GLuint _uniform_terrain_uMdl
;
246 static GLuint _uniform_terrain_uPv
;
247 static GLuint _uniform_terrain_uPvmPrev
;
248 static GLuint _uniform_terrain_uTexGarbage
;
249 static GLuint _uniform_terrain_uTexGradients
;
250 static GLuint _uniform_terrain_uCamera
;
251 static GLuint _uniform_terrain_uSandColour
;
252 static GLuint _uniform_terrain_uBlendOffset
;
253 static GLuint _uniform_terrain_g_world_depth
;
254 static void shader_terrain_uMdl(m4x3f m
){
255 glUniformMatrix4x3fv(_uniform_terrain_uMdl
,1,GL_FALSE
,(float*)m
);
257 static void shader_terrain_uPv(m4x4f m
){
258 glUniformMatrix4fv(_uniform_terrain_uPv
,1,GL_FALSE
,(float*)m
);
260 static void shader_terrain_uPvmPrev(m4x4f m
){
261 glUniformMatrix4fv(_uniform_terrain_uPvmPrev
,1,GL_FALSE
,(float*)m
);
263 static void shader_terrain_uTexGarbage(int i
){
264 glUniform1i(_uniform_terrain_uTexGarbage
,i
);
266 static void shader_terrain_uTexGradients(int i
){
267 glUniform1i(_uniform_terrain_uTexGradients
,i
);
269 static void shader_terrain_uCamera(v3f v
){
270 glUniform3fv(_uniform_terrain_uCamera
,1,v
);
272 static void shader_terrain_uSandColour(v3f v
){
273 glUniform3fv(_uniform_terrain_uSandColour
,1,v
);
275 static void shader_terrain_uBlendOffset(v2f v
){
276 glUniform2fv(_uniform_terrain_uBlendOffset
,1,v
);
278 static void shader_terrain_g_world_depth(int i
){
279 glUniform1i(_uniform_terrain_g_world_depth
,i
);
281 static void shader_terrain_register(void){
282 vg_shader_register( &_shader_terrain
);
284 static void shader_terrain_use(void){ glUseProgram(_shader_terrain
.id
); }
285 static void shader_terrain_link(void){
286 _uniform_terrain_uMdl
= glGetUniformLocation( _shader_terrain
.id
, "uMdl" );
287 _uniform_terrain_uPv
= glGetUniformLocation( _shader_terrain
.id
, "uPv" );
288 _uniform_terrain_uPvmPrev
= glGetUniformLocation( _shader_terrain
.id
, "uPvmPrev" );
289 _uniform_terrain_uTexGarbage
= glGetUniformLocation( _shader_terrain
.id
, "uTexGarbage" );
290 _uniform_terrain_uTexGradients
= glGetUniformLocation( _shader_terrain
.id
, "uTexGradients" );
291 _uniform_terrain_uCamera
= glGetUniformLocation( _shader_terrain
.id
, "uCamera" );
292 _uniform_terrain_uSandColour
= glGetUniformLocation( _shader_terrain
.id
, "uSandColour" );
293 _uniform_terrain_uBlendOffset
= glGetUniformLocation( _shader_terrain
.id
, "uBlendOffset" );
294 _uniform_terrain_g_world_depth
= glGetUniformLocation( _shader_terrain
.id
, "g_world_depth" );
296 #endif /* SHADER_terrain_H */