1 #ifndef SHADER_viewchar_H
2 #define SHADER_viewchar_H
3 static void shader_viewchar_link(void);
4 static void shader_viewchar_register(void);
5 static struct vg_shader _shader_viewchar
= {
7 .link
= shader_viewchar_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 "const float k_motion_lerp_amount = 0.05;\n"
24 "out vec3 aMotionVec0;\n"
25 "out vec3 aMotionVec1;\n"
27 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
29 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
31 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
32 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
39 "// TODO: Send a previous transform matrix stack\n"
41 "uniform mat4x3 uTransforms[32];\n"
47 "out vec3 aWorldCo;\n"
51 " vec4 co_local = vec4( a_co, 1.0 );\n"
52 " vec3 co0 = uTransforms[ a_groups[0] ] * co_local;\n"
53 " vec3 co1 = uTransforms[ a_groups[1] ] * co_local;\n"
54 " vec3 co2 = uTransforms[ a_groups[2] ] * co_local;\n"
55 " vec3 n0 = mat3(uTransforms[ a_groups[0] ]) * a_norm;\n"
56 " vec3 n1 = mat3(uTransforms[ a_groups[1] ]) * a_norm;\n"
57 " vec3 n2 = mat3(uTransforms[ a_groups[2] ]) * a_norm;\n"
59 " vec3 world_pos = co0*a_weights[0] + co1*a_weights[1] + co2*a_weights[2];\n"
60 " vec3 world_normal = n0*a_weights[0] + n1*a_weights[1] + n2*a_weights[2];\n"
62 " gl_Position = uPv * vec4( world_pos, 1.0 );\n"
63 " aColour = a_colour;\n"
65 " aNorm = world_normal;\n"
67 " aWorldCo = world_pos;\n"
70 " aMotionVec0 = vec3(1.0);\n"
71 " aMotionVec1 = vec3(1.0);\n"
77 "uniform sampler2D uTexMain;\n"
78 "uniform vec3 uCamera;\n"
87 "layout (location = 0) out vec4 oColour;\n"
89 "layout (std140) uniform ub_world_lighting\n"
91 " vec4 g_light_colours[3];\n"
92 " vec4 g_light_directions[3];\n"
93 " vec4 g_ambient_colour;\n"
95 " vec4 g_water_plane;\n"
96 " vec4 g_depth_bounds;\n"
97 " float g_water_fog;\n"
98 " int g_light_count;\n"
99 " int g_light_preview;\n"
100 " int g_shadow_samples;\n"
103 "uniform sampler2D g_world_depth;\n"
105 "// Standard diffuse + spec models\n"
106 "// ==============================\n"
108 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
110 " vec3 vtotal = g_ambient_colour.rgb;\n"
112 " for( int i=0; i<g_light_count; i++ )\n"
114 " vec3 vcolour = g_light_colours[i].rgb;\n"
115 " vec3 vdir = g_light_directions[i].xyz;\n"
117 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
118 " vtotal += vcolour*flight;\n"
121 " return vfrag * vtotal;\n"
124 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
126 " vec3 vcolour = g_light_colours[0].rgb;\n"
127 " vec3 vdir = g_light_directions[0].xyz;\n"
129 " vec3 specdir = reflect( -vdir, wnormal );\n"
130 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
131 " return vfrag + vcolour*spec*fintensity;\n"
134 "float world_depth_sample( vec3 pos )\n"
136 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
137 " return texture( g_world_depth, depth_coord ).r;\n"
140 "float shadow_sample( vec3 vdir )\n"
142 " vec3 sample_pos = aWorldCo + vdir;\n"
143 " float height_sample = world_depth_sample( sample_pos );\n"
145 " float fdelta = height_sample - sample_pos.y;\n"
146 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
149 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
151 " float faccum = 0.0;\n"
152 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
153 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
154 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
155 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
156 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
157 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
158 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
159 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
160 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
163 "vec3 do_light_shadowing( vec3 vfrag )\n"
165 " if( g_shadow_samples == 0 )\n"
170 " float fspread = g_light_colours[0].w;\n"
171 " vec3 vdir = g_light_directions[0].xyz;\n"
172 " float flength = g_light_directions[0].w;\n"
174 " float famt = 0.0;\n"
175 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
176 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
177 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
178 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
179 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
180 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
181 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
182 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
183 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
186 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
188 " float dist = pow(fdist*0.0008,1.2);\n"
189 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
194 "const float k_motion_lerp_amount = 0.05;\n"
198 "layout (location = 1) out vec2 oMotionVec;\n"
200 "in vec3 aMotionVec0;\n"
201 "in vec3 aMotionVec1;\n"
203 "void compute_motion_vectors()\n"
205 " // Write motion vectors\n"
206 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
207 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
209 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
216 " compute_motion_vectors();\n"
218 " vec3 vfrag = texture( uTexMain, aUv ).rgb;\n"
221 " vec3 halfview = uCamera - aWorldCo;\n"
222 " float fdist = length( halfview );\n"
223 " halfview /= fdist;\n"
225 " vec3 qnorm = normalize(floor(aNorm*2.0)*0.5) + vec3(0.001,0.0,0.0);\n"
227 " vfrag = mix( vfrag, do_light_diffuse( vfrag, qnorm ), 0.5 );\n"
228 " //vfrag = do_light_spec( vfrag, qnorm, halfview, 0.1 );\n"
229 " vfrag = do_light_shadowing( vfrag );\n"
230 " vfrag = apply_fog( vfrag, fdist );\n"
232 " float opacity = clamp( fdist*fdist, 0.1, 1.0 );\n"
233 " oColour = vec4(vfrag,opacity);\n"
238 static GLuint _uniform_viewchar_uPv
;
239 static GLuint _uniform_viewchar_uTransforms
;
240 static GLuint _uniform_viewchar_uTexMain
;
241 static GLuint _uniform_viewchar_uCamera
;
242 static GLuint _uniform_viewchar_g_world_depth
;
243 static void shader_viewchar_uPv(m4x4f m
){
244 glUniformMatrix4fv(_uniform_viewchar_uPv
,1,GL_FALSE
,(float*)m
);
246 static void shader_viewchar_uTexMain(int i
){
247 glUniform1i(_uniform_viewchar_uTexMain
,i
);
249 static void shader_viewchar_uCamera(v3f v
){
250 glUniform3fv(_uniform_viewchar_uCamera
,1,v
);
252 static void shader_viewchar_g_world_depth(int i
){
253 glUniform1i(_uniform_viewchar_g_world_depth
,i
);
255 static void shader_viewchar_register(void){
256 vg_shader_register( &_shader_viewchar
);
258 static void shader_viewchar_use(void){ glUseProgram(_shader_viewchar
.id
); }
259 static void shader_viewchar_link(void){
260 _uniform_viewchar_uPv
= glGetUniformLocation( _shader_viewchar
.id
, "uPv" );
261 _uniform_viewchar_uTransforms
= glGetUniformLocation( _shader_viewchar
.id
, "uTransforms" );
262 _uniform_viewchar_uTexMain
= glGetUniformLocation( _shader_viewchar
.id
, "uTexMain" );
263 _uniform_viewchar_uCamera
= glGetUniformLocation( _shader_viewchar
.id
, "uCamera" );
264 _uniform_viewchar_g_world_depth
= glGetUniformLocation( _shader_viewchar
.id
, "g_world_depth" );
266 #endif /* SHADER_viewchar_H */