[4065] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Patrick Boenzli |
---|
| 13 | */ |
---|
| 14 | |
---|
[4079] | 15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER |
---|
[4065] | 16 | |
---|
| 17 | #include "md2Model.h" |
---|
| 18 | |
---|
[4079] | 19 | #include <fstream> |
---|
[4082] | 20 | #include <string> |
---|
[4142] | 21 | #include <vector> |
---|
[4079] | 22 | |
---|
[4080] | 23 | |
---|
[4065] | 24 | using namespace std; |
---|
| 25 | |
---|
[4077] | 26 | /******************************************************************************** |
---|
| 27 | * MD2MODEL * |
---|
| 28 | ********************************************************************************/ |
---|
[4065] | 29 | |
---|
| 30 | /** |
---|
| 31 | \brief standard constructor |
---|
[4076] | 32 | |
---|
[4140] | 33 | creates a new model |
---|
[4065] | 34 | */ |
---|
| 35 | MD2Model::MD2Model () |
---|
| 36 | { |
---|
[4140] | 37 | this->setClassName ("MD2Model"); |
---|
[4168] | 38 | |
---|
| 39 | MD2Loader* md2loader = new MD2Loader(); |
---|
| 40 | this->model = new t3DModel; |
---|
| 41 | md2loader->importMD2(this->model, "../data/models/tris.md2"); |
---|
[4169] | 42 | delete md2loader; |
---|
[4065] | 43 | } |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | \brief standard deconstructor |
---|
| 48 | |
---|
| 49 | */ |
---|
| 50 | MD2Model::~MD2Model () |
---|
| 51 | { |
---|
| 52 | // delete what has to be deleted here |
---|
| 53 | } |
---|
[4076] | 54 | |
---|
[4161] | 55 | |
---|
[4168] | 56 | void MD2Model::animate() |
---|
[4161] | 57 | { |
---|
[4168] | 58 | if( unlikely(this->model->objectList.size() <= 0)) return; |
---|
[4162] | 59 | /* get current animation from the list */ |
---|
[4168] | 60 | tAnimationInfo *pAnim = &(this->model->animationList[this->model->currentAnim]); |
---|
[4161] | 61 | |
---|
[4168] | 62 | int nextFrame = (this->model->currentFrame + 1) % pAnim->endFrame; |
---|
[4162] | 63 | if( unlikely(nextFrame == 0)) |
---|
| 64 | nextFrame = pAnim->startFrame; |
---|
[4161] | 65 | |
---|
[4168] | 66 | t3DObject *pFrame = &this->model->objectList[this->model->currentFrame]; |
---|
| 67 | t3DObject *pNextFrame = &this->model->objectList[nextFrame]; |
---|
[4161] | 68 | |
---|
[4162] | 69 | /* we have stored the texture and face information only in the first frame */ |
---|
[4168] | 70 | t3DObject *pFirstFrame = &this->model->objectList[0]; |
---|
[4161] | 71 | |
---|
[4162] | 72 | /* get the current time as a value in the domain [0..1] :)) */ |
---|
[4168] | 73 | float t = this->getCurrentTime(this->model, nextFrame); |
---|
[4161] | 74 | |
---|
[4162] | 75 | glBegin(GL_TRIANGLES); |
---|
| 76 | for(int j = 0; j < pFrame->numOfFaces; j++) |
---|
| 77 | { |
---|
| 78 | for(int whichVertex = 0; whichVertex < 3; whichVertex++) |
---|
| 79 | { |
---|
| 80 | int vertIndex = pFirstFrame->pFaces[j].vertIndex[whichVertex]; |
---|
| 81 | int texIndex = pFirstFrame->pFaces[j].coordIndex[whichVertex]; |
---|
[4161] | 82 | |
---|
[4162] | 83 | if( likely(pFirstFrame->pTexVerts != NULL)) |
---|
| 84 | { |
---|
[4168] | 85 | glTexCoord2f(pFirstFrame->pTexVerts[texIndex].x, |
---|
| 86 | pFirstFrame->pTexVerts[texIndex].y); |
---|
[4162] | 87 | } |
---|
[4161] | 88 | |
---|
[4162] | 89 | /* here comes the interpolation part */ |
---|
| 90 | CVector3 vPoint1 = pFrame->pVerts[vertIndex]; |
---|
| 91 | CVector3 vPoint2 = pNextFrame->pVerts[vertIndex]; |
---|
| 92 | glVertex3f(vPoint1.x + t * (vPoint2.x - vPoint1.x), |
---|
| 93 | vPoint1.y + t * (vPoint2.y - vPoint1.y), |
---|
| 94 | vPoint1.z + t * (vPoint2.z - vPoint1.z)); |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | glEnd(); |
---|
[4161] | 98 | } |
---|
| 99 | |
---|
| 100 | |
---|
[4162] | 101 | float MD2Model::getCurrentTime(t3DModel *pModel, int nextFrame) |
---|
[4161] | 102 | { |
---|
[4162] | 103 | /* stretch the time with animation speed (and make seconds out of it) */ |
---|
[4163] | 104 | //float t = this->dtS / kAnimationSpeed; |
---|
[4161] | 105 | |
---|
[4163] | 106 | if ( unlikely(this->localTime*1000.0 >= 1000.0/kAnimationSpeed) ) |
---|
| 107 | { |
---|
[4162] | 108 | pModel->currentFrame = nextFrame; |
---|
[4163] | 109 | printf("changing frame\n"); |
---|
| 110 | this->localTime = 0.0f; |
---|
| 111 | } |
---|
| 112 | printf("t = %f\n", this->localTime); |
---|
[4168] | 113 | return (this->localTime / kAnimationSpeed); |
---|
[4161] | 114 | } |
---|
| 115 | |
---|
[4162] | 116 | |
---|
| 117 | void MD2Model::tick(float dtS) |
---|
| 118 | { |
---|
[4163] | 119 | this->localTime += dtS; |
---|
[4162] | 120 | } |
---|
| 121 | |
---|
[4138] | 122 | /** |
---|
[4140] | 123 | \brief draw function |
---|
[4138] | 124 | |
---|
[4140] | 125 | these function will take NO argument in the final version, just for testing |
---|
[4138] | 126 | */ |
---|
[4168] | 127 | void MD2Model::draw() |
---|
[4138] | 128 | { |
---|
[4168] | 129 | if( this->model->objectList.size() <= 0) return; |
---|
[4076] | 130 | |
---|
[4168] | 131 | t3DObject *pObject = &this->model->objectList[0]; |
---|
[4140] | 132 | glBegin(GL_TRIANGLES); |
---|
| 133 | for(int j = 0; j < pObject->numOfFaces; j++) |
---|
| 134 | { |
---|
| 135 | for(int whichVertex = 0; whichVertex < 3; whichVertex++) |
---|
| 136 | { |
---|
| 137 | int index = pObject->pFaces[j].vertIndex[whichVertex]; |
---|
| 138 | int index2 = pObject->pFaces[j].coordIndex[whichVertex]; |
---|
| 139 | /* we invert the normals since the md2 file format uses different style */ |
---|
[4150] | 140 | /* FIX FIX FIX: ther are actualy no reasons to compute the normals every frame: change this later*/ |
---|
[4147] | 141 | glNormal3f(-pObject->pNormals[index].x, -pObject->pNormals[index].y, -pObject->pNormals[index].z); |
---|
| 142 | |
---|
[4158] | 143 | if( likely(pObject->pTexVerts != NULL)) |
---|
[4140] | 144 | { |
---|
[4158] | 145 | glTexCoord2f(pObject->pTexVerts[index2].x, pObject->pTexVerts[index2].y); |
---|
[4140] | 146 | } |
---|
[4158] | 147 | glVertex3f(pObject->pVerts[index].x, pObject->pVerts[index].y, pObject->pVerts[index].z); |
---|
[4140] | 148 | } |
---|
| 149 | } |
---|
| 150 | glEnd(); |
---|
[4138] | 151 | } |
---|
| 152 | |
---|
[4077] | 153 | /******************************************************************************** |
---|
| 154 | * MD2LOADER * |
---|
| 155 | ********************************************************************************/ |
---|
| 156 | |
---|
[4076] | 157 | /** |
---|
| 158 | \brief standard deconstructor |
---|
[4140] | 159 | creates a new model loader |
---|
[4076] | 160 | */ |
---|
| 161 | MD2Loader::MD2Loader() |
---|
[4077] | 162 | { |
---|
[4140] | 163 | this->setClassName ("MD2Loader"); |
---|
| 164 | /* initialize all data to initial state */ |
---|
| 165 | memset(&this->header, 0, sizeof(tMd2Header)); |
---|
| 166 | this->pSkins = NULL; |
---|
| 167 | this->pTexCoords = NULL; |
---|
| 168 | this->pTriangles = NULL; |
---|
| 169 | this->pFrames = NULL; |
---|
[4077] | 170 | } |
---|
[4076] | 171 | |
---|
| 172 | /** |
---|
| 173 | \brief standard deconstructor |
---|
| 174 | */ |
---|
| 175 | MD2Loader::~MD2Loader() |
---|
| 176 | {} |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | /** |
---|
[4140] | 180 | \brief this is called by the client to open the .Md2 file, read it, then clean up |
---|
| 181 | \param model to load in |
---|
| 182 | \param file name to load |
---|
| 183 | \param texture name to load |
---|
[4076] | 184 | */ |
---|
[4079] | 185 | bool MD2Loader::importMD2(t3DModel *pModel, char *fileName, char *textureName) |
---|
| 186 | { |
---|
[4140] | 187 | this->pFile = fopen(fileName, "rb"); |
---|
| 188 | if( unlikely(!pFile)) |
---|
| 189 | { |
---|
| 190 | PRINTF(1)("Couldn't open the MD2 File for loading. Exiting.\n"); |
---|
| 191 | return false; |
---|
| 192 | } |
---|
| 193 | fread(&this->header, 1, sizeof(tMd2Header), pFile); |
---|
| 194 | /* check for the header version: make sure its a md2 file :) */ |
---|
| 195 | if( likely(this->header.version != 8)) |
---|
| 196 | { |
---|
| 197 | PRINTF(1)("Couldn't load file %s: invalid file format: stop loading\n", fileName); |
---|
| 198 | return false; |
---|
| 199 | } |
---|
[4079] | 200 | |
---|
[4140] | 201 | this->readMD2Data(); |
---|
| 202 | this->convertDataStructures(pModel); |
---|
[4150] | 203 | this->computeNormals(pModel); |
---|
[4079] | 204 | |
---|
[4140] | 205 | if( likely((int)textureName)) |
---|
| 206 | { |
---|
[4146] | 207 | tMaterialInfo textureInfo; |
---|
| 208 | strcpy(textureInfo.strFile, textureName); |
---|
[4140] | 209 | /* since there is only one texture for a .Md2 file, the ID is always 0 */ |
---|
[4146] | 210 | textureInfo.texureId = 0; |
---|
| 211 | textureInfo.uTile = 1; |
---|
[4140] | 212 | /* we only have 1 material for a model */ |
---|
| 213 | pModel->numOfMaterials = 1; |
---|
[4146] | 214 | pModel->materialList.push_back(textureInfo); |
---|
[4140] | 215 | } |
---|
[4079] | 216 | |
---|
[4140] | 217 | this->cleanUp(); |
---|
| 218 | return true; |
---|
[4079] | 219 | } |
---|
| 220 | |
---|
[4157] | 221 | |
---|
[4076] | 222 | /** |
---|
[4140] | 223 | \brief This function reads in all of the model's data, except the animation frames |
---|
[4076] | 224 | */ |
---|
| 225 | void MD2Loader::readMD2Data() |
---|
[4080] | 226 | { |
---|
[4140] | 227 | unsigned char buffer[MD2_MAX_FRAMESIZE]; |
---|
| 228 | this->pSkins = new tMd2Skin[this->header.numSkins]; |
---|
| 229 | this->pTexCoords = new tMd2TexCoord[this->header.numTexCoords]; |
---|
| 230 | this->pTriangles = new tMd2Face[this->header.numTriangles]; |
---|
| 231 | this->pFrames = new tMd2Frame[this->header.numFrames]; |
---|
[4076] | 232 | |
---|
[4140] | 233 | /* we read the skins */ |
---|
| 234 | fseek(this->pFile, this->header.offsetSkins, SEEK_SET); |
---|
| 235 | fread(this->pSkins, sizeof(tMd2Skin), this->header.numSkins, this->pFile); |
---|
| 236 | /* read all vertex data */ |
---|
| 237 | fseek(this->pFile, this->header.offsetTexCoords, SEEK_SET); |
---|
| 238 | fread(this->pTexCoords, sizeof(tMd2TexCoord), this->header.numTexCoords, this->pFile); |
---|
| 239 | /* read face data for each triangle (normals) */ |
---|
| 240 | fseek(this->pFile, this->header.offsetTriangles, SEEK_SET); |
---|
| 241 | fread(this->pTriangles, sizeof(tMd2Face), this->header.numTriangles, this->pFile); |
---|
| 242 | /* reading all frame data */ |
---|
| 243 | fseek(this->pFile, this->header.offsetFrames, SEEK_SET); |
---|
[4147] | 244 | for( int i = 0; i < this->header.numFrames; i++) |
---|
[4140] | 245 | { |
---|
| 246 | tMd2AliasFrame *pFrame = (tMd2AliasFrame *) buffer; |
---|
| 247 | this->pFrames[i].pVertices = new tMd2Triangle [this->header.numVertices]; |
---|
| 248 | |
---|
| 249 | /* read the frame animation data */ |
---|
| 250 | fread(pFrame, 1, this->header.frameSize, this->pFile); |
---|
| 251 | strcpy(this->pFrames[i].strName, pFrame->name); |
---|
| 252 | tMd2Triangle *pVertices = this->pFrames[i].pVertices; |
---|
| 253 | /* |
---|
| 254 | scale translations, store vertices: since id used a non-opengl xyz notation, we have to swap y and z |
---|
| 255 | and negate z axis |
---|
| 256 | */ |
---|
[4147] | 257 | for( int j = 0; j < this->header.numVertices; j++) |
---|
[4080] | 258 | { |
---|
[4140] | 259 | pVertices[j].vertex[0] = pFrame->aliasVertices[j].vertex[0] * pFrame->scale[0] + pFrame->translate[0]; |
---|
| 260 | pVertices[j].vertex[2] = -1 * (pFrame->aliasVertices[j].vertex[1] * pFrame->scale[1] + pFrame->translate[1]); |
---|
| 261 | pVertices[j].vertex[1] = pFrame->aliasVertices[j].vertex[2] * pFrame->scale[2] + pFrame->translate[2]; |
---|
[4080] | 262 | } |
---|
[4140] | 263 | } |
---|
[4080] | 264 | } |
---|
| 265 | |
---|
[4157] | 266 | |
---|
[4076] | 267 | /** |
---|
[4140] | 268 | \brief this function fills in the animation list for each animation by name and frame |
---|
| 269 | \param model |
---|
[4076] | 270 | */ |
---|
| 271 | void MD2Loader::parseAnimations(t3DModel *pModel) |
---|
[4082] | 272 | { |
---|
[4146] | 273 | tAnimationInfo animationInfo; |
---|
[4140] | 274 | string strLastName = ""; |
---|
[4076] | 275 | |
---|
[4141] | 276 | /* the animation parse process looks a little bit wired: this is because there are no |
---|
[4140] | 277 | fix bounds for the animation lengths, so the frames are destingushed using their names |
---|
| 278 | which is normaly composed of <animationname><number> |
---|
| 279 | */ |
---|
[4082] | 280 | |
---|
[4140] | 281 | for(int i = 0; i < pModel->numOfObjects; i++) |
---|
| 282 | { |
---|
| 283 | string strName = this->pFrames[i].strName; |
---|
| 284 | int frameNum = 0; |
---|
[4141] | 285 | |
---|
| 286 | /* erease the frame number from the frame-name */ |
---|
[4140] | 287 | for(unsigned int j = 0; j < strName.length(); j++) |
---|
[4082] | 288 | { |
---|
[4140] | 289 | if( isdigit(strName[j]) && j >= strName.length() - 2) |
---|
| 290 | { |
---|
| 291 | frameNum = atoi(&strName[j]); |
---|
| 292 | strName.erase(j, strName.length() - j); |
---|
| 293 | break; |
---|
| 294 | } |
---|
| 295 | } |
---|
[4082] | 296 | |
---|
[4141] | 297 | printf("current: %s, last: %s\n", strName.c_str(), strLastName.c_str()); |
---|
| 298 | |
---|
[4140] | 299 | /* animations are sorted through their names: this is how its been done: */ |
---|
[4141] | 300 | if( strName != strLastName || i == pModel->numOfObjects - 1) |
---|
[4140] | 301 | { |
---|
[4141] | 302 | if( strLastName != "") |
---|
[4140] | 303 | { |
---|
[4146] | 304 | strcpy(animationInfo.strName, strLastName.c_str()); |
---|
| 305 | animationInfo.endFrame = i; |
---|
| 306 | pModel->animationList.push_back(animationInfo); |
---|
[4140] | 307 | memset(&animationInfo, 0, sizeof(tAnimationInfo)); |
---|
| 308 | pModel->numOfAnimations++; |
---|
| 309 | } |
---|
[4146] | 310 | animationInfo.startFrame = frameNum - 1 + i; |
---|
[4082] | 311 | } |
---|
[4140] | 312 | strLastName = strName; |
---|
| 313 | } |
---|
[4082] | 314 | } |
---|
| 315 | |
---|
[4157] | 316 | |
---|
[4076] | 317 | /** |
---|
[4140] | 318 | \brief this function converts the .md2 structures to our own model and object structures: decompress |
---|
| 319 | \param model |
---|
[4076] | 320 | */ |
---|
| 321 | void MD2Loader::convertDataStructures(t3DModel *pModel) |
---|
[4087] | 322 | { |
---|
[4140] | 323 | int j = 0, i = 0; |
---|
[4087] | 324 | |
---|
[4140] | 325 | memset(pModel, 0, sizeof(t3DModel)); |
---|
| 326 | pModel->numOfObjects = this->header.numFrames; |
---|
[4076] | 327 | |
---|
[4140] | 328 | this->parseAnimations(pModel); |
---|
[4076] | 329 | |
---|
[4140] | 330 | for (i = 0; i < pModel->numOfObjects; i++) |
---|
| 331 | { |
---|
[4146] | 332 | t3DObject currentFrame; |
---|
[4087] | 333 | |
---|
[4146] | 334 | currentFrame.numOfVerts = this->header.numVertices; |
---|
| 335 | currentFrame.numTexVertex = this->header.numTexCoords; |
---|
| 336 | currentFrame.numOfFaces = this->header.numTriangles; |
---|
[4087] | 337 | |
---|
[4146] | 338 | currentFrame.pVerts = new CVector3[currentFrame.numOfVerts]; |
---|
[4087] | 339 | |
---|
[4146] | 340 | for (j = 0; j < currentFrame.numOfVerts; j++) |
---|
[4140] | 341 | { |
---|
[4146] | 342 | currentFrame.pVerts[j].x = this->pFrames[i].pVertices[j].vertex[0]; |
---|
| 343 | currentFrame.pVerts[j].y = this->pFrames[i].pVertices[j].vertex[1]; |
---|
| 344 | currentFrame.pVerts[j].z = this->pFrames[i].pVertices[j].vertex[2]; |
---|
[4140] | 345 | } |
---|
[4087] | 346 | |
---|
[4140] | 347 | delete this->pFrames[i].pVertices; |
---|
[4087] | 348 | |
---|
[4140] | 349 | if( likely(i > 0)) |
---|
| 350 | { |
---|
[4146] | 351 | pModel->objectList.push_back(currentFrame); |
---|
[4140] | 352 | continue; |
---|
| 353 | } |
---|
[4087] | 354 | |
---|
[4146] | 355 | currentFrame.pTexVerts = new CVector2[currentFrame.numTexVertex]; |
---|
| 356 | currentFrame.pFaces = new tFace[currentFrame.numOfFaces]; |
---|
[4087] | 357 | |
---|
[4146] | 358 | for(j = 0; j < currentFrame.numTexVertex; j++) |
---|
[4140] | 359 | { |
---|
[4146] | 360 | currentFrame.pTexVerts[j].x = this->pTexCoords[j].u / float(this->header.skinWidth); |
---|
| 361 | currentFrame.pTexVerts[j].y = 1 - this->pTexCoords[j].v / float(this->header.skinHeight); |
---|
[4140] | 362 | } |
---|
[4087] | 363 | |
---|
[4146] | 364 | for(j = 0; j < currentFrame.numOfFaces; j++) |
---|
[4140] | 365 | { |
---|
[4146] | 366 | currentFrame.pFaces[j].vertIndex[0] = this->pTriangles[j].vertexIndices[0]; |
---|
| 367 | currentFrame.pFaces[j].vertIndex[1] = this->pTriangles[j].vertexIndices[1]; |
---|
| 368 | currentFrame.pFaces[j].vertIndex[2] = this->pTriangles[j].vertexIndices[2]; |
---|
[4087] | 369 | |
---|
[4146] | 370 | currentFrame.pFaces[j].coordIndex[0] = this->pTriangles[j].textureIndices[0]; |
---|
| 371 | currentFrame.pFaces[j].coordIndex[1] = this->pTriangles[j].textureIndices[1]; |
---|
| 372 | currentFrame.pFaces[j].coordIndex[2] = this->pTriangles[j].textureIndices[2]; |
---|
[4087] | 373 | } |
---|
[4146] | 374 | pModel->objectList.push_back(currentFrame); |
---|
[4140] | 375 | } |
---|
[4087] | 376 | } |
---|
| 377 | |
---|
| 378 | |
---|
[4076] | 379 | /** |
---|
[4140] | 380 | \brief this function conputes the normals of the model |
---|
| 381 | \param model |
---|
[4076] | 382 | */ |
---|
| 383 | void MD2Loader::computeNormals(t3DModel *pModel) |
---|
[4148] | 384 | { |
---|
[4149] | 385 | CVector3 vVector1, vVector2, vNormal, vPoly[3]; |
---|
[4076] | 386 | |
---|
[4149] | 387 | if( unlikely(pModel->numOfObjects <= 0)) |
---|
| 388 | return; |
---|
| 389 | /* now computing face normals: this means just averaging the vertex normals of a face */ |
---|
| 390 | /* so for every object: */ |
---|
| 391 | for(int index = 0; index < pModel->numOfObjects; index++) |
---|
| 392 | { |
---|
| 393 | t3DObject *pObject = &(pModel->objectList[index]); |
---|
[4126] | 394 | |
---|
[4149] | 395 | /* allocate all the memory we need to calculate the normals */ |
---|
| 396 | CVector3 *pNormals = new CVector3 [pObject->numOfFaces]; |
---|
| 397 | CVector3 *pTempNormals = new CVector3 [pObject->numOfFaces]; |
---|
| 398 | pObject->pNormals = new CVector3 [pObject->numOfVerts]; |
---|
[4148] | 399 | |
---|
[4149] | 400 | for(int i=0; i < pObject->numOfFaces; i++) |
---|
| 401 | { |
---|
| 402 | /* cache the points to make coding easier :) */ |
---|
| 403 | vPoly[0] = pObject->pVerts[pObject->pFaces[i].vertIndex[0]]; |
---|
| 404 | vPoly[1] = pObject->pVerts[pObject->pFaces[i].vertIndex[1]]; |
---|
| 405 | vPoly[2] = pObject->pVerts[pObject->pFaces[i].vertIndex[2]]; |
---|
[4148] | 406 | |
---|
[4149] | 407 | vVector1 = MathHelp::VectorDiff(vPoly[0], vPoly[2]); |
---|
| 408 | vVector2 = MathHelp::VectorDiff(vPoly[2], vPoly[1]); |
---|
[4148] | 409 | |
---|
[4149] | 410 | vNormal = MathHelp::CrossProduct(vVector1, vVector2); |
---|
| 411 | pTempNormals[i] = vNormal; |
---|
| 412 | vNormal = MathHelp::NormalizeVector(vNormal); |
---|
[4148] | 413 | |
---|
[4149] | 414 | pNormals[i] = vNormal; |
---|
| 415 | } |
---|
[4148] | 416 | |
---|
[4149] | 417 | /* now calculating vertex normals */ |
---|
| 418 | CVector3 vSum = {0.0, 0.0, 0.0}; |
---|
| 419 | CVector3 vZero = vSum; |
---|
| 420 | int shared=0; |
---|
[4148] | 421 | |
---|
[4149] | 422 | for (int i = 0; i < pObject->numOfVerts; i++) |
---|
| 423 | { |
---|
| 424 | for (int j = 0; j < pObject->numOfFaces; j++) |
---|
| 425 | { |
---|
| 426 | if (pObject->pFaces[j].vertIndex[0] == i || |
---|
| 427 | pObject->pFaces[j].vertIndex[1] == i || |
---|
| 428 | pObject->pFaces[j].vertIndex[2] == i) |
---|
[4148] | 429 | { |
---|
[4149] | 430 | vSum = MathHelp::AddVector(vSum, pTempNormals[j]); |
---|
| 431 | shared++; } |
---|
| 432 | } |
---|
| 433 | pObject->pNormals[i] = MathHelp::DivideVectorByScaler(vSum, float(-shared)); |
---|
| 434 | pObject->pNormals[i] = MathHelp::NormalizeVector(pObject->pNormals[i]); |
---|
[4148] | 435 | |
---|
[4149] | 436 | vSum = vZero; |
---|
| 437 | shared = 0; |
---|
[4148] | 438 | } |
---|
[4149] | 439 | delete [] pTempNormals; |
---|
| 440 | delete [] pNormals; |
---|
| 441 | } |
---|
[4148] | 442 | } |
---|
| 443 | |
---|
| 444 | |
---|
[4076] | 445 | /** |
---|
[4140] | 446 | \brief This function cleans up our allocated memory and closes the file |
---|
[4076] | 447 | */ |
---|
| 448 | void MD2Loader::cleanUp() |
---|
[4087] | 449 | { |
---|
[4140] | 450 | fclose(this->pFile); |
---|
[4087] | 451 | |
---|
[4140] | 452 | if( this->pSkins) delete [] this->pSkins; |
---|
| 453 | if( this->pTexCoords) delete this->pTexCoords; |
---|
| 454 | if( this->pTriangles) delete this->pTriangles; |
---|
| 455 | if( this->pFrames) delete this->pFrames; |
---|
[4087] | 456 | } |
---|