3f5bfdc14ed136eb21c6856f253926a1af0e797f
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / vblend.h
1 #ifndef SHADER_vblend_H
2 #define SHADER_vblend_H
3 static void shader_vblend_link(void);
4 static void shader_vblend_register(void);
5 static struct vg_shader _shader_vblend = {
6 .name = "vblend",
7 .link = shader_vblend_link,
8 .vs =
9 {
10 .orig_file = "../shaders/standard.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 vec4 a_colour;\n"
15 "layout (location=3) in vec2 a_uv;\n"
16 "\n"
17 "#line 2 0 \n"
18 "\n"
19 "uniform mat4 uPv;\n"
20 "uniform mat4x3 uMdl;\n"
21 "\n"
22 "out vec4 aColour;\n"
23 "out vec2 aUv;\n"
24 "out vec3 aNorm;\n"
25 "out vec3 aCo;\n"
26 "out vec3 aWorldCo;\n"
27 "\n"
28 "void main()\n"
29 "{\n"
30 " vec3 world_pos = uMdl * vec4(a_co,1.0);\n"
31 " gl_Position = uPv * vec4( world_pos, 1.0 );\n"
32 " aColour = a_colour;\n"
33 " aUv = a_uv;\n"
34 " aNorm = mat3(uMdl) * a_norm;\n"
35 " aCo = a_co;\n"
36 " aWorldCo = world_pos;\n"
37 "}\n"
38 ""},
39 .fs =
40 {
41 .orig_file = "../shaders/vblend.fs",
42 .static_src =
43 "out vec4 FragColor;\n"
44 "\n"
45 "uniform sampler2D uTexGarbage;\n"
46 "uniform sampler2D uTexGradients;\n"
47 "uniform vec3 uCamera;\n"
48 "uniform vec4 uPlane;\n"
49 "\n"
50 "in vec4 aColour;\n"
51 "in vec2 aUv;\n"
52 "in vec3 aNorm;\n"
53 "in vec3 aCo;\n"
54 "in vec3 aWorldCo;\n"
55 "\n"
56 "#line 1 1 \n"
57 "layout (std140) uniform ub_world_lighting\n"
58 "{\n"
59 " vec4 g_light_colours[3];\n"
60 " vec4 g_light_directions[3];\n"
61 " vec4 g_ambient_colour;\n"
62 "\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 "};\n"
69 "\n"
70 "uniform sampler2D g_world_depth;\n"
71 "\n"
72 "// Standard diffuse + spec models\n"
73 "// ==============================\n"
74 "\n"
75 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
76 "{\n"
77 " vec3 vtotal = g_ambient_colour.rgb;\n"
78 "\n"
79 " for( int i=0; i<g_light_count; i++ )\n"
80 " {\n"
81 " vec3 vcolour = g_light_colours[i].rgb;\n"
82 " vec3 vdir = g_light_directions[i].xyz;\n"
83 "\n"
84 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
85 " vtotal += vcolour*flight;\n"
86 " }\n"
87 "\n"
88 " return vfrag * vtotal;\n"
89 "}\n"
90 "\n"
91 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
92 "{\n"
93 " vec3 vcolour = g_light_colours[0].rgb;\n"
94 " vec3 vdir = g_light_directions[0].xyz;\n"
95 "\n"
96 " vec3 specdir = reflect( -vdir, wnormal );\n"
97 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
98 " return vfrag + vcolour*spec*fintensity;\n"
99 "}\n"
100 "\n"
101 "float world_depth_sample( vec3 pos )\n"
102 "{\n"
103 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
104 " return texture( g_world_depth, depth_coord ).r;\n"
105 "}\n"
106 "\n"
107 "float shadow_sample( vec3 vdir )\n"
108 "{\n"
109 " vec3 sample_pos = aWorldCo + vdir;\n"
110 " float height_sample = world_depth_sample( sample_pos );\n"
111 "\n"
112 " float fdelta = height_sample - sample_pos.y;\n"
113 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
114 "}\n"
115 "\n"
116 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
117 "{\n"
118 " float faccum = 0.0;\n"
119 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
120 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
121 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
122 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
123 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
124 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
125 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
126 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
127 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
128 "}\n"
129 "\n"
130 "vec3 do_light_shadowing( vec3 vfrag )\n"
131 "{\n"
132 " float fspread = g_light_colours[0].w;\n"
133 " vec3 vdir = g_light_directions[0].xyz;\n"
134 " float flength = g_light_directions[0].w;\n"
135 "\n"
136 " float famt = 0.0;\n"
137 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
138 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
139 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
140 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
141 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
142 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
143 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
144 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
145 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
146 "}\n"
147 "\n"
148 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
149 "{\n"
150 " float dist = pow(fdist*0.0008,1.2);\n"
151 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
152 "}\n"
153 "\n"
154 "#line 15 0 \n"
155 "\n"
156 "void main()\n"
157 "{\n"
158 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
159 "\n"
160 " // ws modulation\n"
161 " vec4 wgarbage = texture( uTexGarbage, aCo.xz * 0.160 );\n"
162 " \n"
163 " // Creating normal patches\n"
164 " vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;\n"
165 " vec3 qnorm = normalize(floor(aNorm*4.0+modnorm)*0.25) + vec3(0.001,0.0,0.0);\n"
166 "\n"
167 " vec3 tangent0 = normalize(cross(qnorm,vec3(0.0,1.0,0.0)));\n"
168 " vec3 tangent1 = cross(qnorm,tangent0);\n"
169 " vec2 uvdiffuse = vec2( dot(tangent0,aCo), dot(tangent1,aCo) ) * 0.160;\n"
170 " \n"
171 " // Patch local noise\n"
172 " vec4 rgarbage = texture( uTexGarbage, uvdiffuse );\n"
173 "\n"
174 " // Colour blending\n"
175 " float fblendclip = step(0.380,aColour.r + (rgarbage.r-0.5)*-1.740)*0.320;\n"
176 " vec2 uvgradients = aUv + vec2( fblendclip, 0.0 );\n"
177 "\n"
178 " vfrag = texture( uTexGradients, uvgradients ).rgb;\n"
179 " vfrag -= rgarbage.a*0.04;\n"
180 "\n"
181 " if( g_light_preview == 1 )\n"
182 " {\n"
183 " vfrag = vec3(0.5);\n"
184 " }\n"
185 "\n"
186 " // Lighting\n"
187 " vec3 halfview = uCamera - aWorldCo;\n"
188 " float fdist = length( halfview );\n"
189 " halfview /= fdist;\n"
190 "\n"
191 " vfrag = do_light_diffuse( vfrag, qnorm );\n"
192 " vfrag = do_light_spec( vfrag, qnorm, halfview, 0.1 );\n"
193 " vfrag = do_light_shadowing( vfrag );\n"
194 " vfrag = apply_fog( vfrag, fdist );\n"
195 "\n"
196 " FragColor = vec4(vfrag, 1.0 );\n"
197 "}\n"
198 ""},
199 };
200
201 static GLuint _uniform_vblend_uPv;
202 static GLuint _uniform_vblend_uMdl;
203 static GLuint _uniform_vblend_uTexGarbage;
204 static GLuint _uniform_vblend_uTexGradients;
205 static GLuint _uniform_vblend_uCamera;
206 static GLuint _uniform_vblend_uPlane;
207 static GLuint _uniform_vblend_g_world_depth;
208 static void shader_vblend_uPv(m4x4f m){
209 glUniformMatrix4fv( _uniform_vblend_uPv, 1, GL_FALSE, (float *)m );
210 }
211 static void shader_vblend_uMdl(m4x3f m){
212 glUniformMatrix4x3fv( _uniform_vblend_uMdl, 1, GL_FALSE, (float *)m );
213 }
214 static void shader_vblend_uTexGarbage(int i){
215 glUniform1i( _uniform_vblend_uTexGarbage, i );
216 }
217 static void shader_vblend_uTexGradients(int i){
218 glUniform1i( _uniform_vblend_uTexGradients, i );
219 }
220 static void shader_vblend_uCamera(v3f v){
221 glUniform3fv( _uniform_vblend_uCamera, 1, v );
222 }
223 static void shader_vblend_uPlane(v4f v){
224 glUniform4fv( _uniform_vblend_uPlane, 1, v );
225 }
226 static void shader_vblend_g_world_depth(int i){
227 glUniform1i( _uniform_vblend_g_world_depth, i );
228 }
229 static void shader_vblend_register(void){
230 vg_shader_register( &_shader_vblend );
231 }
232 static void shader_vblend_use(void){ glUseProgram(_shader_vblend.id); }
233 static void shader_vblend_link(void){
234 _uniform_vblend_uPv = glGetUniformLocation( _shader_vblend.id, "uPv" );
235 _uniform_vblend_uMdl = glGetUniformLocation( _shader_vblend.id, "uMdl" );
236 _uniform_vblend_uTexGarbage = glGetUniformLocation( _shader_vblend.id, "uTexGarbage" );
237 _uniform_vblend_uTexGradients = glGetUniformLocation( _shader_vblend.id, "uTexGradients" );
238 _uniform_vblend_uCamera = glGetUniformLocation( _shader_vblend.id, "uCamera" );
239 _uniform_vblend_uPlane = glGetUniformLocation( _shader_vblend.id, "uPlane" );
240 _uniform_vblend_g_world_depth = glGetUniformLocation( _shader_vblend.id, "g_world_depth" );
241 }
242 #endif /* SHADER_vblend_H */