1 #ifndef SHADER_standard_H
2 #define SHADER_standard_H
3 static void shader_standard_link(void);
4 static void shader_standard_register(void);
5 static struct vg_shader _shader_standard
= {
7 .link
= shader_standard_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 "uniform mat4x3 uMdl;\n"
27 "out vec3 aWorldCo;\n"
31 " vec3 world_pos = uMdl * vec4(a_co,1.0);\n"
32 " gl_Position = uPv * vec4( world_pos, 1.0 );\n"
33 " aColour = a_colour;\n"
35 " aNorm = mat3(uMdl) * a_norm;\n"
37 " aWorldCo = world_pos;\n"
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"
68 " int g_shadow_samples;\n"
71 "uniform sampler2D g_world_depth;\n"
73 "// Standard diffuse + spec models\n"
74 "// ==============================\n"
76 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
78 " vec3 vtotal = g_ambient_colour.rgb;\n"
80 " for( int i=0; i<g_light_count; i++ )\n"
82 " vec3 vcolour = g_light_colours[i].rgb;\n"
83 " vec3 vdir = g_light_directions[i].xyz;\n"
85 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
86 " vtotal += vcolour*flight;\n"
89 " return vfrag * vtotal;\n"
92 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
94 " vec3 vcolour = g_light_colours[0].rgb;\n"
95 " vec3 vdir = g_light_directions[0].xyz;\n"
97 " vec3 specdir = reflect( -vdir, wnormal );\n"
98 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
99 " return vfrag + vcolour*spec*fintensity;\n"
102 "float world_depth_sample( vec3 pos )\n"
104 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
105 " return texture( g_world_depth, depth_coord ).r;\n"
108 "float shadow_sample( vec3 vdir )\n"
110 " vec3 sample_pos = aWorldCo + vdir;\n"
111 " float height_sample = world_depth_sample( sample_pos );\n"
113 " float fdelta = height_sample - sample_pos.y;\n"
114 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
117 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
119 " float faccum = 0.0;\n"
120 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
121 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
122 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
123 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
124 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
125 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
126 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
127 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
128 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
131 "vec3 do_light_shadowing( vec3 vfrag )\n"
133 " if( g_shadow_samples == 0 )\n"
138 " float fspread = g_light_colours[0].w;\n"
139 " vec3 vdir = g_light_directions[0].xyz;\n"
140 " float flength = g_light_directions[0].w;\n"
142 " float famt = 0.0;\n"
143 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
144 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
145 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
146 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
147 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
148 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
149 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
150 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
151 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
154 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
156 " float dist = pow(fdist*0.0008,1.2);\n"
157 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
164 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
165 " vec4 vsamplemain = texture( uTexMain, aUv );\n"
166 " vec3 qnorm = normalize(aNorm);\n"
168 " vfrag = vsamplemain.rgb;\n"
170 " if( g_light_preview == 1 )\n"
172 " vfrag = vec3(0.5);\n"
176 " vec3 halfview = uCamera - aWorldCo;\n"
177 " float fdist = length( halfview );\n"
178 " halfview /= fdist;\n"
180 " vfrag = do_light_diffuse( vfrag, qnorm );\n"
181 " vfrag = do_light_spec( vfrag, qnorm, halfview, 0.1 );\n"
182 " vfrag = do_light_shadowing( vfrag );\n"
183 " vfrag = apply_fog( vfrag, fdist );\n"
185 " FragColor = vec4(vfrag, 1.0);\n"
190 static GLuint _uniform_standard_uMdl
;
191 static GLuint _uniform_standard_uPv
;
192 static GLuint _uniform_standard_uTexGarbage
;
193 static GLuint _uniform_standard_uTexMain
;
194 static GLuint _uniform_standard_uCamera
;
195 static GLuint _uniform_standard_uPlane
;
196 static GLuint _uniform_standard_g_world_depth
;
197 static void shader_standard_uMdl(m4x3f m
){
198 glUniformMatrix4x3fv(_uniform_standard_uMdl
,1,GL_FALSE
,(float*)m
);
200 static void shader_standard_uPv(m4x4f m
){
201 glUniformMatrix4fv(_uniform_standard_uPv
,1,GL_FALSE
,(float*)m
);
203 static void shader_standard_uTexGarbage(int i
){
204 glUniform1i(_uniform_standard_uTexGarbage
,i
);
206 static void shader_standard_uTexMain(int i
){
207 glUniform1i(_uniform_standard_uTexMain
,i
);
209 static void shader_standard_uCamera(v3f v
){
210 glUniform3fv(_uniform_standard_uCamera
,1,v
);
212 static void shader_standard_uPlane(v4f v
){
213 glUniform4fv(_uniform_standard_uPlane
,1,v
);
215 static void shader_standard_g_world_depth(int i
){
216 glUniform1i(_uniform_standard_g_world_depth
,i
);
218 static void shader_standard_register(void){
219 vg_shader_register( &_shader_standard
);
221 static void shader_standard_use(void){ glUseProgram(_shader_standard
.id
); }
222 static void shader_standard_link(void){
223 _uniform_standard_uMdl
= glGetUniformLocation( _shader_standard
.id
, "uMdl" );
224 _uniform_standard_uPv
= glGetUniformLocation( _shader_standard
.id
, "uPv" );
225 _uniform_standard_uTexGarbage
= glGetUniformLocation( _shader_standard
.id
, "uTexGarbage" );
226 _uniform_standard_uTexMain
= glGetUniformLocation( _shader_standard
.id
, "uTexMain" );
227 _uniform_standard_uCamera
= glGetUniformLocation( _shader_standard
.id
, "uCamera" );
228 _uniform_standard_uPlane
= glGetUniformLocation( _shader_standard
.id
, "uPlane" );
229 _uniform_standard_g_world_depth
= glGetUniformLocation( _shader_standard
.id
, "g_world_depth" );
231 #endif /* SHADER_standard_H */