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