1 #ifndef SHADER_model_sky_H
2 #define SHADER_model_sky_H
3 static void shader_model_sky_link(void);
4 static void shader_model_sky_register(void);
5 static struct vg_shader _shader_model_sky
= {
7 .link
= shader_model_sky_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"
19 "const float k_motion_lerp_amount = 0.01;\n"
23 "out vec3 aMotionVec0;\n"
24 "out vec3 aMotionVec1;\n"
26 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
28 " // This magically solves some artifacting errors!\n"
30 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
32 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
33 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
38 "uniform mat4x3 uMdl;\n"
40 "uniform mat4 uPvmPrev;\n"
46 "out vec3 aWorldCo;\n"
50 " vec3 world_pos0 = uMdl * vec4( a_co, 1.0 );\n"
51 " vec4 vproj0 = uPv * vec4( world_pos0, 1.0 );\n"
52 " vec4 vproj1 = uPvmPrev * vec4( a_co, 1.0 );\n"
54 " vs_motion_out( vproj0, vproj1 );\n"
56 " gl_Position = vproj0;\n"
57 " aWorldCo = world_pos0;\n"
58 " aColour = a_colour;\n"
60 " aNorm = mat3(uMdl) * a_norm;\n"
67 "uniform sampler2D uTexGarbage;\n"
68 "uniform float uTime;\n"
77 "layout (location = 0) out vec4 oColour;\n"
79 "layout (std140) uniform ub_world_lighting\n"
81 " vec4 g_light_colours[3];\n"
82 " vec4 g_light_directions[3];\n"
83 " vec4 g_ambient_colour;\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"
94 " //vec4 g_point_light_positions[32];\n"
95 " //vec4 g_point_light_colours[32];\n"
98 "uniform sampler2D g_world_depth;\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 world_water_depth( vec3 pos )\n"
108 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
109 " float ref_depth = g_water_plane.y*g_water_plane.w;\n"
110 " return texture( g_world_depth, depth_coord ).g - ref_depth;\n"
113 "float shadow_sample( vec3 vdir )\n"
115 " vec3 sample_pos = aWorldCo + vdir;\n"
116 " float height_sample = world_depth_sample( sample_pos );\n"
118 " float fdelta = height_sample - sample_pos.y;\n"
119 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
122 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
124 " float dist = pow(fdist*0.0008,1.2);\n"
125 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
129 "// New lighting model\n"
131 "vec3 newlight_compute_ambient()\n"
133 " return g_ambient_colour.rgb;\n"
136 "float newlight_compute_sun_shadow()\n"
138 " if( g_shadow_samples == 0 )\n"
143 " float fspread = g_light_colours[0].w;\n"
144 " vec3 vdir = g_light_directions[0].xyz;\n"
145 " float flength = g_light_directions[0].w;\n"
147 " float famt = 0.0;\n"
148 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
149 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
150 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
151 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
152 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
153 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
154 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
155 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
157 " return 1.0 - famt;\n"
160 "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n"
162 " vec3 vtotal = g_ambient_colour.rgb;\n"
164 " for( int i=0; i<g_light_count; i++ )\n"
166 " vec3 vcolour = g_light_colours[i].rgb;\n"
167 " vec3 vdir = g_light_directions[i].xyz;\n"
169 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
170 " vtotal += vcolour*flight;\n"
176 "vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )\n"
178 " vec3 vcolour = g_light_colours[0].rgb;\n"
179 " vec3 vdir = g_light_directions[0].xyz;\n"
181 " vec3 specdir = reflect( -vdir, wnormal );\n"
182 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
183 " return vcolour*spec*fintensity;\n"
186 "vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, \n"
187 " vec3 light_pos, vec3 light_colour )\n"
189 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
191 " float quadratic = dot(light_delta,light_delta);\n"
192 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
193 " attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );\n"
195 " return light_colour*attenuation;\n"
200 "const float k_motion_lerp_amount = 0.01;\n"
204 "layout (location = 1) out vec2 oMotionVec;\n"
206 "in vec3 aMotionVec0;\n"
207 "in vec3 aMotionVec1;\n"
209 "void compute_motion_vectors()\n"
211 " // Write motion vectors\n"
212 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
213 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
215 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
222 " compute_motion_vectors();\n"
224 " vec3 rd = normalize(aNorm);\n"
226 " float fintensity = 1.0-(abs(rd.y)*0.7);\n"
227 " float fblend = pow(fintensity,4.0);\n"
228 " vec3 horizon = vec3( 0.87, 0.93, 0.98 );\n"
229 " vec3 skycolour = vec3( 0.16, 0.58, 0.95 ) - rd.y*rd.y*0.5;\n"
230 " vec3 diffuse = mix( skycolour, horizon, fblend );\n"
232 " float fmove = uTime * 0.004;\n"
233 " vec2 cloudplane = (rd.xz / (rd.y*sign(rd.y))) * 0.05;\n"
234 " vec4 clouds1 = texture( uTexGarbage, cloudplane + vec2(0.1,0.4)*fmove*2.0 );\n"
235 " vec4 clouds2 = texture( uTexGarbage, cloudplane*2.0 + vec2(0.3,0.1)*fmove );\n"
237 " float cloud_d = max(clouds1.b*clouds2.r -0.2 - clouds2.g*0.4,0.0);\n"
238 " float cloud_e = pow(cloud_d,1.5)*pow(abs(rd.y),0.3)*2.0;\n"
240 " vec3 colour_ocean = vec3( 0.61, 0.84, 0.9 );\n"
241 " float fhorizon = step( rd.y * 0.5 + 0.5, 0.5 );\n"
243 " vec3 skycomp = mix(diffuse, vec3(1.0,1.0,1.0), cloud_e);\n"
246 " float sundot = clamp(dot(rd, -g_light_directions[0].xyz), 0.0, 1.0);\n"
247 " vec3 sun = 0.25 * vec3(1.0,0.7,0.4) * pow( sundot,5.0 );\n"
248 " sun += 0.25 * vec3(1.0,0.8,0.6) * pow( sundot,64.0 );\n"
249 " sun += 0.2 * vec3(1.0,0.8,0.6) * pow( sundot,512.0 );\n"
250 " skycomp += sun * g_light_colours[0].rgb;\n"
252 " oColour = vec4(skycomp,1.0);\n"
257 static GLuint _uniform_model_sky_uMdl
;
258 static GLuint _uniform_model_sky_uPv
;
259 static GLuint _uniform_model_sky_uPvmPrev
;
260 static GLuint _uniform_model_sky_uTexGarbage
;
261 static GLuint _uniform_model_sky_uTime
;
262 static GLuint _uniform_model_sky_g_world_depth
;
263 static void shader_model_sky_uMdl(m4x3f m
){
264 glUniformMatrix4x3fv(_uniform_model_sky_uMdl
,1,GL_FALSE
,(float*)m
);
266 static void shader_model_sky_uPv(m4x4f m
){
267 glUniformMatrix4fv(_uniform_model_sky_uPv
,1,GL_FALSE
,(float*)m
);
269 static void shader_model_sky_uPvmPrev(m4x4f m
){
270 glUniformMatrix4fv(_uniform_model_sky_uPvmPrev
,1,GL_FALSE
,(float*)m
);
272 static void shader_model_sky_uTexGarbage(int i
){
273 glUniform1i(_uniform_model_sky_uTexGarbage
,i
);
275 static void shader_model_sky_uTime(float f
){
276 glUniform1f(_uniform_model_sky_uTime
,f
);
278 static void shader_model_sky_g_world_depth(int i
){
279 glUniform1i(_uniform_model_sky_g_world_depth
,i
);
281 static void shader_model_sky_register(void){
282 vg_shader_register( &_shader_model_sky
);
284 static void shader_model_sky_use(void){ glUseProgram(_shader_model_sky
.id
); }
285 static void shader_model_sky_link(void){
286 _uniform_model_sky_uMdl
= glGetUniformLocation( _shader_model_sky
.id
, "uMdl" );
287 _uniform_model_sky_uPv
= glGetUniformLocation( _shader_model_sky
.id
, "uPv" );
288 _uniform_model_sky_uPvmPrev
= glGetUniformLocation( _shader_model_sky
.id
, "uPvmPrev" );
289 _uniform_model_sky_uTexGarbage
= glGetUniformLocation( _shader_model_sky
.id
, "uTexGarbage" );
290 _uniform_model_sky_uTime
= glGetUniformLocation( _shader_model_sky
.id
, "uTime" );
291 _uniform_model_sky_g_world_depth
= glGetUniformLocation( _shader_model_sky
.id
, "g_world_depth" );
293 #endif /* SHADER_model_sky_H */