rev |
line source |
nuclear@0
|
1 /*
|
nuclear@0
|
2 ---------------------------------------------------------------------------
|
nuclear@0
|
3 Open Asset Import Library (assimp)
|
nuclear@0
|
4 ---------------------------------------------------------------------------
|
nuclear@0
|
5
|
nuclear@0
|
6 Copyright (c) 2006-2012, assimp team
|
nuclear@0
|
7
|
nuclear@0
|
8 All rights reserved.
|
nuclear@0
|
9
|
nuclear@0
|
10 Redistribution and use of this software in source and binary forms,
|
nuclear@0
|
11 with or without modification, are permitted provided that the following
|
nuclear@0
|
12 conditions are met:
|
nuclear@0
|
13
|
nuclear@0
|
14 * Redistributions of source code must retain the above
|
nuclear@0
|
15 copyright notice, this list of conditions and the
|
nuclear@0
|
16 following disclaimer.
|
nuclear@0
|
17
|
nuclear@0
|
18 * Redistributions in binary form must reproduce the above
|
nuclear@0
|
19 copyright notice, this list of conditions and the
|
nuclear@0
|
20 following disclaimer in the documentation and/or other
|
nuclear@0
|
21 materials provided with the distribution.
|
nuclear@0
|
22
|
nuclear@0
|
23 * Neither the name of the assimp team, nor the names of its
|
nuclear@0
|
24 contributors may be used to endorse or promote products
|
nuclear@0
|
25 derived from this software without specific prior
|
nuclear@0
|
26 written permission of the assimp team.
|
nuclear@0
|
27
|
nuclear@0
|
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
nuclear@0
|
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
nuclear@0
|
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
nuclear@0
|
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
nuclear@0
|
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
nuclear@0
|
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
nuclear@0
|
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
nuclear@0
|
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
nuclear@0
|
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
nuclear@0
|
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
nuclear@0
|
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
nuclear@0
|
39 ---------------------------------------------------------------------------
|
nuclear@0
|
40 */
|
nuclear@0
|
41
|
nuclear@0
|
42 #include "AssimpPCH.h"
|
nuclear@0
|
43 #ifndef ASSIMP_BUILD_NO_MD2_IMPORTER
|
nuclear@0
|
44
|
nuclear@0
|
45 /** @file Implementation of the MD2 importer class */
|
nuclear@0
|
46 #include "MD2Loader.h"
|
nuclear@0
|
47 #include "ByteSwap.h"
|
nuclear@0
|
48 #include "MD2NormalTable.h" // shouldn't be included by other units
|
nuclear@0
|
49
|
nuclear@0
|
50 using namespace Assimp;
|
nuclear@0
|
51 using namespace Assimp::MD2;
|
nuclear@0
|
52
|
nuclear@0
|
53
|
nuclear@0
|
54 // helper macro to determine the size of an array
|
nuclear@0
|
55 #if (!defined ARRAYSIZE)
|
nuclear@0
|
56 # define ARRAYSIZE(_array) (int(sizeof(_array) / sizeof(_array[0])))
|
nuclear@0
|
57 #endif
|
nuclear@0
|
58
|
nuclear@0
|
59 static const aiImporterDesc desc = {
|
nuclear@0
|
60 "Quake II Mesh Importer",
|
nuclear@0
|
61 "",
|
nuclear@0
|
62 "",
|
nuclear@0
|
63 "",
|
nuclear@0
|
64 aiImporterFlags_SupportBinaryFlavour,
|
nuclear@0
|
65 0,
|
nuclear@0
|
66 0,
|
nuclear@0
|
67 0,
|
nuclear@0
|
68 0,
|
nuclear@0
|
69 "md2"
|
nuclear@0
|
70 };
|
nuclear@0
|
71
|
nuclear@0
|
72 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
73 // Helper function to lookup a normal in Quake 2's precalculated table
|
nuclear@0
|
74 void MD2::LookupNormalIndex(uint8_t iNormalIndex,aiVector3D& vOut)
|
nuclear@0
|
75 {
|
nuclear@0
|
76 // make sure the normal index has a valid value
|
nuclear@0
|
77 if (iNormalIndex >= ARRAYSIZE(g_avNormals)) {
|
nuclear@0
|
78 DefaultLogger::get()->warn("Index overflow in Quake II normal vector list");
|
nuclear@0
|
79 iNormalIndex = ARRAYSIZE(g_avNormals) - 1;
|
nuclear@0
|
80 }
|
nuclear@0
|
81 vOut = *((const aiVector3D*)(&g_avNormals[iNormalIndex]));
|
nuclear@0
|
82 }
|
nuclear@0
|
83
|
nuclear@0
|
84
|
nuclear@0
|
85 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
86 // Constructor to be privately used by Importer
|
nuclear@0
|
87 MD2Importer::MD2Importer()
|
nuclear@0
|
88 {}
|
nuclear@0
|
89
|
nuclear@0
|
90 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
91 // Destructor, private as well
|
nuclear@0
|
92 MD2Importer::~MD2Importer()
|
nuclear@0
|
93 {}
|
nuclear@0
|
94
|
nuclear@0
|
95 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
96 // Returns whether the class can handle the format of the given file.
|
nuclear@0
|
97 bool MD2Importer::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
|
nuclear@0
|
98 {
|
nuclear@0
|
99 const std::string extension = GetExtension(pFile);
|
nuclear@0
|
100 if (extension == "md2")
|
nuclear@0
|
101 return true;
|
nuclear@0
|
102
|
nuclear@0
|
103 // if check for extension is not enough, check for the magic tokens
|
nuclear@0
|
104 if (!extension.length() || checkSig) {
|
nuclear@0
|
105 uint32_t tokens[1];
|
nuclear@0
|
106 tokens[0] = AI_MD2_MAGIC_NUMBER_LE;
|
nuclear@0
|
107 return CheckMagicToken(pIOHandler,pFile,tokens,1);
|
nuclear@0
|
108 }
|
nuclear@0
|
109 return false;
|
nuclear@0
|
110 }
|
nuclear@0
|
111
|
nuclear@0
|
112 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
113 // Get a list of all extensions supported by this loader
|
nuclear@0
|
114 const aiImporterDesc* MD2Importer::GetInfo () const
|
nuclear@0
|
115 {
|
nuclear@0
|
116 return &desc;
|
nuclear@0
|
117 }
|
nuclear@0
|
118
|
nuclear@0
|
119 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
120 // Setup configuration properties
|
nuclear@0
|
121 void MD2Importer::SetupProperties(const Importer* pImp)
|
nuclear@0
|
122 {
|
nuclear@0
|
123 // The
|
nuclear@0
|
124 // AI_CONFIG_IMPORT_MD2_KEYFRAME option overrides the
|
nuclear@0
|
125 // AI_CONFIG_IMPORT_GLOBAL_KEYFRAME option.
|
nuclear@0
|
126 configFrameID = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_MD2_KEYFRAME,-1);
|
nuclear@0
|
127 if(static_cast<unsigned int>(-1) == configFrameID){
|
nuclear@0
|
128 configFrameID = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_GLOBAL_KEYFRAME,0);
|
nuclear@0
|
129 }
|
nuclear@0
|
130 }
|
nuclear@0
|
131 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
132 // Validate the file header
|
nuclear@0
|
133 void MD2Importer::ValidateHeader( )
|
nuclear@0
|
134 {
|
nuclear@0
|
135 // check magic number
|
nuclear@0
|
136 if (m_pcHeader->magic != AI_MD2_MAGIC_NUMBER_BE &&
|
nuclear@0
|
137 m_pcHeader->magic != AI_MD2_MAGIC_NUMBER_LE)
|
nuclear@0
|
138 {
|
nuclear@0
|
139 char szBuffer[5];
|
nuclear@0
|
140 szBuffer[0] = ((char*)&m_pcHeader->magic)[0];
|
nuclear@0
|
141 szBuffer[1] = ((char*)&m_pcHeader->magic)[1];
|
nuclear@0
|
142 szBuffer[2] = ((char*)&m_pcHeader->magic)[2];
|
nuclear@0
|
143 szBuffer[3] = ((char*)&m_pcHeader->magic)[3];
|
nuclear@0
|
144 szBuffer[4] = '\0';
|
nuclear@0
|
145
|
nuclear@0
|
146 throw DeadlyImportError("Invalid MD2 magic word: should be IDP2, the "
|
nuclear@0
|
147 "magic word found is " + std::string(szBuffer));
|
nuclear@0
|
148 }
|
nuclear@0
|
149
|
nuclear@0
|
150 // check file format version
|
nuclear@0
|
151 if (m_pcHeader->version != 8)
|
nuclear@0
|
152 DefaultLogger::get()->warn( "Unsupported md2 file version. Continuing happily ...");
|
nuclear@0
|
153
|
nuclear@0
|
154 // check some values whether they are valid
|
nuclear@0
|
155 if (0 == m_pcHeader->numFrames)
|
nuclear@0
|
156 throw DeadlyImportError( "Invalid md2 file: NUM_FRAMES is 0");
|
nuclear@0
|
157
|
nuclear@0
|
158 if (m_pcHeader->offsetEnd > (uint32_t)fileSize)
|
nuclear@0
|
159 throw DeadlyImportError( "Invalid md2 file: File is too small");
|
nuclear@0
|
160
|
nuclear@0
|
161 if (m_pcHeader->offsetSkins + m_pcHeader->numSkins * sizeof (MD2::Skin) >= fileSize ||
|
nuclear@0
|
162 m_pcHeader->offsetTexCoords + m_pcHeader->numTexCoords * sizeof (MD2::TexCoord) >= fileSize ||
|
nuclear@0
|
163 m_pcHeader->offsetTriangles + m_pcHeader->numTriangles * sizeof (MD2::Triangle) >= fileSize ||
|
nuclear@0
|
164 m_pcHeader->offsetFrames + m_pcHeader->numFrames * sizeof (MD2::Frame) >= fileSize ||
|
nuclear@0
|
165 m_pcHeader->offsetEnd > fileSize)
|
nuclear@0
|
166 {
|
nuclear@0
|
167 throw DeadlyImportError("Invalid MD2 header: some offsets are outside the file");
|
nuclear@0
|
168 }
|
nuclear@0
|
169
|
nuclear@0
|
170 if (m_pcHeader->numSkins > AI_MD2_MAX_SKINS)
|
nuclear@0
|
171 DefaultLogger::get()->warn("The model contains more skins than Quake 2 supports");
|
nuclear@0
|
172 if ( m_pcHeader->numFrames > AI_MD2_MAX_FRAMES)
|
nuclear@0
|
173 DefaultLogger::get()->warn("The model contains more frames than Quake 2 supports");
|
nuclear@0
|
174 if (m_pcHeader->numVertices > AI_MD2_MAX_VERTS)
|
nuclear@0
|
175 DefaultLogger::get()->warn("The model contains more vertices than Quake 2 supports");
|
nuclear@0
|
176
|
nuclear@0
|
177 if (m_pcHeader->numFrames <= configFrameID )
|
nuclear@0
|
178 throw DeadlyImportError("The requested frame is not existing the file");
|
nuclear@0
|
179 }
|
nuclear@0
|
180
|
nuclear@0
|
181 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
182 // Imports the given file into the given scene structure.
|
nuclear@0
|
183 void MD2Importer::InternReadFile( const std::string& pFile,
|
nuclear@0
|
184 aiScene* pScene, IOSystem* pIOHandler)
|
nuclear@0
|
185 {
|
nuclear@0
|
186 boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
|
nuclear@0
|
187
|
nuclear@0
|
188 // Check whether we can read from the file
|
nuclear@0
|
189 if( file.get() == NULL)
|
nuclear@0
|
190 throw DeadlyImportError( "Failed to open MD2 file " + pFile + "");
|
nuclear@0
|
191
|
nuclear@0
|
192 // check whether the md3 file is large enough to contain
|
nuclear@0
|
193 // at least the file header
|
nuclear@0
|
194 fileSize = (unsigned int)file->FileSize();
|
nuclear@0
|
195 if( fileSize < sizeof(MD2::Header))
|
nuclear@0
|
196 throw DeadlyImportError( "MD2 File is too small");
|
nuclear@0
|
197
|
nuclear@0
|
198 std::vector<uint8_t> mBuffer2(fileSize);
|
nuclear@0
|
199 file->Read(&mBuffer2[0], 1, fileSize);
|
nuclear@0
|
200 mBuffer = &mBuffer2[0];
|
nuclear@0
|
201
|
nuclear@0
|
202
|
nuclear@0
|
203 m_pcHeader = (BE_NCONST MD2::Header*)mBuffer;
|
nuclear@0
|
204
|
nuclear@0
|
205 #ifdef AI_BUILD_BIG_ENDIAN
|
nuclear@0
|
206
|
nuclear@0
|
207 ByteSwap::Swap4(&m_pcHeader->frameSize);
|
nuclear@0
|
208 ByteSwap::Swap4(&m_pcHeader->magic);
|
nuclear@0
|
209 ByteSwap::Swap4(&m_pcHeader->numFrames);
|
nuclear@0
|
210 ByteSwap::Swap4(&m_pcHeader->numGlCommands);
|
nuclear@0
|
211 ByteSwap::Swap4(&m_pcHeader->numSkins);
|
nuclear@0
|
212 ByteSwap::Swap4(&m_pcHeader->numTexCoords);
|
nuclear@0
|
213 ByteSwap::Swap4(&m_pcHeader->numTriangles);
|
nuclear@0
|
214 ByteSwap::Swap4(&m_pcHeader->numVertices);
|
nuclear@0
|
215 ByteSwap::Swap4(&m_pcHeader->offsetEnd);
|
nuclear@0
|
216 ByteSwap::Swap4(&m_pcHeader->offsetFrames);
|
nuclear@0
|
217 ByteSwap::Swap4(&m_pcHeader->offsetGlCommands);
|
nuclear@0
|
218 ByteSwap::Swap4(&m_pcHeader->offsetSkins);
|
nuclear@0
|
219 ByteSwap::Swap4(&m_pcHeader->offsetTexCoords);
|
nuclear@0
|
220 ByteSwap::Swap4(&m_pcHeader->offsetTriangles);
|
nuclear@0
|
221 ByteSwap::Swap4(&m_pcHeader->skinHeight);
|
nuclear@0
|
222 ByteSwap::Swap4(&m_pcHeader->skinWidth);
|
nuclear@0
|
223 ByteSwap::Swap4(&m_pcHeader->version);
|
nuclear@0
|
224
|
nuclear@0
|
225 #endif
|
nuclear@0
|
226
|
nuclear@0
|
227 ValidateHeader();
|
nuclear@0
|
228
|
nuclear@0
|
229 // there won't be more than one mesh inside the file
|
nuclear@0
|
230 pScene->mNumMaterials = 1;
|
nuclear@0
|
231 pScene->mRootNode = new aiNode();
|
nuclear@0
|
232 pScene->mRootNode->mNumMeshes = 1;
|
nuclear@0
|
233 pScene->mRootNode->mMeshes = new unsigned int[1];
|
nuclear@0
|
234 pScene->mRootNode->mMeshes[0] = 0;
|
nuclear@0
|
235 pScene->mMaterials = new aiMaterial*[1];
|
nuclear@0
|
236 pScene->mMaterials[0] = new aiMaterial();
|
nuclear@0
|
237 pScene->mNumMeshes = 1;
|
nuclear@0
|
238 pScene->mMeshes = new aiMesh*[1];
|
nuclear@0
|
239
|
nuclear@0
|
240 aiMesh* pcMesh = pScene->mMeshes[0] = new aiMesh();
|
nuclear@0
|
241 pcMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
|
nuclear@0
|
242
|
nuclear@0
|
243 // navigate to the begin of the frame data
|
nuclear@0
|
244 BE_NCONST MD2::Frame* pcFrame = (BE_NCONST MD2::Frame*) ((uint8_t*)
|
nuclear@0
|
245 m_pcHeader + m_pcHeader->offsetFrames);
|
nuclear@0
|
246
|
nuclear@0
|
247 pcFrame += configFrameID;
|
nuclear@0
|
248
|
nuclear@0
|
249 // navigate to the begin of the triangle data
|
nuclear@0
|
250 MD2::Triangle* pcTriangles = (MD2::Triangle*) ((uint8_t*)
|
nuclear@0
|
251 m_pcHeader + m_pcHeader->offsetTriangles);
|
nuclear@0
|
252
|
nuclear@0
|
253 // navigate to the begin of the tex coords data
|
nuclear@0
|
254 BE_NCONST MD2::TexCoord* pcTexCoords = (BE_NCONST MD2::TexCoord*) ((uint8_t*)
|
nuclear@0
|
255 m_pcHeader + m_pcHeader->offsetTexCoords);
|
nuclear@0
|
256
|
nuclear@0
|
257 // navigate to the begin of the vertex data
|
nuclear@0
|
258 BE_NCONST MD2::Vertex* pcVerts = (BE_NCONST MD2::Vertex*) (pcFrame->vertices);
|
nuclear@0
|
259
|
nuclear@0
|
260 #ifdef AI_BUILD_BIG_ENDIAN
|
nuclear@0
|
261 for (uint32_t i = 0; i< m_pcHeader->numTriangles; ++i)
|
nuclear@0
|
262 {
|
nuclear@0
|
263 for (unsigned int p = 0; p < 3;++p)
|
nuclear@0
|
264 {
|
nuclear@0
|
265 ByteSwap::Swap2(& pcTriangles[i].textureIndices[p]);
|
nuclear@0
|
266 ByteSwap::Swap2(& pcTriangles[i].vertexIndices[p]);
|
nuclear@0
|
267 }
|
nuclear@0
|
268 }
|
nuclear@0
|
269 for (uint32_t i = 0; i < m_pcHeader->offsetTexCoords;++i)
|
nuclear@0
|
270 {
|
nuclear@0
|
271 ByteSwap::Swap2(& pcTexCoords[i].s);
|
nuclear@0
|
272 ByteSwap::Swap2(& pcTexCoords[i].t);
|
nuclear@0
|
273 }
|
nuclear@0
|
274 ByteSwap::Swap4( & pcFrame->scale[0] );
|
nuclear@0
|
275 ByteSwap::Swap4( & pcFrame->scale[1] );
|
nuclear@0
|
276 ByteSwap::Swap4( & pcFrame->scale[2] );
|
nuclear@0
|
277 ByteSwap::Swap4( & pcFrame->translate[0] );
|
nuclear@0
|
278 ByteSwap::Swap4( & pcFrame->translate[1] );
|
nuclear@0
|
279 ByteSwap::Swap4( & pcFrame->translate[2] );
|
nuclear@0
|
280 #endif
|
nuclear@0
|
281
|
nuclear@0
|
282 pcMesh->mNumFaces = m_pcHeader->numTriangles;
|
nuclear@0
|
283 pcMesh->mFaces = new aiFace[m_pcHeader->numTriangles];
|
nuclear@0
|
284
|
nuclear@0
|
285 // allocate output storage
|
nuclear@0
|
286 pcMesh->mNumVertices = (unsigned int)pcMesh->mNumFaces*3;
|
nuclear@0
|
287 pcMesh->mVertices = new aiVector3D[pcMesh->mNumVertices];
|
nuclear@0
|
288 pcMesh->mNormals = new aiVector3D[pcMesh->mNumVertices];
|
nuclear@0
|
289
|
nuclear@0
|
290 // Not sure whether there are MD2 files without texture coordinates
|
nuclear@0
|
291 // NOTE: texture coordinates can be there without a texture,
|
nuclear@0
|
292 // but a texture can't be there without a valid UV channel
|
nuclear@0
|
293 aiMaterial* pcHelper = (aiMaterial*)pScene->mMaterials[0];
|
nuclear@0
|
294 const int iMode = (int)aiShadingMode_Gouraud;
|
nuclear@0
|
295 pcHelper->AddProperty<int>(&iMode, 1, AI_MATKEY_SHADING_MODEL);
|
nuclear@0
|
296
|
nuclear@0
|
297 if (m_pcHeader->numTexCoords && m_pcHeader->numSkins)
|
nuclear@0
|
298 {
|
nuclear@0
|
299 // navigate to the first texture associated with the mesh
|
nuclear@0
|
300 const MD2::Skin* pcSkins = (const MD2::Skin*) ((unsigned char*)m_pcHeader +
|
nuclear@0
|
301 m_pcHeader->offsetSkins);
|
nuclear@0
|
302
|
nuclear@0
|
303 aiColor3D clr;
|
nuclear@0
|
304 clr.b = clr.g = clr.r = 1.0f;
|
nuclear@0
|
305 pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_DIFFUSE);
|
nuclear@0
|
306 pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_SPECULAR);
|
nuclear@0
|
307
|
nuclear@0
|
308 clr.b = clr.g = clr.r = 0.05f;
|
nuclear@0
|
309 pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_AMBIENT);
|
nuclear@0
|
310
|
nuclear@0
|
311 if (pcSkins->name[0])
|
nuclear@0
|
312 {
|
nuclear@0
|
313 aiString szString;
|
nuclear@0
|
314 const size_t iLen = ::strlen(pcSkins->name);
|
nuclear@0
|
315 ::memcpy(szString.data,pcSkins->name,iLen);
|
nuclear@0
|
316 szString.data[iLen] = '\0';
|
nuclear@0
|
317 szString.length = iLen;
|
nuclear@0
|
318
|
nuclear@0
|
319 pcHelper->AddProperty(&szString,AI_MATKEY_TEXTURE_DIFFUSE(0));
|
nuclear@0
|
320 }
|
nuclear@0
|
321 else{
|
nuclear@0
|
322 DefaultLogger::get()->warn("Texture file name has zero length. It will be skipped.");
|
nuclear@0
|
323 }
|
nuclear@0
|
324 }
|
nuclear@0
|
325 else {
|
nuclear@0
|
326 // apply a default material
|
nuclear@0
|
327 aiColor3D clr;
|
nuclear@0
|
328 clr.b = clr.g = clr.r = 0.6f;
|
nuclear@0
|
329 pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_DIFFUSE);
|
nuclear@0
|
330 pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_SPECULAR);
|
nuclear@0
|
331
|
nuclear@0
|
332 clr.b = clr.g = clr.r = 0.05f;
|
nuclear@0
|
333 pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_AMBIENT);
|
nuclear@0
|
334
|
nuclear@0
|
335 aiString szName;
|
nuclear@0
|
336 szName.Set(AI_DEFAULT_MATERIAL_NAME);
|
nuclear@0
|
337 pcHelper->AddProperty(&szName,AI_MATKEY_NAME);
|
nuclear@0
|
338
|
nuclear@0
|
339 aiString sz;
|
nuclear@0
|
340
|
nuclear@0
|
341 // TODO: Try to guess the name of the texture file from the model file name
|
nuclear@0
|
342
|
nuclear@0
|
343 sz.Set("$texture_dummy.bmp");
|
nuclear@0
|
344 pcHelper->AddProperty(&sz,AI_MATKEY_TEXTURE_DIFFUSE(0));
|
nuclear@0
|
345 }
|
nuclear@0
|
346
|
nuclear@0
|
347
|
nuclear@0
|
348 // now read all triangles of the first frame, apply scaling and translation
|
nuclear@0
|
349 unsigned int iCurrent = 0;
|
nuclear@0
|
350
|
nuclear@0
|
351 float fDivisorU = 1.0f,fDivisorV = 1.0f;
|
nuclear@0
|
352 if (m_pcHeader->numTexCoords) {
|
nuclear@0
|
353 // allocate storage for texture coordinates, too
|
nuclear@0
|
354 pcMesh->mTextureCoords[0] = new aiVector3D[pcMesh->mNumVertices];
|
nuclear@0
|
355 pcMesh->mNumUVComponents[0] = 2;
|
nuclear@0
|
356
|
nuclear@0
|
357 // check whether the skin width or height are zero (this would
|
nuclear@0
|
358 // cause a division through zero)
|
nuclear@0
|
359 if (!m_pcHeader->skinWidth) {
|
nuclear@0
|
360 DefaultLogger::get()->error("MD2: No valid skin width given");
|
nuclear@0
|
361 }
|
nuclear@0
|
362 else fDivisorU = (float)m_pcHeader->skinWidth;
|
nuclear@0
|
363 if (!m_pcHeader->skinHeight){
|
nuclear@0
|
364 DefaultLogger::get()->error("MD2: No valid skin height given");
|
nuclear@0
|
365 }
|
nuclear@0
|
366 else fDivisorV = (float)m_pcHeader->skinHeight;
|
nuclear@0
|
367 }
|
nuclear@0
|
368
|
nuclear@0
|
369 for (unsigned int i = 0; i < (unsigned int)m_pcHeader->numTriangles;++i) {
|
nuclear@0
|
370 // Allocate the face
|
nuclear@0
|
371 pScene->mMeshes[0]->mFaces[i].mIndices = new unsigned int[3];
|
nuclear@0
|
372 pScene->mMeshes[0]->mFaces[i].mNumIndices = 3;
|
nuclear@0
|
373
|
nuclear@0
|
374 // copy texture coordinates
|
nuclear@0
|
375 // check whether they are different from the previous value at this index.
|
nuclear@0
|
376 // In this case, create a full separate set of vertices/normals/texcoords
|
nuclear@0
|
377 for (unsigned int c = 0; c < 3;++c,++iCurrent) {
|
nuclear@0
|
378
|
nuclear@0
|
379 // validate vertex indices
|
nuclear@0
|
380 register unsigned int iIndex = (unsigned int)pcTriangles[i].vertexIndices[c];
|
nuclear@0
|
381 if (iIndex >= m_pcHeader->numVertices) {
|
nuclear@0
|
382 DefaultLogger::get()->error("MD2: Vertex index is outside the allowed range");
|
nuclear@0
|
383 iIndex = m_pcHeader->numVertices-1;
|
nuclear@0
|
384 }
|
nuclear@0
|
385
|
nuclear@0
|
386 // read x,y, and z component of the vertex
|
nuclear@0
|
387 aiVector3D& vec = pcMesh->mVertices[iCurrent];
|
nuclear@0
|
388
|
nuclear@0
|
389 vec.x = (float)pcVerts[iIndex].vertex[0] * pcFrame->scale[0];
|
nuclear@0
|
390 vec.x += pcFrame->translate[0];
|
nuclear@0
|
391
|
nuclear@0
|
392 vec.y = (float)pcVerts[iIndex].vertex[1] * pcFrame->scale[1];
|
nuclear@0
|
393 vec.y += pcFrame->translate[1];
|
nuclear@0
|
394
|
nuclear@0
|
395 vec.z = (float)pcVerts[iIndex].vertex[2] * pcFrame->scale[2];
|
nuclear@0
|
396 vec.z += pcFrame->translate[2];
|
nuclear@0
|
397
|
nuclear@0
|
398 // read the normal vector from the precalculated normal table
|
nuclear@0
|
399 aiVector3D& vNormal = pcMesh->mNormals[iCurrent];
|
nuclear@0
|
400 LookupNormalIndex(pcVerts[iIndex].lightNormalIndex,vNormal);
|
nuclear@0
|
401
|
nuclear@0
|
402 // flip z and y to become right-handed
|
nuclear@0
|
403 std::swap((float&)vNormal.z,(float&)vNormal.y);
|
nuclear@0
|
404 std::swap((float&)vec.z,(float&)vec.y);
|
nuclear@0
|
405
|
nuclear@0
|
406 if (m_pcHeader->numTexCoords) {
|
nuclear@0
|
407 // validate texture coordinates
|
nuclear@0
|
408 iIndex = pcTriangles[i].textureIndices[c];
|
nuclear@0
|
409 if (iIndex >= m_pcHeader->numTexCoords) {
|
nuclear@0
|
410 DefaultLogger::get()->error("MD2: UV index is outside the allowed range");
|
nuclear@0
|
411 iIndex = m_pcHeader->numTexCoords-1;
|
nuclear@0
|
412 }
|
nuclear@0
|
413
|
nuclear@0
|
414 aiVector3D& pcOut = pcMesh->mTextureCoords[0][iCurrent];
|
nuclear@0
|
415
|
nuclear@0
|
416 // the texture coordinates are absolute values but we
|
nuclear@0
|
417 // need relative values between 0 and 1
|
nuclear@0
|
418 pcOut.x = pcTexCoords[iIndex].s / fDivisorU;
|
nuclear@0
|
419 pcOut.y = 1.f-pcTexCoords[iIndex].t / fDivisorV;
|
nuclear@0
|
420 }
|
nuclear@0
|
421 pScene->mMeshes[0]->mFaces[i].mIndices[c] = iCurrent;
|
nuclear@0
|
422 }
|
nuclear@0
|
423 }
|
nuclear@0
|
424 }
|
nuclear@0
|
425
|
nuclear@0
|
426 #endif // !! ASSIMP_BUILD_NO_MD2_IMPORTER
|