-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaterial.h
357 lines (314 loc) · 10.2 KB
/
Material.h
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//
// Created by rthier on 2016.04.25..
//
#ifndef NFTSIMPLEPROJ_MATERIAL_H
#define NFTSIMPLEPROJ_MATERIAL_H
#include <string>
#include <vector>
#include <bitset>
#include "objmasterlog.h"
#include <unordered_set> /* - Used for test code */
namespace ObjMaster {
/**
* Defines a material. This corresponds to the supported properties of a newmtl in an *.mtl file
*/
class Material {
public:
//
// General attributes
//
/** The name of the material */
std::string name;
// TODO: Change the bitset size when necessary...
/** Defines which fields are used - when creating a material on our own, we must fill this properly! See the various F_* consts for indexing this */
std::bitset<32> enabledFields;
// Constants for indexing the bitset
const static int F_KA = 0;
const static int F_KD = 1;
const static int F_KS = 2;
const static int F_MAP_KA = 3;
const static int F_MAP_KD = 4;
const static int F_MAP_KS = 5;
const static int F_MAP_BUMP = 6;
//
// Color attributes
//
/** Ambient color */
std::vector<float> ka;
/** Diffuse color */
std::vector<float> kd;
/** Specular color */
std::vector<float> ks;
//
// Texture assets
//
/** ambient texture */
std::string map_ka;
/** diffuse texture */
std::string map_kd;
/** specular texture */
std::string map_ks;
/** bump and map_bump */
std::string map_bump;
//
// Convenience methods for setting fields and enabling them automatically
//
// Color attributes convenience methods
inline void setAndEnableKa(std::vector<float> newKa){
ka = newKa;
enabledFields[F_KA] = true;
}
inline void setAndEnableKd(std::vector<float> newKd){
kd = newKd;
enabledFields[F_KD] = true;
}
inline void setAndEnableKs(std::vector<float> newKs){
ks = newKs;
enabledFields[F_KS] = true;
}
// Texture assets convenience methods
inline void setAndEnableMapKa(std::string newMapKa){
map_ka = newMapKa;
enabledFields[F_MAP_KA] = true;
}
inline void setAndEnableMapKd(std::string newMapKd){
map_kd = newMapKd;
enabledFields[F_MAP_KD] = true;
}
inline void setAndEnableMapKs(std::string newMapKs){
map_ks = newMapKs;
enabledFields[F_MAP_KS] = true;
}
inline void setAndEnableMapBump(std::string newMapBump){
map_bump = newMapBump;
enabledFields[F_MAP_BUMP] = true;
}
//
// Construction
//
/**
* Create a material from a list of fields of lines of the *.mtl - basically this is the parser.
* This can be used by going through the file and collecting the descriptor lines in a vector
* until the next material (or the eof) comes.
*
* @param name The name of the material
* @param descriptorLineFields The collected descriptor lines for the material(following newmtl)
*/
Material(std::string materialName, std::vector<std::string> descriptorLineFields);
/** Create an empty material with the given name */
Material(std::string materialName) { name = materialName; }
/** Create an empty material with no name */
Material() {};
// Copies are defeaulted
Material(const Material &other) = default;
Material& operator=(const Material &other) = default;
// Moves are defaulted
Material(Material &&other) = default;
Material& operator=(Material &&other) = default;
/** Clones this material into the other */
inline void cloneInto(Material *other) {
other->ka = ka;
other->kd = kd;
other->ks = ks;
other->map_ka = map_ka;
other->map_kd = map_kd;
other->map_ks = map_ks;
other->map_bump = map_bump;
other->enabledFields = enabledFields;
other->name = name;
}
/** Returns the *.mtl supported text representation as a vector of strings (one per each line) - empty vector is returned for empty material! */
inline std::vector<std::string> asText() const {
// counter for making unique fallback names
static int ncnt = 0;
std::vector<std::string> lines;
bool hasField = false;
// always give a name - except if we are not having any field!
std::string mtlName = name.empty() ? "unknown_" + std::to_string(++ncnt) : name;
lines.push_back("newmtl " + mtlName);
// Various color data
if(enabledFields[Material::F_KA]) {
lines.push_back("Ka " + std::to_string(ka[0]) + " " + std::to_string(ka[1]) + " " + std::to_string(ka[2]));
hasField = true;
}
if(enabledFields[Material::F_KD]) {
lines.push_back("Kd " + std::to_string(kd[0]) + " " + std::to_string(kd[1]) + " " + std::to_string(kd[2]));
hasField = true;
}
if(enabledFields[Material::F_KS]) {
lines.push_back("Ks " + std::to_string(ks[0]) + " " + std::to_string(ks[1]) + " " + std::to_string(ks[2]));
hasField = true;
}
// Various texture data
if(enabledFields[Material::F_MAP_KA]) {
lines.push_back("map_Ka " + map_ka);
hasField = true;
}
if(enabledFields[Material::F_MAP_KD]) {
lines.push_back("map_Kd " + map_kd);
hasField = true;
}
if(enabledFields[Material::F_MAP_KS]) {
lines.push_back("map_Ks " + map_ks);
hasField = true;
}
if(enabledFields[Material::F_MAP_BUMP]) {
lines.push_back("map_bump " + map_bump);
hasField = true;
}
// Check for empty material
if(hasField) {
// Had a meaningful field - return collected data
return lines;
} else {
// Had no meaningful field - return empty vector!
return std::vector<std::string>();
}
}
private:
static std::vector<float> fetchRGBParam(std::string &mtlLine);
static std::string fetchStringParam(std::string &mtlLine);
bool static isPrefixOf(std::string a, std::string b);
bool static isPrefixOf(std::string a, const char* b);
};
/** testing output-related operations (like asText()) */
static int TEST_Material_Output(){
int errorCount = 0;
// The test-case
std::vector<std::string> example {
"newmtl Material_1",
"Ns 1.960784",
"Ka 0.000000 0.000000 0.000000",
"Kd 0.301176 0.301176 0.301176",
"Ks 0.045000 0.045000 0.045000",
"Ni 1.000000",
"d 1.000000",
"illum 2",
"map_Kd witch_hat(color).jpg",
"map_Bump witch_hat(normal).jpg",
"map_Ka witch_hat(color).jpg",
"map_Ks witch_hat(specular).jpg",
};
// The expected result after parsing and outputting a test case
std::unordered_set<std::string> expectedLines {
"newmtl Material_1",
"Ka 0.000000 0.000000 0.000000",
"Kd 0.301176 0.301176 0.301176",
"Ks 0.045000 0.045000 0.045000",
"map_Ka witch_hat(color).jpg",
"map_Kd witch_hat(color).jpg",
"map_Ks witch_hat(specular).jpg",
"map_bump witch_hat(normal).jpg",
};
// Parse
Material m1("Material_1", example);
// Output
std::vector<std::string> output = m1.asText();
// Test1 (parsed)
#ifdef DEBUG
OMLOGI("Parsed mtl material asText returns:");
OMLOGI("-----------------------------------");
#endif
for(auto s : output){
#ifdef DEBUG
OMLOGI("%s", s.c_str());
#endif
auto got = expectedLines.find(s);
if(got == expectedLines.end()){
// Got something unexpected
OMLOGE("Unexpected mtl line in asText() output: %s", s.c_str());
OMLOGI("This asText error might mean that the tests or the code is broken!");
++errorCount; // Indicate error
}
}
// The expected result for second test case
std::unordered_set<std::string> expectedLines2 {
"newmtl Material_2",
"Kd 1.000000 0.200000 0.200000",
"map_Kd my_diff_tex.png",
"map_bump my_bump_tex.png",
};
// Create new
Material m2("Material_2");
// with reddish diffuse color and some simple textures
m2.setAndEnableKd({1.0f, 0.2f, 0.2f});
m2.setAndEnableMapKd("my_diff_tex.png");
m2.setAndEnableMapBump("my_bump_tex.png");
// Output new
output = m2.asText();
// Test2 (new)
#ifdef DEBUG
OMLOGI("Parsed mtl material asText returns:");
OMLOGI("-----------------------------------");
#endif
for(auto s : output){
#ifdef DEBUG
OMLOGI("%s", s.c_str());
#endif
auto got = expectedLines2.find(s);
if(got == expectedLines2.end()){
// Got something unexpected
OMLOGE("Unexpected mtl line in asText() output: %s", s.c_str());
OMLOGI("This asText error might mean that the tests or the code is broken!");
++errorCount; // Indicate error
}
}
return errorCount;
}
/** Test if parsing and loading stuff works */
static bool TEST_Material() {
#ifdef DEBUG
OMLOGI("TEST_Material...");
#endif
Material test1 = Material("test1", std::vector<std::string> {
std::string("Ka 1.0 2.0 4.0"),
std::string("badline"),
std::string("map_Ka ambient.png"),
std::string("badline")
});
// Test "enabledFields"
if(!test1.enabledFields[Material::F_MAP_KA]) {
OMLOGE("TEST_Material: enabledFields[F_MAP_KA] error!");
return false;
}
if(!test1.enabledFields[Material::F_KA]) {
OMLOGE("TEST_Material: enabledFields[F_KA] error!");
return false;
}
if(test1.enabledFields[Material::F_KD]) {
OMLOGE("TEST_Material: enabledFields[F_KD] error!");
return false;
}
if(test1.enabledFields[Material::F_KS]) {
OMLOGE("TEST_Material: enabledFields[F_KS] error!");
return false;
}
if(test1.enabledFields[Material::F_MAP_KD]) {
OMLOGE("TEST_Material: enabledFields[F_MAP_KD] error!");
return false;
}
if(test1.enabledFields[Material::F_MAP_KS]) {
OMLOGE("TEST_Material: enabledFields[F_MAP_KS] error!");
return false;
}
if(test1.enabledFields[Material::F_MAP_BUMP]) {
OMLOGE("TEST_Material: enabledFields[F_MAP_BUMP] error!");
return false;
}
// Test values
if(test1.ka[0] != 1.0f) {
OMLOGE("TEST_Material: Ka error 1");
return false;
}
if(test1.map_ka != "ambient.png") {
OMLOGE("TEST_Material: map_Ka error 1: %s", test1.map_ka.c_str());
return false;
}
#ifdef DEBUG
OMLOGI("...TEST_Material success!");
#endif
// All checks hopefully passed!
return true;
}
}
#endif //NFTSIMPLEPROJ_MATERIAL_H