add motion vectors to all shaders
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / gpos.h
1 #ifndef SHADER_gpos_H
2 #define SHADER_gpos_H
3 static void shader_gpos_link(void);
4 static void shader_gpos_register(void);
5 static struct vg_shader _shader_gpos = {
6 .name = "gpos",
7 .link = shader_gpos_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 "#line 1 2 \n"
20 "out vec3 aMotionVec0;\n"
21 "out vec3 aMotionVec1;\n"
22 "\n"
23 "void vs_motion_out( vec4 vproj0, vec4 vproj1 )\n"
24 "{\n"
25 " aMotionVec0 = vec3( vproj0.xy, vproj0.w );\n"
26 " aMotionVec1 = vec3( vproj1.xy, vproj1.w );\n"
27 "}\n"
28 "\n"
29 "#line 3 0 \n"
30 "\n"
31 "uniform mat4x3 uMdl;\n"
32 "uniform mat4 uPv;\n"
33 "uniform mat4 uPvmPrev;\n"
34 "\n"
35 "out vec4 aColour;\n"
36 "out vec2 aUv;\n"
37 "out vec3 aNorm;\n"
38 "out vec3 aCo;\n"
39 "out vec3 aWorldCo;\n"
40 "\n"
41 "void main()\n"
42 "{\n"
43 " vec3 world_pos0 = uMdl * vec4( a_co, 1.0 );\n"
44 " vec4 vproj0 = uPv * vec4( world_pos0, 1.0 );\n"
45 " vec4 vproj1 = uPvmPrev * vec4( a_co, 1.0 );\n"
46 "\n"
47 " vs_motion_out( vproj0, vproj1 );\n"
48 "\n"
49 " gl_Position = vproj0;\n"
50 " aWorldCo = world_pos0;\n"
51 " aColour = a_colour;\n"
52 " aUv = a_uv;\n"
53 " aNorm = mat3(uMdl) * a_norm;\n"
54 " aCo = a_co;\n"
55 "}\n"
56 ""},
57 .fs =
58 {
59 .static_src =
60 "out vec4 FragColor;\n"
61 "\n"
62 "uniform vec3 uCamera;\n"
63 "\n"
64 "in vec4 aColour;\n"
65 "in vec2 aUv;\n"
66 "in vec3 aNorm;\n"
67 "in vec3 aCo;\n"
68 "in vec3 aWorldCo;\n"
69 "\n"
70 "#line 1 1 \n"
71 "layout (location = 0) out vec4 oColour;\n"
72 "\n"
73 "layout (std140) uniform ub_world_lighting\n"
74 "{\n"
75 " vec4 g_light_colours[3];\n"
76 " vec4 g_light_directions[3];\n"
77 " vec4 g_ambient_colour;\n"
78 "\n"
79 " vec4 g_water_plane;\n"
80 " vec4 g_depth_bounds;\n"
81 " float g_water_fog;\n"
82 " int g_light_count;\n"
83 " int g_light_preview;\n"
84 " int g_shadow_samples;\n"
85 "};\n"
86 "\n"
87 "uniform sampler2D g_world_depth;\n"
88 "\n"
89 "// Standard diffuse + spec models\n"
90 "// ==============================\n"
91 "\n"
92 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
93 "{\n"
94 " vec3 vtotal = g_ambient_colour.rgb;\n"
95 "\n"
96 " for( int i=0; i<g_light_count; i++ )\n"
97 " {\n"
98 " vec3 vcolour = g_light_colours[i].rgb;\n"
99 " vec3 vdir = g_light_directions[i].xyz;\n"
100 "\n"
101 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
102 " vtotal += vcolour*flight;\n"
103 " }\n"
104 "\n"
105 " return vfrag * vtotal;\n"
106 "}\n"
107 "\n"
108 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
109 "{\n"
110 " vec3 vcolour = g_light_colours[0].rgb;\n"
111 " vec3 vdir = g_light_directions[0].xyz;\n"
112 "\n"
113 " vec3 specdir = reflect( -vdir, wnormal );\n"
114 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
115 " return vfrag + vcolour*spec*fintensity;\n"
116 "}\n"
117 "\n"
118 "float world_depth_sample( vec3 pos )\n"
119 "{\n"
120 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
121 " return texture( g_world_depth, depth_coord ).r;\n"
122 "}\n"
123 "\n"
124 "float shadow_sample( vec3 vdir )\n"
125 "{\n"
126 " vec3 sample_pos = aWorldCo + vdir;\n"
127 " float height_sample = world_depth_sample( sample_pos );\n"
128 "\n"
129 " float fdelta = height_sample - sample_pos.y;\n"
130 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
131 "}\n"
132 "\n"
133 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
134 "{\n"
135 " float faccum = 0.0;\n"
136 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
137 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
138 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
139 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
140 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
141 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
142 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
143 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
144 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
145 "}\n"
146 "\n"
147 "vec3 do_light_shadowing( vec3 vfrag )\n"
148 "{\n"
149 " if( g_shadow_samples == 0 )\n"
150 " {\n"
151 " return vfrag;\n"
152 " }\n"
153 "\n"
154 " float fspread = g_light_colours[0].w;\n"
155 " vec3 vdir = g_light_directions[0].xyz;\n"
156 " float flength = g_light_directions[0].w;\n"
157 "\n"
158 " float famt = 0.0;\n"
159 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
160 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
161 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
162 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
163 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
164 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
165 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
166 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
167 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
168 "}\n"
169 "\n"
170 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
171 "{\n"
172 " float dist = pow(fdist*0.0008,1.2);\n"
173 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
174 "}\n"
175 "\n"
176 "#line 12 0 \n"
177 "\n"
178 "// Water blending\n"
179 "// ==============\n"
180 "\n"
181 "float water_depth( vec3 pos, vec3 halfview )\n"
182 "{\n"
183 " vec3 pnorm = g_water_plane.xyz;\n"
184 " float pdist = g_water_plane.w;\n"
185 "\n"
186 " float d = dot( pnorm, halfview );\n"
187 " float t = dot((pnorm*pdist - pos), pnorm) / d;\n"
188 " return t * g_water_fog;\n"
189 "}\n"
190 "\n"
191 "void main()\n"
192 "{\n"
193 " vec3 halfview = normalize( uCamera - aCo );\n"
194 " vec3 world_pos = vec3( aCo.y, aCo.x, aCo.z );\n"
195 " FragColor = vec4( world_pos, water_depth( aCo, halfview ) );\n"
196 "}\n"
197 ""},
198 };
199
200 static GLuint _uniform_gpos_uMdl;
201 static GLuint _uniform_gpos_uPv;
202 static GLuint _uniform_gpos_uPvmPrev;
203 static GLuint _uniform_gpos_uCamera;
204 static GLuint _uniform_gpos_g_world_depth;
205 static void shader_gpos_uMdl(m4x3f m){
206 glUniformMatrix4x3fv(_uniform_gpos_uMdl,1,GL_FALSE,(float*)m);
207 }
208 static void shader_gpos_uPv(m4x4f m){
209 glUniformMatrix4fv(_uniform_gpos_uPv,1,GL_FALSE,(float*)m);
210 }
211 static void shader_gpos_uPvmPrev(m4x4f m){
212 glUniformMatrix4fv(_uniform_gpos_uPvmPrev,1,GL_FALSE,(float*)m);
213 }
214 static void shader_gpos_uCamera(v3f v){
215 glUniform3fv(_uniform_gpos_uCamera,1,v);
216 }
217 static void shader_gpos_g_world_depth(int i){
218 glUniform1i(_uniform_gpos_g_world_depth,i);
219 }
220 static void shader_gpos_register(void){
221 vg_shader_register( &_shader_gpos );
222 }
223 static void shader_gpos_use(void){ glUseProgram(_shader_gpos.id); }
224 static void shader_gpos_link(void){
225 _uniform_gpos_uMdl = glGetUniformLocation( _shader_gpos.id, "uMdl" );
226 _uniform_gpos_uPv = glGetUniformLocation( _shader_gpos.id, "uPv" );
227 _uniform_gpos_uPvmPrev = glGetUniformLocation( _shader_gpos.id, "uPvmPrev" );
228 _uniform_gpos_uCamera = glGetUniformLocation( _shader_gpos.id, "uCamera" );
229 _uniform_gpos_g_world_depth = glGetUniformLocation( _shader_gpos.id, "g_world_depth" );
230 }
231 #endif /* SHADER_gpos_H */