-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckTexture.h
37 lines (29 loc) · 1.23 KB
/
checkTexture.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
#pragma once
#include "rt.h"
class CheckerTexture : public Texture
{
private:
TextureType texture_type = TextureType::CHECKER_TEXTURE;
double inv_scale;
// Single Color Texture -> even
std::shared_ptr<Texture> even;
// Single Color Texture -> odd
std::shared_ptr<Texture> odd;
public:
// Shared_Ptr Use Copy to make sure the count is correct when use texture
CheckerTexture(double scale, std::shared_ptr<Texture> even, std::shared_ptr<Texture> odd)
: inv_scale(1.0 / scale), even(even), odd(odd) {}
CheckerTexture(double scale,
const color& c1, const color& c2) :
inv_scale(1.0 / scale),
even(std::make_shared<SolidColor>(c1)),
odd(std::make_shared<SolidColor>(c2)) {}
color value(double u, double v, const vec3& p) const override
{
auto xInteger = static_cast<int>(std::floor(inv_scale * p.x()));
auto yInteger = static_cast<int>(std::floor(inv_scale * p.y()));
auto zInteger = static_cast<int>(std::floor(inv_scale * p.z()));
bool isEven = (xInteger + yInteger + zInteger) % 2 == 0;
return isEven ? even->value(u, v, p) : odd->value(u, v, p);
}
};