Debian Codegraph @

code/renderergl1/tr_animation.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 #include "tr_local.h"
24
25 /*
26
27 All bones should be an identity orientation to display the mesh exactly
28 as it is specified.
29
30 For all other frames, the bones represent the transformation from the
31 orientation of the bone in the base frame to the orientation in this
32 frame.
33
34 */
35
36
37 // copied and adapted from tr_mesh.c
38
39 /*
40 =============
41 R_MDRCullModel
42 =============
43 */
44
45 static int R_MDRCullModel( mdrHeader_t *header, trRefEntity_t *ent ) {
46 vec3_t bounds[2];
47 mdrFrame_t *oldFrame, *newFrame;
48 int i, frameSize;
49
50 frameSize = (size_t)( &((mdrFrame_t *)0)->bones[ header->numBones ] );
51
52 // compute frame pointers
53 newFrame = ( mdrFrame_t * ) ( ( byte * ) header + header->ofsFrames + frameSize * ent->e.frame);
54 oldFrame = ( mdrFrame_t * ) ( ( byte * ) header + header->ofsFrames + frameSize * ent->e.oldframe);
55
56 // cull bounding sphere ONLY if this is not an upscaled entity
57 if ( !ent->e.nonNormalizedAxes )
58 {
59 if ( ent->e.frame == ent->e.oldframe )
60 {
61 switch ( R_CullLocalPointAndRadius( newFrame->localOrigin, newFrame->radius ) )
62 {
63 // Ummm... yeah yeah I know we don't really have an md3 here.. but we pretend
64 // we do. After all, the purpose of mdrs are not that different, are they?
65
66 case CULL_OUT:
67 tr.pc.c_sphere_cull_md3_out++;
68 return CULL_OUT;
69
70 case CULL_IN:
71 tr.pc.c_sphere_cull_md3_in++;
72 return CULL_IN;
73
74 case CULL_CLIP:
75 tr.pc.c_sphere_cull_md3_clip++;
76 break;
77 }
78 }
79 else
80 {
81 int sphereCull, sphereCullB;
82
83 sphereCull = R_CullLocalPointAndRadius( newFrame->localOrigin, newFrame->radius );
84 if ( newFrame == oldFrame ) {
85 sphereCullB = sphereCull;
86 } else {
87 sphereCullB = R_CullLocalPointAndRadius( oldFrame->localOrigin, oldFrame->radius );
88 }
89
90 if ( sphereCull == sphereCullB )
91 {
92 if ( sphereCull == CULL_OUT )
93 {
94 tr.pc.c_sphere_cull_md3_out++;
95 return CULL_OUT;
96 }
97 else if ( sphereCull == CULL_IN )
98 {
99 tr.pc.c_sphere_cull_md3_in++;
100 return CULL_IN;
101 }
102 else
103 {
104 tr.pc.c_sphere_cull_md3_clip++;
105 }
106 }
107 }
108 }
109
110 // calculate a bounding box in the current coordinate system
111 for (i = 0 ; i < 3 ; i++) {
112 bounds[0][i] = oldFrame->bounds[0][i] < newFrame->bounds[0][i] ? oldFrame->bounds[0][i] : newFrame->bounds[0][i];
113 bounds[1][i] = oldFrame->bounds[1][i] > newFrame->bounds[1][i] ? oldFrame->bounds[1][i] : newFrame->bounds[1][i];
114 }
115
116 switch ( R_CullLocalBox( bounds ) )
117 {
118 case CULL_IN:
119 tr.pc.c_box_cull_md3_in++;
120 return CULL_IN;
121 case CULL_CLIP:
122 tr.pc.c_box_cull_md3_clip++;
123 return CULL_CLIP;
124 case CULL_OUT:
125 default:
126 tr.pc.c_box_cull_md3_out++;
127 return CULL_OUT;
128 }
129 }
130
131 /*
132 =================
133 R_MDRComputeFogNum
134
135 =================
136 */
137
138 int R_MDRComputeFogNum( mdrHeader_t *header, trRefEntity_t *ent ) {
139 int i, j;
140 fog_t *fog;
141 mdrFrame_t *mdrFrame;
142 vec3_t localOrigin;
143 int frameSize;
144
145 if ( tr.refdef.rdflags & RDF_NOWORLDMODEL ) {
146 return 0;
147 }
148
149 frameSize = (size_t)( &((mdrFrame_t *)0)->bones[ header->numBones ] );
150
151 // FIXME: non-normalized axis issues
152 mdrFrame = ( mdrFrame_t * ) ( ( byte * ) header + header->ofsFrames + frameSize * ent->e.frame);
153 VectorAdd( ent->e.origin, mdrFrame->localOrigin, localOrigin );
154 for ( i = 1 ; i < tr.world->numfogs ; i++ ) {
155 fog = &tr.world->fogs[i];
156 for ( j = 0 ; j < 3 ; j++ ) {
157 if ( localOrigin[j] - mdrFrame->radius >= fog->bounds[1][j] ) {
158 break;
159 }
160 if ( localOrigin[j] + mdrFrame->radius <= fog->bounds[0][j] ) {
161 break;
162 }
163 }
164 if ( j == 3 ) {
165 return i;
166 }
167 }
168
169 return 0;
170 }
171
172
173 /*
174 ==============
175 R_MDRAddAnimSurfaces
176 ==============
177 */
178
179 // much stuff in there is just copied from R_AddMd3Surfaces in tr_mesh.c
180
181 void R_MDRAddAnimSurfaces( trRefEntity_t *ent ) {
182 mdrHeader_t *header;
183 mdrSurface_t *surface;
184 mdrLOD_t *lod;
185 shader_t *shader;
186 skin_t *skin;
187 int i, j;
188 int lodnum = 0;
189 int fogNum = 0;
190 int cull;
191 qboolean personalModel;
192
193 header = (mdrHeader_t *) tr.currentModel->modelData;
194
195 personalModel = (ent->e.renderfx & RF_THIRD_PERSON) && !tr.viewParms.isPortal;
196
197 if ( ent->e.renderfx & RF_WRAP_FRAMES )
198 {
199 ent->e.frame %= header->numFrames;
200 ent->e.oldframe %= header->numFrames;
201 }
202
203 //
204 // Validate the frames so there is no chance of a crash.
205 // This will write directly into the entity structure, so
206 // when the surfaces are rendered, they don't need to be
207 // range checked again.
208 //
209 if ((ent->e.frame >= header->numFrames)
210 || (ent->e.frame < 0)
211 || (ent->e.oldframe >= header->numFrames)
212 || (ent->e.oldframe < 0) )
213 {
214 ri.Printf( PRINT_DEVELOPER, "R_MDRAddAnimSurfaces: no such frame %d to %d for '%s'\n",
215 ent->e.oldframe, ent->e.frame, tr.currentModel->name );
216 ent->e.frame = 0;
217 ent->e.oldframe = 0;
218 }
219
220 //
221 // cull the entire model if merged bounding box of both frames
222 // is outside the view frustum.
223 //
224 cull = R_MDRCullModel (header, ent);
225 if ( cull == CULL_OUT ) {
226 return;
227 }
228
229 // figure out the current LOD of the model we're rendering, and set the lod pointer respectively.
230 lodnum = R_ComputeLOD(ent);
231 // check whether this model has as that many LODs at all. If not, try the closest thing we got.
232 if(header->numLODs <= 0)
233 return;
234 if(header->numLODs <= lodnum)
235 lodnum = header->numLODs - 1;
236
237 lod = (mdrLOD_t *)( (byte *)header + header->ofsLODs);
238 for(i = 0; i < lodnum; i++)
239 {
240 lod = (mdrLOD_t *) ((byte *) lod + lod->ofsEnd);
241 }
242
243 // set up lighting
244 if ( !personalModel || r_shadows->integer > 1 )
245 {
246 R_SetupEntityLighting( &tr.refdef, ent );
247 }
248
249 // fogNum?
250 fogNum = R_MDRComputeFogNum( header, ent );
251
252 surface = (mdrSurface_t *)( (byte *)lod + lod->ofsSurfaces );
253
254 for ( i = 0 ; i < lod->numSurfaces ; i++ )
255 {
256
257 if(ent->e.customShader)
258 shader = R_GetShaderByHandle(ent->e.customShader);
259 else if(ent->e.customSkin > 0 && ent->e.customSkin < tr.numSkins)
260 {
261 skin = R_GetSkinByHandle(ent->e.customSkin);
262 shader = tr.defaultShader;
263
264 for(j = 0; j < skin->numSurfaces; j++)
265 {
266 if (!strcmp(skin->surfaces[j].name, surface->name))
267 {
268 shader = skin->surfaces[j].shader;
269 break;
270 }
271 }
272 }
273 else if(surface->shaderIndex > 0)
274 shader = R_GetShaderByHandle( surface->shaderIndex );
275 else
276 shader = tr.defaultShader;
277
278 // we will add shadows even if the main object isn't visible in the view
279
280 // stencil shadows can't do personal models unless I polyhedron clip
281 if ( !personalModel
282 && r_shadows->integer == 2
283 && fogNum == 0
284 && !(ent->e.renderfx & ( RF_NOSHADOW | RF_DEPTHHACK ) )
285 && shader->sort == SS_OPAQUE )
286 {
287 R_AddDrawSurf( (void *)surface, tr.shadowShader, 0, qfalse );
288 }
289
290 // projection shadows work fine with personal models
291 if ( r_shadows->integer == 3
292 && fogNum == 0
293 && (ent->e.renderfx & RF_SHADOW_PLANE )
294 && shader->sort == SS_OPAQUE )
295 {
296 R_AddDrawSurf( (void *)surface, tr.projectionShadowShader, 0, qfalse );
297 }
298
299 if (!personalModel)
300 R_AddDrawSurf( (void *)surface, shader, fogNum, qfalse );
301
302 surface = (mdrSurface_t *)( (byte *)surface + surface->ofsEnd );
303 }
304 }
305
306 /*
307 ==============
308 RB_MDRSurfaceAnim
309 ==============
310 */
311 void RB_MDRSurfaceAnim( mdrSurface_t *surface )
312 {
313 int i, j, k;
314 float frontlerp, backlerp;
315 int *triangles;
316 int indexes;
317 int baseIndex, baseVertex;
318 int numVerts;
319 mdrVertex_t *v;
320 mdrHeader_t *header;
321 mdrFrame_t *frame;
322 mdrFrame_t *oldFrame;
323 mdrBone_t bones[MDR_MAX_BONES], *bonePtr, *bone;
324
325 int frameSize;
326
327 // don't lerp if lerping off, or this is the only frame, or the last frame...
328 //
329 if (backEnd.currentEntity->e.oldframe == backEnd.currentEntity->e.frame)
330 {
331 backlerp = 0; // if backlerp is 0, lerping is off and frontlerp is never used
332 frontlerp = 1;
333 }
334 else
335 {
336 backlerp = backEnd.currentEntity->e.backlerp;
337 frontlerp = 1.0f - backlerp;
338 }
339
340 header = (mdrHeader_t *)((byte *)surface + surface->ofsHeader);
341
342 frameSize = (size_t)( &((mdrFrame_t *)0)->bones[ header->numBones ] );
343
344 frame = (mdrFrame_t *)((byte *)header + header->ofsFrames +
345 backEnd.currentEntity->e.frame * frameSize );
346 oldFrame = (mdrFrame_t *)((byte *)header + header->ofsFrames +
347 backEnd.currentEntity->e.oldframe * frameSize );
348
349 RB_CHECKOVERFLOW( surface->numVerts, surface->numTriangles * 3 );
350
351 triangles = (int *) ((byte *)surface + surface->ofsTriangles);
352 indexes = surface->numTriangles * 3;
353 baseIndex = tess.numIndexes;
354 baseVertex = tess.numVertexes;
355
356 // Set up all triangles.
357 for (j = 0 ; j < indexes ; j++)
358 {
359 tess.indexes[baseIndex + j] = baseVertex + triangles[j];
360 }
361 tess.numIndexes += indexes;
362
363 //
364 // lerp all the needed bones
365 //
366 if ( !backlerp )
367 {
368 // no lerping needed
369 bonePtr = frame->bones;
370 }
371 else
372 {
373 bonePtr = bones;
374
375 for ( i = 0 ; i < header->numBones*12 ; i++ )
376 {
377 ((float *)bonePtr)[i] = frontlerp * ((float *)frame->bones)[i] + backlerp * ((float *)oldFrame->bones)[i];
378 }
379 }
380
381 //
382 // deform the vertexes by the lerped bones
383 //
384 numVerts = surface->numVerts;
385 v = (mdrVertex_t *) ((byte *)surface + surface->ofsVerts);
386 for ( j = 0; j < numVerts; j++ )
387 {
388 vec3_t tempVert, tempNormal;
389 mdrWeight_t *w;
390
391 VectorClear( tempVert );
392 VectorClear( tempNormal );
393 w = v->weights;
394 for ( k = 0 ; k < v->numWeights ; k++, w++ )
395 {
396 bone = bonePtr + w->boneIndex;
397
398 tempVert[0] += w->boneWeight * ( DotProduct( bone->matrix[0], w->offset ) + bone->matrix[0][3] );
399 tempVert[1] += w->boneWeight * ( DotProduct( bone->matrix[1], w->offset ) + bone->matrix[1][3] );
400 tempVert[2] += w->boneWeight * ( DotProduct( bone->matrix[2], w->offset ) + bone->matrix[2][3] );
401
402 tempNormal[0] += w->boneWeight * DotProduct( bone->matrix[0], v->normal );
403 tempNormal[1] += w->boneWeight * DotProduct( bone->matrix[1], v->normal );
404 tempNormal[2] += w->boneWeight * DotProduct( bone->matrix[2], v->normal );
405 }
406
407 tess.xyz[baseVertex + j][0] = tempVert[0];
408 tess.xyz[baseVertex + j][1] = tempVert[1];
409 tess.xyz[baseVertex + j][2] = tempVert[2];
410
411 tess.normal[baseVertex + j][0] = tempNormal[0];
412 tess.normal[baseVertex + j][1] = tempNormal[1];
413 tess.normal[baseVertex + j][2] = tempNormal[2];
414
415 tess.texCoords[baseVertex + j][0][0] = v->texCoords[0];
416 tess.texCoords[baseVertex + j][0][1] = v->texCoords[1];
417
418 v = (mdrVertex_t *)&v->weights[v->numWeights];
419 }
420
421 tess.numVertexes += surface->numVerts;
422 }
423
424
425 #define MC_MASK_X ((1<<(MC_BITS_X))-1)
426 #define MC_MASK_Y ((1<<(MC_BITS_Y))-1)
427 #define MC_MASK_Z ((1<<(MC_BITS_Z))-1)
428 #define MC_MASK_VECT ((1<<(MC_BITS_VECT))-1)
429
430 #define MC_SCALE_VECT (1.0f/(float)((1<<(MC_BITS_VECT-1))-2))
431
432 #define MC_POS_X (0)
433 #define MC_SHIFT_X (0)
434
435 #define MC_POS_Y ((((MC_BITS_X))/8))
436 #define MC_SHIFT_Y ((((MC_BITS_X)%8)))
437
438 #define MC_POS_Z ((((MC_BITS_X+MC_BITS_Y))/8))
439 #define MC_SHIFT_Z ((((MC_BITS_X+MC_BITS_Y)%8)))
440
441 #define MC_POS_V11 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z))/8))
442 #define MC_SHIFT_V11 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z)%8)))
443
444 #define MC_POS_V12 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT))/8))
445 #define MC_SHIFT_V12 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT)%8)))
446
447 #define MC_POS_V13 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*2))/8))
448 #define MC_SHIFT_V13 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*2)%8)))
449
450 #define MC_POS_V21 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*3))/8))
451 #define MC_SHIFT_V21 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*3)%8)))
452
453 #define MC_POS_V22 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*4))/8))
454 #define MC_SHIFT_V22 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*4)%8)))
455
456 #define MC_POS_V23 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*5))/8))
457 #define MC_SHIFT_V23 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*5)%8)))
458
459 #define MC_POS_V31 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*6))/8))
460 #define MC_SHIFT_V31 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*6)%8)))
461
462 #define MC_POS_V32 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*7))/8))
463 #define MC_SHIFT_V32 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*7)%8)))
464
465 #define MC_POS_V33 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*8))/8))
466 #define MC_SHIFT_V33 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*8)%8)))
467
468 void MC_UnCompress(float mat[3][4],const unsigned char * comp)
469 {
470 int val;
471
472 val=(int)((unsigned short *)(comp))[0];
473 val-=1<<(MC_BITS_X-1);
474 mat[0][3]=((float)(val))*MC_SCALE_X;
475
476 val=(int)((unsigned short *)(comp))[1];
477 val-=1<<(MC_BITS_Y-1);
478 mat[1][3]=((float)(val))*MC_SCALE_Y;
479
480 val=(int)((unsigned short *)(comp))[2];
481 val-=1<<(MC_BITS_Z-1);
482 mat[2][3]=((float)(val))*MC_SCALE_Z;
483
484 val=(int)((unsigned short *)(comp))[3];
485 val-=1<<(MC_BITS_VECT-1);
486 mat[0][0]=((float)(val))*MC_SCALE_VECT;
487
488 val=(int)((unsigned short *)(comp))[4];
489 val-=1<<(MC_BITS_VECT-1);
490 mat[0][1]=((float)(val))*MC_SCALE_VECT;
491
492 val=(int)((unsigned short *)(comp))[5];
493 val-=1<<(MC_BITS_VECT-1);
494 mat[0][2]=((float)(val))*MC_SCALE_VECT;
495
496
497 val=(int)((unsigned short *)(comp))[6];
498 val-=1<<(MC_BITS_VECT-1);
499 mat[1][0]=((float)(val))*MC_SCALE_VECT;
500
501 val=(int)((unsigned short *)(comp))[7];
502 val-=1<<(MC_BITS_VECT-1);
503 mat[1][1]=((float)(val))*MC_SCALE_VECT;
504
505 val=(int)((unsigned short *)(comp))[8];
506 val-=1<<(MC_BITS_VECT-1);
507 mat[1][2]=((float)(val))*MC_SCALE_VECT;
508
509
510 val=(int)((unsigned short *)(comp))[9];
511 val-=1<<(MC_BITS_VECT-1);
512 mat[2][0]=((float)(val))*MC_SCALE_VECT;
513
514 val=(int)((unsigned short *)(comp))[10];
515 val-=1<<(MC_BITS_VECT-1);
516 mat[2][1]=((float)(val))*MC_SCALE_VECT;
517
518 val=(int)((unsigned short *)(comp))[11];
519 val-=1<<(MC_BITS_VECT-1);
520 mat[2][2]=((float)(val))*MC_SCALE_VECT;
521 }