refmod_test

diff sdr_ref_models.glsl @ 0:b469e6a72636

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 18 Feb 2016 23:15:43 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sdr_ref_models.glsl	Thu Feb 18 23:15:43 2016 +0200
     1.3 @@ -0,0 +1,102 @@
     1.4 +#version 110
     1.5 +
     1.6 +#define SQ(x)	((x) * (x))
     1.7 +#define PI		3.141592653
     1.8 +
     1.9 +/* -- Lambert's cosine law
    1.10 + * J. H. Lambert, Photometria sive de mensura de gratibus luminis, colorum et umbrae.
    1.11 + * Eberhard Klett, 1760.
    1.12 + */
    1.13 +float lambert(vec3 norm, vec3 ldir)
    1.14 +{
    1.15 +	return max(dot(norm, ldir), 0.0);
    1.16 +}
    1.17 +
    1.18 +/* -- Phong specular model
    1.19 + * B. T. Phong, "Illumination for Computer Generated Pictures"
    1.20 + * Communications of the ACM, 1975.
    1.21 + */
    1.22 +float phong(vec3 view, vec3 norm, vec3 ldir, float spow)
    1.23 +{
    1.24 +	vec3 lref = reflect(-ldir, norm);
    1.25 +	return pow(max(dot(lref, view), 0.0), spow);
    1.26 +}
    1.27 +
    1.28 +/* -- Blinn-Phong specular model
    1.29 + * J. F. Blinn, "Models of Light Reflection for Computer Synthesized Pictures"
    1.30 + * in Proceedings of SIGGRAPH 1977
    1.31 + */
    1.32 +float blinn(vec3 norm, vec3 hvec, float spow)
    1.33 +{
    1.34 +	return pow(max(dot(norm, hvec), 0.0), spow);
    1.35 +}
    1.36 +
    1.37 +/* -- Cook & Torrance model.
    1.38 + * R. L. Cook and K. E. Torrance, "A Reflectance Model for Computer Graphics"
    1.39 + * in Proceedings of SIGGRAPH 1981.
    1.40 + */
    1.41 +float cook_torrance(vec3 view, vec3 norm, vec3 ldir, vec3 hvec, float rough, float ior)
    1.42 +{
    1.43 +	float m = max(rough, 0.0001);
    1.44 +	
    1.45 +	// various useful dot products
    1.46 +	float ndoth = max(dot(norm, hvec), 0.0);
    1.47 +	float ndotv = max(dot(norm, view), 0.0);
    1.48 +	float ndotl = dot(norm, ldir);
    1.49 +	float vdoth = max(dot(view, hvec), 0.0);
    1.50 +	float ndoth_sq = SQ(ndoth);
    1.51 +
    1.52 +	// geometric term (shadowing/masking)
    1.53 +	float geom_a = (2.0 * ndoth * ndotv) / vdoth;
    1.54 +	float geom_b = (2.0 * ndoth * ndotl) / vdoth;
    1.55 +	float geom = min(1.0, min(geom_a, geom_b));
    1.56 +
    1.57 +	// beckmann microfacet distribution term
    1.58 +	float sin2_ang = 1.0 - ndoth_sq;	// sin^2(a) = 1 - cos^2(a)
    1.59 +	float tan2_ang = sin2_ang / ndoth_sq;	// tan^2(a) = sin^2(a) / cos^2(a)
    1.60 +	float d_mf = exp(-tan2_ang / SQ(m)) / (SQ(m) * SQ(ndoth_sq));
    1.61 +
    1.62 +	// fresnel term
    1.63 +	float c = vdoth;
    1.64 +	float g = sqrt(SQ(ior) + SQ(c) - 1.0);
    1.65 +	float ftmp = (c * (g + c) - 1.0) / (c * (g - c) + 1.0);
    1.66 +	float fres = 0.5 * (SQ(g - c) / SQ(g + c)) * (1.0 + SQ(ftmp));
    1.67 +
    1.68 +	return (fres / PI) * (d_mf / ndotl) * (geom / ndotv);
    1.69 +}
    1.70 +
    1.71 +/* -- Oren & Nayar model
    1.72 + * M. Oren and S. K. Nayar, "Generalization of Lambert's Reflectance Model"
    1.73 + * in Proceedings of SIGGRAPH 1994.
    1.74 + */
    1.75 +float oren_nayar(vec3 view, vec3 norm, vec3 ldir, float rough)
    1.76 +{
    1.77 +	float vdotn = max(dot(view, norm), 0.0);
    1.78 +	float ldotn = max(dot(ldir, norm), 0.0);
    1.79 +
    1.80 +	float theta_r = acos(vdotn);
    1.81 +	float theta_i = acos(ldotn);
    1.82 +	float cos_pr_minus_pi = dot(normalize(view - norm * vdotn), normalize(ldir - norm * ldotn));
    1.83 +	float alpha = max(theta_r, theta_i);
    1.84 +	float beta = min(theta_r, theta_i);
    1.85 +
    1.86 +	float sigma_sq = SQ(rough);
    1.87 +	float a = 1.0 - 0.5 * sigma_sq / (sigma_sq + 0.33);
    1.88 +	float b = 0.45 * sigma_sq / (sigma_sq + 0.09);
    1.89 +
    1.90 +	if(cos_pr_minus_pi >= 0.0) {
    1.91 +		b *= cos_pr_minus_pi * sin(alpha) * tan(beta);
    1.92 +	} else {
    1.93 +		b = 0.0;
    1.94 +	}
    1.95 +
    1.96 +	return ldotn * (a + b);
    1.97 +}
    1.98 +
    1.99 +/* TODO: need tangent/bitangent
   1.100 +float ward(vec3 view, vec3 norm, vec3 ldir, vec3 hvec, vec3 tang, vec3 bitan, float ax, float ay)
   1.101 +{
   1.102 +	float vdotn = dot(view, norm);
   1.103 +	float ldotn = dot(light, norm);
   1.104 +}
   1.105 +*/