add motion vectors to all shaders
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / standard.h
1 #ifndef SHADER_standard_H
2 #define SHADER_standard_H
3 static void shader_standard_link(void);
4 static void shader_standard_register(void);
5 static struct vg_shader _shader_standard = {
6 .name = "standard",
7 .link = shader_standard_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 "uniform sampler2D uTexGarbage;\n"
61 "uniform sampler2D uTexMain;\n"
62 "uniform vec3 uCamera;\n"
63 "uniform vec4 uPlane;\n"
64 "\n"
65 "in vec4 aColour;\n"
66 "in vec2 aUv;\n"
67 "in vec3 aNorm;\n"
68 "in vec3 aCo;\n"
69 "in vec3 aWorldCo;\n"
70 "\n"
71 "#line 1 1 \n"
72 "layout (location = 0) out vec4 oColour;\n"
73 "\n"
74 "layout (std140) uniform ub_world_lighting\n"
75 "{\n"
76 " vec4 g_light_colours[3];\n"
77 " vec4 g_light_directions[3];\n"
78 " vec4 g_ambient_colour;\n"
79 "\n"
80 " vec4 g_water_plane;\n"
81 " vec4 g_depth_bounds;\n"
82 " float g_water_fog;\n"
83 " int g_light_count;\n"
84 " int g_light_preview;\n"
85 " int g_shadow_samples;\n"
86 "};\n"
87 "\n"
88 "uniform sampler2D g_world_depth;\n"
89 "\n"
90 "// Standard diffuse + spec models\n"
91 "// ==============================\n"
92 "\n"
93 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
94 "{\n"
95 " vec3 vtotal = g_ambient_colour.rgb;\n"
96 "\n"
97 " for( int i=0; i<g_light_count; i++ )\n"
98 " {\n"
99 " vec3 vcolour = g_light_colours[i].rgb;\n"
100 " vec3 vdir = g_light_directions[i].xyz;\n"
101 "\n"
102 " float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);\n"
103 " vtotal += vcolour*flight;\n"
104 " }\n"
105 "\n"
106 " return vfrag * vtotal;\n"
107 "}\n"
108 "\n"
109 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
110 "{\n"
111 " vec3 vcolour = g_light_colours[0].rgb;\n"
112 " vec3 vdir = g_light_directions[0].xyz;\n"
113 "\n"
114 " vec3 specdir = reflect( -vdir, wnormal );\n"
115 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
116 " return vfrag + vcolour*spec*fintensity;\n"
117 "}\n"
118 "\n"
119 "float world_depth_sample( vec3 pos )\n"
120 "{\n"
121 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
122 " return texture( g_world_depth, depth_coord ).r;\n"
123 "}\n"
124 "\n"
125 "float shadow_sample( vec3 vdir )\n"
126 "{\n"
127 " vec3 sample_pos = aWorldCo + vdir;\n"
128 " float height_sample = world_depth_sample( sample_pos );\n"
129 "\n"
130 " float fdelta = height_sample - sample_pos.y;\n"
131 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
132 "}\n"
133 "\n"
134 "vec3 do_light_shadowing_old( vec3 vfrag )\n"
135 "{\n"
136 " float faccum = 0.0;\n"
137 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
138 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
139 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
140 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
141 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
142 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
143 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
144 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
145 " return mix( vfrag, g_ambient_colour.rgb, faccum );\n"
146 "}\n"
147 "\n"
148 "vec3 do_light_shadowing( vec3 vfrag )\n"
149 "{\n"
150 " if( g_shadow_samples == 0 )\n"
151 " {\n"
152 " return vfrag;\n"
153 " }\n"
154 "\n"
155 " float fspread = g_light_colours[0].w;\n"
156 " vec3 vdir = g_light_directions[0].xyz;\n"
157 " float flength = g_light_directions[0].w;\n"
158 "\n"
159 " float famt = 0.0;\n"
160 " famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n"
161 " famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n"
162 " famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n"
163 " famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n"
164 " famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n"
165 " famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n"
166 " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n"
167 " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n"
168 " return mix( vfrag, g_ambient_colour.rgb, famt );\n"
169 "}\n"
170 "\n"
171 "vec3 apply_fog( vec3 vfrag, float fdist )\n"
172 "{\n"
173 " float dist = pow(fdist*0.0008,1.2);\n"
174 " return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n"
175 "}\n"
176 "\n"
177 "#line 13 0 \n"
178 "#line 1 2 \n"
179 "layout (location = 1) out vec2 oMotionVec;\n"
180 "\n"
181 "in vec3 aMotionVec0;\n"
182 "in vec3 aMotionVec1;\n"
183 "\n"
184 "void compute_motion_vectors()\n"
185 "{\n"
186 " // Write motion vectors\n"
187 " vec2 vmotion0 = aMotionVec0.xy / aMotionVec0.z;\n"
188 " vec2 vmotion1 = aMotionVec1.xy / aMotionVec1.z;\n"
189 " oMotionVec = vmotion1-vmotion0;\n"
190 "}\n"
191 "\n"
192 "#line 14 0 \n"
193 "\n"
194 "void main()\n"
195 "{\n"
196 " compute_motion_vectors();\n"
197 "\n"
198 " vec3 vfrag = vec3(0.5,0.5,0.5);\n"
199 " vec4 vsamplemain = texture( uTexMain, aUv );\n"
200 " vec3 qnorm = normalize(aNorm);\n"
201 "\n"
202 " vfrag = vsamplemain.rgb;\n"
203 "\n"
204 " if( g_light_preview == 1 )\n"
205 " {\n"
206 " vfrag = vec3(0.5);\n"
207 " }\n"
208 "\n"
209 " // Lighting\n"
210 " vec3 halfview = uCamera - aWorldCo;\n"
211 " float fdist = length( halfview );\n"
212 " halfview /= fdist;\n"
213 "\n"
214 " vfrag = do_light_diffuse( vfrag, qnorm );\n"
215 " vfrag = do_light_spec( vfrag, qnorm, halfview, 0.1 );\n"
216 " vfrag = do_light_shadowing( vfrag );\n"
217 " vfrag = apply_fog( vfrag, fdist );\n"
218 "\n"
219 " oColour = vec4(vfrag, 1.0);\n"
220 "}\n"
221 ""},
222 };
223
224 static GLuint _uniform_standard_uMdl;
225 static GLuint _uniform_standard_uPv;
226 static GLuint _uniform_standard_uPvmPrev;
227 static GLuint _uniform_standard_uTexGarbage;
228 static GLuint _uniform_standard_uTexMain;
229 static GLuint _uniform_standard_uCamera;
230 static GLuint _uniform_standard_uPlane;
231 static GLuint _uniform_standard_g_world_depth;
232 static void shader_standard_uMdl(m4x3f m){
233 glUniformMatrix4x3fv(_uniform_standard_uMdl,1,GL_FALSE,(float*)m);
234 }
235 static void shader_standard_uPv(m4x4f m){
236 glUniformMatrix4fv(_uniform_standard_uPv,1,GL_FALSE,(float*)m);
237 }
238 static void shader_standard_uPvmPrev(m4x4f m){
239 glUniformMatrix4fv(_uniform_standard_uPvmPrev,1,GL_FALSE,(float*)m);
240 }
241 static void shader_standard_uTexGarbage(int i){
242 glUniform1i(_uniform_standard_uTexGarbage,i);
243 }
244 static void shader_standard_uTexMain(int i){
245 glUniform1i(_uniform_standard_uTexMain,i);
246 }
247 static void shader_standard_uCamera(v3f v){
248 glUniform3fv(_uniform_standard_uCamera,1,v);
249 }
250 static void shader_standard_uPlane(v4f v){
251 glUniform4fv(_uniform_standard_uPlane,1,v);
252 }
253 static void shader_standard_g_world_depth(int i){
254 glUniform1i(_uniform_standard_g_world_depth,i);
255 }
256 static void shader_standard_register(void){
257 vg_shader_register( &_shader_standard );
258 }
259 static void shader_standard_use(void){ glUseProgram(_shader_standard.id); }
260 static void shader_standard_link(void){
261 _uniform_standard_uMdl = glGetUniformLocation( _shader_standard.id, "uMdl" );
262 _uniform_standard_uPv = glGetUniformLocation( _shader_standard.id, "uPv" );
263 _uniform_standard_uPvmPrev = glGetUniformLocation( _shader_standard.id, "uPvmPrev" );
264 _uniform_standard_uTexGarbage = glGetUniformLocation( _shader_standard.id, "uTexGarbage" );
265 _uniform_standard_uTexMain = glGetUniformLocation( _shader_standard.id, "uTexMain" );
266 _uniform_standard_uCamera = glGetUniformLocation( _shader_standard.id, "uCamera" );
267 _uniform_standard_uPlane = glGetUniformLocation( _shader_standard.id, "uPlane" );
268 _uniform_standard_g_world_depth = glGetUniformLocation( _shader_standard.id, "g_world_depth" );
269 }
270 #endif /* SHADER_standard_H */