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