-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathasset-mtl.cpp
327 lines (276 loc) · 8.64 KB
/
asset-mtl.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
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
#include "framework.h"
#include "asset-internal.h"
namespace Framework
{
// Infrastructure for compiling Wavefront .mtl material libraries.
namespace OBJMtlLibCompiler
{
static const char * s_suffixMtlLib = "/material_lib";
struct Material
{
std::string m_mtlName;
std::string m_texDiffuseColor;
std::string m_texSpecColor;
std::string m_texHeight;
rgb m_rgbDiffuseColor;
rgb m_rgbSpecColor;
float m_specPower;
float m_bumpScale;
};
struct Context
{
std::vector<Material> m_mtls;
};
// Prototype various helper functions
bool ParseMTL(const char * path, Context * pCtxOut);
void SerializeMtlLib(Context * pCtx, std::vector<byte> * pDataOut);
}
// Compiler entry point
bool CompileOBJMtlLibAsset(
const AssetCompileInfo * pACI,
mz_zip_archive * pZipOut)
{
ASSERT_ERR(pACI);
ASSERT_ERR(pACI->m_pathSrc);
ASSERT_ERR(pACI->m_ack == ACK_OBJMtlLib);
ASSERT_ERR(pZipOut);
using namespace AssetCompiler;
using namespace OBJMtlLibCompiler;
// Read the material definitions from the MTL file
Context ctx = {};
if (!ParseMTL(pACI->m_pathSrc, &ctx))
return false;
// Write the data out to the archive
std::vector<byte> serializedMtlLib;
SerializeMtlLib(&ctx, &serializedMtlLib);
return WriteAssetDataToZip(pACI->m_pathSrc, s_suffixMtlLib, &serializedMtlLib[0], serializedMtlLib.size(), pZipOut);
}
namespace OBJMtlLibCompiler
{
bool ParseMTL(const char * path, Context * pCtxOut)
{
ASSERT_ERR(path);
ASSERT_ERR(pCtxOut);
// Read the whole file into memory
std::vector<byte> data;
if (!LoadFile(path, &data, LFK_Text))
return false;
Material * pMtlCur = nullptr;
// Set up some sane defaults for materials that don't specify all parameters
Material mtlDefault =
{
std::string(), // m_mtlName
std::string(), // m_texDiffuseColor
std::string(), // m_texSpecColor
std::string(), // m_texHeight
{ 1.0f, 1.0f, 1.0f, }, // m_rgbDiffuseColor
{ 0.0f, 0.0f, 0.0f, }, // m_rgbSpecColor
0.0f, // m_specPower
1.0f, // m_bumpScale
};
// Parse line-by-line
TextParsingHelper tph((char *)&data[0], path);
while (tph.NextLine())
{
char * pToken = tph.NextToken();
if (_stricmp(pToken, "newmtl") == 0)
{
pCtxOut->m_mtls.push_back(mtlDefault);
pMtlCur = &pCtxOut->m_mtls.back();
pMtlCur->m_mtlName = tph.ExpectOneToken("material name");
tph.ExpectEOL();
makeLowercase(pMtlCur->m_mtlName);
}
else if (_stricmp(pToken, "map_Kd") == 0)
{
if (!pMtlCur)
{
WARN("%s: syntax error at line %d: material parameters specified before any \"newmtl\" command; ignoring",
path, tph.m_iLine);
continue;
}
pMtlCur->m_texDiffuseColor = tph.ExpectOneToken("texture name");
tph.ExpectEOL();
makeLowercase(pMtlCur->m_texDiffuseColor);
replaceChars(pMtlCur->m_texDiffuseColor, '\\', '/');
}
else if (_stricmp(pToken, "map_Ks") == 0)
{
if (!pMtlCur)
{
WARN("%s: syntax error at line %d: material parameters specified before any \"newmtl\" command; ignoring",
path, tph.m_iLine);
continue;
}
pMtlCur->m_texSpecColor = tph.ExpectOneToken("texture name");
tph.ExpectEOL();
makeLowercase(pMtlCur->m_texSpecColor);
replaceChars(pMtlCur->m_texSpecColor, '\\', '/');
}
else if (_stricmp(pToken, "map_bump") == 0 ||
_stricmp(pToken, "bump") == 0)
{
if (!pMtlCur)
{
WARN("%s: syntax error at line %d: material parameters specified before any \"newmtl\" command; ignoring",
path, tph.m_iLine);
continue;
}
pToken = tph.ExpectOneToken("texture name or options");
if (_stricmp(pToken, "-bm") == 0)
{
// Parse bump scale
float bumpScale = float(atof(tph.ExpectOneToken("bump scale")));
if (bumpScale < 0.0f)
{
WARN("%s: bump scale at line %d is less than 0; clamping", path, tph.m_iLine);
bumpScale = 0.0f;
}
pMtlCur->m_bumpScale = bumpScale;
pToken = tph.ExpectOneToken("texture name or options");
}
pMtlCur->m_texHeight = pToken;
tph.ExpectEOL();
makeLowercase(pMtlCur->m_texHeight);
replaceChars(pMtlCur->m_texHeight, '\\', '/');
}
else if (_stricmp(pToken, "Kd") == 0)
{
char * tokens[3] = {};
tph.ExpectTokens(tokens, dim(tokens), "RGB color");
tph.ExpectEOL();
srgb color = { float(atof(tokens[0])), float(atof(tokens[1])), float(atof(tokens[2])) };
if (any(color < 0.0f) || any(color > 1.0f))
{
WARN("%s: RGB color at line %d is outside [0, 1]; clamping", path, tph.m_iLine);
color = saturate(color);
}
pMtlCur->m_rgbDiffuseColor = SRGBtoLinear(color);
}
else if (_stricmp(pToken, "Ks") == 0)
{
char * tokens[3] = {};
tph.ExpectTokens(tokens, dim(tokens), "RGB color");
tph.ExpectEOL();
srgb color = { float(atof(tokens[0])), float(atof(tokens[1])), float(atof(tokens[2])) };
if (any(color < 0.0f) || any(color > 1.0f))
{
WARN("%s: RGB color at line %d is outside [0, 1]; clamping", path, tph.m_iLine);
color = saturate(color);
}
pMtlCur->m_rgbSpecColor = SRGBtoLinear(color);
}
else if (_stricmp(pToken, "Ns") == 0)
{
float n = float(atof(tph.ExpectOneToken("specular power")));
tph.ExpectEOL();
if (n < 0.0f)
{
WARN("%s: specular power at line %d is below zero; clamping", path, tph.m_iLine);
n = 0.0f;
}
pMtlCur->m_specPower = n;
}
else
{
// Unknown command; just ignore
}
}
return true;
}
void SerializeMtlLib(Context * pCtx, std::vector<byte> * pDataOut)
{
ASSERT_ERR(pCtx);
ASSERT_ERR(pDataOut);
SerializeHelper sh(pDataOut);
for (int i = 0, cMtl = int(pCtx->m_mtls.size()); i < cMtl; ++i)
{
const Material * pMtl = &pCtx->m_mtls[i];
sh.WriteString(pMtl->m_mtlName);
sh.WriteString(pMtl->m_texDiffuseColor);
sh.WriteString(pMtl->m_texSpecColor);
sh.WriteString(pMtl->m_texHeight);
sh.Write(pMtl->m_rgbDiffuseColor);
sh.Write(pMtl->m_rgbSpecColor);
sh.Write(pMtl->m_specPower);
sh.Write(pMtl->m_bumpScale);
}
}
}
// Load compiled data into a runtime game object
bool LoadMaterialLibFromAssetPack(
AssetPack * pPack,
const char * path,
TextureLib * pTexLib,
MaterialLib * pMtlLibOut)
{
ASSERT_ERR(pPack);
ASSERT_ERR(path);
ASSERT_ERR(pMtlLibOut);
using namespace OBJMtlLibCompiler;
pMtlLibOut->m_pPack = pPack;
// Look for the data in the asset pack
byte * pData;
int dataSize;
if (!pPack->LookupFile(path, s_suffixMtlLib, (void **)&pData, &dataSize))
{
WARN("Couldn't find data for material lib %s in asset pack %s", path, pPack->m_path.c_str());
return false;
}
// Use the MTL's path within the zip as the base for looking up relative paths of textures
std::string dirBase = findDirectory(path);
// Deserialize it
DeserializeHelper dh(pData, dataSize);
while (!dh.AtEOF())
{
Framework::Material mtl = {};
// Read data
const char * texDiffuseColorName;
const char * texSpecColorName;
const char * texHeightName;
if (!dh.ReadString(&mtl.m_mtlName) ||
!dh.ReadString(&texDiffuseColorName) ||
!dh.ReadString(&texSpecColorName) ||
!dh.ReadString(&texHeightName) ||
!dh.Read(&mtl.m_rgbDiffuseColor) ||
!dh.Read(&mtl.m_rgbSpecColor) ||
!dh.Read(&mtl.m_specPower) ||
!dh.Read(&mtl.m_bumpScale))
{
return false;
}
// Validate data
if (any(mtl.m_rgbDiffuseColor < 0.0f) || any(mtl.m_rgbDiffuseColor > 1.0f) ||
any(mtl.m_rgbSpecColor < 0.0f) || any(mtl.m_rgbSpecColor > 1.0f) ||
mtl.m_specPower < 0.0f || mtl.m_bumpScale < 0.0f)
{
WARN("Corrupt material lib: numeric parameter out of range");
return false;
}
// Look up textures by name
if (pTexLib)
{
if (*texDiffuseColorName)
{
mtl.m_pTexDiffuseColor = pTexLib->Lookup(dirBase + texDiffuseColorName);
ASSERT_WARN_MSG(mtl.m_pTexDiffuseColor,
"Material %s: couldn't find texture %s in texture library", mtl.m_mtlName, texDiffuseColorName);
}
if (*texSpecColorName)
{
mtl.m_pTexSpecColor = pTexLib->Lookup(dirBase + texSpecColorName);
ASSERT_WARN_MSG(mtl.m_pTexSpecColor,
"Material %s: couldn't find texture %s in texture library", mtl.m_mtlName, texSpecColorName);
}
if (*texHeightName)
{
mtl.m_pTexHeight = pTexLib->Lookup(dirBase + texHeightName);
ASSERT_WARN_MSG(mtl.m_pTexHeight,
"Material %s: couldn't find texture %s in texture library", mtl.m_mtlName, texHeightName);
}
}
pMtlLibOut->m_mtls.insert(std::make_pair(std::string(mtl.m_mtlName), mtl));
}
return true;
}
}