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