-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexture.hpp
67 lines (61 loc) · 1.65 KB
/
texture.hpp
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
#pragma once
#include "vector.hpp"
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
namespace pathtracing
{
struct Texture
{
virtual Vector3 get_color(double u, double v) const = 0;
};
struct Strip : public Texture
{
public:
explicit Strip(Vector3 color1 = Vector3(), Vector3 color2 = Vector3(0.75), unsigned int n_lines = 20U) : m_c1(color1), m_c2(color2), m_n_lines(n_lines){};
Vector3 get_color(double u, double v) const
{
unsigned int u_ = (int)(v * m_n_lines * 100);
unsigned int location = u_ % 100;
// std::cout << u << " " << u_ << " " << location << std::endl;
if (location < 40)
{
return m_c1;
}
else
{
return m_c2;
}
}
private:
Vector3 m_c1, m_c2;
unsigned int m_n_lines;
};
struct Image : public Texture
{
public:
explicit Image(const char *filename, bool updown = false) : m_updown(updown)
{
data = stbi_load(filename, &width, &height, &color_bytes, 3);
};
Vector3 get_color(double u, double v) const
{
//std::cout<< u << " "<< v << std::endl;
int column = (int)(u * width);
int row = (int)(v * height);
if (m_updown)
row = height - row;
unsigned long base = (row * width + column) * 3;
//std::cout<< base << std::endl;
return Vector3((double)data[base] / 255, (double)data[base + 1] / 255, (double)data[base + 2] / 255);
}
~Image()
{
stbi_image_free(data);
}
private:
int width, height, color_bytes;
unsigned char *data;
bool m_updown;
};
} // namespace pathtracing