checkin
[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 .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 "\n"
27 "void main()\n"
28 "{\n"
29 " gl_Position = uPv * vec4( uMdl * vec4(a_co,1.0), 1.0 );\n"
30 " aColour = a_colour;\n"
31 " aUv = a_uv;\n"
32 " aNorm = mat3(uMdl) * a_norm;\n"
33 " aCo = a_co;\n"
34 "}\n"
35 ""},
36 .fs =
37 {
38 .orig_file = "../shaders/terrain.fs",
39 .static_src =
40 "out vec4 FragColor;\n"
41 "\n"
42 "uniform sampler2D uTexGarbage;\n"
43 "uniform sampler2D uTexGradients;\n"
44 "uniform vec3 uCamera;\n"
45 "\n"
46 "in vec4 aColour;\n"
47 "in vec2 aUv;\n"
48 "in vec3 aNorm;\n"
49 "in vec3 aCo;\n"
50 "\n"
51 "#line 1 1 \n"
52 "layout (std140) uniform ub_world_lighting\n"
53 "{\n"
54 " vec4 g_light_colours[3];\n"
55 " vec4 g_light_directions[3];\n"
56 " vec4 g_ambient_colour;\n"
57 "\n"
58 " vec4 g_water_plane;\n"
59 " vec4 g_depth_bounds;\n"
60 " float g_water_fog;\n"
61 " int g_light_count;\n"
62 " int g_light_preview;\n"
63 "};\n"
64 "\n"
65 "uniform sampler2D g_world_depth;\n"
66 "\n"
67 "// Standard diffuse + spec models\n"
68 "// ==============================\n"
69 "\n"
70 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
71 "{\n"
72 " vec3 vtotal = g_ambient_colour.rgb;\n"
73 "\n"
74 " for( int i=0; i<g_light_count; i++ )\n"
75 " {\n"
76 " vec3 vcolour = g_light_colours[i].rgb;\n"
77 " vec3 vdir = g_light_directions[i].xyz;\n"
78 "\n"
79 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
80 " vtotal += vcolour*flight;\n"
81 " }\n"
82 "\n"
83 " return vfrag * vtotal;\n"
84 "}\n"
85 "\n"
86 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
87 "{\n"
88 " vec3 vcolour = g_light_colours[0].rgb;\n"
89 " vec3 vdir = g_light_directions[0].xyz;\n"
90 "\n"
91 " vec3 specdir = reflect( -vdir, wnormal );\n"
92 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
93 " return vfrag + vcolour*spec*fintensity;\n"
94 "}\n"
95 "\n"
96 "float world_depth_sample( vec3 pos )\n"
97 "{\n"
98 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
99 " return texture( g_world_depth, depth_coord ).r;\n"
100 "}\n"
101 "\n"
102 "float shadow_sample( vec3 vdir )\n"
103 "{\n"
104 " vec3 sample_pos = aCo + vdir;\n"
105 " float height_sample = world_depth_sample( sample_pos );\n"
106 "\n"
107 " float fdelta = height_sample - sample_pos.y;\n"
108 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
109 "}\n"
110 "\n"
111 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
112 "{\n"
113 " float faccum = 0.0;\n"
114 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
115 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
116 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
117 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
118 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
119 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
120 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
121 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
122 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
123 "}\n"
124 "\n"
125 "vec3 do_light_shadowing( vec3 vfrag )\n"
126 "{\n"
127 " float fspread = g_light_colours[0].w;\n"
128 " vec3 vdir = g_light_directions[0].xyz;\n"
129 " float flength = g_light_directions[0].w;\n"
130 "\n"
131 " float famt = 0.0;\n"
132 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
133 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
134 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
135 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
136 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
137 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
138 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
139 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
140 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
141 "}\n"
142 "\n"
143 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
144 "{\n"
145 " float dist = pow(fdist*0.0008,1.2);\n"
146 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
147 "}\n"
148 "\n"
149 "#line 13 0 \n"
150 "\n"
151 "void main()\n"
152 "{\n"
153 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
154 "\n"
155 " // ws modulation\n"
156 " vec4 wgarbage = texture( uTexGarbage, aCo.xz * 0.015 );\n"
157 " \n"
158 " // Creating normal patches\n"
159 " vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;\n"
160 " vec3 qnorm = normalize(floor(aNorm*4.0+modnorm)*0.25) + vec3(0.001,0.0,0.0);\n"
161 " vec2 dir = normalize(qnorm.xz);\n"
162 " vec2 uvdiffuse = aCo.xz * 0.02;\n"
163 " uvdiffuse = mat2(dir.y, dir.x, -dir.x, dir.y) * uvdiffuse;\n"
164 " \n"
165 " // Patch local noise\n"
166 " vec4 rgarbage = texture( uTexGarbage, uvdiffuse );\n"
167 "\n"
168 " // Colour blending\n"
169 " float amtgrass = step(qnorm.y,0.6);\n"
170 " float amtsand = min(max((aCo.y - 10.0) * -0.1,0.0)*qnorm.y,1.0);\n"
171 " vec2 uvgradients = aUv + vec2( amtgrass*0.5 + rgarbage.a*0.4, 0.0 );\n"
172 " vfrag = texture( uTexGradients, uvgradients ).rgb;\n"
173 " vfrag = mix( vfrag, vec3(1.0,0.9,0.8)*0.9, amtsand );\n"
174 "\n"
175 " qnorm = mix( qnorm, aNorm, amtsand );\n"
176 " \n"
177 " if( g_light_preview == 1 )\n"
178 " {\n"
179 " vfrag = vec3(0.5);\n"
180 " }\n"
181 "\n"
182 " // Lighting\n"
183 " vec3 halfview = uCamera - aCo;\n"
184 " float fdist = length( halfview );\n"
185 " halfview /= fdist;\n"
186 "\n"
187 " vfrag = do_light_diffuse( vfrag, qnorm );\n"
188 " vfrag = do_light_spec( vfrag, qnorm, halfview, 0.1 );\n"
189 " vfrag = do_light_shadowing( vfrag );\n"
190 " vfrag = apply_fog( vfrag, fdist );\n"
191 "\n"
192 " FragColor = vec4(vfrag, 1.0 );\n"
193 "}\n"
194 ""},
195 };
196
197 static GLuint _uniform_terrain_uPv;
198 static GLuint _uniform_terrain_uMdl;
199 static GLuint _uniform_terrain_uTexGarbage;
200 static GLuint _uniform_terrain_uTexGradients;
201 static GLuint _uniform_terrain_uCamera;
202 static GLuint _uniform_terrain_g_world_depth;
203 static void shader_terrain_uPv(m4x4f m){
204 glUniformMatrix4fv( _uniform_terrain_uPv, 1, GL_FALSE, (float *)m );
205 }
206 static void shader_terrain_uMdl(m4x3f m){
207 glUniformMatrix4x3fv( _uniform_terrain_uMdl, 1, GL_FALSE, (float *)m );
208 }
209 static void shader_terrain_uTexGarbage(int i){
210 glUniform1i( _uniform_terrain_uTexGarbage, i );
211 }
212 static void shader_terrain_uTexGradients(int i){
213 glUniform1i( _uniform_terrain_uTexGradients, i );
214 }
215 static void shader_terrain_uCamera(v3f v){
216 glUniform3fv( _uniform_terrain_uCamera, 1, v );
217 }
218 static void shader_terrain_g_world_depth(int i){
219 glUniform1i( _uniform_terrain_g_world_depth, i );
220 }
221 static void shader_terrain_register(void){
222 vg_shader_register( &_shader_terrain );
223 }
224 static void shader_terrain_use(void){ glUseProgram(_shader_terrain.id); }
225 static void shader_terrain_link(void){
226 _uniform_terrain_uPv = glGetUniformLocation( _shader_terrain.id, "uPv" );
227 _uniform_terrain_uMdl = glGetUniformLocation( _shader_terrain.id, "uMdl" );
228 _uniform_terrain_uTexGarbage = glGetUniformLocation( _shader_terrain.id, "uTexGarbage" );
229 _uniform_terrain_uTexGradients = glGetUniformLocation( _shader_terrain.id, "uTexGradients" );
230 _uniform_terrain_uCamera = glGetUniformLocation( _shader_terrain.id, "uCamera" );
231 _uniform_terrain_g_world_depth = glGetUniformLocation( _shader_terrain.id, "g_world_depth" );
232 }
233 #endif /* SHADER_terrain_H */