1 #ifndef SHADER_scene_standard_H
2 #define SHADER_scene_standard_H
3 static void shader_scene_standard_link(void);
4 static void shader_scene_standard_register(void);
5 static struct vg_shader _shader_scene_standard
= {
6 .name
= "scene_standard",
7 .link
= shader_scene_standard_link
,
11 "layout (location=0) in vec3 a_co;\n"
12 "layout (location=1) in vec4 a_norm;\n"
13 "layout (location=2) in vec2 a_uv;\n"
14 "layout (location=3) in ivec4 a_lights;\n"
17 "const float k_motion_lerp_amount = 0.01;\n"
21 "out vec3 aMotionVec0;\n"
22 "out vec3 aMotionVec1;\n"
24 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
26 " // This magically solves some artifacting errors!\n"
28 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
30 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
31 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
36 "uniform mat4x3 uMdl;\n"
38 "uniform mat4 uPvmPrev;\n"
39 "uniform samplerBuffer uLightsArray;\n"
44 "out vec3 aWorldCo;\n"
45 "flat out vec4 light_colours[3];\n"
46 "flat out vec4 light_positions[3];\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"
59 " aNorm = vec4( mat3(uMdl) * a_norm.xyz, a_norm.w );\n"
61 " aWorldCo = world_pos0;\n"
64 " light_colours[0] = texelFetch( uLightsArray, a_lights.x*2+0 );\n"
65 " light_colours[1] = texelFetch( uLightsArray, a_lights.y*2+0 );\n"
66 " light_colours[2] = texelFetch( uLightsArray, a_lights.z*2+0 );\n"
67 " light_positions[0] = texelFetch( uLightsArray, a_lights.x*2+1 );\n"
68 " light_positions[1] = texelFetch( uLightsArray, a_lights.y*2+1 );\n"
69 " light_positions[2] = texelFetch( uLightsArray, a_lights.z*2+1 );\n"
75 "uniform sampler2D uTexGarbage;\n"
76 "uniform sampler2D uTexMain;\n"
77 "uniform vec3 uCamera;\n"
78 "uniform vec4 uPlane;\n"
79 "uniform vec3 uBoard0;\n"
80 "uniform vec3 uBoard1;\n"
89 "flat in vec4 light_colours[3];\n"
90 "flat in vec4 light_positions[3];\n"
93 "layout (location = 0) out vec4 oColour;\n"
95 "layout (std140) uniform ub_world_lighting\n"
97 " vec4 g_light_colours[3];\n"
98 " vec4 g_light_directions[3];\n"
99 " vec4 g_ambient_colour;\n"
101 " vec4 g_water_plane;\n"
102 " vec4 g_depth_bounds;\n"
103 " float g_water_fog;\n"
104 " int g_light_count;\n"
105 " int g_light_preview;\n"
106 " int g_shadow_samples;\n"
110 " //vec4 g_point_light_positions[32];\n"
111 " //vec4 g_point_light_colours[32];\n"
114 "uniform sampler2D g_world_depth;\n"
116 "float world_depth_sample( vec3 pos )\n"
118 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
119 " return texture( g_world_depth, depth_coord ).r;\n"
122 "float world_water_depth( vec3 pos )\n"
124 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
125 " float ref_depth = g_water_plane.y*g_water_plane.w;\n"
126 " return texture( g_world_depth, depth_coord ).g - ref_depth;\n"
129 "float shadow_sample( vec3 vdir )\n"
131 " vec3 sample_pos = aWorldCo + vdir;\n"
132 " float height_sample = world_depth_sample( sample_pos );\n"
134 " float fdelta = height_sample - sample_pos.y;\n"
135 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
138 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
140 " float dist = pow(fdist*0.0008,1.2);\n"
141 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
145 "// New lighting model\n"
147 "vec3 newlight_compute_ambient()\n"
149 " return g_ambient_colour.rgb;\n"
152 "float newlight_compute_sun_shadow()\n"
154 " if( g_shadow_samples == 0 )\n"
159 " float fspread = g_light_colours[0].w;\n"
160 " vec3 vdir = g_light_directions[0].xyz;\n"
161 " float flength = g_light_directions[0].w;\n"
163 " float famt = 0.0;\n"
164 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
165 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
166 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
167 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
168 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
169 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
170 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
171 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
173 " return 1.0 - famt;\n"
176 "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n"
178 " vec3 vtotal = g_ambient_colour.rgb;\n"
180 " for( int i=0; i<g_light_count; i++ )\n"
182 " vec3 vcolour = g_light_colours[i].rgb;\n"
183 " vec3 vdir = g_light_directions[i].xyz;\n"
185 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
186 " vtotal += vcolour*flight;\n"
192 "vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )\n"
194 " vec3 vcolour = g_light_colours[0].rgb;\n"
195 " vec3 vdir = g_light_directions[0].xyz;\n"
197 " vec3 specdir = reflect( -vdir, wnormal );\n"
198 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
199 " return vcolour*spec*fintensity;\n"
202 "vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, \n"
203 " vec3 light_pos, vec3 light_colour )\n"
205 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
207 " float quadratic = dot(light_delta,light_delta);\n"
208 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
209 " attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );\n"
211 " return light_colour*attenuation;\n"
216 "float sdLine( vec3 p, vec3 a, vec3 b )\n"
218 " vec3 pa = p - a;\n"
219 " vec3 ba = b - a;\n"
221 " float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n"
222 " return length( pa - ba*h );\n"
225 "float compute_board_shadow()\n"
227 " // player shadow\n"
228 " float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 );\n"
229 " float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );\n"
230 " player_shadow *= player_shadow*player_shadow*player_shadow;\n"
232 " return 1.0 - player_shadow*0.8;\n"
235 "vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )\n"
238 " vec3 halfview = uCamera - aWorldCo;\n"
239 " float fdist = length(halfview);\n"
240 " halfview /= fdist;\n"
242 " vec3 total_light = newlight_compute_ambient();\n"
244 " // Compute world lighting contribution and apply it according to the\n"
247 " vec3 world_light = newlight_compute_world_diffuse( wnormal );\n"
248 " world_light += newlight_compute_sun_spec( wnormal, halfview, 0.1 );\n"
250 " float world_shadow = newlight_compute_sun_shadow();\n"
251 " float board_shadow = compute_board_shadow();\n"
253 " total_light += world_light * min( board_shadow, world_shadow );\n"
255 " // Compute the other lights that exist in the map, not effected by the sun\n"
258 " total_light += newlight_compute_quadratic\n"
260 " wnormal, halfview,\n"
261 " light_positions[0].xyz,\n"
262 " light_colours[0].rgb \n"
263 " ) * board_shadow;\n"
264 " total_light += newlight_compute_quadratic\n"
266 " wnormal, halfview,\n"
267 " light_positions[1].xyz,\n"
268 " light_colours[1].rgb \n"
269 " ) * board_shadow;\n"
270 " total_light += newlight_compute_quadratic\n"
272 " wnormal, halfview,\n"
273 " light_positions[2].xyz,\n"
274 " light_colours[2].rgb \n"
275 " ) * board_shadow;\n"
277 " return apply_fog( diffuse * total_light, fdist );\n"
282 "const float k_motion_lerp_amount = 0.01;\n"
286 "layout (location = 1) out vec2 oMotionVec;\n"
288 "in vec3 aMotionVec0;\n"
289 "in vec3 aMotionVec1;\n"
291 "void compute_motion_vectors()\n"
293 " // Write motion vectors\n"
294 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
295 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
297 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
304 " compute_motion_vectors();\n"
306 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
307 " vec4 vsamplemain = texture( uTexMain, aUv );\n"
308 " vec3 qnorm = aNorm.xyz;\n"
310 " vfrag = vsamplemain.rgb;\n"
312 " if( g_light_preview == 1 )\n"
314 " vfrag = vec3(0.5);\n"
317 " vfrag = scene_do_lighting( vfrag, qnorm );\n"
318 " oColour = vec4( vfrag, 1.0 );\n"
323 static GLuint _uniform_scene_standard_uMdl
;
324 static GLuint _uniform_scene_standard_uPv
;
325 static GLuint _uniform_scene_standard_uPvmPrev
;
326 static GLuint _uniform_scene_standard_uLightsArray
;
327 static GLuint _uniform_scene_standard_uTexGarbage
;
328 static GLuint _uniform_scene_standard_uTexMain
;
329 static GLuint _uniform_scene_standard_uCamera
;
330 static GLuint _uniform_scene_standard_uPlane
;
331 static GLuint _uniform_scene_standard_uBoard0
;
332 static GLuint _uniform_scene_standard_uBoard1
;
333 static GLuint _uniform_scene_standard_g_world_depth
;
334 static void shader_scene_standard_uMdl(m4x3f m
){
335 glUniformMatrix4x3fv(_uniform_scene_standard_uMdl
,1,GL_FALSE
,(float*)m
);
337 static void shader_scene_standard_uPv(m4x4f m
){
338 glUniformMatrix4fv(_uniform_scene_standard_uPv
,1,GL_FALSE
,(float*)m
);
340 static void shader_scene_standard_uPvmPrev(m4x4f m
){
341 glUniformMatrix4fv(_uniform_scene_standard_uPvmPrev
,1,GL_FALSE
,(float*)m
);
343 static void shader_scene_standard_uTexGarbage(int i
){
344 glUniform1i(_uniform_scene_standard_uTexGarbage
,i
);
346 static void shader_scene_standard_uTexMain(int i
){
347 glUniform1i(_uniform_scene_standard_uTexMain
,i
);
349 static void shader_scene_standard_uCamera(v3f v
){
350 glUniform3fv(_uniform_scene_standard_uCamera
,1,v
);
352 static void shader_scene_standard_uPlane(v4f v
){
353 glUniform4fv(_uniform_scene_standard_uPlane
,1,v
);
355 static void shader_scene_standard_uBoard0(v3f v
){
356 glUniform3fv(_uniform_scene_standard_uBoard0
,1,v
);
358 static void shader_scene_standard_uBoard1(v3f v
){
359 glUniform3fv(_uniform_scene_standard_uBoard1
,1,v
);
361 static void shader_scene_standard_g_world_depth(int i
){
362 glUniform1i(_uniform_scene_standard_g_world_depth
,i
);
364 static void shader_scene_standard_register(void){
365 vg_shader_register( &_shader_scene_standard
);
367 static void shader_scene_standard_use(void){ glUseProgram(_shader_scene_standard
.id
); }
368 static void shader_scene_standard_link(void){
369 _uniform_scene_standard_uMdl
= glGetUniformLocation( _shader_scene_standard
.id
, "uMdl" );
370 _uniform_scene_standard_uPv
= glGetUniformLocation( _shader_scene_standard
.id
, "uPv" );
371 _uniform_scene_standard_uPvmPrev
= glGetUniformLocation( _shader_scene_standard
.id
, "uPvmPrev" );
372 _uniform_scene_standard_uLightsArray
= glGetUniformLocation( _shader_scene_standard
.id
, "uLightsArray" );
373 _uniform_scene_standard_uTexGarbage
= glGetUniformLocation( _shader_scene_standard
.id
, "uTexGarbage" );
374 _uniform_scene_standard_uTexMain
= glGetUniformLocation( _shader_scene_standard
.id
, "uTexMain" );
375 _uniform_scene_standard_uCamera
= glGetUniformLocation( _shader_scene_standard
.id
, "uCamera" );
376 _uniform_scene_standard_uPlane
= glGetUniformLocation( _shader_scene_standard
.id
, "uPlane" );
377 _uniform_scene_standard_uBoard0
= glGetUniformLocation( _shader_scene_standard
.id
, "uBoard0" );
378 _uniform_scene_standard_uBoard1
= glGetUniformLocation( _shader_scene_standard
.id
, "uBoard1" );
379 _uniform_scene_standard_g_world_depth
= glGetUniformLocation( _shader_scene_standard
.id
, "g_world_depth" );
381 #endif /* SHADER_scene_standard_H */