Arty
textures.h
1 #ifndef TEXTURES_H
2 #define TEXTURES_H
3 
4 #include "color.h"
5 #include "image.h"
6 
8 class Texture {
9 public:
10  virtual ~Texture() {}
11  virtual rgb operator () (float u, float v) const = 0;
12 };
13 
15 class ConstantTexture : public Texture {
16 public:
17  ConstantTexture(const rgb& c) : color(c) {}
18  rgb operator () (float, float) const override final { return color; }
19 
20 private:
21  rgb color;
22 };
23 
25 class ImageTexture : public Texture {
26 public:
27  ImageTexture(Image&& img) : img(img) {}
28 
29  rgb operator () (float u, float v) const override final {
30  u = u - (int)u;
31  u = u < 0.0f ? 1.0f + u : u;
32  v = v - (int)v;
33  v = v < 0.0f ? 1.0f + v : v;
34  v = 1.0f - v;
35  auto kx = u * img.width;;
36  auto ky = v * img.height;
37  auto ix = size_t(kx);
38  auto iy = size_t(ky);
39  auto fx = kx - ix;
40  auto fy = ky - iy;
41  auto x0 = clamp(ix, size_t(0), img.width - 1);
42  auto y0 = clamp(iy, size_t(0), img.height - 1);
43  auto x1 = x0 + 1 >= img.width ? 0 : x0 + 1;
44  auto y1 = y0 + 1 >= img.height ? 0 : y0 + 1;
45  return lerp(lerp(rgb(img(x0, y0)), rgb(img(x1, y0)), fx),
46  lerp(rgb(img(x0, y1)), rgb(img(x1, y1)), fx),
47  fy);
48  }
49 
50  const Image& image() const { return img; }
51 
52 private:
53  Image img;
54 };
55 
56 #endif // TEXTURES_h
Image-based texture, using bilinear filtering.
Definition: textures.h:25
Base class for all textures.
Definition: textures.h:8
Constant texture, returns the same value everywhere.
Definition: textures.h:15
Definition: image.h:9
Definition: color.h:9