diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/irrlicht-1.8/source/Irrlicht/CSMFMeshFileLoader.cpp | 464 |
1 files changed, 232 insertions, 232 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/CSMFMeshFileLoader.cpp b/libraries/irrlicht-1.8/source/Irrlicht/CSMFMeshFileLoader.cpp index 099f2aa..96def08 100644 --- a/libraries/irrlicht-1.8/source/Irrlicht/CSMFMeshFileLoader.cpp +++ b/libraries/irrlicht-1.8/source/Irrlicht/CSMFMeshFileLoader.cpp | |||
@@ -1,232 +1,232 @@ | |||
1 | // Copyright (C) 2010-2012 Gaz Davidson / Joseph Ellis | 1 | // Copyright (C) 2010-2012 Gaz Davidson / Joseph Ellis |
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_SMF_LOADER_ | 7 | #ifdef _IRR_COMPILE_WITH_SMF_LOADER_ |
8 | 8 | ||
9 | #include "CSMFMeshFileLoader.h" | 9 | #include "CSMFMeshFileLoader.h" |
10 | #include "SAnimatedMesh.h" | 10 | #include "SAnimatedMesh.h" |
11 | #include "SMeshBuffer.h" | 11 | #include "SMeshBuffer.h" |
12 | #include "IReadFile.h" | 12 | #include "IReadFile.h" |
13 | #include "coreutil.h" | 13 | #include "coreutil.h" |
14 | #include "os.h" | 14 | #include "os.h" |
15 | #include "IVideoDriver.h" | 15 | #include "IVideoDriver.h" |
16 | 16 | ||
17 | namespace irr | 17 | namespace irr |
18 | { | 18 | { |
19 | namespace scene | 19 | namespace scene |
20 | { | 20 | { |
21 | 21 | ||
22 | CSMFMeshFileLoader::CSMFMeshFileLoader(video::IVideoDriver* driver) | 22 | CSMFMeshFileLoader::CSMFMeshFileLoader(video::IVideoDriver* driver) |
23 | : Driver(driver) | 23 | : Driver(driver) |
24 | { | 24 | { |
25 | } | 25 | } |
26 | 26 | ||
27 | //! Returns true if the file might be loaded by this class. | 27 | //! Returns true if the file might be loaded by this class. |
28 | bool CSMFMeshFileLoader::isALoadableFileExtension(const io::path& filename) const | 28 | bool CSMFMeshFileLoader::isALoadableFileExtension(const io::path& filename) const |
29 | { | 29 | { |
30 | return core::hasFileExtension(filename, "smf"); | 30 | return core::hasFileExtension(filename, "smf"); |
31 | } | 31 | } |
32 | 32 | ||
33 | //! Creates/loads an animated mesh from the file. | 33 | //! Creates/loads an animated mesh from the file. |
34 | IAnimatedMesh* CSMFMeshFileLoader::createMesh(io::IReadFile* file) | 34 | IAnimatedMesh* CSMFMeshFileLoader::createMesh(io::IReadFile* file) |
35 | { | 35 | { |
36 | // create empty mesh | 36 | // create empty mesh |
37 | SMesh *mesh = new SMesh(); | 37 | SMesh *mesh = new SMesh(); |
38 | 38 | ||
39 | // load file | 39 | // load file |
40 | u16 version; | 40 | u16 version; |
41 | u8 flags; | 41 | u8 flags; |
42 | s32 limbCount; | 42 | s32 limbCount; |
43 | s32 i; | 43 | s32 i; |
44 | 44 | ||
45 | io::BinaryFile::read(file, version); | 45 | io::BinaryFile::read(file, version); |
46 | io::BinaryFile::read(file, flags); | 46 | io::BinaryFile::read(file, flags); |
47 | io::BinaryFile::read(file, limbCount); | 47 | io::BinaryFile::read(file, limbCount); |
48 | 48 | ||
49 | // load mesh data | 49 | // load mesh data |
50 | core::matrix4 identity; | 50 | core::matrix4 identity; |
51 | for (i=0; i < limbCount; ++i) | 51 | for (i=0; i < limbCount; ++i) |
52 | loadLimb(file, mesh, identity); | 52 | loadLimb(file, mesh, identity); |
53 | 53 | ||
54 | // recalculate buffer bounding boxes | 54 | // recalculate buffer bounding boxes |
55 | for (i=0; i < (s32)mesh->getMeshBufferCount(); ++i) | 55 | for (i=0; i < (s32)mesh->getMeshBufferCount(); ++i) |
56 | mesh->getMeshBuffer(i)->recalculateBoundingBox(); | 56 | mesh->getMeshBuffer(i)->recalculateBoundingBox(); |
57 | 57 | ||
58 | mesh->recalculateBoundingBox(); | 58 | mesh->recalculateBoundingBox(); |
59 | SAnimatedMesh *am = new SAnimatedMesh(); | 59 | SAnimatedMesh *am = new SAnimatedMesh(); |
60 | am->addMesh(mesh); | 60 | am->addMesh(mesh); |
61 | mesh->drop(); | 61 | mesh->drop(); |
62 | am->recalculateBoundingBox(); | 62 | am->recalculateBoundingBox(); |
63 | 63 | ||
64 | return am; | 64 | return am; |
65 | } | 65 | } |
66 | 66 | ||
67 | void CSMFMeshFileLoader::loadLimb(io::IReadFile* file, SMesh* mesh, const core::matrix4 &parentTransformation) | 67 | void CSMFMeshFileLoader::loadLimb(io::IReadFile* file, SMesh* mesh, const core::matrix4 &parentTransformation) |
68 | { | 68 | { |
69 | core::matrix4 transformation; | 69 | core::matrix4 transformation; |
70 | 70 | ||
71 | // limb transformation | 71 | // limb transformation |
72 | core::vector3df translate, rotate, scale; | 72 | core::vector3df translate, rotate, scale; |
73 | io::BinaryFile::read(file, translate); | 73 | io::BinaryFile::read(file, translate); |
74 | io::BinaryFile::read(file, rotate); | 74 | io::BinaryFile::read(file, rotate); |
75 | io::BinaryFile::read(file, scale); | 75 | io::BinaryFile::read(file, scale); |
76 | 76 | ||
77 | transformation.setTranslation(translate); | 77 | transformation.setTranslation(translate); |
78 | transformation.setRotationDegrees(rotate); | 78 | transformation.setRotationDegrees(rotate); |
79 | transformation.setScale(scale); | 79 | transformation.setScale(scale); |
80 | 80 | ||
81 | transformation = parentTransformation * transformation; | 81 | transformation = parentTransformation * transformation; |
82 | 82 | ||
83 | core::stringc textureName, textureGroupName; | 83 | core::stringc textureName, textureGroupName; |
84 | 84 | ||
85 | // texture information | 85 | // texture information |
86 | io::BinaryFile::read(file, textureGroupName); | 86 | io::BinaryFile::read(file, textureGroupName); |
87 | io::BinaryFile::read(file, textureName); | 87 | io::BinaryFile::read(file, textureName); |
88 | 88 | ||
89 | // attempt to load texture using known formats | 89 | // attempt to load texture using known formats |
90 | video::ITexture* texture = 0; | 90 | video::ITexture* texture = 0; |
91 | 91 | ||
92 | const c8* extensions[] = {".jpg", ".png", ".tga", ".bmp", 0}; | 92 | const c8* extensions[] = {".jpg", ".png", ".tga", ".bmp", 0}; |
93 | 93 | ||
94 | for (const c8 **ext = extensions; !texture && *ext; ++ext) | 94 | for (const c8 **ext = extensions; !texture && *ext; ++ext) |
95 | { | 95 | { |
96 | texture = Driver->getTexture(textureName + *ext); | 96 | texture = Driver->getTexture(textureName + *ext); |
97 | if (texture) | 97 | if (texture) |
98 | textureName = textureName + *ext; | 98 | textureName = textureName + *ext; |
99 | } | 99 | } |
100 | // find the correct mesh buffer | 100 | // find the correct mesh buffer |
101 | u32 i; | 101 | u32 i; |
102 | for (i=0; i<mesh->MeshBuffers.size(); ++i) | 102 | for (i=0; i<mesh->MeshBuffers.size(); ++i) |
103 | if (mesh->MeshBuffers[i]->getMaterial().TextureLayer[0].Texture == texture) | 103 | if (mesh->MeshBuffers[i]->getMaterial().TextureLayer[0].Texture == texture) |
104 | break; | 104 | break; |
105 | 105 | ||
106 | // create mesh buffer if none was found | 106 | // create mesh buffer if none was found |
107 | if (i == mesh->MeshBuffers.size()) | 107 | if (i == mesh->MeshBuffers.size()) |
108 | { | 108 | { |
109 | CMeshBuffer<video::S3DVertex>* mb = new CMeshBuffer<video::S3DVertex>(); | 109 | CMeshBuffer<video::S3DVertex>* mb = new CMeshBuffer<video::S3DVertex>(); |
110 | mb->Material.TextureLayer[0].Texture = texture; | 110 | mb->Material.TextureLayer[0].Texture = texture; |
111 | 111 | ||
112 | // horribly hacky way to do this, maybe it's in the flags? | 112 | // horribly hacky way to do this, maybe it's in the flags? |
113 | if (core::hasFileExtension(textureName, "tga", "png")) | 113 | if (core::hasFileExtension(textureName, "tga", "png")) |
114 | mb->Material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; | 114 | mb->Material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; |
115 | else | 115 | else |
116 | mb->Material.MaterialType = video::EMT_SOLID; | 116 | mb->Material.MaterialType = video::EMT_SOLID; |
117 | 117 | ||
118 | mesh->MeshBuffers.push_back(mb); | 118 | mesh->MeshBuffers.push_back(mb); |
119 | } | 119 | } |
120 | 120 | ||
121 | CMeshBuffer<video::S3DVertex>* mb = (CMeshBuffer<video::S3DVertex>*)mesh->MeshBuffers[i]; | 121 | CMeshBuffer<video::S3DVertex>* mb = (CMeshBuffer<video::S3DVertex>*)mesh->MeshBuffers[i]; |
122 | 122 | ||
123 | u16 vertexCount, firstVertex = mb->getVertexCount(); | 123 | u16 vertexCount, firstVertex = mb->getVertexCount(); |
124 | 124 | ||
125 | io::BinaryFile::read(file, vertexCount); | 125 | io::BinaryFile::read(file, vertexCount); |
126 | mb->Vertices.reallocate(mb->Vertices.size() + vertexCount); | 126 | mb->Vertices.reallocate(mb->Vertices.size() + vertexCount); |
127 | 127 | ||
128 | // add vertices and set positions | 128 | // add vertices and set positions |
129 | for (i=0; i<vertexCount; ++i) | 129 | for (i=0; i<vertexCount; ++i) |
130 | { | 130 | { |
131 | core::vector3df pos; | 131 | core::vector3df pos; |
132 | io::BinaryFile::read(file, pos); | 132 | io::BinaryFile::read(file, pos); |
133 | transformation.transformVect(pos); | 133 | transformation.transformVect(pos); |
134 | video::S3DVertex vert; | 134 | video::S3DVertex vert; |
135 | vert.Color = 0xFFFFFFFF; | 135 | vert.Color = 0xFFFFFFFF; |
136 | vert.Pos = pos; | 136 | vert.Pos = pos; |
137 | mb->Vertices.push_back(vert); | 137 | mb->Vertices.push_back(vert); |
138 | } | 138 | } |
139 | 139 | ||
140 | // set vertex normals | 140 | // set vertex normals |
141 | for (i=0; i < vertexCount; ++i) | 141 | for (i=0; i < vertexCount; ++i) |
142 | { | 142 | { |
143 | core::vector3df normal; | 143 | core::vector3df normal; |
144 | io::BinaryFile::read(file, normal); | 144 | io::BinaryFile::read(file, normal); |
145 | transformation.rotateVect(normal); | 145 | transformation.rotateVect(normal); |
146 | mb->Vertices[firstVertex + i].Normal = normal; | 146 | mb->Vertices[firstVertex + i].Normal = normal; |
147 | } | 147 | } |
148 | // set texture coordinates | 148 | // set texture coordinates |
149 | 149 | ||
150 | for (i=0; i < vertexCount; ++i) | 150 | for (i=0; i < vertexCount; ++i) |
151 | { | 151 | { |
152 | core::vector2df tcoords; | 152 | core::vector2df tcoords; |
153 | io::BinaryFile::read(file, tcoords); | 153 | io::BinaryFile::read(file, tcoords); |
154 | mb->Vertices[firstVertex + i].TCoords = tcoords; | 154 | mb->Vertices[firstVertex + i].TCoords = tcoords; |
155 | } | 155 | } |
156 | 156 | ||
157 | // triangles | 157 | // triangles |
158 | u32 triangleCount; | 158 | u32 triangleCount; |
159 | // vertexCount used as temporary | 159 | // vertexCount used as temporary |
160 | io::BinaryFile::read(file, vertexCount); | 160 | io::BinaryFile::read(file, vertexCount); |
161 | triangleCount=3*vertexCount; | 161 | triangleCount=3*vertexCount; |
162 | mb->Indices.reallocate(mb->Indices.size() + triangleCount); | 162 | mb->Indices.reallocate(mb->Indices.size() + triangleCount); |
163 | 163 | ||
164 | for (i=0; i < triangleCount; ++i) | 164 | for (i=0; i < triangleCount; ++i) |
165 | { | 165 | { |
166 | u16 index; | 166 | u16 index; |
167 | io::BinaryFile::read(file, index); | 167 | io::BinaryFile::read(file, index); |
168 | mb->Indices.push_back(firstVertex + index); | 168 | mb->Indices.push_back(firstVertex + index); |
169 | } | 169 | } |
170 | 170 | ||
171 | // read limbs | 171 | // read limbs |
172 | s32 limbCount; | 172 | s32 limbCount; |
173 | io::BinaryFile::read(file, limbCount); | 173 | io::BinaryFile::read(file, limbCount); |
174 | 174 | ||
175 | for (s32 l=0; l < limbCount; ++l) | 175 | for (s32 l=0; l < limbCount; ++l) |
176 | loadLimb(file, mesh, transformation); | 176 | loadLimb(file, mesh, transformation); |
177 | } | 177 | } |
178 | 178 | ||
179 | } // namespace scene | 179 | } // namespace scene |
180 | 180 | ||
181 | // todo: at some point in the future let's move these to a place where everyone can use them. | 181 | // todo: at some point in the future let's move these to a place where everyone can use them. |
182 | namespace io | 182 | namespace io |
183 | { | 183 | { |
184 | 184 | ||
185 | #if _BIGENDIAN | 185 | #if _BIGENDIAN |
186 | #define _SYSTEM_BIG_ENDIAN_ (true) | 186 | #define _SYSTEM_BIG_ENDIAN_ (true) |
187 | #else | 187 | #else |
188 | #define _SYSTEM_BIG_ENDIAN_ (false) | 188 | #define _SYSTEM_BIG_ENDIAN_ (false) |
189 | #endif | 189 | #endif |
190 | 190 | ||
191 | template <class T> | 191 | template <class T> |
192 | void BinaryFile::read(io::IReadFile* file, T &out, bool bigEndian) | 192 | void BinaryFile::read(io::IReadFile* file, T &out, bool bigEndian) |
193 | { | 193 | { |
194 | file->read((void*)&out, sizeof(out)); | 194 | file->read((void*)&out, sizeof(out)); |
195 | if (bigEndian != (_SYSTEM_BIG_ENDIAN_)) | 195 | if (bigEndian != (_SYSTEM_BIG_ENDIAN_)) |
196 | out = os::Byteswap::byteswap(out); | 196 | out = os::Byteswap::byteswap(out); |
197 | } | 197 | } |
198 | 198 | ||
199 | //! reads a 3d vector from the file, moving the file pointer along | 199 | //! reads a 3d vector from the file, moving the file pointer along |
200 | void BinaryFile::read(io::IReadFile* file, core::vector3df &outVector2d, bool bigEndian) | 200 | void BinaryFile::read(io::IReadFile* file, core::vector3df &outVector2d, bool bigEndian) |
201 | { | 201 | { |
202 | BinaryFile::read(file, outVector2d.X, bigEndian); | 202 | BinaryFile::read(file, outVector2d.X, bigEndian); |
203 | BinaryFile::read(file, outVector2d.Y, bigEndian); | 203 | BinaryFile::read(file, outVector2d.Y, bigEndian); |
204 | BinaryFile::read(file, outVector2d.Z, bigEndian); | 204 | BinaryFile::read(file, outVector2d.Z, bigEndian); |
205 | } | 205 | } |
206 | 206 | ||
207 | //! reads a 2d vector from the file, moving the file pointer along | 207 | //! reads a 2d vector from the file, moving the file pointer along |
208 | void BinaryFile::read(io::IReadFile* file, core::vector2df &outVector2d, bool bigEndian) | 208 | void BinaryFile::read(io::IReadFile* file, core::vector2df &outVector2d, bool bigEndian) |
209 | { | 209 | { |
210 | BinaryFile::read(file, outVector2d.X, bigEndian); | 210 | BinaryFile::read(file, outVector2d.X, bigEndian); |
211 | BinaryFile::read(file, outVector2d.Y, bigEndian); | 211 | BinaryFile::read(file, outVector2d.Y, bigEndian); |
212 | } | 212 | } |
213 | 213 | ||
214 | //! reads a null terminated string from the file, moving the file pointer along | 214 | //! reads a null terminated string from the file, moving the file pointer along |
215 | void BinaryFile::read(io::IReadFile* file, core::stringc &outString, bool bigEndian) | 215 | void BinaryFile::read(io::IReadFile* file, core::stringc &outString, bool bigEndian) |
216 | { | 216 | { |
217 | c8 c; | 217 | c8 c; |
218 | file->read((void*)&c, 1); | 218 | file->read((void*)&c, 1); |
219 | 219 | ||
220 | while (c) | 220 | while (c) |
221 | { | 221 | { |
222 | outString += c; | 222 | outString += c; |
223 | file->read((void*)&c, 1); | 223 | file->read((void*)&c, 1); |
224 | } | 224 | } |
225 | } | 225 | } |
226 | 226 | ||
227 | } // namespace io | 227 | } // namespace io |
228 | 228 | ||
229 | } // namespace irr | 229 | } // namespace irr |
230 | 230 | ||
231 | #endif // compile with SMF loader | 231 | #endif // compile with SMF loader |
232 | 232 | ||