]> git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/model.c
Merge commit '70f0925f0000a132c709b935aa5cb0942b5080d9' into master-merge
[xonotic/netradiant.git] / tools / quake3 / q3map2 / model.c
1 /* -------------------------------------------------------------------------------
2
3    Copyright (C) 1999-2007 id Software, Inc. and contributors.
4    For a list of contributors, see the accompanying CONTRIBUTORS file.
5
6    This file is part of GtkRadiant.
7
8    GtkRadiant is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    GtkRadiant is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GtkRadiant; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21
22    ----------------------------------------------------------------------------------
23
24    This code has been altered significantly from its original form, to support
25    several games based on the Quake III Arena engine, in the form of "Q3Map2."
26
27    ------------------------------------------------------------------------------- */
28
29
30
31 /* marker */
32 #define MODEL_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /*
42    PicoPrintFunc()
43    callback for picomodel.lib
44  */
45
46 void PicoPrintFunc( int level, const char *str ){
47         if ( str == NULL ) {
48                 return;
49         }
50         switch ( level )
51         {
52         case PICO_NORMAL:
53                 Sys_Printf( "%s\n", str );
54                 break;
55
56         case PICO_VERBOSE:
57                 Sys_FPrintf( SYS_VRB, "%s\n", str );
58                 break;
59
60         case PICO_WARNING:
61                 Sys_FPrintf( SYS_WRN, "WARNING: %s\n", str );
62                 break;
63
64         case PICO_ERROR:
65                 Sys_FPrintf( SYS_ERR, "ERROR: %s\n", str );
66                 break;
67
68         case PICO_FATAL:
69                 Error( "ERROR: %s\n", str );
70                 break;
71         }
72 }
73
74
75
76 /*
77    PicoLoadFileFunc()
78    callback for picomodel.lib
79  */
80
81 void PicoLoadFileFunc( const char *name, byte **buffer, int *bufSize ){
82         *bufSize = vfsLoadFile( name, (void**) buffer, 0 );
83 }
84
85
86
87 /*
88    FindModel() - ydnar
89    finds an existing picoModel and returns a pointer to the picoModel_t struct or NULL if not found
90  */
91
92 picoModel_t *FindModel( const char *name, int frame ){
93         int i;
94
95
96         /* init */
97         if ( numPicoModels <= 0 ) {
98                 memset( picoModels, 0, sizeof( picoModels ) );
99         }
100
101         /* dummy check */
102         if ( name == NULL || name[ 0 ] == '\0' ) {
103                 return NULL;
104         }
105
106         /* search list */
107         for ( i = 0; i < MAX_MODELS; i++ )
108         {
109                 if ( picoModels[ i ] != NULL &&
110                          !strcmp( PicoGetModelName( picoModels[ i ] ), name ) &&
111                          PicoGetModelFrameNum( picoModels[ i ] ) == frame ) {
112                         return picoModels[ i ];
113                 }
114         }
115
116         /* no matching picoModel found */
117         return NULL;
118 }
119
120
121
122 /*
123    LoadModel() - ydnar
124    loads a picoModel and returns a pointer to the picoModel_t struct or NULL if not found
125  */
126
127 picoModel_t *LoadModel( const char *name, int frame ){
128         int i;
129         picoModel_t     *model, **pm;
130
131
132         /* init */
133         if ( numPicoModels <= 0 ) {
134                 memset( picoModels, 0, sizeof( picoModels ) );
135         }
136
137         /* dummy check */
138         if ( name == NULL || name[ 0 ] == '\0' ) {
139                 return NULL;
140         }
141
142         /* try to find existing picoModel */
143         model = FindModel( name, frame );
144         if ( model != NULL ) {
145                 return model;
146         }
147
148         /* none found, so find first non-null picoModel */
149         pm = NULL;
150         for ( i = 0; i < MAX_MODELS; i++ )
151         {
152                 if ( picoModels[ i ] == NULL ) {
153                         pm = &picoModels[ i ];
154                         break;
155                 }
156         }
157
158         /* too many picoModels? */
159         if ( pm == NULL ) {
160                 Error( "MAX_MODELS (%d) exceeded, there are too many model files referenced by the map.", MAX_MODELS );
161         }
162
163         /* attempt to parse model */
164         *pm = PicoLoadModel( name, frame );
165
166         /* if loading failed, make a bogus model to silence the rest of the warnings */
167         if ( *pm == NULL ) {
168                 /* allocate a new model */
169                 *pm = PicoNewModel();
170                 if ( *pm == NULL ) {
171                         return NULL;
172                 }
173
174                 /* set data */
175                 PicoSetModelName( *pm, name );
176                 PicoSetModelFrameNum( *pm, frame );
177         }
178
179         /* debug code */
180         #if 0
181         {
182                 int numSurfaces, numVertexes;
183                 picoSurface_t   *ps;
184
185
186                 Sys_Printf( "Model %s\n", name );
187                 numSurfaces = PicoGetModelNumSurfaces( *pm );
188                 for ( i = 0; i < numSurfaces; i++ )
189                 {
190                         ps = PicoGetModelSurface( *pm, i );
191                         numVertexes = PicoGetSurfaceNumVertexes( ps );
192                         Sys_Printf( "Surface %d has %d vertexes\n", i, numVertexes );
193                 }
194         }
195         #endif
196
197         /* set count */
198         if ( *pm != NULL ) {
199                 numPicoModels++;
200         }
201
202         /* return the picoModel */
203         return *pm;
204 }
205
206
207
208 /*
209    InsertModel() - ydnar
210    adds a picomodel into the bsp
211  */
212
213 void InsertModel( const char *name, int skin, int frame, m4x4_t transform, remap_t *remap, shaderInfo_t *celShader, int eNum, int castShadows, int recvShadows, int spawnFlags, float lightmapScale, int lightmapSampleSize, float shadeAngle ){
214         int i, j, s, 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         double normalEpsilon_save;
230         double distanceEpsilon_save;
231         char skinfilename[ MAX_QPATH ];
232         char                *skinfilecontent;
233         int skinfilesize;
234         char                *skinfileptr, *skinfilenextptr;
235
236
237         /* get model */
238         model = LoadModel( name, frame );
239         if ( model == NULL ) {
240                 return;
241         }
242
243         /* load skin file */
244         snprintf( skinfilename, sizeof( skinfilename ), "%s_%d.skin", name, skin );
245         skinfilename[sizeof( skinfilename ) - 1] = 0;
246         skinfilesize = vfsLoadFile( skinfilename, (void**) &skinfilecontent, 0 );
247         if ( skinfilesize < 0 && skin != 0 ) {
248                 /* fallback to skin 0 if invalid */
249                 snprintf( skinfilename, sizeof( skinfilename ), "%s_0.skin", name );
250                 skinfilename[sizeof( skinfilename ) - 1] = 0;
251                 skinfilesize = vfsLoadFile( skinfilename, (void**) &skinfilecontent, 0 );
252                 if ( skinfilesize >= 0 ) {
253                         Sys_Printf( "Skin %d of %s does not exist, using 0 instead\n", skin, name );
254                 }
255         }
256         sf = NULL;
257         if ( skinfilesize >= 0 ) {
258                 Sys_Printf( "Using skin %d of %s\n", skin, name );
259                 int pos;
260                 for ( skinfileptr = skinfilecontent; *skinfileptr; skinfileptr = skinfilenextptr )
261                 {
262                         // for fscanf
263                         char format[64];
264
265                         skinfilenextptr = strchr( skinfileptr, '\r' );
266                         if ( skinfilenextptr ) {
267                                 *skinfilenextptr++ = 0;
268                         }
269                         else
270                         {
271                                 skinfilenextptr = strchr( skinfileptr, '\n' );
272                                 if ( skinfilenextptr ) {
273                                         *skinfilenextptr++ = 0;
274                                 }
275                                 else{
276                                         skinfilenextptr = skinfileptr + strlen( skinfileptr );
277                                 }
278                         }
279
280                         /* create new item */
281                         sf2 = sf;
282                         sf = safe_malloc( sizeof( *sf ) );
283                         sf->next = sf2;
284
285                         sprintf( format, "replace %%%ds %%%ds", (int)sizeof( sf->name ) - 1, (int)sizeof( sf->to ) - 1 );
286                         if ( sscanf( skinfileptr, format, sf->name, sf->to ) == 2 ) {
287                                 continue;
288                         }
289                         sprintf( format, " %%%d[^,  ] ,%%%ds", (int)sizeof( sf->name ) - 1, (int)sizeof( sf->to ) - 1 );
290                         if ( ( pos = sscanf( skinfileptr, format, sf->name, sf->to ) ) == 2 ) {
291                                 continue;
292                         }
293
294                         /* invalid input line -> discard sf struct */
295                         Sys_Printf( "Discarding skin directive in %s: %s\n", skinfilename, skinfileptr );
296                         free( sf );
297                         sf = sf2;
298                 }
299                 free( skinfilecontent );
300         }
301
302         /* handle null matrix */
303         if ( transform == NULL ) {
304                 m4x4_identity( identity );
305                 transform = identity;
306         }
307
308         /* hack: Stable-1_2 and trunk have differing row/column major matrix order
309            this transpose is necessary with Stable-1_2
310            uncomment the following line with old m4x4_t (non 1.3/spog_branch) code */
311         //%     m4x4_transpose( transform );
312
313         /* create transform matrix for normals */
314         memcpy( nTransform, transform, sizeof( m4x4_t ) );
315         if ( m4x4_invert( nTransform ) ) {
316                 Sys_FPrintf( SYS_VRB, "WARNING: Can't invert model transform matrix, using transpose instead\n" );
317         }
318         m4x4_transpose( nTransform );
319
320         /* fix bogus lightmap scale */
321         if ( lightmapScale <= 0.0f ) {
322                 lightmapScale = 1.0f;
323         }
324
325         /* fix bogus shade angle */
326         if ( shadeAngle <= 0.0f ) {
327                 shadeAngle = 0.0f;
328         }
329
330         /* each surface on the model will become a new map drawsurface */
331         numSurfaces = PicoGetModelNumSurfaces( model );
332         //%     Sys_FPrintf( SYS_VRB, "Model %s has %d surfaces\n", name, numSurfaces );
333         for ( s = 0; s < numSurfaces; s++ )
334         {
335                 /* get surface */
336                 surface = PicoGetModelSurface( model, s );
337                 if ( surface == NULL ) {
338                         continue;
339                 }
340
341                 /* only handle triangle surfaces initially (fixme: support patches) */
342                 if ( PicoGetSurfaceType( surface ) != PICO_TRIANGLES ) {
343                         continue;
344                 }
345
346                 /* get shader name */
347                 shader = PicoGetSurfaceShader( surface );
348                 if ( shader == NULL ) {
349                         picoShaderName = "";
350                 }
351                 else{
352                         picoShaderName = PicoGetShaderName( shader );
353                 }
354
355                 /* handle .skin file */
356                 if ( sf ) {
357                         picoShaderName = NULL;
358                         for ( sf2 = sf; sf2 != NULL; sf2 = sf2->next )
359                         {
360                                 if ( !Q_stricmp( surface->name, sf2->name ) ) {
361                                         Sys_FPrintf( SYS_VRB, "Skin file: mapping %s to %s\n", surface->name, sf2->to );
362                                         picoShaderName = sf2->to;
363                                         break;
364                                 }
365                         }
366                         if ( !picoShaderName ) {
367                                 Sys_FPrintf( SYS_VRB, "Skin file: not mapping %s\n", surface->name );
368                                 continue;
369                         }
370                 }
371
372                 /* handle shader remapping */
373                 glob = NULL;
374                 for ( rm = remap; rm != NULL; rm = rm->next )
375                 {
376                         if ( rm->from[ 0 ] == '*' && rm->from[ 1 ] == '\0' ) {
377                                 glob = rm;
378                         }
379                         else if ( !Q_stricmp( picoShaderName, rm->from ) ) {
380                                 Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", picoShaderName, rm->to );
381                                 picoShaderName = rm->to;
382                                 glob = NULL;
383                                 break;
384                         }
385                 }
386
387                 if ( glob != NULL ) {
388                         Sys_FPrintf( SYS_VRB, "Globbing %s to %s\n", picoShaderName, glob->to );
389                         picoShaderName = glob->to;
390                 }
391
392                 /* shader renaming for sof2 */
393                 if ( renameModelShaders ) {
394                         strcpy( shaderName, picoShaderName );
395                         StripExtension( shaderName );
396                         if ( spawnFlags & 1 ) {
397                                 strcat( shaderName, "_RMG_BSP" );
398                         }
399                         else{
400                                 strcat( shaderName, "_BSP" );
401                         }
402                         si = ShaderInfoForShader( shaderName );
403                 }
404                 else{
405                         si = ShaderInfoForShader( picoShaderName );
406                 }
407
408                 /* allocate a surface (ydnar: gs mods) */
409                 ds = AllocDrawSurface( SURFACE_TRIANGLES );
410                 ds->entityNum = eNum;
411                 ds->castShadows = castShadows;
412                 ds->recvShadows = recvShadows;
413
414                 /* set shader */
415                 ds->shaderInfo = si;
416
417                 /* force to meta? */
418                 if ( ( si != NULL && si->forceMeta ) || ( spawnFlags & 4 ) ) { /* 3rd bit */
419                         ds->type = SURFACE_FORCED_META;
420                 }
421 /*              else
422                         {
423                                 //fix not requested lightmapping of models :E
424                                 // else force vertexlit
425                                 //      ApplySurfaceParm( "pointlight", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
426                                 //   si->compileFlags |= C_VERTEXLIT;
427                                 //ds->type == SURFACE_TRIANGLES;
428                         }
429 */
430                 /* fix the surface's normals (jal: conditioned by shader info) */
431                 if ( !( spawnFlags & 64 ) && ( shadeAngle == 0.0f || ds->type != SURFACE_FORCED_META ) ) {
432                         PicoFixSurfaceNormals( surface );
433                 }
434
435                 /* set sample size */
436                 if ( lightmapSampleSize > 0.0f ) {
437                         ds->sampleSize = lightmapSampleSize;
438                 }
439
440                 /* set lightmap scale */
441                 if ( lightmapScale > 0.0f ) {
442                         ds->lightmapScale = lightmapScale;
443                 }
444
445                 /* set shading angle */
446                 if ( shadeAngle > 0.0f ) {
447                         ds->shadeAngleDegrees = shadeAngle;
448                 }
449
450                 /* set particulars */
451                 ds->numVerts = PicoGetSurfaceNumVertexes( surface );
452                 ds->verts = safe_malloc0( ds->numVerts * sizeof( ds->verts[ 0 ] ) );
453
454                 ds->numIndexes = PicoGetSurfaceNumIndexes( surface );
455                 ds->indexes = safe_malloc0( ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
456
457                 /* copy vertexes */
458                 for ( i = 0; i < ds->numVerts; i++ )
459                 {
460                         /* get vertex */
461                         dv = &ds->verts[ i ];
462
463                         /* xyz and normal */
464                         xyz = PicoGetSurfaceXYZ( surface, i );
465                         VectorCopy( xyz, dv->xyz );
466                         m4x4_transform_point( transform, dv->xyz );
467
468                         normal = PicoGetSurfaceNormal( surface, i );
469                         VectorCopy( normal, dv->normal );
470                         m4x4_transform_normal( nTransform, dv->normal );
471                         VectorNormalize( dv->normal, dv->normal );
472
473                         /* ydnar: tek-fu celshading support for flat shaded shit */
474                         if ( flat ) {
475                                 dv->st[ 0 ] = si->stFlat[ 0 ];
476                                 dv->st[ 1 ] = si->stFlat[ 1 ];
477                         }
478
479                         /* ydnar: gs mods: added support for explicit shader texcoord generation */
480                         else if ( si->tcGen ) {
481                                 /* project the texture */
482                                 dv->st[ 0 ] = DotProduct( si->vecs[ 0 ], dv->xyz );
483                                 dv->st[ 1 ] = DotProduct( si->vecs[ 1 ], dv->xyz );
484                         }
485
486                         /* normal texture coordinates */
487                         else
488                         {
489                                 st = PicoGetSurfaceST( surface, 0, i );
490                                 dv->st[ 0 ] = st[ 0 ];
491                                 dv->st[ 1 ] = st[ 1 ];
492                         }
493
494                         /* set lightmap/color bits */
495                         color = PicoGetSurfaceColor( surface, 0, i );
496                         for ( j = 0; j < MAX_LIGHTMAPS; j++ )
497                         {
498                                 dv->lightmap[ j ][ 0 ] = 0.0f;
499                                 dv->lightmap[ j ][ 1 ] = 0.0f;
500                                 if ( spawnFlags & 32 ) { // spawnflag 32: model color -> alpha hack
501                                         dv->color[ j ][ 0 ] = 255.0f;
502                                         dv->color[ j ][ 1 ] = 255.0f;
503                                         dv->color[ j ][ 2 ] = 255.0f;
504                                         dv->color[ j ][ 3 ] = RGBTOGRAY( color );
505                                 }
506                                 else
507                                 {
508                                         dv->color[ j ][ 0 ] = color[ 0 ];
509                                         dv->color[ j ][ 1 ] = color[ 1 ];
510                                         dv->color[ j ][ 2 ] = color[ 2 ];
511                                         dv->color[ j ][ 3 ] = color[ 3 ];
512                                 }
513                         }
514                 }
515
516                 /* copy indexes */
517                 indexes = PicoGetSurfaceIndexes( surface, 0 );
518                 for ( i = 0; i < ds->numIndexes; i++ )
519                         ds->indexes[ i ] = indexes[ i ];
520
521                 /* set cel shader */
522                 ds->celShader = celShader;
523
524                 /* ydnar: giant hack land: generate clipping brushes for model triangles */
525                 if ( si->clipModel || ( spawnFlags & 2 ) ) { /* 2nd bit */
526                         vec3_t points[ 4 ], backs[ 3 ];
527                         vec4_t plane, reverse, pa, pb, pc;
528
529
530                         /* temp hack */
531                         if ( !si->clipModel && !( si->compileFlags & C_SOLID ) ) {
532                                 continue;
533                         }
534
535                         /* walk triangle list */
536                         for ( i = 0; i < ds->numIndexes; i += 3 )
537                         {
538                                 /* overflow hack */
539                                 AUTOEXPAND_BY_REALLOC( mapplanes, ( nummapplanes + 64 ) << 1, allocatedmapplanes, 1024 );
540
541                                 /* make points and back points */
542                                 for ( j = 0; j < 3; j++ )
543                                 {
544                                         /* get vertex */
545                                         dv = &ds->verts[ ds->indexes[ i + j ] ];
546
547                                         /* copy xyz */
548                                         VectorCopy( dv->xyz, points[ j ] );
549                                 }
550
551                                 VectorCopy( points[0], points[3] ); // for cyclic usage
552
553                                 /* make plane for triangle */
554                                 // div0: add some extra spawnflags:
555                                 //   0: snap normals to axial planes for extrusion
556                                 //   8: extrude with the original normals
557                                 //  16: extrude only with up/down normals (ideal for terrain)
558                                 //  24: extrude by distance zero (may need engine changes)
559                                 if ( PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] ) ) {
560                                         vec3_t bestNormal;
561                                         float backPlaneDistance = 2;
562
563                                         if ( spawnFlags & 8 ) { // use a DOWN normal
564                                                 if ( spawnFlags & 16 ) {
565                                                         // 24: normal as is, and zero width (broken)
566                                                         VectorCopy( plane, bestNormal );
567                                                 }
568                                                 else
569                                                 {
570                                                         // 8: normal as is
571                                                         VectorCopy( plane, bestNormal );
572                                                 }
573                                         }
574                                         else
575                                         {
576                                                 if ( spawnFlags & 16 ) {
577                                                         // 16: UP/DOWN normal
578                                                         VectorSet( bestNormal, 0, 0, ( plane[2] >= 0 ? 1 : -1 ) );
579                                                 }
580                                                 else
581                                                 {
582                                                         // 0: axial normal
583                                                         if ( fabs( plane[0] ) > fabs( plane[1] ) ) { // x>y
584                                                                 if ( fabs( plane[1] ) > fabs( plane[2] ) ) { // x>y, y>z
585                                                                         VectorSet( bestNormal, ( plane[0] >= 0 ? 1 : -1 ), 0, 0 );
586                                                                 }
587                                                                 else // x>y, z>=y
588                                                                 if ( fabs( plane[0] ) > fabs( plane[2] ) ) { // x>z, z>=y
589                                                                         VectorSet( bestNormal, ( plane[0] >= 0 ? 1 : -1 ), 0, 0 );
590                                                                 }
591                                                                 else{    // z>=x, x>y
592                                                                         VectorSet( bestNormal, 0, 0, ( plane[2] >= 0 ? 1 : -1 ) );
593                                                                 }
594                                                         }
595                                                         else // y>=x
596                                                         if ( fabs( plane[1] ) > fabs( plane[2] ) ) { // y>z, y>=x
597                                                                 VectorSet( bestNormal, 0, ( plane[1] >= 0 ? 1 : -1 ), 0 );
598                                                         }
599                                                         else{    // z>=y, y>=x
600                                                                 VectorSet( bestNormal, 0, 0, ( plane[2] >= 0 ? 1 : -1 ) );
601                                                         }
602                                                 }
603                                         }
604
605                                         /* build a brush */
606                                         buildBrush = AllocBrush( 48 );
607                                         buildBrush->entityNum = mapEntityNum;
608                                         buildBrush->original = buildBrush;
609                                         buildBrush->contentShader = si;
610                                         buildBrush->compileFlags = si->compileFlags;
611                                         buildBrush->contentFlags = si->contentFlags;
612                                         normalEpsilon_save = normalEpsilon;
613                                         distanceEpsilon_save = distanceEpsilon;
614                                         if ( si->compileFlags & C_STRUCTURAL ) { // allow forced structural brushes here
615                                                 buildBrush->detail = qfalse;
616
617                                                 // only allow EXACT matches when snapping for these (this is mostly for caulk brushes inside a model)
618                                                 if ( normalEpsilon > 0 ) {
619                                                         normalEpsilon = 0;
620                                                 }
621                                                 if ( distanceEpsilon > 0 ) {
622                                                         distanceEpsilon = 0;
623                                                 }
624                                         }
625                                         else{
626                                                 buildBrush->detail = qtrue;
627                                         }
628
629                                         /* regenerate back points */
630                                         for ( j = 0; j < 3; j++ )
631                                         {
632                                                 /* get vertex */
633                                                 dv = &ds->verts[ ds->indexes[ i + j ] ];
634
635                                                 // shift by some units
636                                                 VectorMA( dv->xyz, -64.0f, bestNormal, backs[j] ); // 64 prevents roundoff errors a bit
637                                         }
638
639                                         /* make back plane */
640                                         VectorScale( plane, -1.0f, reverse );
641                                         reverse[ 3 ] = -plane[ 3 ];
642                                         if ( ( spawnFlags & 24 ) != 24 ) {
643                                                 reverse[3] += DotProduct( bestNormal, plane ) * backPlaneDistance;
644                                         }
645                                         // that's at least sqrt(1/3) backPlaneDistance, unless in DOWN mode; in DOWN mode, we are screwed anyway if we encounter a plane that's perpendicular to the xy plane)
646
647                                         if ( PlaneFromPoints( pa, points[ 2 ], points[ 1 ], backs[ 1 ] ) &&
648                                                  PlaneFromPoints( pb, points[ 1 ], points[ 0 ], backs[ 0 ] ) &&
649                                                  PlaneFromPoints( pc, points[ 0 ], points[ 2 ], backs[ 2 ] ) ) {
650                                                 /* set up brush sides */
651                                                 buildBrush->numsides = 5;
652                                                 buildBrush->sides[ 0 ].shaderInfo = si;
653                                                 for ( j = 1; j < buildBrush->numsides; j++ )
654                                                         buildBrush->sides[ j ].shaderInfo = NULL;  // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
655
656                                                 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
657                                                 buildBrush->sides[ 1 ].planenum = FindFloatPlane( pa, pa[ 3 ], 2, &points[ 1 ] ); // pa contains points[1] and points[2]
658                                                 buildBrush->sides[ 2 ].planenum = FindFloatPlane( pb, pb[ 3 ], 2, &points[ 0 ] ); // pb contains points[0] and points[1]
659                                                 buildBrush->sides[ 3 ].planenum = FindFloatPlane( pc, pc[ 3 ], 2, &points[ 2 ] ); // pc contains points[2] and points[0] (copied to points[3]
660                                                 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 3, backs );
661                                         }
662                                         else
663                                         {
664                                                 free( buildBrush );
665                                                 continue;
666                                         }
667
668                                         normalEpsilon = normalEpsilon_save;
669                                         distanceEpsilon = distanceEpsilon_save;
670
671                                         /* add to entity */
672                                         if ( CreateBrushWindings( buildBrush ) ) {
673                                                 AddBrushBevels();
674                                                 //%     EmitBrushes( buildBrush, NULL, NULL );
675                                                 buildBrush->next = entities[ mapEntityNum ].brushes;
676                                                 entities[ mapEntityNum ].brushes = buildBrush;
677                                                 entities[ mapEntityNum ].numBrushes++;
678                                         }
679                                         else{
680                                                 free( buildBrush );
681                                         }
682                                 }
683                         }
684                 }
685         }
686 }
687
688
689
690 /*
691    AddTriangleModels()
692    adds misc_model surfaces to the bsp
693  */
694
695 void AddTriangleModels( entity_t *e ){
696         int num, frame, skin, castShadows, recvShadows, spawnFlags;
697         entity_t        *e2;
698         const char      *targetName;
699         const char      *target, *model, *value;
700         char shader[ MAX_QPATH ];
701         shaderInfo_t    *celShader;
702         float temp, baseLightmapScale, lightmapScale;
703         float shadeAngle;
704         int lightmapSampleSize;
705         vec3_t origin, scale, angles;
706         m4x4_t transform;
707         epair_t         *ep;
708         remap_t         *remap, *remap2;
709         char            *split;
710
711
712         /* note it */
713         Sys_FPrintf( SYS_VRB, "--- AddTriangleModels ---\n" );
714
715         /* get current brush entity targetname */
716         if ( e == entities ) {
717                 targetName = "";
718         }
719         else
720         {
721                 targetName = ValueForKey( e, "targetname" );
722
723                 /* misc_model entities target non-worldspawn brush model entities */
724                 if ( targetName[ 0 ] == '\0' ) {
725                         return;
726                 }
727         }
728
729         /* get lightmap scale */
730         /* vortex: added _ls key (short name of lightmapscale) */
731         baseLightmapScale = 0.0f;
732         if ( strcmp( "", ValueForKey( e, "lightmapscale" ) ) ||
733                  strcmp( "", ValueForKey( e, "_lightmapscale" ) ) ||
734                  strcmp( "", ValueForKey( e, "_ls" ) ) ) {
735                 baseLightmapScale = FloatForKey( e, "lightmapscale" );
736                 if ( baseLightmapScale <= 0.0f ) {
737                         baseLightmapScale = FloatForKey( e, "_lightmapscale" );
738                 }
739                 if ( baseLightmapScale <= 0.0f ) {
740                         baseLightmapScale = FloatForKey( e, "_ls" );
741                 }
742                 if ( baseLightmapScale < 0.0f ) {
743                         baseLightmapScale = 0.0f;
744                 }
745                 if ( baseLightmapScale > 0.0f ) {
746                         Sys_Printf( "World Entity has lightmap scale of %.4f\n", baseLightmapScale );
747                 }
748         }
749
750         /* walk the entity list */
751         for ( num = 1; num < numEntities; num++ )
752         {
753                 /* get e2 */
754                 e2 = &entities[ num ];
755
756                 /* convert misc_models into raw geometry */
757                 if ( Q_stricmp( "misc_model", ValueForKey( e2, "classname" ) ) ) {
758                         continue;
759                 }
760
761                 /* ydnar: added support for md3 models on non-worldspawn models */
762                 target = ValueForKey( e2, "target" );
763                 if ( strcmp( target, targetName ) ) {
764                         continue;
765                 }
766
767                 /* get model name */
768                 model = ValueForKey( e2, "model" );
769                 if ( model[ 0 ] == '\0' ) {
770                         Sys_FPrintf( SYS_WRN, "WARNING: misc_model at %i %i %i without a model key\n",
771                                                 (int) origin[ 0 ], (int) origin[ 1 ], (int) origin[ 2 ] );
772                         continue;
773                 }
774
775                 /* get model frame */
776                 frame = 0;
777                 if ( strcmp( "", ValueForKey( e2, "_frame" ) ) ) {
778                         frame = IntForKey( e2, "_frame" );
779                 }
780                 else if ( strcmp( "", ValueForKey( e2, "frame" ) ) ) {
781                         frame = IntForKey( e2, "frame" );
782                 }
783
784                 /* worldspawn (and func_groups) default to cast/recv shadows in worldspawn group */
785                 if ( e == entities ) {
786                         castShadows = WORLDSPAWN_CAST_SHADOWS;
787                         recvShadows = WORLDSPAWN_RECV_SHADOWS;
788                 }
789
790                 /* other entities don't cast any shadows, but recv worldspawn shadows */
791                 else
792                 {
793                         castShadows = ENTITY_CAST_SHADOWS;
794                         recvShadows = ENTITY_RECV_SHADOWS;
795                 }
796
797                 /* get explicit shadow flags */
798                 GetEntityShadowFlags( e2, e, &castShadows, &recvShadows );
799
800                 /* get spawnflags */
801                 spawnFlags = IntForKey( e2, "spawnflags" );
802
803                 /* get origin */
804                 GetVectorForKey( e2, "origin", origin );
805                 VectorSubtract( origin, e->origin, origin );    /* offset by parent */
806
807                 /* get scale */
808                 scale[ 0 ] = scale[ 1 ] = scale[ 2 ] = 1.0f;
809                 temp = FloatForKey( e2, "modelscale" );
810                 if ( temp != 0.0f ) {
811                         scale[ 0 ] = scale[ 1 ] = scale[ 2 ] = temp;
812                 }
813                 value = ValueForKey( e2, "modelscale_vec" );
814                 if ( value[ 0 ] != '\0' ) {
815                         sscanf( value, "%f %f %f", &scale[ 0 ], &scale[ 1 ], &scale[ 2 ] );
816                 }
817
818                 /* get "angle" (yaw) or "angles" (pitch yaw roll) */
819                 angles[ 0 ] = angles[ 1 ] = angles[ 2 ] = 0.0f;
820                 angles[ 2 ] = FloatForKey( e2, "angle" );
821                 value = ValueForKey( e2, "angles" );
822                 if ( value[ 0 ] != '\0' ) {
823                         sscanf( value, "%f %f %f", &angles[ 1 ], &angles[ 2 ], &angles[ 0 ] );
824                 }
825
826                 /* set transform matrix (thanks spog) */
827                 m4x4_identity( transform );
828                 m4x4_pivoted_transform_by_vec3( transform, origin, angles, eXYZ, scale, vec3_origin );
829
830                 /* get shader remappings */
831                 remap = NULL;
832                 for ( ep = e2->epairs; ep != NULL; ep = ep->next )
833                 {
834                         /* look for keys prefixed with "_remap" */
835                         if ( ep->key != NULL && ep->value != NULL &&
836                                  ep->key[ 0 ] != '\0' && ep->value[ 0 ] != '\0' &&
837                                  !Q_strncasecmp( ep->key, "_remap", 6 ) ) {
838                                 /* create new remapping */
839                                 remap2 = remap;
840                                 remap = safe_malloc( sizeof( *remap ) );
841                                 remap->next = remap2;
842                                 strcpy( remap->from, ep->value );
843
844                                 /* split the string */
845                                 split = strchr( remap->from, ';' );
846                                 if ( split == NULL ) {
847                                         Sys_FPrintf( SYS_WRN, "WARNING: Shader _remap key found in misc_model without a ; character\n" );
848                                         free( remap );
849                                         remap = remap2;
850                                         continue;
851                                 }
852
853                                 /* store the split */
854                                 *split = '\0';
855                                 strcpy( remap->to, ( split + 1 ) );
856
857                                 /* note it */
858                                 //%     Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", remap->from, remap->to );
859                         }
860                 }
861
862                 /* ydnar: cel shader support */
863                 value = ValueForKey( e2, "_celshader" );
864                 if ( value[ 0 ] == '\0' ) {
865                         value = ValueForKey( &entities[ 0 ], "_celshader" );
866                 }
867                 if ( value[ 0 ] != '\0' ) {
868                         sprintf( shader, "textures/%s", value );
869                         celShader = ShaderInfoForShader( shader );
870                 }
871                 else{
872                         celShader = *globalCelShader ? ShaderInfoForShader( globalCelShader ) : NULL;
873                 }
874
875                 /* jal : entity based _samplesize */
876                 lightmapSampleSize = 0;
877                 if ( strcmp( "", ValueForKey( e2, "_lightmapsamplesize" ) ) ) {
878                         lightmapSampleSize = IntForKey( e2, "_lightmapsamplesize" );
879                 }
880                 else if ( strcmp( "", ValueForKey( e2, "_samplesize" ) ) ) {
881                         lightmapSampleSize = IntForKey( e2, "_samplesize" );
882                 }
883                 else if ( strcmp( "", ValueForKey( e2, "_ss" ) ) ) {
884                         lightmapSampleSize = IntForKey( e2, "_ss" );
885                 }
886
887                 if ( lightmapSampleSize < 0 ) {
888                         lightmapSampleSize = 0;
889                 }
890
891                 if ( lightmapSampleSize > 0.0f ) {
892                         Sys_Printf( "misc_model has lightmap sample size of %.d\n", lightmapSampleSize );
893                 }
894
895                 /* get lightmap scale */
896                 /* vortex: added _ls key (short name of lightmapscale) */
897                 lightmapScale = 0.0f;
898                 if ( strcmp( "", ValueForKey( e2, "lightmapscale" ) ) ||
899                          strcmp( "", ValueForKey( e2, "_lightmapscale" ) ) ||
900                          strcmp( "", ValueForKey( e2, "_ls" ) ) ) {
901                         lightmapScale = FloatForKey( e2, "lightmapscale" );
902                         if ( lightmapScale <= 0.0f ) {
903                                 lightmapScale = FloatForKey( e2, "_lightmapscale" );
904                         }
905                         if ( lightmapScale <= 0.0f ) {
906                                 lightmapScale = FloatForKey( e2, "_ls" );
907                         }
908                         if ( lightmapScale < 0.0f ) {
909                                 lightmapScale = 0.0f;
910                         }
911                         if ( lightmapScale > 0.0f ) {
912                                 Sys_Printf( "misc_model has lightmap scale of %.4f\n", lightmapScale );
913                         }
914                 }
915
916                 /* jal : entity based _shadeangle */
917                 shadeAngle = 0.0f;
918                 if ( strcmp( "", ValueForKey( e2, "_shadeangle" ) ) ) {
919                         shadeAngle = FloatForKey( e2, "_shadeangle" );
920                 }
921                 /* vortex' aliases */
922                 else if ( strcmp( "", ValueForKey( e2, "_smoothnormals" ) ) ) {
923                         shadeAngle = FloatForKey( e2, "_smoothnormals" );
924                 }
925                 else if ( strcmp( "", ValueForKey( e2, "_sn" ) ) ) {
926                         shadeAngle = FloatForKey( e2, "_sn" );
927                 }
928                 else if ( strcmp( "", ValueForKey( e2, "_sa" ) ) ) {
929                         shadeAngle = FloatForKey( e2, "_sa" );
930                 }
931                 else if ( strcmp( "", ValueForKey( e2, "_smooth" ) ) ) {
932                         shadeAngle = FloatForKey( e2, "_smooth" );
933                 }
934
935                 if ( shadeAngle < 0.0f ) {
936                         shadeAngle = 0.0f;
937                 }
938
939                 if ( shadeAngle > 0.0f ) {
940                         Sys_Printf( "misc_model has shading angle of %.4f\n", shadeAngle );
941                 }
942
943                 skin = 0;
944                 if ( strcmp( "", ValueForKey( e2, "_skin" ) ) ) {
945                         skin = IntForKey( e2, "_skin" );
946                 }
947                 else if ( strcmp( "", ValueForKey( e2, "skin" ) ) ) {
948                         skin = IntForKey( e2, "skin" );
949                 }
950
951                 /* insert the model */
952                 InsertModel( model, skin, frame, transform, remap, celShader, mapEntityNum, castShadows, recvShadows, spawnFlags, lightmapScale, lightmapSampleSize, shadeAngle );
953
954                 /* free shader remappings */
955                 while ( remap != NULL )
956                 {
957                         remap2 = remap->next;
958                         free( remap );
959                         remap = remap2;
960                 }
961         }
962 }