[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" |
---|
| 27 | |
---|
[8351] | 28 | namespace md3 |
---|
| 29 | { |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * md3 model |
---|
| 33 | */ |
---|
[8353] | 34 | MD3Model::MD3Model(std::string filename, float scaling) |
---|
| 35 | { |
---|
[8354] | 36 | this->md3Data = new MD3Data(filename, filename, scaling); |
---|
[8533] | 37 | |
---|
| 38 | MD3AnimationCfg cfg("/home/boenzlip/tmp/q3/Downloads/MOH/q3mdl-alien3/models/players/alien3/animation.cfg"); |
---|
[8556] | 39 | |
---|
| 40 | this->tmpBoneFrame = new MD3BoneFrame(); |
---|
[8557] | 41 | this->tmpMesh = new sVec3D[2048]; //!< the temporary mesh, hopefully newer bigger than 2048 vertices |
---|
[8353] | 42 | } |
---|
[8351] | 43 | |
---|
| 44 | |
---|
[8353] | 45 | |
---|
[8351] | 46 | MD3Model::~MD3Model() |
---|
[8556] | 47 | { |
---|
| 48 | delete this->tmpBoneFrame; |
---|
[8557] | 49 | delete [] this->tmpMesh; |
---|
[8556] | 50 | } |
---|
[8351] | 51 | |
---|
| 52 | |
---|
[8549] | 53 | |
---|
| 54 | /** |
---|
| 55 | * this draws the md3 model |
---|
| 56 | */ |
---|
| 57 | void MD3Model::draw() |
---|
| 58 | { |
---|
| 59 | //draw current bone frame |
---|
[8550] | 60 | this->draw(this->md3Data); |
---|
| 61 | } |
---|
[8549] | 62 | |
---|
| 63 | |
---|
[8550] | 64 | /** |
---|
| 65 | * draw the md3model |
---|
| 66 | * @param data: the data to be drawn |
---|
| 67 | */ |
---|
| 68 | void MD3Model::draw(MD3Data* data) |
---|
[8551] | 69 | { |
---|
[8556] | 70 | |
---|
| 71 | // draw the bones if needed |
---|
[8551] | 72 | if( this->bDrawBones) |
---|
| 73 | { |
---|
| 74 | // get bone frame, interpolate if necessary |
---|
| 75 | if( data->animationState.interpolationFraction != 0.0 && |
---|
| 76 | data->animationState.currentFrame != data->animationState.nextFrame) { |
---|
| 77 | //interpolate bone frame |
---|
| 78 | this->drawBoneFrame(this->interpolateBoneFrame(data->boneFrames[data->animationState.currentFrame], |
---|
| 79 | data->boneFrames[data->animationState.nextFrame], |
---|
| 80 | data->animationState.interpolationFraction)); |
---|
| 81 | } |
---|
| 82 | else { |
---|
| 83 | //stick with current bone frame |
---|
| 84 | this->drawBoneFrame(data->boneFrames[data->animationState.currentFrame]); |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
[8556] | 88 | |
---|
[8552] | 89 | //draw all meshes of current frame of this model |
---|
[8556] | 90 | for( int i = 0; i < data->meshNum; i++) |
---|
| 91 | { |
---|
[8552] | 92 | MD3Mesh* mesh = data->meshes[i]; |
---|
[8551] | 93 | |
---|
[8552] | 94 | // gl.glBlendFunc(mesh.GLSrcBlendFunc, mesh.GLDstBlendFunc); |
---|
| 95 | // gl.glDepthMask(mesh.GLDepthMask); |
---|
[8551] | 96 | |
---|
[8552] | 97 | if( mesh->header->textureNum > 0 && &mesh->material[0] != NULL) |
---|
| 98 | mesh->material[0].select(); |
---|
| 99 | |
---|
| 100 | // get mesh frame, do interpolation if necessary |
---|
| 101 | sVec3D* frame; |
---|
| 102 | if( data->animationState.interpolationFraction != 0.0 && |
---|
| 103 | data->animationState.currentFrame != data->animationState.nextFrame) { |
---|
| 104 | //interpolate mesh frame between the 2 current mesh frames |
---|
[8557] | 105 | // frame = this->interpolateMeshFrame(mesh, data->animationState.currentFrame, data->animationState.nextFrame, |
---|
| 106 | // data->animationState.interpolationFraction); |
---|
| 107 | frame = this->interpolateMeshFrame( data->meshes[data->animationState.currentFrame]->meshFrames, |
---|
| 108 | data->meshes[data->animationState.nextFrame]->meshFrames, |
---|
| 109 | data->animationState.interpolationFraction, mesh); |
---|
[8552] | 110 | } |
---|
| 111 | else { |
---|
| 112 | //no interpolation needed, just draw current frame |
---|
| 113 | frame = &mesh->meshFrames[data->animationState.currentFrame]; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | this->drawMesh(mesh, frame); |
---|
| 117 | |
---|
[8556] | 118 | |
---|
| 119 | // draw vertex normals if needed |
---|
[8552] | 120 | if( this->bDrawNormals) { |
---|
| 121 | // get vertex normals, interpolate if necessary |
---|
| 122 | if( data->animationState.interpolationFraction != 0.0 && |
---|
| 123 | data->animationState.currentFrame != data->animationState.nextFrame) { |
---|
| 124 | //interpolate vertex normals |
---|
| 125 | this->drawVertexNormals(frame, this->interpolateVertexNormals(&mesh->normals[data->animationState.currentFrame], |
---|
| 126 | &mesh->normals[data->animationState.nextFrame], data->animationState.interpolationFraction)); |
---|
| 127 | } |
---|
| 128 | else { |
---|
| 129 | //stick with current vertex normals |
---|
| 130 | this->drawVertexNormals(frame, &mesh->normals[data->animationState.currentFrame]); |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | |
---|
[8556] | 136 | // draw all models linked to this model |
---|
[8552] | 137 | std::map<int, MD3Data*>::iterator it = data->sortedMap.begin(); |
---|
[8556] | 138 | while( it != data->sortedMap.end()) |
---|
| 139 | { |
---|
[8552] | 140 | MD3Data* child = it->second; |
---|
| 141 | |
---|
| 142 | //build transformation array m from matrix, interpolate if necessary |
---|
| 143 | float* m = new float[16]; |
---|
| 144 | |
---|
[8553] | 145 | MD3Tag* currFrameTag = data->boneFrames[data->animationState.currentFrame]->tags[child->parentTagIndex]; |
---|
| 146 | |
---|
| 147 | if( data->animationState.interpolationFraction != 0.0 && |
---|
| 148 | data->animationState.currentFrame != data->animationState.nextFrame) { |
---|
| 149 | //we need to interpolate |
---|
| 150 | MD3Tag* nextFrameTag = data->boneFrames[data->animationState.nextFrame]->tags[child->parentTagIndex]; |
---|
| 151 | m = this->interpolateTransformation(currFrameTag, nextFrameTag, data->animationState.interpolationFraction); |
---|
| 152 | } |
---|
| 153 | else { |
---|
| 154 | //no interpolation needed, stay with last transformation |
---|
| 155 | //OpenGL matrix is in column-major order |
---|
| 156 | m[0] = currFrameTag->matrix[0][0]; |
---|
| 157 | m[1] = currFrameTag->matrix[1][0]; |
---|
| 158 | m[2] = currFrameTag->matrix[2][0]; |
---|
| 159 | m[3] = 0.0f; |
---|
| 160 | m[4] = currFrameTag->matrix[0][1]; |
---|
| 161 | m[5] = currFrameTag->matrix[1][1]; |
---|
| 162 | m[6] = currFrameTag->matrix[2][1]; |
---|
| 163 | m[7] = 0.0f; |
---|
| 164 | m[8] = currFrameTag->matrix[0][2]; |
---|
| 165 | m[9] = currFrameTag->matrix[1][2]; |
---|
| 166 | m[10]= currFrameTag->matrix[2][2]; |
---|
| 167 | m[11]= 0.0f; |
---|
| 168 | m[12] = currFrameTag->position.x; |
---|
| 169 | m[13] = currFrameTag->position.y; |
---|
| 170 | m[14] = currFrameTag->position.z; |
---|
| 171 | m[15] = 1.0f; |
---|
| 172 | } |
---|
[8556] | 173 | |
---|
| 174 | //switch to child coord system |
---|
| 175 | glPushMatrix(); |
---|
| 176 | glMultMatrixf(m); |
---|
| 177 | |
---|
| 178 | // and draw child |
---|
| 179 | this->draw(child); |
---|
| 180 | |
---|
| 181 | glPopMatrix(); |
---|
[8552] | 182 | } |
---|
[8551] | 183 | } |
---|
| 184 | |
---|
| 185 | |
---|
[8552] | 186 | /** |
---|
| 187 | * draws the mesh |
---|
| 188 | */ |
---|
| 189 | void MD3Model::drawMesh(MD3Mesh* mesh, sVec3D* frame) |
---|
| 190 | {} |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | /** |
---|
| 194 | * drawo vertex normals |
---|
| 195 | */ |
---|
| 196 | void MD3Model::drawVertexNormals(sVec3D* frame, MD3Normal* normals) |
---|
| 197 | {} |
---|
| 198 | |
---|
| 199 | |
---|
| 200 | /** |
---|
[8557] | 201 | * draw bone frame |
---|
| 202 | */ |
---|
| 203 | void MD3Model::drawBoneFrame(MD3BoneFrame* frame) |
---|
| 204 | {} |
---|
| 205 | |
---|
| 206 | |
---|
| 207 | /** |
---|
[8552] | 208 | * interpolate bone frame |
---|
[8556] | 209 | * @param currBoneFrame Start bone frame. |
---|
| 210 | * @param nextBoneFrame End bone frame. |
---|
| 211 | * @param frac Interpolation fraction, in [0,1]. |
---|
[8552] | 212 | */ |
---|
[8553] | 213 | MD3BoneFrame* MD3Model::interpolateBoneFrame(MD3BoneFrame* currBoneFrame, MD3BoneFrame* nextBoneFrame, float frac) |
---|
[8556] | 214 | { |
---|
| 215 | this->tmpBoneFrame->mins.x = (1.0f - frac) * currBoneFrame->mins.x + frac * nextBoneFrame->mins.x; |
---|
| 216 | this->tmpBoneFrame->maxs.x = (1.0f - frac) * currBoneFrame->maxs.x + frac * nextBoneFrame->maxs.x; |
---|
| 217 | this->tmpBoneFrame->position.x = (1.0f - frac) * currBoneFrame->position.x + frac * nextBoneFrame->position.x; |
---|
| 218 | this->tmpBoneFrame->mins.y = (1.0f - frac) * currBoneFrame->mins.y + frac * nextBoneFrame->mins.y; |
---|
| 219 | this->tmpBoneFrame->maxs.y = (1.0f - frac) * currBoneFrame->maxs.y + frac * nextBoneFrame->maxs.y; |
---|
| 220 | this->tmpBoneFrame->position.y = (1.0f - frac) * currBoneFrame->position.y + frac * nextBoneFrame->position.y; |
---|
| 221 | this->tmpBoneFrame->mins.z = (1.0f - frac) * currBoneFrame->mins.z + frac * nextBoneFrame->mins.z; |
---|
| 222 | this->tmpBoneFrame->maxs.z = (1.0f - frac) * currBoneFrame->maxs.z + frac * nextBoneFrame->maxs.z; |
---|
| 223 | this->tmpBoneFrame->position.z = (1.0f - frac) * currBoneFrame->position.z + frac * nextBoneFrame->position.z; |
---|
[8549] | 224 | |
---|
[8556] | 225 | return this->tmpBoneFrame; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | |
---|
[8552] | 229 | /** |
---|
[8557] | 230 | * interpolate mesh frame |
---|
[8552] | 231 | */ |
---|
[8557] | 232 | sVec3D* MD3Model::interpolateMeshFrame(sVec3D* currMeshFrame, sVec3D* nextMeshFrame, float frac, MD3Mesh* mesh) |
---|
| 233 | { |
---|
| 234 | int vertexNum = mesh->header->vertexNum; |
---|
[8549] | 235 | |
---|
[8557] | 236 | // calc interpolated vertices |
---|
| 237 | for( int t = 0; t < vertexNum * 3.0f; t++) |
---|
| 238 | { |
---|
| 239 | this->tmpMesh[t][0] = (1.0f - frac) * currMeshFrame[t][0] + frac * nextMeshFrame[t][0]; |
---|
| 240 | this->tmpMesh[t][1] = (1.0f - frac) * currMeshFrame[t][1] + frac * nextMeshFrame[t][1]; |
---|
| 241 | this->tmpMesh[t][2] = (1.0f - frac) * currMeshFrame[t][2] + frac * nextMeshFrame[t][2]; |
---|
| 242 | } |
---|
[8549] | 243 | |
---|
[8557] | 244 | return this->tmpMesh; |
---|
| 245 | } |
---|
[8551] | 246 | |
---|
[8552] | 247 | |
---|
| 248 | /** |
---|
| 249 | * interpolate vertex normal |
---|
| 250 | */ |
---|
| 251 | MD3Normal* MD3Model::interpolateVertexNormals(MD3Normal* curNormals, MD3Normal* nextNormals, float frac) |
---|
| 252 | {} |
---|
| 253 | |
---|
| 254 | |
---|
[8553] | 255 | /** |
---|
| 256 | * interpolate transformation |
---|
| 257 | */ |
---|
| 258 | float* MD3Model::interpolateTransformation(MD3Tag* currFrameTag, MD3Tag* nextFrameTag, float frac) |
---|
| 259 | {} |
---|
| 260 | |
---|
| 261 | |
---|
[8351] | 262 | } |
---|