-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaterial.c
55 lines (43 loc) · 1.25 KB
/
material.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdlib.h>
#include <math.h>
#include "material.h"
Vector reflect(const Vector v, const Vector n)
{
Vector vp = new_vector(new_point(0, 0, 0));
float tmp = 2.0 * v.dot(v, n);
vp = vp.mul_scalar(n, tmp);
vp = vp.sub(v, vp);
return vp;
}
bool refract(const Vector v, const Vector n, float ni_over_nt, Vector *refracted)
{
Vector uv = unit_vector(v);
float dt = uv.dot(uv, n);
float discriminat = 1.0 - ni_over_nt * ni_over_nt * (1 - dt * dt);
if (discriminat > 0) {
// ni_over_nt * (uv - n * dt) - n * sqrt(discriminat)
Vector tmp = uv.sub(uv, n.mul_scalar(n, dt));
tmp = uv.mul_scalar(tmp, ni_over_nt);
tmp = tmp.sub(tmp, n.mul_scalar(n, sqrt(discriminat)));
*refracted = tmp;
return true;
} else {
return false;
}
}
float schlick(float cosine, float ref_idx)
{
float r0 = (1 - ref_idx) / (1 + ref_idx);
r0 = r0 * r0;
return r0 + (1 - r0) * pow((1 - cosine), 5);
}
Vector random_in_uint_sphere(void)
{
Vector p;
do {
p = new_vector(new_point(drand48(), drand48(), drand48()));
p = p.mul_scalar(p, 2.0);
p = p.sub(p, new_vector(new_point(1, 1, 1)));
} while (p.squared_length(p) >= 1.0);
return p;
}