[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" |
---|
[4172] | 18 | #include "material.h" |
---|
[4065] | 19 | |
---|
[4079] | 20 | #include <fstream> |
---|
[4082] | 21 | #include <string> |
---|
[4142] | 22 | #include <vector> |
---|
[4079] | 23 | |
---|
[4080] | 24 | |
---|
[4065] | 25 | using namespace std; |
---|
| 26 | |
---|
[4077] | 27 | /******************************************************************************** |
---|
| 28 | * MD2MODEL * |
---|
| 29 | ********************************************************************************/ |
---|
[4065] | 30 | |
---|
| 31 | /** |
---|
| 32 | \brief standard constructor |
---|
[4076] | 33 | |
---|
[4140] | 34 | creates a new model |
---|
[4065] | 35 | */ |
---|
| 36 | MD2Model::MD2Model () |
---|
| 37 | { |
---|
[4140] | 38 | this->setClassName ("MD2Model"); |
---|
[4168] | 39 | |
---|
| 40 | MD2Loader* md2loader = new MD2Loader(); |
---|
| 41 | this->model = new t3DModel; |
---|
| 42 | md2loader->importMD2(this->model, "../data/models/tris.md2"); |
---|
[4169] | 43 | delete md2loader; |
---|
[4065] | 44 | } |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | \brief standard deconstructor |
---|
| 49 | |
---|
| 50 | */ |
---|
| 51 | MD2Model::~MD2Model () |
---|
| 52 | { |
---|
| 53 | // delete what has to be deleted here |
---|
| 54 | } |
---|
[4076] | 55 | |
---|
[4161] | 56 | |
---|
[4168] | 57 | void MD2Model::animate() |
---|
[4161] | 58 | { |
---|
[4168] | 59 | if( unlikely(this->model->objectList.size() <= 0)) return; |
---|
[4162] | 60 | /* get current animation from the list */ |
---|
[4168] | 61 | tAnimationInfo *pAnim = &(this->model->animationList[this->model->currentAnim]); |
---|
[4161] | 62 | |
---|
[4168] | 63 | int nextFrame = (this->model->currentFrame + 1) % pAnim->endFrame; |
---|
[4162] | 64 | if( unlikely(nextFrame == 0)) |
---|
| 65 | nextFrame = pAnim->startFrame; |
---|
[4161] | 66 | |
---|
[4168] | 67 | t3DObject *pFrame = &this->model->objectList[this->model->currentFrame]; |
---|
| 68 | t3DObject *pNextFrame = &this->model->objectList[nextFrame]; |
---|
[4161] | 69 | |
---|
[4162] | 70 | /* we have stored the texture and face information only in the first frame */ |
---|
[4168] | 71 | t3DObject *pFirstFrame = &this->model->objectList[0]; |
---|
[4161] | 72 | |
---|
[4162] | 73 | /* get the current time as a value in the domain [0..1] :)) */ |
---|
[4168] | 74 | float t = this->getCurrentTime(this->model, nextFrame); |
---|
[4161] | 75 | |
---|
[4162] | 76 | glBegin(GL_TRIANGLES); |
---|
| 77 | for(int j = 0; j < pFrame->numOfFaces; j++) |
---|
| 78 | { |
---|
| 79 | for(int whichVertex = 0; whichVertex < 3; whichVertex++) |
---|
| 80 | { |
---|
| 81 | int vertIndex = pFirstFrame->pFaces[j].vertIndex[whichVertex]; |
---|
| 82 | int texIndex = pFirstFrame->pFaces[j].coordIndex[whichVertex]; |
---|
[4161] | 83 | |
---|
[4162] | 84 | if( likely(pFirstFrame->pTexVerts != NULL)) |
---|
| 85 | { |
---|
[4168] | 86 | glTexCoord2f(pFirstFrame->pTexVerts[texIndex].x, |
---|
| 87 | pFirstFrame->pTexVerts[texIndex].y); |
---|
[4162] | 88 | } |
---|
[4161] | 89 | |
---|
[4162] | 90 | /* here comes the interpolation part */ |
---|
| 91 | CVector3 vPoint1 = pFrame->pVerts[vertIndex]; |
---|
| 92 | CVector3 vPoint2 = pNextFrame->pVerts[vertIndex]; |
---|
| 93 | glVertex3f(vPoint1.x + t * (vPoint2.x - vPoint1.x), |
---|
| 94 | vPoint1.y + t * (vPoint2.y - vPoint1.y), |
---|
| 95 | vPoint1.z + t * (vPoint2.z - vPoint1.z)); |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | glEnd(); |
---|
[4161] | 99 | } |
---|
| 100 | |
---|
| 101 | |
---|
[4162] | 102 | float MD2Model::getCurrentTime(t3DModel *pModel, int nextFrame) |
---|
[4161] | 103 | { |
---|
[4162] | 104 | /* stretch the time with animation speed (and make seconds out of it) */ |
---|
[4163] | 105 | //float t = this->dtS / kAnimationSpeed; |
---|
[4161] | 106 | |
---|
[4163] | 107 | if ( unlikely(this->localTime*1000.0 >= 1000.0/kAnimationSpeed) ) |
---|
| 108 | { |
---|
[4162] | 109 | pModel->currentFrame = nextFrame; |
---|
[4163] | 110 | this->localTime = 0.0f; |
---|
| 111 | } |
---|
[4168] | 112 | return (this->localTime / kAnimationSpeed); |
---|
[4161] | 113 | } |
---|
| 114 | |
---|
[4162] | 115 | |
---|
| 116 | void MD2Model::tick(float dtS) |
---|
| 117 | { |
---|
[4163] | 118 | this->localTime += dtS; |
---|
[4162] | 119 | } |
---|
| 120 | |
---|
[4138] | 121 | /** |
---|
[4140] | 122 | \brief draw function |
---|
[4138] | 123 | |
---|
[4140] | 124 | these function will take NO argument in the final version, just for testing |
---|
[4138] | 125 | */ |
---|
[4168] | 126 | void MD2Model::draw() |
---|
[4138] | 127 | { |
---|
[4168] | 128 | if( this->model->objectList.size() <= 0) return; |
---|
[4076] | 129 | |
---|
[4168] | 130 | t3DObject *pObject = &this->model->objectList[0]; |
---|
[4140] | 131 | glBegin(GL_TRIANGLES); |
---|
| 132 | for(int j = 0; j < pObject->numOfFaces; j++) |
---|
| 133 | { |
---|
| 134 | for(int whichVertex = 0; whichVertex < 3; whichVertex++) |
---|
| 135 | { |
---|
| 136 | int index = pObject->pFaces[j].vertIndex[whichVertex]; |
---|
| 137 | int index2 = pObject->pFaces[j].coordIndex[whichVertex]; |
---|
| 138 | /* we invert the normals since the md2 file format uses different style */ |
---|
[4150] | 139 | /* FIX FIX FIX: ther are actualy no reasons to compute the normals every frame: change this later*/ |
---|
[4147] | 140 | glNormal3f(-pObject->pNormals[index].x, -pObject->pNormals[index].y, -pObject->pNormals[index].z); |
---|
| 141 | |
---|
[4158] | 142 | if( likely(pObject->pTexVerts != NULL)) |
---|
[4140] | 143 | { |
---|
[4158] | 144 | glTexCoord2f(pObject->pTexVerts[index2].x, pObject->pTexVerts[index2].y); |
---|
[4140] | 145 | } |
---|
[4158] | 146 | glVertex3f(pObject->pVerts[index].x, pObject->pVerts[index].y, pObject->pVerts[index].z); |
---|
[4140] | 147 | } |
---|
| 148 | } |
---|
| 149 | glEnd(); |
---|
[4138] | 150 | } |
---|
| 151 | |
---|
[4077] | 152 | /******************************************************************************** |
---|
| 153 | * MD2LOADER * |
---|
| 154 | ********************************************************************************/ |
---|
| 155 | |
---|
[4076] | 156 | /** |
---|
| 157 | \brief standard deconstructor |
---|
[4140] | 158 | creates a new model loader |
---|
[4076] | 159 | */ |
---|
| 160 | MD2Loader::MD2Loader() |
---|
[4077] | 161 | { |
---|
[4140] | 162 | this->setClassName ("MD2Loader"); |
---|
| 163 | /* initialize all data to initial state */ |
---|
| 164 | memset(&this->header, 0, sizeof(tMd2Header)); |
---|
| 165 | this->pSkins = NULL; |
---|
| 166 | this->pTexCoords = NULL; |
---|
| 167 | this->pTriangles = NULL; |
---|
| 168 | this->pFrames = NULL; |
---|
[4077] | 169 | } |
---|
[4076] | 170 | |
---|
| 171 | /** |
---|
| 172 | \brief standard deconstructor |
---|
| 173 | */ |
---|
| 174 | MD2Loader::~MD2Loader() |
---|
| 175 | {} |
---|
| 176 | |
---|
| 177 | |
---|
| 178 | /** |
---|
[4140] | 179 | \brief this is called by the client to open the .Md2 file, read it, then clean up |
---|
| 180 | \param model to load in |
---|
| 181 | \param file name to load |
---|
| 182 | \param texture name to load |
---|
[4076] | 183 | */ |
---|
[4079] | 184 | bool MD2Loader::importMD2(t3DModel *pModel, char *fileName, char *textureName) |
---|
| 185 | { |
---|
[4140] | 186 | this->pFile = fopen(fileName, "rb"); |
---|
| 187 | if( unlikely(!pFile)) |
---|
| 188 | { |
---|
| 189 | PRINTF(1)("Couldn't open the MD2 File for loading. Exiting.\n"); |
---|
| 190 | return false; |
---|
| 191 | } |
---|
| 192 | fread(&this->header, 1, sizeof(tMd2Header), pFile); |
---|
| 193 | /* check for the header version: make sure its a md2 file :) */ |
---|
| 194 | if( likely(this->header.version != 8)) |
---|
| 195 | { |
---|
| 196 | PRINTF(1)("Couldn't load file %s: invalid file format: stop loading\n", fileName); |
---|
| 197 | return false; |
---|
| 198 | } |
---|
[4079] | 199 | |
---|
[4140] | 200 | this->readMD2Data(); |
---|
| 201 | this->convertDataStructures(pModel); |
---|
[4150] | 202 | this->computeNormals(pModel); |
---|
[4079] | 203 | |
---|
[4140] | 204 | if( likely((int)textureName)) |
---|
| 205 | { |
---|
[4146] | 206 | tMaterialInfo textureInfo; |
---|
| 207 | strcpy(textureInfo.strFile, textureName); |
---|
[4140] | 208 | /* since there is only one texture for a .Md2 file, the ID is always 0 */ |
---|
[4146] | 209 | textureInfo.texureId = 0; |
---|
| 210 | textureInfo.uTile = 1; |
---|
[4140] | 211 | /* we only have 1 material for a model */ |
---|
| 212 | pModel->numOfMaterials = 1; |
---|
[4146] | 213 | pModel->materialList.push_back(textureInfo); |
---|
[4140] | 214 | } |
---|
[4079] | 215 | |
---|
[4140] | 216 | this->cleanUp(); |
---|
| 217 | return true; |
---|
[4079] | 218 | } |
---|
| 219 | |
---|
[4157] | 220 | |
---|
[4076] | 221 | /** |
---|
[4140] | 222 | \brief This function reads in all of the model's data, except the animation frames |
---|
[4076] | 223 | */ |
---|
| 224 | void MD2Loader::readMD2Data() |
---|
[4080] | 225 | { |
---|
[4140] | 226 | unsigned char buffer[MD2_MAX_FRAMESIZE]; |
---|
| 227 | this->pSkins = new tMd2Skin[this->header.numSkins]; |
---|
| 228 | this->pTexCoords = new tMd2TexCoord[this->header.numTexCoords]; |
---|
| 229 | this->pTriangles = new tMd2Face[this->header.numTriangles]; |
---|
| 230 | this->pFrames = new tMd2Frame[this->header.numFrames]; |
---|
[4076] | 231 | |
---|
[4140] | 232 | /* we read the skins */ |
---|
| 233 | fseek(this->pFile, this->header.offsetSkins, SEEK_SET); |
---|
| 234 | fread(this->pSkins, sizeof(tMd2Skin), this->header.numSkins, this->pFile); |
---|
| 235 | /* read all vertex data */ |
---|
| 236 | fseek(this->pFile, this->header.offsetTexCoords, SEEK_SET); |
---|
| 237 | fread(this->pTexCoords, sizeof(tMd2TexCoord), this->header.numTexCoords, this->pFile); |
---|
| 238 | /* read face data for each triangle (normals) */ |
---|
| 239 | fseek(this->pFile, this->header.offsetTriangles, SEEK_SET); |
---|
| 240 | fread(this->pTriangles, sizeof(tMd2Face), this->header.numTriangles, this->pFile); |
---|
| 241 | /* reading all frame data */ |
---|
| 242 | fseek(this->pFile, this->header.offsetFrames, SEEK_SET); |
---|
[4147] | 243 | for( int i = 0; i < this->header.numFrames; i++) |
---|
[4140] | 244 | { |
---|
| 245 | tMd2AliasFrame *pFrame = (tMd2AliasFrame *) buffer; |
---|
| 246 | this->pFrames[i].pVertices = new tMd2Triangle [this->header.numVertices]; |
---|
| 247 | |
---|
| 248 | /* read the frame animation data */ |
---|
| 249 | fread(pFrame, 1, this->header.frameSize, this->pFile); |
---|
| 250 | strcpy(this->pFrames[i].strName, pFrame->name); |
---|
| 251 | tMd2Triangle *pVertices = this->pFrames[i].pVertices; |
---|
| 252 | /* |
---|
| 253 | scale translations, store vertices: since id used a non-opengl xyz notation, we have to swap y and z |
---|
| 254 | and negate z axis |
---|
| 255 | */ |
---|
[4147] | 256 | for( int j = 0; j < this->header.numVertices; j++) |
---|
[4080] | 257 | { |
---|
[4140] | 258 | pVertices[j].vertex[0] = pFrame->aliasVertices[j].vertex[0] * pFrame->scale[0] + pFrame->translate[0]; |
---|
| 259 | pVertices[j].vertex[2] = -1 * (pFrame->aliasVertices[j].vertex[1] * pFrame->scale[1] + pFrame->translate[1]); |
---|
| 260 | pVertices[j].vertex[1] = pFrame->aliasVertices[j].vertex[2] * pFrame->scale[2] + pFrame->translate[2]; |
---|
[4080] | 261 | } |
---|
[4140] | 262 | } |
---|
[4080] | 263 | } |
---|
| 264 | |
---|
[4157] | 265 | |
---|
[4076] | 266 | /** |
---|
[4140] | 267 | \brief this function fills in the animation list for each animation by name and frame |
---|
| 268 | \param model |
---|
[4076] | 269 | */ |
---|
| 270 | void MD2Loader::parseAnimations(t3DModel *pModel) |
---|
[4082] | 271 | { |
---|
[4146] | 272 | tAnimationInfo animationInfo; |
---|
[4140] | 273 | string strLastName = ""; |
---|
[4076] | 274 | |
---|
[4141] | 275 | /* the animation parse process looks a little bit wired: this is because there are no |
---|
[4140] | 276 | fix bounds for the animation lengths, so the frames are destingushed using their names |
---|
| 277 | which is normaly composed of <animationname><number> |
---|
| 278 | */ |
---|
[4082] | 279 | |
---|
[4140] | 280 | for(int i = 0; i < pModel->numOfObjects; i++) |
---|
| 281 | { |
---|
| 282 | string strName = this->pFrames[i].strName; |
---|
| 283 | int frameNum = 0; |
---|
[4141] | 284 | |
---|
| 285 | /* erease the frame number from the frame-name */ |
---|
[4140] | 286 | for(unsigned int j = 0; j < strName.length(); j++) |
---|
[4082] | 287 | { |
---|
[4140] | 288 | if( isdigit(strName[j]) && j >= strName.length() - 2) |
---|
| 289 | { |
---|
| 290 | frameNum = atoi(&strName[j]); |
---|
| 291 | strName.erase(j, strName.length() - j); |
---|
| 292 | break; |
---|
| 293 | } |
---|
| 294 | } |
---|
[4082] | 295 | |
---|
[4141] | 296 | printf("current: %s, last: %s\n", strName.c_str(), strLastName.c_str()); |
---|
| 297 | |
---|
[4140] | 298 | /* animations are sorted through their names: this is how its been done: */ |
---|
[4141] | 299 | if( strName != strLastName || i == pModel->numOfObjects - 1) |
---|
[4140] | 300 | { |
---|
[4141] | 301 | if( strLastName != "") |
---|
[4140] | 302 | { |
---|
[4146] | 303 | strcpy(animationInfo.strName, strLastName.c_str()); |
---|
| 304 | animationInfo.endFrame = i; |
---|
| 305 | pModel->animationList.push_back(animationInfo); |
---|
[4140] | 306 | memset(&animationInfo, 0, sizeof(tAnimationInfo)); |
---|
| 307 | pModel->numOfAnimations++; |
---|
| 308 | } |
---|
[4146] | 309 | animationInfo.startFrame = frameNum - 1 + i; |
---|
[4082] | 310 | } |
---|
[4140] | 311 | strLastName = strName; |
---|
| 312 | } |
---|
[4082] | 313 | } |
---|
| 314 | |
---|
[4157] | 315 | |
---|
[4076] | 316 | /** |
---|
[4140] | 317 | \brief this function converts the .md2 structures to our own model and object structures: decompress |
---|
| 318 | \param model |
---|
[4076] | 319 | */ |
---|
| 320 | void MD2Loader::convertDataStructures(t3DModel *pModel) |
---|
[4087] | 321 | { |
---|
[4140] | 322 | int j = 0, i = 0; |
---|
[4087] | 323 | |
---|
[4140] | 324 | memset(pModel, 0, sizeof(t3DModel)); |
---|
| 325 | pModel->numOfObjects = this->header.numFrames; |
---|
[4076] | 326 | |
---|
[4140] | 327 | this->parseAnimations(pModel); |
---|
[4076] | 328 | |
---|
[4140] | 329 | for (i = 0; i < pModel->numOfObjects; i++) |
---|
| 330 | { |
---|
[4146] | 331 | t3DObject currentFrame; |
---|
[4087] | 332 | |
---|
[4146] | 333 | currentFrame.numOfVerts = this->header.numVertices; |
---|
| 334 | currentFrame.numTexVertex = this->header.numTexCoords; |
---|
| 335 | currentFrame.numOfFaces = this->header.numTriangles; |
---|
[4087] | 336 | |
---|
[4146] | 337 | currentFrame.pVerts = new CVector3[currentFrame.numOfVerts]; |
---|
[4087] | 338 | |
---|
[4146] | 339 | for (j = 0; j < currentFrame.numOfVerts; j++) |
---|
[4140] | 340 | { |
---|
[4146] | 341 | currentFrame.pVerts[j].x = this->pFrames[i].pVertices[j].vertex[0]; |
---|
| 342 | currentFrame.pVerts[j].y = this->pFrames[i].pVertices[j].vertex[1]; |
---|
| 343 | currentFrame.pVerts[j].z = this->pFrames[i].pVertices[j].vertex[2]; |
---|
[4140] | 344 | } |
---|
[4087] | 345 | |
---|
[4140] | 346 | delete this->pFrames[i].pVertices; |
---|
[4087] | 347 | |
---|
[4140] | 348 | if( likely(i > 0)) |
---|
| 349 | { |
---|
[4146] | 350 | pModel->objectList.push_back(currentFrame); |
---|
[4140] | 351 | continue; |
---|
| 352 | } |
---|
[4087] | 353 | |
---|
[4146] | 354 | currentFrame.pTexVerts = new CVector2[currentFrame.numTexVertex]; |
---|
| 355 | currentFrame.pFaces = new tFace[currentFrame.numOfFaces]; |
---|
[4087] | 356 | |
---|
[4146] | 357 | for(j = 0; j < currentFrame.numTexVertex; j++) |
---|
[4140] | 358 | { |
---|
[4146] | 359 | currentFrame.pTexVerts[j].x = this->pTexCoords[j].u / float(this->header.skinWidth); |
---|
| 360 | currentFrame.pTexVerts[j].y = 1 - this->pTexCoords[j].v / float(this->header.skinHeight); |
---|
[4140] | 361 | } |
---|
[4087] | 362 | |
---|
[4146] | 363 | for(j = 0; j < currentFrame.numOfFaces; j++) |
---|
[4140] | 364 | { |
---|
[4146] | 365 | currentFrame.pFaces[j].vertIndex[0] = this->pTriangles[j].vertexIndices[0]; |
---|
| 366 | currentFrame.pFaces[j].vertIndex[1] = this->pTriangles[j].vertexIndices[1]; |
---|
| 367 | currentFrame.pFaces[j].vertIndex[2] = this->pTriangles[j].vertexIndices[2]; |
---|
[4087] | 368 | |
---|
[4146] | 369 | currentFrame.pFaces[j].coordIndex[0] = this->pTriangles[j].textureIndices[0]; |
---|
| 370 | currentFrame.pFaces[j].coordIndex[1] = this->pTriangles[j].textureIndices[1]; |
---|
| 371 | currentFrame.pFaces[j].coordIndex[2] = this->pTriangles[j].textureIndices[2]; |
---|
[4087] | 372 | } |
---|
[4146] | 373 | pModel->objectList.push_back(currentFrame); |
---|
[4140] | 374 | } |
---|
[4087] | 375 | } |
---|
| 376 | |
---|
| 377 | |
---|
[4076] | 378 | /** |
---|
[4140] | 379 | \brief this function conputes the normals of the model |
---|
| 380 | \param model |
---|
[4076] | 381 | */ |
---|
| 382 | void MD2Loader::computeNormals(t3DModel *pModel) |
---|
[4148] | 383 | { |
---|
[4149] | 384 | CVector3 vVector1, vVector2, vNormal, vPoly[3]; |
---|
[4076] | 385 | |
---|
[4149] | 386 | if( unlikely(pModel->numOfObjects <= 0)) |
---|
| 387 | return; |
---|
| 388 | /* now computing face normals: this means just averaging the vertex normals of a face */ |
---|
| 389 | /* so for every object: */ |
---|
| 390 | for(int index = 0; index < pModel->numOfObjects; index++) |
---|
| 391 | { |
---|
| 392 | t3DObject *pObject = &(pModel->objectList[index]); |
---|
[4126] | 393 | |
---|
[4149] | 394 | /* allocate all the memory we need to calculate the normals */ |
---|
| 395 | CVector3 *pNormals = new CVector3 [pObject->numOfFaces]; |
---|
| 396 | CVector3 *pTempNormals = new CVector3 [pObject->numOfFaces]; |
---|
| 397 | pObject->pNormals = new CVector3 [pObject->numOfVerts]; |
---|
[4148] | 398 | |
---|
[4149] | 399 | for(int i=0; i < pObject->numOfFaces; i++) |
---|
| 400 | { |
---|
| 401 | /* cache the points to make coding easier :) */ |
---|
| 402 | vPoly[0] = pObject->pVerts[pObject->pFaces[i].vertIndex[0]]; |
---|
| 403 | vPoly[1] = pObject->pVerts[pObject->pFaces[i].vertIndex[1]]; |
---|
| 404 | vPoly[2] = pObject->pVerts[pObject->pFaces[i].vertIndex[2]]; |
---|
[4148] | 405 | |
---|
[4149] | 406 | vVector1 = MathHelp::VectorDiff(vPoly[0], vPoly[2]); |
---|
| 407 | vVector2 = MathHelp::VectorDiff(vPoly[2], vPoly[1]); |
---|
[4148] | 408 | |
---|
[4149] | 409 | vNormal = MathHelp::CrossProduct(vVector1, vVector2); |
---|
| 410 | pTempNormals[i] = vNormal; |
---|
| 411 | vNormal = MathHelp::NormalizeVector(vNormal); |
---|
[4148] | 412 | |
---|
[4149] | 413 | pNormals[i] = vNormal; |
---|
| 414 | } |
---|
[4148] | 415 | |
---|
[4149] | 416 | /* now calculating vertex normals */ |
---|
| 417 | CVector3 vSum = {0.0, 0.0, 0.0}; |
---|
| 418 | CVector3 vZero = vSum; |
---|
| 419 | int shared=0; |
---|
[4148] | 420 | |
---|
[4149] | 421 | for (int i = 0; i < pObject->numOfVerts; i++) |
---|
| 422 | { |
---|
| 423 | for (int j = 0; j < pObject->numOfFaces; j++) |
---|
| 424 | { |
---|
| 425 | if (pObject->pFaces[j].vertIndex[0] == i || |
---|
| 426 | pObject->pFaces[j].vertIndex[1] == i || |
---|
| 427 | pObject->pFaces[j].vertIndex[2] == i) |
---|
[4148] | 428 | { |
---|
[4149] | 429 | vSum = MathHelp::AddVector(vSum, pTempNormals[j]); |
---|
| 430 | shared++; } |
---|
| 431 | } |
---|
| 432 | pObject->pNormals[i] = MathHelp::DivideVectorByScaler(vSum, float(-shared)); |
---|
| 433 | pObject->pNormals[i] = MathHelp::NormalizeVector(pObject->pNormals[i]); |
---|
[4148] | 434 | |
---|
[4149] | 435 | vSum = vZero; |
---|
| 436 | shared = 0; |
---|
[4148] | 437 | } |
---|
[4149] | 438 | delete [] pTempNormals; |
---|
| 439 | delete [] pNormals; |
---|
| 440 | } |
---|
[4148] | 441 | } |
---|
| 442 | |
---|
| 443 | |
---|
[4076] | 444 | /** |
---|
[4140] | 445 | \brief This function cleans up our allocated memory and closes the file |
---|
[4076] | 446 | */ |
---|
| 447 | void MD2Loader::cleanUp() |
---|
[4087] | 448 | { |
---|
[4140] | 449 | fclose(this->pFile); |
---|
[4087] | 450 | |
---|
[4140] | 451 | if( this->pSkins) delete [] this->pSkins; |
---|
| 452 | if( this->pTexCoords) delete this->pTexCoords; |
---|
| 453 | if( this->pTriangles) delete this->pTriangles; |
---|
| 454 | if( this->pFrames) delete this->pFrames; |
---|
[4087] | 455 | } |
---|
[4172] | 456 | |
---|
| 457 | |
---|
| 458 | /******************************************************************************** |
---|
| 459 | * MD2LOADER2 * |
---|
| 460 | ********************************************************************************/ |
---|
| 461 | |
---|
| 462 | MD2Model2::MD2Model2() |
---|
| 463 | { |
---|
| 464 | this->pVertices = NULL; |
---|
| 465 | this->pGLCommands = NULL; |
---|
| 466 | this->pLightNormals = NULL; |
---|
| 467 | |
---|
| 468 | this->numFrames = 0; |
---|
| 469 | this->numVertices = 0; |
---|
| 470 | this->numGLCommands = 0; |
---|
| 471 | |
---|
| 472 | this->textureID = 0; |
---|
| 473 | this->scaleFactor = 1.0f; |
---|
| 474 | |
---|
| 475 | //this->setAnim(0); |
---|
| 476 | } |
---|
| 477 | |
---|
| 478 | |
---|
| 479 | MD2Model2::~MD2Model2() |
---|
| 480 | { |
---|
| 481 | delete [] this->pVertices; |
---|
| 482 | delete [] this->pGLCommands; |
---|
| 483 | delete [] this->pLightNormals; |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | |
---|
| 487 | |
---|
| 488 | bool MD2Model2::loadModel(const char *fileName) |
---|
| 489 | { |
---|
| 490 | FILE *pFile; //file stream |
---|
| 491 | tMd2Header header; |
---|
| 492 | char* buffer; //buffer for frame data |
---|
| 493 | sFrame* frame; //temp frame |
---|
| 494 | sVec3D *pVertex; |
---|
| 495 | int* pNormals; |
---|
| 496 | |
---|
| 497 | pFile = fopen(fileName, "rb"); |
---|
| 498 | if( unlikely(!pFile)) |
---|
| 499 | { |
---|
| 500 | PRINTF(1)("Couldn't open the MD2 File for loading. Exiting.\n"); |
---|
| 501 | return false; |
---|
| 502 | } |
---|
| 503 | fread(&header, 1, sizeof(tMd2Header), pFile); |
---|
| 504 | /* check for the header version: make sure its a md2 file :) */ |
---|
| 505 | if( unlikely(header.version != MD2_VERSION) && unlikely(header.ident != MD2_IDENT)) |
---|
| 506 | { |
---|
| 507 | PRINTF(1)("Couldn't load file %s: invalid file format: stop loading\n", fileName); |
---|
| 508 | return false; |
---|
| 509 | } |
---|
| 510 | |
---|
| 511 | /* got the data: map it to locals */ |
---|
| 512 | this->numFrames = header.numFrames; |
---|
| 513 | this->numVertices = header.numVertices; |
---|
| 514 | this->numGLCommands = header.numGlCommands; |
---|
| 515 | /* allocate memory for the data storage */ |
---|
| 516 | this->pVertices = new sVec3D[this->numVertices * this->numFrames]; |
---|
| 517 | this->pGLCommands = new int[this->numGLCommands]; |
---|
| 518 | this->pLightNormals = new int[this->numVertices * this->numFrames]; |
---|
| 519 | buffer = new char[this->numFrames * header.frameSize]; |
---|
| 520 | |
---|
| 521 | /* read frame data from the file to a temp buffer */ |
---|
| 522 | fseek(pFile, header.offsetFrames, SEEK_SET); |
---|
| 523 | fread(buffer, header.frameSize, this->numFrames, pFile); |
---|
| 524 | /* read opengl commands */ |
---|
| 525 | fseek(pFile, header.offsetGlCommands, SEEK_SET); |
---|
| 526 | fread(this->pGLCommands, sizeof(int), this->numGLCommands, pFile); |
---|
| 527 | |
---|
| 528 | for(int i = 0; i < this->numFrames; ++i) |
---|
| 529 | { |
---|
| 530 | frame = (sFrame*)(buffer + header.frameSize * i); |
---|
| 531 | pVertex = this->pVertices + this->numVertices * i; |
---|
| 532 | pNormals = this->pLightNormals + this->numVertices * i; |
---|
| 533 | |
---|
| 534 | for(int j = 0; j < this->numVertices; ++j) |
---|
| 535 | { |
---|
| 536 | /* SPEEDUP: *(pVerts + i + 0) = (*(frame->pVertices + i + 0)... */ |
---|
| 537 | pVertex[i][0] = (frame->pVertices[i].v[0] * frame->scale[0]) + frame->translate[0]; |
---|
| 538 | pVertex[i][1] = (frame->pVertices[i].v[1] * frame->scale[1]) + frame->translate[1]; |
---|
| 539 | pVertex[i][2] = (frame->pVertices[i].v[2] * frame->scale[2]) + frame->translate[2]; |
---|
| 540 | |
---|
| 541 | pNormals[i] = frame->pVertices[i].lightNormalIndex; |
---|
| 542 | } |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | delete [] buffer; |
---|
| 546 | fclose(pFile); |
---|
| 547 | } |
---|
| 548 | |
---|
| 549 | |
---|
| 550 | bool MD2Model2::loadSkin(const char* fileName) |
---|
| 551 | { |
---|
| 552 | this->material = new Material("md2ModelTest"); |
---|
| 553 | this->material->setDiffuseMap(fileName); |
---|
| 554 | this->material->setIllum(3); |
---|
| 555 | this->material->setAmbient(1.0, 1.0, 1.0); |
---|
| 556 | } |
---|
| 557 | |
---|
| 558 | |
---|
| 559 | void MD2Model2::draw() |
---|
| 560 | { |
---|
| 561 | glPushMatrix(); |
---|
| 562 | /* rotate because id software uses another orientation then the openGL default */ |
---|
| 563 | /* \todo: rotate the axis already when loading... easy :) */ |
---|
| 564 | glRotatef(-90.0, 1.0, 0.0, 0.0); |
---|
| 565 | glRotatef(-90.0, 0.0, 0.0, 1.0); |
---|
| 566 | |
---|
| 567 | this->renderFrame(); |
---|
| 568 | |
---|
| 569 | glPopMatrix(); |
---|
| 570 | } |
---|
| 571 | |
---|
| 572 | |
---|
| 573 | /** |
---|
| 574 | \brief initializes an array of vert with the current frame scaled vertices |
---|
| 575 | |
---|
| 576 | we won't use the pVertices array directly, since its much easier and we need |
---|
| 577 | saving of data anyway |
---|
| 578 | */ |
---|
| 579 | void MD2Model2::interpolate(sVec3D* verticesList) |
---|
| 580 | { |
---|
| 581 | for(int i = 0; i < this->numFrames; ++i) |
---|
| 582 | { |
---|
| 583 | verticesList[i][0] = this->pVertices[i + (this->numFrames * this->animationState.currentFrame)][0] * this->scaleFactor; |
---|
| 584 | verticesList[i][1] = this->pVertices[i + (this->numFrames * this->animationState.currentFrame)][1] * this->scaleFactor; |
---|
| 585 | verticesList[i][2] = this->pVertices[i + (this->numFrames * this->animationState.currentFrame)][2] * this->scaleFactor; |
---|
| 586 | } |
---|
| 587 | } |
---|
| 588 | |
---|
| 589 | |
---|
| 590 | /* hhmmm... id used a very different way to do lightning...*/ |
---|
| 591 | void MD2Model2::processLighting() |
---|
| 592 | { |
---|
| 593 | } |
---|
| 594 | |
---|
| 595 | |
---|
| 596 | void MD2Model2::renderFrame() |
---|
| 597 | { |
---|
| 598 | static sVec3D verticesList[MD2_MAX_VERTICES]; /* performance: created only once in a lifetime */ |
---|
| 599 | int* pCommands = this->pGLCommands; |
---|
| 600 | |
---|
| 601 | /* some face culling stuff */ |
---|
| 602 | glPushAttrib(GL_POLYGON_BIT); |
---|
| 603 | glFrontFace(GL_CW); |
---|
| 604 | glEnable(GL_CULL_FACE); |
---|
| 605 | glCullFace(GL_BACK); |
---|
| 606 | |
---|
| 607 | this->interpolate(verticesList); |
---|
| 608 | this->material->select(); |
---|
| 609 | |
---|
| 610 | /* draw the triangles */ |
---|
| 611 | /* \todo: take int i out of while loop */ |
---|
| 612 | while( int i = *(pCommands++)) /* strange looking while loop for maximum performance */ |
---|
| 613 | { |
---|
| 614 | if( i < 0) |
---|
| 615 | { |
---|
| 616 | glBegin(GL_TRIANGLE_FAN); |
---|
| 617 | i = -i; |
---|
| 618 | } |
---|
| 619 | else |
---|
| 620 | { |
---|
| 621 | glBegin(GL_TRIANGLE_STRIP); |
---|
| 622 | } |
---|
| 623 | |
---|
| 624 | for(; i > 0; --i, pCommands += 3) /* down counting for loop, next 3 gl commands */ |
---|
| 625 | { |
---|
| 626 | /* for quake2 lighting */ |
---|
| 627 | //float l = shadedots[this->pLightNormals[pCommands[2]]]; |
---|
| 628 | //glColor3f(l * lcolor[0], l * lcolor[1], l * lcolor[2]); |
---|
| 629 | |
---|
| 630 | glTexCoord2f( ((float *)pCommands)[0], ((float *)pCommands)[1] ); |
---|
| 631 | //glNormal3fv(anorms[this->pLightNormals[2]]); |
---|
| 632 | glVertex3fv(verticesList[pCommands[2]]); |
---|
| 633 | } |
---|
| 634 | glEnd(); |
---|
| 635 | } |
---|
| 636 | glDisable(GL_CULL_FACE); |
---|
| 637 | glPopAttrib(); |
---|
| 638 | } |
---|
| 639 | |
---|