shadows basically works

This commit is contained in:
2023-02-09 17:21:59 +03:00
parent 65dd078f07
commit 3cf466e5d3
29 changed files with 293 additions and 206 deletions

View File

@@ -19,7 +19,8 @@ in vec3 view_dir, world_dir;
uniform vec2 dt;
uniform float z_near;
uniform sampler2D tex_coeffs[2];
uniform sampler2D tex_0, tex_1, tex_2, tex_3, tex_4;
uniform sampler2D tex_0, tex_1, tex_2, tex_3, tex_4, tex_sh;
uniform sampler2DShadow tex_shadow[16];
uniform samplerCube tex_env;
uniform int lights_start, lights_count;
@@ -29,12 +30,20 @@ uniform mat3 view_mat;
const vec3 luma = vec3(0.299, 0.587, 0.114);
const float _min_rough = 1.e-8, max_lod = 8;
const float PI = 3.1416;
vec4 pos, lpos, shp;
vec3 li, si, ldir, halfV, bn, bn2, lwdir;
//vec3 vds, vds2;
float rough_diff, rough_spec, dist, NdotL, NdotH, spot, ldist, diff, spec, sdist, shadow;
vec4 mapScreenToShadow(in int light_index) {
vec4 shp = qgl_light_position[light_index].shadow_matrix * pos;
shp.z += 0.095;
return shp;
}
void calcLight(in int index, in vec3 n, in vec3 v) {
lpos = qgl_light_position[index].position;
ldir = lpos.xyz - (pos.xyz * lpos.w);
@@ -49,6 +58,10 @@ void calcLight(in int index, in vec3 n, in vec3 v) {
float scos = max(dot(-ldir, qgl_light_position[index].direction.xyz), 0.);
spot *= scos * step(qgl_light_parameter[index].angles.w, scos);
spot *= smoothstep(qgl_light_parameter[index].angles.w, qgl_light_parameter[index].angles.y, scos);
vec4 shp = mapScreenToShadow(index);
spot *= textureProj(tex_shadow[index - lights_start], shp);
/*//lwdir = mat3(mat_viewi) * qgl_Light[index].direction.xyz;
//bn = normalize(cross(lwdir, vec3(1, 0, 0)));
//bn2 = normalize(cross(lwdir, bn));
@@ -93,7 +106,7 @@ void calcLight(in int index, in vec3 n, in vec3 v) {
ndlc = (1. - NdotHs) / NdotHs;
float der = NdotHs * (rough_spec + ndlc);
spec = rough_spec / (der*der) / 3.1416;
spec = rough_spec / (der*der) / PI;
//spec = texture(tex_coeffs[1], vec2(roughness, (NdotHs))).r;
si += spot * spec * qgl_light_parameter[index].color.rgb;
}
@@ -172,7 +185,17 @@ void main(void) {
res_col = mix(res_col, fog_color.rgb, fog);
qgl_FragColor = vec4(res_col, alpha);
/*
#ifdef SPOT
vec4 wpos = vec4(world_dir * z, 1);
vec4 shp = qgl_light_position[lights_start].shadow_matrix * (pos);
//shp.z += 0.095;
//qgl_FragColor.rgb = vec3(texture(tex_sh, shp.xy).rgb);
qgl_FragColor.rgb = vec3(textureProj(tex_shadow[0], shp));
//vec4 rp = qgl_ViewProjMatrix*vec4(qgl_FragTexture.xy,z,1);
//qgl_FragColor.rgb = vec3(shp.xy,0);
#endif
*/
//vec3 specular = prefilteredColor * (F * envBRDF.x + envBRDF.y);
//qgl_FragColor.rgb = vec3(shlick * brdf.x + brdf.y);
//qgl_FragColor.rgb = vec3(alpha);

View File

@@ -22,4 +22,5 @@ void main(void) {
l = 1 - exp(-l*g);
res = max(vec3(0.f), res * l);
qgl_FragColor = vec4(res, dot(res, luma));
//qgl_FragColor = vec4(0.5);
}

24
shaders/shadow.glsl Normal file
View File

@@ -0,0 +1,24 @@
// vert //
void main(void) {
qgl_MaterialIndex = qgl_Material;
qgl_FragTexture = qgl_getFragTexture();
gl_Position = qgl_ftransform();
}
// frag //
float z_near = 0.1f;
const float _pe = 2.4e-7;
void main(void) {
vec4 diffuse = qgl_materialTexture(QGL_MAP_DIFFUSE, qgl_FragTexture.xy, vec4(0));
if(diffuse.a < 0.5)
discard;
float z = gl_FragCoord.z;
z = z + z - 1;
z = ((_pe - 2.) * z_near) / (z + _pe - 1.); // infinite depth
qgl_FragData[0] = vec4(z / 20);
}