This repository has been archived on 2020-09-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
libs/qglview/shaders/dsl_pass_1.frag

95 lines
2.9 KiB
GLSL

#version 130
//#extension GL_EXT_gpu_shader4 : enable
uniform vec3 ambient;
uniform sampler2D t0, t1, t2, tb;
uniform int gid, lightsCount;
uniform bool firstPass;
uniform vec2 dt;
uniform vec4 back_color;
//uniform vec3 eye;
//uniform vec4 lpos[8];
uniform mat4 mat;
//uniform float zNear, zFar;
float light_diffuse(int model, vec3 l, vec3 n, vec3 h, vec3 v, float shininess) {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, dist, NdotL, spot, ldist;
void calcLight(in int index, in vec3 n, in vec3 v, in vec4 v2) {
lpos = gl_LightSource[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));
}
halfV = normalize(ldir + v);
//i += gl_LightSource[i].diffuse.rgb * NdotL;
//si += gl_LightSource[i].specular.rgb * pow(max(dot(n, halfV), 0.), sh);
spot /= (gl_LightSource[index].constantAttenuation + ldist * (gl_LightSource[index].linearAttenuation + ldist * gl_LightSource[index].quadraticAttenuation));
li += spot * gl_LightSource[index].diffuse.rgb * light_diffuse(0, ldir, n, halfV, v, sh);
si += spot * gl_LightSource[index].specular.rgb * v2.rgb * light_specular(0, ldir, n, halfV, v, sh);
}
//li = vec3(gl_LightSource[index].spotExponent);
//si = vec3(0);
}
void main(void) {
//if (d == 1.) discard;
vec4 v0 = texture2D(t0, gl_TexCoord[0].xy);
if (v0.w == 0.) {
gl_FragColor = back_color;
return;
}
vec4 v1 = texture2D(t1, gl_TexCoord[0].xy), v2 = texture2D(t2, gl_TexCoord[0].xy);//, v3 = texture2D(t3, gl_TexCoord[0].xy);
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 = gl_LightModel.ambient.rgb;
si = vec3(0.);
pos = vec4(sp, 0, 1.) * mat;
pos *= v0.w;
pos.rgb += n * height;
vec3 v = normalize(-pos.xyz);
sh = v1.w;
//int light_model = int(v3.r * 255. + 0.5);
//li = ambient;
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);
}
}
}
}
}
}
}
}
gl_FragColor.rgb = li * dc + si + texture2D(tb, gl_TexCoord[0].xy).rgb;
//gl_FragColor.rgb = v0.rgb;
}