]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/model.c
set eol-style
[xonotic/netradiant.git] / tools / quake3 / q3map2 / model.c
1 /*
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 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 GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 ----------------------------------------------------------------------------------
22
23 This code has been altered significantly from its original form, to support
24 several games based on the Quake III Arena engine, in the form of "Q3Map2."
25
26 ------------------------------------------------------------------------------- */
27
28
29
30 /* marker */
31 #define MODEL_C
32
33
34
35 /* dependencies */
36 #include "q3map2.h"
37
38
39
40 /* 
41 PicoPrintFunc()
42 callback for picomodel.lib
43 */
44
45 void PicoPrintFunc( int level, const char *str )
46 {
47         if( str == NULL )
48                 return;
49         switch( level )
50         {
51                 case PICO_NORMAL:
52                         Sys_Printf( "%s\n", str );
53                         break;
54                 
55                 case PICO_VERBOSE:
56                         Sys_FPrintf( SYS_VRB, "%s\n", str );
57                         break;
58                 
59                 case PICO_WARNING:
60                         Sys_Printf( "WARNING: %s\n", str );
61                         break;
62                 
63                 case PICO_ERROR:
64                         Sys_Printf( "ERROR: %s\n", str );
65                         break;
66                 
67                 case PICO_FATAL:
68                         Error( "ERROR: %s\n", str );
69                         break;
70         }
71 }
72
73
74
75 /* 
76 PicoLoadFileFunc()
77 callback for picomodel.lib
78 */
79
80 void PicoLoadFileFunc( char *name, byte **buffer, int *bufSize )
81 {
82         *bufSize = vfsLoadFile( (const char*) name, (void**) buffer, 0 );
83 }
84
85
86
87 /*
88 FindModel() - ydnar
89 finds an existing picoModel and returns a pointer to the picoModel_t struct or NULL if not found
90 */
91
92 picoModel_t *FindModel( char *name, int frame )
93 {
94         int                     i;
95         
96         
97         /* init */
98         if( numPicoModels <= 0 )
99                 memset( picoModels, 0, sizeof( picoModels ) );
100         
101         /* dummy check */
102         if( name == NULL || name[ 0 ] == '\0' )
103                 return NULL;
104         
105         /* search list */
106         for( i = 0; i < MAX_MODELS; i++ )
107         {
108                 if( picoModels[ i ] != NULL &&
109                         !strcmp( PicoGetModelName( picoModels[ i ] ), name ) &&
110                         PicoGetModelFrameNum( picoModels[ i ] ) == frame )
111                         return picoModels[ i ];
112         }
113         
114         /* no matching picoModel found */
115         return NULL;
116 }
117
118
119
120 /*
121 LoadModel() - ydnar
122 loads a picoModel and returns a pointer to the picoModel_t struct or NULL if not found
123 */
124
125 picoModel_t *LoadModel( char *name, int frame )
126 {
127         int                             i;
128         picoModel_t             *model, **pm;
129         
130         
131         /* init */
132         if( numPicoModels <= 0 )
133                 memset( picoModels, 0, sizeof( picoModels ) );
134         
135         /* dummy check */
136         if( name == NULL || name[ 0 ] == '\0' )
137                 return NULL;
138         
139         /* try to find existing picoModel */
140         model = FindModel( name, frame );
141         if( model != NULL )
142                 return model;
143         
144         /* none found, so find first non-null picoModel */
145         pm = NULL;
146         for( i = 0; i < MAX_MODELS; i++ )
147         {
148                 if( picoModels[ i ] == NULL )
149                 {
150                         pm = &picoModels[ i ];
151                         break;
152                 }
153         }
154         
155         /* too many picoModels? */
156         if( pm == NULL )
157                 Error( "MAX_MODELS (%d) exceeded, there are too many model files referenced by the map.", MAX_MODELS );
158         
159         /* attempt to parse model */
160         *pm = PicoLoadModel( (char*) name, frame );
161         
162         /* if loading failed, make a bogus model to silence the rest of the warnings */
163         if( *pm == NULL )
164         {
165                 /* allocate a new model */
166                 *pm = PicoNewModel();
167                 if( *pm == NULL )
168                         return NULL;
169                 
170                 /* set data */
171                 PicoSetModelName( *pm, name );
172                 PicoSetModelFrameNum( *pm, frame );
173         }
174         
175         /* debug code */
176         #if 0
177         {
178                 int                             numSurfaces, numVertexes;
179                 picoSurface_t   *ps;
180                 
181                 
182                 Sys_Printf( "Model %s\n", name );
183                 numSurfaces = PicoGetModelNumSurfaces( *pm );
184                 for( i = 0; i < numSurfaces; i++ )
185                 {
186                         ps = PicoGetModelSurface( *pm, i );
187                         numVertexes = PicoGetSurfaceNumVertexes( ps );
188                         Sys_Printf( "Surface %d has %d vertexes\n", i, numVertexes );
189                 }
190         }
191         #endif
192         
193         /* set count */
194         if( *pm != NULL )
195                 numPicoModels++;
196         
197         /* return the picoModel */
198         return *pm;
199 }
200
201
202
203 /*
204 InsertModel() - ydnar
205 adds a picomodel into the bsp
206 */
207
208 void InsertModel( char *name, int frame, m4x4_t transform, remap_t *remap, shaderInfo_t *celShader, int eNum, int castShadows, int recvShadows, int spawnFlags, float lightmapScale )
209 {
210         int                                     i, j, k, s, numSurfaces;
211         m4x4_t                          identity, nTransform;
212         picoModel_t                     *model;
213         picoShader_t            *shader;
214         picoSurface_t           *surface;
215         shaderInfo_t            *si;
216         mapDrawSurface_t        *ds;
217         bspDrawVert_t           *dv;
218         char                            *picoShaderName;
219         char                            shaderName[ MAX_QPATH ];
220         picoVec_t                       *xyz, *normal, *st;
221         byte                            *color;
222         picoIndex_t                     *indexes;
223         remap_t                         *rm, *glob;
224         
225         
226         /* get model */
227         model = LoadModel( name, frame );
228         if( model == NULL )
229                 return;
230         
231         /* handle null matrix */
232         if( transform == NULL )
233         {
234                 m4x4_identity( identity );
235                 transform = identity;
236         }
237         
238         /* hack: Stable-1_2 and trunk have differing row/column major matrix order
239            this transpose is necessary with Stable-1_2
240            uncomment the following line with old m4x4_t (non 1.3/spog_branch) code */
241         //%     m4x4_transpose( transform );
242         
243         /* create transform matrix for normals */
244         memcpy( nTransform, transform, sizeof( m4x4_t ) );
245         if( m4x4_invert( nTransform ) )
246                 Sys_FPrintf( SYS_VRB, "WARNING: Can't invert model transform matrix, using transpose instead\n" );
247         m4x4_transpose( nTransform );
248         
249         /* fix bogus lightmap scale */
250         if( lightmapScale <= 0.0f )
251                 lightmapScale = 1.0f;
252         
253         /* each surface on the model will become a new map drawsurface */
254         numSurfaces = PicoGetModelNumSurfaces( model );
255         //%     Sys_FPrintf( SYS_VRB, "Model %s has %d surfaces\n", name, numSurfaces );
256         for( s = 0; s < numSurfaces; s++ )
257         {
258                 /* get surface */
259                 surface = PicoGetModelSurface( model, s );
260                 if( surface == NULL )
261                         continue;
262                 
263                 /* only handle triangle surfaces initially (fixme: support patches) */
264                 if( PicoGetSurfaceType( surface ) != PICO_TRIANGLES )
265                         continue;
266                 
267                 /* fix the surface's normals */
268                 PicoFixSurfaceNormals( surface );
269                 
270                 /* allocate a surface (ydnar: gs mods) */
271                 ds = AllocDrawSurface( SURFACE_TRIANGLES );
272                 ds->entityNum = eNum;
273                 ds->castShadows = castShadows;
274                 ds->recvShadows = recvShadows;
275                 
276                 /* get shader name */
277         shader = PicoGetSurfaceShader( surface );
278                 if( shader == NULL )
279                         picoShaderName = "";
280                 else
281                         picoShaderName = PicoGetShaderName( shader );
282                 
283                 /* handle shader remapping */
284                 glob = NULL;
285                 for( rm = remap; rm != NULL; rm = rm->next )
286                 {
287                         if( rm->from[ 0 ] == '*' && rm->from[ 1 ] == '\0' )
288                                 glob = rm;
289                         else if( !Q_stricmp( picoShaderName, rm->from ) )
290                         {
291                                 Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", picoShaderName, rm->to );
292                                 picoShaderName = rm->to;
293                                 glob = NULL;
294                                 break;
295                         }
296                 }
297                 
298                 if( glob != NULL )
299                 {
300                         Sys_FPrintf( SYS_VRB, "Globbing %s to %s\n", picoShaderName, glob->to );
301                         picoShaderName = glob->to;
302                 }
303                 
304                 /* shader renaming for sof2 */
305                 if( renameModelShaders )
306                 {
307                         strcpy( shaderName, picoShaderName );
308                         StripExtension( shaderName );
309                         if( spawnFlags & 1 )
310                                 strcat( shaderName, "_RMG_BSP" );
311                         else
312                                 strcat( shaderName, "_BSP" );
313                         si = ShaderInfoForShader( shaderName );
314                 }
315                 else
316                         si = ShaderInfoForShader( picoShaderName );
317                 
318                 /* set shader */
319                 ds->shaderInfo = si;
320                 
321                 /* set lightmap scale */
322                 ds->lightmapScale = lightmapScale;
323                 
324                 /* force to meta? */
325                 if( si != NULL && si->forceMeta )
326                         ds->type = SURFACE_FORCED_META;
327                 
328                 /* set particulars */
329                 ds->numVerts = PicoGetSurfaceNumVertexes( surface );
330                 ds->verts = safe_malloc( ds->numVerts * sizeof( ds->verts[ 0 ] ) );
331                 memset( ds->verts, 0, ds->numVerts * sizeof( ds->verts[ 0 ] ) );
332                 
333                 ds->numIndexes = PicoGetSurfaceNumIndexes( surface );
334                 ds->indexes = safe_malloc( ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
335                 memset( ds->indexes, 0, ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
336                 
337                 /* copy vertexes */
338                 for( i = 0; i < ds->numVerts; i++ )
339                 {
340                         /* get vertex */
341                         dv = &ds->verts[ i ];
342                         
343                         /* xyz and normal */
344                         xyz = PicoGetSurfaceXYZ( surface, i );
345                         VectorCopy( xyz, dv->xyz );
346                         m4x4_transform_point( transform, dv->xyz );
347                         
348                         normal = PicoGetSurfaceNormal( surface, i );
349                         VectorCopy( normal, dv->normal );
350                         m4x4_transform_normal( nTransform, dv->normal );
351                         VectorNormalize( dv->normal, dv->normal );
352
353                         /* ydnar: tek-fu celshading support for flat shaded shit */
354                         if( flat )
355                         {
356                                 dv->st[ 0 ] = si->stFlat[ 0 ];
357                                 dv->st[ 1 ] = si->stFlat[ 1 ];
358                         }
359                         
360                         /* ydnar: gs mods: added support for explicit shader texcoord generation */
361                         else if( si->tcGen )
362                         {
363                                 /* project the texture */
364                                 dv->st[ 0 ] = DotProduct( si->vecs[ 0 ], dv->xyz );
365                                 dv->st[ 1 ] = DotProduct( si->vecs[ 1 ], dv->xyz );
366                         }
367                         
368                         /* normal texture coordinates */
369                         {
370                                 st = PicoGetSurfaceST( surface, 0, i );
371                                 dv->st[ 0 ] = st[ 0 ];
372                                 dv->st[ 1 ] = st[ 1 ];
373                         }
374                         
375                         /* set lightmap/color bits */
376                         color = PicoGetSurfaceColor( surface, 0, i );
377                         for( j = 0; j < MAX_LIGHTMAPS; j++ )
378                         {
379                                 dv->lightmap[ j ][ 0 ] = 0.0f;
380                                 dv->lightmap[ j ][ 1 ] = 0.0f;
381                                 dv->color[ j ][ 0 ] = color[ 0 ];
382                                 dv->color[ j ][ 1 ] = color[ 1 ];
383                                 dv->color[ j ][ 2 ] = color[ 2 ];
384                                 dv->color[ j ][ 3 ] = color[ 3 ];
385                         }
386                 }
387                 
388                 /* copy indexes */
389                 indexes = PicoGetSurfaceIndexes( surface, 0 );
390                 for( i = 0; i < ds->numIndexes; i++ )
391                         ds->indexes[ i ] = indexes[ i ];
392                 
393                 /* set cel shader */
394                 ds->celShader = celShader;
395                 
396                 /* finish surface */
397                 FinishSurface( ds );
398                 
399                 /* ydnar: giant hack land: generate clipping brushes for model triangles */
400                 if( si->clipModel || (spawnFlags & 2) ) /* 2nd bit */
401                 {
402                         vec3_t          points[ 3 ], backs[ 3 ];
403                         vec4_t          plane, reverse, pa, pb, pc;
404                         vec3_t          nadir;
405                         
406                         
407                         /* temp hack */
408                         if( (si->compileFlags & C_TRANSLUCENT) || !(si->compileFlags & C_SOLID) )
409                                 continue;
410                         
411                         /* overflow check */
412                         if( (nummapplanes + 64) >= (MAX_MAP_PLANES >> 1) )
413                                 continue;
414                         
415                         /* walk triangle list */
416                         for( i = 0; i < ds->numIndexes; i += 3 )
417                         {
418                                 /* overflow hack */
419                                 if( (nummapplanes + 64) >= (MAX_MAP_PLANES >> 1) )
420                                 {
421                                         Sys_Printf( "WARNING: MAX_MAP_PLANES (%d) hit generating clip brushes for model %s.\n",
422                                                 MAX_MAP_PLANES, name );
423                                         break;
424                                 }
425                                 
426                                 /* make points and back points */
427                                 for( j = 0; j < 3; j++ )
428                                 {
429                                         /* get vertex */
430                                         dv = &ds->verts[ ds->indexes[ i + j ] ];
431                                         
432                                         /* copy xyz */
433                                         VectorCopy( dv->xyz, points[ j ] );
434                                         VectorCopy( dv->xyz, backs[ j ] );
435                                         
436                                         /* find nearest axial to normal and push back points opposite */
437                                         /* note: this doesn't work as well as simply using the plane of the triangle, below */
438                                         for( k = 0; k < 3; k++ )
439                                         {
440                                                 if( fabs( dv->normal[ k ] ) > fabs( dv->normal[ (k + 1) % 3 ] ) &&
441                                                         fabs( dv->normal[ k ] ) > fabs( dv->normal[ (k + 2) % 3 ] ) )
442                                                 {
443                                                         backs[ j ][ k ] += dv->normal[ k ] < 0.0f ? 64.0f : -64.0f;
444                                                         break;
445                                                 }
446                                         }
447                                 }
448                                 
449                                 /* make plane for triangle */
450                                 if( PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] ) )
451                                 {
452                                         /* regenerate back points */
453                                         for( j = 0; j < 3; j++ )
454                                         {
455                                                 /* get vertex */
456                                                 dv = &ds->verts[ ds->indexes[ i + j ] ];
457                                                 
458                                                 /* copy xyz */
459                                                 VectorCopy( dv->xyz, backs[ j ] );
460                                                 
461                                                 /* find nearest axial to plane normal and push back points opposite */
462                                                 for( k = 0; k < 3; k++ )
463                                                 {
464                                                         if( fabs( plane[ k ] ) > fabs( plane[ (k + 1) % 3 ] ) &&
465                                                                 fabs( plane[ k ] ) > fabs( plane[ (k + 2) % 3 ] ) )
466                                                         {
467                                                                 backs[ j ][ k ] += plane[ k ] < 0.0f ? 64.0f : -64.0f;
468                                                                 break;
469                                                         }
470                                                 }
471                                         }
472                                         
473                                         /* make back plane */
474                                         VectorScale( plane, -1.0f, reverse );
475                                         reverse[ 3 ] = -(plane[ 3 ] - 1);
476                                         
477                                         /* make back pyramid point */
478                                         VectorCopy( points[ 0 ], nadir );
479                                         VectorAdd( nadir, points[ 1 ], nadir );
480                                         VectorAdd( nadir, points[ 2 ], nadir );
481                                         VectorScale( nadir, 0.3333333333333f, nadir );
482                                         VectorMA( nadir, -2.0f, plane, nadir );
483                                         
484                                         /* make 3 more planes */
485                                         //%     if( PlaneFromPoints( pa, points[ 2 ], points[ 1 ], nadir ) &&
486                                         //%             PlaneFromPoints( pb, points[ 1 ], points[ 0 ], nadir ) &&
487                                         //%             PlaneFromPoints( pc, points[ 0 ], points[ 2 ], nadir ) )
488                                         if( PlaneFromPoints( pa, points[ 2 ], points[ 1 ], backs[ 1 ] ) &&
489                                                 PlaneFromPoints( pb, points[ 1 ], points[ 0 ], backs[ 0 ] ) &&
490                                                 PlaneFromPoints( pc, points[ 0 ], points[ 2 ], backs[ 2 ] ) )
491                                         {
492                                                 /* build a brush */
493                                                 buildBrush = AllocBrush( 48 );
494                                                 
495                                                 buildBrush->entityNum = mapEntityNum;
496                                                 buildBrush->original = buildBrush;
497                                                 buildBrush->contentShader = si;
498                                                 buildBrush->compileFlags = si->compileFlags;
499                                                 buildBrush->contentFlags = si->contentFlags;
500                                                 buildBrush->detail = qtrue;
501                                                 
502                                                 /* set up brush sides */
503                                                 buildBrush->numsides = 5;
504                                                 for( j = 0; j < buildBrush->numsides; j++ )
505                                                         buildBrush->sides[ j ].shaderInfo = si;
506                                                 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
507                                                 buildBrush->sides[ 1 ].planenum = FindFloatPlane( pa, pa[ 3 ], 1, &points[ 2 ] );
508                                                 buildBrush->sides[ 2 ].planenum = FindFloatPlane( pb, pb[ 3 ], 1, &points[ 1 ] );
509                                                 buildBrush->sides[ 3 ].planenum = FindFloatPlane( pc, pc[ 3 ], 1, &points[ 0 ] );
510                                                 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 3, points );
511                                                 
512                                                 /* add to entity */
513                                                 if( CreateBrushWindings( buildBrush ) )
514                                                 {
515                                                         AddBrushBevels();
516                                                         //%     EmitBrushes( buildBrush, NULL, NULL );
517                                                         buildBrush->next = entities[ mapEntityNum ].brushes;
518                                                         entities[ mapEntityNum ].brushes = buildBrush;
519                                                         entities[ mapEntityNum ].numBrushes++;
520                                                 }
521                                                 else
522                                                         free( buildBrush );
523                                         }
524                                 }
525                         }
526                 }
527         }
528 }
529
530
531
532 /*
533 AddTriangleModels()
534 adds misc_model surfaces to the bsp
535 */
536
537 void AddTriangleModels( entity_t *e )
538 {
539         int                             num, frame, castShadows, recvShadows, spawnFlags;
540         entity_t                *e2;
541         const char              *targetName;
542         const char              *target, *model, *value;
543         char                    shader[ MAX_QPATH ];
544         shaderInfo_t    *celShader;
545         float                   temp, baseLightmapScale, lightmapScale;
546         vec3_t                  origin, scale, angles;
547         m4x4_t                  transform;
548         epair_t                 *ep;
549         remap_t                 *remap, *remap2;
550         char                    *split;
551         
552         
553         /* note it */
554         Sys_FPrintf( SYS_VRB, "--- AddTriangleModels ---\n" );
555         
556         /* get current brush entity targetname */
557         if( e == entities )
558                 targetName = "";
559         else
560         {
561                 targetName = ValueForKey( e, "targetname" );
562         
563                 /* misc_model entities target non-worldspawn brush model entities */
564                 if( targetName[ 0 ] == '\0' )
565                         return;
566         }
567         
568         /* get lightmap scale */
569         baseLightmapScale = FloatForKey( e, "_lightmapscale" );
570         if( baseLightmapScale <= 0.0f )
571                 baseLightmapScale = 0.0f;
572         
573         /* walk the entity list */
574         for( num = 1; num < numEntities; num++ )
575         {
576                 /* get e2 */
577                 e2 = &entities[ num ];
578                 
579                 /* convert misc_models into raw geometry */
580                 if( Q_stricmp( "misc_model", ValueForKey( e2, "classname" ) ) )
581                         continue;
582
583                 /* ydnar: added support for md3 models on non-worldspawn models */
584                 target = ValueForKey( e2, "target" );
585                 if( strcmp( target, targetName ) )
586                         continue;
587                 
588                 /* get model name */
589                 model = ValueForKey( e2, "model" );
590                 if( model[ 0 ] == '\0' )
591                 {
592                         Sys_Printf( "WARNING: misc_model at %i %i %i without a model key\n",
593                                 (int) origin[ 0 ], (int) origin[ 1 ], (int) origin[ 2 ] );
594                         continue;
595                 }
596                 
597                 /* get model frame */
598                 frame = IntForKey( e2, "_frame" );
599                 
600                 /* worldspawn (and func_groups) default to cast/recv shadows in worldspawn group */
601                 if( e == entities )
602                 {
603                         castShadows = WORLDSPAWN_CAST_SHADOWS;
604                         recvShadows = WORLDSPAWN_RECV_SHADOWS;
605                 }
606                 
607                 /* other entities don't cast any shadows, but recv worldspawn shadows */
608                 else
609                 {
610                         castShadows = ENTITY_CAST_SHADOWS;
611                         recvShadows = ENTITY_RECV_SHADOWS;
612                 }
613                 
614                 /* get explicit shadow flags */
615                 GetEntityShadowFlags( e2, e, &castShadows, &recvShadows );
616                 
617                 /* get spawnflags */
618                 spawnFlags = IntForKey( e2, "spawnflags" );
619                 
620                 /* get origin */
621                 GetVectorForKey( e2, "origin", origin );
622                 VectorSubtract( origin, e->origin, origin );    /* offset by parent */
623                 
624                 /* get scale */
625                 scale[ 0 ] = scale[ 1 ] = scale[ 2 ] = 1.0f;
626                 temp = FloatForKey( e2, "modelscale" );
627                 if( temp != 0.0f )
628                         scale[ 0 ] = scale[ 1 ] = scale[ 2 ] = temp;
629                 value = ValueForKey( e2, "modelscale_vec" );
630                 if( value[ 0 ] != '\0' )
631                         sscanf( value, "%f %f %f", &scale[ 0 ], &scale[ 1 ], &scale[ 2 ] );
632                 
633                 /* get "angle" (yaw) or "angles" (pitch yaw roll) */
634                 angles[ 0 ] = angles[ 1 ] = angles[ 2 ] = 0.0f;
635                 angles[ 2 ] = FloatForKey( e2, "angle" );
636                 value = ValueForKey( e2, "angles" );
637                 if( value[ 0 ] != '\0' )
638                         sscanf( value, "%f %f %f", &angles[ 1 ], &angles[ 2 ], &angles[ 0 ] );
639                 
640                 /* set transform matrix (thanks spog) */
641                 m4x4_identity( transform );
642                 m4x4_pivoted_transform_by_vec3( transform, origin, angles, eXYZ, scale, vec3_origin );
643                 
644                 /* get shader remappings */
645                 remap = NULL;
646                 for( ep = e2->epairs; ep != NULL; ep = ep->next )
647                 {
648                         /* look for keys prefixed with "_remap" */
649                         if( ep->key != NULL && ep->value != NULL &&
650                                 ep->key[ 0 ] != '\0' && ep->value[ 0 ] != '\0' &&
651                                 !Q_strncasecmp( ep->key, "_remap", 6 ) )
652                         {
653                                 /* create new remapping */
654                                 remap2 = remap;
655                                 remap = safe_malloc( sizeof( *remap ) );
656                                 remap->next = remap2;
657                                 strcpy( remap->from, ep->value );
658                                 
659                                 /* split the string */
660                                 split = strchr( remap->from, ';' );
661                                 if( split == NULL )
662                                 {
663                                         Sys_Printf( "WARNING: Shader _remap key found in misc_model without a ; character\n" );
664                                         free( remap );
665                                         remap = remap2;
666                                         continue;
667                                 }
668                                 
669                                 /* store the split */
670                                 *split = '\0';
671                                 strcpy( remap->to, (split + 1) );
672                                 
673                                 /* note it */
674                                 //%     Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", remap->from, remap->to );
675                         }
676                 }
677                 
678                 /* ydnar: cel shader support */
679                 value = ValueForKey( e2, "_celshader" );
680                 if( value[ 0 ] == '\0' )
681                         value = ValueForKey( &entities[ 0 ], "_celshader" );
682                 if( value[ 0 ] != '\0' )
683                 {
684                         sprintf( shader, "textures/%s", value );
685                         celShader = ShaderInfoForShader( shader );
686                 }
687                 else
688                         celShader = NULL;
689                 
690                 /* get lightmap scale */
691                 lightmapScale = FloatForKey( e2, "_lightmapscale" );
692                 if( lightmapScale <= 0.0f )
693                         lightmapScale = baseLightmapScale;
694                 
695                 /* insert the model */
696                 InsertModel( (char*) model, frame, transform, remap, celShader, mapEntityNum, castShadows, recvShadows, spawnFlags, lightmapScale );
697                 
698                 /* free shader remappings */
699                 while( remap != NULL )
700                 {
701                         remap2 = remap->next;
702                         free( remap );
703                         remap = remap2;
704                 }
705         }
706 }