[8342] | 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 | |
---|
| 15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER |
---|
| 16 | |
---|
[8346] | 17 | #include "md3_model.h" |
---|
[8342] | 18 | |
---|
[8354] | 19 | #include "md3_data.h" |
---|
[8552] | 20 | #include "md3_mesh.h" |
---|
| 21 | #include "md3_tag.h" |
---|
[8553] | 22 | #include "md3_bone_frame.h" |
---|
[8342] | 23 | |
---|
[8533] | 24 | #include "md3_animation_cfg.h" |
---|
[8342] | 25 | |
---|
[8552] | 26 | #include "material.h" |
---|
[8560] | 27 | #include "quaternion.h" |
---|
[8552] | 28 | |
---|
[8590] | 29 | #include "loading/resource_manager.h" |
---|
| 30 | |
---|
[8562] | 31 | #include "debug.h" |
---|
| 32 | |
---|
[8351] | 33 | namespace md3 |
---|
| 34 | { |
---|
| 35 | |
---|
| 36 | /** |
---|
| 37 | * md3 model |
---|
| 38 | */ |
---|
[8353] | 39 | MD3Model::MD3Model(std::string filename, float scaling) |
---|
| 40 | { |
---|
[8579] | 41 | this->autoAssemblePlayerModel(filename, scaling); |
---|
[8556] | 42 | |
---|
[8567] | 43 | this->bDrawBones = false; |
---|
| 44 | this->bDrawNormals = false; |
---|
[8353] | 45 | } |
---|
[8351] | 46 | |
---|
| 47 | |
---|
[8353] | 48 | |
---|
[8351] | 49 | MD3Model::~MD3Model() |
---|
[8556] | 50 | { |
---|
[8634] | 51 | // delete this->tmpBoneFrame; |
---|
[8564] | 52 | // delete [] this->tmpMesh; |
---|
| 53 | |
---|
| 54 | ///TODO deleting mesh |
---|
| 55 | ///TODO deleting matrices |
---|
[8556] | 56 | } |
---|
[8351] | 57 | |
---|
| 58 | |
---|
[8579] | 59 | /** |
---|
| 60 | * auto assemples a player model |
---|
[8590] | 61 | * @param filename is the name to the directory of the modelzzzzzz |
---|
[8579] | 62 | */ |
---|
| 63 | void MD3Model::autoAssemblePlayerModel(std::string filename, float scaling) |
---|
| 64 | { |
---|
[8592] | 65 | // loading the config file |
---|
[8591] | 66 | std::string cfgName(filename + "/animation.cfg"); |
---|
| 67 | this->config = (MD3AnimationCfg*)ResourceManager::getInstance()->load(cfgName, MD3_CONFIG, RP_GAME); |
---|
[8580] | 68 | |
---|
[8592] | 69 | //first load the torso or the upper part |
---|
| 70 | std::string nameUpper(filename + "/upper.md3"); |
---|
| 71 | if( (this->md3Data = (MD3Data*)ResourceManager::getInstance()->load(nameUpper, MD3, RP_GAME, nameUpper, scaling)) == NULL) |
---|
| 72 | { |
---|
| 73 | std::string nameTorso(filename + "/torso.md3"); |
---|
| 74 | this->md3Data = (MD3Data*)ResourceManager::getInstance()->load(nameTorso, MD3, RP_GAME, nameTorso, scaling); |
---|
| 75 | } |
---|
[8580] | 76 | |
---|
[8592] | 77 | if( this->md3Data == NULL) |
---|
| 78 | { |
---|
| 79 | PRINTF(1)("Problems loading the MD3Model. Abording\n"); |
---|
| 80 | return; |
---|
| 81 | } |
---|
[8590] | 82 | |
---|
[8592] | 83 | // load lower |
---|
| 84 | std::string nameLower(filename + "/lower.md3"); |
---|
| 85 | MD3Data* lower = (MD3Data*)ResourceManager::getInstance()->load(nameLower, MD3, RP_GAME, nameLower, scaling); |
---|
| 86 | if( lower != NULL) |
---|
| 87 | { |
---|
[8597] | 88 | int tag = this->md3Data->getTagIndexByName("tag_lower"); |
---|
| 89 | PRINTF(0)("Loaded the %s model on index %i\n", nameLower.c_str(), tag); |
---|
| 90 | if( tag >= 0) |
---|
| 91 | this->md3Data->addLinkedModel(tag, lower); |
---|
| 92 | else |
---|
| 93 | PRINTF(0)("Could not add %s\n", nameLower.c_str()); |
---|
[8592] | 94 | } |
---|
[8590] | 95 | |
---|
| 96 | |
---|
[8592] | 97 | // load head |
---|
| 98 | std::string nameHead(filename + "/head.md3"); |
---|
| 99 | MD3Data* head = (MD3Data*)ResourceManager::getInstance()->load(nameHead, MD3, RP_GAME, nameLower, scaling); |
---|
[8597] | 100 | if( head != NULL) |
---|
[8592] | 101 | { |
---|
[8597] | 102 | int tag = this->md3Data->getTagIndexByName("tag_head"); |
---|
| 103 | PRINTF(0)("Loaded the %s model on index %i\n", nameHead.c_str(), tag); |
---|
| 104 | if( tag >= 0) |
---|
| 105 | this->md3Data->addLinkedModel(tag, head); |
---|
| 106 | else |
---|
| 107 | PRINTF(0)("Could not add %s\n", nameHead.c_str()); |
---|
[8592] | 108 | } |
---|
[8581] | 109 | |
---|
[8579] | 110 | } |
---|
[8549] | 111 | |
---|
[8563] | 112 | |
---|
[8579] | 113 | |
---|
[8597] | 114 | /** |
---|
| 115 | * tick float |
---|
| 116 | * @param time: time elapsed |
---|
| 117 | */ |
---|
[8563] | 118 | void MD3Model::tick(float time) |
---|
| 119 | { |
---|
| 120 | |
---|
[8567] | 121 | this->tick(time, this->md3Data); |
---|
[8563] | 122 | } |
---|
| 123 | |
---|
[8567] | 124 | |
---|
[8597] | 125 | /** |
---|
| 126 | * tick each data |
---|
| 127 | */ |
---|
[8563] | 128 | void MD3Model::tick(float time, MD3Data* data) |
---|
| 129 | { |
---|
| 130 | // draw the bones if needed |
---|
| 131 | if( this->bDrawBones) |
---|
| 132 | { |
---|
| 133 | // get bone frame, interpolate if necessary |
---|
| 134 | if( data->animationState.interpolationFraction != 0.0 && |
---|
[8568] | 135 | data->animationState.currentFrame != data->animationState.nextFrame) |
---|
| 136 | { |
---|
[8563] | 137 | //interpolate bone frame |
---|
[8634] | 138 | data->tmpBoneFrame = this->interpolateBoneFrame(data, data->boneFrames[data->animationState.currentFrame], |
---|
[8576] | 139 | data->boneFrames[data->animationState.nextFrame], |
---|
| 140 | data->animationState.interpolationFraction); |
---|
[8568] | 141 | } |
---|
[8576] | 142 | else |
---|
| 143 | { |
---|
[8634] | 144 | data->tmpBoneFrame = data->boneFrames[data->animationState.currentFrame]; |
---|
[8576] | 145 | } |
---|
[8563] | 146 | } |
---|
| 147 | |
---|
| 148 | //draw all meshes of current frame of this model |
---|
[8566] | 149 | for( int i = 0; i < data->header->meshNum; i++) |
---|
[8563] | 150 | { |
---|
| 151 | MD3Mesh* mesh = data->meshes[i]; |
---|
| 152 | |
---|
| 153 | // get mesh frame, do interpolation if necessary |
---|
| 154 | sVec3D* frame; |
---|
| 155 | if( data->animationState.interpolationFraction != 0.0 && |
---|
[8576] | 156 | data->animationState.currentFrame != data->animationState.nextFrame) |
---|
| 157 | { |
---|
[8563] | 158 | //interpolate mesh frame between the 2 current mesh frames |
---|
[8634] | 159 | frame = this->interpolateMeshFrame( data, data->meshes[data->animationState.currentFrame]->meshFrames, |
---|
[8563] | 160 | data->meshes[data->animationState.nextFrame]->meshFrames, |
---|
| 161 | data->animationState.interpolationFraction, mesh, i); |
---|
[8576] | 162 | } |
---|
| 163 | else |
---|
| 164 | { |
---|
| 165 | //no interpolation needed, just draw current frame |
---|
| 166 | frame = &mesh->meshFrames[data->animationState.currentFrame]; |
---|
| 167 | } |
---|
[8634] | 168 | data->tmpMesh[i] = frame; |
---|
[8563] | 169 | |
---|
[8576] | 170 | // draw vertex normals if needed |
---|
| 171 | if( this->bDrawNormals) |
---|
| 172 | { |
---|
| 173 | // get vertex normals, interpolate if necessary |
---|
| 174 | if( data->animationState.interpolationFraction != 0.0 && |
---|
| 175 | data->animationState.currentFrame != data->animationState.nextFrame) |
---|
| 176 | { |
---|
| 177 | //interpolate vertex normals |
---|
[8634] | 178 | this->interpolateVertexNormals(data, &mesh->normals[data->animationState.currentFrame], |
---|
[8576] | 179 | &mesh->normals[data->animationState.nextFrame], |
---|
| 180 | data->animationState.interpolationFraction, mesh, i); |
---|
| 181 | } |
---|
| 182 | } |
---|
[8563] | 183 | } |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | // draw all models linked to this model |
---|
| 187 | std::map<int, MD3Data*>::iterator it = data->sortedMap.begin(); |
---|
[8564] | 188 | int i = 0; |
---|
[8563] | 189 | while( it != data->sortedMap.end()) |
---|
| 190 | { |
---|
| 191 | MD3Data* child = it->second; |
---|
| 192 | |
---|
| 193 | //build transformation array m from matrix, interpolate if necessary |
---|
| 194 | |
---|
| 195 | MD3Tag* currFrameTag = data->boneFrames[data->animationState.currentFrame]->tags[child->parentTagIndex]; |
---|
| 196 | |
---|
| 197 | if( data->animationState.interpolationFraction != 0.0 && |
---|
[8600] | 198 | data->animationState.currentFrame != data->animationState.nextFrame) |
---|
| 199 | { |
---|
[8563] | 200 | //we need to interpolate |
---|
| 201 | MD3Tag* nextFrameTag = data->boneFrames[data->animationState.nextFrame]->tags[child->parentTagIndex]; |
---|
[8634] | 202 | this->interpolateTransformation(child, currFrameTag, nextFrameTag, data->animationState.interpolationFraction, i); |
---|
[8597] | 203 | } |
---|
| 204 | else |
---|
| 205 | { |
---|
| 206 | //no interpolation needed, stay with last transformation |
---|
| 207 | //OpenGL matrix is in column-major order |
---|
[8634] | 208 | data->tmpMatrix[i][0] = currFrameTag->matrix[0][0]; |
---|
| 209 | data->tmpMatrix[i][1] = currFrameTag->matrix[1][0]; |
---|
| 210 | data->tmpMatrix[i][2] = currFrameTag->matrix[2][0]; |
---|
| 211 | data->tmpMatrix[i][3] = 0.0f; |
---|
| 212 | data->tmpMatrix[i][4] = currFrameTag->matrix[0][1]; |
---|
| 213 | data->tmpMatrix[i][5] = currFrameTag->matrix[1][1]; |
---|
| 214 | data->tmpMatrix[i][6] = currFrameTag->matrix[2][1]; |
---|
| 215 | data->tmpMatrix[i][7] = 0.0f; |
---|
| 216 | data->tmpMatrix[i][8] = currFrameTag->matrix[0][2]; |
---|
| 217 | data->tmpMatrix[i][9] = currFrameTag->matrix[1][2]; |
---|
| 218 | data->tmpMatrix[i][10]= currFrameTag->matrix[2][2]; |
---|
| 219 | data->tmpMatrix[i][11]= 0.0f; |
---|
| 220 | data->tmpMatrix[i][12] = currFrameTag->position.x; |
---|
| 221 | data->tmpMatrix[i][13] = currFrameTag->position.y; |
---|
| 222 | data->tmpMatrix[i][14] = currFrameTag->position.z; |
---|
| 223 | data->tmpMatrix[i][15] = 1.0f; |
---|
[8597] | 224 | } |
---|
[8563] | 225 | |
---|
[8597] | 226 | // switch to child coord system |
---|
[8563] | 227 | |
---|
[8597] | 228 | // and tick child |
---|
| 229 | this->tick(time, child); |
---|
[8563] | 230 | |
---|
[8597] | 231 | i++; |
---|
| 232 | it++; |
---|
[8563] | 233 | } |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | |
---|
[8549] | 237 | /** |
---|
| 238 | * this draws the md3 model |
---|
| 239 | */ |
---|
[8565] | 240 | void MD3Model::draw() const |
---|
[8549] | 241 | { |
---|
[8602] | 242 | PRINTF(0)("\ndraw========================\n"); |
---|
[8549] | 243 | //draw current bone frame |
---|
[8565] | 244 | this->draw(this->md3Data); |
---|
[8550] | 245 | } |
---|
[8549] | 246 | |
---|
| 247 | |
---|
[8550] | 248 | /** |
---|
| 249 | * draw the md3model |
---|
| 250 | * @param data: the data to be drawn |
---|
| 251 | */ |
---|
[8565] | 252 | void MD3Model::draw(MD3Data* data) const |
---|
[8551] | 253 | { |
---|
[8556] | 254 | |
---|
| 255 | // draw the bones if needed |
---|
[8551] | 256 | if( this->bDrawBones) |
---|
| 257 | { |
---|
| 258 | // get bone frame, interpolate if necessary |
---|
| 259 | if( data->animationState.interpolationFraction != 0.0 && |
---|
| 260 | data->animationState.currentFrame != data->animationState.nextFrame) { |
---|
| 261 | //interpolate bone frame |
---|
[8634] | 262 | this->drawBoneFrame(data->tmpBoneFrame); |
---|
[8551] | 263 | } |
---|
| 264 | else { |
---|
| 265 | //stick with current bone frame |
---|
| 266 | this->drawBoneFrame(data->boneFrames[data->animationState.currentFrame]); |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | |
---|
[8556] | 270 | |
---|
[8552] | 271 | //draw all meshes of current frame of this model |
---|
[8566] | 272 | for( int i = 0; i < data->header->meshNum; i++) |
---|
[8556] | 273 | { |
---|
[8552] | 274 | MD3Mesh* mesh = data->meshes[i]; |
---|
[8551] | 275 | |
---|
[8552] | 276 | if( mesh->header->textureNum > 0 && &mesh->material[0] != NULL) |
---|
| 277 | mesh->material[0].select(); |
---|
| 278 | |
---|
| 279 | // get mesh frame, do interpolation if necessary |
---|
[8634] | 280 | sVec3D* frame = data->tmpMesh[i]; |
---|
[8552] | 281 | |
---|
[8565] | 282 | this->drawMesh(mesh, frame); |
---|
[8564] | 283 | |
---|
[8556] | 284 | // draw vertex normals if needed |
---|
[8600] | 285 | if( this->bDrawNormals) |
---|
| 286 | { |
---|
[8552] | 287 | // get vertex normals, interpolate if necessary |
---|
| 288 | if( data->animationState.interpolationFraction != 0.0 && |
---|
[8600] | 289 | data->animationState.currentFrame != data->animationState.nextFrame) |
---|
| 290 | { |
---|
[8552] | 291 | //interpolate vertex normals |
---|
[8634] | 292 | this->drawVertexNormals(frame, data->tmpNormal[i]); |
---|
[8600] | 293 | } |
---|
| 294 | else { |
---|
[8565] | 295 | //stick with current vertex normals |
---|
[8600] | 296 | this->drawVertexNormals(frame, &mesh->normals[data->animationState.currentFrame]); |
---|
| 297 | } |
---|
[8552] | 298 | } |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | |
---|
[8556] | 302 | // draw all models linked to this model |
---|
[8565] | 303 | int i = 0; |
---|
[8552] | 304 | std::map<int, MD3Data*>::iterator it = data->sortedMap.begin(); |
---|
[8556] | 305 | while( it != data->sortedMap.end()) |
---|
| 306 | { |
---|
[8552] | 307 | MD3Data* child = it->second; |
---|
| 308 | |
---|
[8556] | 309 | //switch to child coord system |
---|
[8637] | 310 | glPushMatrix(); |
---|
| 311 | glMultMatrixf(data->tmpMatrix[i]); |
---|
| 312 | |
---|
[8556] | 313 | // and draw child |
---|
| 314 | this->draw(child); |
---|
| 315 | |
---|
[8637] | 316 | glPopMatrix(); |
---|
| 317 | |
---|
[8565] | 318 | i++; |
---|
[8597] | 319 | it++; |
---|
[8552] | 320 | } |
---|
[8600] | 321 | |
---|
[8551] | 322 | } |
---|
| 323 | |
---|
| 324 | |
---|
[8552] | 325 | /** |
---|
| 326 | * draws the mesh |
---|
| 327 | */ |
---|
[8565] | 328 | void MD3Model::drawMesh(MD3Mesh* mesh, sVec3D* frame) const |
---|
[8562] | 329 | { |
---|
[8602] | 330 | PRINTF(0)("drawMesh: %s\n", mesh->header->name); |
---|
| 331 | Vector tmpVec1, tmpVec2; |
---|
| 332 | |
---|
[8562] | 333 | glColor3f(1.0f, 1.0f, 1.0f); |
---|
[8579] | 334 | glBegin( GL_TRIANGLES); |
---|
[8552] | 335 | |
---|
[8562] | 336 | // upload all triangles in the frame to OpenGL |
---|
[8579] | 337 | for( int t = 0; t < mesh->header->triangleNum; t++) |
---|
| 338 | { |
---|
[8562] | 339 | // calc normal vector |
---|
[8565] | 340 | tmpVec1.x = frame[mesh->triangles[t].vertexOffset[1]][0] - frame[mesh->triangles[t].vertexOffset[0]][0]; |
---|
[8579] | 341 | tmpVec1.y = frame[mesh->triangles[t].vertexOffset[1]][1] - frame[mesh->triangles[t].vertexOffset[0]][1]; |
---|
| 342 | tmpVec1.z = frame[mesh->triangles[t].vertexOffset[1]][2] - frame[mesh->triangles[t].vertexOffset[0]][2]; |
---|
| 343 | |
---|
[8565] | 344 | tmpVec2.x = frame[mesh->triangles[t].vertexOffset[2]][0] - frame[mesh->triangles[t].vertexOffset[0]][0]; |
---|
| 345 | tmpVec2.y = frame[mesh->triangles[t].vertexOffset[2]][1] - frame[mesh->triangles[t].vertexOffset[0]][1]; |
---|
| 346 | tmpVec2.z = frame[mesh->triangles[t].vertexOffset[2]][2] - frame[mesh->triangles[t].vertexOffset[0]][2]; |
---|
[8562] | 347 | |
---|
[8565] | 348 | Vector normal = tmpVec1.cross(tmpVec2); |
---|
[8562] | 349 | normal.normalize(); |
---|
| 350 | |
---|
[8579] | 351 | // PRINTF(0)("normal: %f, %f, %f\n", normal.x, normal.y, normal.z); |
---|
| 352 | |
---|
[8562] | 353 | glNormal3f(normal.x, normal.y, normal.z); |
---|
| 354 | glTexCoord2fv( mesh->texVecs[mesh->triangles[t].vertexOffset[0]].textureCoord); |
---|
| 355 | glVertex3f( frame[mesh->triangles[t].vertexOffset[0]][0], |
---|
[8578] | 356 | frame[mesh->triangles[t].vertexOffset[0]][2], |
---|
| 357 | frame[mesh->triangles[t].vertexOffset[0]][1]); |
---|
[8579] | 358 | |
---|
| 359 | glNormal3f(normal.x, normal.y, normal.z); |
---|
[8562] | 360 | glTexCoord2fv( mesh->texVecs[mesh->triangles[t].vertexOffset[1]].textureCoord); |
---|
| 361 | glVertex3f( frame[mesh->triangles[t].vertexOffset[1]][0], |
---|
[8578] | 362 | frame[mesh->triangles[t].vertexOffset[1]][2], |
---|
| 363 | frame[mesh->triangles[t].vertexOffset[1]][1]); |
---|
[8579] | 364 | |
---|
| 365 | glNormal3f(normal.x, normal.y, normal.z); |
---|
[8562] | 366 | glTexCoord2fv( mesh->texVecs[mesh->triangles[t].vertexOffset[2]].textureCoord); |
---|
| 367 | glVertex3f( frame[mesh->triangles[t].vertexOffset[2]][0], |
---|
[8578] | 368 | frame[mesh->triangles[t].vertexOffset[2]][2], |
---|
| 369 | frame[mesh->triangles[t].vertexOffset[2]][1]); |
---|
[8562] | 370 | } |
---|
[8579] | 371 | glEnd(); |
---|
[8562] | 372 | } |
---|
| 373 | |
---|
| 374 | |
---|
[8552] | 375 | /** |
---|
| 376 | * drawo vertex normals |
---|
| 377 | */ |
---|
[8565] | 378 | void MD3Model::drawVertexNormals(sVec3D* frame, MD3Normal* normals) const |
---|
[8552] | 379 | {} |
---|
| 380 | |
---|
| 381 | |
---|
| 382 | /** |
---|
[8557] | 383 | * draw bone frame |
---|
| 384 | */ |
---|
[8565] | 385 | void MD3Model::drawBoneFrame(MD3BoneFrame* frame) const |
---|
[8561] | 386 | { |
---|
| 387 | float x1 = frame->mins.x; |
---|
| 388 | float y1 = frame->mins.y; |
---|
| 389 | float z1 = frame->mins.z; |
---|
| 390 | float x2 = frame->maxs.x; |
---|
| 391 | float y2 = frame->maxs.y; |
---|
| 392 | float z2 = frame->maxs.z; |
---|
[8557] | 393 | |
---|
[8579] | 394 | glPushAttrib(GL_TEXTURE_2D); |
---|
| 395 | glPushAttrib(GL_LIGHTING); |
---|
[8557] | 396 | |
---|
[8561] | 397 | glColor3f(1.0f,0.0f,0.0f); |
---|
| 398 | glPointSize(6.0f); |
---|
| 399 | |
---|
| 400 | glBegin(GL_POINTS); |
---|
| 401 | glVertex3f(frame->position.x, frame->position.y, frame->position.z); |
---|
| 402 | glEnd(); |
---|
| 403 | glPointSize(1.0f); |
---|
| 404 | |
---|
| 405 | glColor3f(0.0f,1.0f,0.0f); |
---|
| 406 | glBegin(GL_LINE_LOOP); |
---|
| 407 | glVertex3f(x1,y1,z1); |
---|
| 408 | glVertex3f(x1,y1,z2); |
---|
| 409 | glVertex3f(x1,y2,z2); |
---|
| 410 | glVertex3f(x1,y2,z1); |
---|
| 411 | glEnd(); |
---|
| 412 | |
---|
| 413 | glBegin(GL_LINE_LOOP); |
---|
| 414 | glVertex3f(x2,y2,z2); |
---|
| 415 | glVertex3f(x2,y1,z2); |
---|
| 416 | glVertex3f(x2,y1,z1); |
---|
| 417 | glVertex3f(x2,y2,z1); |
---|
| 418 | glEnd(); |
---|
| 419 | |
---|
| 420 | glBegin(GL_LINES); |
---|
| 421 | glVertex3f(x1,y1,z1); |
---|
| 422 | glVertex3f(x2,y1,z1); |
---|
| 423 | |
---|
| 424 | glVertex3f(x1,y1,z2); |
---|
| 425 | glVertex3f(x2,y1,z2); |
---|
| 426 | |
---|
| 427 | glVertex3f(x1,y2,z2); |
---|
| 428 | glVertex3f(x2,y2,z2); |
---|
| 429 | |
---|
| 430 | glVertex3f(x1,y2,z1); |
---|
| 431 | glVertex3f(x2,y2,z1); |
---|
| 432 | glEnd(); |
---|
| 433 | |
---|
[8579] | 434 | glPopAttrib(); |
---|
| 435 | glPopAttrib(); |
---|
[8561] | 436 | } |
---|
| 437 | |
---|
| 438 | |
---|
[8557] | 439 | /** |
---|
[8552] | 440 | * interpolate bone frame |
---|
[8556] | 441 | * @param currBoneFrame Start bone frame. |
---|
| 442 | * @param nextBoneFrame End bone frame. |
---|
| 443 | * @param frac Interpolation fraction, in [0,1]. |
---|
[8552] | 444 | */ |
---|
[8634] | 445 | MD3BoneFrame* MD3Model::interpolateBoneFrame(MD3Data* data, MD3BoneFrame* currBoneFrame, MD3BoneFrame* nextBoneFrame, float frac) |
---|
[8556] | 446 | { |
---|
[8634] | 447 | data->tmpBoneFrame->mins.x = (1.0f - frac) * currBoneFrame->mins.x + frac * nextBoneFrame->mins.x; |
---|
| 448 | data->tmpBoneFrame->maxs.x = (1.0f - frac) * currBoneFrame->maxs.x + frac * nextBoneFrame->maxs.x; |
---|
| 449 | data->tmpBoneFrame->position.x = (1.0f - frac) * currBoneFrame->position.x + frac * nextBoneFrame->position.x; |
---|
| 450 | data->tmpBoneFrame->mins.y = (1.0f - frac) * currBoneFrame->mins.y + frac * nextBoneFrame->mins.y; |
---|
| 451 | data->tmpBoneFrame->maxs.y = (1.0f - frac) * currBoneFrame->maxs.y + frac * nextBoneFrame->maxs.y; |
---|
| 452 | data->tmpBoneFrame->position.y = (1.0f - frac) * currBoneFrame->position.y + frac * nextBoneFrame->position.y; |
---|
| 453 | data->tmpBoneFrame->mins.z = (1.0f - frac) * currBoneFrame->mins.z + frac * nextBoneFrame->mins.z; |
---|
| 454 | data->tmpBoneFrame->maxs.z = (1.0f - frac) * currBoneFrame->maxs.z + frac * nextBoneFrame->maxs.z; |
---|
| 455 | data->tmpBoneFrame->position.z = (1.0f - frac) * currBoneFrame->position.z + frac * nextBoneFrame->position.z; |
---|
[8549] | 456 | |
---|
[8634] | 457 | return data->tmpBoneFrame; |
---|
[8556] | 458 | } |
---|
| 459 | |
---|
| 460 | |
---|
[8563] | 461 | |
---|
[8552] | 462 | /** |
---|
[8557] | 463 | * interpolate mesh frame |
---|
[8552] | 464 | */ |
---|
[8634] | 465 | sVec3D* MD3Model::interpolateMeshFrame(MD3Data* data, sVec3D* currMeshFrame, sVec3D* nextMeshFrame, float frac, MD3Mesh* mesh, int i) |
---|
[8557] | 466 | { |
---|
| 467 | int vertexNum = mesh->header->vertexNum; |
---|
[8549] | 468 | |
---|
[8557] | 469 | // calc interpolated vertices |
---|
| 470 | for( int t = 0; t < vertexNum * 3.0f; t++) |
---|
| 471 | { |
---|
[8634] | 472 | data->tmpMesh[i][t][0] = (1.0f - frac) * currMeshFrame[t][0] + frac * nextMeshFrame[t][0]; |
---|
| 473 | data->tmpMesh[i][t][1] = (1.0f - frac) * currMeshFrame[t][1] + frac * nextMeshFrame[t][1]; |
---|
| 474 | data->tmpMesh[i][t][2] = (1.0f - frac) * currMeshFrame[t][2] + frac * nextMeshFrame[t][2]; |
---|
[8557] | 475 | } |
---|
[8549] | 476 | |
---|
[8634] | 477 | return data->tmpMesh[i]; |
---|
[8557] | 478 | } |
---|
[8551] | 479 | |
---|
[8552] | 480 | |
---|
| 481 | /** |
---|
| 482 | * interpolate vertex normal |
---|
| 483 | */ |
---|
[8634] | 484 | MD3Normal* MD3Model::interpolateVertexNormals(MD3Data* data, MD3Normal* currNormals, MD3Normal* nextNormals, float frac, MD3Mesh* mesh, int i) |
---|
[8558] | 485 | { |
---|
[8565] | 486 | for( int j = 0; j < mesh->header->vertexNum; j++) |
---|
[8558] | 487 | { |
---|
[8634] | 488 | data->tmpNormal[i][j].vertexNormal[0] = (int)((1.0f - frac) * currNormals[j].vertexNormal[0] + frac * nextNormals[j].vertexNormal[0]); |
---|
| 489 | data->tmpNormal[i][j].vertexNormal[1] = (int)((1.0f - frac) * currNormals[j].vertexNormal[1] + frac * nextNormals[j].vertexNormal[1]); |
---|
[8558] | 490 | } |
---|
[8552] | 491 | |
---|
[8634] | 492 | return data->tmpNormal[i]; |
---|
[8558] | 493 | } |
---|
[8552] | 494 | |
---|
[8558] | 495 | |
---|
[8553] | 496 | /** |
---|
| 497 | * interpolate transformation |
---|
| 498 | */ |
---|
[8634] | 499 | float* MD3Model::interpolateTransformation(MD3Data* data, MD3Tag* currFrameTag, MD3Tag* nextFrameTag, float frac, int i) |
---|
[8560] | 500 | { |
---|
| 501 | // interpolate position |
---|
| 502 | Vector interpolatedPosition = currFrameTag->position * (1.0f - frac) + nextFrameTag->position * frac; |
---|
[8553] | 503 | |
---|
| 504 | |
---|
[8560] | 505 | // interpolate rotation matrix |
---|
| 506 | float currRot[4][4]; |
---|
| 507 | float nextRot[4][4]; |
---|
| 508 | float interpolatedMatrix[4][4]; |
---|
| 509 | |
---|
| 510 | Quaternion currQuat(currFrameTag->matrix); currQuat.matrix(currRot); |
---|
| 511 | Quaternion nextQuat(nextFrameTag->matrix); nextQuat.matrix(nextRot); |
---|
| 512 | |
---|
| 513 | Quaternion interpolatedQuat = Quaternion::quatSlerp(currQuat, nextQuat, frac); interpolatedQuat.matrix(interpolatedMatrix); |
---|
| 514 | |
---|
| 515 | // quaternion code is column based, so use transposed matrix when spitting out to gl |
---|
[8634] | 516 | data->tmpMatrix[i][0] = interpolatedMatrix[0][0]; |
---|
| 517 | data->tmpMatrix[i][4] = interpolatedMatrix[1][0]; |
---|
| 518 | data->tmpMatrix[i][8] = interpolatedMatrix[2][0]; |
---|
| 519 | data->tmpMatrix[i][12] = interpolatedPosition.x; |
---|
| 520 | data->tmpMatrix[i][1] = interpolatedMatrix[0][1]; |
---|
| 521 | data->tmpMatrix[i][5] = interpolatedMatrix[1][1]; |
---|
| 522 | data->tmpMatrix[i][9] = interpolatedMatrix[2][1]; |
---|
| 523 | data->tmpMatrix[i][13] = interpolatedPosition.y; |
---|
| 524 | data->tmpMatrix[i][2] = interpolatedMatrix[0][2]; |
---|
| 525 | data->tmpMatrix[i][6] = interpolatedMatrix[1][2]; |
---|
| 526 | data->tmpMatrix[i][10]= interpolatedMatrix[2][2]; |
---|
| 527 | data->tmpMatrix[i][14] = interpolatedPosition.z; |
---|
| 528 | data->tmpMatrix[i][3] = 0.0f; |
---|
| 529 | data->tmpMatrix[i][7] = 0.0f; |
---|
| 530 | data->tmpMatrix[i][11]= 0.0f; |
---|
| 531 | data->tmpMatrix[i][15] = 1.0f; |
---|
[8560] | 532 | |
---|
[8634] | 533 | return data->tmpMatrix[i]; |
---|
[8560] | 534 | |
---|
| 535 | } |
---|
| 536 | |
---|
| 537 | |
---|
[8637] | 538 | |
---|
| 539 | |
---|
| 540 | /** |
---|
| 541 | * Create a new visitor to apply an animation operation (NEXT, REWIND, ...) |
---|
| 542 | * to a MD3 model. The operation is executed in the context of the specified |
---|
| 543 | * animation. |
---|
| 544 | * |
---|
| 545 | * @param anim The animation that provides the context for the operation. |
---|
| 546 | * @param op The operation to apply. |
---|
| 547 | * @param interpolate Should interpolation be done? |
---|
| 548 | */ |
---|
| 549 | void MD3Model::interpolate(MD3Data* data, MD3Animation* anim, int op, bool interpolate) |
---|
| 550 | { |
---|
| 551 | |
---|
| 552 | // // data-> |
---|
| 553 | // this.anim=anim; |
---|
| 554 | // if( op == NEXT || op == PREVIOUS || op == REWIND) |
---|
| 555 | // this.op=op; |
---|
| 556 | // |
---|
| 557 | // this.interpolate=interpolate; |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | |
---|
| 561 | /** |
---|
| 562 | * calc next frame number |
---|
| 563 | */ |
---|
| 564 | int MD3Model::next(int nr) |
---|
| 565 | { |
---|
| 566 | #if 0 |
---|
| 567 | if( nr < (upperBound-1)) |
---|
| 568 | return nr+1; |
---|
| 569 | else |
---|
| 570 | { //rewind needed |
---|
| 571 | if( anim.num < 0) |
---|
| 572 | return anim.first; |
---|
| 573 | else { |
---|
| 574 | nr = (anim.looping != 0)?(anim.num - anim.looping):0; |
---|
| 575 | return anim.first + nr; |
---|
| 576 | } |
---|
| 577 | } |
---|
| 578 | #endif |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | |
---|
| 582 | /** |
---|
| 583 | * calc prev frame number |
---|
| 584 | */ |
---|
| 585 | int MD3Model::prev(int nr) |
---|
| 586 | { |
---|
| 587 | #if 0 |
---|
| 588 | if( nr == anim.first) |
---|
| 589 | return upperBound-1; |
---|
| 590 | else |
---|
| 591 | return nr-1; |
---|
| 592 | #endif |
---|
| 593 | } |
---|
| 594 | |
---|
| 595 | |
---|
| 596 | /** |
---|
| 597 | * apply the specified operation to the animation state data members of the model |
---|
| 598 | * taking the specified animation into account |
---|
| 599 | */ |
---|
| 600 | void MD3Model::doOp(MD3Data* data) |
---|
| 601 | { |
---|
| 602 | #if 0 |
---|
| 603 | //anim to be applied could have illegal data with respect to this model, |
---|
| 604 | //ignore anim in this case |
---|
| 605 | if( anim.first >= model.boneFrameNum || anim.first < 0) |
---|
| 606 | return; |
---|
| 607 | |
---|
| 608 | //calc upper bound for animation frames in this model |
---|
| 609 | if( anim.num < 0) |
---|
| 610 | upperBound=model.boneFrameNum; //use all available frames |
---|
| 611 | else |
---|
| 612 | upperBound=model.boneFrameNum<(anim.first+anim.num)?model.boneFrameNum:(anim.first+anim.num); |
---|
| 613 | |
---|
| 614 | switch (op) { |
---|
| 615 | case NEXT: |
---|
| 616 | if (interpolate) { |
---|
| 617 | model.interpolationFraction+=FRACTION; |
---|
| 618 | if (model.interpolationFraction>=1.0f) { |
---|
| 619 | model.currentFrame=model.nextFrame; |
---|
| 620 | model.nextFrame=next(model.nextFrame); |
---|
| 621 | model.interpolationFraction=0.0f; |
---|
| 622 | } |
---|
| 623 | } |
---|
| 624 | else { |
---|
| 625 | model.currentFrame=model.nextFrame; |
---|
| 626 | model.nextFrame=next(model.nextFrame); |
---|
| 627 | } |
---|
| 628 | break; |
---|
| 629 | case PREVIOUS: |
---|
| 630 | if (interpolate) { |
---|
| 631 | model.interpolationFraction-=FRACTION; |
---|
| 632 | if (model.interpolationFraction<0.0f) { |
---|
| 633 | model.nextFrame=model.currentFrame; |
---|
| 634 | model.currentFrame=prev(model.currentFrame); |
---|
| 635 | model.interpolationFraction=0.8f; |
---|
| 636 | } |
---|
| 637 | } |
---|
| 638 | else { |
---|
| 639 | model.nextFrame=model.currentFrame; |
---|
| 640 | model.currentFrame=prev(model.currentFrame); |
---|
| 641 | } |
---|
| 642 | break; |
---|
| 643 | case REWIND: |
---|
| 644 | model.currentFrame=anim.first; |
---|
| 645 | model.nextFrame=next(model.currentFrame); |
---|
| 646 | model.interpolationFraction=0.0f; |
---|
| 647 | break; |
---|
| 648 | } |
---|
| 649 | #endif |
---|
| 650 | } |
---|
| 651 | |
---|
| 652 | |
---|
| 653 | |
---|
[8351] | 654 | } |
---|