even more collision filtering
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / viewchar.h
1 #ifndef SHADER_viewchar_H
2 #define SHADER_viewchar_H
3 static void shader_viewchar_link(void);
4 static void shader_viewchar_register(void);
5 static struct vg_shader _shader_viewchar = {
6 .name = "viewchar",
7 .link = shader_viewchar_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 mat4 uPv;\n"
21 "uniform mat4x3 uTransforms[32];\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 " vec4 co_local = vec4( a_co, 1.0 );\n"
32 " vec3 co0 = uTransforms[ a_groups[0] ] * co_local;\n"
33 " vec3 co1 = uTransforms[ a_groups[1] ] * co_local;\n"
34 " vec3 co2 = uTransforms[ a_groups[2] ] * co_local;\n"
35 " vec3 n0 = mat3(uTransforms[ a_groups[0] ]) * a_norm;\n"
36 " vec3 n1 = mat3(uTransforms[ a_groups[1] ]) * a_norm;\n"
37 " vec3 n2 = mat3(uTransforms[ a_groups[2] ]) * a_norm;\n"
38 "\n"
39 " vec3 world_pos = co0*a_weights[0] + co1*a_weights[1] + co2*a_weights[2];\n"
40 " vec3 world_normal = n0*a_weights[0] + n1*a_weights[1] + n2*a_weights[2];\n"
41 " \n"
42 " gl_Position = uPv * vec4( world_pos, 1.0 );\n"
43 " aColour = a_colour;\n"
44 " aUv = a_uv;\n"
45 " aNorm = world_normal;\n"
46 " aCo = a_co;\n"
47 " aWorldCo = world_pos;\n"
48 "}\n"
49 ""},
50 .fs =
51 {
52 .static_src =
53 "out vec4 FragColor;\n"
54 "\n"
55 "uniform sampler2D uTexMain;\n"
56 "uniform vec3 uCamera;\n"
57 "\n"
58 "in vec4 aColour;\n"
59 "in vec2 aUv;\n"
60 "in vec3 aNorm;\n"
61 "in vec3 aCo;\n"
62 "in vec3 aWorldCo;\n"
63 "\n"
64 "#line 1 1 \n"
65 "layout (std140) uniform ub_world_lighting\n"
66 "{\n"
67 " vec4 g_light_colours[3];\n"
68 " vec4 g_light_directions[3];\n"
69 " vec4 g_ambient_colour;\n"
70 "\n"
71 " vec4 g_water_plane;\n"
72 " vec4 g_depth_bounds;\n"
73 " float g_water_fog;\n"
74 " int g_light_count;\n"
75 " int g_light_preview;\n"
76 " int g_shadow_samples;\n"
77 "};\n"
78 "\n"
79 "uniform sampler2D g_world_depth;\n"
80 "\n"
81 "// Standard diffuse + spec models\n"
82 "// ==============================\n"
83 "\n"
84 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
85 "{\n"
86 " vec3 vtotal = g_ambient_colour.rgb;\n"
87 "\n"
88 " for( int i=0; i<g_light_count; i++ )\n"
89 " {\n"
90 " vec3 vcolour = g_light_colours[i].rgb;\n"
91 " vec3 vdir = g_light_directions[i].xyz;\n"
92 "\n"
93 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
94 " vtotal += vcolour*flight;\n"
95 " }\n"
96 "\n"
97 " return vfrag * vtotal;\n"
98 "}\n"
99 "\n"
100 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
101 "{\n"
102 " vec3 vcolour = g_light_colours[0].rgb;\n"
103 " vec3 vdir = g_light_directions[0].xyz;\n"
104 "\n"
105 " vec3 specdir = reflect( -vdir, wnormal );\n"
106 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
107 " return vfrag + vcolour*spec*fintensity;\n"
108 "}\n"
109 "\n"
110 "float world_depth_sample( vec3 pos )\n"
111 "{\n"
112 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
113 " return texture( g_world_depth, depth_coord ).r;\n"
114 "}\n"
115 "\n"
116 "float shadow_sample( vec3 vdir )\n"
117 "{\n"
118 " vec3 sample_pos = aWorldCo + vdir;\n"
119 " float height_sample = world_depth_sample( sample_pos );\n"
120 "\n"
121 " float fdelta = height_sample - sample_pos.y;\n"
122 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
123 "}\n"
124 "\n"
125 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
126 "{\n"
127 " float faccum = 0.0;\n"
128 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
129 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
130 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
131 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
132 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
133 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
134 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
135 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
136 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
137 "}\n"
138 "\n"
139 "vec3 do_light_shadowing( vec3 vfrag )\n"
140 "{\n"
141 " if( g_shadow_samples == 0 )\n"
142 " {\n"
143 " return vfrag;\n"
144 " }\n"
145 "\n"
146 " float fspread = g_light_colours[0].w;\n"
147 " vec3 vdir = g_light_directions[0].xyz;\n"
148 " float flength = g_light_directions[0].w;\n"
149 "\n"
150 " float famt = 0.0;\n"
151 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
152 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
153 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
154 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
155 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
156 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
157 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
158 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
159 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
160 "}\n"
161 "\n"
162 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
163 "{\n"
164 " float dist = pow(fdist*0.0008,1.2);\n"
165 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
166 "}\n"
167 "\n"
168 "#line 13 0 \n"
169 "\n"
170 "void main()\n"
171 "{\n"
172 " vec3 vfrag = texture( uTexMain, aUv ).rgb;\n"
173 "\n"
174 " // Lighting\n"
175 " vec3 halfview = uCamera - aWorldCo;\n"
176 " float fdist = length( halfview );\n"
177 " halfview /= fdist;\n"
178 "\n"
179 " vec3 qnorm = normalize(floor(aNorm*2.0)*0.5) + vec3(0.001,0.0,0.0);\n"
180 "\n"
181 " vfrag = mix( vfrag, do_light_diffuse( vfrag, qnorm ), 0.5 );\n"
182 " //vfrag = do_light_spec( vfrag, qnorm, halfview, 0.1 );\n"
183 " vfrag = do_light_shadowing( vfrag );\n"
184 " vfrag = apply_fog( vfrag, fdist );\n"
185 "\n"
186 " float opacity = clamp( fdist*fdist, 0.1, 1.0 );\n"
187 " FragColor = vec4(vfrag,opacity);\n"
188 "}\n"
189 ""},
190 };
191
192 static GLuint _uniform_viewchar_uPv;
193 static GLuint _uniform_viewchar_uTransforms;
194 static GLuint _uniform_viewchar_uTexMain;
195 static GLuint _uniform_viewchar_uCamera;
196 static GLuint _uniform_viewchar_g_world_depth;
197 static void shader_viewchar_uPv(m4x4f m){
198 glUniformMatrix4fv(_uniform_viewchar_uPv,1,GL_FALSE,(float*)m);
199 }
200 static void shader_viewchar_uTexMain(int i){
201 glUniform1i(_uniform_viewchar_uTexMain,i);
202 }
203 static void shader_viewchar_uCamera(v3f v){
204 glUniform3fv(_uniform_viewchar_uCamera,1,v);
205 }
206 static void shader_viewchar_g_world_depth(int i){
207 glUniform1i(_uniform_viewchar_g_world_depth,i);
208 }
209 static void shader_viewchar_register(void){
210 vg_shader_register( &_shader_viewchar );
211 }
212 static void shader_viewchar_use(void){ glUseProgram(_shader_viewchar.id); }
213 static void shader_viewchar_link(void){
214 _uniform_viewchar_uPv = glGetUniformLocation( _shader_viewchar.id, "uPv" );
215 _uniform_viewchar_uTransforms = glGetUniformLocation( _shader_viewchar.id, "uTransforms" );
216 _uniform_viewchar_uTexMain = glGetUniformLocation( _shader_viewchar.id, "uTexMain" );
217 _uniform_viewchar_uCamera = glGetUniformLocation( _shader_viewchar.id, "uCamera" );
218 _uniform_viewchar_g_world_depth = glGetUniformLocation( _shader_viewchar.id, "g_world_depth" );
219 }
220 #endif /* SHADER_viewchar_H */