diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/irrlicht-1.8/source/Irrlicht/CSTLMeshFileLoader.cpp | 506 |
1 files changed, 253 insertions, 253 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/CSTLMeshFileLoader.cpp b/libraries/irrlicht-1.8/source/Irrlicht/CSTLMeshFileLoader.cpp index bf9ffb3..43a9793 100644 --- a/libraries/irrlicht-1.8/source/Irrlicht/CSTLMeshFileLoader.cpp +++ b/libraries/irrlicht-1.8/source/Irrlicht/CSTLMeshFileLoader.cpp | |||
@@ -1,253 +1,253 @@ | |||
1 | // Copyright (C) 2007-2012 Christian Stehno | 1 | // Copyright (C) 2007-2012 Christian Stehno |
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 | 6 | ||
7 | #ifdef _IRR_COMPILE_WITH_STL_LOADER_ | 7 | #ifdef _IRR_COMPILE_WITH_STL_LOADER_ |
8 | 8 | ||
9 | #include "CSTLMeshFileLoader.h" | 9 | #include "CSTLMeshFileLoader.h" |
10 | #include "SMesh.h" | 10 | #include "SMesh.h" |
11 | #include "SMeshBuffer.h" | 11 | #include "SMeshBuffer.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 "coreutil.h" | 15 | #include "coreutil.h" |
16 | #include "os.h" | 16 | #include "os.h" |
17 | 17 | ||
18 | namespace irr | 18 | namespace irr |
19 | { | 19 | { |
20 | namespace scene | 20 | namespace scene |
21 | { | 21 | { |
22 | 22 | ||
23 | 23 | ||
24 | //! returns true if the file maybe is able to be loaded by this class | 24 | //! returns true if the file maybe is able to be loaded by this class |
25 | //! based on the file extension (e.g. ".bsp") | 25 | //! based on the file extension (e.g. ".bsp") |
26 | bool CSTLMeshFileLoader::isALoadableFileExtension(const io::path& filename) const | 26 | bool CSTLMeshFileLoader::isALoadableFileExtension(const io::path& filename) const |
27 | { | 27 | { |
28 | return core::hasFileExtension ( filename, "stl" ); | 28 | return core::hasFileExtension ( filename, "stl" ); |
29 | } | 29 | } |
30 | 30 | ||
31 | 31 | ||
32 | 32 | ||
33 | //! creates/loads an animated mesh from the file. | 33 | //! creates/loads an animated mesh from the file. |
34 | //! \return Pointer to the created mesh. Returns 0 if loading failed. | 34 | //! \return Pointer to the created mesh. Returns 0 if loading failed. |
35 | //! If you no longer need the mesh, you should call IAnimatedMesh::drop(). | 35 | //! If you no longer need the mesh, you should call IAnimatedMesh::drop(). |
36 | //! See IReferenceCounted::drop() for more information. | 36 | //! See IReferenceCounted::drop() for more information. |
37 | IAnimatedMesh* CSTLMeshFileLoader::createMesh(io::IReadFile* file) | 37 | IAnimatedMesh* CSTLMeshFileLoader::createMesh(io::IReadFile* file) |
38 | { | 38 | { |
39 | const long filesize = file->getSize(); | 39 | const long filesize = file->getSize(); |
40 | if (filesize < 6) // we need a header | 40 | if (filesize < 6) // we need a header |
41 | return 0; | 41 | return 0; |
42 | 42 | ||
43 | SMesh* mesh = new SMesh(); | 43 | SMesh* mesh = new SMesh(); |
44 | SMeshBuffer* meshBuffer = new SMeshBuffer(); | 44 | SMeshBuffer* meshBuffer = new SMeshBuffer(); |
45 | mesh->addMeshBuffer(meshBuffer); | 45 | mesh->addMeshBuffer(meshBuffer); |
46 | meshBuffer->drop(); | 46 | meshBuffer->drop(); |
47 | 47 | ||
48 | core::vector3df vertex[3]; | 48 | core::vector3df vertex[3]; |
49 | core::vector3df normal; | 49 | core::vector3df normal; |
50 | 50 | ||
51 | bool binary = false; | 51 | bool binary = false; |
52 | core::stringc token; | 52 | core::stringc token; |
53 | if (getNextToken(file, token) != "solid") | 53 | if (getNextToken(file, token) != "solid") |
54 | binary = true; | 54 | binary = true; |
55 | // read/skip header | 55 | // read/skip header |
56 | u32 binFaceCount = 0; | 56 | u32 binFaceCount = 0; |
57 | if (binary) | 57 | if (binary) |
58 | { | 58 | { |
59 | file->seek(80); | 59 | file->seek(80); |
60 | file->read(&binFaceCount, 4); | 60 | file->read(&binFaceCount, 4); |
61 | #ifdef __BIG_ENDIAN__ | 61 | #ifdef __BIG_ENDIAN__ |
62 | binFaceCount = os::Byteswap::byteswap(binFaceCount); | 62 | binFaceCount = os::Byteswap::byteswap(binFaceCount); |
63 | #endif | 63 | #endif |
64 | } | 64 | } |
65 | else | 65 | else |
66 | goNextLine(file); | 66 | goNextLine(file); |
67 | 67 | ||
68 | u16 attrib=0; | 68 | u16 attrib=0; |
69 | token.reserve(32); | 69 | token.reserve(32); |
70 | 70 | ||
71 | while (file->getPos() < filesize) | 71 | while (file->getPos() < filesize) |
72 | { | 72 | { |
73 | if (!binary) | 73 | if (!binary) |
74 | { | 74 | { |
75 | if (getNextToken(file, token) != "facet") | 75 | if (getNextToken(file, token) != "facet") |
76 | { | 76 | { |
77 | if (token=="endsolid") | 77 | if (token=="endsolid") |
78 | break; | 78 | break; |
79 | mesh->drop(); | 79 | mesh->drop(); |
80 | return 0; | 80 | return 0; |
81 | } | 81 | } |
82 | if (getNextToken(file, token) != "normal") | 82 | if (getNextToken(file, token) != "normal") |
83 | { | 83 | { |
84 | mesh->drop(); | 84 | mesh->drop(); |
85 | return 0; | 85 | return 0; |
86 | } | 86 | } |
87 | } | 87 | } |
88 | getNextVector(file, normal, binary); | 88 | getNextVector(file, normal, binary); |
89 | if (!binary) | 89 | if (!binary) |
90 | { | 90 | { |
91 | if (getNextToken(file, token) != "outer") | 91 | if (getNextToken(file, token) != "outer") |
92 | { | 92 | { |
93 | mesh->drop(); | 93 | mesh->drop(); |
94 | return 0; | 94 | return 0; |
95 | } | 95 | } |
96 | if (getNextToken(file, token) != "loop") | 96 | if (getNextToken(file, token) != "loop") |
97 | { | 97 | { |
98 | mesh->drop(); | 98 | mesh->drop(); |
99 | return 0; | 99 | return 0; |
100 | } | 100 | } |
101 | } | 101 | } |
102 | for (u32 i=0; i<3; ++i) | 102 | for (u32 i=0; i<3; ++i) |
103 | { | 103 | { |
104 | if (!binary) | 104 | if (!binary) |
105 | { | 105 | { |
106 | if (getNextToken(file, token) != "vertex") | 106 | if (getNextToken(file, token) != "vertex") |
107 | { | 107 | { |
108 | mesh->drop(); | 108 | mesh->drop(); |
109 | return 0; | 109 | return 0; |
110 | } | 110 | } |
111 | } | 111 | } |
112 | getNextVector(file, vertex[i], binary); | 112 | getNextVector(file, vertex[i], binary); |
113 | } | 113 | } |
114 | if (!binary) | 114 | if (!binary) |
115 | { | 115 | { |
116 | if (getNextToken(file, token) != "endloop") | 116 | if (getNextToken(file, token) != "endloop") |
117 | { | 117 | { |
118 | mesh->drop(); | 118 | mesh->drop(); |
119 | return 0; | 119 | return 0; |
120 | } | 120 | } |
121 | if (getNextToken(file, token) != "endfacet") | 121 | if (getNextToken(file, token) != "endfacet") |
122 | { | 122 | { |
123 | mesh->drop(); | 123 | mesh->drop(); |
124 | return 0; | 124 | return 0; |
125 | } | 125 | } |
126 | } | 126 | } |
127 | else | 127 | else |
128 | { | 128 | { |
129 | file->read(&attrib, 2); | 129 | file->read(&attrib, 2); |
130 | #ifdef __BIG_ENDIAN__ | 130 | #ifdef __BIG_ENDIAN__ |
131 | attrib = os::Byteswap::byteswap(attrib); | 131 | attrib = os::Byteswap::byteswap(attrib); |
132 | #endif | 132 | #endif |
133 | } | 133 | } |
134 | 134 | ||
135 | SMeshBuffer* mb = reinterpret_cast<SMeshBuffer*>(mesh->getMeshBuffer(mesh->getMeshBufferCount()-1)); | 135 | SMeshBuffer* mb = reinterpret_cast<SMeshBuffer*>(mesh->getMeshBuffer(mesh->getMeshBufferCount()-1)); |
136 | u32 vCount = mb->getVertexCount(); | 136 | u32 vCount = mb->getVertexCount(); |
137 | video::SColor color(0xffffffff); | 137 | video::SColor color(0xffffffff); |
138 | if (attrib & 0x8000) | 138 | if (attrib & 0x8000) |
139 | color = video::A1R5G5B5toA8R8G8B8(attrib); | 139 | color = video::A1R5G5B5toA8R8G8B8(attrib); |
140 | if (normal==core::vector3df()) | 140 | if (normal==core::vector3df()) |
141 | normal=core::plane3df(vertex[2],vertex[1],vertex[0]).Normal; | 141 | normal=core::plane3df(vertex[2],vertex[1],vertex[0]).Normal; |
142 | mb->Vertices.push_back(video::S3DVertex(vertex[2],normal,color, core::vector2df())); | 142 | mb->Vertices.push_back(video::S3DVertex(vertex[2],normal,color, core::vector2df())); |
143 | mb->Vertices.push_back(video::S3DVertex(vertex[1],normal,color, core::vector2df())); | 143 | mb->Vertices.push_back(video::S3DVertex(vertex[1],normal,color, core::vector2df())); |
144 | mb->Vertices.push_back(video::S3DVertex(vertex[0],normal,color, core::vector2df())); | 144 | mb->Vertices.push_back(video::S3DVertex(vertex[0],normal,color, core::vector2df())); |
145 | mb->Indices.push_back(vCount); | 145 | mb->Indices.push_back(vCount); |
146 | mb->Indices.push_back(vCount+1); | 146 | mb->Indices.push_back(vCount+1); |
147 | mb->Indices.push_back(vCount+2); | 147 | mb->Indices.push_back(vCount+2); |
148 | } // end while (file->getPos() < filesize) | 148 | } // end while (file->getPos() < filesize) |
149 | mesh->getMeshBuffer(0)->recalculateBoundingBox(); | 149 | mesh->getMeshBuffer(0)->recalculateBoundingBox(); |
150 | 150 | ||
151 | // Create the Animated mesh if there's anything in the mesh | 151 | // Create the Animated mesh if there's anything in the mesh |
152 | SAnimatedMesh* pAM = 0; | 152 | SAnimatedMesh* pAM = 0; |
153 | if ( 0 != mesh->getMeshBufferCount() ) | 153 | if ( 0 != mesh->getMeshBufferCount() ) |
154 | { | 154 | { |
155 | mesh->recalculateBoundingBox(); | 155 | mesh->recalculateBoundingBox(); |
156 | pAM = new SAnimatedMesh(); | 156 | pAM = new SAnimatedMesh(); |
157 | pAM->Type = EAMT_OBJ; | 157 | pAM->Type = EAMT_OBJ; |
158 | pAM->addMesh(mesh); | 158 | pAM->addMesh(mesh); |
159 | pAM->recalculateBoundingBox(); | 159 | pAM->recalculateBoundingBox(); |
160 | } | 160 | } |
161 | 161 | ||
162 | mesh->drop(); | 162 | mesh->drop(); |
163 | 163 | ||
164 | return pAM; | 164 | return pAM; |
165 | } | 165 | } |
166 | 166 | ||
167 | 167 | ||
168 | //! Read 3d vector of floats | 168 | //! Read 3d vector of floats |
169 | void CSTLMeshFileLoader::getNextVector(io::IReadFile* file, core::vector3df& vec, bool binary) const | 169 | void CSTLMeshFileLoader::getNextVector(io::IReadFile* file, core::vector3df& vec, bool binary) const |
170 | { | 170 | { |
171 | if (binary) | 171 | if (binary) |
172 | { | 172 | { |
173 | file->read(&vec.X, 4); | 173 | file->read(&vec.X, 4); |
174 | file->read(&vec.Y, 4); | 174 | file->read(&vec.Y, 4); |
175 | file->read(&vec.Z, 4); | 175 | file->read(&vec.Z, 4); |
176 | #ifdef __BIG_ENDIAN__ | 176 | #ifdef __BIG_ENDIAN__ |
177 | vec.X = os::Byteswap::byteswap(vec.X); | 177 | vec.X = os::Byteswap::byteswap(vec.X); |
178 | vec.Y = os::Byteswap::byteswap(vec.Y); | 178 | vec.Y = os::Byteswap::byteswap(vec.Y); |
179 | vec.Z = os::Byteswap::byteswap(vec.Z); | 179 | vec.Z = os::Byteswap::byteswap(vec.Z); |
180 | #endif | 180 | #endif |
181 | } | 181 | } |
182 | else | 182 | else |
183 | { | 183 | { |
184 | goNextWord(file); | 184 | goNextWord(file); |
185 | core::stringc tmp; | 185 | core::stringc tmp; |
186 | 186 | ||
187 | getNextToken(file, tmp); | 187 | getNextToken(file, tmp); |
188 | core::fast_atof_move(tmp.c_str(), vec.X); | 188 | core::fast_atof_move(tmp.c_str(), vec.X); |
189 | getNextToken(file, tmp); | 189 | getNextToken(file, tmp); |
190 | core::fast_atof_move(tmp.c_str(), vec.Y); | 190 | core::fast_atof_move(tmp.c_str(), vec.Y); |
191 | getNextToken(file, tmp); | 191 | getNextToken(file, tmp); |
192 | core::fast_atof_move(tmp.c_str(), vec.Z); | 192 | core::fast_atof_move(tmp.c_str(), vec.Z); |
193 | } | 193 | } |
194 | vec.X=-vec.X; | 194 | vec.X=-vec.X; |
195 | } | 195 | } |
196 | 196 | ||
197 | 197 | ||
198 | //! Read next word | 198 | //! Read next word |
199 | const core::stringc& CSTLMeshFileLoader::getNextToken(io::IReadFile* file, core::stringc& token) const | 199 | const core::stringc& CSTLMeshFileLoader::getNextToken(io::IReadFile* file, core::stringc& token) const |
200 | { | 200 | { |
201 | goNextWord(file); | 201 | goNextWord(file); |
202 | u8 c; | 202 | u8 c; |
203 | token = ""; | 203 | token = ""; |
204 | while(file->getPos() != file->getSize()) | 204 | while(file->getPos() != file->getSize()) |
205 | { | 205 | { |
206 | file->read(&c, 1); | 206 | file->read(&c, 1); |
207 | // found it, so leave | 207 | // found it, so leave |
208 | if (core::isspace(c)) | 208 | if (core::isspace(c)) |
209 | break; | 209 | break; |
210 | token.append(c); | 210 | token.append(c); |
211 | } | 211 | } |
212 | return token; | 212 | return token; |
213 | } | 213 | } |
214 | 214 | ||
215 | 215 | ||
216 | //! skip to next word | 216 | //! skip to next word |
217 | void CSTLMeshFileLoader::goNextWord(io::IReadFile* file) const | 217 | void CSTLMeshFileLoader::goNextWord(io::IReadFile* file) const |
218 | { | 218 | { |
219 | u8 c; | 219 | u8 c; |
220 | while(file->getPos() != file->getSize()) | 220 | while(file->getPos() != file->getSize()) |
221 | { | 221 | { |
222 | file->read(&c, 1); | 222 | file->read(&c, 1); |
223 | // found it, so leave | 223 | // found it, so leave |
224 | if (!core::isspace(c)) | 224 | if (!core::isspace(c)) |
225 | { | 225 | { |
226 | file->seek(-1, true); | 226 | file->seek(-1, true); |
227 | break; | 227 | break; |
228 | } | 228 | } |
229 | } | 229 | } |
230 | } | 230 | } |
231 | 231 | ||
232 | 232 | ||
233 | //! Read until line break is reached and stop at the next non-space character | 233 | //! Read until line break is reached and stop at the next non-space character |
234 | void CSTLMeshFileLoader::goNextLine(io::IReadFile* file) const | 234 | void CSTLMeshFileLoader::goNextLine(io::IReadFile* file) const |
235 | { | 235 | { |
236 | u8 c; | 236 | u8 c; |
237 | // look for newline characters | 237 | // look for newline characters |
238 | while(file->getPos() != file->getSize()) | 238 | while(file->getPos() != file->getSize()) |
239 | { | 239 | { |
240 | file->read(&c, 1); | 240 | file->read(&c, 1); |
241 | // found it, so leave | 241 | // found it, so leave |
242 | if (c=='\n' || c=='\r') | 242 | if (c=='\n' || c=='\r') |
243 | break; | 243 | break; |
244 | } | 244 | } |
245 | } | 245 | } |
246 | 246 | ||
247 | 247 | ||
248 | } // end namespace scene | 248 | } // end namespace scene |
249 | } // end namespace irr | 249 | } // end namespace irr |
250 | 250 | ||
251 | 251 | ||
252 | #endif // _IRR_COMPILE_WITH_STL_LOADER_ | 252 | #endif // _IRR_COMPILE_WITH_STL_LOADER_ |
253 | 253 | ||