checkin
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / model_character_view.h
1 #ifndef SHADER_model_character_view_H
2 #define SHADER_model_character_view_H
3 static void shader_model_character_view_link(void);
4 static void shader_model_character_view_register(void);
5 static struct vg_shader _shader_model_character_view = {
6 .name = "model_character_view",
7 .link = shader_model_character_view_link,
8 .vs =
9 {
10 .orig_file = "shaders/model_skinned.vs",
11 .static_src =
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"
18 "\n"
19 "#line 1 1 \n"
20 "const float k_motion_lerp_amount = 0.01;\n"
21 "\n"
22 "#line 2 0 \n"
23 "\n"
24 "out vec3 aMotionVec0;\n"
25 "out vec3 aMotionVec1;\n"
26 "\n"
27 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
28 "{\n"
29 " // This magically solves some artifacting errors!\n"
30 " //\n"
31 " vproj1 = vproj0*(1.0-k_motion_lerp_amount) + vproj1*k_motion_lerp_amount;\n"
32 "\n"
33 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
34 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
35 "}\n"
36 "\n"
37 "#line 9 0 \n"
38 "\n"
39 "uniform mat4 uPv;\n"
40 "uniform mat4x3 uTransforms[32];\n"
41 "\n"
42 "out vec4 aColour;\n"
43 "out vec2 aUv;\n"
44 "out vec3 aNorm;\n"
45 "out vec3 aCo;\n"
46 "out vec3 aWorldCo;\n"
47 "\n"
48 "void main()\n"
49 "{\n"
50 " vec4 co_local = vec4( a_co, 1.0 );\n"
51 " vec3 co0 = uTransforms[ a_groups[0] ] * co_local;\n"
52 " vec3 co1 = uTransforms[ a_groups[1] ] * co_local;\n"
53 " vec3 co2 = uTransforms[ a_groups[2] ] * co_local;\n"
54 " vec3 n0 = mat3(uTransforms[ a_groups[0] ]) * a_norm;\n"
55 " vec3 n1 = mat3(uTransforms[ a_groups[1] ]) * a_norm;\n"
56 " vec3 n2 = mat3(uTransforms[ a_groups[2] ]) * a_norm;\n"
57 "\n"
58 " vec3 world_pos = co0*a_weights[0] + co1*a_weights[1] + co2*a_weights[2];\n"
59 " vec3 world_normal = n0*a_weights[0] + n1*a_weights[1] + n2*a_weights[2];\n"
60 " \n"
61 " gl_Position = uPv * vec4( world_pos, 1.0 );\n"
62 " aColour = a_colour;\n"
63 " aUv = a_uv;\n"
64 " aNorm = world_normal;\n"
65 " aCo = a_co;\n"
66 " aWorldCo = world_pos;\n"
67 "\n"
68 " // TODO:\n"
69 " aMotionVec0 = vec3(1.0);\n"
70 " aMotionVec1 = vec3(1.0);\n"
71 "}\n"
72 ""},
73 .fs =
74 {
75 .orig_file = "shaders/model_character_view.fs",
76 .static_src =
77 "uniform sampler2D uTexMain;\n"
78 "uniform vec3 uCamera;\n"
79 "\n"
80 "in vec4 aColour;\n"
81 "in vec2 aUv;\n"
82 "in vec3 aNorm;\n"
83 "in vec3 aCo;\n"
84 "in vec3 aWorldCo;\n"
85 "\n"
86 "#line 1 1 \n"
87 "layout (location = 0) out vec4 oColour;\n"
88 "\n"
89 "layout (std140) uniform ub_world_lighting\n"
90 "{\n"
91 " vec4 g_light_colours[3];\n"
92 " vec4 g_light_directions[3];\n"
93 " vec4 g_ambient_colour;\n"
94 "\n"
95 " vec4 g_water_plane;\n"
96 " vec4 g_depth_bounds;\n"
97 " float g_water_fog;\n"
98 " float g_time;\n"
99 " int g_light_count;\n"
100 " int g_light_preview;\n"
101 " int g_shadow_samples;\n"
102 "\n"
103 " int g_debug_indices;\n"
104 " int g_debug_complexity;\n"
105 "\n"
106 " // g_time ?\n"
107 "\n"
108 " //vec4 g_point_light_positions[32];\n"
109 " //vec4 g_point_light_colours[32];\n"
110 "};\n"
111 "\n"
112 "uniform sampler2D g_world_depth;\n"
113 "\n"
114 "float world_depth_sample( vec3 pos )\n"
115 "{\n"
116 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
117 " return texture( g_world_depth, depth_coord ).r;\n"
118 "}\n"
119 "\n"
120 "float world_water_depth( vec3 pos )\n"
121 "{\n"
122 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
123 " float ref_depth = g_water_plane.y*g_water_plane.w;\n"
124 " return texture( g_world_depth, depth_coord ).g - ref_depth;\n"
125 "}\n"
126 "\n"
127 "float shadow_sample( vec3 vdir )\n"
128 "{\n"
129 " vec3 sample_pos = aWorldCo + vdir;\n"
130 " float height_sample = world_depth_sample( sample_pos );\n"
131 "\n"
132 " float fdelta = height_sample - sample_pos.y;\n"
133 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
134 "}\n"
135 "\n"
136 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
137 "{\n"
138 " float dist = pow(fdist*0.0008,1.2);\n"
139 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
140 "}\n"
141 "\n"
142 "\n"
143 "// New lighting model\n"
144 "\n"
145 "vec3 newlight_compute_ambient()\n"
146 "{\n"
147 " return g_ambient_colour.rgb;\n"
148 "}\n"
149 "\n"
150 "float newlight_compute_sun_shadow( vec3 dir )\n"
151 "{\n"
152 " if( g_shadow_samples == 0 )\n"
153 " {\n"
154 " return 1.0;\n"
155 " }\n"
156 "\n"
157 " float fspread = g_light_colours[0].w;\n"
158 " vec3 vdir = dir;\n"
159 " float flength = g_light_directions[0].w;\n"
160 "\n"
161 " float famt = 0.0;\n"
162 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
163 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
164 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
165 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
166 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
167 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
168 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
169 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
170 "\n"
171 " return 1.0 - famt;\n"
172 "}\n"
173 "\n"
174 "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n"
175 "{\n"
176 " vec3 vtotal = g_ambient_colour.rgb;\n"
177 "\n"
178 " for( int i=0; i<g_light_count; i++ )\n"
179 " {\n"
180 " vec3 vcolour = g_light_colours[i].rgb;\n"
181 " vec3 vdir = g_light_directions[i].xyz;\n"
182 "\n"
183 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
184 " vtotal += vcolour*flight;\n"
185 " }\n"
186 "\n"
187 " return vtotal;\n"
188 "}\n"
189 "\n"
190 "vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )\n"
191 "{\n"
192 " vec3 vcolour = g_light_colours[0].rgb;\n"
193 " vec3 vdir = g_light_directions[0].xyz;\n"
194 "\n"
195 " vec3 specdir = reflect( -vdir, wnormal );\n"
196 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
197 " return vcolour*spec*fintensity;\n"
198 "}\n"
199 "\n"
200 "float newlight_specular( vec3 wnormal, vec3 dir, vec3 halfview, float exponent )\n"
201 "{\n"
202 " vec3 specdir = reflect( -dir, wnormal );\n"
203 " return pow(max(dot( halfview, specdir ), 0.0), exponent);\n"
204 "}\n"
205 "\n"
206 "vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, \n"
207 " vec3 light_colour, vec3 light_pos )\n"
208 "{\n"
209 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
210 "\n"
211 " float quadratic = dot(light_delta,light_delta);\n"
212 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
213 " attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );\n"
214 "\n"
215 " return light_colour*attenuation;\n"
216 "}\n"
217 "\n"
218 "vec3 newlight_compute_spot( vec3 wnormal, vec3 halfview, \n"
219 " vec3 light_colour, vec3 light_pos,\n"
220 " vec4 light_dir )\n"
221 "{\n"
222 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
223 "\n"
224 " float quadratic = dot(light_delta,light_delta);\n"
225 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
226 "\n"
227 " light_delta = normalize( light_delta );\n"
228 " attenuation *= max( 0.0, dot( light_delta, wnormal ) );\n"
229 "\n"
230 " float spot_theta = max( 0.0, dot( light_delta, -light_dir.xyz ) ),\n"
231 " falloff = max( 0.0,( spot_theta - light_dir.w ) / (1.0-light_dir.w) );\n"
232 "\n"
233 " return light_colour*attenuation*falloff;\n"
234 "}\n"
235 "\n"
236 "#line 11 0 \n"
237 "#line 1 2 \n"
238 "const float k_motion_lerp_amount = 0.01;\n"
239 "\n"
240 "#line 2 0 \n"
241 "\n"
242 "layout (location = 1) out vec2 oMotionVec;\n"
243 "\n"
244 "in vec3 aMotionVec0;\n"
245 "in vec3 aMotionVec1;\n"
246 "\n"
247 "void compute_motion_vectors()\n"
248 "{\n"
249 " // Write motion vectors\n"
250 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
251 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
252 "\n"
253 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
254 "}\n"
255 "\n"
256 "#line 12 0 \n"
257 "\n"
258 "void main()\n"
259 "{\n"
260 " compute_motion_vectors();\n"
261 "\n"
262 " vec3 vfrag = texture( uTexMain, aUv ).rgb;\n"
263 "\n"
264 " // Lighting\n"
265 " vec3 halfview = uCamera - aWorldCo;\n"
266 " float fdist = length( halfview );\n"
267 " halfview /= fdist;\n"
268 " fdist -= 0.08;\n"
269 "\n"
270 " vec3 qnorm = normalize(floor(aNorm*2.0)*0.5) + vec3(0.001,0.0,0.0);\n"
271 "\n"
272 " vec3 total_light = newlight_compute_ambient();\n"
273 " vec3 world_light = newlight_compute_world_diffuse( qnorm );\n"
274 "\n"
275 " float world_shadow = newlight_compute_sun_shadow( vec3(1.0) );\n"
276 " total_light += world_light * world_shadow;\n"
277 "\n"
278 " vfrag = apply_fog( vfrag * total_light, fdist );\n"
279 "\n"
280 " float opacity = clamp( fdist*fdist, 0.0, 1.0 );\n"
281 " oColour = vec4(vfrag,opacity);\n"
282 "}\n"
283 ""},
284 };
285
286 static GLuint _uniform_model_character_view_uPv;
287 static GLuint _uniform_model_character_view_uTransforms;
288 static GLuint _uniform_model_character_view_uTexMain;
289 static GLuint _uniform_model_character_view_uCamera;
290 static GLuint _uniform_model_character_view_g_world_depth;
291 static void shader_model_character_view_uPv(m4x4f m){
292 glUniformMatrix4fv(_uniform_model_character_view_uPv,1,GL_FALSE,(float*)m);
293 }
294 static void shader_model_character_view_uTexMain(int i){
295 glUniform1i(_uniform_model_character_view_uTexMain,i);
296 }
297 static void shader_model_character_view_uCamera(v3f v){
298 glUniform3fv(_uniform_model_character_view_uCamera,1,v);
299 }
300 static void shader_model_character_view_g_world_depth(int i){
301 glUniform1i(_uniform_model_character_view_g_world_depth,i);
302 }
303 static void shader_model_character_view_register(void){
304 vg_shader_register( &_shader_model_character_view );
305 }
306 static void shader_model_character_view_use(void){ glUseProgram(_shader_model_character_view.id); }
307 static void shader_model_character_view_link(void){
308 _uniform_model_character_view_uPv = glGetUniformLocation( _shader_model_character_view.id, "uPv" );
309 _uniform_model_character_view_uTransforms = glGetUniformLocation( _shader_model_character_view.id, "uTransforms" );
310 _uniform_model_character_view_uTexMain = glGetUniformLocation( _shader_model_character_view.id, "uTexMain" );
311 _uniform_model_character_view_uCamera = glGetUniformLocation( _shader_model_character_view.id, "uCamera" );
312 _uniform_model_character_view_g_world_depth = glGetUniformLocation( _shader_model_character_view.id, "g_world_depth" );
313 }
314 #endif /* SHADER_model_character_view_H */