diff options
author | David Walter Seikel | 2016-03-28 22:28:34 +1000 |
---|---|---|
committer | David Walter Seikel | 2016-03-28 22:28:34 +1000 |
commit | 7028cbe09c688437910a25623098762bf0fa592d (patch) | |
tree | 10b5af58277d9880380c2251f109325542c4e6eb /src/others/irrlicht-1.8.1/source/Irrlicht/CPLYMeshFileLoader.cpp | |
parent | Move lemon to the src/others directory. (diff) | |
download | SledjHamr-7028cbe09c688437910a25623098762bf0fa592d.zip SledjHamr-7028cbe09c688437910a25623098762bf0fa592d.tar.gz SledjHamr-7028cbe09c688437910a25623098762bf0fa592d.tar.bz2 SledjHamr-7028cbe09c688437910a25623098762bf0fa592d.tar.xz |
Move Irrlicht to src/others.
Diffstat (limited to 'src/others/irrlicht-1.8.1/source/Irrlicht/CPLYMeshFileLoader.cpp')
-rw-r--r-- | src/others/irrlicht-1.8.1/source/Irrlicht/CPLYMeshFileLoader.cpp | 817 |
1 files changed, 817 insertions, 0 deletions
diff --git a/src/others/irrlicht-1.8.1/source/Irrlicht/CPLYMeshFileLoader.cpp b/src/others/irrlicht-1.8.1/source/Irrlicht/CPLYMeshFileLoader.cpp new file mode 100644 index 0000000..1ef4946 --- /dev/null +++ b/src/others/irrlicht-1.8.1/source/Irrlicht/CPLYMeshFileLoader.cpp | |||
@@ -0,0 +1,817 @@ | |||
1 | // Copyright (C) 2009-2012 Gaz Davidson | ||
2 | // This file is part of the "Irrlicht Engine". | ||
3 | // For conditions of distribution and use, see copyright notice in irrlicht.h | ||
4 | |||
5 | #include "IrrCompileConfig.h" | ||
6 | #ifdef _IRR_COMPILE_WITH_PLY_LOADER_ | ||
7 | |||
8 | #include "CPLYMeshFileLoader.h" | ||
9 | #include "IMeshManipulator.h" | ||
10 | #include "SMesh.h" | ||
11 | #include "CDynamicMeshBuffer.h" | ||
12 | #include "SAnimatedMesh.h" | ||
13 | #include "IReadFile.h" | ||
14 | #include "fast_atof.h" | ||
15 | #include "os.h" | ||
16 | |||
17 | namespace irr | ||
18 | { | ||
19 | namespace scene | ||
20 | { | ||
21 | |||
22 | // input buffer must be at least twice as long as the longest line in the file | ||
23 | #define PLY_INPUT_BUFFER_SIZE 51200 // file is loaded in 50k chunks | ||
24 | |||
25 | // constructor | ||
26 | CPLYMeshFileLoader::CPLYMeshFileLoader(scene::ISceneManager* smgr) | ||
27 | : SceneManager(smgr), File(0), Buffer(0) | ||
28 | { | ||
29 | } | ||
30 | |||
31 | |||
32 | CPLYMeshFileLoader::~CPLYMeshFileLoader() | ||
33 | { | ||
34 | // delete the buffer in case we didn't earlier | ||
35 | // (we do, but this could be disabled to increase the speed of loading hundreds of meshes) | ||
36 | if (Buffer) | ||
37 | { | ||
38 | delete [] Buffer; | ||
39 | Buffer = 0; | ||
40 | } | ||
41 | |||
42 | // Destroy the element list if it exists | ||
43 | for (u32 i=0; i<ElementList.size(); ++i) | ||
44 | delete ElementList[i]; | ||
45 | ElementList.clear(); | ||
46 | } | ||
47 | |||
48 | |||
49 | //! returns true if the file maybe is able to be loaded by this class | ||
50 | bool CPLYMeshFileLoader::isALoadableFileExtension(const io::path& filename) const | ||
51 | { | ||
52 | return core::hasFileExtension(filename, "ply"); | ||
53 | } | ||
54 | |||
55 | |||
56 | //! creates/loads an animated mesh from the file. | ||
57 | IAnimatedMesh* CPLYMeshFileLoader::createMesh(io::IReadFile* file) | ||
58 | { | ||
59 | if (!file) | ||
60 | return 0; | ||
61 | |||
62 | File = file; | ||
63 | File->grab(); | ||
64 | |||
65 | // attempt to allocate the buffer and fill with data | ||
66 | if (!allocateBuffer()) | ||
67 | { | ||
68 | File->drop(); | ||
69 | File = 0; | ||
70 | return 0; | ||
71 | } | ||
72 | |||
73 | // start with empty mesh | ||
74 | SAnimatedMesh* animMesh = 0; | ||
75 | u32 vertCount=0; | ||
76 | |||
77 | // Currently only supports ASCII meshes | ||
78 | if (strcmp(getNextLine(), "ply")) | ||
79 | { | ||
80 | os::Printer::log("Not a valid PLY file", file->getFileName().c_str(), ELL_ERROR); | ||
81 | } | ||
82 | else | ||
83 | { | ||
84 | // cut the next line out | ||
85 | getNextLine(); | ||
86 | // grab the word from this line | ||
87 | c8 *word = getNextWord(); | ||
88 | |||
89 | // ignore comments | ||
90 | while (strcmp(word, "comment") == 0) | ||
91 | { | ||
92 | getNextLine(); | ||
93 | word = getNextWord(); | ||
94 | } | ||
95 | |||
96 | bool readingHeader = true; | ||
97 | bool continueReading = true; | ||
98 | IsBinaryFile = false; | ||
99 | IsWrongEndian= false; | ||
100 | |||
101 | do | ||
102 | { | ||
103 | if (strcmp(word, "format") == 0) | ||
104 | { | ||
105 | word = getNextWord(); | ||
106 | |||
107 | if (strcmp(word, "binary_little_endian") == 0) | ||
108 | { | ||
109 | IsBinaryFile = true; | ||
110 | #ifdef __BIG_ENDIAN__ | ||
111 | IsWrongEndian = true; | ||
112 | #endif | ||
113 | |||
114 | } | ||
115 | else if (strcmp(word, "binary_big_endian") == 0) | ||
116 | { | ||
117 | IsBinaryFile = true; | ||
118 | #ifndef __BIG_ENDIAN__ | ||
119 | IsWrongEndian = true; | ||
120 | #endif | ||
121 | } | ||
122 | else if (strcmp(word, "ascii")) | ||
123 | { | ||
124 | // abort if this isn't an ascii or a binary mesh | ||
125 | os::Printer::log("Unsupported PLY mesh format", word, ELL_ERROR); | ||
126 | continueReading = false; | ||
127 | } | ||
128 | |||
129 | if (continueReading) | ||
130 | { | ||
131 | word = getNextWord(); | ||
132 | if (strcmp(word, "1.0")) | ||
133 | { | ||
134 | os::Printer::log("Unsupported PLY mesh version", word, ELL_WARNING); | ||
135 | } | ||
136 | } | ||
137 | } | ||
138 | else if (strcmp(word, "property") == 0) | ||
139 | { | ||
140 | word = getNextWord(); | ||
141 | |||
142 | if (!ElementList.size()) | ||
143 | { | ||
144 | os::Printer::log("PLY property found before element", word, ELL_WARNING); | ||
145 | } | ||
146 | else | ||
147 | { | ||
148 | // get element | ||
149 | SPLYElement* el = ElementList[ElementList.size()-1]; | ||
150 | |||
151 | // fill property struct | ||
152 | SPLYProperty prop; | ||
153 | prop.Type = getPropertyType(word); | ||
154 | el->KnownSize += prop.size(); | ||
155 | |||
156 | if (prop.Type == EPLYPT_LIST) | ||
157 | { | ||
158 | el->IsFixedWidth = false; | ||
159 | |||
160 | word = getNextWord(); | ||
161 | |||
162 | prop.Data.List.CountType = getPropertyType(word); | ||
163 | if (IsBinaryFile && prop.Data.List.CountType == EPLYPT_UNKNOWN) | ||
164 | { | ||
165 | os::Printer::log("Cannot read binary PLY file containing data types of unknown length", word, ELL_ERROR); | ||
166 | continueReading = false; | ||
167 | } | ||
168 | else | ||
169 | { | ||
170 | word = getNextWord(); | ||
171 | prop.Data.List.ItemType = getPropertyType(word); | ||
172 | if (IsBinaryFile && prop.Data.List.ItemType == EPLYPT_UNKNOWN) | ||
173 | { | ||
174 | os::Printer::log("Cannot read binary PLY file containing data types of unknown length", word, ELL_ERROR); | ||
175 | continueReading = false; | ||
176 | } | ||
177 | } | ||
178 | } | ||
179 | else if (IsBinaryFile && prop.Type == EPLYPT_UNKNOWN) | ||
180 | { | ||
181 | os::Printer::log("Cannot read binary PLY file containing data types of unknown length", word, ELL_ERROR); | ||
182 | continueReading = false; | ||
183 | } | ||
184 | |||
185 | prop.Name = getNextWord(); | ||
186 | |||
187 | // add property to element | ||
188 | el->Properties.push_back(prop); | ||
189 | } | ||
190 | } | ||
191 | else if (strcmp(word, "element") == 0) | ||
192 | { | ||
193 | SPLYElement* el = new SPLYElement; | ||
194 | el->Name = getNextWord(); | ||
195 | el->Count = atoi(getNextWord()); | ||
196 | el->IsFixedWidth = true; | ||
197 | el->KnownSize = 0; | ||
198 | ElementList.push_back(el); | ||
199 | |||
200 | if (el->Name == "vertex") | ||
201 | vertCount = el->Count; | ||
202 | |||
203 | } | ||
204 | else if (strcmp(word, "end_header") == 0) | ||
205 | { | ||
206 | readingHeader = false; | ||
207 | if (IsBinaryFile) | ||
208 | { | ||
209 | StartPointer = LineEndPointer + 1; | ||
210 | } | ||
211 | } | ||
212 | else if (strcmp(word, "comment") == 0) | ||
213 | { | ||
214 | // ignore line | ||
215 | } | ||
216 | else | ||
217 | { | ||
218 | os::Printer::log("Unknown item in PLY file", word, ELL_WARNING); | ||
219 | } | ||
220 | |||
221 | if (readingHeader && continueReading) | ||
222 | { | ||
223 | getNextLine(); | ||
224 | word = getNextWord(); | ||
225 | } | ||
226 | } | ||
227 | while (readingHeader && continueReading); | ||
228 | |||
229 | // now to read the actual data from the file | ||
230 | if (continueReading) | ||
231 | { | ||
232 | // create a mesh buffer | ||
233 | CDynamicMeshBuffer *mb = new CDynamicMeshBuffer(video::EVT_STANDARD, vertCount > 65565 ? video::EIT_32BIT : video::EIT_16BIT); | ||
234 | mb->getVertexBuffer().reallocate(vertCount); | ||
235 | mb->getIndexBuffer().reallocate(vertCount); | ||
236 | mb->setHardwareMappingHint(EHM_STATIC); | ||
237 | |||
238 | bool hasNormals=true; | ||
239 | // loop through each of the elements | ||
240 | for (u32 i=0; i<ElementList.size(); ++i) | ||
241 | { | ||
242 | // do we want this element type? | ||
243 | if (ElementList[i]->Name == "vertex") | ||
244 | { | ||
245 | // loop through vertex properties | ||
246 | for (u32 j=0; j < ElementList[i]->Count; ++j) | ||
247 | hasNormals &= readVertex(*ElementList[i], mb); | ||
248 | } | ||
249 | else if (ElementList[i]->Name == "face") | ||
250 | { | ||
251 | // read faces | ||
252 | for (u32 j=0; j < ElementList[i]->Count; ++j) | ||
253 | readFace(*ElementList[i], mb); | ||
254 | } | ||
255 | else | ||
256 | { | ||
257 | // skip these elements | ||
258 | for (u32 j=0; j < ElementList[i]->Count; ++j) | ||
259 | skipElement(*ElementList[i]); | ||
260 | } | ||
261 | } | ||
262 | mb->recalculateBoundingBox(); | ||
263 | if (!hasNormals) | ||
264 | SceneManager->getMeshManipulator()->recalculateNormals(mb); | ||
265 | SMesh* m = new SMesh(); | ||
266 | m->addMeshBuffer(mb); | ||
267 | m->recalculateBoundingBox(); | ||
268 | mb->drop(); | ||
269 | animMesh = new SAnimatedMesh(); | ||
270 | animMesh->addMesh(m); | ||
271 | animMesh->recalculateBoundingBox(); | ||
272 | m->drop(); | ||
273 | } | ||
274 | } | ||
275 | |||
276 | |||
277 | // free the buffer | ||
278 | delete [] Buffer; | ||
279 | Buffer = 0; | ||
280 | File->drop(); | ||
281 | File = 0; | ||
282 | |||
283 | // if we managed to create a mesh, return it | ||
284 | return animMesh; | ||
285 | } | ||
286 | |||
287 | |||
288 | bool CPLYMeshFileLoader::readVertex(const SPLYElement &Element, scene::CDynamicMeshBuffer* mb) | ||
289 | { | ||
290 | if (!IsBinaryFile) | ||
291 | getNextLine(); | ||
292 | |||
293 | video::S3DVertex vert; | ||
294 | vert.Color.set(255,255,255,255); | ||
295 | vert.TCoords.X = 0.0f; | ||
296 | vert.TCoords.Y = 0.0f; | ||
297 | vert.Normal.X = 0.0f; | ||
298 | vert.Normal.Y = 1.0f; | ||
299 | vert.Normal.Z = 0.0f; | ||
300 | |||
301 | bool result=false; | ||
302 | for (u32 i=0; i < Element.Properties.size(); ++i) | ||
303 | { | ||
304 | E_PLY_PROPERTY_TYPE t = Element.Properties[i].Type; | ||
305 | |||
306 | if (Element.Properties[i].Name == "x") | ||
307 | vert.Pos.X = getFloat(t); | ||
308 | else if (Element.Properties[i].Name == "y") | ||
309 | vert.Pos.Z = getFloat(t); | ||
310 | else if (Element.Properties[i].Name == "z") | ||
311 | vert.Pos.Y = getFloat(t); | ||
312 | else if (Element.Properties[i].Name == "nx") | ||
313 | { | ||
314 | vert.Normal.X = getFloat(t); | ||
315 | result=true; | ||
316 | } | ||
317 | else if (Element.Properties[i].Name == "ny") | ||
318 | { | ||
319 | vert.Normal.Z = getFloat(t); | ||
320 | result=true; | ||
321 | } | ||
322 | else if (Element.Properties[i].Name == "nz") | ||
323 | { | ||
324 | vert.Normal.Y = getFloat(t); | ||
325 | result=true; | ||
326 | } | ||
327 | else if (Element.Properties[i].Name == "u") | ||
328 | vert.TCoords.X = getFloat(t); | ||
329 | else if (Element.Properties[i].Name == "v") | ||
330 | vert.TCoords.Y = getFloat(t); | ||
331 | else if (Element.Properties[i].Name == "red") | ||
332 | { | ||
333 | u32 value = Element.Properties[i].isFloat() ? (u32)(getFloat(t)*255.0f) : getInt(t); | ||
334 | vert.Color.setRed(value); | ||
335 | } | ||
336 | else if (Element.Properties[i].Name == "green") | ||
337 | { | ||
338 | u32 value = Element.Properties[i].isFloat() ? (u32)(getFloat(t)*255.0f) : getInt(t); | ||
339 | vert.Color.setGreen(value); | ||
340 | } | ||
341 | else if (Element.Properties[i].Name == "blue") | ||
342 | { | ||
343 | u32 value = Element.Properties[i].isFloat() ? (u32)(getFloat(t)*255.0f) : getInt(t); | ||
344 | vert.Color.setBlue(value); | ||
345 | } | ||
346 | else if (Element.Properties[i].Name == "alpha") | ||
347 | { | ||
348 | u32 value = Element.Properties[i].isFloat() ? (u32)(getFloat(t)*255.0f) : getInt(t); | ||
349 | vert.Color.setAlpha(value); | ||
350 | } | ||
351 | else | ||
352 | skipProperty(Element.Properties[i]); | ||
353 | } | ||
354 | |||
355 | mb->getVertexBuffer().push_back(vert); | ||
356 | |||
357 | return result; | ||
358 | } | ||
359 | |||
360 | |||
361 | bool CPLYMeshFileLoader::readFace(const SPLYElement &Element, scene::CDynamicMeshBuffer* mb) | ||
362 | { | ||
363 | if (!IsBinaryFile) | ||
364 | getNextLine(); | ||
365 | |||
366 | for (u32 i=0; i < Element.Properties.size(); ++i) | ||
367 | { | ||
368 | if ( (Element.Properties[i].Name == "vertex_indices" || | ||
369 | Element.Properties[i].Name == "vertex_index") && Element.Properties[i].Type == EPLYPT_LIST) | ||
370 | { | ||
371 | // get count | ||
372 | s32 count = getInt(Element.Properties[i].Data.List.CountType); | ||
373 | u32 a = getInt(Element.Properties[i].Data.List.ItemType), | ||
374 | b = getInt(Element.Properties[i].Data.List.ItemType), | ||
375 | c = getInt(Element.Properties[i].Data.List.ItemType); | ||
376 | s32 j = 3; | ||
377 | |||
378 | mb->getIndexBuffer().push_back(a); | ||
379 | mb->getIndexBuffer().push_back(c); | ||
380 | mb->getIndexBuffer().push_back(b); | ||
381 | |||
382 | for (; j < count; ++j) | ||
383 | { | ||
384 | b = c; | ||
385 | c = getInt(Element.Properties[i].Data.List.ItemType); | ||
386 | mb->getIndexBuffer().push_back(a); | ||
387 | mb->getIndexBuffer().push_back(c); | ||
388 | mb->getIndexBuffer().push_back(b); | ||
389 | } | ||
390 | } | ||
391 | else if (Element.Properties[i].Name == "intensity") | ||
392 | { | ||
393 | // todo: face intensity | ||
394 | skipProperty(Element.Properties[i]); | ||
395 | } | ||
396 | else | ||
397 | skipProperty(Element.Properties[i]); | ||
398 | } | ||
399 | return true; | ||
400 | } | ||
401 | |||
402 | |||
403 | // skips an element and all properties. return false on EOF | ||
404 | void CPLYMeshFileLoader::skipElement(const SPLYElement &Element) | ||
405 | { | ||
406 | if (IsBinaryFile) | ||
407 | if (Element.IsFixedWidth) | ||
408 | moveForward(Element.KnownSize); | ||
409 | else | ||
410 | for (u32 i=0; i < Element.Properties.size(); ++i) | ||
411 | skipProperty(Element.Properties[i]); | ||
412 | else | ||
413 | getNextLine(); | ||
414 | } | ||
415 | |||
416 | |||
417 | void CPLYMeshFileLoader::skipProperty(const SPLYProperty &Property) | ||
418 | { | ||
419 | if (Property.Type == EPLYPT_LIST) | ||
420 | { | ||
421 | s32 count = getInt(Property.Data.List.CountType); | ||
422 | |||
423 | for (s32 i=0; i < count; ++i) | ||
424 | getInt(Property.Data.List.CountType); | ||
425 | } | ||
426 | else | ||
427 | { | ||
428 | if (IsBinaryFile) | ||
429 | moveForward(Property.size()); | ||
430 | else | ||
431 | getNextWord(); | ||
432 | } | ||
433 | } | ||
434 | |||
435 | |||
436 | bool CPLYMeshFileLoader::allocateBuffer() | ||
437 | { | ||
438 | // Destroy the element list if it exists | ||
439 | for (u32 i=0; i<ElementList.size(); ++i) | ||
440 | delete ElementList[i]; | ||
441 | ElementList.clear(); | ||
442 | |||
443 | if (!Buffer) | ||
444 | Buffer = new c8[PLY_INPUT_BUFFER_SIZE]; | ||
445 | |||
446 | // not enough memory? | ||
447 | if (!Buffer) | ||
448 | return false; | ||
449 | |||
450 | // blank memory | ||
451 | memset(Buffer, 0, PLY_INPUT_BUFFER_SIZE); | ||
452 | |||
453 | StartPointer = Buffer; | ||
454 | EndPointer = Buffer; | ||
455 | LineEndPointer = Buffer-1; | ||
456 | WordLength = -1; | ||
457 | EndOfFile = false; | ||
458 | |||
459 | // get data from the file | ||
460 | fillBuffer(); | ||
461 | |||
462 | return true; | ||
463 | } | ||
464 | |||
465 | |||
466 | // gets more data from the file. returns false on EOF | ||
467 | void CPLYMeshFileLoader::fillBuffer() | ||
468 | { | ||
469 | if (EndOfFile) | ||
470 | return; | ||
471 | |||
472 | u32 length = (u32)(EndPointer - StartPointer); | ||
473 | if (length && StartPointer != Buffer) | ||
474 | { | ||
475 | // copy the remaining data to the start of the buffer | ||
476 | memcpy(Buffer, StartPointer, length); | ||
477 | } | ||
478 | // reset start position | ||
479 | StartPointer = Buffer; | ||
480 | EndPointer = StartPointer + length; | ||
481 | |||
482 | if (File->getPos() == File->getSize()) | ||
483 | { | ||
484 | EndOfFile = true; | ||
485 | } | ||
486 | else | ||
487 | { | ||
488 | // read data from the file | ||
489 | u32 count = File->read(EndPointer, PLY_INPUT_BUFFER_SIZE - length); | ||
490 | |||
491 | // increment the end pointer by the number of bytes read | ||
492 | EndPointer = EndPointer + count; | ||
493 | |||
494 | // if we didn't completely fill the buffer | ||
495 | if (count != PLY_INPUT_BUFFER_SIZE - length) | ||
496 | { | ||
497 | // blank the rest of the memory | ||
498 | memset(EndPointer, 0, Buffer + PLY_INPUT_BUFFER_SIZE - EndPointer); | ||
499 | |||
500 | // end of file | ||
501 | EndOfFile = true; | ||
502 | } | ||
503 | } | ||
504 | } | ||
505 | |||
506 | |||
507 | // skips x bytes in the file, getting more data if required | ||
508 | void CPLYMeshFileLoader::moveForward(u32 bytes) | ||
509 | { | ||
510 | if (StartPointer + bytes >= EndPointer) | ||
511 | fillBuffer(); | ||
512 | if (StartPointer + bytes < EndPointer) | ||
513 | StartPointer += bytes; | ||
514 | else | ||
515 | StartPointer = EndPointer; | ||
516 | } | ||
517 | |||
518 | |||
519 | E_PLY_PROPERTY_TYPE CPLYMeshFileLoader::getPropertyType(const c8* typeString) const | ||
520 | { | ||
521 | if (strcmp(typeString, "char") == 0 || | ||
522 | strcmp(typeString, "uchar") == 0 || | ||
523 | strcmp(typeString, "int8") == 0 || | ||
524 | strcmp(typeString, "uint8") == 0) | ||
525 | { | ||
526 | return EPLYPT_INT8; | ||
527 | } | ||
528 | else if (strcmp(typeString, "uint") == 0 || | ||
529 | strcmp(typeString, "int16") == 0 || | ||
530 | strcmp(typeString, "uint16") == 0 || | ||
531 | strcmp(typeString, "short") == 0 || | ||
532 | strcmp(typeString, "ushort") == 0) | ||
533 | { | ||
534 | return EPLYPT_INT16; | ||
535 | } | ||
536 | else if (strcmp(typeString, "int") == 0 || | ||
537 | strcmp(typeString, "long") == 0 || | ||
538 | strcmp(typeString, "ulong") == 0 || | ||
539 | strcmp(typeString, "int32") == 0 || | ||
540 | strcmp(typeString, "uint32") == 0) | ||
541 | { | ||
542 | return EPLYPT_INT32; | ||
543 | } | ||
544 | else if (strcmp(typeString, "float") == 0 || | ||
545 | strcmp(typeString, "float32") == 0) | ||
546 | { | ||
547 | return EPLYPT_FLOAT32; | ||
548 | } | ||
549 | else if (strcmp(typeString, "float64") == 0 || | ||
550 | strcmp(typeString, "double") == 0) | ||
551 | { | ||
552 | return EPLYPT_FLOAT64; | ||
553 | } | ||
554 | else if ( strcmp(typeString, "list") == 0 ) | ||
555 | { | ||
556 | return EPLYPT_LIST; | ||
557 | } | ||
558 | else | ||
559 | { | ||
560 | // unsupported type. | ||
561 | // cannot be loaded in binary mode | ||
562 | return EPLYPT_UNKNOWN; | ||
563 | } | ||
564 | } | ||
565 | |||
566 | |||
567 | // Split the string data into a line in place by terminating it instead of copying. | ||
568 | c8* CPLYMeshFileLoader::getNextLine() | ||
569 | { | ||
570 | // move the start pointer along | ||
571 | StartPointer = LineEndPointer + 1; | ||
572 | |||
573 | // crlf split across buffer move | ||
574 | if (*StartPointer == '\n') | ||
575 | { | ||
576 | *StartPointer = '\0'; | ||
577 | ++StartPointer; | ||
578 | } | ||
579 | |||
580 | // begin at the start of the next line | ||
581 | c8* pos = StartPointer; | ||
582 | while (pos < EndPointer && *pos && *pos != '\r' && *pos != '\n') | ||
583 | ++pos; | ||
584 | |||
585 | if ( pos < EndPointer && ( *(pos+1) == '\r' || *(pos+1) == '\n') ) | ||
586 | { | ||
587 | *pos = '\0'; | ||
588 | ++pos; | ||
589 | } | ||
590 | |||
591 | // we have reached the end of the buffer | ||
592 | if (pos >= EndPointer) | ||
593 | { | ||
594 | // get data from the file | ||
595 | if (!EndOfFile) | ||
596 | { | ||
597 | fillBuffer(); | ||
598 | // reset line end pointer | ||
599 | LineEndPointer = StartPointer - 1; | ||
600 | |||
601 | if (StartPointer != EndPointer) | ||
602 | return getNextLine(); | ||
603 | else | ||
604 | return Buffer; | ||
605 | } | ||
606 | else | ||
607 | { | ||
608 | // EOF | ||
609 | StartPointer = EndPointer-1; | ||
610 | *StartPointer = '\0'; | ||
611 | return StartPointer; | ||
612 | } | ||
613 | } | ||
614 | else | ||
615 | { | ||
616 | // null terminate the string in place | ||
617 | *pos = '\0'; | ||
618 | LineEndPointer = pos; | ||
619 | WordLength = -1; | ||
620 | // return pointer to the start of the line | ||
621 | return StartPointer; | ||
622 | } | ||
623 | } | ||
624 | |||
625 | |||
626 | // null terminate the next word on the previous line and move the next word pointer along | ||
627 | // since we already have a full line in the buffer, we never need to retrieve more data | ||
628 | c8* CPLYMeshFileLoader::getNextWord() | ||
629 | { | ||
630 | // move the start pointer along | ||
631 | StartPointer += WordLength + 1; | ||
632 | |||
633 | if (StartPointer == LineEndPointer) | ||
634 | { | ||
635 | WordLength = -1; // | ||
636 | return LineEndPointer; | ||
637 | } | ||
638 | // begin at the start of the next word | ||
639 | c8* pos = StartPointer; | ||
640 | while (*pos && pos < LineEndPointer && pos < EndPointer && *pos != ' ' && *pos != '\t') | ||
641 | ++pos; | ||
642 | |||
643 | while(*pos && pos < LineEndPointer && pos < EndPointer && (*pos == ' ' || *pos == '\t') ) | ||
644 | { | ||
645 | // null terminate the string in place | ||
646 | *pos = '\0'; | ||
647 | ++pos; | ||
648 | } | ||
649 | --pos; | ||
650 | WordLength = (s32)(pos-StartPointer); | ||
651 | // return pointer to the start of the word | ||
652 | return StartPointer; | ||
653 | } | ||
654 | |||
655 | |||
656 | // read the next float from the file and move the start pointer along | ||
657 | f32 CPLYMeshFileLoader::getFloat(E_PLY_PROPERTY_TYPE t) | ||
658 | { | ||
659 | f32 retVal = 0.0f; | ||
660 | |||
661 | if (IsBinaryFile) | ||
662 | { | ||
663 | if (EndPointer - StartPointer < 8) | ||
664 | fillBuffer(); | ||
665 | |||
666 | if (EndPointer - StartPointer > 0) | ||
667 | { | ||
668 | switch (t) | ||
669 | { | ||
670 | case EPLYPT_INT8: | ||
671 | retVal = *StartPointer; | ||
672 | StartPointer++; | ||
673 | break; | ||
674 | case EPLYPT_INT16: | ||
675 | if (IsWrongEndian) | ||
676 | retVal = os::Byteswap::byteswap(*(reinterpret_cast<s16*>(StartPointer))); | ||
677 | else | ||
678 | retVal = *(reinterpret_cast<s16*>(StartPointer)); | ||
679 | StartPointer += 2; | ||
680 | break; | ||
681 | case EPLYPT_INT32: | ||
682 | if (IsWrongEndian) | ||
683 | retVal = f32(os::Byteswap::byteswap(*(reinterpret_cast<s32*>(StartPointer)))); | ||
684 | else | ||
685 | retVal = f32(*(reinterpret_cast<s32*>(StartPointer))); | ||
686 | StartPointer += 4; | ||
687 | break; | ||
688 | case EPLYPT_FLOAT32: | ||
689 | if (IsWrongEndian) | ||
690 | retVal = os::Byteswap::byteswap(*(reinterpret_cast<f32*>(StartPointer))); | ||
691 | else | ||
692 | retVal = *(reinterpret_cast<f32*>(StartPointer)); | ||
693 | StartPointer += 4; | ||
694 | break; | ||
695 | case EPLYPT_FLOAT64: | ||
696 | // todo: byteswap 64-bit | ||
697 | retVal = f32(*(reinterpret_cast<f64*>(StartPointer))); | ||
698 | StartPointer += 8; | ||
699 | break; | ||
700 | case EPLYPT_LIST: | ||
701 | case EPLYPT_UNKNOWN: | ||
702 | default: | ||
703 | retVal = 0.0f; | ||
704 | StartPointer++; // ouch! | ||
705 | } | ||
706 | } | ||
707 | else | ||
708 | retVal = 0.0f; | ||
709 | } | ||
710 | else | ||
711 | { | ||
712 | c8* word = getNextWord(); | ||
713 | switch (t) | ||
714 | { | ||
715 | case EPLYPT_INT8: | ||
716 | case EPLYPT_INT16: | ||
717 | case EPLYPT_INT32: | ||
718 | retVal = f32(atoi(word)); | ||
719 | break; | ||
720 | case EPLYPT_FLOAT32: | ||
721 | case EPLYPT_FLOAT64: | ||
722 | retVal = f32(atof(word)); | ||
723 | break; | ||
724 | case EPLYPT_LIST: | ||
725 | case EPLYPT_UNKNOWN: | ||
726 | default: | ||
727 | retVal = 0.0f; | ||
728 | } | ||
729 | } | ||
730 | return retVal; | ||
731 | } | ||
732 | |||
733 | |||
734 | // read the next int from the file and move the start pointer along | ||
735 | u32 CPLYMeshFileLoader::getInt(E_PLY_PROPERTY_TYPE t) | ||
736 | { | ||
737 | u32 retVal = 0; | ||
738 | |||
739 | if (IsBinaryFile) | ||
740 | { | ||
741 | if (!EndOfFile && EndPointer - StartPointer < 8) | ||
742 | fillBuffer(); | ||
743 | |||
744 | if (EndPointer - StartPointer) | ||
745 | { | ||
746 | switch (t) | ||
747 | { | ||
748 | case EPLYPT_INT8: | ||
749 | retVal = *StartPointer; | ||
750 | StartPointer++; | ||
751 | break; | ||
752 | case EPLYPT_INT16: | ||
753 | if (IsWrongEndian) | ||
754 | retVal = os::Byteswap::byteswap(*(reinterpret_cast<u16*>(StartPointer))); | ||
755 | else | ||
756 | retVal = *(reinterpret_cast<u16*>(StartPointer)); | ||
757 | StartPointer += 2; | ||
758 | break; | ||
759 | case EPLYPT_INT32: | ||
760 | if (IsWrongEndian) | ||
761 | retVal = os::Byteswap::byteswap(*(reinterpret_cast<s32*>(StartPointer))); | ||
762 | else | ||
763 | retVal = *(reinterpret_cast<s32*>(StartPointer)); | ||
764 | StartPointer += 4; | ||
765 | break; | ||
766 | case EPLYPT_FLOAT32: | ||
767 | if (IsWrongEndian) | ||
768 | retVal = (u32)os::Byteswap::byteswap(*(reinterpret_cast<f32*>(StartPointer))); | ||
769 | else | ||
770 | retVal = (u32)(*(reinterpret_cast<f32*>(StartPointer))); | ||
771 | StartPointer += 4; | ||
772 | break; | ||
773 | case EPLYPT_FLOAT64: | ||
774 | // todo: byteswap 64-bit | ||
775 | retVal = (u32)(*(reinterpret_cast<f64*>(StartPointer))); | ||
776 | StartPointer += 8; | ||
777 | break; | ||
778 | case EPLYPT_LIST: | ||
779 | case EPLYPT_UNKNOWN: | ||
780 | default: | ||
781 | retVal = 0; | ||
782 | StartPointer++; // ouch! | ||
783 | } | ||
784 | } | ||
785 | else | ||
786 | retVal = 0; | ||
787 | } | ||
788 | else | ||
789 | { | ||
790 | c8* word = getNextWord(); | ||
791 | switch (t) | ||
792 | { | ||
793 | case EPLYPT_INT8: | ||
794 | case EPLYPT_INT16: | ||
795 | case EPLYPT_INT32: | ||
796 | retVal = atoi(word); | ||
797 | break; | ||
798 | case EPLYPT_FLOAT32: | ||
799 | case EPLYPT_FLOAT64: | ||
800 | retVal = u32(atof(word)); | ||
801 | break; | ||
802 | case EPLYPT_LIST: | ||
803 | case EPLYPT_UNKNOWN: | ||
804 | default: | ||
805 | retVal = 0; | ||
806 | } | ||
807 | } | ||
808 | return retVal; | ||
809 | } | ||
810 | |||
811 | |||
812 | |||
813 | } // end namespace scene | ||
814 | } // end namespace irr | ||
815 | |||
816 | #endif // _IRR_COMPILE_WITH_PLY_LOADER_ | ||
817 | |||