diffuse and specular better, but shadow maps off

master
Martin Felis 2017-02-22 13:49:43 +01:00
parent 3c6752ed34
commit 59522a9560
2 changed files with 102 additions and 11 deletions

View File

@ -11,6 +11,7 @@ uniform vec4 u_color;
uniform vec4 u_shadowMapParams;
#define u_shadowMapSize u_shadowMapParams.x
#define u_shadowMapBias u_shadowMapParams.y
#define shininess 0.5
#if SHADOW_PACKED_DEPTH
SAMPLER2D(u_shadowMap, 0);
@ -31,7 +32,21 @@ vec2 lit(vec3 _ld, vec3 _n, vec3 _vd, float _exp)
//spec
vec3 r = 2.0*ndotl*_n - _ld; //reflect(_ld, _n);
float rdotv = dot(r, _vd);
r = reflect(-_ld, _n);
float rdotv = dot(r, _n);
float spec = step(0.0, ndotl) * pow(max(0.0, rdotv), _exp) * (2.0 + _exp)/8.0;
return max(vec2(ndotl, spec), 0.0);
}
vec2 lit_old(vec3 _ld, vec3 _n, vec3 _vd, float _exp)
{
//diff
float ndotl = dot(_n, _ld);
//spec
vec3 r = 2.0*ndotl*_n - _ld; //reflect(_ld, _n);
r = reflect(-_ld, _n);
float rdotv = dot(r, _n);
float spec = step(0.0, ndotl) * pow(max(0.0, rdotv), _exp) * (2.0 + _exp)/8.0;
return max(vec2(ndotl, spec), 0.0);
@ -86,6 +101,39 @@ float PCF(Sampler _sampler, vec4 _shadowCoord, float _bias, vec2 _texelSize)
return result / 16.0;
}
vec2 blinn(vec3 _lightDir, vec3 _normal, vec3 _viewDir)
{
float ndotl = dot(_normal, _lightDir);
vec3 reflected = 2.0*ndotl*_normal - _lightDir; // reflect(_lightDir, _normal);
float rdotv = dot(reflected, _viewDir);
return vec2(ndotl, rdotv);
}
vec4 lit(float _ndotl, float _rdotv, float _m)
{
float diff = max(0.0, _ndotl);
float spec = step(0.0, _ndotl) * pow(max(0.0, _rdotv), _m);
return vec4(1.0, diff, spec, 1.0);
}
vec3 calcLight(vec3 _view, vec3 _normal, vec3 _viewDir)
{
// vec3 lightPos = mul(u_view, vec4(u_lightPos.xyz, 1.0)).xyz;
vec3 lightPos = u_lightPos.xyz;
vec3 toLight = lightPos - _view;
vec3 lightDir = normalize(toLight);
vec2 bln = blinn(lightDir, _normal, _viewDir);
vec4 lc = lit(bln.x, bln.y, shininess);
float dist = max(length(toLight), u_lightPos.w);
float attn = 250.0 * pow(dist, -2.0);
vec3 rgb = (lc.y * u_color.xyz + lc.z * vec3 (0.0, 0.8, 0.0))
* vec3(0.2, 0.2, 0.9) * attn;
return rgb;
}
void main()
{
vec3 color = u_color.xyz;
@ -93,16 +141,29 @@ void main()
vec3 v = v_view;
vec3 vd = -normalize(v);
vec3 n = v_normal;
vec3 l = u_lightPos.xyz;
vec3 l = u_lightPos.xyz - v_view;
vec3 ld = normalize(l);
vec2 lc = lit(ld, n, vd, 1.0);
vec2 lc = lit(ld, n, vd, 128.0);
// diffuse
float diff = max (0, dot(n, ld));
// spec
vec3 r = reflect(-ld, n);
float rdotv = max (0.0, dot (n, r));
float spec = pow (rdotv, 128.);
vec2 texelSize = vec2_splat(1.0/u_shadowMapSize);
float visibility = PCF(u_shadowMap, v_shadowcoord, u_shadowMapBias, texelSize);
vec3 ambient = 0.05 * color;
vec3 brdf = (lc.x + lc.y) * color * visibility;
// if (spec > 0.1)
// color = vec3(1., 0, 0);
vec3 brdf = lc.x * color + lc.y * vec3 (1.0, 1.0, 1.0); // * visibility;
// vec3 brdf = diff * color + spec * vec3 (1.0, 1.0, 1.0); // * visibility;
brdf = brdf * visibility;
#if TEXTURED
vec4 texcolor = toLinear (texture2D(sceneDefaultTexture, v_texcoord0) );

View File

@ -1518,7 +1518,7 @@ void Renderer::paintGL() {
for (size_t i = 0; i < entities.size(); i++) {
float mtxLightViewProjInv[16];
float light_pos_world[3];
// shadow map pass
for (uint32_t j = 0; j < entities[i]->mesh.meshes.size(); ++j) {
bx::mtxMul(
@ -1526,6 +1526,28 @@ void Renderer::paintGL() {
entities[i]->mesh.meshMatrices[j].data(),
lights[0].mtxShadow
);
float tmp[4];
bx::vec4MulMtx(
tmp,
lightMtx,
lights[0].pos);
bgfx::setUniform(lights[0].u_lightPos, tmp);
Matrix44f mesh_matrix = entities[i]->mesh.meshMatrices[j];
Vector4f light_pos (
lights[0].pos[0],
lights[0].pos[1],
lights[0].pos[2],
lights[0].pos[3]);
light_pos = mesh_matrix.inverse() * light_pos;
// tmp = light_pos;
bgfx::setUniform(lights[i].u_lightPos, light_pos.data());
bgfx::setUniform(lights[0].u_lightMtx, lightMtx);
bgfxutils::meshSubmit (
entities[i]->mesh.meshes[j],
@ -1537,13 +1559,21 @@ void Renderer::paintGL() {
// scene pass
for (uint32_t j = 0; j < entities[i]->mesh.meshes.size(); ++j) {
bx::mtxMul(
lightMtx,
entities[i]->mesh.meshMatrices[j].data(),
lights[0].mtxShadow
);
Matrix44f mesh_matrix = entities[i]->mesh.meshMatrices[j];
Vector4f light_pos (
lights[0].pos[0],
lights[0].pos[1],
lights[0].pos[2],
lights[0].pos[3]);
light_pos = mesh_matrix * light_pos;
// tmp = light_pos;
bgfx::setUniform(lights[i].u_lightPos, light_pos.data());
bgfx::setUniform(lights[0].u_lightMtx, lightMtx);
bgfx::setUniform(u_color, entities[i]->color, 4);
Vector4f color (0.1, 0.0, 1.0, 1.0);
bgfx::setUniform(u_color, color.data(), 4);
bgfxutils::meshSubmit (
entities[i]->mesh.meshes[j],
&s_renderStates[RenderState::Scene],