]> git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/model.c
Merge commit '39f598c5f44010cc32f1b445b12cb088a72bbc50' into master-merge
[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         if ( str == NULL ) {
48                 return;
49         }
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_FPrintf( SYS_WRN, "WARNING: %s\n", str );
62                 break;
63
64         case PICO_ERROR:
65                 Sys_FPrintf( SYS_ERR, "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( const char *name, byte **buffer, int *bufSize ){
82         *bufSize = vfsLoadFile( name, (void**) buffer, 0 );
83 }
84
85
86
87 /*
88    FindModel() - ydnar
89    finds an existing picoModel and returns a pointer to the picoModel_t struct or NULL if not found
90  */
91
92 picoModel_t *FindModel( const char *name, int frame ){
93         int i;
94
95
96         /* init */
97         if ( numPicoModels <= 0 ) {
98                 memset( picoModels, 0, sizeof( picoModels ) );
99         }
100
101         /* dummy check */
102         if ( name == NULL || name[ 0 ] == '\0' ) {
103                 return NULL;
104         }
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
116         /* no matching picoModel found */
117         return NULL;
118 }
119
120
121
122 /*
123    LoadModel() - ydnar
124    loads a picoModel and returns a pointer to the picoModel_t struct or NULL if not found
125  */
126
127 picoModel_t *LoadModel( const char *name, int frame ){
128         int i;
129         picoModel_t     *model, **pm;
130
131
132         /* init */
133         if ( numPicoModels <= 0 ) {
134                 memset( picoModels, 0, sizeof( picoModels ) );
135         }
136
137         /* dummy check */
138         if ( name == NULL || name[ 0 ] == '\0' ) {
139                 return NULL;
140         }
141
142         /* try to find existing picoModel */
143         model = FindModel( name, frame );
144         if ( model != NULL ) {
145                 return model;
146         }
147
148         /* none found, so find first non-null picoModel */
149         pm = NULL;
150         for ( i = 0; i < MAX_MODELS; i++ )
151         {
152                 if ( picoModels[ i ] == NULL ) {
153                         pm = &picoModels[ i ];
154                         break;
155                 }
156         }
157
158         /* too many picoModels? */
159         if ( pm == NULL ) {
160                 Error( "MAX_MODELS (%d) exceeded, there are too many model files referenced by the map.", MAX_MODELS );
161         }
162
163         /* attempt to parse model */
164         *pm = PicoLoadModel( name, frame );
165
166         /* if loading failed, make a bogus model to silence the rest of the warnings */
167         if ( *pm == NULL ) {
168                 /* allocate a new model */
169                 *pm = PicoNewModel();
170                 if ( *pm == NULL ) {
171                         return NULL;
172                 }
173
174                 /* set data */
175                 PicoSetModelName( *pm, name );
176                 PicoSetModelFrameNum( *pm, frame );
177         }
178
179         /* debug code */
180         #if 0
181         {
182                 int numSurfaces, numVertexes;
183                 picoSurface_t   *ps;
184
185
186                 Sys_Printf( "Model %s\n", name );
187                 numSurfaces = PicoGetModelNumSurfaces( *pm );
188                 for ( i = 0; i < numSurfaces; i++ )
189                 {
190                         ps = PicoGetModelSurface( *pm, i );
191                         numVertexes = PicoGetSurfaceNumVertexes( ps );
192                         Sys_Printf( "Surface %d has %d vertexes\n", i, numVertexes );
193                 }
194         }
195         #endif
196
197         /* set count */
198         if ( *pm != NULL ) {
199                 numPicoModels++;
200         }
201
202         /* return the picoModel */
203         return *pm;
204 }
205
206
207
208 /*
209    InsertModel() - ydnar
210    adds a picomodel into the bsp
211  */
212
213 void InsertModel( const 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, float clipDepth ){
214         int i, j, s, k, numSurfaces;
215         m4x4_t identity, nTransform;
216         picoModel_t         *model;
217         picoShader_t        *shader;
218         picoSurface_t       *surface;
219         shaderInfo_t        *si;
220         mapDrawSurface_t    *ds;
221         bspDrawVert_t       *dv;
222         char                *picoShaderName;
223         char shaderName[ MAX_QPATH ];
224         picoVec_t           *xyz, *normal, *st;
225         byte                *color;
226         picoIndex_t         *indexes;
227         remap_t             *rm, *glob;
228         skinfile_t          *sf, *sf2;
229         char skinfilename[ MAX_QPATH ];
230         char                *skinfilecontent;
231         int skinfilesize;
232         char                *skinfileptr, *skinfilenextptr;
233         int ok=0, notok=0, spf = ( spawnFlags & 8088 );
234         float limDepth=0;
235
236
237         if ( clipDepth < 0 ){
238                 limDepth = -clipDepth;
239                 clipDepth = 2.0;
240         }
241
242
243         /* get model */
244         model = LoadModel( name, frame );
245         if ( model == NULL ) {
246                 return;
247         }
248
249         /* load skin file */
250         snprintf( skinfilename, sizeof( skinfilename ), "%s_%d.skin", name, skin );
251         skinfilename[sizeof( skinfilename ) - 1] = 0;
252         skinfilesize = vfsLoadFile( skinfilename, (void**) &skinfilecontent, 0 );
253         if ( skinfilesize < 0 && skin != 0 ) {
254                 /* fallback to skin 0 if invalid */
255                 snprintf( skinfilename, sizeof( skinfilename ), "%s_0.skin", name );
256                 skinfilename[sizeof( skinfilename ) - 1] = 0;
257                 skinfilesize = vfsLoadFile( skinfilename, (void**) &skinfilecontent, 0 );
258                 if ( skinfilesize >= 0 ) {
259                         Sys_Printf( "Skin %d of %s does not exist, using 0 instead\n", skin, name );
260                 }
261         }
262         sf = NULL;
263         if ( skinfilesize >= 0 ) {
264                 Sys_Printf( "Using skin %d of %s\n", skin, name );
265                 int pos;
266                 for ( skinfileptr = skinfilecontent; *skinfileptr; skinfileptr = skinfilenextptr )
267                 {
268                         // for fscanf
269                         char format[64];
270
271                         skinfilenextptr = strchr( skinfileptr, '\r' );
272                         if ( skinfilenextptr ) {
273                                 *skinfilenextptr++ = 0;
274                         }
275                         else
276                         {
277                                 skinfilenextptr = strchr( skinfileptr, '\n' );
278                                 if ( skinfilenextptr ) {
279                                         *skinfilenextptr++ = 0;
280                                 }
281                                 else{
282                                         skinfilenextptr = skinfileptr + strlen( skinfileptr );
283                                 }
284                         }
285
286                         /* create new item */
287                         sf2 = sf;
288                         sf = safe_malloc( sizeof( *sf ) );
289                         sf->next = sf2;
290
291                         sprintf( format, "replace %%%ds %%%ds", (int)sizeof( sf->name ) - 1, (int)sizeof( sf->to ) - 1 );
292                         if ( sscanf( skinfileptr, format, sf->name, sf->to ) == 2 ) {
293                                 continue;
294                         }
295                         sprintf( format, " %%%d[^,  ] ,%%%ds", (int)sizeof( sf->name ) - 1, (int)sizeof( sf->to ) - 1 );
296                         if ( ( pos = sscanf( skinfileptr, format, sf->name, sf->to ) ) == 2 ) {
297                                 continue;
298                         }
299
300                         /* invalid input line -> discard sf struct */
301                         Sys_Printf( "Discarding skin directive in %s: %s\n", skinfilename, skinfileptr );
302                         free( sf );
303                         sf = sf2;
304                 }
305                 free( skinfilecontent );
306         }
307
308         /* handle null matrix */
309         if ( transform == NULL ) {
310                 m4x4_identity( identity );
311                 transform = identity;
312         }
313
314         /* hack: Stable-1_2 and trunk have differing row/column major matrix order
315            this transpose is necessary with Stable-1_2
316            uncomment the following line with old m4x4_t (non 1.3/spog_branch) code */
317         //%     m4x4_transpose( transform );
318
319         /* create transform matrix for normals */
320         memcpy( nTransform, transform, sizeof( m4x4_t ) );
321         if ( m4x4_invert( nTransform ) ) {
322                 Sys_FPrintf( SYS_VRB, "WARNING: Can't invert model transform matrix, using transpose instead\n" );
323         }
324         m4x4_transpose( nTransform );
325
326         /* fix bogus lightmap scale */
327         if ( lightmapScale <= 0.0f ) {
328                 lightmapScale = 1.0f;
329         }
330
331         /* fix bogus shade angle */
332         if ( shadeAngle <= 0.0f ) {
333                 shadeAngle = 0.0f;
334         }
335
336         /* each surface on the model will become a new map drawsurface */
337         numSurfaces = PicoGetModelNumSurfaces( model );
338         //%     Sys_FPrintf( SYS_VRB, "Model %s has %d surfaces\n", name, numSurfaces );
339         for ( s = 0; s < numSurfaces; s++ )
340         {
341                 /* get surface */
342                 surface = PicoGetModelSurface( model, s );
343                 if ( surface == NULL ) {
344                         continue;
345                 }
346
347                 /* only handle triangle surfaces initially (fixme: support patches) */
348                 if ( PicoGetSurfaceType( surface ) != PICO_TRIANGLES ) {
349                         continue;
350                 }
351
352                 /* get shader name */
353                 shader = PicoGetSurfaceShader( surface );
354                 if ( shader == NULL ) {
355                         picoShaderName = "";
356                 }
357                 else{
358                         picoShaderName = PicoGetShaderName( shader );
359                 }
360
361                 /* handle .skin file */
362                 if ( sf ) {
363                         picoShaderName = NULL;
364                         for ( sf2 = sf; sf2 != NULL; sf2 = sf2->next )
365                         {
366                                 if ( !Q_stricmp( surface->name, sf2->name ) ) {
367                                         Sys_FPrintf( SYS_VRB, "Skin file: mapping %s to %s\n", surface->name, sf2->to );
368                                         picoShaderName = sf2->to;
369                                         break;
370                                 }
371                         }
372                         if ( !picoShaderName ) {
373                                 Sys_FPrintf( SYS_VRB, "Skin file: not mapping %s\n", surface->name );
374                                 continue;
375                         }
376                 }
377
378                 /* handle shader remapping */
379                 glob = NULL;
380                 for ( rm = remap; rm != NULL; rm = rm->next )
381                 {
382                         if ( rm->from[ 0 ] == '*' && rm->from[ 1 ] == '\0' ) {
383                                 glob = rm;
384                         }
385                         else if ( !Q_stricmp( picoShaderName, rm->from ) ) {
386                                 Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", picoShaderName, rm->to );
387                                 picoShaderName = rm->to;
388                                 glob = NULL;
389                                 break;
390                         }
391                 }
392
393                 if ( glob != NULL ) {
394                         Sys_FPrintf( SYS_VRB, "Globbing %s to %s\n", picoShaderName, glob->to );
395                         picoShaderName = glob->to;
396                 }
397
398                 /* shader renaming for sof2 */
399                 if ( renameModelShaders ) {
400                         strcpy( shaderName, picoShaderName );
401                         StripExtension( shaderName );
402                         if ( spawnFlags & 1 ) {
403                                 strcat( shaderName, "_RMG_BSP" );
404                         }
405                         else{
406                                 strcat( shaderName, "_BSP" );
407                         }
408                         si = ShaderInfoForShader( shaderName );
409                 }
410                 else{
411                         si = ShaderInfoForShader( picoShaderName );
412                 }
413
414                 /* allocate a surface (ydnar: gs mods) */
415                 ds = AllocDrawSurface( SURFACE_TRIANGLES );
416                 ds->entityNum = eNum;
417                 ds->castShadows = castShadows;
418                 ds->recvShadows = recvShadows;
419
420                 /* set shader */
421                 ds->shaderInfo = si;
422
423                 /* force to meta? */
424                 if ( ( si != NULL && si->forceMeta ) || ( spawnFlags & 4 ) ) { /* 3rd bit */
425                         ds->type = SURFACE_FORCED_META;
426                 }
427
428                 /* fix the surface's normals (jal: conditioned by shader info) */
429                 if ( !( spawnFlags & 64 ) && ( shadeAngle == 0.0f || ds->type != SURFACE_FORCED_META ) ) {
430                         PicoFixSurfaceNormals( surface );
431                 }
432
433                 /* set sample size */
434                 if ( lightmapSampleSize > 0.0f ) {
435                         ds->sampleSize = lightmapSampleSize;
436                 }
437
438                 /* set lightmap scale */
439                 if ( lightmapScale > 0.0f ) {
440                         ds->lightmapScale = lightmapScale;
441                 }
442
443                 /* set shading angle */
444                 if ( shadeAngle > 0.0f ) {
445                         ds->shadeAngleDegrees = shadeAngle;
446                 }
447
448                 /* set particulars */
449                 ds->numVerts = PicoGetSurfaceNumVertexes( surface );
450                 ds->verts = safe_malloc0( ds->numVerts * sizeof( ds->verts[ 0 ] ) );
451
452                 ds->numIndexes = PicoGetSurfaceNumIndexes( surface );
453                 ds->indexes = safe_malloc0( ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
454
455                 /* copy vertexes */
456                 for ( i = 0; i < ds->numVerts; i++ )
457                 {
458                         /* get vertex */
459                         dv = &ds->verts[ i ];
460
461                         /* xyz and normal */
462                         xyz = PicoGetSurfaceXYZ( surface, i );
463                         VectorCopy( xyz, dv->xyz );
464                         m4x4_transform_point( transform, dv->xyz );
465
466                         normal = PicoGetSurfaceNormal( surface, i );
467                         VectorCopy( normal, dv->normal );
468                         m4x4_transform_normal( nTransform, dv->normal );
469                         VectorNormalize( dv->normal, dv->normal );
470
471                         /* ydnar: tek-fu celshading support for flat shaded shit */
472                         if ( flat ) {
473                                 dv->st[ 0 ] = si->stFlat[ 0 ];
474                                 dv->st[ 1 ] = si->stFlat[ 1 ];
475                         }
476
477                         /* ydnar: gs mods: added support for explicit shader texcoord generation */
478                         else if ( si->tcGen ) {
479                                 /* project the texture */
480                                 dv->st[ 0 ] = DotProduct( si->vecs[ 0 ], dv->xyz );
481                                 dv->st[ 1 ] = DotProduct( si->vecs[ 1 ], dv->xyz );
482                         }
483
484                         /* normal texture coordinates */
485                         else
486                         {
487                                 st = PicoGetSurfaceST( surface, 0, i );
488                                 dv->st[ 0 ] = st[ 0 ];
489                                 dv->st[ 1 ] = st[ 1 ];
490                         }
491
492                         /* set lightmap/color bits */
493                         color = PicoGetSurfaceColor( surface, 0, i );
494                         for ( j = 0; j < MAX_LIGHTMAPS; j++ )
495                         {
496                                 dv->lightmap[ j ][ 0 ] = 0.0f;
497                                 dv->lightmap[ j ][ 1 ] = 0.0f;
498                                 if ( spawnFlags & 32 ) { // spawnflag 32: model color -> alpha hack
499                                         dv->color[ j ][ 0 ] = 255.0f;
500                                         dv->color[ j ][ 1 ] = 255.0f;
501                                         dv->color[ j ][ 2 ] = 255.0f;
502                                         dv->color[ j ][ 3 ] = RGBTOGRAY( color );
503                                 }
504                                 else
505                                 {
506                                         dv->color[ j ][ 0 ] = color[ 0 ];
507                                         dv->color[ j ][ 1 ] = color[ 1 ];
508                                         dv->color[ j ][ 2 ] = color[ 2 ];
509                                         dv->color[ j ][ 3 ] = color[ 3 ];
510                                 }
511                         }
512                 }
513
514                 /* copy indexes */
515                 indexes = PicoGetSurfaceIndexes( surface, 0 );
516                 for ( i = 0; i < ds->numIndexes; i++ )
517                         ds->indexes[ i ] = indexes[ i ];
518
519                 /* set cel shader */
520                 ds->celShader = celShader;
521
522                 /* ydnar: giant hack land: generate clipping brushes for model triangles */
523                 if ( ( si->clipModel && !( spf ) ) ||   //default CLIPMODEL
524                         ( ( spawnFlags & 8090 ) == 2 ) ||       //default CLIPMODEL
525                         ( spf == 8 ) ||         //EXTRUDE_FACE_NORMALS
526                         ( spf == 16 )   ||      //EXTRUDE_TERRAIN
527                         ( spf == 128 )  ||      //EXTRUDE_VERTEX_NORMALS
528                         ( spf == 256 )  ||      //PYRAMIDAL_CLIP
529                         ( spf == 512 )  ||      //EXTRUDE_DOWNWARDS
530                         ( spf == 1024 ) ||      //EXTRUDE_UPWARDS
531                         ( spf == 4096 ) ||      //default CLIPMODEL + AXIAL_BACKPLANE
532                         ( spf == 264 )  ||      //EXTRUDE_FACE_NORMALS+PYRAMIDAL_CLIP (extrude 45)
533                         ( spf == 2064 ) ||      //EXTRUDE_TERRAIN+MAX_EXTRUDE
534                         ( spf == 4112 ) ||      //EXTRUDE_TERRAIN+AXIAL_BACKPLANE
535                         ( spf == 384 )  ||      //EXTRUDE_VERTEX_NORMALS + PYRAMIDAL_CLIP - vertex normals + don't check for sides, sticking outwards
536                         ( spf == 4352 ) ||      //PYRAMIDAL_CLIP+AXIAL_BACKPLANE
537                         ( spf == 1536 ) ||      //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS
538                         ( spf == 2560 ) ||      //EXTRUDE_DOWNWARDS+MAX_EXTRUDE
539                         ( spf == 4608 ) ||      //EXTRUDE_DOWNWARDS+AXIAL_BACKPLANE
540                         ( spf == 3584 ) ||      //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS+MAX_EXTRUDE
541                         ( spf == 5632 ) ||      //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS+AXIAL_BACKPLANE
542                         ( spf == 3072 ) ||      //EXTRUDE_UPWARDS+MAX_EXTRUDE
543                         ( spf == 5120 ) ){      //EXTRUDE_UPWARDS+AXIAL_BACKPLANE
544                         vec3_t points[ 4 ], backs[ 3 ], cnt, bestNormal, nrm, Vnorm[3], Enorm[3];
545                         vec4_t plane, reverse, p[3];
546                         double normalEpsilon_save;
547                         qboolean snpd;
548                         vec3_t min = { 999999, 999999, 999999 }, max = { -999999, -999999, -999999 };
549                         vec3_t avgDirection = { 0, 0, 0 };
550                         int axis;
551                         #define nonax_clip_dbg 0
552
553                         /* temp hack */
554                         if ( !si->clipModel && !( si->compileFlags & C_SOLID ) ) {
555                                 continue;
556                         }
557
558                         //wont snap these in normal way, or will explode
559                         normalEpsilon_save = normalEpsilon;
560                         //normalEpsilon = 0.000001;
561
562
563                         //MAX_EXTRUDE or EXTRUDE_TERRAIN
564                         if ( ( spawnFlags & 2048 ) || ( spawnFlags & 16 ) ){
565
566                                 for ( i = 0; i < ds->numIndexes; i += 3 )
567                                 {
568                                         for ( j = 0; j < 3; j++ )
569                                         {
570                                                 dv = &ds->verts[ ds->indexes[ i + j ] ];
571                                                 VectorCopy( dv->xyz, points[ j ] );
572                                         }
573                                         if ( PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] ) ){
574                                                 if ( spawnFlags & 16 ) VectorAdd( avgDirection, plane, avgDirection );  //calculate average mesh facing direction
575
576                                                 //get min/max
577                                                 for ( k = 2; k > -1; k-- ){
578                                                         if ( plane[k] > 0 ){
579                                                                 for ( j = 0; j < 3; j++ ){ if ( points[j][k] < min[k] ) min[k] = points[j][k]; }
580                                                         }
581                                                         else if ( plane[k] < 0 ){
582                                                                 for ( j = 0; j < 3; j++ ){ if ( points[j][k] > max[k] ) max[k] = points[j][k]; }
583                                                         }
584                                                         //if EXTRUDE_DOWNWARDS or EXTRUDE_UPWARDS
585                                                         if ( ( spawnFlags & 512 ) || ( spawnFlags & 1024 ) ){
586                                                                 break;
587                                                         }
588                                                 }
589                                         }
590                                 }
591                                 //unify avg direction
592                                 if ( spawnFlags & 16 ){
593                                         for ( j = 0; j < 3; j++ ){
594                                                 if ( fabs(avgDirection[j]) > fabs(avgDirection[(j+1)%3]) ){
595                                                         avgDirection[(j+1)%3] = 0.0;
596                                                         axis = j;
597                                                 }
598                                                 else {
599                                                         avgDirection[j] = 0.0;
600                                                 }
601                                         }
602                                         if ( VectorNormalize( avgDirection, avgDirection ) == 0 ){
603                                                 axis = 2;
604                                                 VectorSet( avgDirection, 0, 0, 1 );
605                                         }
606                                 }
607                         }
608
609                         /* walk triangle list */
610                         for ( i = 0; i < ds->numIndexes; i += 3 )
611                         {
612                                 /* overflow hack */
613                                 AUTOEXPAND_BY_REALLOC( mapplanes, ( nummapplanes + 64 ) << 1, allocatedmapplanes, 1024 );
614
615                                 /* make points */
616                                 for ( j = 0; j < 3; j++ )
617                                 {
618                                         /* get vertex */
619                                         dv = &ds->verts[ ds->indexes[ i + j ] ];
620
621                                         /* copy xyz */
622                                         VectorCopy( dv->xyz, points[ j ] );
623                                 }
624
625                                 /* make plane for triangle */
626                                 if ( PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] ) ) {
627
628                                         /* build a brush */
629                                         buildBrush = AllocBrush( 48 );
630                                         buildBrush->entityNum = mapEntityNum;
631                                         buildBrush->original = buildBrush;
632                                         buildBrush->contentShader = si;
633                                         buildBrush->compileFlags = si->compileFlags;
634                                         buildBrush->contentFlags = si->contentFlags;
635                                         buildBrush->detail = qtrue;
636
637                                         //snap points before using them for further calculations
638                                         //precision suffers a lot, when two of normal values are under .00025 (often no collision, knocking up effect in ioq3)
639                                         //also broken drawsurfs in case of normal brushes
640                                         snpd = qfalse;
641                                         for ( j=0; j<3; j++ )
642                                         {
643                                                 if ( fabs(plane[j]) < 0.00025 && fabs(plane[(j+1)%3]) < 0.00025 && ( plane[j] != 0.0 || plane[(j+1)%3] != 0.0 ) ){
644                                                         VectorAdd( points[ 0 ], points[ 1 ], cnt );
645                                                         VectorAdd( cnt, points[ 2 ], cnt );
646                                                         VectorScale( cnt, 0.3333333333333f, cnt );
647                                                         points[0][(j+2)%3]=points[1][(j+2)%3]=points[2][(j+2)%3]=cnt[(j+2)%3];
648                                                         snpd = qtrue;
649                                                         break;
650                                                 }
651                                         }
652
653                                         //snap pairs of points to prevent bad side planes
654                                         for ( j=0; j<3; j++ )
655                                         {
656                                                 VectorSubtract( points[j], points[(j+1)%3], nrm );
657                                                 VectorNormalize( nrm, nrm );
658                                                 for ( k=0; k<3; k++ )
659                                                 {
660                                                         if ( nrm[k] != 0.0 && fabs(nrm[k]) < 0.00025 ){
661                                                                 //Sys_Printf( "b4(%6.6f %6.6f %6.6f)(%6.6f %6.6f %6.6f)\n", points[j][0], points[j][1], points[j][2], points[(j+1)%3][0], points[(j+1)%3][1], points[(j+1)%3][2] );
662                                                                 points[j][k]=points[(j+1)%3][k] = ( points[j][k] + points[(j+1)%3][k] ) / 2.0;
663                                                                 //Sys_Printf( "sn(%6.6f %6.6f %6.6f)(%6.6f %6.6f %6.6f)\n", points[j][0], points[j][1], points[j][2], points[(j+1)%3][0], points[(j+1)%3][1], points[(j+1)%3][2] );
664                                                                 snpd = qtrue;
665                                                         }
666                                                 }
667                                         }
668
669                                         if ( snpd ) {
670                                                 PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] );
671                                                 snpd = qfalse;
672                                         }
673
674                                         //vector-is-close-to-be-on-axis check again, happens after previous code sometimes
675                                         for ( j=0; j<3; j++ )
676                                         {
677                                                 if ( fabs(plane[j]) < 0.00025 && fabs(plane[(j+1)%3]) < 0.00025 && ( plane[j] != 0.0 || plane[(j+1)%3] != 0.0 ) ){
678                                                         VectorAdd( points[ 0 ], points[ 1 ], cnt );
679                                                         VectorAdd( cnt, points[ 2 ], cnt );
680                                                         VectorScale( cnt, 0.3333333333333f, cnt );
681                                                         points[0][(j+2)%3]=points[1][(j+2)%3]=points[2][(j+2)%3]=cnt[(j+2)%3];
682                                                         PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] );
683                                                         break;
684                                                 }
685                                         }
686
687                                         //snap single snappable normal components
688                                         for ( j=0; j<3; j++ )
689                                         {
690                                                 if ( plane[j] != 0.0 && fabs(plane[j]) < 0.00005 ){
691                                                         plane[j]=0.0;
692                                                         snpd = qtrue;
693                                                 }
694                                         }
695
696                                         //adjust plane dist
697                                         if ( snpd ) {
698                                                 VectorAdd( points[ 0 ], points[ 1 ], cnt );
699                                                 VectorAdd( cnt, points[ 2 ], cnt );
700                                                 VectorScale( cnt, 0.3333333333333f, cnt );
701                                                 VectorNormalize( plane, plane );
702                                                 plane[3] = DotProduct( plane, cnt );
703
704                                                 //project points to resulting plane to keep intersections precision
705                                                 for ( j=0; j<3; j++ )
706                                                 {
707                                                         //Sys_Printf( "b4 %i (%6.7f %6.7f %6.7f)\n", j, points[j][0], points[j][1], points[j][2] );
708                                                         VectorMA( points[j], plane[3] - DotProduct( plane, points[j]), plane, points[j] );
709                                                         //Sys_Printf( "sn %i (%6.7f %6.7f %6.7f)\n", j, points[j][0], points[j][1], points[j][2] );
710                                                 }
711                                                 //Sys_Printf( "sn pln (%6.7f %6.7f %6.7f %6.7f)\n", plane[0], plane[1], plane[2], plane[3] );
712                                                 //PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] );
713                                                 //Sys_Printf( "pts pln (%6.7f %6.7f %6.7f %6.7f)\n", plane[0], plane[1], plane[2], plane[3] );
714                                         }
715
716
717                                         if ( spf == 4352 ){     //PYRAMIDAL_CLIP+AXIAL_BACKPLANE
718
719                                                 for ( j=0; j<3; j++ )
720                                                 {
721                                                         if ( fabs(plane[j]) < 0.05 && fabs(plane[(j+1)%3]) < 0.05 ){ //no way, close to lay on two axises
722                                                                 goto default_CLIPMODEL;
723                                                         }
724                                                 }
725
726                                                 // best axial normal
727                                                 VectorCopy( plane, bestNormal );
728                                                 for ( j = 0; j < 3; j++ ){
729                                                         if ( fabs(bestNormal[j]) > fabs(bestNormal[(j+1)%3]) ){
730                                                                 bestNormal[(j+1)%3] = 0.0;
731                                                                 axis = j;
732                                                         }
733                                                         else {
734                                                                 bestNormal[j] = 0.0;
735                                                         }
736                                                 }
737                                                 VectorNormalize( bestNormal, bestNormal );
738
739
740                                                 float bestdist, currdist, bestangle, currangle, mindist = 999999;
741
742                                                 for ( j = 0; j < 3; j++ ){//planes
743                                                         bestdist = 999999;
744                                                         bestangle = 1;
745                                                         for ( k = 0; k < 3; k++ ){//axises
746                                                                 VectorSubtract( points[ (j+1)%3 ], points[ j ], nrm );
747                                                                 if ( k == axis ){
748                                                                         CrossProduct( bestNormal, nrm, reverse );
749                                                                 }
750                                                                 else{
751                                                                         VectorClear( Vnorm[0] );
752                                                                         if ( (k+1)%3 == axis ){
753                                                                                 if ( nrm[ (k+2)%3 ] == 0 ) continue;
754                                                                                 Vnorm[0][ (k+2)%3 ] = nrm[ (k+2)%3 ];
755                                                                         }
756                                                                         else{
757                                                                                 if ( nrm[ (k+1)%3 ] == 0 ) continue;
758                                                                                 Vnorm[0][ (k+1)%3 ] = nrm[ (k+1)%3 ];
759                                                                         }
760                                                                         CrossProduct( bestNormal, Vnorm[0], Enorm[0] );
761                                                                         CrossProduct( Enorm[0], nrm, reverse );
762                                                                 }
763                                                                 VectorNormalize( reverse, reverse );
764                                                                 reverse[3] = DotProduct( points[ j ], reverse );
765                                                                 //check facing, thickness
766                                                                 currdist = reverse[3] - DotProduct( reverse, points[ (j+2)%3 ] );
767                                                                 currangle = DotProduct( reverse, plane );
768                                                                 if ( ( ( currdist > 0.1 ) && ( currdist < bestdist ) && ( currangle < 0 ) ) ||
769                                                                         ( ( currangle >= 0 ) && ( currangle <= bestangle ) ) ){
770                                                                         bestangle = currangle;
771                                                                         if ( currangle < 0 ) bestdist = currdist;
772                                                                         VectorCopy( reverse, p[j] );
773                                                                         p[j][3] = reverse[3];
774                                                                 }
775                                                         }
776                                                         //if ( bestdist == 999999 && bestangle == 1 ) Sys_Printf("default_CLIPMODEL\n");
777                                                         if ( bestdist == 999999 && bestangle == 1 ) goto default_CLIPMODEL;
778                                                         if ( bestdist < mindist ) mindist = bestdist;
779                                                 }
780                                                 if ( (limDepth != 0.0) && (mindist > limDepth) ) goto default_CLIPMODEL;
781
782
783 #if nonax_clip_dbg
784                                                 for ( j = 0; j < 3; j++ )
785                                                 {
786                                                         for ( k = 0; k < 3; k++ )
787                                                         {
788                                                                 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
789                                                                         Sys_Printf( "nonax nrm %6.17f %6.17f %6.17f\n", p[j][0], p[j][1], p[j][2] );
790                                                                 }
791                                                         }
792                                                 }
793 #endif
794                                                 /* set up brush sides */
795                                                 buildBrush->numsides = 4;
796                                                 buildBrush->sides[ 0 ].shaderInfo = si;
797                                                 for ( j = 1; j < buildBrush->numsides; j++ ) {
798                                                         if ( debugClip ) {
799                                                                 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
800                                                                 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
801                                                         }
802                                                         else {
803                                                                 buildBrush->sides[ j ].shaderInfo = NULL;  // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
804                                                         }
805                                                 }
806                                                 VectorCopy( points[0], points[3] ); // for cyclic usage
807
808                                                 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
809                                                 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
810                                                 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
811                                                 buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
812                                         }
813
814
815                                         else if ( ( spf == 16 ) ||      //EXTRUDE_TERRAIN
816                                                 ( spf == 512 ) ||       //EXTRUDE_DOWNWARDS
817                                                 ( spf == 1024 ) ||      //EXTRUDE_UPWARDS
818                                                 ( spf == 4096 ) ||      //default CLIPMODEL + AXIAL_BACKPLANE
819                                                 ( spf == 2064 ) ||      //EXTRUDE_TERRAIN+MAX_EXTRUDE
820                                                 ( spf == 4112 ) ||      //EXTRUDE_TERRAIN+AXIAL_BACKPLANE
821                                                 ( spf == 1536 ) ||      //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS
822                                                 ( spf == 2560 ) ||      //EXTRUDE_DOWNWARDS+MAX_EXTRUDE
823                                                 ( spf == 4608 ) ||      //EXTRUDE_DOWNWARDS+AXIAL_BACKPLANE
824                                                 ( spf == 3584 ) ||      //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS+MAX_EXTRUDE
825                                                 ( spf == 5632 ) ||      //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS+AXIAL_BACKPLANE
826                                                 ( spf == 3072 ) ||      //EXTRUDE_UPWARDS+MAX_EXTRUDE
827                                                 ( spf == 5120 ) ){      //EXTRUDE_UPWARDS+AXIAL_BACKPLANE
828
829                                                 if ( spawnFlags & 16 ){ //autodirection
830                                                         VectorCopy( avgDirection, bestNormal );
831                                                 }
832                                                 else{
833                                                         axis = 2;
834                                                         if ( ( spawnFlags & 1536 ) == 1536 ){ //up+down
835                                                                 VectorSet( bestNormal, 0, 0, ( plane[2] >= 0 ? 1 : -1 ) );
836                                                         }
837                                                         else if ( spawnFlags & 512 ){ //down
838                                                                 VectorSet( bestNormal, 0, 0, 1 );
839
840                                                         }
841                                                         else if ( spawnFlags & 1024 ){ //up
842                                                                 VectorSet( bestNormal, 0, 0, -1 );
843                                                         }
844                                                         else{ // best axial normal
845                                                                 VectorCopy( plane, bestNormal );
846                                                                 for ( j = 0; j < 3; j++ ){
847                                                                         if ( fabs(bestNormal[j]) > fabs(bestNormal[(j+1)%3]) ){
848                                                                                 bestNormal[(j+1)%3] = 0.0;
849                                                                                 axis = j;
850                                                                         }
851                                                                         else {
852                                                                                 bestNormal[j] = 0.0;
853                                                                         }
854                                                                 }
855                                                                 VectorNormalize( bestNormal, bestNormal );
856                                                         }
857                                                 }
858
859                                                 if ( DotProduct( plane, bestNormal ) < 0.05 ){
860                                                         goto default_CLIPMODEL;
861                                                 }
862
863
864                                                 /* make side planes */
865                                                 for ( j = 0; j < 3; j++ )
866                                                 {
867                                                         VectorSubtract( points[(j+1)%3], points[ j ], nrm );
868                                                         CrossProduct( bestNormal, nrm, p[ j ] );
869                                                         VectorNormalize( p[ j ], p[ j ] );
870                                                         p[j][3] = DotProduct( points[j], p[j] );
871                                                 }
872
873                                                 /* make back plane */
874                                                 if ( spawnFlags & 2048 ){ //max extrude
875                                                         VectorScale( bestNormal, -1.0f, reverse );
876                                                         if ( bestNormal[axis] > 0 ){
877                                                                 reverse[3] = -min[axis] + clipDepth;
878                                                         }
879                                                         else{
880                                                                 reverse[3] = max[axis] + clipDepth;
881                                                         }
882                                                 }
883                                                 else if ( spawnFlags & 4096 ){ //axial backplane
884                                                         VectorScale( bestNormal, -1.0f, reverse );
885                                                         reverse[3] = points[0][axis];
886                                                         if ( bestNormal[axis] > 0 ){
887                                                                 for ( j = 1; j < 3; j++ ){
888                                                                         if ( points[j][axis] < reverse[3] ){
889                                                                                 reverse[3] = points[j][axis];
890                                                                         }
891                                                                 }
892                                                                 reverse[3] = -reverse[3] + clipDepth;
893                                                         }
894                                                         else{
895                                                                 for ( j = 1; j < 3; j++ ){
896                                                                         if ( points[j][axis] > reverse[3] ){
897                                                                                 reverse[3] = points[j][axis];
898                                                                         }
899                                                                 }
900                                                                 reverse[3] += clipDepth;
901                                                         }
902                                                         if (limDepth != 0.0){
903                                                                 VectorCopy( points[0], cnt );
904                                                                 if ( bestNormal[axis] > 0 ){
905                                                                         for ( j = 1; j < 3; j++ ){
906                                                                                 if ( points[j][axis] > cnt[axis] ){
907                                                                                         VectorCopy( points[j], cnt );
908                                                                                 }
909                                                                         }
910                                                                 }
911                                                                 else {
912                                                                         for ( j = 1; j < 3; j++ ){
913                                                                                 if ( points[j][axis] < cnt[axis] ){
914                                                                                         VectorCopy( points[j], cnt );
915                                                                                 }
916                                                                         }
917                                                                 }
918                                                                 VectorMA( cnt, reverse[3] - DotProduct( reverse, cnt ), reverse, cnt );
919                                                                 if ( ( plane[3] - DotProduct( plane, cnt ) ) > limDepth ){
920                                                                         VectorScale( plane, -1.0f, reverse );
921                                                                         reverse[ 3 ] = -plane[ 3 ];
922                                                                         reverse[3] += clipDepth;
923                                                                 }
924                                                         }
925                                                 }
926                                                 else{   //normal backplane
927                                                         VectorScale( plane, -1.0f, reverse );
928                                                         reverse[ 3 ] = -plane[ 3 ];
929                                                         reverse[3] += clipDepth;
930                                                 }
931 #if nonax_clip_dbg
932                                                 for ( j = 0; j < 3; j++ )
933                                                 {
934                                                         for ( k = 0; k < 3; k++ )
935                                                         {
936                                                                 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
937                                                                         Sys_Printf( "nonax nrm %6.17f %6.17f %6.17f\n", p[j][0], p[j][1], p[j][2] );
938                                                                 }
939                                                         }
940                                                 }
941 #endif
942                                                 /* set up brush sides */
943                                                 buildBrush->numsides = 5;
944                                                 buildBrush->sides[ 0 ].shaderInfo = si;
945                                                 for ( j = 1; j < buildBrush->numsides; j++ ) {
946                                                         if ( debugClip ) {
947                                                                 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
948                                                                 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
949                                                         }
950                                                         else {
951                                                                 buildBrush->sides[ j ].shaderInfo = NULL;  // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
952                                                         }
953                                                 }
954                                                 VectorCopy( points[0], points[3] ); // for cyclic usage
955
956                                                 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
957                                                 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
958                                                 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
959                                                 buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
960                                                 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 0, NULL );
961                                         }
962
963
964                                         else if ( spf == 264 ){ //EXTRUDE_FACE_NORMALS+PYRAMIDAL_CLIP (extrude 45)
965
966                                                 //45 degrees normals for side planes
967                                                 for ( j = 0; j < 3; j++ )
968                                                 {
969                                                         VectorSubtract( points[(j+1)%3], points[ j ], nrm );
970                                                         CrossProduct( plane, nrm, Enorm[ j ] );
971                                                         VectorNormalize( Enorm[ j ], Enorm[ j ] );
972                                                         VectorAdd( plane, Enorm[ j ], Enorm[ j ] );
973                                                         VectorNormalize( Enorm[ j ], Enorm[ j ] );
974                                                         /* make side planes */
975                                                         CrossProduct( Enorm[ j ], nrm, p[ j ] );
976                                                         VectorNormalize( p[ j ], p[ j ] );
977                                                         p[j][3] = DotProduct( points[j], p[j] );
978                                                         //snap nearly axial side planes
979                                                         snpd = qfalse;
980                                                         for ( k = 0; k < 3; k++ )
981                                                         {
982                                                                 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
983                                                                         p[j][k] = 0.0;
984                                                                         snpd = qtrue;
985                                                                 }
986                                                         }
987                                                         if ( snpd ){
988                                                                 VectorNormalize( p[j], p[j] );
989                                                                 VectorAdd( points[j], points[(j+1)%3], cnt );
990                                                                 VectorScale( cnt, 0.5f, cnt );
991                                                                 p[j][3] = DotProduct( cnt, p[j] );
992                                                         }
993                                                 }
994
995                                                 /* make back plane */
996                                                 VectorScale( plane, -1.0f, reverse );
997                                                 reverse[ 3 ] = -plane[ 3 ];
998                                                 reverse[3] += clipDepth;
999
1000                                                 /* set up brush sides */
1001                                                 buildBrush->numsides = 5;
1002                                                 buildBrush->sides[ 0 ].shaderInfo = si;
1003                                                 for ( j = 1; j < buildBrush->numsides; j++ ) {
1004                                                         if ( debugClip ) {
1005                                                                 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
1006                                                                 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
1007                                                         }
1008                                                         else {
1009                                                                 buildBrush->sides[ j ].shaderInfo = NULL;  // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
1010                                                         }
1011                                                 }
1012                                                 VectorCopy( points[0], points[3] ); // for cyclic usage
1013
1014                                                 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
1015                                                 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
1016                                                 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
1017                                                 buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
1018                                                 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 0, NULL );
1019                                         }
1020
1021
1022                                         else if ( ( spf == 128 ) ||     //EXTRUDE_VERTEX_NORMALS
1023                                                 ( spf == 384 ) ){               //EXTRUDE_VERTEX_NORMALS + PYRAMIDAL_CLIP - vertex normals + don't check for sides, sticking outwards
1024                                                 /* get vertex normals */
1025                                                 for ( j = 0; j < 3; j++ )
1026                                                 {
1027                                                         /* get vertex */
1028                                                         dv = &ds->verts[ ds->indexes[ i + j ] ];
1029                                                         /* copy normal */
1030                                                         VectorCopy( dv->normal, Vnorm[ j ] );
1031                                                 }
1032
1033                                                 //avg normals for side planes
1034                                                 for ( j = 0; j < 3; j++ )
1035                                                 {
1036                                                         VectorAdd( Vnorm[ j ], Vnorm[ (j+1)%3 ], Enorm[ j ] );
1037                                                         VectorNormalize( Enorm[ j ], Enorm[ j ] );
1038                                                         //check fuer bad ones
1039                                                         VectorSubtract( points[(j+1)%3], points[ j ], cnt );
1040                                                         CrossProduct( plane, cnt, nrm );
1041                                                         VectorNormalize( nrm, nrm );
1042                                                         //check for negative or outside direction
1043                                                         if ( DotProduct( Enorm[ j ], plane ) > 0.1 ){
1044                                                                 if ( ( DotProduct( Enorm[ j ], nrm ) > -0.2 ) || ( spawnFlags & 256 ) ){
1045                                                                         //ok++;
1046                                                                         continue;
1047                                                                 }
1048                                                         }
1049                                                         //notok++;
1050                                                         //Sys_Printf( "faulty Enormal %i/%i\n", notok, ok );
1051                                                         //use 45 normal
1052                                                         VectorAdd( plane, nrm, Enorm[ j ] );
1053                                                         VectorNormalize( Enorm[ j ], Enorm[ j ] );
1054                                                 }
1055
1056                                                 /* make side planes */
1057                                                 for ( j = 0; j < 3; j++ )
1058                                                 {
1059                                                         VectorSubtract( points[(j+1)%3], points[ j ], nrm );
1060                                                         CrossProduct( Enorm[ j ], nrm, p[ j ] );
1061                                                         VectorNormalize( p[ j ], p[ j ] );
1062                                                         p[j][3] = DotProduct( points[j], p[j] );
1063                                                         //snap nearly axial side planes
1064                                                         snpd = qfalse;
1065                                                         for ( k = 0; k < 3; k++ )
1066                                                         {
1067                                                                 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
1068                                                                         //Sys_Printf( "init plane %6.8f %6.8f %6.8f %6.8f\n", p[j][0], p[j][1], p[j][2], p[j][3]);
1069                                                                         p[j][k] = 0.0;
1070                                                                         snpd = qtrue;
1071                                                                 }
1072                                                         }
1073                                                         if ( snpd ){
1074                                                                 VectorNormalize( p[j], p[j] );
1075                                                                 //Sys_Printf( "nrm plane %6.8f %6.8f %6.8f %6.8f\n", p[j][0], p[j][1], p[j][2], p[j][3]);
1076                                                                 VectorAdd( points[j], points[(j+1)%3], cnt );
1077                                                                 VectorScale( cnt, 0.5f, cnt );
1078                                                                 p[j][3] = DotProduct( cnt, p[j] );
1079                                                                 //Sys_Printf( "dst plane %6.8f %6.8f %6.8f %6.8f\n", p[j][0], p[j][1], p[j][2], p[j][3]);
1080                                                         }
1081                                                 }
1082
1083                                                 /* make back plane */
1084                                                 VectorScale( plane, -1.0f, reverse );
1085                                                 reverse[ 3 ] = -plane[ 3 ];
1086                                                 reverse[3] += clipDepth;
1087
1088                                                 /* set up brush sides */
1089                                                 buildBrush->numsides = 5;
1090                                                 buildBrush->sides[ 0 ].shaderInfo = si;
1091                                                 for ( j = 1; j < buildBrush->numsides; j++ ) {
1092                                                         if ( debugClip ) {
1093                                                                 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
1094                                                                 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
1095                                                         }
1096                                                         else {
1097                                                                 buildBrush->sides[ j ].shaderInfo = NULL;  // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
1098                                                         }
1099                                                 }
1100                                                 VectorCopy( points[0], points[3] ); // for cyclic usage
1101
1102                                                 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
1103                                                 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
1104                                                 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
1105                                                 buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
1106                                                 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 0, NULL );
1107                                         }
1108
1109
1110                                         else if ( spf == 8 ){   //EXTRUDE_FACE_NORMALS
1111
1112                                                 /* make side planes */
1113                                                 for ( j = 0; j < 3; j++ )
1114                                                 {
1115                                                         VectorSubtract( points[(j+1)%3], points[ j ], nrm );
1116                                                         CrossProduct( plane, nrm, p[ j ] );
1117                                                         VectorNormalize( p[ j ], p[ j ] );
1118                                                         p[j][3] = DotProduct( points[j], p[j] );
1119                                                         //snap nearly axial side planes
1120                                                         snpd = qfalse;
1121                                                         for ( k = 0; k < 3; k++ )
1122                                                         {
1123                                                                 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
1124                                                                         //Sys_Printf( "init plane %6.8f %6.8f %6.8f %6.8f\n", p[j][0], p[j][1], p[j][2], p[j][3]);
1125                                                                         p[j][k] = 0.0;
1126                                                                         snpd = qtrue;
1127                                                                 }
1128                                                         }
1129                                                         if ( snpd ){
1130                                                                 VectorNormalize( p[j], p[j] );
1131                                                                 //Sys_Printf( "nrm plane %6.8f %6.8f %6.8f %6.8f\n", p[j][0], p[j][1], p[j][2], p[j][3]);
1132                                                                 VectorAdd( points[j], points[(j+1)%3], cnt );
1133                                                                 VectorScale( cnt, 0.5f, cnt );
1134                                                                 p[j][3] = DotProduct( cnt, p[j] );
1135                                                                 //Sys_Printf( "dst plane %6.8f %6.8f %6.8f %6.8f\n", p[j][0], p[j][1], p[j][2], p[j][3]);
1136                                                         }
1137                                                 }
1138
1139                                                 /* make back plane */
1140                                                 VectorScale( plane, -1.0f, reverse );
1141                                                 reverse[ 3 ] = -plane[ 3 ];
1142                                                 reverse[3] += clipDepth;
1143 #if nonax_clip_dbg
1144                                                 for ( j = 0; j < 3; j++ )
1145                                                 {
1146                                                         for ( k = 0; k < 3; k++ )
1147                                                         {
1148                                                                 if ( fabs(p[j][k]) < 0.00005 && p[j][k] != 0.0 ){
1149                                                                         Sys_Printf( "nonax nrm %6.17f %6.17f %6.17f\n", p[j][0], p[j][1], p[j][2] );
1150                                                                         Sys_Printf( "frm src nrm %6.17f %6.17f %6.17f\n", plane[0], plane[1], plane[2]);
1151                                                                 }
1152                                                         }
1153                                                 }
1154 #endif
1155                                                 /* set up brush sides */
1156                                                 buildBrush->numsides = 5;
1157                                                 buildBrush->sides[ 0 ].shaderInfo = si;
1158                                                 for ( j = 1; j < buildBrush->numsides; j++ ) {
1159                                                         if ( debugClip ) {
1160                                                                 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
1161                                                                 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
1162                                                         }
1163                                                         else {
1164                                                                 buildBrush->sides[ j ].shaderInfo = NULL;  // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
1165                                                         }
1166                                                 }
1167                                                 VectorCopy( points[0], points[3] ); // for cyclic usage
1168
1169                                                 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
1170                                                 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
1171                                                 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
1172                                                 buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
1173                                                 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 0, NULL );
1174                                         }
1175
1176
1177                                         else if ( spf == 256 ){ //PYRAMIDAL_CLIP
1178
1179                                                 /* calculate center */
1180                                                 VectorAdd( points[ 0 ], points[ 1 ], cnt );
1181                                                 VectorAdd( cnt, points[ 2 ], cnt );
1182                                                 VectorScale( cnt, 0.3333333333333f, cnt );
1183
1184                                                 /* make back pyramid point */
1185                                                 VectorMA( cnt, -clipDepth, plane, cnt );
1186
1187                                                 /* make 3 more planes */
1188                                                 if( PlaneFromPoints( p[0], points[ 2 ], points[ 1 ], cnt ) &&
1189                                                         PlaneFromPoints( p[1], points[ 1 ], points[ 0 ], cnt ) &&
1190                                                         PlaneFromPoints( p[2], points[ 0 ], points[ 2 ], cnt ) ) {
1191
1192                                                         //check for dangerous planes
1193                                                         while( (( p[0][0] != 0.0 || p[0][1] != 0.0 ) && fabs(p[0][0]) < 0.00025 && fabs(p[0][1]) < 0.00025) ||
1194                                                                 (( p[0][0] != 0.0 || p[0][2] != 0.0 ) && fabs(p[0][0]) < 0.00025 && fabs(p[0][2]) < 0.00025) ||
1195                                                                 (( p[0][2] != 0.0 || p[0][1] != 0.0 ) && fabs(p[0][2]) < 0.00025 && fabs(p[0][1]) < 0.00025) ||
1196                                                                 (( p[1][0] != 0.0 || p[1][1] != 0.0 ) && fabs(p[1][0]) < 0.00025 && fabs(p[1][1]) < 0.00025) ||
1197                                                                 (( p[1][0] != 0.0 || p[1][2] != 0.0 ) && fabs(p[1][0]) < 0.00025 && fabs(p[1][2]) < 0.00025) ||
1198                                                                 (( p[1][2] != 0.0 || p[1][1] != 0.0 ) && fabs(p[1][2]) < 0.00025 && fabs(p[1][1]) < 0.00025) ||
1199                                                                 (( p[2][0] != 0.0 || p[2][1] != 0.0 ) && fabs(p[2][0]) < 0.00025 && fabs(p[2][1]) < 0.00025) ||
1200                                                                 (( p[2][0] != 0.0 || p[2][2] != 0.0 ) && fabs(p[2][0]) < 0.00025 && fabs(p[2][2]) < 0.00025) ||
1201                                                                 (( p[2][2] != 0.0 || p[2][1] != 0.0 ) && fabs(p[2][2]) < 0.00025 && fabs(p[2][1]) < 0.00025) ) {
1202                                                                 VectorMA( cnt, -0.1f, plane, cnt );
1203                                                                 //      Sys_Printf( "shifting pyramid point\n" );
1204                                                                 PlaneFromPoints( p[0], points[ 2 ], points[ 1 ], cnt );
1205                                                                 PlaneFromPoints( p[1], points[ 1 ], points[ 0 ], cnt );
1206                                                                 PlaneFromPoints( p[2], points[ 0 ], points[ 2 ], cnt );
1207                                                         }
1208 #if nonax_clip_dbg
1209                                                         for ( j = 0; j < 3; j++ )
1210                                                         {
1211                                                                 for ( k = 0; k < 3; k++ )
1212                                                                 {
1213                                                                         if ( fabs(p[j][k]) < 0.00005 && p[j][k] != 0.0 ){
1214                                                                                 Sys_Printf( "nonax nrm %6.17f %6.17f %6.17f\n (%6.8f %6.8f %6.8f)\n (%6.8f %6.8f %6.8f)\n (%6.8f %6.8f %6.8f)\n", p[j][0], p[j][1], p[j][2], points[j][0], points[j][1], points[j][2], points[(j+1)%3][0], points[(j+1)%3][1], points[(j+1)%3][2], cnt[0], cnt[1], cnt[2] );
1215                                                                         }
1216                                                                 }
1217                                                         }
1218 #endif
1219                                                         /* set up brush sides */
1220                                                         buildBrush->numsides = 4;
1221                                                         buildBrush->sides[ 0 ].shaderInfo = si;
1222                                                         for ( j = 1; j < buildBrush->numsides; j++ ) {
1223                                                                 if ( debugClip ) {
1224                                                                         buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
1225                                                                         buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
1226                                                                 }
1227                                                                 else {
1228                                                                         buildBrush->sides[ j ].shaderInfo = NULL;  // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
1229                                                                 }
1230                                                         }
1231                                                         VectorCopy( points[0], points[3] ); // for cyclic usage
1232
1233                                                         buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
1234                                                         buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 1 ] ); // p[0] contains points[1] and points[2]
1235                                                         buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 0 ] ); // p[1] contains points[0] and points[1]
1236                                                         buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
1237                                                 }
1238                                                 else
1239                                                 {
1240                                                         Sys_Printf( "WARNING: triangle (%6.0f %6.0f %6.0f) (%6.0f %6.0f %6.0f) (%6.0f %6.0f %6.0f) of %s was not autoclipped\n", points[0][0], points[0][1], points[0][2], points[1][0], points[1][1], points[1][2], points[2][0], points[2][1], points[2][2], name );
1241                                                         free( buildBrush );
1242                                                         continue;
1243                                                 }
1244                                         }
1245
1246
1247                                         else if ( ( si->clipModel && !( spf ) ) || ( ( spawnFlags & 8090 ) == 2 ) ){    //default CLIPMODEL
1248
1249                                                 default_CLIPMODEL:
1250                                                 // axial normal
1251                                                 VectorCopy( plane, bestNormal );
1252                                                 for ( j = 0; j < 3; j++ ){
1253                                                         if ( fabs(bestNormal[j]) > fabs(bestNormal[(j+1)%3]) ){
1254                                                                 bestNormal[(j+1)%3] = 0.0;
1255                                                         }
1256                                                         else {
1257                                                                 bestNormal[j] = 0.0;
1258                                                         }
1259                                                 }
1260                                                 VectorNormalize( bestNormal, bestNormal );
1261
1262                                                 /* make side planes */
1263                                                 for ( j = 0; j < 3; j++ )
1264                                                 {
1265                                                         VectorSubtract( points[(j+1)%3], points[ j ], nrm );
1266                                                         CrossProduct( bestNormal, nrm, p[ j ] );
1267                                                         VectorNormalize( p[ j ], p[ j ] );
1268                                                         p[j][3] = DotProduct( points[j], p[j] );
1269                                                 }
1270
1271                                                 /* make back plane */
1272                                                 VectorScale( plane, -1.0f, reverse );
1273                                                 reverse[ 3 ] = -plane[ 3 ];
1274                                                 reverse[3] += DotProduct( bestNormal, plane ) * clipDepth;
1275 #if nonax_clip_dbg
1276                                                 for ( j = 0; j < 3; j++ )
1277                                                 {
1278                                                         for ( k = 0; k < 3; k++ )
1279                                                         {
1280                                                                 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
1281                                                                         Sys_Printf( "nonax nrm %6.17f %6.17f %6.17f\n", p[j][0], p[j][1], p[j][2] );
1282                                                                 }
1283                                                         }
1284                                                 }
1285 #endif
1286                                                 /* set up brush sides */
1287                                                 buildBrush->numsides = 5;
1288                                                 buildBrush->sides[ 0 ].shaderInfo = si;
1289                                                 for ( j = 1; j < buildBrush->numsides; j++ ) {
1290                                                         if ( debugClip ) {
1291                                                                 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
1292                                                                 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
1293                                                         }
1294                                                         else {
1295                                                                 buildBrush->sides[ j ].shaderInfo = NULL;  // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
1296                                                         }
1297                                                 }
1298                                                 VectorCopy( points[0], points[3] ); // for cyclic usage
1299
1300                                                 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
1301                                                 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
1302                                                 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
1303                                                 buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
1304                                                 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 0, NULL );
1305                                         }
1306
1307
1308                                         /* add to entity */
1309                                         if ( CreateBrushWindings( buildBrush ) ) {
1310                                                 AddBrushBevels();
1311                                                 //%     EmitBrushes( buildBrush, NULL, NULL );
1312                                                 buildBrush->next = entities[ mapEntityNum ].brushes;
1313                                                 entities[ mapEntityNum ].brushes = buildBrush;
1314                                                 entities[ mapEntityNum ].numBrushes++;
1315                                         }
1316                                         else{
1317                                                 Sys_Printf( "WARNING: triangle (%6.0f %6.0f %6.0f) (%6.0f %6.0f %6.0f) (%6.0f %6.0f %6.0f) of %s was not autoclipped\n", points[0][0], points[0][1], points[0][2], points[1][0], points[1][1], points[1][2], points[2][0], points[2][1], points[2][2], name );
1318                                                 free( buildBrush );
1319                                         }
1320                                 }
1321                         }
1322                         normalEpsilon = normalEpsilon_save;
1323                 }
1324                 else if ( spawnFlags & 8090 ){
1325                         Sys_Printf( "WARNING: nonexistent clipping mode selected\n" );
1326                 }
1327         }
1328 }
1329
1330
1331
1332 /*
1333    AddTriangleModels()
1334    adds misc_model surfaces to the bsp
1335  */
1336
1337 void AddTriangleModels( entity_t *e ){
1338         int num, frame, skin, castShadows, recvShadows, spawnFlags;
1339         entity_t        *e2;
1340         const char      *targetName;
1341         const char      *target, *model, *value;
1342         char shader[ MAX_QPATH ];
1343         shaderInfo_t    *celShader;
1344         float temp, baseLightmapScale, lightmapScale, clipDepth;
1345         float shadeAngle;
1346         int lightmapSampleSize;
1347         vec3_t origin, scale, angles;
1348         m4x4_t transform;
1349         epair_t         *ep;
1350         remap_t         *remap, *remap2;
1351         char            *split;
1352
1353
1354         /* note it */
1355         Sys_FPrintf( SYS_VRB, "--- AddTriangleModels ---\n" );
1356
1357
1358         /* get current brush entity targetname */
1359         if ( e == entities ) {
1360                 targetName = "";
1361         }
1362         else
1363         {
1364                 targetName = ValueForKey( e, "targetname" );
1365
1366                 /* misc_model entities target non-worldspawn brush model entities */
1367                 if ( targetName[ 0 ] == '\0' ) {
1368                         return;
1369                 }
1370         }
1371
1372         /* get lightmap scale */
1373         /* vortex: added _ls key (short name of lightmapscale) */
1374         baseLightmapScale = 0.0f;
1375         if ( strcmp( "", ValueForKey( e, "lightmapscale" ) ) ||
1376                  strcmp( "", ValueForKey( e, "_lightmapscale" ) ) ||
1377                  strcmp( "", ValueForKey( e, "_ls" ) ) ) {
1378                 baseLightmapScale = FloatForKey( e, "lightmapscale" );
1379                 if ( baseLightmapScale <= 0.0f ) {
1380                         baseLightmapScale = FloatForKey( e, "_lightmapscale" );
1381                 }
1382                 if ( baseLightmapScale <= 0.0f ) {
1383                         baseLightmapScale = FloatForKey( e, "_ls" );
1384                 }
1385                 if ( baseLightmapScale < 0.0f ) {
1386                         baseLightmapScale = 0.0f;
1387                 }
1388                 if ( baseLightmapScale > 0.0f ) {
1389                         Sys_Printf( "World Entity has lightmap scale of %.4f\n", baseLightmapScale );
1390                 }
1391         }
1392
1393         /* walk the entity list */
1394         for ( num = 1; num < numEntities; num++ )
1395         {
1396                 /* get e2 */
1397                 e2 = &entities[ num ];
1398
1399                 /* convert misc_models into raw geometry */
1400                 if ( Q_stricmp( "misc_model", ValueForKey( e2, "classname" ) ) ) {
1401                         continue;
1402                 }
1403
1404                 /* ydnar: added support for md3 models on non-worldspawn models */
1405                 target = ValueForKey( e2, "target" );
1406                 if ( strcmp( target, targetName ) ) {
1407                         continue;
1408                 }
1409
1410                 /* get model name */
1411                 model = ValueForKey( e2, "model" );
1412                 if ( model[ 0 ] == '\0' ) {
1413                         Sys_FPrintf( SYS_WRN, "WARNING: misc_model at %i %i %i without a model key\n",
1414                                                 (int) origin[ 0 ], (int) origin[ 1 ], (int) origin[ 2 ] );
1415                         continue;
1416                 }
1417
1418                 /* get model frame */
1419                 frame = 0;
1420                 if ( strcmp( "", ValueForKey( e2, "_frame" ) ) ) {
1421                         frame = IntForKey( e2, "_frame" );
1422                 }
1423                 else if ( strcmp( "", ValueForKey( e2, "frame" ) ) ) {
1424                         frame = IntForKey( e2, "frame" );
1425                 }
1426
1427                 /* worldspawn (and func_groups) default to cast/recv shadows in worldspawn group */
1428                 if ( e == entities ) {
1429                         castShadows = WORLDSPAWN_CAST_SHADOWS;
1430                         recvShadows = WORLDSPAWN_RECV_SHADOWS;
1431                 }
1432
1433                 /* other entities don't cast any shadows, but recv worldspawn shadows */
1434                 else
1435                 {
1436                         castShadows = ENTITY_CAST_SHADOWS;
1437                         recvShadows = ENTITY_RECV_SHADOWS;
1438                 }
1439
1440                 /* get explicit shadow flags */
1441                 GetEntityShadowFlags( e2, e, &castShadows, &recvShadows );
1442
1443                 /* get spawnflags */
1444                 spawnFlags = IntForKey( e2, "spawnflags" );
1445
1446                 /* get origin */
1447                 GetVectorForKey( e2, "origin", origin );
1448                 VectorSubtract( origin, e->origin, origin );    /* offset by parent */
1449
1450                 /* get scale */
1451                 scale[ 0 ] = scale[ 1 ] = scale[ 2 ] = 1.0f;
1452                 temp = FloatForKey( e2, "modelscale" );
1453                 if ( temp != 0.0f ) {
1454                         scale[ 0 ] = scale[ 1 ] = scale[ 2 ] = temp;
1455                 }
1456                 value = ValueForKey( e2, "modelscale_vec" );
1457                 if ( value[ 0 ] != '\0' ) {
1458                         sscanf( value, "%f %f %f", &scale[ 0 ], &scale[ 1 ], &scale[ 2 ] );
1459                 }
1460
1461                 /* get "angle" (yaw) or "angles" (pitch yaw roll) */
1462                 angles[ 0 ] = angles[ 1 ] = angles[ 2 ] = 0.0f;
1463                 angles[ 2 ] = FloatForKey( e2, "angle" );
1464                 value = ValueForKey( e2, "angles" );
1465                 if ( value[ 0 ] != '\0' ) {
1466                         sscanf( value, "%f %f %f", &angles[ 1 ], &angles[ 2 ], &angles[ 0 ] );
1467                 }
1468
1469                 /* set transform matrix (thanks spog) */
1470                 m4x4_identity( transform );
1471                 m4x4_pivoted_transform_by_vec3( transform, origin, angles, eXYZ, scale, vec3_origin );
1472
1473                 /* get shader remappings */
1474                 remap = NULL;
1475                 for ( ep = e2->epairs; ep != NULL; ep = ep->next )
1476                 {
1477                         /* look for keys prefixed with "_remap" */
1478                         if ( ep->key != NULL && ep->value != NULL &&
1479                                  ep->key[ 0 ] != '\0' && ep->value[ 0 ] != '\0' &&
1480                                  !Q_strncasecmp( ep->key, "_remap", 6 ) ) {
1481                                 /* create new remapping */
1482                                 remap2 = remap;
1483                                 remap = safe_malloc( sizeof( *remap ) );
1484                                 remap->next = remap2;
1485                                 strcpy( remap->from, ep->value );
1486
1487                                 /* split the string */
1488                                 split = strchr( remap->from, ';' );
1489                                 if ( split == NULL ) {
1490                                         Sys_FPrintf( SYS_WRN, "WARNING: Shader _remap key found in misc_model without a ; character\n" );
1491                                         free( remap );
1492                                         remap = remap2;
1493                                         continue;
1494                                 }
1495
1496                                 /* store the split */
1497                                 *split = '\0';
1498                                 strcpy( remap->to, ( split + 1 ) );
1499
1500                                 /* note it */
1501                                 //%     Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", remap->from, remap->to );
1502                         }
1503                 }
1504
1505                 /* ydnar: cel shader support */
1506                 value = ValueForKey( e2, "_celshader" );
1507                 if ( value[ 0 ] == '\0' ) {
1508                         value = ValueForKey( &entities[ 0 ], "_celshader" );
1509                 }
1510                 if ( value[ 0 ] != '\0' ) {
1511                         sprintf( shader, "textures/%s", value );
1512                         celShader = ShaderInfoForShader( shader );
1513                 }
1514                 else{
1515                         celShader = *globalCelShader ? ShaderInfoForShader( globalCelShader ) : NULL;
1516                 }
1517
1518                 /* jal : entity based _samplesize */
1519                 lightmapSampleSize = 0;
1520                 if ( strcmp( "", ValueForKey( e2, "_lightmapsamplesize" ) ) ) {
1521                         lightmapSampleSize = IntForKey( e2, "_lightmapsamplesize" );
1522                 }
1523                 else if ( strcmp( "", ValueForKey( e2, "_samplesize" ) ) ) {
1524                         lightmapSampleSize = IntForKey( e2, "_samplesize" );
1525                 }
1526                 else if ( strcmp( "", ValueForKey( e2, "_ss" ) ) ) {
1527                         lightmapSampleSize = IntForKey( e2, "_ss" );
1528                 }
1529
1530                 if ( lightmapSampleSize < 0 ) {
1531                         lightmapSampleSize = 0;
1532                 }
1533
1534                 if ( lightmapSampleSize > 0.0f ) {
1535                         Sys_Printf( "misc_model has lightmap sample size of %.d\n", lightmapSampleSize );
1536                 }
1537
1538                 /* get lightmap scale */
1539                 /* vortex: added _ls key (short name of lightmapscale) */
1540                 lightmapScale = 0.0f;
1541                 if ( strcmp( "", ValueForKey( e2, "lightmapscale" ) ) ||
1542                          strcmp( "", ValueForKey( e2, "_lightmapscale" ) ) ||
1543                          strcmp( "", ValueForKey( e2, "_ls" ) ) ) {
1544                         lightmapScale = FloatForKey( e2, "lightmapscale" );
1545                         if ( lightmapScale <= 0.0f ) {
1546                                 lightmapScale = FloatForKey( e2, "_lightmapscale" );
1547                         }
1548                         if ( lightmapScale <= 0.0f ) {
1549                                 lightmapScale = FloatForKey( e2, "_ls" );
1550                         }
1551                         if ( lightmapScale < 0.0f ) {
1552                                 lightmapScale = 0.0f;
1553                         }
1554                         if ( lightmapScale > 0.0f ) {
1555                                 Sys_Printf( "misc_model has lightmap scale of %.4f\n", lightmapScale );
1556                         }
1557                 }
1558
1559                 /* jal : entity based _shadeangle */
1560                 shadeAngle = 0.0f;
1561                 if ( strcmp( "", ValueForKey( e2, "_shadeangle" ) ) ) {
1562                         shadeAngle = FloatForKey( e2, "_shadeangle" );
1563                 }
1564                 /* vortex' aliases */
1565                 else if ( strcmp( "", ValueForKey( e2, "_smoothnormals" ) ) ) {
1566                         shadeAngle = FloatForKey( e2, "_smoothnormals" );
1567                 }
1568                 else if ( strcmp( "", ValueForKey( e2, "_sn" ) ) ) {
1569                         shadeAngle = FloatForKey( e2, "_sn" );
1570                 }
1571                 else if ( strcmp( "", ValueForKey( e2, "_sa" ) ) ) {
1572                         shadeAngle = FloatForKey( e2, "_sa" );
1573                 }
1574                 else if ( strcmp( "", ValueForKey( e2, "_smooth" ) ) ) {
1575                         shadeAngle = FloatForKey( e2, "_smooth" );
1576                 }
1577
1578                 if ( shadeAngle < 0.0f ) {
1579                         shadeAngle = 0.0f;
1580                 }
1581
1582                 if ( shadeAngle > 0.0f ) {
1583                         Sys_Printf( "misc_model has shading angle of %.4f\n", shadeAngle );
1584                 }
1585
1586                 skin = 0;
1587                 if ( strcmp( "", ValueForKey( e2, "_skin" ) ) ) {
1588                         skin = IntForKey( e2, "_skin" );
1589                 }
1590                 else if ( strcmp( "", ValueForKey( e2, "skin" ) ) ) {
1591                         skin = IntForKey( e2, "skin" );
1592                 }
1593
1594                 clipDepth = clipDepthGlobal;
1595                 if ( strcmp( "", ValueForKey( e2, "_clipdepth" ) ) ) {
1596                         clipDepth = FloatForKey( e2, "_clipdepth" );
1597                         Sys_Printf( "misc_model has autoclip depth of %.3f\n", clipDepth );
1598                 }
1599
1600
1601                 /* insert the model */
1602                 InsertModel( model, skin, frame, transform, remap, celShader, mapEntityNum, castShadows, recvShadows, spawnFlags, lightmapScale, lightmapSampleSize, shadeAngle, clipDepth );
1603
1604                 /* free shader remappings */
1605                 while ( remap != NULL )
1606                 {
1607                         remap2 = remap->next;
1608                         free( remap );
1609                         remap = remap2;
1610                 }
1611         }
1612
1613 }