now we're doing a bunch of them
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / model_sky.h
1 #ifndef SHADER_model_sky_H
2 #define SHADER_model_sky_H
3 static void shader_model_sky_link(void);
4 static void shader_model_sky_register(void);
5 static struct vg_shader _shader_model_sky = {
6 .name = "model_sky",
7 .link = shader_model_sky_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 mat4x3 uMdl;\n"
39 "uniform mat4 uPv;\n"
40 "uniform mat4 uPvmPrev;\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 " vec3 world_pos0 = uMdl * vec4( a_co, 1.0 );\n"
51 " vec4 vproj0 = uPv * vec4( world_pos0, 1.0 );\n"
52 " vec4 vproj1 = uPvmPrev * vec4( a_co, 1.0 );\n"
53 "\n"
54 " vs_motion_out( vproj0, vproj1 );\n"
55 "\n"
56 " gl_Position = vproj0;\n"
57 " aWorldCo = world_pos0;\n"
58 " aColour = a_colour;\n"
59 " aUv = a_uv;\n"
60 " aNorm = mat3(uMdl) * a_norm;\n"
61 " aCo = a_co;\n"
62 "}\n"
63 ""},
64 .fs =
65 {
66 .static_src =
67 "uniform sampler2D uTexGarbage;\n"
68 "uniform float uTime;\n"
69 "\n"
70 "in vec4 aColour;\n"
71 "in vec2 aUv;\n"
72 "in vec3 aNorm;\n"
73 "in vec3 aCo;\n"
74 "in vec3 aWorldCo;\n"
75 "\n"
76 "#line 1 1 \n"
77 "layout (location = 0) out vec4 oColour;\n"
78 "\n"
79 "layout (std140) uniform ub_world_lighting\n"
80 "{\n"
81 " vec4 g_light_colours[3];\n"
82 " vec4 g_light_directions[3];\n"
83 " vec4 g_ambient_colour;\n"
84 "\n"
85 " vec4 g_water_plane;\n"
86 " vec4 g_depth_bounds;\n"
87 " float g_water_fog;\n"
88 " int g_light_count;\n"
89 " int g_light_preview;\n"
90 " int g_shadow_samples;\n"
91 "\n"
92 " // g_time ?\n"
93 "\n"
94 " //vec4 g_point_light_positions[32];\n"
95 " //vec4 g_point_light_colours[32];\n"
96 "};\n"
97 "\n"
98 "uniform sampler2D g_world_depth;\n"
99 "\n"
100 "float world_depth_sample( vec3 pos )\n"
101 "{\n"
102 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
103 " return texture( g_world_depth, depth_coord ).r;\n"
104 "}\n"
105 "\n"
106 "float world_water_depth( vec3 pos )\n"
107 "{\n"
108 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
109 " float ref_depth = g_water_plane.y*g_water_plane.w;\n"
110 " return texture( g_world_depth, depth_coord ).g - ref_depth;\n"
111 "}\n"
112 "\n"
113 "float shadow_sample( vec3 vdir )\n"
114 "{\n"
115 " vec3 sample_pos = aWorldCo + vdir;\n"
116 " float height_sample = world_depth_sample( sample_pos );\n"
117 "\n"
118 " float fdelta = height_sample - sample_pos.y;\n"
119 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
120 "}\n"
121 "\n"
122 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
123 "{\n"
124 " float dist = pow(fdist*0.0008,1.2);\n"
125 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
126 "}\n"
127 "\n"
128 "\n"
129 "// New lighting model\n"
130 "\n"
131 "vec3 newlight_compute_ambient()\n"
132 "{\n"
133 " return g_ambient_colour.rgb;\n"
134 "}\n"
135 "\n"
136 "float newlight_compute_sun_shadow()\n"
137 "{\n"
138 " if( g_shadow_samples == 0 )\n"
139 " {\n"
140 " return 1.0;\n"
141 " }\n"
142 "\n"
143 " float fspread = g_light_colours[0].w;\n"
144 " vec3 vdir = g_light_directions[0].xyz;\n"
145 " float flength = g_light_directions[0].w;\n"
146 "\n"
147 " float famt = 0.0;\n"
148 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
149 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
150 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
151 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
152 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
153 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
154 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
155 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
156 "\n"
157 " return 1.0 - famt;\n"
158 "}\n"
159 "\n"
160 "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n"
161 "{\n"
162 " vec3 vtotal = g_ambient_colour.rgb;\n"
163 "\n"
164 " for( int i=0; i<g_light_count; i++ )\n"
165 " {\n"
166 " vec3 vcolour = g_light_colours[i].rgb;\n"
167 " vec3 vdir = g_light_directions[i].xyz;\n"
168 "\n"
169 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
170 " vtotal += vcolour*flight;\n"
171 " }\n"
172 "\n"
173 " return vtotal;\n"
174 "}\n"
175 "\n"
176 "vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )\n"
177 "{\n"
178 " vec3 vcolour = g_light_colours[0].rgb;\n"
179 " vec3 vdir = g_light_directions[0].xyz;\n"
180 "\n"
181 " vec3 specdir = reflect( -vdir, wnormal );\n"
182 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
183 " return vcolour*spec*fintensity;\n"
184 "}\n"
185 "\n"
186 "vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, \n"
187 " vec3 light_pos, vec3 light_colour )\n"
188 "{\n"
189 " vec3 light_delta = (light_pos-aWorldCo) * 10.0;\n"
190 "\n"
191 " float quadratic = dot(light_delta,light_delta);\n"
192 " float attenuation = 1.0f/( 1.0f + quadratic );\n"
193 " attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );\n"
194 "\n"
195 " return light_colour*attenuation;\n"
196 "}\n"
197 "\n"
198 "#line 11 0 \n"
199 "#line 1 2 \n"
200 "const float k_motion_lerp_amount = 0.01;\n"
201 "\n"
202 "#line 2 0 \n"
203 "\n"
204 "layout (location = 1) out vec2 oMotionVec;\n"
205 "\n"
206 "in vec3 aMotionVec0;\n"
207 "in vec3 aMotionVec1;\n"
208 "\n"
209 "void compute_motion_vectors()\n"
210 "{\n"
211 " // Write motion vectors\n"
212 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
213 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
214 "\n"
215 " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n"
216 "}\n"
217 "\n"
218 "#line 12 0 \n"
219 "\n"
220 "void main()\n"
221 "{\n"
222 " compute_motion_vectors();\n"
223 "\n"
224 " vec3 rd = normalize(aNorm);\n"
225 "\n"
226 " float fintensity = 1.0-(abs(rd.y)*0.7);\n"
227 " float fblend = pow(fintensity,4.0);\n"
228 " vec3 horizon = vec3( 0.87, 0.93, 0.98 );\n"
229 " vec3 skycolour = vec3( 0.16, 0.58, 0.95 ) - rd.y*rd.y*0.5;\n"
230 " vec3 diffuse = mix( skycolour, horizon, fblend );\n"
231 "\n"
232 " float fmove = uTime * 0.004;\n"
233 " vec2 cloudplane = (rd.xz / (rd.y*sign(rd.y))) * 0.05;\n"
234 " vec4 clouds1 = texture( uTexGarbage, cloudplane + vec2(0.1,0.4)*fmove*2.0 );\n"
235 " vec4 clouds2 = texture( uTexGarbage, cloudplane*2.0 + vec2(0.3,0.1)*fmove );\n"
236 "\n"
237 " float cloud_d = max(clouds1.b*clouds2.r -0.2 - clouds2.g*0.4,0.0);\n"
238 " float cloud_e = pow(cloud_d,1.5)*pow(abs(rd.y),0.3)*2.0;\n"
239 "\n"
240 " vec3 colour_ocean = vec3( 0.61, 0.84, 0.9 );\n"
241 " float fhorizon = step( rd.y * 0.5 + 0.5, 0.5 );\n"
242 "\n"
243 " vec3 skycomp = mix(diffuse, vec3(1.0,1.0,1.0), cloud_e);\n"
244 "\n"
245 "\n"
246 " float sundot = clamp(dot(rd, -g_light_directions[0].xyz), 0.0, 1.0);\n"
247 " vec3 sun = 0.25 * vec3(1.0,0.7,0.4) * pow( sundot,5.0 );\n"
248 " sun += 0.25 * vec3(1.0,0.8,0.6) * pow( sundot,64.0 );\n"
249 " sun += 0.2 * vec3(1.0,0.8,0.6) * pow( sundot,512.0 );\n"
250 " skycomp += sun * g_light_colours[0].rgb;\n"
251 "\n"
252 " oColour = vec4(skycomp,1.0);\n"
253 "}\n"
254 ""},
255 };
256
257 static GLuint _uniform_model_sky_uMdl;
258 static GLuint _uniform_model_sky_uPv;
259 static GLuint _uniform_model_sky_uPvmPrev;
260 static GLuint _uniform_model_sky_uTexGarbage;
261 static GLuint _uniform_model_sky_uTime;
262 static GLuint _uniform_model_sky_g_world_depth;
263 static void shader_model_sky_uMdl(m4x3f m){
264 glUniformMatrix4x3fv(_uniform_model_sky_uMdl,1,GL_FALSE,(float*)m);
265 }
266 static void shader_model_sky_uPv(m4x4f m){
267 glUniformMatrix4fv(_uniform_model_sky_uPv,1,GL_FALSE,(float*)m);
268 }
269 static void shader_model_sky_uPvmPrev(m4x4f m){
270 glUniformMatrix4fv(_uniform_model_sky_uPvmPrev,1,GL_FALSE,(float*)m);
271 }
272 static void shader_model_sky_uTexGarbage(int i){
273 glUniform1i(_uniform_model_sky_uTexGarbage,i);
274 }
275 static void shader_model_sky_uTime(float f){
276 glUniform1f(_uniform_model_sky_uTime,f);
277 }
278 static void shader_model_sky_g_world_depth(int i){
279 glUniform1i(_uniform_model_sky_g_world_depth,i);
280 }
281 static void shader_model_sky_register(void){
282 vg_shader_register( &_shader_model_sky );
283 }
284 static void shader_model_sky_use(void){ glUseProgram(_shader_model_sky.id); }
285 static void shader_model_sky_link(void){
286 _uniform_model_sky_uMdl = glGetUniformLocation( _shader_model_sky.id, "uMdl" );
287 _uniform_model_sky_uPv = glGetUniformLocation( _shader_model_sky.id, "uPv" );
288 _uniform_model_sky_uPvmPrev = glGetUniformLocation( _shader_model_sky.id, "uPvmPrev" );
289 _uniform_model_sky_uTexGarbage = glGetUniformLocation( _shader_model_sky.id, "uTexGarbage" );
290 _uniform_model_sky_uTime = glGetUniformLocation( _shader_model_sky.id, "uTime" );
291 _uniform_model_sky_g_world_depth = glGetUniformLocation( _shader_model_sky.id, "g_world_depth" );
292 }
293 #endif /* SHADER_model_sky_H */