refmod_test

view 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 source
1 #version 110
3 #define SQ(x) ((x) * (x))
4 #define PI 3.141592653
6 /* -- Lambert's cosine law
7 * J. H. Lambert, Photometria sive de mensura de gratibus luminis, colorum et umbrae.
8 * Eberhard Klett, 1760.
9 */
10 float lambert(vec3 norm, vec3 ldir)
11 {
12 return max(dot(norm, ldir), 0.0);
13 }
15 /* -- Phong specular model
16 * B. T. Phong, "Illumination for Computer Generated Pictures"
17 * Communications of the ACM, 1975.
18 */
19 float phong(vec3 view, vec3 norm, vec3 ldir, float spow)
20 {
21 vec3 lref = reflect(-ldir, norm);
22 return pow(max(dot(lref, view), 0.0), spow);
23 }
25 /* -- Blinn-Phong specular model
26 * J. F. Blinn, "Models of Light Reflection for Computer Synthesized Pictures"
27 * in Proceedings of SIGGRAPH 1977
28 */
29 float blinn(vec3 norm, vec3 hvec, float spow)
30 {
31 return pow(max(dot(norm, hvec), 0.0), spow);
32 }
34 /* -- Cook & Torrance model.
35 * R. L. Cook and K. E. Torrance, "A Reflectance Model for Computer Graphics"
36 * in Proceedings of SIGGRAPH 1981.
37 */
38 float cook_torrance(vec3 view, vec3 norm, vec3 ldir, vec3 hvec, float rough, float ior)
39 {
40 float m = max(rough, 0.0001);
42 // various useful dot products
43 float ndoth = max(dot(norm, hvec), 0.0);
44 float ndotv = max(dot(norm, view), 0.0);
45 float ndotl = dot(norm, ldir);
46 float vdoth = max(dot(view, hvec), 0.0);
47 float ndoth_sq = SQ(ndoth);
49 // geometric term (shadowing/masking)
50 float geom_a = (2.0 * ndoth * ndotv) / vdoth;
51 float geom_b = (2.0 * ndoth * ndotl) / vdoth;
52 float geom = min(1.0, min(geom_a, geom_b));
54 // beckmann microfacet distribution term
55 float sin2_ang = 1.0 - ndoth_sq; // sin^2(a) = 1 - cos^2(a)
56 float tan2_ang = sin2_ang / ndoth_sq; // tan^2(a) = sin^2(a) / cos^2(a)
57 float d_mf = exp(-tan2_ang / SQ(m)) / (SQ(m) * SQ(ndoth_sq));
59 // fresnel term
60 float c = vdoth;
61 float g = sqrt(SQ(ior) + SQ(c) - 1.0);
62 float ftmp = (c * (g + c) - 1.0) / (c * (g - c) + 1.0);
63 float fres = 0.5 * (SQ(g - c) / SQ(g + c)) * (1.0 + SQ(ftmp));
65 return (fres / PI) * (d_mf / ndotl) * (geom / ndotv);
66 }
68 /* -- Oren & Nayar model
69 * M. Oren and S. K. Nayar, "Generalization of Lambert's Reflectance Model"
70 * in Proceedings of SIGGRAPH 1994.
71 */
72 float oren_nayar(vec3 view, vec3 norm, vec3 ldir, float rough)
73 {
74 float vdotn = max(dot(view, norm), 0.0);
75 float ldotn = max(dot(ldir, norm), 0.0);
77 float theta_r = acos(vdotn);
78 float theta_i = acos(ldotn);
79 float cos_pr_minus_pi = dot(normalize(view - norm * vdotn), normalize(ldir - norm * ldotn));
80 float alpha = max(theta_r, theta_i);
81 float beta = min(theta_r, theta_i);
83 float sigma_sq = SQ(rough);
84 float a = 1.0 - 0.5 * sigma_sq / (sigma_sq + 0.33);
85 float b = 0.45 * sigma_sq / (sigma_sq + 0.09);
87 if(cos_pr_minus_pi >= 0.0) {
88 b *= cos_pr_minus_pi * sin(alpha) * tan(beta);
89 } else {
90 b = 0.0;
91 }
93 return ldotn * (a + b);
94 }
96 /* TODO: need tangent/bitangent
97 float ward(vec3 view, vec3 norm, vec3 ldir, vec3 hvec, vec3 tang, vec3 bitan, float ax, float ay)
98 {
99 float vdotn = dot(view, norm);
100 float ldotn = dot(light, norm);
101 }
102 */