]> icculus.org git repositories - divverent/netradiant.git/blob - tools/quake3/q3map2/model.c
Merge remote branch 'icculus/master'
[divverent/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, int lightmapSampleSize, float shadeAngle )
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         double                          normalEpsilon_save;
226         double                          distanceEpsilon_save;
227         
228         
229         /* get model */
230         model = LoadModel( name, frame );
231         if( model == NULL )
232                 return;
233         
234         /* handle null matrix */
235         if( transform == NULL )
236         {
237                 m4x4_identity( identity );
238                 transform = identity;
239         }
240         
241         /* hack: Stable-1_2 and trunk have differing row/column major matrix order
242            this transpose is necessary with Stable-1_2
243            uncomment the following line with old m4x4_t (non 1.3/spog_branch) code */
244         //%     m4x4_transpose( transform );
245         
246         /* create transform matrix for normals */
247         memcpy( nTransform, transform, sizeof( m4x4_t ) );
248         if( m4x4_invert( nTransform ) )
249                 Sys_FPrintf( SYS_VRB, "WARNING: Can't invert model transform matrix, using transpose instead\n" );
250         m4x4_transpose( nTransform );
251         
252         /* fix bogus lightmap scale */
253         if( lightmapScale <= 0.0f )
254                 lightmapScale = 1.0f;
255
256         /* fix bogus shade angle */
257         if( shadeAngle <= 0.0f )
258                 shadeAngle = 0.0f;
259         
260         /* each surface on the model will become a new map drawsurface */
261         numSurfaces = PicoGetModelNumSurfaces( model );
262         //%     Sys_FPrintf( SYS_VRB, "Model %s has %d surfaces\n", name, numSurfaces );
263         for( s = 0; s < numSurfaces; s++ )
264         {
265                 /* get surface */
266                 surface = PicoGetModelSurface( model, s );
267                 if( surface == NULL )
268                         continue;
269                 
270                 /* only handle triangle surfaces initially (fixme: support patches) */
271                 if( PicoGetSurfaceType( surface ) != PICO_TRIANGLES )
272                         continue;
273                 
274                 /* allocate a surface (ydnar: gs mods) */
275                 ds = AllocDrawSurface( SURFACE_TRIANGLES );
276                 ds->entityNum = eNum;
277                 ds->castShadows = castShadows;
278                 ds->recvShadows = recvShadows;
279                 
280                 /* get shader name */
281         shader = PicoGetSurfaceShader( surface );
282                 if( shader == NULL )
283                         picoShaderName = "";
284                 else
285                         picoShaderName = PicoGetShaderName( shader );
286                 
287                 /* handle shader remapping */
288                 glob = NULL;
289                 for( rm = remap; rm != NULL; rm = rm->next )
290                 {
291                         if( rm->from[ 0 ] == '*' && rm->from[ 1 ] == '\0' )
292                                 glob = rm;
293                         else if( !Q_stricmp( picoShaderName, rm->from ) )
294                         {
295                                 Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", picoShaderName, rm->to );
296                                 picoShaderName = rm->to;
297                                 glob = NULL;
298                                 break;
299                         }
300                 }
301                 
302                 if( glob != NULL )
303                 {
304                         Sys_FPrintf( SYS_VRB, "Globbing %s to %s\n", picoShaderName, glob->to );
305                         picoShaderName = glob->to;
306                 }
307                 
308                 /* shader renaming for sof2 */
309                 if( renameModelShaders )
310                 {
311                         strcpy( shaderName, picoShaderName );
312                         StripExtension( shaderName );
313                         if( spawnFlags & 1 )
314                                 strcat( shaderName, "_RMG_BSP" );
315                         else
316                                 strcat( shaderName, "_BSP" );
317                         si = ShaderInfoForShader( shaderName );
318                 }
319                 else
320                         si = ShaderInfoForShader( picoShaderName );
321                 
322                 /* set shader */
323                 ds->shaderInfo = si;
324
325                 /* force to meta? */
326                 if( (si != NULL && si->forceMeta) || (spawnFlags & 4) ) /* 3rd bit */
327                         ds->type = SURFACE_FORCED_META;
328
329                 /* fix the surface's normals (jal: conditioned by shader info) */
330                 if( !(spawnFlags & 64) && ( shadeAngle == 0.0f || ds->type != SURFACE_FORCED_META ) )
331                         PicoFixSurfaceNormals( surface );
332
333                 /* set sample size */
334                 if( lightmapSampleSize > 0.0f )
335                         ds->sampleSize = lightmapSampleSize;
336                 
337                 /* set lightmap scale */
338                 if( lightmapScale > 0.0f )
339                         ds->lightmapScale = lightmapScale;
340
341                 /* set shading angle */
342                 if( shadeAngle > 0.0f )
343                         ds->shadeAngleDegrees = shadeAngle;
344                 
345                 /* set particulars */
346                 ds->numVerts = PicoGetSurfaceNumVertexes( surface );
347                 ds->verts = safe_malloc( ds->numVerts * sizeof( ds->verts[ 0 ] ) );
348                 memset( ds->verts, 0, ds->numVerts * sizeof( ds->verts[ 0 ] ) );
349                 
350                 ds->numIndexes = PicoGetSurfaceNumIndexes( surface );
351                 ds->indexes = safe_malloc( ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
352                 memset( ds->indexes, 0, ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
353                 
354                 /* copy vertexes */
355                 for( i = 0; i < ds->numVerts; i++ )
356                 {
357                         /* get vertex */
358                         dv = &ds->verts[ i ];
359                         
360                         /* xyz and normal */
361                         xyz = PicoGetSurfaceXYZ( surface, i );
362                         VectorCopy( xyz, dv->xyz );
363                         m4x4_transform_point( transform, dv->xyz );
364                         
365                         normal = PicoGetSurfaceNormal( surface, i );
366                         VectorCopy( normal, dv->normal );
367                         m4x4_transform_normal( nTransform, dv->normal );
368                         VectorNormalize( dv->normal, dv->normal );
369
370                         /* ydnar: tek-fu celshading support for flat shaded shit */
371                         if( flat )
372                         {
373                                 dv->st[ 0 ] = si->stFlat[ 0 ];
374                                 dv->st[ 1 ] = si->stFlat[ 1 ];
375                         }
376                         
377                         /* ydnar: gs mods: added support for explicit shader texcoord generation */
378                         else if( si->tcGen )
379                         {
380                                 /* project the texture */
381                                 dv->st[ 0 ] = DotProduct( si->vecs[ 0 ], dv->xyz );
382                                 dv->st[ 1 ] = DotProduct( si->vecs[ 1 ], dv->xyz );
383                         }
384                         
385                         /* normal texture coordinates */
386                         else
387                         {
388                                 st = PicoGetSurfaceST( surface, 0, i );
389                                 dv->st[ 0 ] = st[ 0 ];
390                                 dv->st[ 1 ] = st[ 1 ];
391                         }
392                         
393                         /* set lightmap/color bits */
394                         color = PicoGetSurfaceColor( surface, 0, i );
395                         for( j = 0; j < MAX_LIGHTMAPS; j++ )
396                         {
397                                 dv->lightmap[ j ][ 0 ] = 0.0f;
398                                 dv->lightmap[ j ][ 1 ] = 0.0f;
399                                 if(spawnFlags & 32) // spawnflag 32: model color -> alpha hack
400                                 {
401                                         dv->color[ j ][ 0 ] = 255.0f;
402                                         dv->color[ j ][ 1 ] = 255.0f;
403                                         dv->color[ j ][ 2 ] = 255.0f;
404                                         dv->color[ j ][ 3 ] = RGBTOGRAY( color );
405                                 }
406                                 else
407                                 {
408                                         dv->color[ j ][ 0 ] = color[ 0 ];
409                                         dv->color[ j ][ 1 ] = color[ 1 ];
410                                         dv->color[ j ][ 2 ] = color[ 2 ];
411                                         dv->color[ j ][ 3 ] = color[ 3 ];
412                                 }
413                         }
414                 }
415                 
416                 /* copy indexes */
417                 indexes = PicoGetSurfaceIndexes( surface, 0 );
418                 for( i = 0; i < ds->numIndexes; i++ )
419                         ds->indexes[ i ] = indexes[ i ];
420                 
421                 /* set cel shader */
422                 ds->celShader = celShader;
423                 
424                 /* ydnar: giant hack land: generate clipping brushes for model triangles */
425                 if( si->clipModel || (spawnFlags & 2) ) /* 2nd bit */
426                 {
427                         vec3_t          points[ 4 ], backs[ 3 ];
428                         vec4_t          plane, reverse, pa, pb, pc;
429                         
430                         
431                         /* temp hack */
432                         if( !si->clipModel && !(si->compileFlags & C_SOLID) )
433                                 continue;
434                         
435                         /* walk triangle list */
436                         for( i = 0; i < ds->numIndexes; i += 3 )
437                         {
438                                 /* overflow hack */
439                                 AUTOEXPAND_BY_REALLOC(mapplanes, (nummapplanes+64) << 1, allocatedmapplanes, 1024);
440                                 
441                                 /* make points and back points */
442                                 for( j = 0; j < 3; j++ )
443                                 {
444                                         /* get vertex */
445                                         dv = &ds->verts[ ds->indexes[ i + j ] ];
446                                         
447                                         /* copy xyz */
448                                         VectorCopy( dv->xyz, points[ j ] );
449                                         VectorCopy( dv->xyz, backs[ j ] );
450                                         
451                                         /* find nearest axial to normal and push back points opposite */
452                                         /* note: this doesn't work as well as simply using the plane of the triangle, below */
453                                         for( k = 0; k < 3; k++ )
454                                         {
455                                                 if( fabs( dv->normal[ k ] ) >= fabs( dv->normal[ (k + 1) % 3 ] ) &&
456                                                         fabs( dv->normal[ k ] ) >= fabs( dv->normal[ (k + 2) % 3 ] ) )
457                                                 {
458                                                         backs[ j ][ k ] += dv->normal[ k ] < 0.0f ? 64.0f : -64.0f;
459                                                         break;
460                                                 }
461                                         }
462                                 }
463
464                                 VectorCopy( points[0], points[3] ); // for cyclic usage
465                                 
466                                 /* make plane for triangle */
467                                 // div0: add some extra spawnflags:
468                                 //   0: snap normals to axial planes for extrusion
469                                 //   8: extrude with the original normals
470                                 //  16: extrude only with up/down normals (ideal for terrain)
471                                 //  24: extrude by distance zero (may need engine changes)
472                                 if( PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] ) )
473                                 {
474                                         vec3_t bestNormal;
475                                         float backPlaneDistance = 2;
476
477                                         if(spawnFlags & 8) // use a DOWN normal
478                                         {
479                                                 if(spawnFlags & 16)
480                                                 {
481                                                         // 24: normal as is, and zero width (broken)
482                                                         VectorCopy(plane, bestNormal);
483                                                 }
484                                                 else
485                                                 {
486                                                         // 8: normal as is
487                                                         VectorCopy(plane, bestNormal);
488                                                 }
489                                         }
490                                         else
491                                         {
492                                                 if(spawnFlags & 16)
493                                                 {
494                                                         // 16: UP/DOWN normal
495                                                         VectorSet(bestNormal, 0, 0, (plane[2] >= 0 ? 1 : -1));
496                                                 }
497                                                 else
498                                                 {
499                                                         // 0: axial normal
500                                                         if(fabs(plane[0]) > fabs(plane[1])) // x>y
501                                                                 if(fabs(plane[1]) > fabs(plane[2])) // x>y, y>z
502                                                                         VectorSet(bestNormal, (plane[0] >= 0 ? 1 : -1), 0, 0);
503                                                                 else // x>y, z>=y
504                                                                         if(fabs(plane[0]) > fabs(plane[2])) // x>z, z>=y
505                                                                                 VectorSet(bestNormal, (plane[0] >= 0 ? 1 : -1), 0, 0);
506                                                                         else // z>=x, x>y
507                                                                                 VectorSet(bestNormal, 0, 0, (plane[2] >= 0 ? 1 : -1));
508                                                         else // y>=x
509                                                                 if(fabs(plane[1]) > fabs(plane[2])) // y>z, y>=x
510                                                                         VectorSet(bestNormal, 0, (plane[1] >= 0 ? 1 : -1), 0);
511                                                                 else // z>=y, y>=x
512                                                                         VectorSet(bestNormal, 0, 0, (plane[2] >= 0 ? 1 : -1));
513                                                 }
514                                         }
515
516                                         /* build a brush */
517                                         buildBrush = AllocBrush( 48 );
518                                         buildBrush->entityNum = mapEntityNum;
519                                         buildBrush->original = buildBrush;
520                                         buildBrush->contentShader = si;
521                                         buildBrush->compileFlags = si->compileFlags;
522                                         buildBrush->contentFlags = si->contentFlags;
523                                         normalEpsilon_save = normalEpsilon;
524                                         distanceEpsilon_save = distanceEpsilon;
525                                         if(si->compileFlags & C_STRUCTURAL) // allow forced structural brushes here
526                                         {
527                                                 buildBrush->detail = qfalse;
528
529                                                 // only allow EXACT matches when snapping for these (this is mostly for caulk brushes inside a model)
530                                                 if(normalEpsilon > 0)
531                                                         normalEpsilon = 0;
532                                                 if(distanceEpsilon > 0)
533                                                         distanceEpsilon = 0;
534                                         }
535                                         else
536                                                 buildBrush->detail = qtrue;
537
538                                         /* regenerate back points */
539                                         for( j = 0; j < 3; j++ )
540                                         {
541                                                 /* get vertex */
542                                                 dv = &ds->verts[ ds->indexes[ i + j ] ];
543
544                                                 // shift by some units
545                                                 VectorMA(dv->xyz, -64.0f, bestNormal, backs[j]); // 64 prevents roundoff errors a bit
546                                         }
547
548                                         /* make back plane */
549                                         VectorScale( plane, -1.0f, reverse );
550                                         reverse[ 3 ] = -plane[ 3 ];
551                                         if((spawnFlags & 24) != 24)
552                                                 reverse[3] += DotProduct(bestNormal, plane) * backPlaneDistance;
553                                         // that's at least sqrt(1/3) backPlaneDistance, unless in DOWN mode; in DOWN mode, we are screwed anyway if we encounter a plane that's perpendicular to the xy plane)
554
555                                         if( PlaneFromPoints( pa, points[ 2 ], points[ 1 ], backs[ 1 ] ) &&
556                                                         PlaneFromPoints( pb, points[ 1 ], points[ 0 ], backs[ 0 ] ) &&
557                                                         PlaneFromPoints( pc, points[ 0 ], points[ 2 ], backs[ 2 ] ) )
558                                         {
559                                                 /* set up brush sides */
560                                                 buildBrush->numsides = 5;
561                                                 buildBrush->sides[ 0 ].shaderInfo = si;
562                                                 for( j = 1; j < buildBrush->numsides; j++ )
563                                                         buildBrush->sides[ j ].shaderInfo = NULL; // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
564
565                                                 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
566                                                 buildBrush->sides[ 1 ].planenum = FindFloatPlane( pa, pa[ 3 ], 2, &points[ 1 ] ); // pa contains points[1] and points[2]
567                                                 buildBrush->sides[ 2 ].planenum = FindFloatPlane( pb, pb[ 3 ], 2, &points[ 0 ] ); // pb contains points[0] and points[1]
568                                                 buildBrush->sides[ 3 ].planenum = FindFloatPlane( pc, pc[ 3 ], 2, &points[ 2 ] ); // pc contains points[2] and points[0] (copied to points[3]
569                                                 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 3, backs );
570                                         }
571                                         else
572                                         {
573                                                 free(buildBrush);
574                                                 continue;
575                                         }
576
577                                         normalEpsilon = normalEpsilon_save;
578                                         distanceEpsilon = distanceEpsilon_save;
579
580                                         /* add to entity */
581                                         if( CreateBrushWindings( buildBrush ) )
582                                         {
583                                                 AddBrushBevels();
584                                                 //%     EmitBrushes( buildBrush, NULL, NULL );
585                                                 buildBrush->next = entities[ mapEntityNum ].brushes;
586                                                 entities[ mapEntityNum ].brushes = buildBrush;
587                                                 entities[ mapEntityNum ].numBrushes++;
588                                         }
589                                         else
590                                                 free( buildBrush );
591                                 }
592                         }
593                 }
594         }
595 }
596
597
598
599 /*
600 AddTriangleModels()
601 adds misc_model surfaces to the bsp
602 */
603
604 void AddTriangleModels( entity_t *e )
605 {
606         int                             num, frame, castShadows, recvShadows, spawnFlags;
607         entity_t                *e2;
608         const char              *targetName;
609         const char              *target, *model, *value;
610         char                    shader[ MAX_QPATH ];
611         shaderInfo_t    *celShader;
612         float                   temp, baseLightmapScale, lightmapScale;
613         float                   shadeAngle;
614         int                             lightmapSampleSize;
615         vec3_t                  origin, scale, angles;
616         m4x4_t                  transform;
617         epair_t                 *ep;
618         remap_t                 *remap, *remap2;
619         char                    *split;
620         
621         
622         /* note it */
623         Sys_FPrintf( SYS_VRB, "--- AddTriangleModels ---\n" );
624         
625         /* get current brush entity targetname */
626         if( e == entities )
627                 targetName = "";
628         else
629         {
630                 targetName = ValueForKey( e, "targetname" );
631         
632                 /* misc_model entities target non-worldspawn brush model entities */
633                 if( targetName[ 0 ] == '\0' )
634                         return;
635         }
636         
637         /* get lightmap scale */
638         /* vortex: added _ls key (short name of lightmapscale) */
639         baseLightmapScale = 0.0f;
640         if( strcmp( "", ValueForKey( e, "lightmapscale" ) ) ||
641                 strcmp( "", ValueForKey( e, "_lightmapscale" ) ) || 
642                 strcmp( "", ValueForKey( e, "_ls" ) ) )
643         {
644                 baseLightmapScale = FloatForKey( e, "lightmapscale" );
645                 if( baseLightmapScale <= 0.0f )
646                         baseLightmapScale = FloatForKey( e, "_lightmapscale" );
647                 if( baseLightmapScale <= 0.0f )
648                         baseLightmapScale = FloatForKey( e, "_ls" );
649                 if( baseLightmapScale < 0.0f )
650                         baseLightmapScale = 0.0f;
651                 if( baseLightmapScale > 0.0f )
652                         Sys_Printf( "World Entity has lightmap scale of %.4f\n", baseLightmapScale );
653         }
654         
655         
656         /* walk the entity list */
657         for( num = 1; num < numEntities; num++ )
658         {
659                 /* get e2 */
660                 e2 = &entities[ num ];
661                 
662                 /* convert misc_models into raw geometry */
663                 if( Q_stricmp( "misc_model", ValueForKey( e2, "classname" ) ) )
664                         continue;
665
666                 /* ydnar: added support for md3 models on non-worldspawn models */
667                 target = ValueForKey( e2, "target" );
668                 if( strcmp( target, targetName ) )
669                         continue;
670                 
671                 /* get model name */
672                 model = ValueForKey( e2, "model" );
673                 if( model[ 0 ] == '\0' )
674                 {
675                         Sys_Printf( "WARNING: misc_model at %i %i %i without a model key\n",
676                                 (int) origin[ 0 ], (int) origin[ 1 ], (int) origin[ 2 ] );
677                         continue;
678                 }
679                 
680                 /* get model frame */
681                 frame = IntForKey( e2, "_frame" );
682                 
683                 /* worldspawn (and func_groups) default to cast/recv shadows in worldspawn group */
684                 if( e == entities )
685                 {
686                         castShadows = WORLDSPAWN_CAST_SHADOWS;
687                         recvShadows = WORLDSPAWN_RECV_SHADOWS;
688                 }
689                 
690                 /* other entities don't cast any shadows, but recv worldspawn shadows */
691                 else
692                 {
693                         castShadows = ENTITY_CAST_SHADOWS;
694                         recvShadows = ENTITY_RECV_SHADOWS;
695                 }
696                 
697                 /* get explicit shadow flags */
698                 GetEntityShadowFlags( e2, e, &castShadows, &recvShadows );
699                 
700                 /* get spawnflags */
701                 spawnFlags = IntForKey( e2, "spawnflags" );
702                 
703                 /* get origin */
704                 GetVectorForKey( e2, "origin", origin );
705                 VectorSubtract( origin, e->origin, origin );    /* offset by parent */
706                 
707                 /* get scale */
708                 scale[ 0 ] = scale[ 1 ] = scale[ 2 ] = 1.0f;
709                 temp = FloatForKey( e2, "modelscale" );
710                 if( temp != 0.0f )
711                         scale[ 0 ] = scale[ 1 ] = scale[ 2 ] = temp;
712                 value = ValueForKey( e2, "modelscale_vec" );
713                 if( value[ 0 ] != '\0' )
714                         sscanf( value, "%f %f %f", &scale[ 0 ], &scale[ 1 ], &scale[ 2 ] );
715                 
716                 /* get "angle" (yaw) or "angles" (pitch yaw roll) */
717                 angles[ 0 ] = angles[ 1 ] = angles[ 2 ] = 0.0f;
718                 angles[ 2 ] = FloatForKey( e2, "angle" );
719                 value = ValueForKey( e2, "angles" );
720                 if( value[ 0 ] != '\0' )
721                         sscanf( value, "%f %f %f", &angles[ 1 ], &angles[ 2 ], &angles[ 0 ] );
722                 
723                 /* set transform matrix (thanks spog) */
724                 m4x4_identity( transform );
725                 m4x4_pivoted_transform_by_vec3( transform, origin, angles, eXYZ, scale, vec3_origin );
726                 
727                 /* get shader remappings */
728                 remap = NULL;
729                 for( ep = e2->epairs; ep != NULL; ep = ep->next )
730                 {
731                         /* look for keys prefixed with "_remap" */
732                         if( ep->key != NULL && ep->value != NULL &&
733                                 ep->key[ 0 ] != '\0' && ep->value[ 0 ] != '\0' &&
734                                 !Q_strncasecmp( ep->key, "_remap", 6 ) )
735                         {
736                                 /* create new remapping */
737                                 remap2 = remap;
738                                 remap = safe_malloc( sizeof( *remap ) );
739                                 remap->next = remap2;
740                                 strcpy( remap->from, ep->value );
741                                 
742                                 /* split the string */
743                                 split = strchr( remap->from, ';' );
744                                 if( split == NULL )
745                                 {
746                                         Sys_Printf( "WARNING: Shader _remap key found in misc_model without a ; character\n" );
747                                         free( remap );
748                                         remap = remap2;
749                                         continue;
750                                 }
751                                 
752                                 /* store the split */
753                                 *split = '\0';
754                                 strcpy( remap->to, (split + 1) );
755                                 
756                                 /* note it */
757                                 //%     Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", remap->from, remap->to );
758                         }
759                 }
760                 
761                 /* ydnar: cel shader support */
762                 value = ValueForKey( e2, "_celshader" );
763                 if( value[ 0 ] == '\0' )
764                         value = ValueForKey( &entities[ 0 ], "_celshader" );
765                 if( value[ 0 ] != '\0' )
766                 {
767                         sprintf( shader, "textures/%s", value );
768                         celShader = ShaderInfoForShader( shader );
769                 }
770                 else
771                         celShader = *globalCelShader ? ShaderInfoForShader(globalCelShader) : NULL;
772
773                 /* jal : entity based _samplesize */
774                 lightmapSampleSize = 0;
775                 if ( strcmp( "", ValueForKey( e2, "_lightmapsamplesize" ) ) )
776                         lightmapSampleSize = IntForKey( e2, "_lightmapsamplesize" );
777                 else if ( strcmp( "", ValueForKey( e2, "_samplesize" ) ) )
778                         lightmapSampleSize = IntForKey( e2, "_samplesize" );
779
780                 if( lightmapSampleSize < 0 )
781                         lightmapSampleSize = 0;
782
783                 if( lightmapSampleSize > 0.0f )
784                         Sys_Printf( "misc_model has lightmap sample size of %.d\n", lightmapSampleSize );
785
786                 /* get lightmap scale */
787                 /* vortex: added _ls key (short name of lightmapscale) */
788                 lightmapScale = 0.0f;
789                 if( strcmp( "", ValueForKey( e2, "lightmapscale" ) ) ||
790                         strcmp( "", ValueForKey( e2, "_lightmapscale" ) ) || 
791                         strcmp( "", ValueForKey( e2, "_ls" ) ) )
792                 {
793                         lightmapScale = FloatForKey( e2, "lightmapscale" );
794                         if( lightmapScale <= 0.0f )
795                                 lightmapScale = FloatForKey( e2, "_lightmapscale" );
796                         if( lightmapScale <= 0.0f )
797                                 lightmapScale = FloatForKey( e2, "_ls" );
798                         if( lightmapScale < 0.0f )
799                                 lightmapScale = 0.0f;
800                         if( lightmapScale > 0.0f )
801                                 Sys_Printf( "misc_model has lightmap scale of %.4f\n", lightmapScale );
802                 }
803
804                 /* jal : entity based _shadeangle */
805                 shadeAngle = 0.0f;
806                 if ( strcmp( "", ValueForKey( e2, "_shadeangle" ) ) )
807                         shadeAngle = FloatForKey( e2, "_shadeangle" );
808                 /* vortex' aliases */
809                 else if ( strcmp( "", ValueForKey( mapEnt, "_smoothnormals" ) ) )
810                         shadeAngle = FloatForKey( mapEnt, "_smoothnormals" );
811                 else if ( strcmp( "", ValueForKey( mapEnt, "_sn" ) ) )
812                         shadeAngle = FloatForKey( mapEnt, "_sn" );
813                 else if ( strcmp( "", ValueForKey( mapEnt, "_smooth" ) ) )
814                         shadeAngle = FloatForKey( mapEnt, "_smooth" );
815
816                 if( shadeAngle < 0.0f )
817                         shadeAngle = 0.0f;
818
819                 if( shadeAngle > 0.0f )
820                         Sys_Printf( "misc_model has shading angle of %.4f\n", shadeAngle );
821                 
822                 /* insert the model */
823                 InsertModel( (char*) model, frame, transform, remap, celShader, mapEntityNum, castShadows, recvShadows, spawnFlags, lightmapScale, lightmapSampleSize, shadeAngle );
824                 
825                 /* free shader remappings */
826                 while( remap != NULL )
827                 {
828                         remap2 = remap->next;
829                         free( remap );
830                         remap = remap2;
831                 }
832         }
833 }