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