added models
[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 "\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/vblend.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 "uniform vec4 uPlane;\n"
46 "\n"
47 "in vec4 aColour;\n"
48 "in vec2 aUv;\n"
49 "in vec3 aNorm;\n"
50 "in vec3 aCo;\n"
51 "\n"
52 "#line 1 1 \n"
53 "layout (std140) uniform ub_world_lighting\n"
54 "{\n"
55 " vec4 g_light_colours[3];\n"
56 " vec4 g_light_directions[3];\n"
57 " vec4 g_ambient_colour;\n"
58 "\n"
59 " vec4 g_water_plane;\n"
60 " vec4 g_depth_bounds;\n"
61 " float g_water_fog;\n"
62 " int g_light_count;\n"
63 " int g_light_preview;\n"
64 "};\n"
65 "\n"
66 "uniform sampler2D g_world_depth;\n"
67 "\n"
68 "// Standard diffuse + spec models\n"
69 "// ==============================\n"
70 "\n"
71 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
72 "{\n"
73 " vec3 vtotal = g_ambient_colour.rgb;\n"
74 "\n"
75 " for( int i=0; i<g_light_count; i++ )\n"
76 " {\n"
77 " vec3 vcolour = g_light_colours[i].rgb;\n"
78 " vec3 vdir = g_light_directions[i].xyz;\n"
79 "\n"
80 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
81 " vtotal += vcolour*flight;\n"
82 " }\n"
83 "\n"
84 " return vfrag * vtotal;\n"
85 "}\n"
86 "\n"
87 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
88 "{\n"
89 " vec3 vcolour = g_light_colours[0].rgb;\n"
90 " vec3 vdir = g_light_directions[0].xyz;\n"
91 "\n"
92 " vec3 specdir = reflect( -vdir, wnormal );\n"
93 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
94 " return vfrag + vcolour*spec*fintensity;\n"
95 "}\n"
96 "\n"
97 "float world_depth_sample( vec3 pos )\n"
98 "{\n"
99 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
100 " return texture( g_world_depth, depth_coord ).r;\n"
101 "}\n"
102 "\n"
103 "float shadow_sample( vec3 vdir )\n"
104 "{\n"
105 " vec3 sample_pos = aCo + vdir;\n"
106 " float height_sample = world_depth_sample( sample_pos );\n"
107 "\n"
108 " float fdelta = height_sample - sample_pos.y;\n"
109 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
110 "}\n"
111 "\n"
112 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
113 "{\n"
114 " float faccum = 0.0;\n"
115 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
116 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
117 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
118 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
119 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
120 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
121 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
122 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
123 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
124 "}\n"
125 "\n"
126 "vec3 do_light_shadowing( vec3 vfrag )\n"
127 "{\n"
128 " float fspread = g_light_colours[0].w;\n"
129 " vec3 vdir = g_light_directions[0].xyz;\n"
130 " float flength = g_light_directions[0].w;\n"
131 "\n"
132 " float famt = 0.0;\n"
133 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
134 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
135 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
136 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
137 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
138 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
139 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
140 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
141 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
142 "}\n"
143 "\n"
144 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
145 "{\n"
146 " float dist = pow(fdist*0.0008,1.2);\n"
147 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
148 "}\n"
149 "\n"
150 "#line 14 0 \n"
151 "\n"
152 "void main()\n"
153 "{\n"
154 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
155 "\n"
156 " // ws modulation\n"
157 " vec4 wgarbage = texture( uTexGarbage, aCo.xz * 0.160 );\n"
158 " \n"
159 " // Creating normal patches\n"
160 " vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;\n"
161 " vec3 qnorm = normalize(floor(aNorm*4.0+modnorm)*0.25) + vec3(0.001,0.0,0.0);\n"
162 "\n"
163 " vec3 tangent0 = normalize(cross(qnorm,vec3(0.0,1.0,0.0)));\n"
164 " vec3 tangent1 = cross(qnorm,tangent0);\n"
165 " vec2 uvdiffuse = vec2( dot(tangent0,aCo), dot(tangent1,aCo) ) * 0.160;\n"
166 " \n"
167 " // Patch local noise\n"
168 " vec4 rgarbage = texture( uTexGarbage, uvdiffuse );\n"
169 "\n"
170 " // Colour blending\n"
171 " float fblendclip = step(0.380,aColour.r + (rgarbage.r-0.5)*-1.740)*0.320;\n"
172 " vec2 uvgradients = aUv + vec2( fblendclip, 0.0 );\n"
173 "\n"
174 " vfrag = texture( uTexGradients, uvgradients ).rgb;\n"
175 " vfrag -= rgarbage.a*0.04;\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_vblend_uPv;
198 static GLuint _uniform_vblend_uMdl;
199 static GLuint _uniform_vblend_uTexGarbage;
200 static GLuint _uniform_vblend_uTexGradients;
201 static GLuint _uniform_vblend_uCamera;
202 static GLuint _uniform_vblend_uPlane;
203 static GLuint _uniform_vblend_g_world_depth;
204 static void shader_vblend_uPv(m4x4f m){
205 glUniformMatrix4fv( _uniform_vblend_uPv, 1, GL_FALSE, (float *)m );
206 }
207 static void shader_vblend_uMdl(m4x3f m){
208 glUniformMatrix4x3fv( _uniform_vblend_uMdl, 1, GL_FALSE, (float *)m );
209 }
210 static void shader_vblend_uTexGarbage(int i){
211 glUniform1i( _uniform_vblend_uTexGarbage, i );
212 }
213 static void shader_vblend_uTexGradients(int i){
214 glUniform1i( _uniform_vblend_uTexGradients, i );
215 }
216 static void shader_vblend_uCamera(v3f v){
217 glUniform3fv( _uniform_vblend_uCamera, 1, v );
218 }
219 static void shader_vblend_uPlane(v4f v){
220 glUniform4fv( _uniform_vblend_uPlane, 1, v );
221 }
222 static void shader_vblend_g_world_depth(int i){
223 glUniform1i( _uniform_vblend_g_world_depth, i );
224 }
225 static void shader_vblend_register(void){
226 vg_shader_register( &_shader_vblend );
227 }
228 static void shader_vblend_use(void){ glUseProgram(_shader_vblend.id); }
229 static void shader_vblend_link(void){
230 _uniform_vblend_uPv = glGetUniformLocation( _shader_vblend.id, "uPv" );
231 _uniform_vblend_uMdl = glGetUniformLocation( _shader_vblend.id, "uMdl" );
232 _uniform_vblend_uTexGarbage = glGetUniformLocation( _shader_vblend.id, "uTexGarbage" );
233 _uniform_vblend_uTexGradients = glGetUniformLocation( _shader_vblend.id, "uTexGradients" );
234 _uniform_vblend_uCamera = glGetUniformLocation( _shader_vblend.id, "uCamera" );
235 _uniform_vblend_uPlane = glGetUniformLocation( _shader_vblend.id, "uPlane" );
236 _uniform_vblend_g_world_depth = glGetUniformLocation( _shader_vblend.id, "g_world_depth" );
237 }
238 #endif /* SHADER_vblend_H */