6b4c3de695a00f9e25e29cd15b088108cb612a6e
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / terrain.h
1 #ifndef SHADER_terrain_H
2 #define SHADER_terrain_H
3 static void shader_terrain_link(void);
4 static void shader_terrain_register(void);
5 static struct vg_shader _shader_terrain = {
6 .name = "terrain",
7 .link = shader_terrain_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 2 0 \n"
19 "\n"
20 "uniform mat4x3 uMdl;\n"
21 "uniform mat4 uPv;\n"
22 "\n"
23 "out vec4 aColour;\n"
24 "out vec2 aUv;\n"
25 "out vec3 aNorm;\n"
26 "out vec3 aCo;\n"
27 "out vec3 aWorldCo;\n"
28 "\n"
29 "void main()\n"
30 "{\n"
31 " vec3 world_pos = uMdl * vec4(a_co,1.0);\n"
32 " gl_Position = uPv * vec4( world_pos, 1.0 );\n"
33 " aColour = a_colour;\n"
34 " aUv = a_uv;\n"
35 " aNorm = mat3(uMdl) * a_norm;\n"
36 " aCo = a_co;\n"
37 " aWorldCo = world_pos;\n"
38 "}\n"
39 ""},
40 .fs =
41 {
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 vec3 uSandColour;\n"
49 "uniform vec2 uBlendOffset;\n"
50 "\n"
51 "in vec4 aColour;\n"
52 "in vec2 aUv;\n"
53 "in vec3 aNorm;\n"
54 "in vec3 aCo;\n"
55 "in vec3 aWorldCo;\n"
56 "\n"
57 "#line 1 1 \n"
58 "layout (std140) uniform ub_world_lighting\n"
59 "{\n"
60 " vec4 g_light_colours[3];\n"
61 " vec4 g_light_directions[3];\n"
62 " vec4 g_ambient_colour;\n"
63 "\n"
64 " vec4 g_water_plane;\n"
65 " vec4 g_depth_bounds;\n"
66 " float g_water_fog;\n"
67 " int g_light_count;\n"
68 " int g_light_preview;\n"
69 " int g_shadow_samples;\n"
70 "};\n"
71 "\n"
72 "uniform sampler2D g_world_depth;\n"
73 "\n"
74 "// Standard diffuse + spec models\n"
75 "// ==============================\n"
76 "\n"
77 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
78 "{\n"
79 " vec3 vtotal = g_ambient_colour.rgb;\n"
80 "\n"
81 " for( int i=0; i<g_light_count; i++ )\n"
82 " {\n"
83 " vec3 vcolour = g_light_colours[i].rgb;\n"
84 " vec3 vdir = g_light_directions[i].xyz;\n"
85 "\n"
86 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
87 " vtotal += vcolour*flight;\n"
88 " }\n"
89 "\n"
90 " return vfrag * vtotal;\n"
91 "}\n"
92 "\n"
93 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
94 "{\n"
95 " vec3 vcolour = g_light_colours[0].rgb;\n"
96 " vec3 vdir = g_light_directions[0].xyz;\n"
97 "\n"
98 " vec3 specdir = reflect( -vdir, wnormal );\n"
99 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
100 " return vfrag + vcolour*spec*fintensity;\n"
101 "}\n"
102 "\n"
103 "float world_depth_sample( vec3 pos )\n"
104 "{\n"
105 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
106 " return texture( g_world_depth, depth_coord ).r;\n"
107 "}\n"
108 "\n"
109 "float shadow_sample( vec3 vdir )\n"
110 "{\n"
111 " vec3 sample_pos = aWorldCo + vdir;\n"
112 " float height_sample = world_depth_sample( sample_pos );\n"
113 "\n"
114 " float fdelta = height_sample - sample_pos.y;\n"
115 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
116 "}\n"
117 "\n"
118 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
119 "{\n"
120 " float faccum = 0.0;\n"
121 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
122 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
123 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
124 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
125 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
126 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
127 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
128 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
129 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
130 "}\n"
131 "\n"
132 "vec3 do_light_shadowing( vec3 vfrag )\n"
133 "{\n"
134 " if( g_shadow_samples == 0 )\n"
135 " {\n"
136 " return vfrag;\n"
137 " }\n"
138 "\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"
142 "\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"
153 "}\n"
154 "\n"
155 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
156 "{\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"
159 "}\n"
160 "\n"
161 "#line 16 0 \n"
162 "\n"
163 "void main()\n"
164 "{\n"
165 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
166 "\n"
167 " // ws modulation\n"
168 " vec4 wgarbage = texture( uTexGarbage, aCo.xz * 0.015 );\n"
169 " \n"
170 " // Creating normal patches\n"
171 " vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;\n"
172 " vec3 qnorm = normalize(floor(aNorm*4.0+modnorm)*0.25) + vec3(0.001,0.0,0.0);\n"
173 " vec2 dir = normalize(qnorm.xz);\n"
174 " vec2 uvdiffuse = aCo.xz * 0.02;\n"
175 " uvdiffuse = mat2(dir.y, dir.x, -dir.x, dir.y) * uvdiffuse;\n"
176 " \n"
177 " // Patch local noise\n"
178 " vec4 rgarbage = texture( uTexGarbage, uvdiffuse );\n"
179 "\n"
180 " // Colour blending\n"
181 " float amtgrass = step(qnorm.y,0.6);\n"
182 " float amtsand = min(max((aCo.y - 10.0) * -0.1,0.0)*qnorm.y,1.0);\n"
183 " vec2 uvgradients = aUv + vec2( amtgrass + rgarbage.a*0.8 )*uBlendOffset;\n"
184 " vfrag = texture( uTexGradients, uvgradients ).rgb;\n"
185 " vfrag = mix( vfrag, uSandColour, amtsand );\n"
186 "\n"
187 " qnorm = mix( qnorm, aNorm, amtsand );\n"
188 " \n"
189 " if( g_light_preview == 1 )\n"
190 " {\n"
191 " vfrag = vec3(0.5);\n"
192 " }\n"
193 "\n"
194 " // Lighting\n"
195 " vec3 halfview = uCamera - aCo;\n"
196 " float fdist = length( halfview );\n"
197 " halfview /= fdist;\n"
198 "\n"
199 " vfrag = do_light_diffuse( vfrag, qnorm );\n"
200 " vfrag = do_light_spec( vfrag, qnorm, halfview, 0.1 );\n"
201 " vfrag = do_light_shadowing( vfrag );\n"
202 " vfrag = apply_fog( vfrag, fdist );\n"
203 "\n"
204 " FragColor = vec4(vfrag, 1.0 );\n"
205 "}\n"
206 ""},
207 };
208
209 static GLuint _uniform_terrain_uMdl;
210 static GLuint _uniform_terrain_uPv;
211 static GLuint _uniform_terrain_uTexGarbage;
212 static GLuint _uniform_terrain_uTexGradients;
213 static GLuint _uniform_terrain_uCamera;
214 static GLuint _uniform_terrain_uSandColour;
215 static GLuint _uniform_terrain_uBlendOffset;
216 static GLuint _uniform_terrain_g_world_depth;
217 static void shader_terrain_uMdl(m4x3f m){
218 glUniformMatrix4x3fv(_uniform_terrain_uMdl,1,GL_FALSE,(float*)m);
219 }
220 static void shader_terrain_uPv(m4x4f m){
221 glUniformMatrix4fv(_uniform_terrain_uPv,1,GL_FALSE,(float*)m);
222 }
223 static void shader_terrain_uTexGarbage(int i){
224 glUniform1i(_uniform_terrain_uTexGarbage,i);
225 }
226 static void shader_terrain_uTexGradients(int i){
227 glUniform1i(_uniform_terrain_uTexGradients,i);
228 }
229 static void shader_terrain_uCamera(v3f v){
230 glUniform3fv(_uniform_terrain_uCamera,1,v);
231 }
232 static void shader_terrain_uSandColour(v3f v){
233 glUniform3fv(_uniform_terrain_uSandColour,1,v);
234 }
235 static void shader_terrain_uBlendOffset(v2f v){
236 glUniform2fv(_uniform_terrain_uBlendOffset,1,v);
237 }
238 static void shader_terrain_g_world_depth(int i){
239 glUniform1i(_uniform_terrain_g_world_depth,i);
240 }
241 static void shader_terrain_register(void){
242 vg_shader_register( &_shader_terrain );
243 }
244 static void shader_terrain_use(void){ glUseProgram(_shader_terrain.id); }
245 static void shader_terrain_link(void){
246 _uniform_terrain_uMdl = glGetUniformLocation( _shader_terrain.id, "uMdl" );
247 _uniform_terrain_uPv = glGetUniformLocation( _shader_terrain.id, "uPv" );
248 _uniform_terrain_uTexGarbage = glGetUniformLocation( _shader_terrain.id, "uTexGarbage" );
249 _uniform_terrain_uTexGradients = glGetUniformLocation( _shader_terrain.id, "uTexGradients" );
250 _uniform_terrain_uCamera = glGetUniformLocation( _shader_terrain.id, "uCamera" );
251 _uniform_terrain_uSandColour = glGetUniformLocation( _shader_terrain.id, "uSandColour" );
252 _uniform_terrain_uBlendOffset = glGetUniformLocation( _shader_terrain.id, "uBlendOffset" );
253 _uniform_terrain_g_world_depth = glGetUniformLocation( _shader_terrain.id, "g_world_depth" );
254 }
255 #endif /* SHADER_terrain_H */