1 #ifndef SHADER_alphatest_H
2 #define SHADER_alphatest_H
3 static void shader_alphatest_link(void);
4 static void shader_alphatest_register(void);
5 static struct vg_shader _shader_alphatest
= {
7 .link
= shader_alphatest_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/std_alphatest.fs",
43 "out vec4 FragColor;\n"
45 "uniform sampler2D uTexGarbage;\n"
46 "uniform sampler2D uTexMain;\n"
47 "uniform vec3 uCamera;\n"
48 "uniform vec4 uPlane;\n"
57 "layout (std140) uniform ub_world_lighting\n"
59 " vec4 g_light_colours[3];\n"
60 " vec4 g_light_directions[3];\n"
61 " vec4 g_ambient_colour;\n"
63 " vec4 g_water_plane;\n"
64 " vec4 g_depth_bounds;\n"
65 " float g_water_fog;\n"
66 " int g_light_count;\n"
67 " int g_light_preview;\n"
70 "uniform sampler2D g_world_depth;\n"
72 "// Standard diffuse + spec models\n"
73 "// ==============================\n"
75 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
77 " vec3 vtotal = g_ambient_colour.rgb;\n"
79 " for( int i=0; i<g_light_count; i++ )\n"
81 " vec3 vcolour = g_light_colours[i].rgb;\n"
82 " vec3 vdir = g_light_directions[i].xyz;\n"
84 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
85 " vtotal += vcolour*flight;\n"
88 " return vfrag * vtotal;\n"
91 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
93 " vec3 vcolour = g_light_colours[0].rgb;\n"
94 " vec3 vdir = g_light_directions[0].xyz;\n"
96 " vec3 specdir = reflect( -vdir, wnormal );\n"
97 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
98 " return vfrag + vcolour*spec*fintensity;\n"
101 "float world_depth_sample( vec3 pos )\n"
103 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
104 " return texture( g_world_depth, depth_coord ).r;\n"
107 "float shadow_sample( vec3 vdir )\n"
109 " vec3 sample_pos = aWorldCo + vdir;\n"
110 " float height_sample = world_depth_sample( sample_pos );\n"
112 " float fdelta = height_sample - sample_pos.y;\n"
113 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
116 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
118 " float faccum = 0.0;\n"
119 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
120 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
121 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
122 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
123 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
124 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
125 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
126 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
127 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
130 "vec3 do_light_shadowing( vec3 vfrag )\n"
132 " float fspread = g_light_colours[0].w;\n"
133 " vec3 vdir = g_light_directions[0].xyz;\n"
134 " float flength = g_light_directions[0].w;\n"
136 " float famt = 0.0;\n"
137 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
138 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
139 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
140 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
141 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
142 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
143 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
144 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
145 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
148 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
150 " float dist = pow(fdist*0.0008,1.2);\n"
151 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
158 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
159 " vec4 vsamplemain = texture( uTexMain, aUv );\n"
160 " vec3 qnorm = normalize(aNorm);\n"
162 " if( vsamplemain.a < 0.15 )\n"
165 " vfrag = vsamplemain.rgb;\n"
167 " if( g_light_preview == 1 )\n"
169 " vfrag = vec3(0.5);\n"
173 " vec3 halfview = uCamera - aWorldCo;\n"
174 " float fdist = length( halfview );\n"
175 " halfview /= fdist;\n"
177 " vfrag = do_light_diffuse( vfrag, qnorm );\n"
178 " vfrag = do_light_spec( vfrag, qnorm, halfview, 0.1 );\n"
179 " vfrag = do_light_shadowing( vfrag );\n"
180 " vfrag = apply_fog( vfrag, fdist );\n"
182 " FragColor = vec4(vfrag, 1.0);\n"
187 static GLuint _uniform_alphatest_uPv
;
188 static GLuint _uniform_alphatest_uMdl
;
189 static GLuint _uniform_alphatest_uTexGarbage
;
190 static GLuint _uniform_alphatest_uTexMain
;
191 static GLuint _uniform_alphatest_uCamera
;
192 static GLuint _uniform_alphatest_uPlane
;
193 static GLuint _uniform_alphatest_g_world_depth
;
194 static void shader_alphatest_uPv(m4x4f m
){
195 glUniformMatrix4fv( _uniform_alphatest_uPv
, 1, GL_FALSE
, (float *)m
);
197 static void shader_alphatest_uMdl(m4x3f m
){
198 glUniformMatrix4x3fv( _uniform_alphatest_uMdl
, 1, GL_FALSE
, (float *)m
);
200 static void shader_alphatest_uTexGarbage(int i
){
201 glUniform1i( _uniform_alphatest_uTexGarbage
, i
);
203 static void shader_alphatest_uTexMain(int i
){
204 glUniform1i( _uniform_alphatest_uTexMain
, i
);
206 static void shader_alphatest_uCamera(v3f v
){
207 glUniform3fv( _uniform_alphatest_uCamera
, 1, v
);
209 static void shader_alphatest_uPlane(v4f v
){
210 glUniform4fv( _uniform_alphatest_uPlane
, 1, v
);
212 static void shader_alphatest_g_world_depth(int i
){
213 glUniform1i( _uniform_alphatest_g_world_depth
, i
);
215 static void shader_alphatest_register(void){
216 vg_shader_register( &_shader_alphatest
);
218 static void shader_alphatest_use(void){ glUseProgram(_shader_alphatest
.id
); }
219 static void shader_alphatest_link(void){
220 _uniform_alphatest_uPv
= glGetUniformLocation( _shader_alphatest
.id
, "uPv" );
221 _uniform_alphatest_uMdl
= glGetUniformLocation( _shader_alphatest
.id
, "uMdl" );
222 _uniform_alphatest_uTexGarbage
= glGetUniformLocation( _shader_alphatest
.id
, "uTexGarbage" );
223 _uniform_alphatest_uTexMain
= glGetUniformLocation( _shader_alphatest
.id
, "uTexMain" );
224 _uniform_alphatest_uCamera
= glGetUniformLocation( _shader_alphatest
.id
, "uCamera" );
225 _uniform_alphatest_uPlane
= glGetUniformLocation( _shader_alphatest
.id
, "uPlane" );
226 _uniform_alphatest_g_world_depth
= glGetUniformLocation( _shader_alphatest
.id
, "g_world_depth" );
228 #endif /* SHADER_alphatest_H */