1 #ifndef SHADER_character_H
2 #define SHADER_character_H
3 static void shader_character_link(void);
4 static void shader_character_register(void);
5 static struct vg_shader _shader_character
= {
7 .link
= shader_character_link
,
10 .orig_file
= "../../shaders/character.vs",
12 "layout (location=0) in vec3 a_co;\n"
13 "layout (location=1) in vec3 a_norm;\n"
14 "layout (location=2) in vec2 a_uv;\n"
15 "layout (location=3) in vec4 a_colour;\n"
16 "layout (location=4) in vec4 a_weights;\n"
17 "layout (location=5) in ivec4 a_groups;\n"
22 "uniform mat4x3 uMdl;\n"
23 "uniform float uOpacity;\n"
29 "out vec3 aWorldCo;\n"
30 "out float aOpacity;\n"
34 " vec3 world_pos = uMdl * vec4(a_co,1.0);\n"
35 " vec4 clip_pos = uPv * vec4(world_pos,1.0);\n"
36 " gl_Position = clip_pos;\n"
38 " aColour = a_colour;\n"
40 " aNorm = mat3(uMdl) * a_norm;\n"
41 " aWorldCo = world_pos;\n"
43 " aOpacity = max(clip_pos.w*3.0,0.1);// 1.0-(gl_Position.y+0.5)*uOpacity;\n"
48 .orig_file
= "../../shaders/character.fs",
50 "out vec4 FragColor;\n"
52 "uniform sampler2D uTexMain;\n"
53 "uniform vec4 uColour;\n"
54 "uniform vec3 uCamera;\n"
61 "in float aOpacity;\n"
64 "layout (std140) uniform ub_world_lighting\n"
66 " vec4 g_light_colours[3];\n"
67 " vec4 g_light_directions[3];\n"
68 " vec4 g_ambient_colour;\n"
70 " vec4 g_water_plane;\n"
71 " vec4 g_depth_bounds;\n"
72 " float g_water_fog;\n"
73 " int g_light_count;\n"
74 " int g_light_preview;\n"
77 "uniform sampler2D g_world_depth;\n"
79 "// Standard diffuse + spec models\n"
80 "// ==============================\n"
82 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
84 " vec3 vtotal = g_ambient_colour.rgb;\n"
86 " for( int i=0; i<g_light_count; i++ )\n"
88 " vec3 vcolour = g_light_colours[i].rgb;\n"
89 " vec3 vdir = g_light_directions[i].xyz;\n"
91 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
92 " vtotal += vcolour*flight;\n"
95 " return vfrag * vtotal;\n"
98 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
100 " vec3 vcolour = g_light_colours[0].rgb;\n"
101 " vec3 vdir = g_light_directions[0].xyz;\n"
103 " vec3 specdir = reflect( -vdir, wnormal );\n"
104 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
105 " return vfrag + vcolour*spec*fintensity;\n"
108 "float world_depth_sample( vec3 pos )\n"
110 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
111 " return texture( g_world_depth, depth_coord ).r;\n"
114 "float shadow_sample( vec3 vdir )\n"
116 " vec3 sample_pos = aWorldCo + vdir;\n"
117 " float height_sample = world_depth_sample( sample_pos );\n"
119 " float fdelta = height_sample - sample_pos.y;\n"
120 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
123 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
125 " float faccum = 0.0;\n"
126 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
127 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
128 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
129 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
130 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
131 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
132 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
133 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
134 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
137 "vec3 do_light_shadowing( vec3 vfrag )\n"
139 " float fspread = g_light_colours[0].w;\n"
140 " vec3 vdir = g_light_directions[0].xyz;\n"
141 " float flength = g_light_directions[0].w;\n"
143 " float famt = 0.0;\n"
144 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
145 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
146 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
147 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
148 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
149 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
150 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
151 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
152 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
155 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
157 " float dist = pow(fdist*0.0008,1.2);\n"
158 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
165 " vec3 vfrag = texture( uTexMain, aUv ).rgb;\n"
168 " vec3 halfview = uCamera - aWorldCo;\n"
169 " float fdist = length( halfview );\n"
170 " halfview /= fdist;\n"
172 " //vfrag = do_light_diffuse( vfrag, aNorm );\n"
173 " vfrag = do_light_spec( vfrag, aNorm, halfview, 0.1 );\n"
174 " vfrag = do_light_shadowing( vfrag );\n"
175 " //vfrag = apply_fog( vfrag, fdist );\n"
177 " FragColor = vec4(vfrag,aOpacity);\n"
182 static GLuint _uniform_character_uPv
;
183 static GLuint _uniform_character_uMdl
;
184 static GLuint _uniform_character_uOpacity
;
185 static GLuint _uniform_character_uTexMain
;
186 static GLuint _uniform_character_uColour
;
187 static GLuint _uniform_character_uCamera
;
188 static GLuint _uniform_character_g_world_depth
;
189 static void shader_character_uPv(m4x4f m
){
190 glUniformMatrix4fv( _uniform_character_uPv
, 1, GL_FALSE
, (float *)m
);
192 static void shader_character_uMdl(m4x3f m
){
193 glUniformMatrix4x3fv( _uniform_character_uMdl
, 1, GL_FALSE
, (float *)m
);
195 static void shader_character_uOpacity(float f
){
196 glUniform1f( _uniform_character_uOpacity
, f
);
198 static void shader_character_uTexMain(int i
){
199 glUniform1i( _uniform_character_uTexMain
, i
);
201 static void shader_character_uColour(v4f v
){
202 glUniform4fv( _uniform_character_uColour
, 1, v
);
204 static void shader_character_uCamera(v3f v
){
205 glUniform3fv( _uniform_character_uCamera
, 1, v
);
207 static void shader_character_g_world_depth(int i
){
208 glUniform1i( _uniform_character_g_world_depth
, i
);
210 static void shader_character_register(void){
211 vg_shader_register( &_shader_character
);
213 static void shader_character_use(void){ glUseProgram(_shader_character
.id
); }
214 static void shader_character_link(void){
215 _uniform_character_uPv
= glGetUniformLocation( _shader_character
.id
, "uPv" );
216 _uniform_character_uMdl
= glGetUniformLocation( _shader_character
.id
, "uMdl" );
217 _uniform_character_uOpacity
= glGetUniformLocation( _shader_character
.id
, "uOpacity" );
218 _uniform_character_uTexMain
= glGetUniformLocation( _shader_character
.id
, "uTexMain" );
219 _uniform_character_uColour
= glGetUniformLocation( _shader_character
.id
, "uColour" );
220 _uniform_character_uCamera
= glGetUniformLocation( _shader_character
.id
, "uCamera" );
221 _uniform_character_g_world_depth
= glGetUniformLocation( _shader_character
.id
, "g_world_depth" );
223 #endif /* SHADER_character_H */