-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaterial.h
372 lines (278 loc) · 8.09 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* File: material.h; Mode: C++; Tab-width: 3; Author: Simon Flannery; */
#ifndef MATERIAL_H
#define MATERIAL_H
#include "math.h"
#include "ray.h"
#include "hit.h"
#include "perlin.h"
class Material
{
public:
Material() { }
Material(const vector3f& c)
{
color = c;
}
virtual color3f GetColor(const point3f&) const { return color; }
virtual color3f Emitted(const point3f&) const { return color3f(0.0f, 0.0f, 0.0f); }
virtual bool IsSpecular(const point3f&) const { return false; }
virtual bool Scatter(const Ray& ray, const Hit& hit, vector3f& scattered) const = 0;
// virtual float ScatterPdf(const Hit& hit, const vector3f& scattered) const { return 0.0f; }
virtual ~Material() { }
protected:
color3f color;
private:
friend class NoiseMaterial;
};
class DiffuseMaterial : public Material
{
public:
DiffuseMaterial(const color3f& c, const color3f light = color3f(0.0f, 0.0f, 0.0f)) : Material(c), glow(light)
{
}
virtual bool Scatter(const Ray& ray, const Hit& hit, vector3f& scattered) const
{
return true;
}
// virtual float ScatterPdf(const Hit& hit, const vector3f& scattered) const
// {
// float cosine = vector3f::Dot(hit.GetNormal(), scattered);
//
// return (float) fmax(FLT_EPSILON, cosine / PI);
// }
virtual color3f Emitted(const point3f&) const { return glow; }
protected:
private:
color3f glow;
};
class ReflectiveMaterial : public Material
{
public:
ReflectiveMaterial(const color3f& c, const float b = 0.0f) : Material(c), blur(b)
{
}
virtual bool IsSpecular(const point3f&) const { return true; }
virtual bool Scatter(const Ray& ray, const Hit& hit, vector3f& scattered) const
{
scattered = ReflectDirection(ray.GetDirection(), hit.GetNormal()) + blur * vector3f::RandomInHemisphere(hit.GetNormal());
return true;
}
protected:
static vector3f ReflectDirection(const vector3f& d, const vector3f& n)
{
return d - (2.0f * vector3f::Dot(d, n) * n);
}
private:
float blur;
};
class GlassMaterial : public ReflectiveMaterial
{
public:
GlassMaterial(const color3f& c, const float ir) : ReflectiveMaterial(c), refraction_index(ir)
{
}
virtual bool IsSpecular(const point3f&) const { return true; }
virtual bool Scatter(const Ray& ray, const Hit& hit, vector3f& scattered) const
{
float refraction_ratio = 1.0f / refraction_index;
vector3f normal = hit.GetNormal();
if (vector3f::Dot(ray.GetDirection(), normal) > 0.0f)
{
refraction_ratio = refraction_index;
normal.Negate();
}
float cos_theta = (float) fmin(vector3f::Dot(-ray.GetDirection(), normal), 1.0f);
float sin_theta = (float) sqrt(1.0f - (cos_theta * cos_theta));
if ((refraction_ratio * sin_theta) > 1.0f ||
Reflectance(cos_theta, refraction_ratio) > random_float())
{
scattered = ReflectDirection(ray.GetDirection(), normal);
}
else
{
scattered = RefractDirection(ray.GetDirection(), normal, cos_theta, refraction_ratio);
}
return true;
}
protected:
private:
static vector3f RefractDirection(const vector3f& uv, const vector3f& n, const float cos_theta, const float etai_over_etat)
{
vector3f perp = etai_over_etat * (uv + cos_theta * n);
vector3f parallel = (float) -sqrt(fabs(1.0f - perp.LengthSq())) * n;
return perp + parallel;
}
static double Reflectance(float cosine, float ref_idx)
{
// 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);
}
float refraction_index;
};
class Checkerboard : public Material
{
public:
Checkerboard(Matrix m, Material* m1, Material* m2) : matrix(m), material1(m1), material2(m2)
{
}
virtual color3f GetColor(const point3f& p) const
{
point3f t = p;
matrix.Transform(t);
int cx = (int) floor(t[x]);
int cy = (int) floor(t[y]);
int cz = (int) floor(t[z]);
color3f color;
if ((cx + cy + cz) % 2 == 0) /* Even! */
{
color = material1->GetColor(p);
}
else
{
color = material2->GetColor(p);
}
return color;
}
virtual color3f Emitted(const point3f& p) const
{
point3f t = p;
matrix.Transform(t);
int cx = (int) floor(t[x]);
int cy = (int) floor(t[y]);
int cz = (int) floor(t[z]);
color3f emitted;
if ((cx + cy + cz) % 2 == 0) /* Even! */
{
emitted = material1->Emitted(p);
}
else
{
emitted = material2->Emitted(p);
}
return emitted;
}
virtual bool IsSpecular(const point3f& p) const
{
point3f t = p;
matrix.Transform(t);
int cx = (int) floor(t[x]);
int cy = (int) floor(t[y]);
int cz = (int) floor(t[z]);
bool specular;
if ((cx + cy + cz) % 2 == 0) /* Even! */
{
specular = material1->IsSpecular(p);
}
else
{
specular = material2->IsSpecular(p);
}
return specular;
}
virtual bool Scatter(const Ray& ray, const Hit& hit, vector3f& scattered) const
{
point3f t = hit.GetIntersectionPoint();
matrix.Transform(t);
int cx = (int) floor(t[x]);
int cy = (int) floor(t[y]);
int cz = (int) floor(t[z]);
bool scatter;
if ((cx + cy + cz) % 2 == 0) /* Even! */
{
scatter = material1->Scatter(ray, hit, scattered);
}
else
{
scatter = material2->Scatter(ray, hit, scattered);
}
return scatter;
}
protected:
Matrix matrix;
Material* material1, * material2;
private:
};
class NoiseMaterial : public Material
{
public:
NoiseMaterial(Matrix m, Material* m1, Material* m2, size_t oct) : matrix(m), material1(m1), material2(m2), octaves(oct)
{
}
virtual bool Scatter(const Ray& ray, const Hit& hit, vector3f& scattered) const
{
scattered = hit.GetNormal() + vector3f::RandomInHemisphere(hit.GetNormal());
return true;
}
protected:
virtual float CalulateNoise(const point3f& point) const
{
point3f t = point;
matrix.Transform(t);
float noise = 0.0f;
for (size_t i = 0, j = 1; i < octaves; ++i, j = 2 * j)
{
noise = noise + (float) (Perlin::noise(t[x] * j, t[y] * j, t[z] * j) / j);
}
return noise;
}
Matrix matrix;
Material* material1, * material2;
size_t octaves;
private:
color3f GetColor(const point3f& point) const
{
float noise = CalulateNoise(point);
color3f color_range = material1->color - material2->color;
color3f color = (color_range * noise) + material2->color;
return color.Clamp();
}
};
class MarbleMaterial : public NoiseMaterial
{
public:
MarbleMaterial(Matrix m, Material* m1, Material* m2, size_t oct, float fre, float amp) : NoiseMaterial(m, m1, m2, oct), frequency(fre), amplitude(amp)
{
}
protected:
virtual float CalulateNoise(const point3f& point) const
{
point3f t = point;
matrix.Transform(t);
float noise = 0.0f;
for (size_t i = 0, j = 1; i < octaves; ++i, j = 2 * j)
{
noise = noise + (float) (Perlin::noise(t[x] * j, t[y] * j, t[z] * j) / j);
}
noise = (float) sin(frequency * t[x] + amplitude * noise);
return noise;
}
private:
float frequency;
float amplitude;
};
class WoodMaterial : public NoiseMaterial
{
public:
WoodMaterial(Matrix m, Material* m1, Material* m2, size_t oct, float fre, float amp) : NoiseMaterial(m, m1, m2, oct), frequency(fre), amplitude(amp)
{
}
protected:
virtual float CalulateNoise(const point3f& point) const
{
point3f t = point;
matrix.Transform(t);
float noise = 0.0f;
for (size_t i = 0, j = 1; i < octaves; ++i, j = 2 * j)
{
noise = noise + (float) (Perlin::noise(t[x] * j, t[y] * j, t[z] * j) / j);
}
noise = noise + (float) sin(frequency * sqrt(t[x] * t[x] + t[y] * t[y]) * amplitude);
return noise;
}
private:
float frequency;
float amplitude;
};
#endif