Debian Codegraph @

code/renderergl1/tr_model.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 // tr_models.c -- model loading and caching
23
24 #include "tr_local.h"
25
26 #define LL(x) x=LittleLong(x)
27
28 static qboolean R_LoadMD3(model_t *mod, int lod, void *buffer, const char *name );
29 static qboolean R_LoadMDR(model_t *mod, void *buffer, int filesize, const char *name );
30
31 /*
32 ====================
33 R_RegisterMD3
34 ====================
35 */
36 qhandle_t R_RegisterMD3(const char *name, model_t *mod)
37 {
38 union {
39 unsigned *u;
40 void *v;
41 } buf;
42 int lod;
43 int ident;
44 qboolean loaded = qfalse;
45 int numLoaded;
46 char filename[MAX_QPATH], namebuf[MAX_QPATH+20];
47 char *fext, defex[] = "md3";
48
49 numLoaded = 0;
50
51 strcpy(filename, name);
52
53 fext = strchr(filename, '.');
54 if(!fext)
55 fext = defex;
56 else
57 {
58 *fext = '\0';
59 fext++;
60 }
61
62 for (lod = MD3_MAX_LODS - 1 ; lod >= 0 ; lod--)
63 {
64 if(lod)
65 Com_sprintf(namebuf, sizeof(namebuf), "%s_%d.%s", filename, lod, fext);
66 else
67 Com_sprintf(namebuf, sizeof(namebuf), "%s.%s", filename, fext);
68
69 ri.FS_ReadFile( namebuf, &buf.v );
70 if(!buf.u)
71 continue;
72
73 ident = LittleLong(* (unsigned *) buf.u);
74 if (ident == MD3_IDENT)
75 loaded = R_LoadMD3(mod, lod, buf.u, name);
76 else
77 ri.Printf(PRINT_WARNING,"R_RegisterMD3: unknown fileid for %s\n", name);
78
79 ri.FS_FreeFile(buf.v);
80
81 if(loaded)
82 {
83 mod->numLods++;
84 numLoaded++;
85 }
86 else
87 break;
88 }
89
90 if(numLoaded)
91 {
92 // duplicate into higher lod spots that weren't
93 // loaded, in case the user changes r_lodbias on the fly
94 for(lod--; lod >= 0; lod--)
95 {
96 mod->numLods++;
97 mod->md3[lod] = mod->md3[lod + 1];
98 }
99
100 return mod->index;
101 }
102
103 #ifdef _DEBUG
104 ri.Printf(PRINT_WARNING,"R_RegisterMD3: couldn't load %s\n", name);
105 #endif
106
107 mod->type = MOD_BAD;
108 return 0;
109 }
110
111 /*
112 ====================
113 R_RegisterMDR
114 ====================
115 */
116 qhandle_t R_RegisterMDR(const char *name, model_t *mod)
117 {
118 union {
119 unsigned *u;
120 void *v;
121 } buf;
122 int ident;
123 qboolean loaded = qfalse;
124 int filesize;
125
126 filesize = ri.FS_ReadFile(name, (void **) &buf.v);
127 if(!buf.u)
128 {
129 mod->type = MOD_BAD;
130 return 0;
131 }
132
133 ident = LittleLong(*(unsigned *)buf.u);
134 if(ident == MDR_IDENT)
135 loaded = R_LoadMDR(mod, buf.u, filesize, name);
136
137 ri.FS_FreeFile (buf.v);
138
139 if(!loaded)
140 {
141 ri.Printf(PRINT_WARNING,"R_RegisterMDR: couldn't load mdr file %s\n", name);
142 mod->type = MOD_BAD;
143 return 0;
144 }
145
146 return mod->index;
147 }
148
149 /*
150 ====================
151 R_RegisterIQM
152 ====================
153 */
154 qhandle_t R_RegisterIQM(const char *name, model_t *mod)
155 {
156 union {
157 unsigned *u;
158 void *v;
159 } buf;
160 qboolean loaded = qfalse;
161 int filesize;
162
163 filesize = ri.FS_ReadFile(name, (void **) &buf.v);
164 if(!buf.u)
165 {
166 mod->type = MOD_BAD;
167 return 0;
168 }
169
170 loaded = R_LoadIQM(mod, buf.u, filesize, name);
171
172 ri.FS_FreeFile (buf.v);
173
174 if(!loaded)
175 {
176 ri.Printf(PRINT_WARNING,"R_RegisterIQM: couldn't load iqm file %s\n", name);
177 mod->type = MOD_BAD;
178 return 0;
179 }
180
181 return mod->index;
182 }
183
184
185 typedef struct
186 {
187 char *ext;
188 qhandle_t (*ModelLoader)( const char *, model_t * );
189 } modelExtToLoaderMap_t;
190
191 // Note that the ordering indicates the order of preference used
192 // when there are multiple models of different formats available
193 static modelExtToLoaderMap_t modelLoaders[ ] =
194 {
195 { "iqm", R_RegisterIQM },
196 { "mdr", R_RegisterMDR },
197 { "md3", R_RegisterMD3 }
198 };
199
200 static int numModelLoaders = ARRAY_LEN(modelLoaders);
201
202 //===============================================================================
203
204 /*
205 ** R_GetModelByHandle
206 */
207 model_t *R_GetModelByHandle( qhandle_t index ) {
208 model_t *mod;
209
210 // out of range gets the defualt model
211 if ( index < 1 || index >= tr.numModels ) {
212 return tr.models[0];
213 }
214
215 mod = tr.models[index];
216
217 return mod;
218 }
219
220 //===============================================================================
221
222 /*
223 ** R_AllocModel
224 */
225 model_t *R_AllocModel( void ) {
226 model_t *mod;
227
228 if ( tr.numModels == MAX_MOD_KNOWN ) {
229 return NULL;
230 }
231
232 mod = ri.Hunk_Alloc( sizeof( *tr.models[tr.numModels] ), h_low );
233 mod->index = tr.numModels;
234 tr.models[tr.numModels] = mod;
235 tr.numModels++;
236
237 return mod;
238 }
239
240 /*
241 ====================
242 RE_RegisterModel
243
244 Loads in a model for the given name
245
246 Zero will be returned if the model fails to load.
247 An entry will be retained for failed models as an
248 optimization to prevent disk rescanning if they are
249 asked for again.
250 ====================
251 */
252 qhandle_t RE_RegisterModel( const char *name ) {
253 model_t *mod;
254 qhandle_t hModel;
255 qboolean orgNameFailed = qfalse;
256 int orgLoader = -1;
257 int i;
258 char localName[ MAX_QPATH ];
259 const char *ext;
260 char altName[ MAX_QPATH ];
261
262 if ( !name || !name[0] ) {
263 ri.Printf( PRINT_ALL, "RE_RegisterModel: NULL name\n" );
264 return 0;
265 }
266
267 if ( strlen( name ) >= MAX_QPATH ) {
268 ri.Printf( PRINT_ALL, "Model name exceeds MAX_QPATH\n" );
269 return 0;
270 }
271
272 //
273 // search the currently loaded models
274 //
275 for ( hModel = 1 ; hModel < tr.numModels; hModel++ ) {
276 mod = tr.models[hModel];
277 if ( !strcmp( mod->name, name ) ) {
278 if( mod->type == MOD_BAD ) {
279 return 0;
280 }
281 return hModel;
282 }
283 }
284
285 // allocate a new model_t
286
287 if ( ( mod = R_AllocModel() ) == NULL ) {
288 ri.Printf( PRINT_WARNING, "RE_RegisterModel: R_AllocModel() failed for '%s'\n", name);
289 return 0;
290 }
291
292 // only set the name after the model has been successfully loaded
293 Q_strncpyz( mod->name, name, sizeof( mod->name ) );
294
295
296 R_IssuePendingRenderCommands();
297
298 mod->type = MOD_BAD;
299 mod->numLods = 0;
300
301 //
302 // load the files
303 //
304 Q_strncpyz( localName, name, MAX_QPATH );
305
306 ext = COM_GetExtension( localName );
307
308 if( *ext )
309 {
310 // Look for the correct loader and use it
311 for( i = 0; i < numModelLoaders; i++ )
312 {
313 if( !Q_stricmp( ext, modelLoaders[ i ].ext ) )
314 {
315 // Load
316 hModel = modelLoaders[ i ].ModelLoader( localName, mod );
317 break;
318 }
319 }
320
321 // A loader was found
322 if( i < numModelLoaders )
323 {
324 if( !hModel )
325 {
326 // Loader failed, most likely because the file isn't there;
327 // try again without the extension
328 orgNameFailed = qtrue;
329 orgLoader = i;
330 COM_StripExtension( name, localName, MAX_QPATH );
331 }
332 else
333 {
334 // Something loaded
335 return mod->index;
336 }
337 }
338 }
339
340 // Try and find a suitable match using all
341 // the model formats supported
342 for( i = 0; i < numModelLoaders; i++ )
343 {
344 if (i == orgLoader)
345 continue;
346
347 Com_sprintf( altName, sizeof (altName), "%s.%s", localName, modelLoaders[ i ].ext );
348
349 // Load
350 hModel = modelLoaders[ i ].ModelLoader( altName, mod );
351
352 if( hModel )
353 {
354 if( orgNameFailed )
355 {
356 ri.Printf( PRINT_DEVELOPER, "WARNING: %s not present, using %s instead\n",
357 name, altName );
358 }
359
360 break;
361 }
362 }
363
364 return hModel;
365 }
366
367 /*
368 =================
369 R_LoadMD3
370 =================
371 */
372 static qboolean R_LoadMD3 (model_t *mod, int lod, void *buffer, const char *mod_name ) {
373 int i, j;
374 md3Header_t *pinmodel;
375 md3Frame_t *frame;
376 md3Surface_t *surf;
377 md3Shader_t *shader;
378 md3Triangle_t *tri;
379 md3St_t *st;
380 md3XyzNormal_t *xyz;
381 md3Tag_t *tag;
382 int version;
383 int size;
384
385 pinmodel = (md3Header_t *)buffer;
386
387 version = LittleLong (pinmodel->version);
388 if (version != MD3_VERSION) {
389 ri.Printf( PRINT_WARNING, "R_LoadMD3: %s has wrong version (%i should be %i)\n",
390 mod_name, version, MD3_VERSION);
391 return qfalse;
392 }
393
394 mod->type = MOD_MESH;
395 size = LittleLong(pinmodel->ofsEnd);
396 mod->dataSize += size;
397 mod->md3[lod] = ri.Hunk_Alloc( size, h_low );
398
399 Com_Memcpy (mod->md3[lod], buffer, LittleLong(pinmodel->ofsEnd) );
400
401 LL(mod->md3[lod]->ident);
402 LL(mod->md3[lod]->version);
403 LL(mod->md3[lod]->numFrames);
404 LL(mod->md3[lod]->numTags);
405 LL(mod->md3[lod]->numSurfaces);
406 LL(mod->md3[lod]->ofsFrames);
407 LL(mod->md3[lod]->ofsTags);
408 LL(mod->md3[lod]->ofsSurfaces);
409 LL(mod->md3[lod]->ofsEnd);
410
411 if ( mod->md3[lod]->numFrames < 1 ) {
412 ri.Printf( PRINT_WARNING, "R_LoadMD3: %s has no frames\n", mod_name );
413 return qfalse;
414 }
415
416 // swap all the frames
417 frame = (md3Frame_t *) ( (byte *)mod->md3[lod] + mod->md3[lod]->ofsFrames );
418 for ( i = 0 ; i < mod->md3[lod]->numFrames ; i++, frame++) {
419 frame->radius = LittleFloat( frame->radius );
420 for ( j = 0 ; j < 3 ; j++ ) {
421 frame->bounds[0][j] = LittleFloat( frame->bounds[0][j] );
422 frame->bounds[1][j] = LittleFloat( frame->bounds[1][j] );
423 frame->localOrigin[j] = LittleFloat( frame->localOrigin[j] );
424 }
425 }
426
427 // swap all the tags
428 tag = (md3Tag_t *) ( (byte *)mod->md3[lod] + mod->md3[lod]->ofsTags );
429 for ( i = 0 ; i < mod->md3[lod]->numTags * mod->md3[lod]->numFrames ; i++, tag++) {
430 for ( j = 0 ; j < 3 ; j++ ) {
431 tag->origin[j] = LittleFloat( tag->origin[j] );
432 tag->axis[0][j] = LittleFloat( tag->axis[0][j] );
433 tag->axis[1][j] = LittleFloat( tag->axis[1][j] );
434 tag->axis[2][j] = LittleFloat( tag->axis[2][j] );
435 }
436 }
437
438 // swap all the surfaces
439 surf = (md3Surface_t *) ( (byte *)mod->md3[lod] + mod->md3[lod]->ofsSurfaces );
440 for ( i = 0 ; i < mod->md3[lod]->numSurfaces ; i++) {
441
442 LL(surf->ident);
443 LL(surf->flags);
444 LL(surf->numFrames);
445 LL(surf->numShaders);
446 LL(surf->numTriangles);
447 LL(surf->ofsTriangles);
448 LL(surf->numVerts);
449 LL(surf->ofsShaders);
450 LL(surf->ofsSt);
451 LL(surf->ofsXyzNormals);
452 LL(surf->ofsEnd);
453
454 if ( surf->numVerts >= SHADER_MAX_VERTEXES ) {
455 ri.Printf(PRINT_WARNING, "R_LoadMD3: %s has more than %i verts on %s (%i).\n",
456 mod_name, SHADER_MAX_VERTEXES - 1, surf->name[0] ? surf->name : "a surface",
457 surf->numVerts );
458 return qfalse;
459 }
460 if ( surf->numTriangles*3 >= SHADER_MAX_INDEXES ) {
461 ri.Printf(PRINT_WARNING, "R_LoadMD3: %s has more than %i triangles on %s (%i).\n",
462 mod_name, ( SHADER_MAX_INDEXES / 3 ) - 1, surf->name[0] ? surf->name : "a surface",
463 surf->numTriangles );
464 return qfalse;
465 }
466
467 // change to surface identifier
468 surf->ident = SF_MD3;
469
470 // lowercase the surface name so skin compares are faster
471 Q_strlwr( surf->name );
472
473 // strip off a trailing _1 or _2
474 // this is a crutch for q3data being a mess
475 j = strlen( surf->name );
476 if ( j > 2 && surf->name[j-2] == '_' ) {
477 surf->name[j-2] = 0;
478 }
479
480 // register the shaders
481 shader = (md3Shader_t *) ( (byte *)surf + surf->ofsShaders );
482 for ( j = 0 ; j < surf->numShaders ; j++, shader++ ) {
483 shader_t *sh;
484
485 sh = R_FindShader( shader->name, LIGHTMAP_NONE, qtrue );
486 if ( sh->defaultShader ) {
487 shader->shaderIndex = 0;
488 } else {
489 shader->shaderIndex = sh->index;
490 }
491 }
492
493 // swap all the triangles
494 tri = (md3Triangle_t *) ( (byte *)surf + surf->ofsTriangles );
495 for ( j = 0 ; j < surf->numTriangles ; j++, tri++ ) {
496 LL(tri->indexes[0]);
497 LL(tri->indexes[1]);
498 LL(tri->indexes[2]);
499 }
500
501 // swap all the ST
502 st = (md3St_t *) ( (byte *)surf + surf->ofsSt );
503 for ( j = 0 ; j < surf->numVerts ; j++, st++ ) {
504 st->st[0] = LittleFloat( st->st[0] );
505 st->st[1] = LittleFloat( st->st[1] );
506 }
507
508 // swap all the XyzNormals
509 xyz = (md3XyzNormal_t *) ( (byte *)surf + surf->ofsXyzNormals );
510 for ( j = 0 ; j < surf->numVerts * surf->numFrames ; j++, xyz++ )
511 {
512 xyz->xyz[0] = LittleShort( xyz->xyz[0] );
513 xyz->xyz[1] = LittleShort( xyz->xyz[1] );
514 xyz->xyz[2] = LittleShort( xyz->xyz[2] );
515
516 xyz->normal = LittleShort( xyz->normal );
517 }
518
519
520 // find the next surface
521 surf = (md3Surface_t *)( (byte *)surf + surf->ofsEnd );
522 }
523
524 return qtrue;
525 }
526
527
528
529 /*
530 =================
531 R_LoadMDR
532 =================
533 */
534 static qboolean R_LoadMDR( model_t *mod, void *buffer, int filesize, const char *mod_name )
535 {
536 int i, j, k, l;
537 mdrHeader_t *pinmodel, *mdr;
538 mdrFrame_t *frame;
539 mdrLOD_t *lod, *curlod;
540 mdrSurface_t *surf, *cursurf;
541 mdrTriangle_t *tri, *curtri;
542 mdrVertex_t *v, *curv;
543 mdrWeight_t *weight, *curweight;
544 mdrTag_t *tag, *curtag;
545 int size;
546 shader_t *sh;
547
548 pinmodel = (mdrHeader_t *)buffer;
549
550 pinmodel->version = LittleLong(pinmodel->version);
551 if (pinmodel->version != MDR_VERSION)
552 {
553 ri.Printf(PRINT_WARNING, "R_LoadMDR: %s has wrong version (%i should be %i)\n", mod_name, pinmodel->version, MDR_VERSION);
554 return qfalse;
555 }
556
557 size = LittleLong(pinmodel->ofsEnd);
558
559 if(size > filesize)
560 {
561 ri.Printf(PRINT_WARNING, "R_LoadMDR: Header of %s is broken. Wrong filesize declared!\n", mod_name);
562 return qfalse;
563 }
564
565 mod->type = MOD_MDR;
566
567 LL(pinmodel->numFrames);
568 LL(pinmodel->numBones);
569 LL(pinmodel->ofsFrames);
570
571 // This is a model that uses some type of compressed Bones. We don't want to uncompress every bone for each rendered frame
572 // over and over again, we'll uncompress it in this function already, so we must adjust the size of the target mdr.
573 if(pinmodel->ofsFrames < 0)
574 {
575 // mdrFrame_t is larger than mdrCompFrame_t:
576 size += pinmodel->numFrames * sizeof(frame->name);
577 // now add enough space for the uncompressed bones.
578 size += pinmodel->numFrames * pinmodel->numBones * ((sizeof(mdrBone_t) - sizeof(mdrCompBone_t)));
579 }
580
581 // simple bounds check
582 if(pinmodel->numBones < 0 ||
583 sizeof(*mdr) + pinmodel->numFrames * (sizeof(*frame) + (pinmodel->numBones - 1) * sizeof(*frame->bones)) > size)
584 {
585 ri.Printf(PRINT_WARNING, "R_LoadMDR: %s has broken structure.\n", mod_name);
586 return qfalse;
587 }
588
589 mod->dataSize += size;
590 mod->modelData = mdr = ri.Hunk_Alloc( size, h_low );
591
592 // Copy all the values over from the file and fix endian issues in the process, if necessary.
593
594 mdr->ident = LittleLong(pinmodel->ident);
595 mdr->version = pinmodel->version; // Don't need to swap byte order on this one, we already did above.
596 Q_strncpyz(mdr->name, pinmodel->name, sizeof(mdr->name));
597 mdr->numFrames = pinmodel->numFrames;
598 mdr->numBones = pinmodel->numBones;
599 mdr->numLODs = LittleLong(pinmodel->numLODs);
600 mdr->numTags = LittleLong(pinmodel->numTags);
601 // We don't care about the other offset values, we'll generate them ourselves while loading.
602
603 mod->numLods = mdr->numLODs;
604
605 if ( mdr->numFrames < 1 )
606 {
607 ri.Printf(PRINT_WARNING, "R_LoadMDR: %s has no frames\n", mod_name);
608 return qfalse;
609 }
610
611 /* The first frame will be put into the first free space after the header */
612 frame = (mdrFrame_t *)(mdr + 1);
613 mdr->ofsFrames = (int)((byte *) frame - (byte *) mdr);
614
615 if (pinmodel->ofsFrames < 0)
616 {
617 mdrCompFrame_t *cframe;
618
619 // compressed model...
620 cframe = (mdrCompFrame_t *)((byte *) pinmodel - pinmodel->ofsFrames);
621
622 for(i = 0; i < mdr->numFrames; i++)
623 {
624 for(j = 0; j < 3; j++)
625 {
626 frame->bounds[0][j] = LittleFloat(cframe->bounds[0][j]);
627 frame->bounds[1][j] = LittleFloat(cframe->bounds[1][j]);
628 frame->localOrigin[j] = LittleFloat(cframe->localOrigin[j]);
629 }
630
631 frame->radius = LittleFloat(cframe->radius);
632 frame->name[0] = '\0'; // No name supplied in the compressed version.
633
634 for(j = 0; j < mdr->numBones; j++)
635 {
636 for(k = 0; k < (sizeof(cframe->bones[j].Comp) / 2); k++)
637 {
638 // Do swapping for the uncompressing functions. They seem to use shorts
639 // values only, so I assume this will work. Never tested it on other
640 // platforms, though.
641
642 ((unsigned short *)(cframe->bones[j].Comp))[k] =
643 LittleShort( ((unsigned short *)(cframe->bones[j].Comp))[k] );
644 }
645
646 /* Now do the actual uncompressing */
647 MC_UnCompress(frame->bones[j].matrix, cframe->bones[j].Comp);
648 }
649
650 // Next Frame...
651 cframe = (mdrCompFrame_t *) &cframe->bones[j];
652 frame = (mdrFrame_t *) &frame->bones[j];
653 }
654 }
655 else
656 {
657 mdrFrame_t *curframe;
658
659 // uncompressed model...
660 //
661
662 curframe = (mdrFrame_t *)((byte *) pinmodel + pinmodel->ofsFrames);
663
664 // swap all the frames
665 for ( i = 0 ; i < mdr->numFrames ; i++)
666 {
667 for(j = 0; j < 3; j++)
668 {
669 frame->bounds[0][j] = LittleFloat(curframe->bounds[0][j]);
670 frame->bounds[1][j] = LittleFloat(curframe->bounds[1][j]);
671 frame->localOrigin[j] = LittleFloat(curframe->localOrigin[j]);
672 }
673
674 frame->radius = LittleFloat(curframe->radius);
675 Q_strncpyz(frame->name, curframe->name, sizeof(frame->name));
676
677 for (j = 0; j < (int) (mdr->numBones * sizeof(mdrBone_t) / 4); j++)
678 {
679 ((float *)frame->bones)[j] = LittleFloat( ((float *)curframe->bones)[j] );
680 }
681
682 curframe = (mdrFrame_t *) &curframe->bones[mdr->numBones];
683 frame = (mdrFrame_t *) &frame->bones[mdr->numBones];
684 }
685 }
686
687 // frame should now point to the first free address after all frames.
688 lod = (mdrLOD_t *) frame;
689 mdr->ofsLODs = (int) ((byte *) lod - (byte *)mdr);
690
691 curlod = (mdrLOD_t *)((byte *) pinmodel + LittleLong(pinmodel->ofsLODs));
692
693 // swap all the LOD's
694 for ( l = 0 ; l < mdr->numLODs ; l++)
695 {
696 // simple bounds check
697 if((byte *) (lod + 1) > (byte *) mdr + size)
698 {
699 ri.Printf(PRINT_WARNING, "R_LoadMDR: %s has broken structure.\n", mod_name);
700 return qfalse;
701 }
702
703 lod->numSurfaces = LittleLong(curlod->numSurfaces);
704
705 // swap all the surfaces
706 surf = (mdrSurface_t *) (lod + 1);
707 lod->ofsSurfaces = (int)((byte *) surf - (byte *) lod);
708 cursurf = (mdrSurface_t *) ((byte *)curlod + LittleLong(curlod->ofsSurfaces));
709
710 for ( i = 0 ; i < lod->numSurfaces ; i++)
711 {
712 // simple bounds check
713 if((byte *) (surf + 1) > (byte *) mdr + size)
714 {
715 ri.Printf(PRINT_WARNING, "R_LoadMDR: %s has broken structure.\n", mod_name);
716 return qfalse;
717 }
718
719 // first do some copying stuff
720
721 surf->ident = SF_MDR;
722 Q_strncpyz(surf->name, cursurf->name, sizeof(surf->name));
723 Q_strncpyz(surf->shader, cursurf->shader, sizeof(surf->shader));
724
725 surf->ofsHeader = (byte *) mdr - (byte *) surf;
726
727 surf->numVerts = LittleLong(cursurf->numVerts);
728 surf->numTriangles = LittleLong(cursurf->numTriangles);
729 // numBoneReferences and BoneReferences generally seem to be unused
730
731 // now do the checks that may fail.
732 if ( surf->numVerts >= SHADER_MAX_VERTEXES )
733 {
734 ri.Printf(PRINT_WARNING, "R_LoadMDR: %s has more than %i verts on %s (%i).\n",
735 mod_name, SHADER_MAX_VERTEXES - 1, surf->name[0] ? surf->name : "a surface",
736 surf->numVerts );
737 return qfalse;
738 }
739 if ( surf->numTriangles*3 >= SHADER_MAX_INDEXES )
740 {
741 ri.Printf(PRINT_WARNING, "R_LoadMDR: %s has more than %i triangles on %s (%i).\n",
742 mod_name, ( SHADER_MAX_INDEXES / 3 ) - 1, surf->name[0] ? surf->name : "a surface",
743 surf->numTriangles );
744 return qfalse;
745 }
746 // lowercase the surface name so skin compares are faster
747 Q_strlwr( surf->name );
748
749 // register the shaders
750 sh = R_FindShader(surf->shader, LIGHTMAP_NONE, qtrue);
751 if ( sh->defaultShader ) {
752 surf->shaderIndex = 0;
753 } else {
754 surf->shaderIndex = sh->index;
755 }
756
757 // now copy the vertexes.
758 v = (mdrVertex_t *) (surf + 1);
759 surf->ofsVerts = (int)((byte *) v - (byte *) surf);
760 curv = (mdrVertex_t *) ((byte *)cursurf + LittleLong(cursurf->ofsVerts));
761
762 for(j = 0; j < surf->numVerts; j++)
763 {
764 LL(curv->numWeights);
765
766 // simple bounds check
767 if(curv->numWeights < 0 || (byte *) (v + 1) + (curv->numWeights - 1) * sizeof(*weight) > (byte *) mdr + size)
768 {
769 ri.Printf(PRINT_WARNING, "R_LoadMDR: %s has broken structure.\n", mod_name);
770 return qfalse;
771 }
772
773 v->normal[0] = LittleFloat(curv->normal[0]);
774 v->normal[1] = LittleFloat(curv->normal[1]);
775 v->normal[2] = LittleFloat(curv->normal[2]);
776
777 v->texCoords[0] = LittleFloat(curv->texCoords[0]);
778 v->texCoords[1] = LittleFloat(curv->texCoords[1]);
779
780 v->numWeights = curv->numWeights;
781 weight = &v->weights[0];
782 curweight = &curv->weights[0];
783
784 // Now copy all the weights
785 for(k = 0; k < v->numWeights; k++)
786 {
787 weight->boneIndex = LittleLong(curweight->boneIndex);
788 weight->boneWeight = LittleFloat(curweight->boneWeight);
789
790 weight->offset[0] = LittleFloat(curweight->offset[0]);
791 weight->offset[1] = LittleFloat(curweight->offset[1]);
792 weight->offset[2] = LittleFloat(curweight->offset[2]);
793
794 weight++;
795 curweight++;
796 }
797
798 v = (mdrVertex_t *) weight;
799 curv = (mdrVertex_t *) curweight;
800 }
801
802 // we know the offset to the triangles now:
803 tri = (mdrTriangle_t *) v;
804 surf->ofsTriangles = (int)((byte *) tri - (byte *) surf);
805 curtri = (mdrTriangle_t *)((byte *) cursurf + LittleLong(cursurf->ofsTriangles));
806
807 // simple bounds check
808 if(surf->numTriangles < 0 || (byte *) (tri + surf->numTriangles) > (byte *) mdr + size)
809 {
810 ri.Printf(PRINT_WARNING, "R_LoadMDR: %s has broken structure.\n", mod_name);
811 return qfalse;
812 }
813
814 for(j = 0; j < surf->numTriangles; j++)
815 {
816 tri->indexes[0] = LittleLong(curtri->indexes[0]);
817 tri->indexes[1] = LittleLong(curtri->indexes[1]);
818 tri->indexes[2] = LittleLong(curtri->indexes[2]);
819
820 tri++;
821 curtri++;
822 }
823
824 // tri now points to the end of the surface.
825 surf->ofsEnd = (byte *) tri - (byte *) surf;
826 surf = (mdrSurface_t *) tri;
827
828 // find the next surface.
829 cursurf = (mdrSurface_t *) ((byte *) cursurf + LittleLong(cursurf->ofsEnd));
830 }
831
832 // surf points to the next lod now.
833 lod->ofsEnd = (int)((byte *) surf - (byte *) lod);
834 lod = (mdrLOD_t *) surf;
835
836 // find the next LOD.
837 curlod = (mdrLOD_t *)((byte *) curlod + LittleLong(curlod->ofsEnd));
838 }
839
840 // lod points to the first tag now, so update the offset too.
841 tag = (mdrTag_t *) lod;
842 mdr->ofsTags = (int)((byte *) tag - (byte *) mdr);
843 curtag = (mdrTag_t *) ((byte *)pinmodel + LittleLong(pinmodel->ofsTags));
844
845 // simple bounds check
846 if(mdr->numTags < 0 || (byte *) (tag + mdr->numTags) > (byte *) mdr + size)
847 {
848 ri.Printf(PRINT_WARNING, "R_LoadMDR: %s has broken structure.\n", mod_name);
849 return qfalse;
850 }
851
852 for (i = 0 ; i < mdr->numTags ; i++)
853 {
854 tag->boneIndex = LittleLong(curtag->boneIndex);
855 Q_strncpyz(tag->name, curtag->name, sizeof(tag->name));
856
857 tag++;
858 curtag++;
859 }
860
861 // And finally we know the real offset to the end.
862 mdr->ofsEnd = (int)((byte *) tag - (byte *) mdr);
863
864 // phew! we're done.
865
866 return qtrue;
867 }
868
869
870
871 //=============================================================================
872
873 /*
874 ** RE_BeginRegistration
875 */
876 void RE_BeginRegistration( glconfig_t *glconfigOut ) {
877
878 R_Init();
879
880 *glconfigOut = glConfig;
881
882 R_IssuePendingRenderCommands();
883
884 tr.viewCluster = -1; // force markleafs to regenerate
885 R_ClearFlares();
886 RE_ClearScene();
887
888 tr.registered = qtrue;
889 }
890
891 //=============================================================================
892
893 /*
894 ===============
895 R_ModelInit
896 ===============
897 */
898 void R_ModelInit( void ) {
899 model_t *mod;
900
901 // leave a space for NULL model
902 tr.numModels = 0;
903
904 mod = R_AllocModel();
905 mod->type = MOD_BAD;
906 }
907
908
909 /*
910 ================
911 R_Modellist_f
912 ================
913 */
914 void R_Modellist_f( void ) {
915 int i, j;
916 model_t *mod;
917 int total;
918 int lods;
919
920 total = 0;
921 for ( i = 1 ; i < tr.numModels; i++ ) {
922 mod = tr.models[i];
923 lods = 1;
924 for ( j = 1 ; j < MD3_MAX_LODS ; j++ ) {
925 if ( mod->md3[j] && mod->md3[j] != mod->md3[j-1] ) {
926 lods++;
927 }
928 }
929 ri.Printf( PRINT_ALL, "%8i : (%i) %s\n",mod->dataSize, lods, mod->name );
930 total += mod->dataSize;
931 }
932 ri.Printf( PRINT_ALL, "%8i : Total models\n", total );
933
934 #if 0 // not working right with new hunk
935 if ( tr.world ) {
936 ri.Printf( PRINT_ALL, "\n%8i : %s\n", tr.world->dataSize, tr.world->name );
937 }
938 #endif
939 }
940
941
942 //=============================================================================
943
944
945 /*
946 ================
947 R_GetTag
948 ================
949 */
950 static md3Tag_t *R_GetTag( md3Header_t *mod, int frame, const char *tagName ) {
951 md3Tag_t *tag;
952 int i;
953
954 if ( frame >= mod->numFrames ) {
955 // it is possible to have a bad frame while changing models, so don't error
956 frame = mod->numFrames - 1;
957 }
958
959 tag = (md3Tag_t *)((byte *)mod + mod->ofsTags) + frame * mod->numTags;
960 for ( i = 0 ; i < mod->numTags ; i++, tag++ ) {
961 if ( !strcmp( tag->name, tagName ) ) {
962 return tag; // found it
963 }
964 }
965
966 return NULL;
967 }
968
969 md3Tag_t *R_GetAnimTag( mdrHeader_t *mod, int framenum, const char *tagName, md3Tag_t * dest)
970 {
971 int i, j, k;
972 int frameSize;
973 mdrFrame_t *frame;
974 mdrTag_t *tag;
975
976 if ( framenum >= mod->numFrames )
977 {
978 // it is possible to have a bad frame while changing models, so don't error
979 framenum = mod->numFrames - 1;
980 }
981
982 tag = (mdrTag_t *)((byte *)mod + mod->ofsTags);
983 for ( i = 0 ; i < mod->numTags ; i++, tag++ )
984 {
985 if ( !strcmp( tag->name, tagName ) )
986 {
987 Q_strncpyz(dest->name, tag->name, sizeof(dest->name));
988
989 // uncompressed model...
990 //
991 frameSize = (intptr_t)( &((mdrFrame_t *)0)->bones[ mod->numBones ] );
992 frame = (mdrFrame_t *)((byte *)mod + mod->ofsFrames + framenum * frameSize );
993
994 for (j = 0; j < 3; j++)
995 {
996 for (k = 0; k < 3; k++)
997 dest->axis[j][k]=frame->bones[tag->boneIndex].matrix[k][j];
998 }
999
1000 dest->origin[0]=frame->bones[tag->boneIndex].matrix[0][3];
1001 dest->origin[1]=frame->bones[tag->boneIndex].matrix[1][3];
1002 dest->origin[2]=frame->bones[tag->boneIndex].matrix[2][3];
1003
1004 return dest;
1005 }
1006 }
1007
1008 return NULL;
1009 }
1010
1011 /*
1012 ================
1013 R_LerpTag
1014 ================
1015 */
1016 int R_LerpTag( orientation_t *tag, qhandle_t handle, int startFrame, int endFrame,
1017 float frac, const char *tagName ) {
1018 md3Tag_t *start, *end;
1019 md3Tag_t start_space, end_space;
1020 int i;
1021 float frontLerp, backLerp;
1022 model_t *model;
1023
1024 model = R_GetModelByHandle( handle );
1025 if ( !model->md3[0] )
1026 {
1027 if(model->type == MOD_MDR)
1028 {
1029 start = R_GetAnimTag((mdrHeader_t *) model->modelData, startFrame, tagName, &start_space);
1030 end = R_GetAnimTag((mdrHeader_t *) model->modelData, endFrame, tagName, &end_space);
1031 }
1032 else if( model->type == MOD_IQM ) {
1033 return R_IQMLerpTag( tag, model->modelData,
1034 startFrame, endFrame,
1035 frac, tagName );
1036 } else {
1037 start = end = NULL;
1038 }
1039 }
1040 else
1041 {
1042 start = R_GetTag( model->md3[0], startFrame, tagName );
1043 end = R_GetTag( model->md3[0], endFrame, tagName );
1044 }
1045
1046 if ( !start || !end ) {
1047 AxisClear( tag->axis );
1048 VectorClear( tag->origin );
1049 return qfalse;
1050 }
1051
1052 frontLerp = frac;
1053 backLerp = 1.0f - frac;
1054
1055 for ( i = 0 ; i < 3 ; i++ ) {
1056 tag->origin[i] = start->origin[i] * backLerp + end->origin[i] * frontLerp;
1057 tag->axis[0][i] = start->axis[0][i] * backLerp + end->axis[0][i] * frontLerp;
1058 tag->axis[1][i] = start->axis[1][i] * backLerp + end->axis[1][i] * frontLerp;
1059 tag->axis[2][i] = start->axis[2][i] * backLerp + end->axis[2][i] * frontLerp;
1060 }
1061 VectorNormalize( tag->axis[0] );
1062 VectorNormalize( tag->axis[1] );
1063 VectorNormalize( tag->axis[2] );
1064 return qtrue;
1065 }
1066
1067
1068 /*
1069 ====================
1070 R_ModelBounds
1071 ====================
1072 */
1073 void R_ModelBounds( qhandle_t handle, vec3_t mins, vec3_t maxs ) {
1074 model_t *model;
1075
1076 model = R_GetModelByHandle( handle );
1077
1078 if(model->type == MOD_BRUSH) {
1079 VectorCopy( model->bmodel->bounds[0], mins );
1080 VectorCopy( model->bmodel->bounds[1], maxs );
1081
1082 return;
1083 } else if (model->type == MOD_MESH) {
1084 md3Header_t *header;
1085 md3Frame_t *frame;
1086
1087 header = model->md3[0];
1088 frame = (md3Frame_t *) ((byte *)header + header->ofsFrames);
1089
1090 VectorCopy( frame->bounds[0], mins );
1091 VectorCopy( frame->bounds[1], maxs );
1092
1093 return;
1094 } else if (model->type == MOD_MDR) {
1095 mdrHeader_t *header;
1096 mdrFrame_t *frame;
1097
1098 header = (mdrHeader_t *)model->modelData;
1099 frame = (mdrFrame_t *) ((byte *)header + header->ofsFrames);
1100
1101 VectorCopy( frame->bounds[0], mins );
1102 VectorCopy( frame->bounds[1], maxs );
1103
1104 return;
1105 } else if(model->type == MOD_IQM) {
1106 iqmData_t *iqmData;
1107
1108 iqmData = model->modelData;
1109
1110 if(iqmData->bounds)
1111 {
1112 VectorCopy(iqmData->bounds, mins);
1113 VectorCopy(iqmData->bounds + 3, maxs);
1114 return;
1115 }
1116 }
1117
1118 VectorClear( mins );
1119 VectorClear( maxs );
1120 }