diff options
Diffstat (limited to 'libraries/irrlicht-1.8/source/Irrlicht/CMD2MeshFileLoader.cpp')
-rw-r--r-- | libraries/irrlicht-1.8/source/Irrlicht/CMD2MeshFileLoader.cpp | 728 |
1 files changed, 364 insertions, 364 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/CMD2MeshFileLoader.cpp b/libraries/irrlicht-1.8/source/Irrlicht/CMD2MeshFileLoader.cpp index 4fa26d5..79333ab 100644 --- a/libraries/irrlicht-1.8/source/Irrlicht/CMD2MeshFileLoader.cpp +++ b/libraries/irrlicht-1.8/source/Irrlicht/CMD2MeshFileLoader.cpp | |||
@@ -1,364 +1,364 @@ | |||
1 | // Copyright (C) 2002-2012 Nikolaus Gebhardt | 1 | // Copyright (C) 2002-2012 Nikolaus Gebhardt |
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_MD2_LOADER_ | 6 | #ifdef _IRR_COMPILE_WITH_MD2_LOADER_ |
7 | 7 | ||
8 | #include "CMD2MeshFileLoader.h" | 8 | #include "CMD2MeshFileLoader.h" |
9 | #include "CAnimatedMeshMD2.h" | 9 | #include "CAnimatedMeshMD2.h" |
10 | #include "os.h" | 10 | #include "os.h" |
11 | 11 | ||
12 | namespace irr | 12 | namespace irr |
13 | { | 13 | { |
14 | namespace scene | 14 | namespace scene |
15 | { | 15 | { |
16 | 16 | ||
17 | 17 | ||
18 | // structs needed to load the md2-format | 18 | // structs needed to load the md2-format |
19 | 19 | ||
20 | const s32 MD2_MAGIC_NUMBER = 844121161; | 20 | const s32 MD2_MAGIC_NUMBER = 844121161; |
21 | const s32 MD2_VERSION = 8; | 21 | const s32 MD2_VERSION = 8; |
22 | const s32 MD2_MAX_VERTS = 2048; | 22 | const s32 MD2_MAX_VERTS = 2048; |
23 | 23 | ||
24 | // byte-align structures | 24 | // byte-align structures |
25 | #include "irrpack.h" | 25 | #include "irrpack.h" |
26 | 26 | ||
27 | struct SMD2Header | 27 | struct SMD2Header |
28 | { | 28 | { |
29 | s32 magic; // four character code "IDP2" | 29 | s32 magic; // four character code "IDP2" |
30 | s32 version; // must be 8 | 30 | s32 version; // must be 8 |
31 | s32 skinWidth; // width of the texture | 31 | s32 skinWidth; // width of the texture |
32 | s32 skinHeight; // height of the texture | 32 | s32 skinHeight; // height of the texture |
33 | s32 frameSize; // size in bytes of an animation frame | 33 | s32 frameSize; // size in bytes of an animation frame |
34 | s32 numSkins; // number of textures | 34 | s32 numSkins; // number of textures |
35 | s32 numVertices; // total number of vertices | 35 | s32 numVertices; // total number of vertices |
36 | s32 numTexcoords; // number of vertices with texture coords | 36 | s32 numTexcoords; // number of vertices with texture coords |
37 | s32 numTriangles; // number of triangles | 37 | s32 numTriangles; // number of triangles |
38 | s32 numGlCommands; // number of opengl commands (triangle strip or triangle fan) | 38 | s32 numGlCommands; // number of opengl commands (triangle strip or triangle fan) |
39 | s32 numFrames; // animation keyframe count | 39 | s32 numFrames; // animation keyframe count |
40 | s32 offsetSkins; // offset in bytes to 64 character skin names | 40 | s32 offsetSkins; // offset in bytes to 64 character skin names |
41 | s32 offsetTexcoords; // offset in bytes to texture coordinate list | 41 | s32 offsetTexcoords; // offset in bytes to texture coordinate list |
42 | s32 offsetTriangles; // offset in bytes to triangle list | 42 | s32 offsetTriangles; // offset in bytes to triangle list |
43 | s32 offsetFrames; // offset in bytes to frame list | 43 | s32 offsetFrames; // offset in bytes to frame list |
44 | s32 offsetGlCommands;// offset in bytes to opengl commands | 44 | s32 offsetGlCommands;// offset in bytes to opengl commands |
45 | s32 offsetEnd; // offset in bytes to end of file | 45 | s32 offsetEnd; // offset in bytes to end of file |
46 | } PACK_STRUCT; | 46 | } PACK_STRUCT; |
47 | 47 | ||
48 | struct SMD2Vertex | 48 | struct SMD2Vertex |
49 | { | 49 | { |
50 | u8 vertex[3]; // [0] = X, [1] = Z, [2] = Y | 50 | u8 vertex[3]; // [0] = X, [1] = Z, [2] = Y |
51 | u8 lightNormalIndex; // index in the normal table | 51 | u8 lightNormalIndex; // index in the normal table |
52 | } PACK_STRUCT; | 52 | } PACK_STRUCT; |
53 | 53 | ||
54 | struct SMD2Frame | 54 | struct SMD2Frame |
55 | { | 55 | { |
56 | f32 scale[3]; // first scale the vertex position | 56 | f32 scale[3]; // first scale the vertex position |
57 | f32 translate[3]; // then translate the position | 57 | f32 translate[3]; // then translate the position |
58 | c8 name[16]; // the name of the animation that this key belongs to | 58 | c8 name[16]; // the name of the animation that this key belongs to |
59 | SMD2Vertex vertices[1]; // vertex 1 of SMD2Header.numVertices | 59 | SMD2Vertex vertices[1]; // vertex 1 of SMD2Header.numVertices |
60 | } PACK_STRUCT; | 60 | } PACK_STRUCT; |
61 | 61 | ||
62 | struct SMD2Triangle | 62 | struct SMD2Triangle |
63 | { | 63 | { |
64 | u16 vertexIndices[3]; | 64 | u16 vertexIndices[3]; |
65 | u16 textureIndices[3]; | 65 | u16 textureIndices[3]; |
66 | } PACK_STRUCT; | 66 | } PACK_STRUCT; |
67 | 67 | ||
68 | struct SMD2TextureCoordinate | 68 | struct SMD2TextureCoordinate |
69 | { | 69 | { |
70 | s16 s; | 70 | s16 s; |
71 | s16 t; | 71 | s16 t; |
72 | } PACK_STRUCT; | 72 | } PACK_STRUCT; |
73 | 73 | ||
74 | struct SMD2GLCommand | 74 | struct SMD2GLCommand |
75 | { | 75 | { |
76 | f32 s, t; | 76 | f32 s, t; |
77 | s32 vertexIndex; | 77 | s32 vertexIndex; |
78 | } PACK_STRUCT; | 78 | } PACK_STRUCT; |
79 | 79 | ||
80 | // Default alignment | 80 | // Default alignment |
81 | #include "irrunpack.h" | 81 | #include "irrunpack.h" |
82 | 82 | ||
83 | //! Constructor | 83 | //! Constructor |
84 | CMD2MeshFileLoader::CMD2MeshFileLoader() | 84 | CMD2MeshFileLoader::CMD2MeshFileLoader() |
85 | { | 85 | { |
86 | #ifdef _DEBUG | 86 | #ifdef _DEBUG |
87 | setDebugName("CMD2MeshFileLoader"); | 87 | setDebugName("CMD2MeshFileLoader"); |
88 | #endif | 88 | #endif |
89 | } | 89 | } |
90 | 90 | ||
91 | 91 | ||
92 | //! returns true if the file maybe is able to be loaded by this class | 92 | //! returns true if the file maybe is able to be loaded by this class |
93 | //! based on the file extension (e.g. ".bsp") | 93 | //! based on the file extension (e.g. ".bsp") |
94 | bool CMD2MeshFileLoader::isALoadableFileExtension(const io::path& filename) const | 94 | bool CMD2MeshFileLoader::isALoadableFileExtension(const io::path& filename) const |
95 | { | 95 | { |
96 | return core::hasFileExtension ( filename, "md2" ); | 96 | return core::hasFileExtension ( filename, "md2" ); |
97 | } | 97 | } |
98 | 98 | ||
99 | 99 | ||
100 | //! creates/loads an animated mesh from the file. | 100 | //! creates/loads an animated mesh from the file. |
101 | //! \return Pointer to the created mesh. Returns 0 if loading failed. | 101 | //! \return Pointer to the created mesh. Returns 0 if loading failed. |
102 | //! If you no longer need the mesh, you should call IAnimatedMesh::drop(). | 102 | //! If you no longer need the mesh, you should call IAnimatedMesh::drop(). |
103 | //! See IReferenceCounted::drop() for more information. | 103 | //! See IReferenceCounted::drop() for more information. |
104 | IAnimatedMesh* CMD2MeshFileLoader::createMesh(io::IReadFile* file) | 104 | IAnimatedMesh* CMD2MeshFileLoader::createMesh(io::IReadFile* file) |
105 | { | 105 | { |
106 | IAnimatedMesh* msh = new CAnimatedMeshMD2(); | 106 | IAnimatedMesh* msh = new CAnimatedMeshMD2(); |
107 | if (msh) | 107 | if (msh) |
108 | { | 108 | { |
109 | if (loadFile(file, (CAnimatedMeshMD2*)msh) ) | 109 | if (loadFile(file, (CAnimatedMeshMD2*)msh) ) |
110 | return msh; | 110 | return msh; |
111 | 111 | ||
112 | msh->drop(); | 112 | msh->drop(); |
113 | } | 113 | } |
114 | 114 | ||
115 | return 0; | 115 | return 0; |
116 | } | 116 | } |
117 | 117 | ||
118 | //! loads an md2 file | 118 | //! loads an md2 file |
119 | bool CMD2MeshFileLoader::loadFile(io::IReadFile* file, CAnimatedMeshMD2* mesh) | 119 | bool CMD2MeshFileLoader::loadFile(io::IReadFile* file, CAnimatedMeshMD2* mesh) |
120 | { | 120 | { |
121 | if (!file) | 121 | if (!file) |
122 | return false; | 122 | return false; |
123 | 123 | ||
124 | SMD2Header header; | 124 | SMD2Header header; |
125 | 125 | ||
126 | file->read(&header, sizeof(SMD2Header)); | 126 | file->read(&header, sizeof(SMD2Header)); |
127 | 127 | ||
128 | #ifdef __BIG_ENDIAN__ | 128 | #ifdef __BIG_ENDIAN__ |
129 | header.magic = os::Byteswap::byteswap(header.magic); | 129 | header.magic = os::Byteswap::byteswap(header.magic); |
130 | header.version = os::Byteswap::byteswap(header.version); | 130 | header.version = os::Byteswap::byteswap(header.version); |
131 | header.skinWidth = os::Byteswap::byteswap(header.skinWidth); | 131 | header.skinWidth = os::Byteswap::byteswap(header.skinWidth); |
132 | header.skinHeight = os::Byteswap::byteswap(header.skinHeight); | 132 | header.skinHeight = os::Byteswap::byteswap(header.skinHeight); |
133 | header.frameSize = os::Byteswap::byteswap(header.frameSize); | 133 | header.frameSize = os::Byteswap::byteswap(header.frameSize); |
134 | header.numSkins = os::Byteswap::byteswap(header.numSkins); | 134 | header.numSkins = os::Byteswap::byteswap(header.numSkins); |
135 | header.numVertices = os::Byteswap::byteswap(header.numVertices); | 135 | header.numVertices = os::Byteswap::byteswap(header.numVertices); |
136 | header.numTexcoords = os::Byteswap::byteswap(header.numTexcoords); | 136 | header.numTexcoords = os::Byteswap::byteswap(header.numTexcoords); |
137 | header.numTriangles = os::Byteswap::byteswap(header.numTriangles); | 137 | header.numTriangles = os::Byteswap::byteswap(header.numTriangles); |
138 | header.numGlCommands = os::Byteswap::byteswap(header.numGlCommands); | 138 | header.numGlCommands = os::Byteswap::byteswap(header.numGlCommands); |
139 | header.numFrames = os::Byteswap::byteswap(header.numFrames); | 139 | header.numFrames = os::Byteswap::byteswap(header.numFrames); |
140 | header.offsetSkins = os::Byteswap::byteswap(header.offsetSkins); | 140 | header.offsetSkins = os::Byteswap::byteswap(header.offsetSkins); |
141 | header.offsetTexcoords = os::Byteswap::byteswap(header.offsetTexcoords); | 141 | header.offsetTexcoords = os::Byteswap::byteswap(header.offsetTexcoords); |
142 | header.offsetTriangles = os::Byteswap::byteswap(header.offsetTriangles); | 142 | header.offsetTriangles = os::Byteswap::byteswap(header.offsetTriangles); |
143 | header.offsetFrames = os::Byteswap::byteswap(header.offsetFrames); | 143 | header.offsetFrames = os::Byteswap::byteswap(header.offsetFrames); |
144 | header.offsetGlCommands = os::Byteswap::byteswap(header.offsetGlCommands); | 144 | header.offsetGlCommands = os::Byteswap::byteswap(header.offsetGlCommands); |
145 | header.offsetEnd = os::Byteswap::byteswap(header.offsetEnd); | 145 | header.offsetEnd = os::Byteswap::byteswap(header.offsetEnd); |
146 | #endif | 146 | #endif |
147 | 147 | ||
148 | if (header.magic != MD2_MAGIC_NUMBER || header.version != MD2_VERSION) | 148 | if (header.magic != MD2_MAGIC_NUMBER || header.version != MD2_VERSION) |
149 | { | 149 | { |
150 | os::Printer::log("MD2 Loader: Wrong file header", file->getFileName(), ELL_WARNING); | 150 | os::Printer::log("MD2 Loader: Wrong file header", file->getFileName(), ELL_WARNING); |
151 | return false; | 151 | return false; |
152 | } | 152 | } |
153 | 153 | ||
154 | // | 154 | // |
155 | // prepare mesh and allocate memory | 155 | // prepare mesh and allocate memory |
156 | // | 156 | // |
157 | 157 | ||
158 | mesh->FrameCount = header.numFrames; | 158 | mesh->FrameCount = header.numFrames; |
159 | 159 | ||
160 | // create keyframes | 160 | // create keyframes |
161 | mesh->FrameTransforms.set_used(header.numFrames); | 161 | mesh->FrameTransforms.set_used(header.numFrames); |
162 | 162 | ||
163 | // create vertex arrays for each keyframe | 163 | // create vertex arrays for each keyframe |
164 | if (mesh->FrameList) | 164 | if (mesh->FrameList) |
165 | delete [] mesh->FrameList; | 165 | delete [] mesh->FrameList; |
166 | mesh->FrameList = new core::array<CAnimatedMeshMD2::SMD2Vert>[header.numFrames]; | 166 | mesh->FrameList = new core::array<CAnimatedMeshMD2::SMD2Vert>[header.numFrames]; |
167 | 167 | ||
168 | // allocate space in vertex arrays | 168 | // allocate space in vertex arrays |
169 | s32 i; | 169 | s32 i; |
170 | for (i=0; i<header.numFrames; ++i) | 170 | for (i=0; i<header.numFrames; ++i) |
171 | mesh->FrameList[i].reallocate(header.numVertices); | 171 | mesh->FrameList[i].reallocate(header.numVertices); |
172 | 172 | ||
173 | // allocate interpolation buffer vertices | 173 | // allocate interpolation buffer vertices |
174 | mesh->InterpolationBuffer->Vertices.set_used(header.numTriangles*3); | 174 | mesh->InterpolationBuffer->Vertices.set_used(header.numTriangles*3); |
175 | 175 | ||
176 | // populate triangles | 176 | // populate triangles |
177 | mesh->InterpolationBuffer->Indices.reallocate(header.numTriangles*3); | 177 | mesh->InterpolationBuffer->Indices.reallocate(header.numTriangles*3); |
178 | const s32 count = header.numTriangles*3; | 178 | const s32 count = header.numTriangles*3; |
179 | for (i=0; i<count; i+=3) | 179 | for (i=0; i<count; i+=3) |
180 | { | 180 | { |
181 | mesh->InterpolationBuffer->Indices.push_back(i); | 181 | mesh->InterpolationBuffer->Indices.push_back(i); |
182 | mesh->InterpolationBuffer->Indices.push_back(i+1); | 182 | mesh->InterpolationBuffer->Indices.push_back(i+1); |
183 | mesh->InterpolationBuffer->Indices.push_back(i+2); | 183 | mesh->InterpolationBuffer->Indices.push_back(i+2); |
184 | } | 184 | } |
185 | 185 | ||
186 | // | 186 | // |
187 | // read texture coordinates | 187 | // read texture coordinates |
188 | // | 188 | // |
189 | 189 | ||
190 | file->seek(header.offsetTexcoords); | 190 | file->seek(header.offsetTexcoords); |
191 | SMD2TextureCoordinate* textureCoords = new SMD2TextureCoordinate[header.numTexcoords]; | 191 | SMD2TextureCoordinate* textureCoords = new SMD2TextureCoordinate[header.numTexcoords]; |
192 | 192 | ||
193 | if (!file->read(textureCoords, sizeof(SMD2TextureCoordinate)*header.numTexcoords)) | 193 | if (!file->read(textureCoords, sizeof(SMD2TextureCoordinate)*header.numTexcoords)) |
194 | { | 194 | { |
195 | delete[] textureCoords; | 195 | delete[] textureCoords; |
196 | os::Printer::log("MD2 Loader: Error reading TextureCoords.", file->getFileName(), ELL_ERROR); | 196 | os::Printer::log("MD2 Loader: Error reading TextureCoords.", file->getFileName(), ELL_ERROR); |
197 | return false; | 197 | return false; |
198 | } | 198 | } |
199 | 199 | ||
200 | #ifdef __BIG_ENDIAN__ | 200 | #ifdef __BIG_ENDIAN__ |
201 | for (i=0; i<header.numTexcoords; ++i) | 201 | for (i=0; i<header.numTexcoords; ++i) |
202 | { | 202 | { |
203 | textureCoords[i].s = os::Byteswap::byteswap(textureCoords[i].s); | 203 | textureCoords[i].s = os::Byteswap::byteswap(textureCoords[i].s); |
204 | textureCoords[i].t = os::Byteswap::byteswap(textureCoords[i].t); | 204 | textureCoords[i].t = os::Byteswap::byteswap(textureCoords[i].t); |
205 | } | 205 | } |
206 | #endif | 206 | #endif |
207 | 207 | ||
208 | // read Triangles | 208 | // read Triangles |
209 | 209 | ||
210 | file->seek(header.offsetTriangles); | 210 | file->seek(header.offsetTriangles); |
211 | 211 | ||
212 | SMD2Triangle *triangles = new SMD2Triangle[header.numTriangles]; | 212 | SMD2Triangle *triangles = new SMD2Triangle[header.numTriangles]; |
213 | if (!file->read(triangles, header.numTriangles *sizeof(SMD2Triangle))) | 213 | if (!file->read(triangles, header.numTriangles *sizeof(SMD2Triangle))) |
214 | { | 214 | { |
215 | delete[] triangles; | 215 | delete[] triangles; |
216 | delete[] textureCoords; | 216 | delete[] textureCoords; |
217 | 217 | ||
218 | os::Printer::log("MD2 Loader: Error reading triangles.", file->getFileName(), ELL_ERROR); | 218 | os::Printer::log("MD2 Loader: Error reading triangles.", file->getFileName(), ELL_ERROR); |
219 | return false; | 219 | return false; |
220 | } | 220 | } |
221 | 221 | ||
222 | #ifdef __BIG_ENDIAN__ | 222 | #ifdef __BIG_ENDIAN__ |
223 | for (i=0; i<header.numTriangles; ++i) | 223 | for (i=0; i<header.numTriangles; ++i) |
224 | { | 224 | { |
225 | triangles[i].vertexIndices[0] = os::Byteswap::byteswap(triangles[i].vertexIndices[0]); | 225 | triangles[i].vertexIndices[0] = os::Byteswap::byteswap(triangles[i].vertexIndices[0]); |
226 | triangles[i].vertexIndices[1] = os::Byteswap::byteswap(triangles[i].vertexIndices[1]); | 226 | triangles[i].vertexIndices[1] = os::Byteswap::byteswap(triangles[i].vertexIndices[1]); |
227 | triangles[i].vertexIndices[2] = os::Byteswap::byteswap(triangles[i].vertexIndices[2]); | 227 | triangles[i].vertexIndices[2] = os::Byteswap::byteswap(triangles[i].vertexIndices[2]); |
228 | triangles[i].textureIndices[0] = os::Byteswap::byteswap(triangles[i].textureIndices[0]); | 228 | triangles[i].textureIndices[0] = os::Byteswap::byteswap(triangles[i].textureIndices[0]); |
229 | triangles[i].textureIndices[1] = os::Byteswap::byteswap(triangles[i].textureIndices[1]); | 229 | triangles[i].textureIndices[1] = os::Byteswap::byteswap(triangles[i].textureIndices[1]); |
230 | triangles[i].textureIndices[2] = os::Byteswap::byteswap(triangles[i].textureIndices[2]); | 230 | triangles[i].textureIndices[2] = os::Byteswap::byteswap(triangles[i].textureIndices[2]); |
231 | } | 231 | } |
232 | #endif | 232 | #endif |
233 | 233 | ||
234 | // read Vertices | 234 | // read Vertices |
235 | 235 | ||
236 | u8 buffer[MD2_MAX_VERTS*4+128]; | 236 | u8 buffer[MD2_MAX_VERTS*4+128]; |
237 | SMD2Frame* frame = (SMD2Frame*)buffer; | 237 | SMD2Frame* frame = (SMD2Frame*)buffer; |
238 | 238 | ||
239 | file->seek(header.offsetFrames); | 239 | file->seek(header.offsetFrames); |
240 | 240 | ||
241 | for (i = 0; i<header.numFrames; ++i) | 241 | for (i = 0; i<header.numFrames; ++i) |
242 | { | 242 | { |
243 | // read vertices | 243 | // read vertices |
244 | 244 | ||
245 | file->read(frame, header.frameSize); | 245 | file->read(frame, header.frameSize); |
246 | 246 | ||
247 | #ifdef __BIG_ENDIAN__ | 247 | #ifdef __BIG_ENDIAN__ |
248 | frame->scale[0] = os::Byteswap::byteswap(frame->scale[0]); | 248 | frame->scale[0] = os::Byteswap::byteswap(frame->scale[0]); |
249 | frame->scale[1] = os::Byteswap::byteswap(frame->scale[1]); | 249 | frame->scale[1] = os::Byteswap::byteswap(frame->scale[1]); |
250 | frame->scale[2] = os::Byteswap::byteswap(frame->scale[2]); | 250 | frame->scale[2] = os::Byteswap::byteswap(frame->scale[2]); |
251 | frame->translate[0] = os::Byteswap::byteswap(frame->translate[0]); | 251 | frame->translate[0] = os::Byteswap::byteswap(frame->translate[0]); |
252 | frame->translate[1] = os::Byteswap::byteswap(frame->translate[1]); | 252 | frame->translate[1] = os::Byteswap::byteswap(frame->translate[1]); |
253 | frame->translate[2] = os::Byteswap::byteswap(frame->translate[2]); | 253 | frame->translate[2] = os::Byteswap::byteswap(frame->translate[2]); |
254 | #endif | 254 | #endif |
255 | // | 255 | // |
256 | // store frame data | 256 | // store frame data |
257 | // | 257 | // |
258 | 258 | ||
259 | CAnimatedMeshMD2::SAnimationData adata; | 259 | CAnimatedMeshMD2::SAnimationData adata; |
260 | adata.begin = i; | 260 | adata.begin = i; |
261 | adata.end = i; | 261 | adata.end = i; |
262 | adata.fps = 7; | 262 | adata.fps = 7; |
263 | 263 | ||
264 | // Add new named animation if necessary | 264 | // Add new named animation if necessary |
265 | if (frame->name[0]) | 265 | if (frame->name[0]) |
266 | { | 266 | { |
267 | // get animation name | 267 | // get animation name |
268 | for (s32 s = 0; s < 16 && frame->name[s]!=0 && (frame->name[s] < '0' || frame->name[s] > '9'); ++s) | 268 | for (s32 s = 0; s < 16 && frame->name[s]!=0 && (frame->name[s] < '0' || frame->name[s] > '9'); ++s) |
269 | { | 269 | { |
270 | adata.name += frame->name[s]; | 270 | adata.name += frame->name[s]; |
271 | } | 271 | } |
272 | 272 | ||
273 | // Does this keyframe have the same animation name as the current animation? | 273 | // Does this keyframe have the same animation name as the current animation? |
274 | if (!mesh->AnimationData.empty() && mesh->AnimationData[mesh->AnimationData.size()-1].name == adata.name) | 274 | if (!mesh->AnimationData.empty() && mesh->AnimationData[mesh->AnimationData.size()-1].name == adata.name) |
275 | { | 275 | { |
276 | // Increase the length of the animation | 276 | // Increase the length of the animation |
277 | ++mesh->AnimationData[mesh->AnimationData.size() - 1].end; | 277 | ++mesh->AnimationData[mesh->AnimationData.size() - 1].end; |
278 | } | 278 | } |
279 | else | 279 | else |
280 | { | 280 | { |
281 | // Add the new animation | 281 | // Add the new animation |
282 | mesh->AnimationData.push_back(adata); | 282 | mesh->AnimationData.push_back(adata); |
283 | } | 283 | } |
284 | } | 284 | } |
285 | 285 | ||
286 | // save keyframe scale and translation | 286 | // save keyframe scale and translation |
287 | 287 | ||
288 | mesh->FrameTransforms[i].scale.X = frame->scale[0]; | 288 | mesh->FrameTransforms[i].scale.X = frame->scale[0]; |
289 | mesh->FrameTransforms[i].scale.Z = frame->scale[1]; | 289 | mesh->FrameTransforms[i].scale.Z = frame->scale[1]; |
290 | mesh->FrameTransforms[i].scale.Y = frame->scale[2]; | 290 | mesh->FrameTransforms[i].scale.Y = frame->scale[2]; |
291 | mesh->FrameTransforms[i].translate.X = frame->translate[0]; | 291 | mesh->FrameTransforms[i].translate.X = frame->translate[0]; |
292 | mesh->FrameTransforms[i].translate.Z = frame->translate[1]; | 292 | mesh->FrameTransforms[i].translate.Z = frame->translate[1]; |
293 | mesh->FrameTransforms[i].translate.Y = frame->translate[2]; | 293 | mesh->FrameTransforms[i].translate.Y = frame->translate[2]; |
294 | 294 | ||
295 | // add vertices | 295 | // add vertices |
296 | for (s32 j=0; j<header.numTriangles; ++j) | 296 | for (s32 j=0; j<header.numTriangles; ++j) |
297 | { | 297 | { |
298 | for (u32 ti=0; ti<3; ++ti) | 298 | for (u32 ti=0; ti<3; ++ti) |
299 | { | 299 | { |
300 | CAnimatedMeshMD2::SMD2Vert v; | 300 | CAnimatedMeshMD2::SMD2Vert v; |
301 | u32 num = triangles[j].vertexIndices[ti]; | 301 | u32 num = triangles[j].vertexIndices[ti]; |
302 | v.Pos.X = frame->vertices[num].vertex[0]; | 302 | v.Pos.X = frame->vertices[num].vertex[0]; |
303 | v.Pos.Z = frame->vertices[num].vertex[1]; | 303 | v.Pos.Z = frame->vertices[num].vertex[1]; |
304 | v.Pos.Y = frame->vertices[num].vertex[2]; | 304 | v.Pos.Y = frame->vertices[num].vertex[2]; |
305 | v.NormalIdx = frame->vertices[num].lightNormalIndex; | 305 | v.NormalIdx = frame->vertices[num].lightNormalIndex; |
306 | 306 | ||
307 | mesh->FrameList[i].push_back(v); | 307 | mesh->FrameList[i].push_back(v); |
308 | } | 308 | } |
309 | } | 309 | } |
310 | 310 | ||
311 | // calculate bounding boxes | 311 | // calculate bounding boxes |
312 | if (header.numVertices) | 312 | if (header.numVertices) |
313 | { | 313 | { |
314 | core::aabbox3d<f32> box; | 314 | core::aabbox3d<f32> box; |
315 | core::vector3df pos; | 315 | core::vector3df pos; |
316 | pos.X = f32(mesh->FrameList[i] [0].Pos.X) * mesh->FrameTransforms[i].scale.X + mesh->FrameTransforms[i].translate.X; | 316 | pos.X = f32(mesh->FrameList[i] [0].Pos.X) * mesh->FrameTransforms[i].scale.X + mesh->FrameTransforms[i].translate.X; |
317 | pos.Y = f32(mesh->FrameList[i] [0].Pos.Y) * mesh->FrameTransforms[i].scale.Y + mesh->FrameTransforms[i].translate.Y; | 317 | pos.Y = f32(mesh->FrameList[i] [0].Pos.Y) * mesh->FrameTransforms[i].scale.Y + mesh->FrameTransforms[i].translate.Y; |
318 | pos.Z = f32(mesh->FrameList[i] [0].Pos.Z) * mesh->FrameTransforms[i].scale.Z + mesh->FrameTransforms[i].translate.Z; | 318 | pos.Z = f32(mesh->FrameList[i] [0].Pos.Z) * mesh->FrameTransforms[i].scale.Z + mesh->FrameTransforms[i].translate.Z; |
319 | 319 | ||
320 | box.reset(pos); | 320 | box.reset(pos); |
321 | 321 | ||
322 | for (s32 j=1; j<header.numTriangles*3; ++j) | 322 | for (s32 j=1; j<header.numTriangles*3; ++j) |
323 | { | 323 | { |
324 | pos.X = f32(mesh->FrameList[i] [j].Pos.X) * mesh->FrameTransforms[i].scale.X + mesh->FrameTransforms[i].translate.X; | 324 | pos.X = f32(mesh->FrameList[i] [j].Pos.X) * mesh->FrameTransforms[i].scale.X + mesh->FrameTransforms[i].translate.X; |
325 | pos.Y = f32(mesh->FrameList[i] [j].Pos.Y) * mesh->FrameTransforms[i].scale.Y + mesh->FrameTransforms[i].translate.Y; | 325 | pos.Y = f32(mesh->FrameList[i] [j].Pos.Y) * mesh->FrameTransforms[i].scale.Y + mesh->FrameTransforms[i].translate.Y; |
326 | pos.Z = f32(mesh->FrameList[i] [j].Pos.Z) * mesh->FrameTransforms[i].scale.Z + mesh->FrameTransforms[i].translate.Z; | 326 | pos.Z = f32(mesh->FrameList[i] [j].Pos.Z) * mesh->FrameTransforms[i].scale.Z + mesh->FrameTransforms[i].translate.Z; |
327 | 327 | ||
328 | box.addInternalPoint(pos); | 328 | box.addInternalPoint(pos); |
329 | } | 329 | } |
330 | mesh->BoxList.push_back(box); | 330 | mesh->BoxList.push_back(box); |
331 | } | 331 | } |
332 | } | 332 | } |
333 | 333 | ||
334 | // populate interpolation buffer with texture coordinates and colors | 334 | // populate interpolation buffer with texture coordinates and colors |
335 | if (header.numFrames) | 335 | if (header.numFrames) |
336 | { | 336 | { |
337 | f32 dmaxs = 1.0f/(header.skinWidth); | 337 | f32 dmaxs = 1.0f/(header.skinWidth); |
338 | f32 dmaxt = 1.0f/(header.skinHeight); | 338 | f32 dmaxt = 1.0f/(header.skinHeight); |
339 | 339 | ||
340 | for (s32 t=0; t<header.numTriangles; ++t) | 340 | for (s32 t=0; t<header.numTriangles; ++t) |
341 | { | 341 | { |
342 | for (s32 n=0; n<3; ++n) | 342 | for (s32 n=0; n<3; ++n) |
343 | { | 343 | { |
344 | mesh->InterpolationBuffer->Vertices[t*3 + n].TCoords.X = (textureCoords[triangles[t].textureIndices[n]].s + 0.5f) * dmaxs; | 344 | mesh->InterpolationBuffer->Vertices[t*3 + n].TCoords.X = (textureCoords[triangles[t].textureIndices[n]].s + 0.5f) * dmaxs; |
345 | mesh->InterpolationBuffer->Vertices[t*3 + n].TCoords.Y = (textureCoords[triangles[t].textureIndices[n]].t + 0.5f) * dmaxt; | 345 | mesh->InterpolationBuffer->Vertices[t*3 + n].TCoords.Y = (textureCoords[triangles[t].textureIndices[n]].t + 0.5f) * dmaxt; |
346 | mesh->InterpolationBuffer->Vertices[t*3 + n].Color = video::SColor(255,255,255,255); | 346 | mesh->InterpolationBuffer->Vertices[t*3 + n].Color = video::SColor(255,255,255,255); |
347 | } | 347 | } |
348 | } | 348 | } |
349 | } | 349 | } |
350 | 350 | ||
351 | // clean up | 351 | // clean up |
352 | delete [] triangles; | 352 | delete [] triangles; |
353 | delete [] textureCoords; | 353 | delete [] textureCoords; |
354 | 354 | ||
355 | // init buffer with start frame. | 355 | // init buffer with start frame. |
356 | mesh->getMesh(0); | 356 | mesh->getMesh(0); |
357 | return true; | 357 | return true; |
358 | } | 358 | } |
359 | 359 | ||
360 | } // end namespace scene | 360 | } // end namespace scene |
361 | } // end namespace irr | 361 | } // end namespace irr |
362 | 362 | ||
363 | 363 | ||
364 | #endif // _IRR_COMPILE_WITH_MD2_LOADER_ | 364 | #endif // _IRR_COMPILE_WITH_MD2_LOADER_ |