/
material.cpp
173 lines (152 loc) · 4.72 KB
/
material.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// <one line to give the program's name and a brief idea of what it does.>
// SPDX-FileCopyrightText: 2023 42yeah <email>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "material.h"
#include "luaenv.h"
int material_id_counter = 0;
HitInfo hit_info()
{
return HitInfo{
0.0f, 1.0f, vec3(0.0f, 0.0f, 0.0f), vec3(0.1f, 0.0f, 0.1f), vec3(1.0f, 0.0f, 1.0f), vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 0.0f, 0.0f)
};
}
Material::Material() : material_name("boring"), metallic(0), metallic_tex(nullptr), ior(1.0f), emission(RGB<float>(0.0f, 0.0f, 0.0f)), emissive_tex(nullptr), ambient(RGB<float>(0.1f, 0.0f, 0.1f)), ambient_tex(nullptr), diffuse({ 1.0f, 0.0f, 1.0f }), diffuse_tex(nullptr), normal_tex(nullptr), specular(RGB<float>(0.0f, 0.0f, 0.0f)), specular_tex(nullptr), id_(material_id_counter++)
{
}
Material::Material(const tinyobj::material_t &mat, std::string mtl_base_dir) : Material()
{
if (mtl_base_dir.empty())
{
mtl_base_dir = "./";
}
else if (mtl_base_dir[mtl_base_dir.size() - 1] != '/')
{
mtl_base_dir += "/";
}
Resources *r = res();
material_name = mat.name;
metallic = mat.metallic;
if (!mat.metallic_texname.empty())
{
metallic_tex = std::make_shared<U8Image>();
metallic_tex->load(mtl_base_dir + mat.metallic_texname); // TODO: issues might arise in the future (path)
r->images.push_back(metallic_tex);
}
ior = mat.ior;
emission = RGB<float>(mat.emission[0], mat.emission[1], mat.emission[2]);
if (!mat.emissive_texname.empty())
{
emissive_tex = std::make_shared<U8Image>();
emissive_tex->load(mtl_base_dir + mat.emissive_texname);
r->images.push_back(emissive_tex);
}
ambient = RGB<float>(mat.ambient[0], mat.ambient[1], mat.ambient[2]);
if (!mat.ambient_texname.empty())
{
ambient_tex = std::make_shared<U8Image>();
ambient_tex->load(mtl_base_dir + mat.ambient_texname);
r->images.push_back(ambient_tex);
}
diffuse = RGB<float>(mat.diffuse[0], mat.diffuse[1], mat.diffuse[2]);
if (!mat.diffuse_texname.empty())
{
diffuse_tex = std::make_shared<U8Image>();
diffuse_tex->load(mtl_base_dir + mat.diffuse_texname);
r->images.push_back(diffuse_tex);
}
if (!mat.normal_texname.empty())
{
normal_tex = std::make_shared<U8Image>();
normal_tex->load(mtl_base_dir + mat.normal_texname);
r->images.push_back(normal_tex);
}
specular = RGB<float>(mat.specular[0], mat.specular[1], mat.specular[2]);
if (!mat.specular_texname.empty())
{
specular_tex = std::make_shared<U8Image>();
specular_tex->load(mtl_base_dir + mat.specular_texname);
r->images.push_back(specular_tex);
}
}
Material::~Material()
{
}
const std::string &Material::get_name() const
{
return material_name;
}
float Material::get_metallic(const glm::vec2 &uv) const
{
if (metallic_tex)
{
return metallic_tex->sample_rgb(uv.x, uv.y, SampleMethod::Repeat).r;
}
return metallic;
}
float Material::get_ior() const
{
return ior;
}
RGB<float> Material::get_emission(const glm::vec2 &uv) const
{
if (emissive_tex)
{
return emissive_tex->sample_rgb(uv.x, uv.y, SampleMethod::Repeat);
}
return emission;
}
RGB<float> Material::get_ambient(const glm::vec2 &uv) const
{
if (ambient_tex)
{
return ambient_tex->sample_rgb(uv.x, uv.y, SampleMethod::Repeat);
}
return ambient;
}
RGB<float> Material::get_diffuse(const glm::vec2 &uv) const
{
if (diffuse_tex)
{
return diffuse_tex->sample_rgb(uv.x, uv.y, SampleMethod::Repeat);
}
return diffuse;
}
RGB<float> Material::get_normal_bump(const glm::vec2 &uv) const
{
if (normal_tex)
{
return normal_tex->sample_rgb(uv.x, uv.y, SampleMethod::Repeat);
}
// Return straight up (no offset)
return RGB<float>(0.0f, 1.0f, 0.0f);
}
RGB<float> Material::get_specular(const glm::vec2 &uv) const
{
if (specular_tex)
{
return specular_tex->sample_rgb(uv.x, uv.y, SampleMethod::Repeat);
}
return specular;
}
int Material::id() const
{
return id_;
}
HitInfo Material::get_hit_info(const glm::vec2 &uv) const
{
// That's a lot of texture samples...
HitInfo info;
info.metallic = get_metallic(uv);
info.ior = ior;
RGB<float> em = get_emission(uv);
info.emission = { em.r, em.g, em.b };
RGB<float> am = get_ambient(uv);
info.ambient = { am.r, am.g, am.b };
RGB<float> dif = get_diffuse(uv);
info.diffuse = { dif.r, dif.g, dif.b };
RGB<float> bump = get_normal_bump(uv);
info.normal_bump = { bump.r, bump.g, bump.b };
RGB<float> spec = get_specular(uv);
info.specular = { spec.r, spec.g, spec.b };
return info;
}