1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
6 This file is part of GtkRadiant.
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.
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.
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
22 ----------------------------------------------------------------------------------
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."
27 ------------------------------------------------------------------------------- */
43 callback for picomodel.lib
46 void PicoPrintFunc( int level, const char *str ){
53 Sys_Printf( "%s\n", str );
57 Sys_FPrintf( SYS_VRB, "%s\n", str );
61 Sys_FPrintf( SYS_WRN, "WARNING: %s\n", str );
65 Sys_FPrintf( SYS_ERR, "ERROR: %s\n", str );
69 Error( "ERROR: %s\n", str );
78 callback for picomodel.lib
81 void PicoLoadFileFunc( const char *name, byte **buffer, int *bufSize ){
82 *bufSize = vfsLoadFile( name, (void**) buffer, 0 );
89 finds an existing picoModel and returns a pointer to the picoModel_t struct or NULL if not found
92 picoModel_t *FindModel( const char *name, int frame ){
97 if ( numPicoModels <= 0 ) {
98 memset( picoModels, 0, sizeof( picoModels ) );
102 if ( name == NULL || name[ 0 ] == '\0' ) {
107 for ( i = 0; i < MAX_MODELS; i++ )
109 if ( picoModels[ i ] != NULL &&
110 !strcmp( PicoGetModelName( picoModels[ i ] ), name ) &&
111 PicoGetModelFrameNum( picoModels[ i ] ) == frame ) {
112 return picoModels[ i ];
116 /* no matching picoModel found */
124 loads a picoModel and returns a pointer to the picoModel_t struct or NULL if not found
127 picoModel_t *LoadModel( const char *name, int frame ){
129 picoModel_t *model, **pm;
133 if ( numPicoModels <= 0 ) {
134 memset( picoModels, 0, sizeof( picoModels ) );
138 if ( name == NULL || name[ 0 ] == '\0' ) {
142 /* try to find existing picoModel */
143 model = FindModel( name, frame );
144 if ( model != NULL ) {
148 /* none found, so find first non-null picoModel */
150 for ( i = 0; i < MAX_MODELS; i++ )
152 if ( picoModels[ i ] == NULL ) {
153 pm = &picoModels[ i ];
158 /* too many picoModels? */
160 Error( "MAX_MODELS (%d) exceeded, there are too many model files referenced by the map.", MAX_MODELS );
163 /* attempt to parse model */
164 *pm = PicoLoadModel( name, frame );
166 /* if loading failed, make a bogus model to silence the rest of the warnings */
168 /* allocate a new model */
169 *pm = PicoNewModel();
175 PicoSetModelName( *pm, name );
176 PicoSetModelFrameNum( *pm, frame );
182 int numSurfaces, numVertexes;
186 Sys_Printf( "Model %s\n", name );
187 numSurfaces = PicoGetModelNumSurfaces( *pm );
188 for ( i = 0; i < numSurfaces; i++ )
190 ps = PicoGetModelSurface( *pm, i );
191 numVertexes = PicoGetSurfaceNumVertexes( ps );
192 Sys_Printf( "Surface %d has %d vertexes\n", i, numVertexes );
202 /* return the picoModel */
209 InsertModel() - ydnar
210 adds a picomodel into the bsp
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;
217 picoShader_t *shader;
218 picoSurface_t *surface;
220 mapDrawSurface_t *ds;
222 char *picoShaderName;
223 char shaderName[ MAX_QPATH ];
224 picoVec_t *xyz, *normal, *st;
226 picoIndex_t *indexes;
228 skinfile_t *sf, *sf2;
229 char skinfilename[ MAX_QPATH ];
230 char *skinfilecontent;
232 char *skinfileptr, *skinfilenextptr;
233 int ok=0, notok=0, spf = ( spawnFlags & 8088 );
237 if ( clipDepth < 0 ){
238 limDepth = -clipDepth;
244 model = LoadModel( name, frame );
245 if ( model == NULL ) {
250 snprintf( skinfilename, sizeof( skinfilename ), "%s_%d.skin", name, skin );
251 skinfilename[sizeof( skinfilename ) - 1] = 0;
252 skinfilesize = vfsLoadFile( skinfilename, (void**) &skinfilecontent, 0 );
253 if ( skinfilesize < 0 && skin != 0 ) {
254 /* fallback to skin 0 if invalid */
255 snprintf( skinfilename, sizeof( skinfilename ), "%s_0.skin", name );
256 skinfilename[sizeof( skinfilename ) - 1] = 0;
257 skinfilesize = vfsLoadFile( skinfilename, (void**) &skinfilecontent, 0 );
258 if ( skinfilesize >= 0 ) {
259 Sys_Printf( "Skin %d of %s does not exist, using 0 instead\n", skin, name );
263 if ( skinfilesize >= 0 ) {
264 Sys_Printf( "Using skin %d of %s\n", skin, name );
266 for ( skinfileptr = skinfilecontent; *skinfileptr; skinfileptr = skinfilenextptr )
271 skinfilenextptr = strchr( skinfileptr, '\r' );
272 if ( skinfilenextptr ) {
273 *skinfilenextptr++ = 0;
277 skinfilenextptr = strchr( skinfileptr, '\n' );
278 if ( skinfilenextptr ) {
279 *skinfilenextptr++ = 0;
282 skinfilenextptr = skinfileptr + strlen( skinfileptr );
286 /* create new item */
288 sf = safe_malloc( sizeof( *sf ) );
291 sprintf( format, "replace %%%ds %%%ds", (int)sizeof( sf->name ) - 1, (int)sizeof( sf->to ) - 1 );
292 if ( sscanf( skinfileptr, format, sf->name, sf->to ) == 2 ) {
295 sprintf( format, " %%%d[^, ] ,%%%ds", (int)sizeof( sf->name ) - 1, (int)sizeof( sf->to ) - 1 );
296 if ( ( pos = sscanf( skinfileptr, format, sf->name, sf->to ) ) == 2 ) {
300 /* invalid input line -> discard sf struct */
301 Sys_Printf( "Discarding skin directive in %s: %s\n", skinfilename, skinfileptr );
305 free( skinfilecontent );
308 /* handle null matrix */
309 if ( transform == NULL ) {
310 m4x4_identity( identity );
311 transform = identity;
314 /* hack: Stable-1_2 and trunk have differing row/column major matrix order
315 this transpose is necessary with Stable-1_2
316 uncomment the following line with old m4x4_t (non 1.3/spog_branch) code */
317 //% m4x4_transpose( transform );
319 /* create transform matrix for normals */
320 memcpy( nTransform, transform, sizeof( m4x4_t ) );
321 if ( m4x4_invert( nTransform ) ) {
322 Sys_FPrintf( SYS_VRB, "WARNING: Can't invert model transform matrix, using transpose instead\n" );
324 m4x4_transpose( nTransform );
326 /* fix bogus lightmap scale */
327 if ( lightmapScale <= 0.0f ) {
328 lightmapScale = 1.0f;
331 /* fix bogus shade angle */
332 if ( shadeAngle <= 0.0f ) {
336 /* each surface on the model will become a new map drawsurface */
337 numSurfaces = PicoGetModelNumSurfaces( model );
338 //% Sys_FPrintf( SYS_VRB, "Model %s has %d surfaces\n", name, numSurfaces );
339 for ( s = 0; s < numSurfaces; s++ )
342 surface = PicoGetModelSurface( model, s );
343 if ( surface == NULL ) {
347 /* only handle triangle surfaces initially (fixme: support patches) */
348 if ( PicoGetSurfaceType( surface ) != PICO_TRIANGLES ) {
352 /* get shader name */
353 shader = PicoGetSurfaceShader( surface );
354 if ( shader == NULL ) {
358 picoShaderName = PicoGetShaderName( shader );
361 /* handle .skin file */
363 picoShaderName = NULL;
364 for ( sf2 = sf; sf2 != NULL; sf2 = sf2->next )
366 if ( !Q_stricmp( surface->name, sf2->name ) ) {
367 Sys_FPrintf( SYS_VRB, "Skin file: mapping %s to %s\n", surface->name, sf2->to );
368 picoShaderName = sf2->to;
372 if ( !picoShaderName ) {
373 Sys_FPrintf( SYS_VRB, "Skin file: not mapping %s\n", surface->name );
378 /* handle shader remapping */
380 for ( rm = remap; rm != NULL; rm = rm->next )
382 if ( rm->from[ 0 ] == '*' && rm->from[ 1 ] == '\0' ) {
385 else if ( !Q_stricmp( picoShaderName, rm->from ) ) {
386 Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", picoShaderName, rm->to );
387 picoShaderName = rm->to;
393 if ( glob != NULL ) {
394 Sys_FPrintf( SYS_VRB, "Globbing %s to %s\n", picoShaderName, glob->to );
395 picoShaderName = glob->to;
398 /* shader renaming for sof2 */
399 if ( renameModelShaders ) {
400 strcpy( shaderName, picoShaderName );
401 StripExtension( shaderName );
402 if ( spawnFlags & 1 ) {
403 strcat( shaderName, "_RMG_BSP" );
406 strcat( shaderName, "_BSP" );
408 si = ShaderInfoForShader( shaderName );
411 si = ShaderInfoForShader( picoShaderName );
414 /* allocate a surface (ydnar: gs mods) */
415 ds = AllocDrawSurface( SURFACE_TRIANGLES );
416 ds->entityNum = eNum;
417 ds->castShadows = castShadows;
418 ds->recvShadows = recvShadows;
424 if ( ( si != NULL && si->forceMeta ) || ( spawnFlags & 4 ) ) { /* 3rd bit */
425 ds->type = SURFACE_FORCED_META;
428 /* fix the surface's normals (jal: conditioned by shader info) */
429 if ( !( spawnFlags & 64 ) && ( shadeAngle == 0.0f || ds->type != SURFACE_FORCED_META ) ) {
430 PicoFixSurfaceNormals( surface );
433 /* set sample size */
434 if ( lightmapSampleSize > 0.0f ) {
435 ds->sampleSize = lightmapSampleSize;
438 /* set lightmap scale */
439 if ( lightmapScale > 0.0f ) {
440 ds->lightmapScale = lightmapScale;
443 /* set shading angle */
444 if ( shadeAngle > 0.0f ) {
445 ds->shadeAngleDegrees = shadeAngle;
448 /* set particulars */
449 ds->numVerts = PicoGetSurfaceNumVertexes( surface );
450 ds->verts = safe_malloc0( ds->numVerts * sizeof( ds->verts[ 0 ] ) );
452 ds->numIndexes = PicoGetSurfaceNumIndexes( surface );
453 ds->indexes = safe_malloc0( ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
456 for ( i = 0; i < ds->numVerts; i++ )
459 dv = &ds->verts[ i ];
462 xyz = PicoGetSurfaceXYZ( surface, i );
463 VectorCopy( xyz, dv->xyz );
464 m4x4_transform_point( transform, dv->xyz );
466 normal = PicoGetSurfaceNormal( surface, i );
467 VectorCopy( normal, dv->normal );
468 m4x4_transform_normal( nTransform, dv->normal );
469 VectorNormalize( dv->normal, dv->normal );
471 /* ydnar: tek-fu celshading support for flat shaded shit */
473 dv->st[ 0 ] = si->stFlat[ 0 ];
474 dv->st[ 1 ] = si->stFlat[ 1 ];
477 /* ydnar: gs mods: added support for explicit shader texcoord generation */
478 else if ( si->tcGen ) {
479 /* project the texture */
480 dv->st[ 0 ] = DotProduct( si->vecs[ 0 ], dv->xyz );
481 dv->st[ 1 ] = DotProduct( si->vecs[ 1 ], dv->xyz );
484 /* normal texture coordinates */
487 st = PicoGetSurfaceST( surface, 0, i );
488 dv->st[ 0 ] = st[ 0 ];
489 dv->st[ 1 ] = st[ 1 ];
492 /* set lightmap/color bits */
493 color = PicoGetSurfaceColor( surface, 0, i );
494 for ( j = 0; j < MAX_LIGHTMAPS; j++ )
496 dv->lightmap[ j ][ 0 ] = 0.0f;
497 dv->lightmap[ j ][ 1 ] = 0.0f;
498 if ( spawnFlags & 32 ) { // spawnflag 32: model color -> alpha hack
499 dv->color[ j ][ 0 ] = 255.0f;
500 dv->color[ j ][ 1 ] = 255.0f;
501 dv->color[ j ][ 2 ] = 255.0f;
502 dv->color[ j ][ 3 ] = RGBTOGRAY( color );
506 dv->color[ j ][ 0 ] = color[ 0 ];
507 dv->color[ j ][ 1 ] = color[ 1 ];
508 dv->color[ j ][ 2 ] = color[ 2 ];
509 dv->color[ j ][ 3 ] = color[ 3 ];
515 indexes = PicoGetSurfaceIndexes( surface, 0 );
516 for ( i = 0; i < ds->numIndexes; i++ )
517 ds->indexes[ i ] = indexes[ i ];
520 ds->celShader = celShader;
522 /* ydnar: giant hack land: generate clipping brushes for model triangles */
523 if ( ( si->clipModel && !( spf ) ) || //default CLIPMODEL
524 ( ( spawnFlags & 8090 ) == 2 ) || //default CLIPMODEL
525 ( spf == 8 ) || //EXTRUDE_FACE_NORMALS
526 ( spf == 16 ) || //EXTRUDE_TERRAIN
527 ( spf == 128 ) || //EXTRUDE_VERTEX_NORMALS
528 ( spf == 256 ) || //PYRAMIDAL_CLIP
529 ( spf == 512 ) || //EXTRUDE_DOWNWARDS
530 ( spf == 1024 ) || //EXTRUDE_UPWARDS
531 ( spf == 4096 ) || //default CLIPMODEL + AXIAL_BACKPLANE
532 ( spf == 264 ) || //EXTRUDE_FACE_NORMALS+PYRAMIDAL_CLIP (extrude 45)
533 ( spf == 2064 ) || //EXTRUDE_TERRAIN+MAX_EXTRUDE
534 ( spf == 4112 ) || //EXTRUDE_TERRAIN+AXIAL_BACKPLANE
535 ( spf == 384 ) || //EXTRUDE_VERTEX_NORMALS + PYRAMIDAL_CLIP - vertex normals + don't check for sides, sticking outwards
536 ( spf == 4352 ) || //PYRAMIDAL_CLIP+AXIAL_BACKPLANE
537 ( spf == 1536 ) || //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS
538 ( spf == 2560 ) || //EXTRUDE_DOWNWARDS+MAX_EXTRUDE
539 ( spf == 4608 ) || //EXTRUDE_DOWNWARDS+AXIAL_BACKPLANE
540 ( spf == 3584 ) || //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS+MAX_EXTRUDE
541 ( spf == 5632 ) || //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS+AXIAL_BACKPLANE
542 ( spf == 3072 ) || //EXTRUDE_UPWARDS+MAX_EXTRUDE
543 ( spf == 5120 ) ){ //EXTRUDE_UPWARDS+AXIAL_BACKPLANE
544 vec3_t points[ 4 ], backs[ 3 ], cnt, bestNormal, nrm, Vnorm[3], Enorm[3];
545 vec4_t plane, reverse, p[3];
546 double normalEpsilon_save;
548 vec3_t min = { 999999, 999999, 999999 }, max = { -999999, -999999, -999999 };
549 vec3_t avgDirection = { 0, 0, 0 };
551 #define nonax_clip_dbg 1
554 if ( !si->clipModel && !( si->compileFlags & C_SOLID ) ) {
558 //wont snap these in normal way, or will explode
559 normalEpsilon_save = normalEpsilon;
560 //normalEpsilon = 0.000001;
563 //MAX_EXTRUDE or EXTRUDE_TERRAIN
564 if ( ( spawnFlags & 2048 ) || ( spawnFlags & 16 ) ){
566 for ( i = 0; i < ds->numIndexes; i += 3 )
568 for ( j = 0; j < 3; j++ )
570 dv = &ds->verts[ ds->indexes[ i + j ] ];
571 VectorCopy( dv->xyz, points[ j ] );
573 if ( PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] ) ){
574 if ( spawnFlags & 16 ) VectorAdd( avgDirection, plane, avgDirection ); //calculate average mesh facing direction
577 for ( k = 2; k > -1; k-- ){
579 for ( j = 0; j < 3; j++ ){ if ( points[j][k] < min[k] ) min[k] = points[j][k]; }
581 else if ( plane[k] < 0 ){
582 for ( j = 0; j < 3; j++ ){ if ( points[j][k] > max[k] ) max[k] = points[j][k]; }
584 //if EXTRUDE_DOWNWARDS or EXTRUDE_UPWARDS
585 if ( ( spawnFlags & 512 ) || ( spawnFlags & 1024 ) ){
591 //unify avg direction
592 if ( spawnFlags & 16 ){
593 for ( j = 0; j < 3; j++ ){
594 if ( fabs(avgDirection[j]) > fabs(avgDirection[(j+1)%3]) ){
595 avgDirection[(j+1)%3] = 0.0;
599 avgDirection[j] = 0.0;
602 if ( VectorNormalize( avgDirection, avgDirection ) == 0 ){
604 VectorSet( avgDirection, 0, 0, 1 );
609 /* walk triangle list */
610 for ( i = 0; i < ds->numIndexes; i += 3 )
613 AUTOEXPAND_BY_REALLOC( mapplanes, ( nummapplanes + 64 ) << 1, allocatedmapplanes, 1024 );
616 for ( j = 0; j < 3; j++ )
619 dv = &ds->verts[ ds->indexes[ i + j ] ];
622 VectorCopy( dv->xyz, points[ j ] );
625 /* make plane for triangle */
626 if ( PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] ) ) {
629 buildBrush = AllocBrush( 48 );
630 buildBrush->entityNum = mapEntityNum;
631 buildBrush->original = buildBrush;
632 buildBrush->contentShader = si;
633 buildBrush->compileFlags = si->compileFlags;
634 buildBrush->contentFlags = si->contentFlags;
635 buildBrush->detail = qtrue;
637 //snap points before using them for further calculations
638 //precision suffers a lot, when two of normal values are under .00025 (often no collision, knocking up effect in ioq3)
639 //also broken drawsurfs in case of normal brushes
641 for ( j=0; j<3; j++ )
643 if ( fabs(plane[j]) < 0.00025 && fabs(plane[(j+1)%3]) < 0.00025 && ( plane[j] != 0.0 || plane[(j+1)%3] != 0.0 ) ){
644 VectorAdd( points[ 0 ], points[ 1 ], cnt );
645 VectorAdd( cnt, points[ 2 ], cnt );
646 VectorScale( cnt, 0.3333333333333f, cnt );
647 points[0][(j+2)%3]=points[1][(j+2)%3]=points[2][(j+2)%3]=cnt[(j+2)%3];
653 //snap pairs of points to prevent bad side planes
654 for ( j=0; j<3; j++ )
656 VectorSubtract( points[j], points[(j+1)%3], nrm );
657 VectorNormalize( nrm, nrm );
658 for ( k=0; k<3; k++ )
660 if ( nrm[k] != 0.0 && fabs(nrm[k]) < 0.00025 ){
661 //Sys_Printf( "b4(%6.6f %6.6f %6.6f)(%6.6f %6.6f %6.6f)\n", points[j][0], points[j][1], points[j][2], points[(j+1)%3][0], points[(j+1)%3][1], points[(j+1)%3][2] );
662 points[j][k]=points[(j+1)%3][k] = ( points[j][k] + points[(j+1)%3][k] ) / 2.0;
663 //Sys_Printf( "sn(%6.6f %6.6f %6.6f)(%6.6f %6.6f %6.6f)\n", points[j][0], points[j][1], points[j][2], points[(j+1)%3][0], points[(j+1)%3][1], points[(j+1)%3][2] );
670 PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] );
674 //vector-is-close-to-be-on-axis check again, happens after previous code sometimes
675 for ( j=0; j<3; j++ )
677 if ( fabs(plane[j]) < 0.00025 && fabs(plane[(j+1)%3]) < 0.00025 && ( plane[j] != 0.0 || plane[(j+1)%3] != 0.0 ) ){
678 VectorAdd( points[ 0 ], points[ 1 ], cnt );
679 VectorAdd( cnt, points[ 2 ], cnt );
680 VectorScale( cnt, 0.3333333333333f, cnt );
681 points[0][(j+2)%3]=points[1][(j+2)%3]=points[2][(j+2)%3]=cnt[(j+2)%3];
682 PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] );
687 //snap single snappable normal components
688 for ( j=0; j<3; j++ )
690 if ( plane[j] != 0.0 && fabs(plane[j]) < 0.00005 ){
698 VectorAdd( points[ 0 ], points[ 1 ], cnt );
699 VectorAdd( cnt, points[ 2 ], cnt );
700 VectorScale( cnt, 0.3333333333333f, cnt );
701 VectorNormalize( plane, plane );
702 plane[3] = DotProduct( plane, cnt );
704 //project points to resulting plane to keep intersections precision
705 for ( j=0; j<3; j++ )
707 //Sys_Printf( "b4 %i (%6.7f %6.7f %6.7f)\n", j, points[j][0], points[j][1], points[j][2] );
708 VectorMA( points[j], plane[3] - DotProduct( plane, points[j]), plane, points[j] );
709 //Sys_Printf( "sn %i (%6.7f %6.7f %6.7f)\n", j, points[j][0], points[j][1], points[j][2] );
711 //Sys_Printf( "sn pln (%6.7f %6.7f %6.7f %6.7f)\n", plane[0], plane[1], plane[2], plane[3] );
712 //PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] );
713 //Sys_Printf( "pts pln (%6.7f %6.7f %6.7f %6.7f)\n", plane[0], plane[1], plane[2], plane[3] );
717 if ( spf == 4352 ){ //PYRAMIDAL_CLIP+AXIAL_BACKPLANE
719 for ( j=0; j<3; j++ )
721 if ( fabs(plane[j]) < 0.05 && fabs(plane[(j+1)%3]) < 0.05 ){ //no way, close to lay on two axises
722 goto default_CLIPMODEL;
727 VectorCopy( plane, bestNormal );
728 for ( j = 0; j < 3; j++ ){
729 if ( fabs(bestNormal[j]) > fabs(bestNormal[(j+1)%3]) ){
730 bestNormal[(j+1)%3] = 0.0;
737 VectorNormalize( bestNormal, bestNormal );
740 float bestdist, currdist, bestangle, currangle, mindist = 999999;
742 for ( j = 0; j < 3; j++ ){//planes
745 for ( k = 0; k < 3; k++ ){//axises
746 VectorSubtract( points[ (j+1)%3 ], points[ j ], nrm );
748 CrossProduct( bestNormal, nrm, reverse );
751 VectorClear( Vnorm[0] );
752 if ( (k+1)%3 == axis ){
753 if ( nrm[ (k+2)%3 ] == 0 ) continue;
754 Vnorm[0][ (k+2)%3 ] = nrm[ (k+2)%3 ];
757 if ( nrm[ (k+1)%3 ] == 0 ) continue;
758 Vnorm[0][ (k+1)%3 ] = nrm[ (k+1)%3 ];
760 CrossProduct( bestNormal, Vnorm[0], Enorm[0] );
761 CrossProduct( Enorm[0], nrm, reverse );
763 VectorNormalize( reverse, reverse );
764 reverse[3] = DotProduct( points[ j ], reverse );
765 //check facing, thickness
766 currdist = reverse[3] - DotProduct( reverse, points[ (j+2)%3 ] );
767 currangle = DotProduct( reverse, plane );
768 if ( ( ( currdist > 0.1 ) && ( currdist < bestdist ) && ( currangle < 0 ) ) ||
769 ( ( currangle >= 0 ) && ( currangle <= bestangle ) ) ){
770 bestangle = currangle;
771 if ( currangle < 0 ) bestdist = currdist;
772 VectorCopy( reverse, p[j] );
773 p[j][3] = reverse[3];
776 //if ( bestdist == 999999 && bestangle == 1 ) Sys_Printf("default_CLIPMODEL\n");
777 if ( bestdist == 999999 && bestangle == 1 ) goto default_CLIPMODEL;
778 if ( bestdist < mindist ) mindist = bestdist;
780 if ( (limDepth != 0.0) && (mindist > limDepth) ) goto default_CLIPMODEL;
784 for ( j = 0; j < 3; j++ )
786 for ( k = 0; k < 3; k++ )
788 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
789 Sys_Printf( "nonax nrm %6.17f %6.17f %6.17f\n", p[j][0], p[j][1], p[j][2] );
794 /* set up brush sides */
795 buildBrush->numsides = 4;
796 buildBrush->sides[ 0 ].shaderInfo = si;
797 for ( j = 1; j < buildBrush->numsides; j++ ) {
799 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
800 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
803 buildBrush->sides[ j ].shaderInfo = NULL; // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
806 VectorCopy( points[0], points[3] ); // for cyclic usage
808 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
809 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
810 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
811 buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
815 else if ( ( spf == 16 ) || //EXTRUDE_TERRAIN
816 ( spf == 512 ) || //EXTRUDE_DOWNWARDS
817 ( spf == 1024 ) || //EXTRUDE_UPWARDS
818 ( spf == 4096 ) || //default CLIPMODEL + AXIAL_BACKPLANE
819 ( spf == 2064 ) || //EXTRUDE_TERRAIN+MAX_EXTRUDE
820 ( spf == 4112 ) || //EXTRUDE_TERRAIN+AXIAL_BACKPLANE
821 ( spf == 1536 ) || //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS
822 ( spf == 2560 ) || //EXTRUDE_DOWNWARDS+MAX_EXTRUDE
823 ( spf == 4608 ) || //EXTRUDE_DOWNWARDS+AXIAL_BACKPLANE
824 ( spf == 3584 ) || //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS+MAX_EXTRUDE
825 ( spf == 5632 ) || //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS+AXIAL_BACKPLANE
826 ( spf == 3072 ) || //EXTRUDE_UPWARDS+MAX_EXTRUDE
827 ( spf == 5120 ) ){ //EXTRUDE_UPWARDS+AXIAL_BACKPLANE
829 if ( spawnFlags & 16 ){ //autodirection
830 VectorCopy( avgDirection, bestNormal );
834 if ( ( spawnFlags & 1536 ) == 1536 ){ //up+down
835 VectorSet( bestNormal, 0, 0, ( plane[2] >= 0 ? 1 : -1 ) );
837 else if ( spawnFlags & 512 ){ //down
838 VectorSet( bestNormal, 0, 0, 1 );
841 else if ( spawnFlags & 1024 ){ //up
842 VectorSet( bestNormal, 0, 0, -1 );
844 else{ // best axial normal
845 VectorCopy( plane, bestNormal );
846 for ( j = 0; j < 3; j++ ){
847 if ( fabs(bestNormal[j]) > fabs(bestNormal[(j+1)%3]) ){
848 bestNormal[(j+1)%3] = 0.0;
855 VectorNormalize( bestNormal, bestNormal );
859 if ( DotProduct( plane, bestNormal ) < 0.05 ){
860 goto default_CLIPMODEL;
864 /* make side planes */
865 for ( j = 0; j < 3; j++ )
867 VectorSubtract( points[(j+1)%3], points[ j ], nrm );
868 CrossProduct( bestNormal, nrm, p[ j ] );
869 VectorNormalize( p[ j ], p[ j ] );
870 p[j][3] = DotProduct( points[j], p[j] );
873 /* make back plane */
874 if ( spawnFlags & 2048 ){ //max extrude
875 VectorScale( bestNormal, -1.0f, reverse );
876 if ( bestNormal[axis] > 0 ){
877 reverse[3] = -min[axis] + clipDepth;
880 reverse[3] = max[axis] + clipDepth;
883 else if ( spawnFlags & 4096 ){ //axial backplane
884 VectorScale( bestNormal, -1.0f, reverse );
885 reverse[3] = points[0][axis];
886 if ( bestNormal[axis] > 0 ){
887 for ( j = 1; j < 3; j++ ){
888 if ( points[j][axis] < reverse[3] ){
889 reverse[3] = points[j][axis];
892 reverse[3] = -reverse[3] + clipDepth;
895 for ( j = 1; j < 3; j++ ){
896 if ( points[j][axis] > reverse[3] ){
897 reverse[3] = points[j][axis];
900 reverse[3] += clipDepth;
902 if (limDepth != 0.0){
903 VectorCopy( points[0], cnt );
904 if ( bestNormal[axis] > 0 ){
905 for ( j = 1; j < 3; j++ ){
906 if ( points[j][axis] > cnt[axis] ){
907 VectorCopy( points[j], cnt );
912 for ( j = 1; j < 3; j++ ){
913 if ( points[j][axis] < cnt[axis] ){
914 VectorCopy( points[j], cnt );
918 VectorMA( cnt, reverse[3] - DotProduct( reverse, cnt ), reverse, cnt );
919 if ( ( plane[3] - DotProduct( plane, cnt ) ) > limDepth ){
920 VectorScale( plane, -1.0f, reverse );
921 reverse[ 3 ] = -plane[ 3 ];
922 reverse[3] += clipDepth;
926 else{ //normal backplane
927 VectorScale( plane, -1.0f, reverse );
928 reverse[ 3 ] = -plane[ 3 ];
929 reverse[3] += clipDepth;
932 for ( j = 0; j < 3; j++ )
934 for ( k = 0; k < 3; k++ )
936 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
937 Sys_Printf( "nonax nrm %6.17f %6.17f %6.17f\n", p[j][0], p[j][1], p[j][2] );
942 /* set up brush sides */
943 buildBrush->numsides = 5;
944 buildBrush->sides[ 0 ].shaderInfo = si;
945 for ( j = 1; j < buildBrush->numsides; j++ ) {
947 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
948 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
951 buildBrush->sides[ j ].shaderInfo = NULL; // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
954 VectorCopy( points[0], points[3] ); // for cyclic usage
956 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
957 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
958 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
959 buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
960 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 0, NULL );
964 else if ( spf == 264 ){ //EXTRUDE_FACE_NORMALS+PYRAMIDAL_CLIP (extrude 45)
966 //45 degrees normals for side planes
967 for ( j = 0; j < 3; j++ )
969 VectorSubtract( points[(j+1)%3], points[ j ], nrm );
970 CrossProduct( plane, nrm, Enorm[ j ] );
971 VectorNormalize( Enorm[ j ], Enorm[ j ] );
972 VectorAdd( plane, Enorm[ j ], Enorm[ j ] );
973 VectorNormalize( Enorm[ j ], Enorm[ j ] );
974 /* make side planes */
975 CrossProduct( Enorm[ j ], nrm, p[ j ] );
976 VectorNormalize( p[ j ], p[ j ] );
977 p[j][3] = DotProduct( points[j], p[j] );
978 //snap nearly axial side planes
980 for ( k = 0; k < 3; k++ )
982 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
988 VectorNormalize( p[j], p[j] );
989 VectorAdd( points[j], points[(j+1)%3], cnt );
990 VectorScale( cnt, 0.5f, cnt );
991 p[j][3] = DotProduct( cnt, p[j] );
995 /* make back plane */
996 VectorScale( plane, -1.0f, reverse );
997 reverse[ 3 ] = -plane[ 3 ];
998 reverse[3] += clipDepth;
1000 /* set up brush sides */
1001 buildBrush->numsides = 5;
1002 buildBrush->sides[ 0 ].shaderInfo = si;
1003 for ( j = 1; j < buildBrush->numsides; j++ ) {
1005 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
1006 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
1009 buildBrush->sides[ j ].shaderInfo = NULL; // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
1012 VectorCopy( points[0], points[3] ); // for cyclic usage
1014 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
1015 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
1016 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
1017 buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
1018 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 0, NULL );
1022 else if ( ( spf == 128 ) || //EXTRUDE_VERTEX_NORMALS
1023 ( spf == 384 ) ){ //EXTRUDE_VERTEX_NORMALS + PYRAMIDAL_CLIP - vertex normals + don't check for sides, sticking outwards
1024 /* get vertex normals */
1025 for ( j = 0; j < 3; j++ )
1028 dv = &ds->verts[ ds->indexes[ i + j ] ];
1030 VectorCopy( dv->normal, Vnorm[ j ] );
1033 //avg normals for side planes
1034 for ( j = 0; j < 3; j++ )
1036 VectorAdd( Vnorm[ j ], Vnorm[ (j+1)%3 ], Enorm[ j ] );
1037 VectorNormalize( Enorm[ j ], Enorm[ j ] );
1038 //check fuer bad ones
1039 VectorSubtract( points[(j+1)%3], points[ j ], cnt );
1040 CrossProduct( plane, cnt, nrm );
1041 VectorNormalize( nrm, nrm );
1042 //check for negative or outside direction
1043 if ( DotProduct( Enorm[ j ], plane ) > 0.1 ){
1044 if ( ( DotProduct( Enorm[ j ], nrm ) > -0.2 ) || ( spawnFlags & 256 ) ){
1050 //Sys_Printf( "faulty Enormal %i/%i\n", notok, ok );
1052 VectorAdd( plane, nrm, Enorm[ j ] );
1053 VectorNormalize( Enorm[ j ], Enorm[ j ] );
1056 /* make side planes */
1057 for ( j = 0; j < 3; j++ )
1059 VectorSubtract( points[(j+1)%3], points[ j ], nrm );
1060 CrossProduct( Enorm[ j ], nrm, p[ j ] );
1061 VectorNormalize( p[ j ], p[ j ] );
1062 p[j][3] = DotProduct( points[j], p[j] );
1063 //snap nearly axial side planes
1065 for ( k = 0; k < 3; k++ )
1067 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
1068 //Sys_Printf( "init plane %6.8f %6.8f %6.8f %6.8f\n", p[j][0], p[j][1], p[j][2], p[j][3]);
1074 VectorNormalize( p[j], p[j] );
1075 //Sys_Printf( "nrm plane %6.8f %6.8f %6.8f %6.8f\n", p[j][0], p[j][1], p[j][2], p[j][3]);
1076 VectorAdd( points[j], points[(j+1)%3], cnt );
1077 VectorScale( cnt, 0.5f, cnt );
1078 p[j][3] = DotProduct( cnt, p[j] );
1079 //Sys_Printf( "dst plane %6.8f %6.8f %6.8f %6.8f\n", p[j][0], p[j][1], p[j][2], p[j][3]);
1083 /* make back plane */
1084 VectorScale( plane, -1.0f, reverse );
1085 reverse[ 3 ] = -plane[ 3 ];
1086 reverse[3] += clipDepth;
1088 /* set up brush sides */
1089 buildBrush->numsides = 5;
1090 buildBrush->sides[ 0 ].shaderInfo = si;
1091 for ( j = 1; j < buildBrush->numsides; j++ ) {
1093 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
1094 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
1097 buildBrush->sides[ j ].shaderInfo = NULL; // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
1100 VectorCopy( points[0], points[3] ); // for cyclic usage
1102 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
1103 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
1104 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
1105 buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
1106 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 0, NULL );
1110 else if ( spf == 8 ){ //EXTRUDE_FACE_NORMALS
1112 /* make side planes */
1113 for ( j = 0; j < 3; j++ )
1115 VectorSubtract( points[(j+1)%3], points[ j ], nrm );
1116 CrossProduct( plane, nrm, p[ j ] );
1117 VectorNormalize( p[ j ], p[ j ] );
1118 p[j][3] = DotProduct( points[j], p[j] );
1119 //snap nearly axial side planes
1121 for ( k = 0; k < 3; k++ )
1123 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
1124 //Sys_Printf( "init plane %6.8f %6.8f %6.8f %6.8f\n", p[j][0], p[j][1], p[j][2], p[j][3]);
1130 VectorNormalize( p[j], p[j] );
1131 //Sys_Printf( "nrm plane %6.8f %6.8f %6.8f %6.8f\n", p[j][0], p[j][1], p[j][2], p[j][3]);
1132 VectorAdd( points[j], points[(j+1)%3], cnt );
1133 VectorScale( cnt, 0.5f, cnt );
1134 p[j][3] = DotProduct( cnt, p[j] );
1135 //Sys_Printf( "dst plane %6.8f %6.8f %6.8f %6.8f\n", p[j][0], p[j][1], p[j][2], p[j][3]);
1139 /* make back plane */
1140 VectorScale( plane, -1.0f, reverse );
1141 reverse[ 3 ] = -plane[ 3 ];
1142 reverse[3] += clipDepth;
1144 for ( j = 0; j < 3; j++ )
1146 for ( k = 0; k < 3; k++ )
1148 if ( fabs(p[j][k]) < 0.00005 && p[j][k] != 0.0 ){
1149 Sys_Printf( "nonax nrm %6.17f %6.17f %6.17f\n", p[j][0], p[j][1], p[j][2] );
1150 Sys_Printf( "frm src nrm %6.17f %6.17f %6.17f\n", plane[0], plane[1], plane[2]);
1155 /* set up brush sides */
1156 buildBrush->numsides = 5;
1157 buildBrush->sides[ 0 ].shaderInfo = si;
1158 for ( j = 1; j < buildBrush->numsides; j++ ) {
1160 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
1161 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
1164 buildBrush->sides[ j ].shaderInfo = NULL; // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
1167 VectorCopy( points[0], points[3] ); // for cyclic usage
1169 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
1170 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
1171 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
1172 buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
1173 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 0, NULL );
1177 else if ( spf == 256 ){ //PYRAMIDAL_CLIP
1179 /* calculate center */
1180 VectorAdd( points[ 0 ], points[ 1 ], cnt );
1181 VectorAdd( cnt, points[ 2 ], cnt );
1182 VectorScale( cnt, 0.3333333333333f, cnt );
1184 /* make back pyramid point */
1185 VectorMA( cnt, -clipDepth, plane, cnt );
1187 /* make 3 more planes */
1188 if( PlaneFromPoints( p[0], points[ 2 ], points[ 1 ], cnt ) &&
1189 PlaneFromPoints( p[1], points[ 1 ], points[ 0 ], cnt ) &&
1190 PlaneFromPoints( p[2], points[ 0 ], points[ 2 ], cnt ) ) {
1192 //check for dangerous planes
1193 while( (( p[0][0] != 0.0 || p[0][1] != 0.0 ) && fabs(p[0][0]) < 0.00025 && fabs(p[0][1]) < 0.00025) ||
1194 (( p[0][0] != 0.0 || p[0][2] != 0.0 ) && fabs(p[0][0]) < 0.00025 && fabs(p[0][2]) < 0.00025) ||
1195 (( p[0][2] != 0.0 || p[0][1] != 0.0 ) && fabs(p[0][2]) < 0.00025 && fabs(p[0][1]) < 0.00025) ||
1196 (( p[1][0] != 0.0 || p[1][1] != 0.0 ) && fabs(p[1][0]) < 0.00025 && fabs(p[1][1]) < 0.00025) ||
1197 (( p[1][0] != 0.0 || p[1][2] != 0.0 ) && fabs(p[1][0]) < 0.00025 && fabs(p[1][2]) < 0.00025) ||
1198 (( p[1][2] != 0.0 || p[1][1] != 0.0 ) && fabs(p[1][2]) < 0.00025 && fabs(p[1][1]) < 0.00025) ||
1199 (( p[2][0] != 0.0 || p[2][1] != 0.0 ) && fabs(p[2][0]) < 0.00025 && fabs(p[2][1]) < 0.00025) ||
1200 (( p[2][0] != 0.0 || p[2][2] != 0.0 ) && fabs(p[2][0]) < 0.00025 && fabs(p[2][2]) < 0.00025) ||
1201 (( p[2][2] != 0.0 || p[2][1] != 0.0 ) && fabs(p[2][2]) < 0.00025 && fabs(p[2][1]) < 0.00025) ) {
1202 VectorMA( cnt, -0.1f, plane, cnt );
1203 // Sys_Printf( "shifting pyramid point\n" );
1204 PlaneFromPoints( p[0], points[ 2 ], points[ 1 ], cnt );
1205 PlaneFromPoints( p[1], points[ 1 ], points[ 0 ], cnt );
1206 PlaneFromPoints( p[2], points[ 0 ], points[ 2 ], cnt );
1209 for ( j = 0; j < 3; j++ )
1211 for ( k = 0; k < 3; k++ )
1213 if ( fabs(p[j][k]) < 0.00005 && p[j][k] != 0.0 ){
1214 Sys_Printf( "nonax nrm %6.17f %6.17f %6.17f\n (%6.8f %6.8f %6.8f)\n (%6.8f %6.8f %6.8f)\n (%6.8f %6.8f %6.8f)\n", p[j][0], p[j][1], p[j][2], points[j][0], points[j][1], points[j][2], points[(j+1)%3][0], points[(j+1)%3][1], points[(j+1)%3][2], cnt[0], cnt[1], cnt[2] );
1219 /* set up brush sides */
1220 buildBrush->numsides = 4;
1221 buildBrush->sides[ 0 ].shaderInfo = si;
1222 for ( j = 1; j < buildBrush->numsides; j++ ) {
1224 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
1225 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
1228 buildBrush->sides[ j ].shaderInfo = NULL; // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
1231 VectorCopy( points[0], points[3] ); // for cyclic usage
1233 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
1234 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 1 ] ); // p[0] contains points[1] and points[2]
1235 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 0 ] ); // p[1] contains points[0] and points[1]
1236 buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
1240 Sys_Printf( "WARNING: triangle (%6.0f %6.0f %6.0f) (%6.0f %6.0f %6.0f) (%6.0f %6.0f %6.0f) of %s was not autoclipped\n", points[0][0], points[0][1], points[0][2], points[1][0], points[1][1], points[1][2], points[2][0], points[2][1], points[2][2], name );
1247 else if ( ( si->clipModel && !( spf ) ) || ( ( spawnFlags & 8090 ) == 2 ) ){ //default CLIPMODEL
1251 VectorCopy( plane, bestNormal );
1252 for ( j = 0; j < 3; j++ ){
1253 if ( fabs(bestNormal[j]) > fabs(bestNormal[(j+1)%3]) ){
1254 bestNormal[(j+1)%3] = 0.0;
1257 bestNormal[j] = 0.0;
1260 VectorNormalize( bestNormal, bestNormal );
1262 /* make side planes */
1263 for ( j = 0; j < 3; j++ )
1265 VectorSubtract( points[(j+1)%3], points[ j ], nrm );
1266 CrossProduct( bestNormal, nrm, p[ j ] );
1267 VectorNormalize( p[ j ], p[ j ] );
1268 p[j][3] = DotProduct( points[j], p[j] );
1271 /* make back plane */
1272 VectorScale( plane, -1.0f, reverse );
1273 reverse[ 3 ] = -plane[ 3 ];
1274 reverse[3] += DotProduct( bestNormal, plane ) * clipDepth;
1276 for ( j = 0; j < 3; j++ )
1278 for ( k = 0; k < 3; k++ )
1280 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
1281 Sys_Printf( "nonax nrm %6.17f %6.17f %6.17f\n", p[j][0], p[j][1], p[j][2] );
1286 /* set up brush sides */
1287 buildBrush->numsides = 5;
1288 buildBrush->sides[ 0 ].shaderInfo = si;
1289 for ( j = 1; j < buildBrush->numsides; j++ ) {
1291 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
1292 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
1295 buildBrush->sides[ j ].shaderInfo = NULL; // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
1298 VectorCopy( points[0], points[3] ); // for cyclic usage
1300 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
1301 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
1302 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
1303 buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
1304 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 0, NULL );
1309 if ( CreateBrushWindings( buildBrush ) ) {
1311 //% EmitBrushes( buildBrush, NULL, NULL );
1312 buildBrush->next = entities[ mapEntityNum ].brushes;
1313 entities[ mapEntityNum ].brushes = buildBrush;
1314 entities[ mapEntityNum ].numBrushes++;
1317 Sys_Printf( "WARNING: triangle (%6.0f %6.0f %6.0f) (%6.0f %6.0f %6.0f) (%6.0f %6.0f %6.0f) of %s was not autoclipped\n", points[0][0], points[0][1], points[0][2], points[1][0], points[1][1], points[1][2], points[2][0], points[2][1], points[2][2], name );
1322 normalEpsilon = normalEpsilon_save;
1324 else if ( spawnFlags & 8090 ){
1325 Sys_Printf( "WARNING: nonexistent clipping mode selected\n" );
1334 adds misc_model surfaces to the bsp
1337 void AddTriangleModels( entity_t *e ){
1338 int num, frame, skin, castShadows, recvShadows, spawnFlags;
1340 const char *targetName;
1341 const char *target, *model, *value;
1342 char shader[ MAX_QPATH ];
1343 shaderInfo_t *celShader;
1344 float temp, baseLightmapScale, lightmapScale, clipDepth;
1346 int lightmapSampleSize;
1347 vec3_t origin, scale, angles;
1350 remap_t *remap, *remap2;
1355 Sys_FPrintf( SYS_VRB, "--- AddTriangleModels ---\n" );
1358 /* get current brush entity targetname */
1359 if ( e == entities ) {
1364 targetName = ValueForKey( e, "targetname" );
1366 /* misc_model entities target non-worldspawn brush model entities */
1367 if ( targetName[ 0 ] == '\0' ) {
1372 /* get lightmap scale */
1373 /* vortex: added _ls key (short name of lightmapscale) */
1374 baseLightmapScale = 0.0f;
1375 if ( strcmp( "", ValueForKey( e, "lightmapscale" ) ) ||
1376 strcmp( "", ValueForKey( e, "_lightmapscale" ) ) ||
1377 strcmp( "", ValueForKey( e, "_ls" ) ) ) {
1378 baseLightmapScale = FloatForKey( e, "lightmapscale" );
1379 if ( baseLightmapScale <= 0.0f ) {
1380 baseLightmapScale = FloatForKey( e, "_lightmapscale" );
1382 if ( baseLightmapScale <= 0.0f ) {
1383 baseLightmapScale = FloatForKey( e, "_ls" );
1385 if ( baseLightmapScale < 0.0f ) {
1386 baseLightmapScale = 0.0f;
1388 if ( baseLightmapScale > 0.0f ) {
1389 Sys_Printf( "World Entity has lightmap scale of %.4f\n", baseLightmapScale );
1393 /* walk the entity list */
1394 for ( num = 1; num < numEntities; num++ )
1397 e2 = &entities[ num ];
1399 /* convert misc_models into raw geometry */
1400 if ( Q_stricmp( "misc_model", ValueForKey( e2, "classname" ) ) ) {
1404 /* ydnar: added support for md3 models on non-worldspawn models */
1405 target = ValueForKey( e2, "target" );
1406 if ( strcmp( target, targetName ) ) {
1410 /* get model name */
1411 model = ValueForKey( e2, "model" );
1412 if ( model[ 0 ] == '\0' ) {
1413 Sys_FPrintf( SYS_WRN, "WARNING: misc_model at %i %i %i without a model key\n",
1414 (int) origin[ 0 ], (int) origin[ 1 ], (int) origin[ 2 ] );
1418 /* get model frame */
1420 if ( strcmp( "", ValueForKey( e2, "_frame" ) ) ) {
1421 frame = IntForKey( e2, "_frame" );
1423 else if ( strcmp( "", ValueForKey( e2, "frame" ) ) ) {
1424 frame = IntForKey( e2, "frame" );
1427 /* worldspawn (and func_groups) default to cast/recv shadows in worldspawn group */
1428 if ( e == entities ) {
1429 castShadows = WORLDSPAWN_CAST_SHADOWS;
1430 recvShadows = WORLDSPAWN_RECV_SHADOWS;
1433 /* other entities don't cast any shadows, but recv worldspawn shadows */
1436 castShadows = ENTITY_CAST_SHADOWS;
1437 recvShadows = ENTITY_RECV_SHADOWS;
1440 /* get explicit shadow flags */
1441 GetEntityShadowFlags( e2, e, &castShadows, &recvShadows );
1443 /* get spawnflags */
1444 spawnFlags = IntForKey( e2, "spawnflags" );
1447 GetVectorForKey( e2, "origin", origin );
1448 VectorSubtract( origin, e->origin, origin ); /* offset by parent */
1451 scale[ 0 ] = scale[ 1 ] = scale[ 2 ] = 1.0f;
1452 temp = FloatForKey( e2, "modelscale" );
1453 if ( temp != 0.0f ) {
1454 scale[ 0 ] = scale[ 1 ] = scale[ 2 ] = temp;
1456 value = ValueForKey( e2, "modelscale_vec" );
1457 if ( value[ 0 ] != '\0' ) {
1458 sscanf( value, "%f %f %f", &scale[ 0 ], &scale[ 1 ], &scale[ 2 ] );
1461 /* get "angle" (yaw) or "angles" (pitch yaw roll) */
1462 angles[ 0 ] = angles[ 1 ] = angles[ 2 ] = 0.0f;
1463 angles[ 2 ] = FloatForKey( e2, "angle" );
1464 value = ValueForKey( e2, "angles" );
1465 if ( value[ 0 ] != '\0' ) {
1466 sscanf( value, "%f %f %f", &angles[ 1 ], &angles[ 2 ], &angles[ 0 ] );
1469 /* set transform matrix (thanks spog) */
1470 m4x4_identity( transform );
1471 m4x4_pivoted_transform_by_vec3( transform, origin, angles, eXYZ, scale, vec3_origin );
1473 /* get shader remappings */
1475 for ( ep = e2->epairs; ep != NULL; ep = ep->next )
1477 /* look for keys prefixed with "_remap" */
1478 if ( ep->key != NULL && ep->value != NULL &&
1479 ep->key[ 0 ] != '\0' && ep->value[ 0 ] != '\0' &&
1480 !Q_strncasecmp( ep->key, "_remap", 6 ) ) {
1481 /* create new remapping */
1483 remap = safe_malloc( sizeof( *remap ) );
1484 remap->next = remap2;
1485 strcpy( remap->from, ep->value );
1487 /* split the string */
1488 split = strchr( remap->from, ';' );
1489 if ( split == NULL ) {
1490 Sys_FPrintf( SYS_WRN, "WARNING: Shader _remap key found in misc_model without a ; character\n" );
1496 /* store the split */
1498 strcpy( remap->to, ( split + 1 ) );
1501 //% Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", remap->from, remap->to );
1505 /* ydnar: cel shader support */
1506 value = ValueForKey( e2, "_celshader" );
1507 if ( value[ 0 ] == '\0' ) {
1508 value = ValueForKey( &entities[ 0 ], "_celshader" );
1510 if ( value[ 0 ] != '\0' ) {
1511 sprintf( shader, "textures/%s", value );
1512 celShader = ShaderInfoForShader( shader );
1515 celShader = *globalCelShader ? ShaderInfoForShader( globalCelShader ) : NULL;
1518 /* jal : entity based _samplesize */
1519 lightmapSampleSize = 0;
1520 if ( strcmp( "", ValueForKey( e2, "_lightmapsamplesize" ) ) ) {
1521 lightmapSampleSize = IntForKey( e2, "_lightmapsamplesize" );
1523 else if ( strcmp( "", ValueForKey( e2, "_samplesize" ) ) ) {
1524 lightmapSampleSize = IntForKey( e2, "_samplesize" );
1526 else if ( strcmp( "", ValueForKey( e2, "_ss" ) ) ) {
1527 lightmapSampleSize = IntForKey( e2, "_ss" );
1530 if ( lightmapSampleSize < 0 ) {
1531 lightmapSampleSize = 0;
1534 if ( lightmapSampleSize > 0.0f ) {
1535 Sys_Printf( "misc_model has lightmap sample size of %.d\n", lightmapSampleSize );
1538 /* get lightmap scale */
1539 /* vortex: added _ls key (short name of lightmapscale) */
1540 lightmapScale = 0.0f;
1541 if ( strcmp( "", ValueForKey( e2, "lightmapscale" ) ) ||
1542 strcmp( "", ValueForKey( e2, "_lightmapscale" ) ) ||
1543 strcmp( "", ValueForKey( e2, "_ls" ) ) ) {
1544 lightmapScale = FloatForKey( e2, "lightmapscale" );
1545 if ( lightmapScale <= 0.0f ) {
1546 lightmapScale = FloatForKey( e2, "_lightmapscale" );
1548 if ( lightmapScale <= 0.0f ) {
1549 lightmapScale = FloatForKey( e2, "_ls" );
1551 if ( lightmapScale < 0.0f ) {
1552 lightmapScale = 0.0f;
1554 if ( lightmapScale > 0.0f ) {
1555 Sys_Printf( "misc_model has lightmap scale of %.4f\n", lightmapScale );
1559 /* jal : entity based _shadeangle */
1561 if ( strcmp( "", ValueForKey( e2, "_shadeangle" ) ) ) {
1562 shadeAngle = FloatForKey( e2, "_shadeangle" );
1564 /* vortex' aliases */
1565 else if ( strcmp( "", ValueForKey( e2, "_smoothnormals" ) ) ) {
1566 shadeAngle = FloatForKey( e2, "_smoothnormals" );
1568 else if ( strcmp( "", ValueForKey( e2, "_sn" ) ) ) {
1569 shadeAngle = FloatForKey( e2, "_sn" );
1571 else if ( strcmp( "", ValueForKey( e2, "_sa" ) ) ) {
1572 shadeAngle = FloatForKey( e2, "_sa" );
1574 else if ( strcmp( "", ValueForKey( e2, "_smooth" ) ) ) {
1575 shadeAngle = FloatForKey( e2, "_smooth" );
1578 if ( shadeAngle < 0.0f ) {
1582 if ( shadeAngle > 0.0f ) {
1583 Sys_Printf( "misc_model has shading angle of %.4f\n", shadeAngle );
1587 if ( strcmp( "", ValueForKey( e2, "_skin" ) ) ) {
1588 skin = IntForKey( e2, "_skin" );
1590 else if ( strcmp( "", ValueForKey( e2, "skin" ) ) ) {
1591 skin = IntForKey( e2, "skin" );
1594 clipDepth = clipDepthGlobal;
1595 if ( strcmp( "", ValueForKey( e2, "_clipdepth" ) ) ) {
1596 clipDepth = FloatForKey( e2, "_clipdepth" );
1597 Sys_Printf( "misc_model has autoclip depth of %.3f\n", clipDepth );
1601 /* insert the model */
1602 InsertModel( model, skin, frame, transform, remap, celShader, mapEntityNum, castShadows, recvShadows, spawnFlags, lightmapScale, lightmapSampleSize, shadeAngle, clipDepth );
1604 /* free shader remappings */
1605 while ( remap != NULL )
1607 remap2 = remap->next;