-
Notifications
You must be signed in to change notification settings - Fork 0
/
texture.h
66 lines (46 loc) · 2.26 KB
/
texture.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
#ifndef TEXTURE_H
#define TEXTURE_H
#include "color.h"
class Texture {
public:
virtual ~Texture() = default;
virtual Color value(double u, double v, Point3 const & p) const = 0;
};
class SolidColorTexture : public Texture {
public:
SolidColorTexture(Color c);
SolidColorTexture(double red, double green, double blue);
Color value(double u, double v, Point3 const & p) const override;
private:
Color _color;
};
class CheckeredTexture : public Texture {
public:
CheckeredTexture(double scale, Color const & odd, Color const & even);
CheckeredTexture(double scale, std::shared_ptr<Texture> const & odd, std::shared_ptr<Texture> const & even);
Color value(double u, double v, Point3 const & p) const override;
private:
double _invertScaleFactor;
std::shared_ptr<Texture> _oddPatternTexture;
std::shared_ptr<Texture> _evenPatternTexture;
};
// ------
SolidColorTexture::SolidColorTexture(Color c) : _color(c) { }
SolidColorTexture::SolidColorTexture(double red, double green, double blue) : _color(Color(red, green, blue)) { }
Color SolidColorTexture::value(double u, double v, Point3 const & p) const {
return this->_color;
}
CheckeredTexture::CheckeredTexture(double scale, Color const & odd, Color const & even)
: _invertScaleFactor(1.0 / scale),
_oddPatternTexture(std::make_shared<SolidColorTexture>(odd)),
_evenPatternTexture(std::make_shared<SolidColorTexture>(even)) { }
CheckeredTexture::CheckeredTexture(double scale, std::shared_ptr<Texture> const & odd, std::shared_ptr<Texture> const & even)
: _invertScaleFactor(1.0 / scale), _oddPatternTexture(odd), _evenPatternTexture(even) { }
Color CheckeredTexture::value(double u, double v, Point3 const & p) const {
auto x = static_cast<int>(std::floor(p.x * this->_invertScaleFactor));
auto y = static_cast<int>(std::floor(p.y * this->_invertScaleFactor));
auto z = static_cast<int>(std::floor(p.z * this->_invertScaleFactor));
auto isEven = ((x + y + z) % 2) == 0;
return isEven ? _evenPatternTexture->value(u, v, p) : _oddPatternTexture->value(u, v, p);
}
#endif