reworked lighting uniforms
[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/terrain.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 " vec3 g_directional;\n"
55 " vec3 g_sun_colour;\n"
56 " vec3 g_shadow_colour;\n"
57 " vec4 g_water_plane;\n"
58 " vec4 g_depth_bounds;\n"
59 " float g_water_fog;\n"
60 "};\n"
61 "\n"
62 "uniform sampler2D g_world_depth;\n"
63 "\n"
64 "// Standard diffuse + spec models\n"
65 "// ==============================\n"
66 "\n"
67 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
68 "{\n"
69 " float flight = dot( g_directional, wnormal )*0.5+0.5;\n"
70 " return vfrag * mix( g_shadow_colour, g_sun_colour, flight );\n"
71 "}\n"
72 "\n"
73 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
74 "{\n"
75 " vec3 specdir = reflect( -g_directional, wnormal );\n"
76 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
77 " return vfrag + g_sun_colour*spec*fintensity;\n"
78 "}\n"
79 "\n"
80 "float world_depth_sample( vec3 pos )\n"
81 "{\n"
82 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
83 " return texture( g_world_depth, depth_coord ).r;\n"
84 "}\n"
85 "\n"
86 "float shadow_sample( vec3 vdir )\n"
87 "{\n"
88 " vec3 sample_pos = aCo + vdir;\n"
89 " float height_sample = world_depth_sample( sample_pos );\n"
90 "\n"
91 " float fdelta = height_sample - sample_pos.y;\n"
92 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
93 "}\n"
94 "\n"
95 "vec3 do_light_shadowing( vec3 vfrag )\n"
96 "{\n"
97 " float faccum = 0.0;\n"
98 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
99 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
100 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
101 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
102 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
103 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
104 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
105 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
106 " return mix( vfrag, g_shadow_colour, faccum );\n"
107 "}\n"
108 "\n"
109 "\n"
110 "#line 13 0 \n"
111 "\n"
112 "void main()\n"
113 "{\n"
114 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
115 "\n"
116 " // ws modulation\n"
117 " vec4 wgarbage = texture( uTexGarbage, aCo.xz * 0.015 );\n"
118 " \n"
119 " // Creating normal patches\n"
120 " vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;\n"
121 " vec3 qnorm = normalize(floor(aNorm*4.0+modnorm)*0.25) + vec3(0.001,0.0,0.0);\n"
122 " vec2 dir = normalize(qnorm.xz);\n"
123 " vec2 uvdiffuse = aCo.xz * 0.02;\n"
124 " uvdiffuse = mat2(dir.y, dir.x, -dir.x, dir.y) * uvdiffuse;\n"
125 " \n"
126 " // Patch local noise\n"
127 " vec4 rgarbage = texture( uTexGarbage, uvdiffuse );\n"
128 "\n"
129 " // Colour blending\n"
130 " float amtgrass = step(qnorm.y,0.6);\n"
131 " float amtsand = min(max((aCo.y - 10.0) * -0.1,0.0)*qnorm.y,1.0);\n"
132 " vec2 uvgradients = aUv + vec2( amtgrass*0.5 + rgarbage.a*0.4, 0.0 );\n"
133 " vfrag = texture( uTexGradients, uvgradients ).rgb;\n"
134 " vfrag = mix( vfrag, vec3(1.0,0.9,0.8), amtsand );\n"
135 "\n"
136 " // Lighting\n"
137 " vec3 halfview = normalize( uCamera - aCo );\n"
138 " vfrag = do_light_diffuse( vfrag, qnorm );\n"
139 " vfrag = do_light_spec( vfrag, qnorm, halfview, 0.2 * rgarbage.a );\n"
140 " vfrag = do_light_shadowing( vfrag );\n"
141 "\n"
142 " FragColor = vec4( vfrag, 1.0 );\n"
143 "}\n"
144 ""},
145 };
146
147 static GLuint _uniform_terrain_uPv;
148 static GLuint _uniform_terrain_uMdl;
149 static GLuint _uniform_terrain_uTexGarbage;
150 static GLuint _uniform_terrain_uTexGradients;
151 static GLuint _uniform_terrain_uCamera;
152 static GLuint _uniform_terrain_g_world_depth;
153 static void shader_terrain_uPv(m4x4f m){
154 glUniformMatrix4fv( _uniform_terrain_uPv, 1, GL_FALSE, (float *)m );
155 }
156 static void shader_terrain_uMdl(m4x3f m){
157 glUniformMatrix4x3fv( _uniform_terrain_uMdl, 1, GL_FALSE, (float *)m );
158 }
159 static void shader_terrain_uTexGarbage(int i){
160 glUniform1i( _uniform_terrain_uTexGarbage, i );
161 }
162 static void shader_terrain_uTexGradients(int i){
163 glUniform1i( _uniform_terrain_uTexGradients, i );
164 }
165 static void shader_terrain_uCamera(v3f v){
166 glUniform3fv( _uniform_terrain_uCamera, 1, v );
167 }
168 static void shader_terrain_g_world_depth(int i){
169 glUniform1i( _uniform_terrain_g_world_depth, i );
170 }
171 static void shader_terrain_register(void){
172 vg_shader_register( &_shader_terrain );
173 }
174 static void shader_terrain_use(void){ glUseProgram(_shader_terrain.id); }
175 static void shader_terrain_link(void){
176 _uniform_terrain_uPv = glGetUniformLocation( _shader_terrain.id, "uPv" );
177 _uniform_terrain_uMdl = glGetUniformLocation( _shader_terrain.id, "uMdl" );
178 _uniform_terrain_uTexGarbage = glGetUniformLocation( _shader_terrain.id, "uTexGarbage" );
179 _uniform_terrain_uTexGradients = glGetUniformLocation( _shader_terrain.id, "uTexGradients" );
180 _uniform_terrain_uCamera = glGetUniformLocation( _shader_terrain.id, "uCamera" );
181 _uniform_terrain_g_world_depth = glGetUniformLocation( _shader_terrain.id, "g_world_depth" );
182 }
183 #endif /* SHADER_terrain_H */