#version 110 #define SQ(x) ((x) * (x)) #define PI 3.141592653 /* -- Lambert's cosine law * J. H. Lambert, Photometria sive de mensura de gratibus luminis, colorum et umbrae. * Eberhard Klett, 1760. */ float lambert(vec3 norm, vec3 ldir) { return max(dot(norm, ldir), 0.0); } /* -- Phong specular model * B. T. Phong, "Illumination for Computer Generated Pictures" * Communications of the ACM, 1975. */ float phong(vec3 view, vec3 norm, vec3 ldir, float spow) { vec3 lref = reflect(-ldir, norm); return pow(max(dot(lref, view), 0.0), spow); } /* -- Blinn-Phong specular model * J. F. Blinn, "Models of Light Reflection for Computer Synthesized Pictures" * in Proceedings of SIGGRAPH 1977 */ float blinn(vec3 norm, vec3 hvec, float spow) { return pow(max(dot(norm, hvec), 0.0), spow); } /* -- Cook & Torrance model. * R. L. Cook and K. E. Torrance, "A Reflectance Model for Computer Graphics" * in Proceedings of SIGGRAPH 1981. */ float cook_torrance(vec3 view, vec3 norm, vec3 ldir, vec3 hvec, float rough, float ior) { float m = max(rough, 0.0001); // various useful dot products float ndoth = max(dot(norm, hvec), 0.0); float ndotv = max(dot(norm, view), 0.0); float ndotl = dot(norm, ldir); float vdoth = max(dot(view, hvec), 0.0); float ndoth_sq = SQ(ndoth); // geometric term (shadowing/masking) float geom_a = (2.0 * ndoth * ndotv) / vdoth; float geom_b = (2.0 * ndoth * ndotl) / vdoth; float geom = min(1.0, min(geom_a, geom_b)); // beckmann microfacet distribution term float sin2_ang = 1.0 - ndoth_sq; // sin^2(a) = 1 - cos^2(a) float tan2_ang = sin2_ang / ndoth_sq; // tan^2(a) = sin^2(a) / cos^2(a) float d_mf = exp(-tan2_ang / SQ(m)) / (SQ(m) * SQ(ndoth_sq)); // fresnel term float c = vdoth; float g = sqrt(SQ(ior) + SQ(c) - 1.0); float ftmp = (c * (g + c) - 1.0) / (c * (g - c) + 1.0); float fres = 0.5 * (SQ(g - c) / SQ(g + c)) * (1.0 + SQ(ftmp)); return (fres / PI) * (d_mf / ndotl) * (geom / ndotv); } /* -- Oren & Nayar model * M. Oren and S. K. Nayar, "Generalization of Lambert's Reflectance Model" * in Proceedings of SIGGRAPH 1994. */ float oren_nayar(vec3 view, vec3 norm, vec3 ldir, float rough) { float vdotn = max(dot(view, norm), 0.0); float ldotn = max(dot(ldir, norm), 0.0); float theta_r = acos(vdotn); float theta_i = acos(ldotn); float cos_pr_minus_pi = dot(normalize(view - norm * vdotn), normalize(ldir - norm * ldotn)); float alpha = max(theta_r, theta_i); float beta = min(theta_r, theta_i); float sigma_sq = SQ(rough); float a = 1.0 - 0.5 * sigma_sq / (sigma_sq + 0.33); float b = 0.45 * sigma_sq / (sigma_sq + 0.09); if(cos_pr_minus_pi >= 0.0) { b *= cos_pr_minus_pi * sin(alpha) * tan(beta); } else { b = 0.0; } return ldotn * (a + b); } /* TODO: need tangent/bitangent float ward(vec3 view, vec3 norm, vec3 ldir, vec3 hvec, vec3 tang, vec3 bitan, float ax, float ay) { float vdotn = dot(view, norm); float ldotn = dot(light, norm); } */