-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture.cpp
More file actions
94 lines (79 loc) · 3.06 KB
/
Texture.cpp
File metadata and controls
94 lines (79 loc) · 3.06 KB
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
#include "Texture.hpp"
#include <iostream>
Image::Image(const std::string_view filename, bool gammaCorrection){
data = stbi_load(filename.data(), &width, &height, &channels, 0);
if(data == nullptr){
std::cerr << "Failed to load image: " << filename << std::endl;
std::abort();
}
if(gammaCorrection && data){
for(int y = 0;y < height;y++){
for(int x = 0;x < width;x++){
for(int i = 0;i < std::min(channels, 3);i++){
data[(y * width + x) * channels + i] = sRGBLUT[data[(y * width + x) * channels + i]];
}
}
}
}
}
Image::~Image(){
stbi_image_free((void*)data);
}
FloatImage::FloatImage(const std::string_view filename){
data = stbi_loadf(filename.data(), &width, &height, &channels, 0);
if(data == nullptr){
std::cerr << "Failed to load image: " << filename << std::endl;
std::abort();
}
}
FloatImage::~FloatImage(){
stbi_image_free((void*)data);
}
SolidColor::SolidColor(const glm::vec3& color, const glm::vec3& colorScale, bool invertTex) : Texture(colorScale, invertTex), albedo(color){}
SolidColor::SolidColor(float r, float g, float b, const glm::vec3& colorScale, bool invertTex) : Texture(colorScale, invertTex), albedo(r, g, b){}
glm::vec3 ImageTexture::texel(int x, int y) const{
glm::ivec2 p = { x,y };
return { image.GetChannelAt(p,1),image.GetChannelAt(p,2),image.GetChannelAt(p,3) };
}
float ImageTexture::alpha(const glm::vec2& uv) const{
if(image.Channels() != 4)return 1;
glm::ivec2 res = image.Resolution();
float x = uv.x * res.x - 0.5f;
float y = uv.y * res.y - 0.5f;
int xi = std::floor(x);
int yi = std::floor(y);
float dx = x - xi;
float dy = y - yi;
float a = image.GetChannelAt({ xi,yi }, 4);
float b = image.GetChannelAt({ xi + 1,yi }, 4);
float c = image.GetChannelAt({ xi,yi + 1 }, 4);
float d = image.GetChannelAt({ xi + 1,yi + 1 }, 4);
return ((1 - dx) * (1 - dy) * a + dx * (1 - dy) * b +
(1 - dx) * dy * c + dx * dy * d);
}
glm::vec3 FloatImageTexture::texel(int x, int y) const{
// repeat wrap:
glm::ivec2 p = { x,y };
return { image.GetChannelAt(p,1),image.GetChannelAt(p,2),image.GetChannelAt(p,3) };
}
float FloatImageTexture::alpha(const glm::vec2& uv) const{
if(image.Channels() != 4)return 1;
glm::ivec2 res = image.Resolution();
float x = uv.x * res.x - 0.5f;
float y = uv.y * res.y - 0.5f;
int xi = std::floor(x);
int yi = std::floor(y);
float dx = x - xi;
float dy = y - yi;
float a = image.GetChannelAt({ xi,yi }, 4);//image.image.GetChannelAt
float b = image.GetChannelAt({ xi + 1,yi }, 4);
float c = image.GetChannelAt({ xi,yi + 1 }, 4);
float d = image.GetChannelAt({ xi + 1,yi + 1 }, 4);
return ((1 - dx) * (1 - dy) * a + dx * (1 - dy) * b +
(1 - dx) * dy * c + dx * dy * d);
}
float CheckerTexture::alpha(const glm::vec2& uv) const{
glm::ivec2 iuv = glm::floor(uv * invScale);
if((iuv.x + iuv.y) % 2 == 0)return tex1->alpha(uv);
return tex2->alpha(uv);
}