-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmaterial.cpp
255 lines (222 loc) · 6.41 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/********************************************************************
created: 2010/04/22
file name: material.cpp
author: maxint [email protected]
*********************************************************************/
#include "material.h"
#include <QImage>
#include <sstream>
namespace RayTracer {
using std::min;
using std::max;
using std::abs;
static const String _DEFAULT_NAME = "_default_";
// ------------------------------------------------------------------------------
// Texture class implementation
// ------------------------------------------------------------------------------
Texture::Texture(const String& aFilename)
: mBitmap(0)
, mWidth(0)
, mHeight(0)
{
QImage img(aFilename.c_str());
if (img.isNull())
{
return;
}
mWidth = img.width();
mHeight = img.height();
mBitmap = new Color[mWidth * mHeight];
Real reci = 1.0f / 256;
for (int x=0; x<mWidth; ++x) for (int y=0; y<mHeight; ++y)
{
QRgb clr = img.pixel(x, y);
mBitmap[x + y*mWidth] = Color(qRed(clr), qGreen(clr), qBlue(clr)) * reci;
}
}
Texture::Texture(RayTracer::Color *data, int w, int h)
: mBitmap(data)
, mWidth(w)
, mHeight(h)
{}
Color Texture::getTexel(Real u, Real v) const
{
u = fmod(u, Real(1.0));
v = fmod(v, Real(1.0));
u = (u < 0.0f) ? (u + 1.0f) : u;
v = (v < 0.0f) ? (v + 1.0f) : v;
v = 1.0f - v;
// fetch a bilinearly filtered texel
Real fu = u * mWidth;
Real fv = v * mHeight;
int u1 = static_cast<int>(fu) % mWidth;
int u2 = static_cast<int>(fu +1) % mWidth;
int v1 = static_cast<int>(fv) % mHeight;
int v2 = static_cast<int>(fv +1) % mHeight;
// calculate fractional parts of u and v
Real fracu = fu - floorf(fu);
Real fracv = fv - floorf(fv);
// calculate weight factors
Real w1 = (1 - fracu) * (1 - fracv);
Real w2 = fracu * (1 - fracv);
Real w3 = (1 - fracu) * fracv;
Real w4 = fracu * fracv;
// fetch four texels
Color c1 = mBitmap[u1 + v1 * mWidth];
Color c2 = mBitmap[u2 + v1 * mWidth];
Color c3 = mBitmap[u1 + v2 * mWidth];
Color c4 = mBitmap[u2 + v2 * mWidth];
// scale and sum the four colors
return c1 * w1 + c2 * w2 + c3 * w3 + c4 * w4;
}
// ------------------------------------------------------------------------------
// Texture Manager class implementation
// ------------------------------------------------------------------------------
TextureManager::TextureManager()
: mIDCounter(0)
{}
TextureManager::~TextureManager()
{
TexListItor it = mTexturePool.begin();
TexListItor it_end = mTexturePool.end();
for (; it!=it_end; ++it)
{
SAFE_DELETE(it->second);
}
mTexturePool.clear();
}
TextureManager& TextureManager::getInstance()
{
static TextureManager mInstance;
return mInstance;
}
Texture* TextureManager::createFromFile(const String& filename, const String& texName /* = */ )
{
if (texName.length() == 0)
{
std::ostringstream oss;
oss << "_Tex" << ++mIDCounter;
Texture *tex = new Texture(filename);
std::cout << "INFO: Texture " << oss.str() << " is created!" << std::endl;
mTexturePool.insert(TexListPair(oss.str(), tex));
return tex;
}
TexListItor it = mTexturePool.find(texName);
if (it != mTexturePool.end())
{ // found
std::cout << "WARNING: Texture " << texName << " has existed!" << std::endl;
return it->second;
}
else
{
Texture *tex = new Texture(filename);
mTexturePool.insert(TexListPair(texName, tex));
return tex;
}
}
Texture* TextureManager::createFromData(Color* data, int w, int h, const String& texName /* = */ )
{
if (texName.length() == 0)
{
std::ostringstream oss;
oss << "_Tex" << ++mIDCounter;
Texture *tex = new Texture(data, w, h);
std::cout << "INFO: Texture " << oss.str() << " is created!" << std::endl;
mTexturePool.insert(TexListPair(oss.str(), tex));
return tex;
}
TexListItor it = mTexturePool.find(texName);
if (it != mTexturePool.end())
{ // found
std::cout << "WARNING: Texture " << texName << " has existed!" << std::endl;
return it->second;
}
else
{
Texture *tex = new Texture(data, w, h);
mTexturePool.insert(TexListPair(texName, tex));
return tex;
}
}
Texture* TextureManager::getTexture(const String& texName)
{
TexListItor it = mTexturePool.find(texName);
if (it != mTexturePool.end())
{ // found
return it->second;
}
else
{
std::cout << "WARNING: Texture " << texName << " did not existed!" << std::endl;
return NULL;
}
}
// ------------------------------------------------------------------------------
// Material class implementation
// ------------------------------------------------------------------------------
void Material::setTexture(const String& texName)
{
mTexture = TextureManager::getInstance().getTexture(texName);
}
// ------------------------------------------------------------------------------
// Material Manager class implementation
// ------------------------------------------------------------------------------
MaterialManager::MaterialManager()
: mIDCounter(0)
{
Material *mat = new Material();
mMaterialPool.insert(std::make_pair(_DEFAULT_NAME, mat));
}
MaterialManager::~MaterialManager()
{
MatMapItor it = mMaterialPool.begin();
MatMapItor it_end = mMaterialPool.end();
for (; it!=it_end; ++it)
{
SAFE_DELETE(it->second);
}
mMaterialPool.clear();
}
MaterialManager& MaterialManager::getInstance()
{
static MaterialManager mInstance;
return mInstance;
}
Material* MaterialManager::createManual(const String& matName /* = "" */)
{
if (matName.length() == 0)
{
std::ostringstream oss;
oss << "_Mat" << ++mIDCounter;
Material* mat = new Material();
std::cout << "INFO: Material " << oss.str() << " is created!" << std::endl;
mMaterialPool.insert(std::make_pair(oss.str(), mat));
return mat;
}
MatMapItor it = mMaterialPool.find(matName);
if (it!=mMaterialPool.end())
{// found
std::cout << "WARNING: Material " << matName << " has existed!" << std::endl;
return it->second;
}
else
{
Material* mat = new Material();
mMaterialPool.insert(std::make_pair(matName, mat));
return mat;
}
}
Material* MaterialManager::getMaterial(const String& matName)
{
MatMapItor it = mMaterialPool.find(matName);
if (it!=mMaterialPool.end())
{
return it->second;
}
else
{
std::cout << "WARNING: Material " << matName << " did not existed!" << std::endl;
return mMaterialPool[_DEFAULT_NAME];
}
}
}; // namespace RayTracer