-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathmayaAsciiReader.cpp
executable file
·387 lines (270 loc) · 8.62 KB
/
mayaAsciiReader.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Copyright(c) 2005-2006. All Rights Reserved
// By Jean-René Bédard (https://github.com/jrbedard/3d-converter)
#include "StdAfx.h"
#include "mayaAsciiReader.h"
using namespace boost::spirit;
CMayaAsciiReader::CMayaAsciiReader(CFile* pFile, const fs::path& fileName)
{
CMayaFile* pMayaFile = static_cast<CMayaFile*>(pFile);
OBJ_ASSERT(pMayaFile);
m_pMayaFile = pMayaFile;
m_fileName = fileName;
}
enum ParseZone
{
eFile,
eMesh,
eTextureCoordinate,
eVertexPosition,
eEdge,
eNormal,
ePolyFaces
};
bool CMayaAsciiReader::Read()
{
OBJ_ASSERT(m_pMayaFile);
if(!m_pMayaFile)
return false;
std::string fileName(m_fileName.string()); // Extract native file path string
int faceID = -1;
int materialID = -1;
int normalIndex = 0;
const int maxline = 1000;
char line[maxline];
int lineNum = 0;
ParseZone parseZone = eFile;
CMayaFile::CMesh curMesh("default");
m_ifs.open(fileName.c_str());
MSG_INFO("Reading Maya .MA file '" << fileName << "'.");
if( !m_ifs.is_open() )
{
MSG_ERROR("Couldn't open .MA file '" << fileName << "'");
return false;
}
while( m_ifs )
{
++lineNum;
m_ifs.getline( line, maxline ); // get the curent line pointer
const char* curPos = line;
std::string command = "";
parse_info<char const*> info;
if( parseZone < eTextureCoordinate || parseZone == ePolyFaces ) // not during numerical parsing
{
info = parse( line, lexeme_d[ *space_p >> +alpha_p[ append(command) ] ] ); // skip spaces
//if( !info.hit ) // if parsing was'nt successful
// continue;
curPos = info.stop;
}
if( command == "createNode" ) // new Material declaration
{
std::string meshName, parentName;
parse_info<char const*> info = parse( curPos,
" mesh -n \"" >> +alnum_p[ append(meshName) ] >> "\" -p \"" >> +alnum_p[ append(parentName) ] >> "\";" );
CMayaFile::CMesh mesh(meshName);
curMesh = mesh;
parseZone = eMesh;
}
else if( command == "setAttr" )
{
int count;
std::string attribute;
// Parse the setAttr command, the -s XXXX is optional
parse_info<char const*> info = parse( curPos,
!(" -s " >> int_p[assign(count)]) >> " \"." >> *alpha_p[ append(attribute) ] >> "[");
curPos = info.stop;
if(attribute == "uvst") // texture coordinates
{
uint beginIndex, endIndex;
uint vectorSize;
parse_info<char const*> infoUV = parse( curPos,
"0].uvsp[" >> int_p[assign(beginIndex)] >> ":" >> int_p[assign(endIndex)] >> "]\" " >>
!("-type \"float" >> int_p[assign(vectorSize)] >> "\"") );
curPos = infoUV.stop;
parseZone = eTextureCoordinate;
}
else if(attribute == "vt") // vertex positions
{
uint beginIndex, endIndex;
parse_info<char const*> infoVT = parse( curPos,
int_p[assign(beginIndex)] >> ":" >> int_p[assign(endIndex)] >> "]\" ");
curPos = infoVT.stop;
parseZone = eVertexPosition;
}
else if(attribute == "ed") // edges
{
uint beginIndex, endIndex;
parse_info<char const*> infoED = parse( curPos,
int_p[assign(beginIndex)] >> ":" >> int_p[assign(endIndex)] >> "]\" ");
curPos = infoED.stop;
parseZone = eEdge;
}
else if(attribute == "n") // normals
{
uint beginIndex, endIndex;
uint vectorSize;
parse_info<char const*> infoN = parse( curPos,
int_p[assign(beginIndex)] >> ":" >> int_p[assign(endIndex)] >> "]\" " >>
!("-type \"float" >> int_p[assign(vectorSize)] >> "\"") );
curPos = infoN.stop;
parseZone = eNormal;
}
else if(attribute == "fc") // polyFaces
{
uint beginIndex, endIndex;
std::string meshType;
parse_info<char const*> infoFC = parse( curPos,
int_p[assign(beginIndex)] >> ":" >> int_p[assign(endIndex)] >> "]\" " >>
!("-type \"" >> +alpha_p[ append(meshType) ] >> "\"") );
if(meshType != "polyFaces")
{
OBJ_ASSERT(false);
MSG_WARNING("Not polyface");
}
normalIndex = 0; // reset the normalIndex for this new mesh
curPos = infoFC.stop;
parseZone = ePolyFaces;
}
}
std::string numricalStr;
// Numerical parsing
if(parseZone == eTextureCoordinate ||
parseZone == eVertexPosition ||
parseZone == eEdge ||
parseZone == eNormal)
{
numricalStr += curPos;
while(!strchr(line, ';')) // concatenate all the numerical coords until the line contains ';'
{
m_ifs.getline( line, maxline );
++lineNum;
numricalStr += line;
}
}
if(parseZone == eTextureCoordinate) // Texture coordinates
{
while(numricalStr.c_str())
{
Vector3D tex(3);
tex[2] = 0.0f; // w defaults to 0
parse_info<char const*> info = parse( numricalStr.c_str(),
real_p[assign(tex[0])] >> real_p[assign(tex[1])],
space_p );
if(!info.hit) // end of texture coordinates
break;
curMesh.GetTextureCoordVector().push_back(tex);
numricalStr = info.stop;
}
parseZone = eMesh;
}
else if(parseZone == eVertexPosition) // Vertex positions
{
while(numricalStr.c_str())
{
Vector3D pos(3);
parse_info<char const*> info = parse( numricalStr.c_str(),
real_p[assign(pos[0])] >> real_p[assign(pos[1])] >> real_p[assign(pos[2])],
space_p );
if(!info.hit) // end of vertex positions
break;
curMesh.GetVertexPositionVector().push_back(pos);
numricalStr = info.stop;
}
parseZone = eMesh;
}
else if(parseZone == eEdge) // Edges
{
while(numricalStr.c_str())
{
Vector3D edge(3);
parse_info<char const*> info = parse( numricalStr.c_str(),
real_p[assign(edge[0])] >> real_p[assign(edge[1])] >> real_p[assign(edge[2])], // the 3rd is always 0 (2 vertex per edge)
space_p );
if(!info.hit) // end of edges
break;
curMesh.GetEdgeVector().push_back(edge); // TODO : should be integers instead of double...
numricalStr = info.stop;
}
parseZone = eMesh;
}
else if(parseZone == eNormal) // Normals
{
while(numricalStr.c_str())
{
Vector3D normal(3);
parse_info<char const*> info = parse( numricalStr.c_str(),
real_p[assign(normal[0])] >> real_p[assign(normal[1])] >> real_p[assign(normal[2])],
space_p );
if(!info.hit) // end of normals
break;
curMesh.GetNormalVector().push_back(normal);
numricalStr = info.stop;
}
parseZone = eMesh;
}
// faces
if(command == "f" && parseZone == ePolyFaces)
{
const char* endLine = line + strlen(line);
uint edgeCount;
CMayaFile::CMesh::CPolyFace polyface;
CMayaFile::CMesh::CPolyFace::CFace face;
parse_info<char const*> info = parse( curPos, int_p[assign(edgeCount)], space_p);
curPos = info.stop;
while( curPos < endLine ) // For each edge index
{
int edgeId;
parse_info<char const*> infoEd = parse( curPos, int_p[assign(edgeId)], space_p);
if( !info.hit )
{
MSG_WARNING("Invalid vertex, " << fileName << ", line:" << lineNum);
}
face.AddEdge(edgeId); // add edge index to the face
if(curMesh.GetNormalVector().size() > 0) // curent mesh contain normals
{
face.AddNormal(normalIndex); // add normal index to the face (1 per polygon vertex)
++normalIndex; // increment index;
}
curPos = infoEd.stop;
if(!strcmp(curPos,";")) // end of PolyFaces
break;
}
faceID++;
polyface.SetFace(face);
curMesh.GetPolyFaceVector().push_back(polyface);
}
// Mus
if(command == "mu" && parseZone == ePolyFaces)
{
const char* endLine = line + strlen(line);
uint uvSet, uvCount;
CMayaFile::CMesh::CPolyFace::CMu mu;
parse_info<char const*> info = parse( curPos, int_p[assign(uvSet)] >> int_p[assign(uvCount)], space_p);
curPos = info.stop;
while( curPos < endLine ) // for each UV coord index
{
int uvId;
parse_info<char const*> infoMu = parse( curPos, int_p[assign(uvId)], space_p);
if( !info.hit )
{
MSG_WARNING("Invalid vertex, " << fileName << ", line:" << lineNum);
}
mu.AddUV(uvId);
curPos = infoMu.stop;
if(!strcmp(curPos,";")) // end of polyfaces
break;
}
curMesh.GetPolyFaceVector().back().SetMu(mu); // add texture coordinates to the current polyface
}
if(parseZone == ePolyFaces)
{
if(!strcmp(curPos,";")) // if we reached the end of polyface
{
parseZone = eMesh;
m_pMayaFile->AddMesh(curMesh);
}
}
// TODO : shading groups / materials / textures
}
m_ifs.close();
return true;
}