]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/model.c
Merge commit '2de8ee725b2a6e54e21d5e217ae453ee115b913a' into garux-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_malloc( ds->numVerts * sizeof( ds->verts[ 0 ] ) );
451                 memset( ds->verts, 0, ds->numVerts * sizeof( ds->verts[ 0 ] ) );
452
453                 ds->numIndexes = PicoGetSurfaceNumIndexes( surface );
454                 ds->indexes = safe_malloc( ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
455                 memset( ds->indexes, 0, ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
456
457                 /* copy vertexes */
458                 for ( i = 0; i < ds->numVerts; i++ )
459                 {
460                         /* get vertex */
461                         dv = &ds->verts[ i ];
462
463                         /* xyz and normal */
464                         xyz = PicoGetSurfaceXYZ( surface, i );
465                         VectorCopy( xyz, dv->xyz );
466                         m4x4_transform_point( transform, dv->xyz );
467
468                         normal = PicoGetSurfaceNormal( surface, i );
469                         VectorCopy( normal, dv->normal );
470                         m4x4_transform_normal( nTransform, dv->normal );
471                         VectorNormalize( dv->normal, dv->normal );
472
473                         /* ydnar: tek-fu celshading support for flat shaded shit */
474                         if ( flat ) {
475                                 dv->st[ 0 ] = si->stFlat[ 0 ];
476                                 dv->st[ 1 ] = si->stFlat[ 1 ];
477                         }
478
479                         /* ydnar: gs mods: added support for explicit shader texcoord generation */
480                         else if ( si->tcGen ) {
481                                 /* project the texture */
482                                 dv->st[ 0 ] = DotProduct( si->vecs[ 0 ], dv->xyz );
483                                 dv->st[ 1 ] = DotProduct( si->vecs[ 1 ], dv->xyz );
484                         }
485
486                         /* normal texture coordinates */
487                         else
488                         {
489                                 st = PicoGetSurfaceST( surface, 0, i );
490                                 dv->st[ 0 ] = st[ 0 ];
491                                 dv->st[ 1 ] = st[ 1 ];
492                         }
493
494                         /* set lightmap/color bits */
495                         color = PicoGetSurfaceColor( surface, 0, i );
496                         for ( j = 0; j < MAX_LIGHTMAPS; j++ )
497                         {
498                                 dv->lightmap[ j ][ 0 ] = 0.0f;
499                                 dv->lightmap[ j ][ 1 ] = 0.0f;
500                                 if ( spawnFlags & 32 ) { // spawnflag 32: model color -> alpha hack
501                                         dv->color[ j ][ 0 ] = 255.0f;
502                                         dv->color[ j ][ 1 ] = 255.0f;
503                                         dv->color[ j ][ 2 ] = 255.0f;
504                                         dv->color[ j ][ 3 ] = RGBTOGRAY( color );
505                                 }
506                                 else
507                                 {
508                                         dv->color[ j ][ 0 ] = color[ 0 ];
509                                         dv->color[ j ][ 1 ] = color[ 1 ];
510                                         dv->color[ j ][ 2 ] = color[ 2 ];
511                                         dv->color[ j ][ 3 ] = color[ 3 ];
512                                 }
513                         }
514                 }
515
516                 /* copy indexes */
517                 indexes = PicoGetSurfaceIndexes( surface, 0 );
518                 for ( i = 0; i < ds->numIndexes; i++ )
519                         ds->indexes[ i ] = indexes[ i ];
520
521                 /* set cel shader */
522                 ds->celShader = celShader;
523
524                 /* ydnar: giant hack land: generate clipping brushes for model triangles */
525                 if ( ( si->clipModel && !( spf ) ) ||   //default CLIPMODEL
526                         ( ( spawnFlags & 8090 ) == 2 ) ||       //default CLIPMODEL
527                         ( spf == 8 ) ||         //EXTRUDE_FACE_NORMALS
528                         ( spf == 16 )   ||      //EXTRUDE_TERRAIN
529                         ( spf == 128 )  ||      //EXTRUDE_VERTEX_NORMALS
530                         ( spf == 256 )  ||      //PYRAMIDAL_CLIP
531                         ( spf == 512 )  ||      //EXTRUDE_DOWNWARDS
532                         ( spf == 1024 ) ||      //EXTRUDE_UPWARDS
533                         ( spf == 4096 ) ||      //default CLIPMODEL + AXIAL_BACKPLANE
534                         ( spf == 264 )  ||      //EXTRUDE_FACE_NORMALS+PYRAMIDAL_CLIP (extrude 45)
535                         ( spf == 2064 ) ||      //EXTRUDE_TERRAIN+MAX_EXTRUDE
536                         ( spf == 4112 ) ||      //EXTRUDE_TERRAIN+AXIAL_BACKPLANE
537                         ( spf == 384 )  ||      //EXTRUDE_VERTEX_NORMALS + PYRAMIDAL_CLIP - vertex normals + don't check for sides, sticking outwards
538                         ( spf == 4352 ) ||      //PYRAMIDAL_CLIP+AXIAL_BACKPLANE
539                         ( spf == 1536 ) ||      //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS
540                         ( spf == 2560 ) ||      //EXTRUDE_DOWNWARDS+MAX_EXTRUDE
541                         ( spf == 4608 ) ||      //EXTRUDE_DOWNWARDS+AXIAL_BACKPLANE
542                         ( spf == 3584 ) ||      //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS+MAX_EXTRUDE
543                         ( spf == 5632 ) ||      //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS+AXIAL_BACKPLANE
544                         ( spf == 3072 ) ||      //EXTRUDE_UPWARDS+MAX_EXTRUDE
545                         ( spf == 5120 ) ){      //EXTRUDE_UPWARDS+AXIAL_BACKPLANE
546                         vec3_t points[ 4 ], backs[ 3 ], cnt, bestNormal, nrm, Vnorm[3], Enorm[3];
547                         vec4_t plane, reverse, p[3];
548                         double normalEpsilon_save;
549                         qboolean snpd;
550                         vec3_t min = { 999999, 999999, 999999 }, max = { -999999, -999999, -999999 };
551                         vec3_t avgDirection = { 0, 0, 0 };
552                         int axis;
553                         #define nonax_clip_dbg 0
554
555                         /* temp hack */
556                         if ( !si->clipModel && !( si->compileFlags & C_SOLID ) ) {
557                                 continue;
558                         }
559
560                         //wont snap these in normal way, or will explode
561                         normalEpsilon_save = normalEpsilon;
562                         //normalEpsilon = 0.000001;
563
564
565                         //MAX_EXTRUDE or EXTRUDE_TERRAIN
566                         if ( ( spawnFlags & 2048 ) || ( spawnFlags & 16 ) ){
567
568                                 for ( i = 0; i < ds->numIndexes; i += 3 )
569                                 {
570                                         for ( j = 0; j < 3; j++ )
571                                         {
572                                                 dv = &ds->verts[ ds->indexes[ i + j ] ];
573                                                 VectorCopy( dv->xyz, points[ j ] );
574                                         }
575                                         if ( PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] ) ){
576                                                 if ( spawnFlags & 16 ) VectorAdd( avgDirection, plane, avgDirection );  //calculate average mesh facing direction
577
578                                                 //get min/max
579                                                 for ( k = 2; k > -1; k-- ){
580                                                         if ( plane[k] > 0 ){
581                                                                 for ( j = 0; j < 3; j++ ){ if ( points[j][k] < min[k] ) min[k] = points[j][k]; }
582                                                         }
583                                                         else if ( plane[k] < 0 ){
584                                                                 for ( j = 0; j < 3; j++ ){ if ( points[j][k] > max[k] ) max[k] = points[j][k]; }
585                                                         }
586                                                         //if EXTRUDE_DOWNWARDS or EXTRUDE_UPWARDS
587                                                         if ( ( spawnFlags & 512 ) || ( spawnFlags & 1024 ) ){
588                                                                 break;
589                                                         }
590                                                 }
591                                         }
592                                 }
593                                 //unify avg direction
594                                 if ( spawnFlags & 16 ){
595                                         for ( j = 0; j < 3; j++ ){
596                                                 if ( fabs(avgDirection[j]) > fabs(avgDirection[(j+1)%3]) ){
597                                                         avgDirection[(j+1)%3] = 0.0;
598                                                         axis = j;
599                                                 }
600                                                 else {
601                                                         avgDirection[j] = 0.0;
602                                                 }
603                                         }
604                                         if ( VectorNormalize( avgDirection, avgDirection ) == 0 ){
605                                                 axis = 2;
606                                                 VectorSet( avgDirection, 0, 0, 1 );
607                                         }
608                                 }
609                         }
610
611                         /* walk triangle list */
612                         for ( i = 0; i < ds->numIndexes; i += 3 )
613                         {
614                                 /* overflow hack */
615                                 AUTOEXPAND_BY_REALLOC( mapplanes, ( nummapplanes + 64 ) << 1, allocatedmapplanes, 1024 );
616
617                                 /* make points */
618                                 for ( j = 0; j < 3; j++ )
619                                 {
620                                         /* get vertex */
621                                         dv = &ds->verts[ ds->indexes[ i + j ] ];
622
623                                         /* copy xyz */
624                                         VectorCopy( dv->xyz, points[ j ] );
625                                 }
626
627                                 /* make plane for triangle */
628                                 if ( PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] ) ) {
629
630                                         /* build a brush */
631                                         buildBrush = AllocBrush( 48 );
632                                         buildBrush->entityNum = mapEntityNum;
633                                         buildBrush->original = buildBrush;
634                                         buildBrush->contentShader = si;
635                                         buildBrush->compileFlags = si->compileFlags;
636                                         buildBrush->contentFlags = si->contentFlags;
637                                         buildBrush->detail = qtrue;
638
639                                         //snap points before using them for further calculations
640                                         //precision suffers a lot, when two of normal values are under .00025 (often no collision, knocking up effect in ioq3)
641                                         //also broken drawsurfs in case of normal brushes
642                                         snpd = qfalse;
643                                         for ( j=0; j<3; j++ )
644                                         {
645                                                 if ( fabs(plane[j]) < 0.00025 && fabs(plane[(j+1)%3]) < 0.00025 && ( plane[j] != 0.0 || plane[(j+1)%3] != 0.0 ) ){
646                                                         VectorAdd( points[ 0 ], points[ 1 ], cnt );
647                                                         VectorAdd( cnt, points[ 2 ], cnt );
648                                                         VectorScale( cnt, 0.3333333333333f, cnt );
649                                                         points[0][(j+2)%3]=points[1][(j+2)%3]=points[2][(j+2)%3]=cnt[(j+2)%3];
650                                                         snpd = qtrue;
651                                                         break;
652                                                 }
653                                         }
654
655                                         //snap pairs of points to prevent bad side planes
656                                         for ( j=0; j<3; j++ )
657                                         {
658                                                 VectorSubtract( points[j], points[(j+1)%3], nrm );
659                                                 VectorNormalize( nrm, nrm );
660                                                 for ( k=0; k<3; k++ )
661                                                 {
662                                                         if ( nrm[k] != 0.0 && fabs(nrm[k]) < 0.00025 ){
663                                                                 //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] );
664                                                                 points[j][k]=points[(j+1)%3][k] = ( points[j][k] + points[(j+1)%3][k] ) / 2.0;
665                                                                 //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] );
666                                                                 snpd = qtrue;
667                                                         }
668                                                 }
669                                         }
670
671                                         if ( snpd ) {
672                                                 PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] );
673                                                 snpd = qfalse;
674                                         }
675
676                                         //vector-is-close-to-be-on-axis check again, happens after previous code sometimes
677                                         for ( j=0; j<3; j++ )
678                                         {
679                                                 if ( fabs(plane[j]) < 0.00025 && fabs(plane[(j+1)%3]) < 0.00025 && ( plane[j] != 0.0 || plane[(j+1)%3] != 0.0 ) ){
680                                                         VectorAdd( points[ 0 ], points[ 1 ], cnt );
681                                                         VectorAdd( cnt, points[ 2 ], cnt );
682                                                         VectorScale( cnt, 0.3333333333333f, cnt );
683                                                         points[0][(j+2)%3]=points[1][(j+2)%3]=points[2][(j+2)%3]=cnt[(j+2)%3];
684                                                         PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] );
685                                                         break;
686                                                 }
687                                         }
688
689                                         //snap single snappable normal components
690                                         for ( j=0; j<3; j++ )
691                                         {
692                                                 if ( plane[j] != 0.0 && fabs(plane[j]) < 0.00005 ){
693                                                         plane[j]=0.0;
694                                                         snpd = qtrue;
695                                                 }
696                                         }
697
698                                         //adjust plane dist
699                                         if ( snpd ) {
700                                                 VectorAdd( points[ 0 ], points[ 1 ], cnt );
701                                                 VectorAdd( cnt, points[ 2 ], cnt );
702                                                 VectorScale( cnt, 0.3333333333333f, cnt );
703                                                 VectorNormalize( plane, plane );
704                                                 plane[3] = DotProduct( plane, cnt );
705
706                                                 //project points to resulting plane to keep intersections precision
707                                                 for ( j=0; j<3; j++ )
708                                                 {
709                                                         //Sys_Printf( "b4 %i (%6.7f %6.7f %6.7f)\n", j, points[j][0], points[j][1], points[j][2] );
710                                                         VectorMA( points[j], plane[3] - DotProduct( plane, points[j]), plane, points[j] );
711                                                         //Sys_Printf( "sn %i (%6.7f %6.7f %6.7f)\n", j, points[j][0], points[j][1], points[j][2] );
712                                                 }
713                                                 //Sys_Printf( "sn pln (%6.7f %6.7f %6.7f %6.7f)\n", plane[0], plane[1], plane[2], plane[3] );
714                                                 //PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] );
715                                                 //Sys_Printf( "pts pln (%6.7f %6.7f %6.7f %6.7f)\n", plane[0], plane[1], plane[2], plane[3] );
716                                         }
717
718
719                                         if ( spf == 4352 ){     //PYRAMIDAL_CLIP+AXIAL_BACKPLANE
720
721                                                 for ( j=0; j<3; j++ )
722                                                 {
723                                                         if ( fabs(plane[j]) < 0.05 && fabs(plane[(j+1)%3]) < 0.05 ){ //no way, close to lay on two axises
724                                                                 goto default_CLIPMODEL;
725                                                         }
726                                                 }
727
728                                                 // best axial normal
729                                                 VectorCopy( plane, bestNormal );
730                                                 for ( j = 0; j < 3; j++ ){
731                                                         if ( fabs(bestNormal[j]) > fabs(bestNormal[(j+1)%3]) ){
732                                                                 bestNormal[(j+1)%3] = 0.0;
733                                                                 axis = j;
734                                                         }
735                                                         else {
736                                                                 bestNormal[j] = 0.0;
737                                                         }
738                                                 }
739                                                 VectorNormalize( bestNormal, bestNormal );
740
741
742                                                 float bestdist, currdist, bestangle, currangle, mindist = 999999;
743
744                                                 for ( j = 0; j < 3; j++ ){//planes
745                                                         bestdist = 999999;
746                                                         bestangle = 1;
747                                                         for ( k = 0; k < 3; k++ ){//axises
748                                                                 VectorSubtract( points[ (j+1)%3 ], points[ j ], nrm );
749                                                                 if ( k == axis ){
750                                                                         CrossProduct( bestNormal, nrm, reverse );
751                                                                 }
752                                                                 else{
753                                                                         VectorClear( Vnorm[0] );
754                                                                         if ( (k+1)%3 == axis ){
755                                                                                 if ( nrm[ (k+2)%3 ] == 0 ) continue;
756                                                                                 Vnorm[0][ (k+2)%3 ] = nrm[ (k+2)%3 ];
757                                                                         }
758                                                                         else{
759                                                                                 if ( nrm[ (k+1)%3 ] == 0 ) continue;
760                                                                                 Vnorm[0][ (k+1)%3 ] = nrm[ (k+1)%3 ];
761                                                                         }
762                                                                         CrossProduct( bestNormal, Vnorm[0], Enorm[0] );
763                                                                         CrossProduct( Enorm[0], nrm, reverse );
764                                                                 }
765                                                                 VectorNormalize( reverse, reverse );
766                                                                 reverse[3] = DotProduct( points[ j ], reverse );
767                                                                 //check facing, thickness
768                                                                 currdist = reverse[3] - DotProduct( reverse, points[ (j+2)%3 ] );
769                                                                 currangle = DotProduct( reverse, plane );
770                                                                 if ( ( ( currdist > 0.1 ) && ( currdist < bestdist ) && ( currangle < 0 ) ) ||
771                                                                         ( ( currangle >= 0 ) && ( currangle <= bestangle ) ) ){
772                                                                         bestangle = currangle;
773                                                                         if ( currangle < 0 ) bestdist = currdist;
774                                                                         VectorCopy( reverse, p[j] );
775                                                                         p[j][3] = reverse[3];
776                                                                 }
777                                                         }
778                                                         //if ( bestdist == 999999 && bestangle == 1 ) Sys_Printf("default_CLIPMODEL\n");
779                                                         if ( bestdist == 999999 && bestangle == 1 ) goto default_CLIPMODEL;
780                                                         if ( bestdist < mindist ) mindist = bestdist;
781                                                 }
782                                                 if ( (limDepth != 0.0) && (mindist > limDepth) ) goto default_CLIPMODEL;
783
784
785 #if nonax_clip_dbg
786                                                 for ( j = 0; j < 3; j++ )
787                                                 {
788                                                         for ( k = 0; k < 3; k++ )
789                                                         {
790                                                                 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
791                                                                         Sys_Printf( "nonax nrm %6.17f %6.17f %6.17f\n", p[j][0], p[j][1], p[j][2] );
792                                                                 }
793                                                         }
794                                                 }
795 #endif
796                                                 /* set up brush sides */
797                                                 buildBrush->numsides = 4;
798                                                 buildBrush->sides[ 0 ].shaderInfo = si;
799                                                 for ( j = 1; j < buildBrush->numsides; j++ ) {
800                                                         if ( debugClip ) {
801                                                                 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
802                                                                 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
803                                                         }
804                                                         else {
805                                                                 buildBrush->sides[ j ].shaderInfo = NULL;  // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
806                                                         }
807                                                 }
808                                                 VectorCopy( points[0], points[3] ); // for cyclic usage
809
810                                                 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
811                                                 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
812                                                 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
813                                                 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]
814                                         }
815
816
817                                         else if ( ( spf == 16 ) ||      //EXTRUDE_TERRAIN
818                                                 ( spf == 512 ) ||       //EXTRUDE_DOWNWARDS
819                                                 ( spf == 1024 ) ||      //EXTRUDE_UPWARDS
820                                                 ( spf == 4096 ) ||      //default CLIPMODEL + AXIAL_BACKPLANE
821                                                 ( spf == 2064 ) ||      //EXTRUDE_TERRAIN+MAX_EXTRUDE
822                                                 ( spf == 4112 ) ||      //EXTRUDE_TERRAIN+AXIAL_BACKPLANE
823                                                 ( spf == 1536 ) ||      //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS
824                                                 ( spf == 2560 ) ||      //EXTRUDE_DOWNWARDS+MAX_EXTRUDE
825                                                 ( spf == 4608 ) ||      //EXTRUDE_DOWNWARDS+AXIAL_BACKPLANE
826                                                 ( spf == 3584 ) ||      //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS+MAX_EXTRUDE
827                                                 ( spf == 5632 ) ||      //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS+AXIAL_BACKPLANE
828                                                 ( spf == 3072 ) ||      //EXTRUDE_UPWARDS+MAX_EXTRUDE
829                                                 ( spf == 5120 ) ){      //EXTRUDE_UPWARDS+AXIAL_BACKPLANE
830
831                                                 if ( spawnFlags & 16 ){ //autodirection
832                                                         VectorCopy( avgDirection, bestNormal );
833                                                 }
834                                                 else{
835                                                         axis = 2;
836                                                         if ( ( spawnFlags & 1536 ) == 1536 ){ //up+down
837                                                                 VectorSet( bestNormal, 0, 0, ( plane[2] >= 0 ? 1 : -1 ) );
838                                                         }
839                                                         else if ( spawnFlags & 512 ){ //down
840                                                                 VectorSet( bestNormal, 0, 0, 1 );
841
842                                                         }
843                                                         else if ( spawnFlags & 1024 ){ //up
844                                                                 VectorSet( bestNormal, 0, 0, -1 );
845                                                         }
846                                                         else{ // best axial normal
847                                                                 VectorCopy( plane, bestNormal );
848                                                                 for ( j = 0; j < 3; j++ ){
849                                                                         if ( fabs(bestNormal[j]) > fabs(bestNormal[(j+1)%3]) ){
850                                                                                 bestNormal[(j+1)%3] = 0.0;
851                                                                                 axis = j;
852                                                                         }
853                                                                         else {
854                                                                                 bestNormal[j] = 0.0;
855                                                                         }
856                                                                 }
857                                                                 VectorNormalize( bestNormal, bestNormal );
858                                                         }
859                                                 }
860
861                                                 if ( DotProduct( plane, bestNormal ) < 0.05 ){
862                                                         goto default_CLIPMODEL;
863                                                 }
864
865
866                                                 /* make side planes */
867                                                 for ( j = 0; j < 3; j++ )
868                                                 {
869                                                         VectorSubtract( points[(j+1)%3], points[ j ], nrm );
870                                                         CrossProduct( bestNormal, nrm, p[ j ] );
871                                                         VectorNormalize( p[ j ], p[ j ] );
872                                                         p[j][3] = DotProduct( points[j], p[j] );
873                                                 }
874
875                                                 /* make back plane */
876                                                 if ( spawnFlags & 2048 ){ //max extrude
877                                                         VectorScale( bestNormal, -1.0f, reverse );
878                                                         if ( bestNormal[axis] > 0 ){
879                                                                 reverse[3] = -min[axis] + clipDepth;
880                                                         }
881                                                         else{
882                                                                 reverse[3] = max[axis] + clipDepth;
883                                                         }
884                                                 }
885                                                 else if ( spawnFlags & 4096 ){ //axial backplane
886                                                         VectorScale( bestNormal, -1.0f, reverse );
887                                                         reverse[3] = points[0][axis];
888                                                         if ( bestNormal[axis] > 0 ){
889                                                                 for ( j = 1; j < 3; j++ ){
890                                                                         if ( points[j][axis] < reverse[3] ){
891                                                                                 reverse[3] = points[j][axis];
892                                                                         }
893                                                                 }
894                                                                 reverse[3] = -reverse[3] + clipDepth;
895                                                         }
896                                                         else{
897                                                                 for ( j = 1; j < 3; j++ ){
898                                                                         if ( points[j][axis] > reverse[3] ){
899                                                                                 reverse[3] = points[j][axis];
900                                                                         }
901                                                                 }
902                                                                 reverse[3] += clipDepth;
903                                                         }
904                                                         if (limDepth != 0.0){
905                                                                 VectorCopy( points[0], cnt );
906                                                                 if ( bestNormal[axis] > 0 ){
907                                                                         for ( j = 1; j < 3; j++ ){
908                                                                                 if ( points[j][axis] > cnt[axis] ){
909                                                                                         VectorCopy( points[j], cnt );
910                                                                                 }
911                                                                         }
912                                                                 }
913                                                                 else {
914                                                                         for ( j = 1; j < 3; j++ ){
915                                                                                 if ( points[j][axis] < cnt[axis] ){
916                                                                                         VectorCopy( points[j], cnt );
917                                                                                 }
918                                                                         }
919                                                                 }
920                                                                 VectorMA( cnt, reverse[3] - DotProduct( reverse, cnt ), reverse, cnt );
921                                                                 if ( ( plane[3] - DotProduct( plane, cnt ) ) > limDepth ){
922                                                                         VectorScale( plane, -1.0f, reverse );
923                                                                         reverse[ 3 ] = -plane[ 3 ];
924                                                                         reverse[3] += clipDepth;
925                                                                 }
926                                                         }
927                                                 }
928                                                 else{   //normal backplane
929                                                         VectorScale( plane, -1.0f, reverse );
930                                                         reverse[ 3 ] = -plane[ 3 ];
931                                                         reverse[3] += clipDepth;
932                                                 }
933 #if nonax_clip_dbg
934                                                 for ( j = 0; j < 3; j++ )
935                                                 {
936                                                         for ( k = 0; k < 3; k++ )
937                                                         {
938                                                                 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
939                                                                         Sys_Printf( "nonax nrm %6.17f %6.17f %6.17f\n", p[j][0], p[j][1], p[j][2] );
940                                                                 }
941                                                         }
942                                                 }
943 #endif
944                                                 /* set up brush sides */
945                                                 buildBrush->numsides = 5;
946                                                 buildBrush->sides[ 0 ].shaderInfo = si;
947                                                 for ( j = 1; j < buildBrush->numsides; j++ ) {
948                                                         if ( debugClip ) {
949                                                                 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
950                                                                 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
951                                                         }
952                                                         else {
953                                                                 buildBrush->sides[ j ].shaderInfo = NULL;  // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
954                                                         }
955                                                 }
956                                                 VectorCopy( points[0], points[3] ); // for cyclic usage
957
958                                                 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
959                                                 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
960                                                 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
961                                                 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]
962                                                 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 0, NULL );
963                                         }
964
965
966                                         else if ( spf == 264 ){ //EXTRUDE_FACE_NORMALS+PYRAMIDAL_CLIP (extrude 45)
967
968                                                 //45 degrees normals for side planes
969                                                 for ( j = 0; j < 3; j++ )
970                                                 {
971                                                         VectorSubtract( points[(j+1)%3], points[ j ], nrm );
972                                                         CrossProduct( plane, nrm, Enorm[ j ] );
973                                                         VectorNormalize( Enorm[ j ], Enorm[ j ] );
974                                                         VectorAdd( plane, Enorm[ j ], Enorm[ j ] );
975                                                         VectorNormalize( Enorm[ j ], Enorm[ j ] );
976                                                         /* make side planes */
977                                                         CrossProduct( Enorm[ j ], nrm, p[ j ] );
978                                                         VectorNormalize( p[ j ], p[ j ] );
979                                                         p[j][3] = DotProduct( points[j], p[j] );
980                                                         //snap nearly axial side planes
981                                                         snpd = qfalse;
982                                                         for ( k = 0; k < 3; k++ )
983                                                         {
984                                                                 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
985                                                                         p[j][k] = 0.0;
986                                                                         snpd = qtrue;
987                                                                 }
988                                                         }
989                                                         if ( snpd ){
990                                                                 VectorNormalize( p[j], p[j] );
991                                                                 VectorAdd( points[j], points[(j+1)%3], cnt );
992                                                                 VectorScale( cnt, 0.5f, cnt );
993                                                                 p[j][3] = DotProduct( cnt, p[j] );
994                                                         }
995                                                 }
996
997                                                 /* make back plane */
998                                                 VectorScale( plane, -1.0f, reverse );
999                                                 reverse[ 3 ] = -plane[ 3 ];
1000                                                 reverse[3] += clipDepth;
1001
1002                                                 /* set up brush sides */
1003                                                 buildBrush->numsides = 5;
1004                                                 buildBrush->sides[ 0 ].shaderInfo = si;
1005                                                 for ( j = 1; j < buildBrush->numsides; j++ ) {
1006                                                         if ( debugClip ) {
1007                                                                 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
1008                                                                 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
1009                                                         }
1010                                                         else {
1011                                                                 buildBrush->sides[ j ].shaderInfo = NULL;  // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
1012                                                         }
1013                                                 }
1014                                                 VectorCopy( points[0], points[3] ); // for cyclic usage
1015
1016                                                 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
1017                                                 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
1018                                                 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
1019                                                 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]
1020                                                 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 0, NULL );
1021                                         }
1022
1023
1024                                         else if ( ( spf == 128 ) ||     //EXTRUDE_VERTEX_NORMALS
1025                                                 ( spf == 384 ) ){               //EXTRUDE_VERTEX_NORMALS + PYRAMIDAL_CLIP - vertex normals + don't check for sides, sticking outwards
1026                                                 /* get vertex normals */
1027                                                 for ( j = 0; j < 3; j++ )
1028                                                 {
1029                                                         /* get vertex */
1030                                                         dv = &ds->verts[ ds->indexes[ i + j ] ];
1031                                                         /* copy normal */
1032                                                         VectorCopy( dv->normal, Vnorm[ j ] );
1033                                                 }
1034
1035                                                 //avg normals for side planes
1036                                                 for ( j = 0; j < 3; j++ )
1037                                                 {
1038                                                         VectorAdd( Vnorm[ j ], Vnorm[ (j+1)%3 ], Enorm[ j ] );
1039                                                         VectorNormalize( Enorm[ j ], Enorm[ j ] );
1040                                                         //check fuer bad ones
1041                                                         VectorSubtract( points[(j+1)%3], points[ j ], cnt );
1042                                                         CrossProduct( plane, cnt, nrm );
1043                                                         VectorNormalize( nrm, nrm );
1044                                                         //check for negative or outside direction
1045                                                         if ( DotProduct( Enorm[ j ], plane ) > 0.1 ){
1046                                                                 if ( ( DotProduct( Enorm[ j ], nrm ) > -0.2 ) || ( spawnFlags & 256 ) ){
1047                                                                         //ok++;
1048                                                                         continue;
1049                                                                 }
1050                                                         }
1051                                                         //notok++;
1052                                                         //Sys_Printf( "faulty Enormal %i/%i\n", notok, ok );
1053                                                         //use 45 normal
1054                                                         VectorAdd( plane, nrm, Enorm[ j ] );
1055                                                         VectorNormalize( Enorm[ j ], Enorm[ j ] );
1056                                                 }
1057
1058                                                 /* make side planes */
1059                                                 for ( j = 0; j < 3; j++ )
1060                                                 {
1061                                                         VectorSubtract( points[(j+1)%3], points[ j ], nrm );
1062                                                         CrossProduct( Enorm[ j ], nrm, p[ j ] );
1063                                                         VectorNormalize( p[ j ], p[ j ] );
1064                                                         p[j][3] = DotProduct( points[j], p[j] );
1065                                                         //snap nearly axial side planes
1066                                                         snpd = qfalse;
1067                                                         for ( k = 0; k < 3; k++ )
1068                                                         {
1069                                                                 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
1070                                                                         //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]);
1071                                                                         p[j][k] = 0.0;
1072                                                                         snpd = qtrue;
1073                                                                 }
1074                                                         }
1075                                                         if ( snpd ){
1076                                                                 VectorNormalize( p[j], p[j] );
1077                                                                 //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]);
1078                                                                 VectorAdd( points[j], points[(j+1)%3], cnt );
1079                                                                 VectorScale( cnt, 0.5f, cnt );
1080                                                                 p[j][3] = DotProduct( cnt, p[j] );
1081                                                                 //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]);
1082                                                         }
1083                                                 }
1084
1085                                                 /* make back plane */
1086                                                 VectorScale( plane, -1.0f, reverse );
1087                                                 reverse[ 3 ] = -plane[ 3 ];
1088                                                 reverse[3] += clipDepth;
1089
1090                                                 /* set up brush sides */
1091                                                 buildBrush->numsides = 5;
1092                                                 buildBrush->sides[ 0 ].shaderInfo = si;
1093                                                 for ( j = 1; j < buildBrush->numsides; j++ ) {
1094                                                         if ( debugClip ) {
1095                                                                 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
1096                                                                 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
1097                                                         }
1098                                                         else {
1099                                                                 buildBrush->sides[ j ].shaderInfo = NULL;  // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
1100                                                         }
1101                                                 }
1102                                                 VectorCopy( points[0], points[3] ); // for cyclic usage
1103
1104                                                 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
1105                                                 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
1106                                                 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
1107                                                 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]
1108                                                 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 0, NULL );
1109                                         }
1110
1111
1112                                         else if ( spf == 8 ){   //EXTRUDE_FACE_NORMALS
1113
1114                                                 /* make side planes */
1115                                                 for ( j = 0; j < 3; j++ )
1116                                                 {
1117                                                         VectorSubtract( points[(j+1)%3], points[ j ], nrm );
1118                                                         CrossProduct( plane, nrm, p[ j ] );
1119                                                         VectorNormalize( p[ j ], p[ j ] );
1120                                                         p[j][3] = DotProduct( points[j], p[j] );
1121                                                         //snap nearly axial side planes
1122                                                         snpd = qfalse;
1123                                                         for ( k = 0; k < 3; k++ )
1124                                                         {
1125                                                                 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
1126                                                                         //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]);
1127                                                                         p[j][k] = 0.0;
1128                                                                         snpd = qtrue;
1129                                                                 }
1130                                                         }
1131                                                         if ( snpd ){
1132                                                                 VectorNormalize( p[j], p[j] );
1133                                                                 //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]);
1134                                                                 VectorAdd( points[j], points[(j+1)%3], cnt );
1135                                                                 VectorScale( cnt, 0.5f, cnt );
1136                                                                 p[j][3] = DotProduct( cnt, p[j] );
1137                                                                 //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]);
1138                                                         }
1139                                                 }
1140
1141                                                 /* make back plane */
1142                                                 VectorScale( plane, -1.0f, reverse );
1143                                                 reverse[ 3 ] = -plane[ 3 ];
1144                                                 reverse[3] += clipDepth;
1145 #if nonax_clip_dbg
1146                                                 for ( j = 0; j < 3; j++ )
1147                                                 {
1148                                                         for ( k = 0; k < 3; k++ )
1149                                                         {
1150                                                                 if ( fabs(p[j][k]) < 0.00005 && p[j][k] != 0.0 ){
1151                                                                         Sys_Printf( "nonax nrm %6.17f %6.17f %6.17f\n", p[j][0], p[j][1], p[j][2] );
1152                                                                         Sys_Printf( "frm src nrm %6.17f %6.17f %6.17f\n", plane[0], plane[1], plane[2]);
1153                                                                 }
1154                                                         }
1155                                                 }
1156 #endif
1157                                                 /* set up brush sides */
1158                                                 buildBrush->numsides = 5;
1159                                                 buildBrush->sides[ 0 ].shaderInfo = si;
1160                                                 for ( j = 1; j < buildBrush->numsides; j++ ) {
1161                                                         if ( debugClip ) {
1162                                                                 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
1163                                                                 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
1164                                                         }
1165                                                         else {
1166                                                                 buildBrush->sides[ j ].shaderInfo = NULL;  // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
1167                                                         }
1168                                                 }
1169                                                 VectorCopy( points[0], points[3] ); // for cyclic usage
1170
1171                                                 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
1172                                                 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
1173                                                 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
1174                                                 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]
1175                                                 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 0, NULL );
1176                                         }
1177
1178
1179                                         else if ( spf == 256 ){ //PYRAMIDAL_CLIP
1180
1181                                                 /* calculate center */
1182                                                 VectorAdd( points[ 0 ], points[ 1 ], cnt );
1183                                                 VectorAdd( cnt, points[ 2 ], cnt );
1184                                                 VectorScale( cnt, 0.3333333333333f, cnt );
1185
1186                                                 /* make back pyramid point */
1187                                                 VectorMA( cnt, -clipDepth, plane, cnt );
1188
1189                                                 /* make 3 more planes */
1190                                                 if( PlaneFromPoints( p[0], points[ 2 ], points[ 1 ], cnt ) &&
1191                                                         PlaneFromPoints( p[1], points[ 1 ], points[ 0 ], cnt ) &&
1192                                                         PlaneFromPoints( p[2], points[ 0 ], points[ 2 ], cnt ) ) {
1193
1194                                                         //check for dangerous planes
1195                                                         while( (( p[0][0] != 0.0 || p[0][1] != 0.0 ) && fabs(p[0][0]) < 0.00025 && fabs(p[0][1]) < 0.00025) ||
1196                                                                 (( p[0][0] != 0.0 || p[0][2] != 0.0 ) && fabs(p[0][0]) < 0.00025 && fabs(p[0][2]) < 0.00025) ||
1197                                                                 (( p[0][2] != 0.0 || p[0][1] != 0.0 ) && fabs(p[0][2]) < 0.00025 && fabs(p[0][1]) < 0.00025) ||
1198                                                                 (( p[1][0] != 0.0 || p[1][1] != 0.0 ) && fabs(p[1][0]) < 0.00025 && fabs(p[1][1]) < 0.00025) ||
1199                                                                 (( p[1][0] != 0.0 || p[1][2] != 0.0 ) && fabs(p[1][0]) < 0.00025 && fabs(p[1][2]) < 0.00025) ||
1200                                                                 (( p[1][2] != 0.0 || p[1][1] != 0.0 ) && fabs(p[1][2]) < 0.00025 && fabs(p[1][1]) < 0.00025) ||
1201                                                                 (( p[2][0] != 0.0 || p[2][1] != 0.0 ) && fabs(p[2][0]) < 0.00025 && fabs(p[2][1]) < 0.00025) ||
1202                                                                 (( p[2][0] != 0.0 || p[2][2] != 0.0 ) && fabs(p[2][0]) < 0.00025 && fabs(p[2][2]) < 0.00025) ||
1203                                                                 (( p[2][2] != 0.0 || p[2][1] != 0.0 ) && fabs(p[2][2]) < 0.00025 && fabs(p[2][1]) < 0.00025) ) {
1204                                                                 VectorMA( cnt, -0.1f, plane, cnt );
1205                                                                 //      Sys_Printf( "shifting pyramid point\n" );
1206                                                                 PlaneFromPoints( p[0], points[ 2 ], points[ 1 ], cnt );
1207                                                                 PlaneFromPoints( p[1], points[ 1 ], points[ 0 ], cnt );
1208                                                                 PlaneFromPoints( p[2], points[ 0 ], points[ 2 ], cnt );
1209                                                         }
1210 #if nonax_clip_dbg
1211                                                         for ( j = 0; j < 3; j++ )
1212                                                         {
1213                                                                 for ( k = 0; k < 3; k++ )
1214                                                                 {
1215                                                                         if ( fabs(p[j][k]) < 0.00005 && p[j][k] != 0.0 ){
1216                                                                                 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] );
1217                                                                         }
1218                                                                 }
1219                                                         }
1220 #endif
1221                                                         /* set up brush sides */
1222                                                         buildBrush->numsides = 4;
1223                                                         buildBrush->sides[ 0 ].shaderInfo = si;
1224                                                         for ( j = 1; j < buildBrush->numsides; j++ ) {
1225                                                                 if ( debugClip ) {
1226                                                                         buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
1227                                                                         buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
1228                                                                 }
1229                                                                 else {
1230                                                                         buildBrush->sides[ j ].shaderInfo = NULL;  // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
1231                                                                 }
1232                                                         }
1233                                                         VectorCopy( points[0], points[3] ); // for cyclic usage
1234
1235                                                         buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
1236                                                         buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 1 ] ); // p[0] contains points[1] and points[2]
1237                                                         buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 0 ] ); // p[1] contains points[0] and points[1]
1238                                                         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]
1239                                                 }
1240                                                 else
1241                                                 {
1242                                                         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 );
1243                                                         free( buildBrush );
1244                                                         continue;
1245                                                 }
1246                                         }
1247
1248
1249                                         else if ( ( si->clipModel && !( spf ) ) || ( ( spawnFlags & 8090 ) == 2 ) ){    //default CLIPMODEL
1250
1251                                                 default_CLIPMODEL:
1252                                                 // axial normal
1253                                                 VectorCopy( plane, bestNormal );
1254                                                 for ( j = 0; j < 3; j++ ){
1255                                                         if ( fabs(bestNormal[j]) > fabs(bestNormal[(j+1)%3]) ){
1256                                                                 bestNormal[(j+1)%3] = 0.0;
1257                                                         }
1258                                                         else {
1259                                                                 bestNormal[j] = 0.0;
1260                                                         }
1261                                                 }
1262                                                 VectorNormalize( bestNormal, bestNormal );
1263
1264                                                 /* make side planes */
1265                                                 for ( j = 0; j < 3; j++ )
1266                                                 {
1267                                                         VectorSubtract( points[(j+1)%3], points[ j ], nrm );
1268                                                         CrossProduct( bestNormal, nrm, p[ j ] );
1269                                                         VectorNormalize( p[ j ], p[ j ] );
1270                                                         p[j][3] = DotProduct( points[j], p[j] );
1271                                                 }
1272
1273                                                 /* make back plane */
1274                                                 VectorScale( plane, -1.0f, reverse );
1275                                                 reverse[ 3 ] = -plane[ 3 ];
1276                                                 reverse[3] += DotProduct( bestNormal, plane ) * clipDepth;
1277 #if nonax_clip_dbg
1278                                                 for ( j = 0; j < 3; j++ )
1279                                                 {
1280                                                         for ( k = 0; k < 3; k++ )
1281                                                         {
1282                                                                 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
1283                                                                         Sys_Printf( "nonax nrm %6.17f %6.17f %6.17f\n", p[j][0], p[j][1], p[j][2] );
1284                                                                 }
1285                                                         }
1286                                                 }
1287 #endif
1288                                                 /* set up brush sides */
1289                                                 buildBrush->numsides = 5;
1290                                                 buildBrush->sides[ 0 ].shaderInfo = si;
1291                                                 for ( j = 1; j < buildBrush->numsides; j++ ) {
1292                                                         if ( debugClip ) {
1293                                                                 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
1294                                                                 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
1295                                                         }
1296                                                         else {
1297                                                                 buildBrush->sides[ j ].shaderInfo = NULL;  // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
1298                                                         }
1299                                                 }
1300                                                 VectorCopy( points[0], points[3] ); // for cyclic usage
1301
1302                                                 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
1303                                                 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
1304                                                 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
1305                                                 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]
1306                                                 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 0, NULL );
1307                                         }
1308
1309
1310                                         /* add to entity */
1311                                         if ( CreateBrushWindings( buildBrush ) ) {
1312                                                 AddBrushBevels();
1313                                                 //%     EmitBrushes( buildBrush, NULL, NULL );
1314                                                 buildBrush->next = entities[ mapEntityNum ].brushes;
1315                                                 entities[ mapEntityNum ].brushes = buildBrush;
1316                                                 entities[ mapEntityNum ].numBrushes++;
1317                                         }
1318                                         else{
1319                                                 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 );
1320                                                 free( buildBrush );
1321                                         }
1322                                 }
1323                         }
1324                         normalEpsilon = normalEpsilon_save;
1325                 }
1326                 else if ( spawnFlags & 8090 ){
1327                         Sys_Printf( "WARNING: nonexistent clipping mode selected\n" );
1328                 }
1329         }
1330 }
1331
1332
1333
1334 /*
1335    AddTriangleModels()
1336    adds misc_model surfaces to the bsp
1337  */
1338
1339 void AddTriangleModels( entity_t *e ){
1340         int num, frame, skin, castShadows, recvShadows, spawnFlags;
1341         entity_t        *e2;
1342         const char      *targetName;
1343         const char      *target, *model, *value;
1344         char shader[ MAX_QPATH ];
1345         shaderInfo_t    *celShader;
1346         float temp, baseLightmapScale, lightmapScale, clipDepth;
1347         float shadeAngle;
1348         int lightmapSampleSize;
1349         vec3_t origin, scale, angles;
1350         m4x4_t transform;
1351         epair_t         *ep;
1352         remap_t         *remap, *remap2;
1353         char            *split;
1354
1355
1356         /* note it */
1357         Sys_FPrintf( SYS_VRB, "--- AddTriangleModels ---\n" );
1358
1359
1360         /* get current brush entity targetname */
1361         if ( e == entities ) {
1362                 targetName = "";
1363         }
1364         else
1365         {
1366                 targetName = ValueForKey( e, "targetname" );
1367
1368                 /* misc_model entities target non-worldspawn brush model entities */
1369                 if ( targetName[ 0 ] == '\0' ) {
1370                         return;
1371                 }
1372         }
1373
1374         /* get lightmap scale */
1375         /* vortex: added _ls key (short name of lightmapscale) */
1376         baseLightmapScale = 0.0f;
1377         if ( strcmp( "", ValueForKey( e, "lightmapscale" ) ) ||
1378                  strcmp( "", ValueForKey( e, "_lightmapscale" ) ) ||
1379                  strcmp( "", ValueForKey( e, "_ls" ) ) ) {
1380                 baseLightmapScale = FloatForKey( e, "lightmapscale" );
1381                 if ( baseLightmapScale <= 0.0f ) {
1382                         baseLightmapScale = FloatForKey( e, "_lightmapscale" );
1383                 }
1384                 if ( baseLightmapScale <= 0.0f ) {
1385                         baseLightmapScale = FloatForKey( e, "_ls" );
1386                 }
1387                 if ( baseLightmapScale < 0.0f ) {
1388                         baseLightmapScale = 0.0f;
1389                 }
1390                 if ( baseLightmapScale > 0.0f ) {
1391                         Sys_Printf( "World Entity has lightmap scale of %.4f\n", baseLightmapScale );
1392                 }
1393         }
1394
1395         /* walk the entity list */
1396         for ( num = 1; num < numEntities; num++ )
1397         {
1398                 /* get e2 */
1399                 e2 = &entities[ num ];
1400
1401                 /* convert misc_models into raw geometry */
1402                 if ( Q_stricmp( "misc_model", ValueForKey( e2, "classname" ) ) ) {
1403                         continue;
1404                 }
1405
1406                 /* ydnar: added support for md3 models on non-worldspawn models */
1407                 target = ValueForKey( e2, "target" );
1408                 if ( strcmp( target, targetName ) ) {
1409                         continue;
1410                 }
1411
1412                 /* get model name */
1413                 model = ValueForKey( e2, "model" );
1414                 if ( model[ 0 ] == '\0' ) {
1415                         Sys_FPrintf( SYS_WRN, "WARNING: misc_model at %i %i %i without a model key\n",
1416                                                 (int) origin[ 0 ], (int) origin[ 1 ], (int) origin[ 2 ] );
1417                         continue;
1418                 }
1419
1420                 /* get model frame */
1421                 frame = 0;
1422                 if ( strcmp( "", ValueForKey( e2, "_frame" ) ) ) {
1423                         frame = IntForKey( e2, "_frame" );
1424                 }
1425                 else if ( strcmp( "", ValueForKey( e2, "frame" ) ) ) {
1426                         frame = IntForKey( e2, "frame" );
1427                 }
1428
1429                 /* worldspawn (and func_groups) default to cast/recv shadows in worldspawn group */
1430                 if ( e == entities ) {
1431                         castShadows = WORLDSPAWN_CAST_SHADOWS;
1432                         recvShadows = WORLDSPAWN_RECV_SHADOWS;
1433                 }
1434
1435                 /* other entities don't cast any shadows, but recv worldspawn shadows */
1436                 else
1437                 {
1438                         castShadows = ENTITY_CAST_SHADOWS;
1439                         recvShadows = ENTITY_RECV_SHADOWS;
1440                 }
1441
1442                 /* get explicit shadow flags */
1443                 GetEntityShadowFlags( e2, e, &castShadows, &recvShadows );
1444
1445                 /* get spawnflags */
1446                 spawnFlags = IntForKey( e2, "spawnflags" );
1447
1448                 /* get origin */
1449                 GetVectorForKey( e2, "origin", origin );
1450                 VectorSubtract( origin, e->origin, origin );    /* offset by parent */
1451
1452                 /* get scale */
1453                 scale[ 0 ] = scale[ 1 ] = scale[ 2 ] = 1.0f;
1454                 temp = FloatForKey( e2, "modelscale" );
1455                 if ( temp != 0.0f ) {
1456                         scale[ 0 ] = scale[ 1 ] = scale[ 2 ] = temp;
1457                 }
1458                 value = ValueForKey( e2, "modelscale_vec" );
1459                 if ( value[ 0 ] != '\0' ) {
1460                         sscanf( value, "%f %f %f", &scale[ 0 ], &scale[ 1 ], &scale[ 2 ] );
1461                 }
1462
1463                 /* get "angle" (yaw) or "angles" (pitch yaw roll) */
1464                 angles[ 0 ] = angles[ 1 ] = angles[ 2 ] = 0.0f;
1465                 angles[ 2 ] = FloatForKey( e2, "angle" );
1466                 value = ValueForKey( e2, "angles" );
1467                 if ( value[ 0 ] != '\0' ) {
1468                         sscanf( value, "%f %f %f", &angles[ 1 ], &angles[ 2 ], &angles[ 0 ] );
1469                 }
1470
1471                 /* set transform matrix (thanks spog) */
1472                 m4x4_identity( transform );
1473                 m4x4_pivoted_transform_by_vec3( transform, origin, angles, eXYZ, scale, vec3_origin );
1474
1475                 /* get shader remappings */
1476                 remap = NULL;
1477                 for ( ep = e2->epairs; ep != NULL; ep = ep->next )
1478                 {
1479                         /* look for keys prefixed with "_remap" */
1480                         if ( ep->key != NULL && ep->value != NULL &&
1481                                  ep->key[ 0 ] != '\0' && ep->value[ 0 ] != '\0' &&
1482                                  !Q_strncasecmp( ep->key, "_remap", 6 ) ) {
1483                                 /* create new remapping */
1484                                 remap2 = remap;
1485                                 remap = safe_malloc( sizeof( *remap ) );
1486                                 remap->next = remap2;
1487                                 strcpy( remap->from, ep->value );
1488
1489                                 /* split the string */
1490                                 split = strchr( remap->from, ';' );
1491                                 if ( split == NULL ) {
1492                                         Sys_FPrintf( SYS_WRN, "WARNING: Shader _remap key found in misc_model without a ; character\n" );
1493                                         free( remap );
1494                                         remap = remap2;
1495                                         continue;
1496                                 }
1497
1498                                 /* store the split */
1499                                 *split = '\0';
1500                                 strcpy( remap->to, ( split + 1 ) );
1501
1502                                 /* note it */
1503                                 //%     Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", remap->from, remap->to );
1504                         }
1505                 }
1506
1507                 /* ydnar: cel shader support */
1508                 value = ValueForKey( e2, "_celshader" );
1509                 if ( value[ 0 ] == '\0' ) {
1510                         value = ValueForKey( &entities[ 0 ], "_celshader" );
1511                 }
1512                 if ( value[ 0 ] != '\0' ) {
1513                         sprintf( shader, "textures/%s", value );
1514                         celShader = ShaderInfoForShader( shader );
1515                 }
1516                 else{
1517                         celShader = *globalCelShader ? ShaderInfoForShader( globalCelShader ) : NULL;
1518                 }
1519
1520                 /* jal : entity based _samplesize */
1521                 lightmapSampleSize = 0;
1522                 if ( strcmp( "", ValueForKey( e2, "_lightmapsamplesize" ) ) ) {
1523                         lightmapSampleSize = IntForKey( e2, "_lightmapsamplesize" );
1524                 }
1525                 else if ( strcmp( "", ValueForKey( e2, "_samplesize" ) ) ) {
1526                         lightmapSampleSize = IntForKey( e2, "_samplesize" );
1527                 }
1528                 else if ( strcmp( "", ValueForKey( e2, "_ss" ) ) ) {
1529                         lightmapSampleSize = IntForKey( e2, "_ss" );
1530                 }
1531
1532                 if ( lightmapSampleSize < 0 ) {
1533                         lightmapSampleSize = 0;
1534                 }
1535
1536                 if ( lightmapSampleSize > 0.0f ) {
1537                         Sys_Printf( "misc_model has lightmap sample size of %.d\n", lightmapSampleSize );
1538                 }
1539
1540                 /* get lightmap scale */
1541                 /* vortex: added _ls key (short name of lightmapscale) */
1542                 lightmapScale = 0.0f;
1543                 if ( strcmp( "", ValueForKey( e2, "lightmapscale" ) ) ||
1544                          strcmp( "", ValueForKey( e2, "_lightmapscale" ) ) ||
1545                          strcmp( "", ValueForKey( e2, "_ls" ) ) ) {
1546                         lightmapScale = FloatForKey( e2, "lightmapscale" );
1547                         if ( lightmapScale <= 0.0f ) {
1548                                 lightmapScale = FloatForKey( e2, "_lightmapscale" );
1549                         }
1550                         if ( lightmapScale <= 0.0f ) {
1551                                 lightmapScale = FloatForKey( e2, "_ls" );
1552                         }
1553                         if ( lightmapScale < 0.0f ) {
1554                                 lightmapScale = 0.0f;
1555                         }
1556                         if ( lightmapScale > 0.0f ) {
1557                                 Sys_Printf( "misc_model has lightmap scale of %.4f\n", lightmapScale );
1558                         }
1559                 }
1560
1561                 /* jal : entity based _shadeangle */
1562                 shadeAngle = 0.0f;
1563                 if ( strcmp( "", ValueForKey( e2, "_shadeangle" ) ) ) {
1564                         shadeAngle = FloatForKey( e2, "_shadeangle" );
1565                 }
1566                 /* vortex' aliases */
1567                 else if ( strcmp( "", ValueForKey( e2, "_smoothnormals" ) ) ) {
1568                         shadeAngle = FloatForKey( e2, "_smoothnormals" );
1569                 }
1570                 else if ( strcmp( "", ValueForKey( e2, "_sn" ) ) ) {
1571                         shadeAngle = FloatForKey( e2, "_sn" );
1572                 }
1573                 else if ( strcmp( "", ValueForKey( e2, "_sa" ) ) ) {
1574                         shadeAngle = FloatForKey( e2, "_sa" );
1575                 }
1576                 else if ( strcmp( "", ValueForKey( e2, "_smooth" ) ) ) {
1577                         shadeAngle = FloatForKey( e2, "_smooth" );
1578                 }
1579
1580                 if ( shadeAngle < 0.0f ) {
1581                         shadeAngle = 0.0f;
1582                 }
1583
1584                 if ( shadeAngle > 0.0f ) {
1585                         Sys_Printf( "misc_model has shading angle of %.4f\n", shadeAngle );
1586                 }
1587
1588                 skin = 0;
1589                 if ( strcmp( "", ValueForKey( e2, "_skin" ) ) ) {
1590                         skin = IntForKey( e2, "_skin" );
1591                 }
1592                 else if ( strcmp( "", ValueForKey( e2, "skin" ) ) ) {
1593                         skin = IntForKey( e2, "skin" );
1594                 }
1595
1596                 clipDepth = clipDepthGlobal;
1597                 if ( strcmp( "", ValueForKey( e2, "_clipdepth" ) ) ) {
1598                         clipDepth = FloatForKey( e2, "_clipdepth" );
1599                         Sys_Printf( "misc_model has autoclip depth of %.3f\n", clipDepth );
1600                 }
1601
1602
1603                 /* insert the model */
1604                 InsertModel( model, skin, frame, transform, remap, celShader, mapEntityNum, castShadows, recvShadows, spawnFlags, lightmapScale, lightmapSampleSize, shadeAngle, clipDepth );
1605
1606                 /* free shader remappings */
1607                 while ( remap != NULL )
1608                 {
1609                         remap2 = remap->next;
1610                         free( remap );
1611                         remap = remap2;
1612                 }
1613         }
1614
1615 }