Debian Codegraph @

code/renderergl2/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 int cubemapIndex;
192 qboolean personalModel;
193
194 header = (mdrHeader_t *) tr.currentModel->modelData;
195
196 personalModel = (ent->e.renderfx & RF_THIRD_PERSON) && !(tr.viewParms.isPortal
197 || (tr.viewParms.flags & (VPF_SHADOWMAP | VPF_DEPTHSHADOW)));
198
199 if ( ent->e.renderfx & RF_WRAP_FRAMES )
200 {
201 ent->e.frame %= header->numFrames;
202 ent->e.oldframe %= header->numFrames;
203 }
204
205 //
206 // Validate the frames so there is no chance of a crash.
207 // This will write directly into the entity structure, so
208 // when the surfaces are rendered, they don't need to be
209 // range checked again.
210 //
211 if ((ent->e.frame >= header->numFrames)
212 || (ent->e.frame < 0)
213 || (ent->e.oldframe >= header->numFrames)
214 || (ent->e.oldframe < 0) )
215 {
216 ri.Printf( PRINT_DEVELOPER, "R_MDRAddAnimSurfaces: no such frame %d to %d for '%s'\n",
217 ent->e.oldframe, ent->e.frame, tr.currentModel->name );
218 ent->e.frame = 0;
219 ent->e.oldframe = 0;
220 }
221
222 //
223 // cull the entire model if merged bounding box of both frames
224 // is outside the view frustum.
225 //
226 cull = R_MDRCullModel (header, ent);
227 if ( cull == CULL_OUT ) {
228 return;
229 }
230
231 // figure out the current LOD of the model we're rendering, and set the lod pointer respectively.
232 lodnum = R_ComputeLOD(ent);
233 // check whether this model has as that many LODs at all. If not, try the closest thing we got.
234 if(header->numLODs <= 0)
235 return;
236 if(header->numLODs <= lodnum)
237 lodnum = header->numLODs - 1;
238
239 lod = (mdrLOD_t *)( (byte *)header + header->ofsLODs);
240 for(i = 0; i < lodnum; i++)
241 {
242 lod = (mdrLOD_t *) ((byte *) lod + lod->ofsEnd);
243 }
244
245 // set up lighting
246 if ( !personalModel || r_shadows->integer > 1 )
247 {
248 R_SetupEntityLighting( &tr.refdef, ent );
249 }
250
251 // fogNum?
252 fogNum = R_MDRComputeFogNum( header, ent );
253
254 cubemapIndex = R_CubemapForPoint(ent->e.origin);
255
256 surface = (mdrSurface_t *)( (byte *)lod + lod->ofsSurfaces );
257
258 for ( i = 0 ; i < lod->numSurfaces ; i++ )
259 {
260
261 if(ent->e.customShader)
262 shader = R_GetShaderByHandle(ent->e.customShader);
263 else if(ent->e.customSkin > 0 && ent->e.customSkin < tr.numSkins)
264 {
265 skin = R_GetSkinByHandle(ent->e.customSkin);
266 shader = tr.defaultShader;
267
268 for(j = 0; j < skin->numSurfaces; j++)
269 {
270 if (!strcmp(skin->surfaces[j].name, surface->name))
271 {
272 shader = skin->surfaces[j].shader;
273 break;
274 }
275 }
276 }
277 else if(surface->shaderIndex > 0)
278 shader = R_GetShaderByHandle( surface->shaderIndex );
279 else
280 shader = tr.defaultShader;
281
282 // we will add shadows even if the main object isn't visible in the view
283
284 // stencil shadows can't do personal models unless I polyhedron clip
285 if ( !personalModel
286 && r_shadows->integer == 2
287 && fogNum == 0
288 && !(ent->e.renderfx & ( RF_NOSHADOW | RF_DEPTHHACK ) )
289 && shader->sort == SS_OPAQUE )
290 {
291 R_AddDrawSurf( (void *)surface, tr.shadowShader, 0, qfalse, qfalse, 0 );
292 }
293
294 // projection shadows work fine with personal models
295 if ( r_shadows->integer == 3
296 && fogNum == 0
297 && (ent->e.renderfx & RF_SHADOW_PLANE )
298 && shader->sort == SS_OPAQUE )
299 {
300 R_AddDrawSurf( (void *)surface, tr.projectionShadowShader, 0, qfalse, qfalse, 0 );
301 }
302
303 if (!personalModel)
304 R_AddDrawSurf( (void *)surface, shader, fogNum, qfalse, qfalse, cubemapIndex );
305
306 surface = (mdrSurface_t *)( (byte *)surface + surface->ofsEnd );
307 }
308 }
309
310 /*
311 ==============
312 RB_MDRSurfaceAnim
313 ==============
314 */
315 void RB_MDRSurfaceAnim( mdrSurface_t *surface )
316 {
317 int i, j, k;
318 float frontlerp, backlerp;
319 int *triangles;
320 int indexes;
321 int baseIndex, baseVertex;
322 int numVerts;
323 mdrVertex_t *v;
324 mdrHeader_t *header;
325 mdrFrame_t *frame;
326 mdrFrame_t *oldFrame;
327 mdrBone_t bones[MDR_MAX_BONES], *bonePtr, *bone;
328
329 int frameSize;
330
331 // don't lerp if lerping off, or this is the only frame, or the last frame...
332 //
333 if (backEnd.currentEntity->e.oldframe == backEnd.currentEntity->e.frame)
334 {
335 backlerp = 0; // if backlerp is 0, lerping is off and frontlerp is never used
336 frontlerp = 1;
337 }
338 else
339 {
340 backlerp = backEnd.currentEntity->e.backlerp;
341 frontlerp = 1.0f - backlerp;
342 }
343
344 header = (mdrHeader_t *)((byte *)surface + surface->ofsHeader);
345
346 frameSize = (size_t)( &((mdrFrame_t *)0)->bones[ header->numBones ] );
347
348 frame = (mdrFrame_t *)((byte *)header + header->ofsFrames +
349 backEnd.currentEntity->e.frame * frameSize );
350 oldFrame = (mdrFrame_t *)((byte *)header + header->ofsFrames +
351 backEnd.currentEntity->e.oldframe * frameSize );
352
353 RB_CHECKOVERFLOW( surface->numVerts, surface->numTriangles * 3 );
354
355 triangles = (int *) ((byte *)surface + surface->ofsTriangles);
356 indexes = surface->numTriangles * 3;
357 baseIndex = tess.numIndexes;
358 baseVertex = tess.numVertexes;
359
360 // Set up all triangles.
361 for (j = 0 ; j < indexes ; j++)
362 {
363 tess.indexes[baseIndex + j] = baseVertex + triangles[j];
364 }
365 tess.numIndexes += indexes;
366
367 //
368 // lerp all the needed bones
369 //
370 if ( !backlerp )
371 {
372 // no lerping needed
373 bonePtr = frame->bones;
374 }
375 else
376 {
377 bonePtr = bones;
378
379 for ( i = 0 ; i < header->numBones*12 ; i++ )
380 {
381 ((float *)bonePtr)[i] = frontlerp * ((float *)frame->bones)[i] + backlerp * ((float *)oldFrame->bones)[i];
382 }
383 }
384
385 //
386 // deform the vertexes by the lerped bones
387 //
388 numVerts = surface->numVerts;
389 v = (mdrVertex_t *) ((byte *)surface + surface->ofsVerts);
390 for ( j = 0; j < numVerts; j++ )
391 {
392 vec3_t tempVert, tempNormal;
393 mdrWeight_t *w;
394
395 VectorClear( tempVert );
396 VectorClear( tempNormal );
397 w = v->weights;
398 for ( k = 0 ; k < v->numWeights ; k++, w++ )
399 {
400 bone = bonePtr + w->boneIndex;
401
402 tempVert[0] += w->boneWeight * ( DotProduct( bone->matrix[0], w->offset ) + bone->matrix[0][3] );
403 tempVert[1] += w->boneWeight * ( DotProduct( bone->matrix[1], w->offset ) + bone->matrix[1][3] );
404 tempVert[2] += w->boneWeight * ( DotProduct( bone->matrix[2], w->offset ) + bone->matrix[2][3] );
405
406 tempNormal[0] += w->boneWeight * DotProduct( bone->matrix[0], v->normal );
407 tempNormal[1] += w->boneWeight * DotProduct( bone->matrix[1], v->normal );
408 tempNormal[2] += w->boneWeight * DotProduct( bone->matrix[2], v->normal );
409 }
410
411 tess.xyz[baseVertex + j][0] = tempVert[0];
412 tess.xyz[baseVertex + j][1] = tempVert[1];
413 tess.xyz[baseVertex + j][2] = tempVert[2];
414
415 R_VaoPackNormal(tess.normal[baseVertex + j], tempNormal);
416
417 tess.texCoords[baseVertex + j][0] = v->texCoords[0];
418 tess.texCoords[baseVertex + j][1] = v->texCoords[1];
419
420 v = (mdrVertex_t *)&v->weights[v->numWeights];
421 }
422
423 tess.numVertexes += surface->numVerts;
424 }
425
426
427 #define MC_MASK_X ((1<<(MC_BITS_X))-1)
428 #define MC_MASK_Y ((1<<(MC_BITS_Y))-1)
429 #define MC_MASK_Z ((1<<(MC_BITS_Z))-1)
430 #define MC_MASK_VECT ((1<<(MC_BITS_VECT))-1)
431
432 #define MC_SCALE_VECT (1.0f/(float)((1<<(MC_BITS_VECT-1))-2))
433
434 #define MC_POS_X (0)
435 #define MC_SHIFT_X (0)
436
437 #define MC_POS_Y ((((MC_BITS_X))/8))
438 #define MC_SHIFT_Y ((((MC_BITS_X)%8)))
439
440 #define MC_POS_Z ((((MC_BITS_X+MC_BITS_Y))/8))
441 #define MC_SHIFT_Z ((((MC_BITS_X+MC_BITS_Y)%8)))
442
443 #define MC_POS_V11 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z))/8))
444 #define MC_SHIFT_V11 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z)%8)))
445
446 #define MC_POS_V12 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT))/8))
447 #define MC_SHIFT_V12 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT)%8)))
448
449 #define MC_POS_V13 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*2))/8))
450 #define MC_SHIFT_V13 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*2)%8)))
451
452 #define MC_POS_V21 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*3))/8))
453 #define MC_SHIFT_V21 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*3)%8)))
454
455 #define MC_POS_V22 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*4))/8))
456 #define MC_SHIFT_V22 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*4)%8)))
457
458 #define MC_POS_V23 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*5))/8))
459 #define MC_SHIFT_V23 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*5)%8)))
460
461 #define MC_POS_V31 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*6))/8))
462 #define MC_SHIFT_V31 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*6)%8)))
463
464 #define MC_POS_V32 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*7))/8))
465 #define MC_SHIFT_V32 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*7)%8)))
466
467 #define MC_POS_V33 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*8))/8))
468 #define MC_SHIFT_V33 ((((MC_BITS_X+MC_BITS_Y+MC_BITS_Z+MC_BITS_VECT*8)%8)))
469
470 void MC_UnCompress(float mat[3][4],const unsigned char * comp)
471 {
472 int val;
473
474 val=(int)((unsigned short *)(comp))[0];
475 val-=1<<(MC_BITS_X-1);
476 mat[0][3]=((float)(val))*MC_SCALE_X;
477
478 val=(int)((unsigned short *)(comp))[1];
479 val-=1<<(MC_BITS_Y-1);
480 mat[1][3]=((float)(val))*MC_SCALE_Y;
481
482 val=(int)((unsigned short *)(comp))[2];
483 val-=1<<(MC_BITS_Z-1);
484 mat[2][3]=((float)(val))*MC_SCALE_Z;
485
486 val=(int)((unsigned short *)(comp))[3];
487 val-=1<<(MC_BITS_VECT-1);
488 mat[0][0]=((float)(val))*MC_SCALE_VECT;
489
490 val=(int)((unsigned short *)(comp))[4];
491 val-=1<<(MC_BITS_VECT-1);
492 mat[0][1]=((float)(val))*MC_SCALE_VECT;
493
494 val=(int)((unsigned short *)(comp))[5];
495 val-=1<<(MC_BITS_VECT-1);
496 mat[0][2]=((float)(val))*MC_SCALE_VECT;
497
498
499 val=(int)((unsigned short *)(comp))[6];
500 val-=1<<(MC_BITS_VECT-1);
501 mat[1][0]=((float)(val))*MC_SCALE_VECT;
502
503 val=(int)((unsigned short *)(comp))[7];
504 val-=1<<(MC_BITS_VECT-1);
505 mat[1][1]=((float)(val))*MC_SCALE_VECT;
506
507 val=(int)((unsigned short *)(comp))[8];
508 val-=1<<(MC_BITS_VECT-1);
509 mat[1][2]=((float)(val))*MC_SCALE_VECT;
510
511
512 val=(int)((unsigned short *)(comp))[9];
513 val-=1<<(MC_BITS_VECT-1);
514 mat[2][0]=((float)(val))*MC_SCALE_VECT;
515
516 val=(int)((unsigned short *)(comp))[10];
517 val-=1<<(MC_BITS_VECT-1);
518 mat[2][1]=((float)(val))*MC_SCALE_VECT;
519
520 val=(int)((unsigned short *)(comp))[11];
521 val-=1<<(MC_BITS_VECT-1);
522 mat[2][2]=((float)(val))*MC_SCALE_VECT;
523 }