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
,
10 .orig_file
= "../shaders/standard.vs",
12 "layout (location=0) in vec3 a_co;\n"
13 "layout (location=1) in vec3 a_norm;\n"
14 "layout (location=2) in vec4 a_colour;\n"
15 "layout (location=3) in vec2 a_uv;\n"
20 "uniform mat4x3 uMdl;\n"
26 "out vec3 aWorldCo;\n"
30 " vec3 world_pos = uMdl * vec4(a_co,1.0);\n"
31 " gl_Position = uPv * vec4( world_pos, 1.0 );\n"
32 " aColour = a_colour;\n"
34 " aNorm = mat3(uMdl) * a_norm;\n"
36 " aWorldCo = world_pos;\n"
41 .orig_file
= "../shaders/terrain.fs",
43 "out vec4 FragColor;\n"
45 "uniform sampler2D uTexGarbage;\n"
46 "uniform sampler2D uTexGradients;\n"
47 "uniform vec3 uCamera;\n"
56 "layout (std140) uniform ub_world_lighting\n"
58 " vec4 g_light_colours[3];\n"
59 " vec4 g_light_directions[3];\n"
60 " vec4 g_ambient_colour;\n"
62 " vec4 g_water_plane;\n"
63 " vec4 g_depth_bounds;\n"
64 " float g_water_fog;\n"
65 " int g_light_count;\n"
66 " int g_light_preview;\n"
69 "uniform sampler2D g_world_depth;\n"
71 "// Standard diffuse + spec models\n"
72 "// ==============================\n"
74 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
76 " vec3 vtotal = g_ambient_colour.rgb;\n"
78 " for( int i=0; i<g_light_count; i++ )\n"
80 " vec3 vcolour = g_light_colours[i].rgb;\n"
81 " vec3 vdir = g_light_directions[i].xyz;\n"
83 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
84 " vtotal += vcolour*flight;\n"
87 " return vfrag * vtotal;\n"
90 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
92 " vec3 vcolour = g_light_colours[0].rgb;\n"
93 " vec3 vdir = g_light_directions[0].xyz;\n"
95 " vec3 specdir = reflect( -vdir, wnormal );\n"
96 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
97 " return vfrag + vcolour*spec*fintensity;\n"
100 "float world_depth_sample( vec3 pos )\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"
106 "float shadow_sample( vec3 vdir )\n"
108 " vec3 sample_pos = aWorldCo + vdir;\n"
109 " float height_sample = world_depth_sample( sample_pos );\n"
111 " float fdelta = height_sample - sample_pos.y;\n"
112 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
115 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
117 " float faccum = 0.0;\n"
118 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
119 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
120 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
121 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
122 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
123 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
124 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
125 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
126 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
129 "vec3 do_light_shadowing( vec3 vfrag )\n"
131 " float fspread = g_light_colours[0].w;\n"
132 " vec3 vdir = g_light_directions[0].xyz;\n"
133 " float flength = g_light_directions[0].w;\n"
135 " float famt = 0.0;\n"
136 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
137 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
138 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
139 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
140 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
141 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
142 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
143 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
144 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
147 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
149 " float dist = pow(fdist*0.0008,1.2);\n"
150 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
157 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
159 " // ws modulation\n"
160 " vec4 wgarbage = texture( uTexGarbage, aCo.xz * 0.015 );\n"
162 " // Creating normal patches\n"
163 " vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;\n"
164 " vec3 qnorm = normalize(floor(aNorm*4.0+modnorm)*0.25) + vec3(0.001,0.0,0.0);\n"
165 " vec2 dir = normalize(qnorm.xz);\n"
166 " vec2 uvdiffuse = aCo.xz * 0.02;\n"
167 " uvdiffuse = mat2(dir.y, dir.x, -dir.x, dir.y) * uvdiffuse;\n"
169 " // Patch local noise\n"
170 " vec4 rgarbage = texture( uTexGarbage, uvdiffuse );\n"
172 " // Colour blending\n"
173 " float amtgrass = step(qnorm.y,0.6);\n"
174 " float amtsand = min(max((aCo.y - 10.0) * -0.1,0.0)*qnorm.y,1.0);\n"
175 " vec2 uvgradients = aUv + vec2( amtgrass*0.5 + rgarbage.a*0.4, 0.0 );\n"
176 " vfrag = texture( uTexGradients, uvgradients ).rgb;\n"
177 " vfrag = mix( vfrag, vec3(1.0,0.9,0.8)*0.9, amtsand );\n"
179 " qnorm = mix( qnorm, aNorm, amtsand );\n"
181 " if( g_light_preview == 1 )\n"
183 " vfrag = vec3(0.5);\n"
187 " vec3 halfview = uCamera - aCo;\n"
188 " float fdist = length( halfview );\n"
189 " halfview /= fdist;\n"
191 " vfrag = do_light_diffuse( vfrag, qnorm );\n"
192 " vfrag = do_light_spec( vfrag, qnorm, halfview, 0.1 );\n"
193 " vfrag = do_light_shadowing( vfrag );\n"
194 " vfrag = apply_fog( vfrag, fdist );\n"
196 " FragColor = vec4(vfrag, 1.0 );\n"
201 static GLuint _uniform_terrain_uPv
;
202 static GLuint _uniform_terrain_uMdl
;
203 static GLuint _uniform_terrain_uTexGarbage
;
204 static GLuint _uniform_terrain_uTexGradients
;
205 static GLuint _uniform_terrain_uCamera
;
206 static GLuint _uniform_terrain_g_world_depth
;
207 static void shader_terrain_uPv(m4x4f m
){
208 glUniformMatrix4fv( _uniform_terrain_uPv
, 1, GL_FALSE
, (float *)m
);
210 static void shader_terrain_uMdl(m4x3f m
){
211 glUniformMatrix4x3fv( _uniform_terrain_uMdl
, 1, GL_FALSE
, (float *)m
);
213 static void shader_terrain_uTexGarbage(int i
){
214 glUniform1i( _uniform_terrain_uTexGarbage
, i
);
216 static void shader_terrain_uTexGradients(int i
){
217 glUniform1i( _uniform_terrain_uTexGradients
, i
);
219 static void shader_terrain_uCamera(v3f v
){
220 glUniform3fv( _uniform_terrain_uCamera
, 1, v
);
222 static void shader_terrain_g_world_depth(int i
){
223 glUniform1i( _uniform_terrain_g_world_depth
, i
);
225 static void shader_terrain_register(void){
226 vg_shader_register( &_shader_terrain
);
228 static void shader_terrain_use(void){ glUseProgram(_shader_terrain
.id
); }
229 static void shader_terrain_link(void){
230 _uniform_terrain_uPv
= glGetUniformLocation( _shader_terrain
.id
, "uPv" );
231 _uniform_terrain_uMdl
= glGetUniformLocation( _shader_terrain
.id
, "uMdl" );
232 _uniform_terrain_uTexGarbage
= glGetUniformLocation( _shader_terrain
.id
, "uTexGarbage" );
233 _uniform_terrain_uTexGradients
= glGetUniformLocation( _shader_terrain
.id
, "uTexGradients" );
234 _uniform_terrain_uCamera
= glGetUniformLocation( _shader_terrain
.id
, "uCamera" );
235 _uniform_terrain_g_world_depth
= glGetUniformLocation( _shader_terrain
.id
, "g_world_depth" );
237 #endif /* SHADER_terrain_H */