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