-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaterial.h
117 lines (93 loc) · 3.92 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#ifndef MATERIAL_H
#define MATERIAL_H
#include "rtweekend.h"
#include "texture.h"
struct hit_record;
class material {
public:
__device__ virtual color emitted(double u, double v, const point3& p) const {
return color(0,0,0);
}
__device__ virtual bool scatter(
const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered, curandState* local_rand_state
) const = 0;
};
class lambertian : public material {
public:
__host__ __device__ lambertian(const color& a) : albedo(new solid_color(a)) {}
__host__ __device__ lambertian(my_texture* a) : albedo(a) {}
__device__ virtual bool scatter(
const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered, curandState* local_rand_state
) const override {
vec3 scatter_direction = rec.normal + random_unit_vector(local_rand_state);
// Catch degenerate scatter direction
if (scatter_direction.near_zero())
scatter_direction = rec.normal;
scattered = ray(rec.p, scatter_direction, r_in.time());
attenuation = albedo->value(rec.u, rec.v, rec.p);
return true;
}
public:
my_texture* albedo;
};
class metal : public material {
public:
__host__ __device__ metal(const color& a, float f) : albedo(a), fuzz(f < 1.0f ? f : 1.0f) {}
__device__ virtual bool scatter(
const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered, curandState* local_rand_state
) const override {
vec3 reflected = reflect(unit_vector(r_in.direction()), rec.normal);
scattered = ray(rec.p, reflected + fuzz*random_in_unit_sphere(local_rand_state), r_in.time());
attenuation = albedo;
return (dot(scattered.direction(), rec.normal) > 0.0f);
}
public:
color albedo;
float fuzz;
};
class dielectric : public material {
public:
__host__ __device__ dielectric(float index_of_refraction) : ir(index_of_refraction) {}
__device__ virtual bool scatter(
const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered, curandState* local_rand_state
) const override {
attenuation = color(1.0f, 1.0f, 1.0f);
float refraction_ratio = rec.front_face ? (1.0f/ir) : ir;
vec3 unit_direction = unit_vector(r_in.direction());
float cos_theta = fmin(dot(-unit_direction, rec.normal), 1.0f);
float sin_theta = sqrt(1.0f - cos_theta*cos_theta);
bool cannot_refract = refraction_ratio * sin_theta > 1.0f;
vec3 direction;
if (cannot_refract || reflectance(cos_theta, refraction_ratio) > random_float(local_rand_state))
direction = reflect(unit_direction, rec.normal);
else
direction = refract(unit_direction, rec.normal, refraction_ratio);
scattered = ray(rec.p, direction, r_in.time());
return true;
}
public:
float ir; // Index of Refraction
private:
__device__ static float reflectance(float cosine, float ref_idx) {
// Use Schlick's approximation for reflectance.
float r0 = (1.0f-ref_idx) / (1.0f+ref_idx);
r0 = r0*r0;
return r0 + (1.0f-r0)*pow((1.0f - cosine),5.0f);
}
};
class diffuse_light : public material {
public:
__device__ diffuse_light(my_texture* a) : emit(a) {}
__device__ diffuse_light(color c) : emit(new solid_color(c)) {}
__device__ virtual bool scatter(
const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered, curandState* local_rand_state
) const override {
return false;
}
__device__ virtual color emitted(double u, double v, const point3& p) const override {
return emit->value(u, v, p);
}
public:
my_texture* emit;
};
#endif