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