reworked lighting uniforms
[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 .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/gpos.fs",
39 .static_src =
40 "out vec4 FragColor;\n"
41 "\n"
42 "uniform vec3 uCamera;\n"
43 "\n"
44 "in vec4 aColour;\n"
45 "in vec2 aUv;\n"
46 "in vec3 aNorm;\n"
47 "in vec3 aCo;\n"
48 "\n"
49 "#line 1 1 \n"
50 "layout (std140) uniform ub_world_lighting\n"
51 "{\n"
52 " vec3 g_directional;\n"
53 " vec3 g_sun_colour;\n"
54 " vec3 g_shadow_colour;\n"
55 " vec4 g_water_plane;\n"
56 " vec4 g_depth_bounds;\n"
57 " float g_water_fog;\n"
58 "};\n"
59 "\n"
60 "uniform sampler2D g_world_depth;\n"
61 "\n"
62 "// Standard diffuse + spec models\n"
63 "// ==============================\n"
64 "\n"
65 "vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )\n"
66 "{\n"
67 " float flight = dot( g_directional, wnormal )*0.5+0.5;\n"
68 " return vfrag * mix( g_shadow_colour, g_sun_colour, flight );\n"
69 "}\n"
70 "\n"
71 "vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )\n"
72 "{\n"
73 " vec3 specdir = reflect( -g_directional, wnormal );\n"
74 " float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);\n"
75 " return vfrag + g_sun_colour*spec*fintensity;\n"
76 "}\n"
77 "\n"
78 "float world_depth_sample( vec3 pos )\n"
79 "{\n"
80 " vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n"
81 " return texture( g_world_depth, depth_coord ).r;\n"
82 "}\n"
83 "\n"
84 "float shadow_sample( vec3 vdir )\n"
85 "{\n"
86 " vec3 sample_pos = aCo + vdir;\n"
87 " float height_sample = world_depth_sample( sample_pos );\n"
88 "\n"
89 " float fdelta = height_sample - sample_pos.y;\n"
90 " return clamp( fdelta, 0.1, 0.2 )-0.1;\n"
91 "}\n"
92 "\n"
93 "vec3 do_light_shadowing( vec3 vfrag )\n"
94 "{\n"
95 " float faccum = 0.0;\n"
96 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));\n"
97 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));\n"
98 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));\n"
99 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));\n"
100 " faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);\n"
101 " faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);\n"
102 " faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);\n"
103 " faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);\n"
104 " return mix( vfrag, g_shadow_colour, faccum );\n"
105 "}\n"
106 "\n"
107 "\n"
108 "#line 11 0 \n"
109 "\n"
110 "// Water blending\n"
111 "// ==============\n"
112 "\n"
113 "float water_depth( vec3 pos, vec3 halfview )\n"
114 "{\n"
115 " vec3 pnorm = g_water_plane.xyz;\n"
116 " float pdist = g_water_plane.w;\n"
117 "\n"
118 " float d = dot( pnorm, halfview );\n"
119 " float t = dot((pnorm*pdist - pos), pnorm) / d;\n"
120 " return t * g_water_fog;\n"
121 "}\n"
122 "\n"
123 "void main()\n"
124 "{\n"
125 " vec3 halfview = normalize( uCamera - aCo );\n"
126 " vec3 world_pos = vec3( aCo.y, aCo.x, aCo.z );\n"
127 " FragColor = vec4( world_pos, water_depth( aCo, halfview ) );\n"
128 "}\n"
129 ""},
130 };
131
132 static GLuint _uniform_gpos_uPv;
133 static GLuint _uniform_gpos_uMdl;
134 static GLuint _uniform_gpos_uCamera;
135 static GLuint _uniform_gpos_g_world_depth;
136 static void shader_gpos_uPv(m4x4f m){
137 glUniformMatrix4fv( _uniform_gpos_uPv, 1, GL_FALSE, (float *)m );
138 }
139 static void shader_gpos_uMdl(m4x3f m){
140 glUniformMatrix4x3fv( _uniform_gpos_uMdl, 1, GL_FALSE, (float *)m );
141 }
142 static void shader_gpos_uCamera(v3f v){
143 glUniform3fv( _uniform_gpos_uCamera, 1, v );
144 }
145 static void shader_gpos_g_world_depth(int i){
146 glUniform1i( _uniform_gpos_g_world_depth, i );
147 }
148 static void shader_gpos_register(void){
149 vg_shader_register( &_shader_gpos );
150 }
151 static void shader_gpos_use(void){ glUseProgram(_shader_gpos.id); }
152 static void shader_gpos_link(void){
153 _uniform_gpos_uPv = glGetUniformLocation( _shader_gpos.id, "uPv" );
154 _uniform_gpos_uMdl = glGetUniformLocation( _shader_gpos.id, "uMdl" );
155 _uniform_gpos_uCamera = glGetUniformLocation( _shader_gpos.id, "uCamera" );
156 _uniform_gpos_g_world_depth = glGetUniformLocation( _shader_gpos.id, "g_world_depth" );
157 }
158 #endif /* SHADER_gpos_H */