3 static void shader_gpos_link(void);
4 static void shader_gpos_register(void);
5 static struct vg_shader _shader_gpos
= {
7 .link
= shader_gpos_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"
29 " gl_Position = uPv * vec4( uMdl * vec4(a_co,1.0), 1.0 );\n"
30 " aColour = a_colour;\n"
32 " aNorm = mat3(uMdl) * a_norm;\n"
38 .orig_file
= "../shaders/gpos.fs",
40 "out vec4 FragColor;\n"
42 "uniform vec3 uCamera;\n"
50 "layout (std140) uniform ub_world_lighting\n"
52 " vec3 g_directional;\n"
53 " vec3 g_sun_colour;\n"
54 " vec3 g_shadow_colour;\n"
55 " vec4 g_water_plane;\n"
56 " vec4 g_depth_bounds;\n"
57 " float g_water_fog;\n"
60 "uniform sampler2D g_world_depth;\n"
62 "// Standard diffuse + spec models\n"
63 "// ==============================\n"
65 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
67 " float flight = dot( g_directional, wnormal )*0.5+0.5;\n"
68 " return vfrag * mix( g_shadow_colour, g_sun_colour, flight );\n"
71 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
73 " vec3 specdir = reflect( -g_directional, wnormal );\n"
74 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
75 " return vfrag + g_sun_colour*spec*fintensity;\n"
78 "float world_depth_sample( vec3 pos )\n"
80 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
81 " return texture( g_world_depth, depth_coord ).r;\n"
84 "float shadow_sample( vec3 vdir )\n"
86 " vec3 sample_pos = aCo + vdir;\n"
87 " float height_sample = world_depth_sample( sample_pos );\n"
89 " float fdelta = height_sample - sample_pos.y;\n"
90 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
93 "vec3 do_light_shadowing( vec3 vfrag )\n"
95 " float faccum = 0.0;\n"
96 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
97 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
98 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
99 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
100 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
101 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
102 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
103 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
104 " return mix( vfrag, g_shadow_colour, faccum );\n"
110 "// Water blending\n"
111 "// ==============\n"
113 "float water_depth( vec3 pos, vec3 halfview )\n"
115 " vec3 pnorm = g_water_plane.xyz;\n"
116 " float pdist = g_water_plane.w;\n"
118 " float d = dot( pnorm, halfview );\n"
119 " float t = dot((pnorm*pdist - pos), pnorm) / d;\n"
120 " return t * g_water_fog;\n"
125 " vec3 halfview = normalize( uCamera - aCo );\n"
126 " vec3 world_pos = vec3( aCo.y, aCo.x, aCo.z );\n"
127 " FragColor = vec4( world_pos, water_depth( aCo, halfview ) );\n"
132 static GLuint _uniform_gpos_uPv
;
133 static GLuint _uniform_gpos_uMdl
;
134 static GLuint _uniform_gpos_uCamera
;
135 static GLuint _uniform_gpos_g_world_depth
;
136 static void shader_gpos_uPv(m4x4f m
){
137 glUniformMatrix4fv( _uniform_gpos_uPv
, 1, GL_FALSE
, (float *)m
);
139 static void shader_gpos_uMdl(m4x3f m
){
140 glUniformMatrix4x3fv( _uniform_gpos_uMdl
, 1, GL_FALSE
, (float *)m
);
142 static void shader_gpos_uCamera(v3f v
){
143 glUniform3fv( _uniform_gpos_uCamera
, 1, v
);
145 static void shader_gpos_g_world_depth(int i
){
146 glUniform1i( _uniform_gpos_g_world_depth
, i
);
148 static void shader_gpos_register(void){
149 vg_shader_register( &_shader_gpos
);
151 static void shader_gpos_use(void){ glUseProgram(_shader_gpos
.id
); }
152 static void shader_gpos_link(void){
153 _uniform_gpos_uPv
= glGetUniformLocation( _shader_gpos
.id
, "uPv" );
154 _uniform_gpos_uMdl
= glGetUniformLocation( _shader_gpos
.id
, "uMdl" );
155 _uniform_gpos_uCamera
= glGetUniformLocation( _shader_gpos
.id
, "uCamera" );
156 _uniform_gpos_g_world_depth
= glGetUniformLocation( _shader_gpos
.id
, "g_world_depth" );
158 #endif /* SHADER_gpos_H */