fixed lights. Diffuse, shadows, work, specular a bit off

master
Martin Felis 2017-02-22 14:13:02 +01:00
parent 59522a9560
commit 91172e2429
2 changed files with 16 additions and 99 deletions

View File

@ -11,7 +11,6 @@ uniform vec4 u_color;
uniform vec4 u_shadowMapParams; uniform vec4 u_shadowMapParams;
#define u_shadowMapSize u_shadowMapParams.x #define u_shadowMapSize u_shadowMapParams.x
#define u_shadowMapBias u_shadowMapParams.y #define u_shadowMapBias u_shadowMapParams.y
#define shininess 0.5
#if SHADOW_PACKED_DEPTH #if SHADOW_PACKED_DEPTH
SAMPLER2D(u_shadowMap, 0); SAMPLER2D(u_shadowMap, 0);
@ -32,20 +31,6 @@ vec2 lit(vec3 _ld, vec3 _n, vec3 _vd, float _exp)
//spec //spec
vec3 r = 2.0*ndotl*_n - _ld; //reflect(_ld, _n); 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);
}
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 rdotv = dot(r, _n);
float spec = step(0.0, ndotl) * pow(max(0.0, rdotv), _exp) * (2.0 + _exp)/8.0; float spec = step(0.0, ndotl) * pow(max(0.0, rdotv), _exp) * (2.0 + _exp)/8.0;
@ -101,39 +86,6 @@ float PCF(Sampler _sampler, vec4 _shadowCoord, float _bias, vec2 _texelSize)
return result / 16.0; 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() void main()
{ {
vec3 color = u_color.xyz; vec3 color = u_color.xyz;
@ -141,29 +93,16 @@ void main()
vec3 v = v_view; vec3 v = v_view;
vec3 vd = -normalize(v); vec3 vd = -normalize(v);
vec3 n = v_normal; vec3 n = v_normal;
vec3 l = u_lightPos.xyz - v_view; vec3 l = u_lightPos.xyz;
vec3 ld = normalize(l); vec3 ld = normalize(l);
vec2 lc = lit(ld, n, vd, 128.0); vec2 lc = lit(ld, n, vd, 1.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); vec2 texelSize = vec2_splat(1.0/u_shadowMapSize);
float visibility = PCF(u_shadowMap, v_shadowcoord, u_shadowMapBias, texelSize); float visibility = PCF(u_shadowMap, v_shadowcoord, u_shadowMapBias, texelSize);
vec3 ambient = 0.05 * color; vec3 ambient = 0.05 * color;
// if (spec > 0.1) vec3 brdf = (lc.x * color + lc.y * color) * visibility;
// 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 #if TEXTURED
vec4 texcolor = toLinear (texture2D(sceneDefaultTexture, v_texcoord0) ); vec4 texcolor = toLinear (texture2D(sceneDefaultTexture, v_texcoord0) );

View File

@ -1526,28 +1526,6 @@ void Renderer::paintGL() {
entities[i]->mesh.meshMatrices[j].data(), entities[i]->mesh.meshMatrices[j].data(),
lights[0].mtxShadow 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); bgfx::setUniform(lights[0].u_lightMtx, lightMtx);
bgfxutils::meshSubmit ( bgfxutils::meshSubmit (
entities[i]->mesh.meshes[j], entities[i]->mesh.meshes[j],
@ -1559,27 +1537,27 @@ void Renderer::paintGL() {
// scene pass // scene pass
for (uint32_t j = 0; j < entities[i]->mesh.meshes.size(); ++j) { for (uint32_t j = 0; j < entities[i]->mesh.meshes.size(); ++j) {
Matrix44f mesh_matrix = entities[i]->mesh.meshMatrices[j]; bx::mtxMul(
lightMtx,
entities[i]->mesh.meshMatrices[j].data(),
lights[0].mtxShadow
);
Vector4f light_pos ( // compute world position of the light
lights[0].pos[0], Vector4f light_pos =
lights[0].pos[1], entities[i]->mesh.meshMatrices[j]
lights[0].pos[2], * SimpleMath::Map<Vector4f>(lights[0].pos, 4, 1);
lights[0].pos[3]);
light_pos = mesh_matrix * light_pos; bgfx::setUniform(lights[0].u_lightPos, light_pos.data());
// tmp = light_pos;
bgfx::setUniform(lights[i].u_lightPos, light_pos.data());
bgfx::setUniform(lights[0].u_lightMtx, lightMtx); bgfx::setUniform(lights[0].u_lightMtx, lightMtx);
Vector4f color (0.1, 0.0, 1.0, 1.0); bgfx::setUniform(u_color, entities[i]->color);
bgfx::setUniform(u_color, color.data(), 4);
bgfxutils::meshSubmit ( bgfxutils::meshSubmit (
entities[i]->mesh.meshes[j], entities[i]->mesh.meshes[j],
&s_renderStates[RenderState::Scene], &s_renderStates[RenderState::Scene],
1, 1,
entities[i]->mesh.meshMatrices[j].data() entities[i]->mesh.meshMatrices[j].data()
); );
} }
} }