109 lines
3.4 KiB
GLSL
109 lines
3.4 KiB
GLSL
#version 150
|
|
//#extension GL_EXT_gpu_shader4 : enable
|
|
|
|
in vec4 view_dir;
|
|
in vec3 view_pos;
|
|
|
|
uniform vec3 ambient;
|
|
uniform sampler2D t0, t1, t2, t3, tb;
|
|
uniform sampler2D td;
|
|
uniform int gid, lightsCount;
|
|
uniform float z_near, z_far;
|
|
uniform bool firstPass;
|
|
uniform vec2 dt;
|
|
uniform vec4 back_color;
|
|
uniform mat4 mat_proji;
|
|
|
|
float light_diffuse(int model, vec3 l, vec3 n) {return max(0., dot(l, n));}
|
|
//float light_specular(int model, vec3 l, vec3 n, vec3 h, vec3 v, float shininess) {return max(0., pow(dot(n, h), shininess));}
|
|
|
|
vec4 pos, lpos;
|
|
vec3 li, si, ldir, halfV;
|
|
float sh_pow, sh_mul, dist, NdotL, spot, ldist, diff;
|
|
|
|
void calcLight(in int index, in vec3 n, in vec3 v, in vec4 v2) {
|
|
lpos = qgl_Light[index].position;
|
|
ldir = lpos.xyz - (pos.xyz * lpos.w);
|
|
ldist = length(ldir);
|
|
ldir = normalize(ldir);
|
|
NdotL = max(dot(n, ldir), 0.);
|
|
spot = step(0., NdotL);
|
|
if (NdotL > 0.) {
|
|
/*if (gl_LightSource[index].spotCutoff < 180.) {
|
|
spot = max(dot(-ldir, gl_LightSource[index].spotDirection.xyz), 0.);
|
|
spot *= step(gl_LightSource[index].spotCosCutoff, spot);
|
|
spot = pow(spot, (gl_LightSource[index].spotExponent + 0.001));
|
|
}*/
|
|
spot /= (qgl_Light[index].constantAttenuation + ldist * (qgl_Light[index].linearAttenuation + ldist * qgl_Light[index].quadraticAttenuation));
|
|
halfV = normalize(ldir + v);
|
|
///li += spot * gl_LightSource[index].diffuse.rgb * light_diffuse(0, ldir, n);
|
|
///si += spot * gl_LightSource[index].specular.rgb * sh_mul * light_specular(0, ldir, n, halfV, v, sh_pow);
|
|
float NdotLs = NdotL*NdotL;
|
|
float ndlc = (1. - NdotLs) / NdotLs;
|
|
float der = NdotLs * (sh_mul + ndlc);
|
|
diff = 2. / (1. + sqrt(1. + (1. - sh_mul) * ndlc));
|
|
li += spot * qgl_Light[index].color.rgb * diff;// * light_diffuse(0, ldir, n);
|
|
si += spot * qgl_Light[index].color.rgb * (sh_mul / (der*der) / 3.1416);
|
|
}
|
|
}
|
|
|
|
void main(void) {
|
|
//if (d == 1.) discard;
|
|
vec2 tc = qgl_FragTexture.xy;
|
|
vec4 v0 = texture2D(t0, tc);
|
|
if (v0.w == 0.) {
|
|
qgl_FragData[0] = back_color;
|
|
return;
|
|
}
|
|
vec4 v1 = texture2D(t1, tc), v2 = texture2D(t2, tc), v3 = texture2D(t3, tc);
|
|
vec2 sp = gl_FragCoord.xy * dt * 2 - vec2(1, 1);
|
|
vec3 dc = v0.rgb, n = v1.xyz * 2. - vec3(1.);
|
|
float height = v2.w;
|
|
li = qgl_AmbientLight.color.rgb * qgl_AmbientLight.intensity;
|
|
//li = vec3(0.);
|
|
si = vec3(0.);
|
|
|
|
pos = vec4(sp, 0, 1)*mat_proji;
|
|
pos.xyz *= v0.w;
|
|
//pos.xy *= 10.;
|
|
//pos.z = v0.w;
|
|
vec3 v = normalize(-pos.xyz);
|
|
sh_pow = 1. / max((1. - v1.w), 0.0001);
|
|
sh_mul = max(1. - v1.w, 0.0001);
|
|
//calcLight(0, n, v, v2);
|
|
/*if (lightsCount > 0) {
|
|
calcLight(0, n, v, v2);
|
|
if (lightsCount > 1) {
|
|
calcLight(1, n, v, v2);
|
|
if (lightsCount > 2) {
|
|
calcLight(2, n, v, v2);
|
|
if (lightsCount > 3) {
|
|
calcLight(3, n, v, v2);
|
|
if (lightsCount > 4) {
|
|
calcLight(4, n, v, v2);
|
|
if (lightsCount > 5) {
|
|
calcLight(5, n, v, v2);
|
|
if (lightsCount > 6) {
|
|
calcLight(6, n, v, v2);
|
|
if (lightsCount > 7) {
|
|
calcLight(7, n, v, v2);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}*/
|
|
//qgl_FragData[0].rgb = li * dc + si * v2.rgb + v3.rgb;// + texture2D(tb, tc).rgb;
|
|
//vec4 lp = mat*qgl_Light[0].position;
|
|
lpos = qgl_Light[0].position;
|
|
ldir = lpos.xyz - pos.xyz;
|
|
ldist = length(ldir);
|
|
float d = texture2D(td, tc).r;
|
|
//float z = ((z_near / (z_near-z_far)) * z_far) / (d - (z_far / (z_far-z_near)));
|
|
float z = z_near * z_far / (d * (z_far - z_near) - z_far);
|
|
//qgl_FragData[0].rgb = vec3(abs((v0.w)+(v3.z))-0.5);
|
|
qgl_FragData[0].rgb = vec3((-z*view_pos));
|
|
}
|