-
Notifications
You must be signed in to change notification settings - Fork 1
/
material.h
86 lines (73 loc) · 1.87 KB
/
material.h
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef MATERIAL_H
# define MATERIAL_H
# include "rtweekend.h"
typedef enum surface
{
metal,
lambertian,
dielectric
} surface;
typedef struct material
{
color albedo;
surface surface;
double fuzz;
double ir;
} material;
typedef struct hit_record
{
point3 p;
vec3 normal;
material material;
double t;
int front_face;
} hit_record;
material material_(surface s, color c, double f, double ir)
{
material m;
m.surface = s;
m.albedo = c;
m.fuzz = f;
m.ir = ir;
return (m);
}
static double reflectance(double cosine, double ref_idx)
{
double r0 = (1-ref_idx) / (1+ref_idx);
r0 = r0*r0;
return (r0 + (1-r0)*pow((1-cosine), 5));
}
int scatter(ray *r_in, hit_record *rec, color *attenuation, ray *scattered)
{
vec3 direction;
vec3 unit_direction;
double refraction_ratio;
double cos_theta;
double sin_theta;
switch (rec->material.surface)
{
case lambertian:
direction = add(rec->normal, random_unit_vector());
if (near_zero(direction))
direction = rec->normal;
break;
case dielectric:
refraction_ratio = rec->front_face ? (1.0/rec->material.ir) : rec->material.ir;
unit_direction = unit_vector(r_in->direction);
cos_theta = fmin(dot(negate(unit_direction), rec->normal), 1.0);
sin_theta = sqrt(1.0 - cos_theta*cos_theta);
if (refraction_ratio * sin_theta > 1.0 || reflectance(cos_theta, refraction_ratio) > random_double())
direction = reflect(unit_vector(r_in->direction), rec->normal);
else
direction = refract(unit_direction, rec->normal, refraction_ratio);
break;
case metal:
direction = reflect(unit_vector(r_in->direction), rec->normal);
add_(&direction, multiply(random_in_unit_sphere(), rec->material.fuzz));
break;
}
*scattered = ray_(rec->p, direction, r_in->time);
*attenuation = rec->material.albedo;
return (rec->material.surface != metal || dot(scattered->direction, rec->normal) > 0);
}
#endif