22{
23 if (*line == 0)
24 return true;
26
27
29
30 char c1 = *line ++;
31 switch (c1) {
32 case '#':
33
34 break;
35 case 'v':
36 {
37
38 char c2 = *line ++;
39 switch (c2) {
40 case 't':
41 {
42
43
44 char c2 = *line ++;
45 if (c2 != ' ' && c2 != '\t')
46 return false;
48 char *endptr = 0;
49 double u = strtod(line, &endptr);
50 if (endptr == 0 || (*endptr != ' ' && *endptr != '\t'))
51 return false;
52 line = endptr;
54 double v = 0;
55 if (*line != 0) {
56 v = strtod(line, &endptr);
57 if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
58 return false;
59 line = endptr;
61 }
62
63 if (*line != 0) {
64 strtod(line, &endptr);
65 if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
66 return false;
67 line = endptr;
69 }
70 if (*line != 0)
71 return false;
74 break;
75 }
76 case 'n':
77 {
78
79
80 char c2 = *line ++;
81 if (c2 != ' ' && c2 != '\t')
82 return false;
84 char *endptr = 0;
85 double x = strtod(line, &endptr);
86 if (endptr == 0 || (*endptr != ' ' && *endptr != '\t'))
87 return false;
88 line = endptr;
90 double y = strtod(line, &endptr);
91 if (endptr == 0 || (*endptr != ' ' && *endptr != '\t'))
92 return false;
93 line = endptr;
95 double z = strtod(line, &endptr);
96 if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
97 return false;
98 line = endptr;
100 if (*line != 0)
101 return false;
102 data.
normals.push_back((
float)x);
103 data.
normals.push_back((
float)y);
104 data.
normals.push_back((
float)z);
105 break;
106 }
107 case 'p':
108 {
109
110 char c2 = *line ++;
111 if (c2 != ' ' && c2 != '\t')
112 return false;
114 char *endptr = 0;
115 double u = strtod(line, &endptr);
116 if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
117 return false;
118 line = endptr;
120 double v = strtod(line, &endptr);
121 if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
122 return false;
123 line = endptr;
125 double w = 0;
126 if (*line != 0) {
127 w = strtod(line, &endptr);
128 if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
129 return false;
130 line = endptr;
132 }
133 if (*line != 0)
134 return false;
138 break;
139 }
140 default:
141 {
142
143 if (c2 != ' ' && c2 != '\t')
144 return false;
146 char *endptr = 0;
147 double x = strtod(line, &endptr);
148 if (endptr == 0 || (*endptr != ' ' && *endptr != '\t'))
149 return false;
150 line = endptr;
152 double y = strtod(line, &endptr);
153 if (endptr == 0 || (*endptr != ' ' && *endptr != '\t'))
154 return false;
155 line = endptr;
157 double z = strtod(line, &endptr);
158 if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
159 return false;
160 line = endptr;
162 double color_x = 0.0, color_y = 0.0, color_z = 0.0, color_w = 0.0;
163 if (*line != 0) {
166 }
167 color_x = strtod(line, &endptr);
168 if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
169 return false;
170 line = endptr;
172 color_y = strtod(line, &endptr);
173 if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
174 return false;
175 line = endptr;
177 color_z = strtod(line, &endptr);
178 if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
179 return false;
180 line = endptr;
182 color_w = 1.0;
183 if (*line != 0) {
184 color_w = strtod(line, &endptr);
185 if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0)) return false;
186 line = endptr;
188 }
189 }
190
191
192
193
194
202 break;
203 }
204 }
205 break;
206 }
207 case 'f':
208 {
209
211 if (*line == 0)
212 return false;
213
214
216 char *endptr = 0;
217 while (*line != 0) {
218
219 vertex.coordIdx = 0;
220 vertex.normalIdx = 0;
221 vertex.textureCoordIdx = 0;
222 vertex.coordIdx = strtol(line, &endptr, 10);
223
224 if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != '/' && *endptr != 0))
225 return false;
226 line = endptr;
227 if (*line == '/') {
228 ++ line;
229
230 if (*line != '/') {
231
232 vertex.textureCoordIdx = strtol(line, &endptr, 10);
233 if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != '/' && *endptr != 0))
234 return false;
235 line = endptr;
236 }
237 if (*line == '/') {
238
239 ++ line;
240 vertex.normalIdx = strtol(line, &endptr, 10);
241 if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
242 return false;
243 line = endptr;
244 }
245 }
246 if (vertex.coordIdx < 0)
248 else
249 -- vertex.coordIdx;
250 if (vertex.normalIdx < 0)
251 vertex.normalIdx += (int)data.
normals.size() / 3;
252 else
253 -- vertex.normalIdx;
254 if (vertex.textureCoordIdx < 0)
256 else
257 -- vertex.textureCoordIdx;
260 }
263 }
265 int face_index_count = 0;
266 for (
int i = data.
vertices.size() - 1; i >= 0; i--) {
267 if (data.
vertices[i].coordIdx == -1) {
268 break;
269 }
270 face_index_count++;
271 }
272 if (face_index_count >= 3) {
273 data.
usemtls.back().face_end += face_index_count - 2;
274 }
275 }
277 vertex.normalIdx = -1;
278 vertex.textureCoordIdx = -1;
280 break;
281 }
282 case 'm':
283 {
284 if (*(line ++) != 't' ||
285 *(line ++) != 'l' ||
286 *(line ++) != 'l' ||
287 *(line ++) != 'i' ||
288 *(line ++) != 'b')
289 return false;
290
291
293 data.
mtllibs.push_back(std::string(line));
294 break;
295 }
296 case 'u':
297 {
298 if (*(line ++) != 's' ||
299 *(line ++) != 'e' ||
300 *(line ++) != 'm' ||
301 *(line ++) != 't' ||
302 *(line ++) != 'l')
303 return false;
304
305
309 }
312 usemtl.name = line;
313 data.
usemtls.push_back(usemtl);
314 if (data.
usemtls.size() == 1) {
315 data.
usemtls.back().face_start = 0;
316 }
317 else {
321 last_usemtl.face_start = last_last_usemtl.face_end + 1;
322 }
323 data.
usemtls.back().face_end = data.
usemtls.back().face_start - 1;
324 break;
325 }
326 case 'o':
327 {
328
330 while (*line != ' ' && *line != '\t' && *line != 0)
331 ++ line;
332
334 if (*line != 0)
335 return false;
338 object.name = line;
339 data.
objects.push_back(
object);
340 break;
341 }
342 case 'g':
343 {
344
345
348 group.name = line;
349 data.
groups.push_back(group);
350 break;
351 }
352 case 's':
353 {
354
355 char c2 = *line ++;
356 if (c2 != ' ' && c2 != '\t')
357 return false;
359 char *endptr = 0;
360 long g = strtol(line, &endptr, 10);
361 if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
362 return false;
363 line = endptr;
365 if (*line != 0)
366 return false;
369 group.smoothingGroupID = g;
371 break;
372 }
373 default:
374 BOOST_LOG_TRIVIAL(error) << "ObjParser: Unknown command: " << c1;
375 break;
376 }
377
378 return true;
379}
std::vector< float > normals
Definition objparser.hpp:108
std::vector< ObjGroup > groups
Definition objparser.hpp:115
std::vector< ObjVertex > vertices
Definition objparser.hpp:119
int vertexIdxFirst
Definition objparser.hpp:29
int coordIdx
Definition objparser.hpp:15
std::vector< float > parameters
Definition objparser.hpp:110
std::vector< ObjUseMtl > usemtls
Definition objparser.hpp:113
std::vector< float > coordinates
Definition objparser.hpp:103
std::vector< std::string > mtllibs
Definition objparser.hpp:112
int vertexIdxFirst
Definition objparser.hpp:60
std::vector< float > textureCoordinates
Definition objparser.hpp:106
bool has_vertex_color
Definition objparser.hpp:104
std::vector< ObjObject > objects
Definition objparser.hpp:114
int vertexIdxFirst
Definition objparser.hpp:73
int vertexIdxFirst
Definition objparser.hpp:85
std::vector< ObjSmoothingGroup > smoothingGroups
Definition objparser.hpp:116
Definition objparser.hpp:28
Definition objparser.hpp:84
Definition objparser.hpp:72
Definition objparser.hpp:59
Definition objparser.hpp:14
#define OBJ_TEXCOORD_LENGTH
Definition objparser.hpp:96
#define OBJ_VERTEX_LENGTH
Definition objparser.hpp:95