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