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