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_malloc( ds->numVerts * sizeof( ds->verts[ 0 ] ) );
451 memset( ds->verts, 0, ds->numVerts * sizeof( ds->verts[ 0 ] ) );
453 ds->numIndexes = PicoGetSurfaceNumIndexes( surface );
454 ds->indexes = safe_malloc( ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
455 memset( ds->indexes, 0, ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
458 for ( i = 0; i < ds->numVerts; i++ )
461 dv = &ds->verts[ i ];
464 xyz = PicoGetSurfaceXYZ( surface, i );
465 VectorCopy( xyz, dv->xyz );
466 m4x4_transform_point( transform, dv->xyz );
468 normal = PicoGetSurfaceNormal( surface, i );
469 VectorCopy( normal, dv->normal );
470 m4x4_transform_normal( nTransform, dv->normal );
471 VectorNormalize( dv->normal, dv->normal );
473 /* ydnar: tek-fu celshading support for flat shaded shit */
475 dv->st[ 0 ] = si->stFlat[ 0 ];
476 dv->st[ 1 ] = si->stFlat[ 1 ];
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 );
486 /* normal texture coordinates */
489 st = PicoGetSurfaceST( surface, 0, i );
490 dv->st[ 0 ] = st[ 0 ];
491 dv->st[ 1 ] = st[ 1 ];
494 /* set lightmap/color bits */
495 color = PicoGetSurfaceColor( surface, 0, i );
496 for ( j = 0; j < MAX_LIGHTMAPS; j++ )
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 );
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 ];
517 indexes = PicoGetSurfaceIndexes( surface, 0 );
518 for ( i = 0; i < ds->numIndexes; i++ )
519 ds->indexes[ i ] = indexes[ i ];
522 ds->celShader = celShader;
524 /* ydnar: giant hack land: generate clipping brushes for model triangles */
525 if ( ( si->clipModel && !( spf ) ) || //default CLIPMODEL
526 ( ( spawnFlags & 8090 ) == 2 ) || //default CLIPMODEL
527 ( spf == 8 ) || //EXTRUDE_FACE_NORMALS
528 ( spf == 16 ) || //EXTRUDE_TERRAIN
529 ( spf == 128 ) || //EXTRUDE_VERTEX_NORMALS
530 ( spf == 256 ) || //PYRAMIDAL_CLIP
531 ( spf == 512 ) || //EXTRUDE_DOWNWARDS
532 ( spf == 1024 ) || //EXTRUDE_UPWARDS
533 ( spf == 4096 ) || //default CLIPMODEL + AXIAL_BACKPLANE
534 ( spf == 264 ) || //EXTRUDE_FACE_NORMALS+PYRAMIDAL_CLIP (extrude 45)
535 ( spf == 2064 ) || //EXTRUDE_TERRAIN+MAX_EXTRUDE
536 ( spf == 4112 ) || //EXTRUDE_TERRAIN+AXIAL_BACKPLANE
537 ( spf == 384 ) || //EXTRUDE_VERTEX_NORMALS + PYRAMIDAL_CLIP - vertex normals + don't check for sides, sticking outwards
538 ( spf == 4352 ) || //PYRAMIDAL_CLIP+AXIAL_BACKPLANE
539 ( spf == 1536 ) || //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS
540 ( spf == 2560 ) || //EXTRUDE_DOWNWARDS+MAX_EXTRUDE
541 ( spf == 4608 ) || //EXTRUDE_DOWNWARDS+AXIAL_BACKPLANE
542 ( spf == 3584 ) || //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS+MAX_EXTRUDE
543 ( spf == 5632 ) || //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS+AXIAL_BACKPLANE
544 ( spf == 3072 ) || //EXTRUDE_UPWARDS+MAX_EXTRUDE
545 ( spf == 5120 ) ){ //EXTRUDE_UPWARDS+AXIAL_BACKPLANE
546 vec3_t points[ 4 ], backs[ 3 ], cnt, bestNormal, nrm, Vnorm[3], Enorm[3];
547 vec4_t plane, reverse, p[3];
548 double normalEpsilon_save;
550 vec3_t min = { 999999, 999999, 999999 }, max = { -999999, -999999, -999999 };
551 vec3_t avgDirection = { 0, 0, 0 };
553 #define nonax_clip_dbg 0
556 if ( !si->clipModel && !( si->compileFlags & C_SOLID ) ) {
560 //wont snap these in normal way, or will explode
561 normalEpsilon_save = normalEpsilon;
562 //normalEpsilon = 0.000001;
565 //MAX_EXTRUDE or EXTRUDE_TERRAIN
566 if ( ( spawnFlags & 2048 ) || ( spawnFlags & 16 ) ){
568 for ( i = 0; i < ds->numIndexes; i += 3 )
570 for ( j = 0; j < 3; j++ )
572 dv = &ds->verts[ ds->indexes[ i + j ] ];
573 VectorCopy( dv->xyz, points[ j ] );
575 if ( PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] ) ){
576 if ( spawnFlags & 16 ) VectorAdd( avgDirection, plane, avgDirection ); //calculate average mesh facing direction
579 for ( k = 2; k > -1; k-- ){
581 for ( j = 0; j < 3; j++ ){ if ( points[j][k] < min[k] ) min[k] = points[j][k]; }
583 else if ( plane[k] < 0 ){
584 for ( j = 0; j < 3; j++ ){ if ( points[j][k] > max[k] ) max[k] = points[j][k]; }
586 //if EXTRUDE_DOWNWARDS or EXTRUDE_UPWARDS
587 if ( ( spawnFlags & 512 ) || ( spawnFlags & 1024 ) ){
593 //unify avg direction
594 if ( spawnFlags & 16 ){
595 for ( j = 0; j < 3; j++ ){
596 if ( fabs(avgDirection[j]) > fabs(avgDirection[(j+1)%3]) ){
597 avgDirection[(j+1)%3] = 0.0;
601 avgDirection[j] = 0.0;
604 if ( VectorNormalize( avgDirection, avgDirection ) == 0 ){
606 VectorSet( avgDirection, 0, 0, 1 );
611 /* walk triangle list */
612 for ( i = 0; i < ds->numIndexes; i += 3 )
615 AUTOEXPAND_BY_REALLOC( mapplanes, ( nummapplanes + 64 ) << 1, allocatedmapplanes, 1024 );
618 for ( j = 0; j < 3; j++ )
621 dv = &ds->verts[ ds->indexes[ i + j ] ];
624 VectorCopy( dv->xyz, points[ j ] );
627 /* make plane for triangle */
628 if ( PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] ) ) {
631 buildBrush = AllocBrush( 48 );
632 buildBrush->entityNum = mapEntityNum;
633 buildBrush->original = buildBrush;
634 buildBrush->contentShader = si;
635 buildBrush->compileFlags = si->compileFlags;
636 buildBrush->contentFlags = si->contentFlags;
637 buildBrush->detail = qtrue;
639 //snap points before using them for further calculations
640 //precision suffers a lot, when two of normal values are under .00025 (often no collision, knocking up effect in ioq3)
641 //also broken drawsurfs in case of normal brushes
643 for ( j=0; j<3; j++ )
645 if ( fabs(plane[j]) < 0.00025 && fabs(plane[(j+1)%3]) < 0.00025 && ( plane[j] != 0.0 || plane[(j+1)%3] != 0.0 ) ){
646 VectorAdd( points[ 0 ], points[ 1 ], cnt );
647 VectorAdd( cnt, points[ 2 ], cnt );
648 VectorScale( cnt, 0.3333333333333f, cnt );
649 points[0][(j+2)%3]=points[1][(j+2)%3]=points[2][(j+2)%3]=cnt[(j+2)%3];
655 //snap pairs of points to prevent bad side planes
656 for ( j=0; j<3; j++ )
658 VectorSubtract( points[j], points[(j+1)%3], nrm );
659 VectorNormalize( nrm, nrm );
660 for ( k=0; k<3; k++ )
662 if ( nrm[k] != 0.0 && fabs(nrm[k]) < 0.00025 ){
663 //Sys_Printf( "b4(%6.6f %6.6f %6.6f)(%6.6f %6.6f %6.6f)\n", points[j][0], points[j][1], points[j][2], points[(j+1)%3][0], points[(j+1)%3][1], points[(j+1)%3][2] );
664 points[j][k]=points[(j+1)%3][k] = ( points[j][k] + points[(j+1)%3][k] ) / 2.0;
665 //Sys_Printf( "sn(%6.6f %6.6f %6.6f)(%6.6f %6.6f %6.6f)\n", points[j][0], points[j][1], points[j][2], points[(j+1)%3][0], points[(j+1)%3][1], points[(j+1)%3][2] );
672 PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] );
676 //vector-is-close-to-be-on-axis check again, happens after previous code sometimes
677 for ( j=0; j<3; j++ )
679 if ( fabs(plane[j]) < 0.00025 && fabs(plane[(j+1)%3]) < 0.00025 && ( plane[j] != 0.0 || plane[(j+1)%3] != 0.0 ) ){
680 VectorAdd( points[ 0 ], points[ 1 ], cnt );
681 VectorAdd( cnt, points[ 2 ], cnt );
682 VectorScale( cnt, 0.3333333333333f, cnt );
683 points[0][(j+2)%3]=points[1][(j+2)%3]=points[2][(j+2)%3]=cnt[(j+2)%3];
684 PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] );
689 //snap single snappable normal components
690 for ( j=0; j<3; j++ )
692 if ( plane[j] != 0.0 && fabs(plane[j]) < 0.00005 ){
700 VectorAdd( points[ 0 ], points[ 1 ], cnt );
701 VectorAdd( cnt, points[ 2 ], cnt );
702 VectorScale( cnt, 0.3333333333333f, cnt );
703 VectorNormalize( plane, plane );
704 plane[3] = DotProduct( plane, cnt );
706 //project points to resulting plane to keep intersections precision
707 for ( j=0; j<3; j++ )
709 //Sys_Printf( "b4 %i (%6.7f %6.7f %6.7f)\n", j, points[j][0], points[j][1], points[j][2] );
710 VectorMA( points[j], plane[3] - DotProduct( plane, points[j]), plane, points[j] );
711 //Sys_Printf( "sn %i (%6.7f %6.7f %6.7f)\n", j, points[j][0], points[j][1], points[j][2] );
713 //Sys_Printf( "sn pln (%6.7f %6.7f %6.7f %6.7f)\n", plane[0], plane[1], plane[2], plane[3] );
714 //PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] );
715 //Sys_Printf( "pts pln (%6.7f %6.7f %6.7f %6.7f)\n", plane[0], plane[1], plane[2], plane[3] );
719 if ( spf == 4352 ){ //PYRAMIDAL_CLIP+AXIAL_BACKPLANE
721 for ( j=0; j<3; j++ )
723 if ( fabs(plane[j]) < 0.05 && fabs(plane[(j+1)%3]) < 0.05 ){ //no way, close to lay on two axises
724 goto default_CLIPMODEL;
729 VectorCopy( plane, bestNormal );
730 for ( j = 0; j < 3; j++ ){
731 if ( fabs(bestNormal[j]) > fabs(bestNormal[(j+1)%3]) ){
732 bestNormal[(j+1)%3] = 0.0;
739 VectorNormalize( bestNormal, bestNormal );
742 float bestdist, currdist, bestangle, currangle, mindist = 999999;
744 for ( j = 0; j < 3; j++ ){//planes
747 for ( k = 0; k < 3; k++ ){//axises
748 VectorSubtract( points[ (j+1)%3 ], points[ j ], nrm );
750 CrossProduct( bestNormal, nrm, reverse );
753 VectorClear( Vnorm[0] );
754 if ( (k+1)%3 == axis ){
755 if ( nrm[ (k+2)%3 ] == 0 ) continue;
756 Vnorm[0][ (k+2)%3 ] = nrm[ (k+2)%3 ];
759 if ( nrm[ (k+1)%3 ] == 0 ) continue;
760 Vnorm[0][ (k+1)%3 ] = nrm[ (k+1)%3 ];
762 CrossProduct( bestNormal, Vnorm[0], Enorm[0] );
763 CrossProduct( Enorm[0], nrm, reverse );
765 VectorNormalize( reverse, reverse );
766 reverse[3] = DotProduct( points[ j ], reverse );
767 //check facing, thickness
768 currdist = reverse[3] - DotProduct( reverse, points[ (j+2)%3 ] );
769 currangle = DotProduct( reverse, plane );
770 if ( ( ( currdist > 0.1 ) && ( currdist < bestdist ) && ( currangle < 0 ) ) ||
771 ( ( currangle >= 0 ) && ( currangle <= bestangle ) ) ){
772 bestangle = currangle;
773 if ( currangle < 0 ) bestdist = currdist;
774 VectorCopy( reverse, p[j] );
775 p[j][3] = reverse[3];
778 //if ( bestdist == 999999 && bestangle == 1 ) Sys_Printf("default_CLIPMODEL\n");
779 if ( bestdist == 999999 && bestangle == 1 ) goto default_CLIPMODEL;
780 if ( bestdist < mindist ) mindist = bestdist;
782 if ( (limDepth != 0.0) && (mindist > limDepth) ) goto default_CLIPMODEL;
786 for ( j = 0; j < 3; j++ )
788 for ( k = 0; k < 3; k++ )
790 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
791 Sys_Printf( "nonax nrm %6.17f %6.17f %6.17f\n", p[j][0], p[j][1], p[j][2] );
796 /* set up brush sides */
797 buildBrush->numsides = 4;
798 buildBrush->sides[ 0 ].shaderInfo = si;
799 for ( j = 1; j < buildBrush->numsides; j++ ) {
801 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
802 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
805 buildBrush->sides[ j ].shaderInfo = NULL; // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
808 VectorCopy( points[0], points[3] ); // for cyclic usage
810 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
811 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
812 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
813 buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
817 else if ( ( spf == 16 ) || //EXTRUDE_TERRAIN
818 ( spf == 512 ) || //EXTRUDE_DOWNWARDS
819 ( spf == 1024 ) || //EXTRUDE_UPWARDS
820 ( spf == 4096 ) || //default CLIPMODEL + AXIAL_BACKPLANE
821 ( spf == 2064 ) || //EXTRUDE_TERRAIN+MAX_EXTRUDE
822 ( spf == 4112 ) || //EXTRUDE_TERRAIN+AXIAL_BACKPLANE
823 ( spf == 1536 ) || //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS
824 ( spf == 2560 ) || //EXTRUDE_DOWNWARDS+MAX_EXTRUDE
825 ( spf == 4608 ) || //EXTRUDE_DOWNWARDS+AXIAL_BACKPLANE
826 ( spf == 3584 ) || //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS+MAX_EXTRUDE
827 ( spf == 5632 ) || //EXTRUDE_DOWNWARDS+EXTRUDE_UPWARDS+AXIAL_BACKPLANE
828 ( spf == 3072 ) || //EXTRUDE_UPWARDS+MAX_EXTRUDE
829 ( spf == 5120 ) ){ //EXTRUDE_UPWARDS+AXIAL_BACKPLANE
831 if ( spawnFlags & 16 ){ //autodirection
832 VectorCopy( avgDirection, bestNormal );
836 if ( ( spawnFlags & 1536 ) == 1536 ){ //up+down
837 VectorSet( bestNormal, 0, 0, ( plane[2] >= 0 ? 1 : -1 ) );
839 else if ( spawnFlags & 512 ){ //down
840 VectorSet( bestNormal, 0, 0, 1 );
843 else if ( spawnFlags & 1024 ){ //up
844 VectorSet( bestNormal, 0, 0, -1 );
846 else{ // best axial normal
847 VectorCopy( plane, bestNormal );
848 for ( j = 0; j < 3; j++ ){
849 if ( fabs(bestNormal[j]) > fabs(bestNormal[(j+1)%3]) ){
850 bestNormal[(j+1)%3] = 0.0;
857 VectorNormalize( bestNormal, bestNormal );
861 if ( DotProduct( plane, bestNormal ) < 0.05 ){
862 goto default_CLIPMODEL;
866 /* make side planes */
867 for ( j = 0; j < 3; j++ )
869 VectorSubtract( points[(j+1)%3], points[ j ], nrm );
870 CrossProduct( bestNormal, nrm, p[ j ] );
871 VectorNormalize( p[ j ], p[ j ] );
872 p[j][3] = DotProduct( points[j], p[j] );
875 /* make back plane */
876 if ( spawnFlags & 2048 ){ //max extrude
877 VectorScale( bestNormal, -1.0f, reverse );
878 if ( bestNormal[axis] > 0 ){
879 reverse[3] = -min[axis] + clipDepth;
882 reverse[3] = max[axis] + clipDepth;
885 else if ( spawnFlags & 4096 ){ //axial backplane
886 VectorScale( bestNormal, -1.0f, reverse );
887 reverse[3] = points[0][axis];
888 if ( bestNormal[axis] > 0 ){
889 for ( j = 1; j < 3; j++ ){
890 if ( points[j][axis] < reverse[3] ){
891 reverse[3] = points[j][axis];
894 reverse[3] = -reverse[3] + clipDepth;
897 for ( j = 1; j < 3; j++ ){
898 if ( points[j][axis] > reverse[3] ){
899 reverse[3] = points[j][axis];
902 reverse[3] += clipDepth;
904 if (limDepth != 0.0){
905 VectorCopy( points[0], cnt );
906 if ( bestNormal[axis] > 0 ){
907 for ( j = 1; j < 3; j++ ){
908 if ( points[j][axis] > cnt[axis] ){
909 VectorCopy( points[j], cnt );
914 for ( j = 1; j < 3; j++ ){
915 if ( points[j][axis] < cnt[axis] ){
916 VectorCopy( points[j], cnt );
920 VectorMA( cnt, reverse[3] - DotProduct( reverse, cnt ), reverse, cnt );
921 if ( ( plane[3] - DotProduct( plane, cnt ) ) > limDepth ){
922 VectorScale( plane, -1.0f, reverse );
923 reverse[ 3 ] = -plane[ 3 ];
924 reverse[3] += clipDepth;
928 else{ //normal backplane
929 VectorScale( plane, -1.0f, reverse );
930 reverse[ 3 ] = -plane[ 3 ];
931 reverse[3] += clipDepth;
934 for ( j = 0; j < 3; j++ )
936 for ( k = 0; k < 3; k++ )
938 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
939 Sys_Printf( "nonax nrm %6.17f %6.17f %6.17f\n", p[j][0], p[j][1], p[j][2] );
944 /* set up brush sides */
945 buildBrush->numsides = 5;
946 buildBrush->sides[ 0 ].shaderInfo = si;
947 for ( j = 1; j < buildBrush->numsides; j++ ) {
949 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
950 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
953 buildBrush->sides[ j ].shaderInfo = NULL; // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
956 VectorCopy( points[0], points[3] ); // for cyclic usage
958 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
959 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
960 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
961 buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
962 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 0, NULL );
966 else if ( spf == 264 ){ //EXTRUDE_FACE_NORMALS+PYRAMIDAL_CLIP (extrude 45)
968 //45 degrees normals for side planes
969 for ( j = 0; j < 3; j++ )
971 VectorSubtract( points[(j+1)%3], points[ j ], nrm );
972 CrossProduct( plane, nrm, Enorm[ j ] );
973 VectorNormalize( Enorm[ j ], Enorm[ j ] );
974 VectorAdd( plane, Enorm[ j ], Enorm[ j ] );
975 VectorNormalize( Enorm[ j ], Enorm[ j ] );
976 /* make side planes */
977 CrossProduct( Enorm[ j ], nrm, p[ j ] );
978 VectorNormalize( p[ j ], p[ j ] );
979 p[j][3] = DotProduct( points[j], p[j] );
980 //snap nearly axial side planes
982 for ( k = 0; k < 3; k++ )
984 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
990 VectorNormalize( p[j], p[j] );
991 VectorAdd( points[j], points[(j+1)%3], cnt );
992 VectorScale( cnt, 0.5f, cnt );
993 p[j][3] = DotProduct( cnt, p[j] );
997 /* make back plane */
998 VectorScale( plane, -1.0f, reverse );
999 reverse[ 3 ] = -plane[ 3 ];
1000 reverse[3] += clipDepth;
1002 /* set up brush sides */
1003 buildBrush->numsides = 5;
1004 buildBrush->sides[ 0 ].shaderInfo = si;
1005 for ( j = 1; j < buildBrush->numsides; j++ ) {
1007 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
1008 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
1011 buildBrush->sides[ j ].shaderInfo = NULL; // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
1014 VectorCopy( points[0], points[3] ); // for cyclic usage
1016 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
1017 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
1018 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
1019 buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
1020 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 0, NULL );
1024 else if ( ( spf == 128 ) || //EXTRUDE_VERTEX_NORMALS
1025 ( spf == 384 ) ){ //EXTRUDE_VERTEX_NORMALS + PYRAMIDAL_CLIP - vertex normals + don't check for sides, sticking outwards
1026 /* get vertex normals */
1027 for ( j = 0; j < 3; j++ )
1030 dv = &ds->verts[ ds->indexes[ i + j ] ];
1032 VectorCopy( dv->normal, Vnorm[ j ] );
1035 //avg normals for side planes
1036 for ( j = 0; j < 3; j++ )
1038 VectorAdd( Vnorm[ j ], Vnorm[ (j+1)%3 ], Enorm[ j ] );
1039 VectorNormalize( Enorm[ j ], Enorm[ j ] );
1040 //check fuer bad ones
1041 VectorSubtract( points[(j+1)%3], points[ j ], cnt );
1042 CrossProduct( plane, cnt, nrm );
1043 VectorNormalize( nrm, nrm );
1044 //check for negative or outside direction
1045 if ( DotProduct( Enorm[ j ], plane ) > 0.1 ){
1046 if ( ( DotProduct( Enorm[ j ], nrm ) > -0.2 ) || ( spawnFlags & 256 ) ){
1052 //Sys_Printf( "faulty Enormal %i/%i\n", notok, ok );
1054 VectorAdd( plane, nrm, Enorm[ j ] );
1055 VectorNormalize( Enorm[ j ], Enorm[ j ] );
1058 /* make side planes */
1059 for ( j = 0; j < 3; j++ )
1061 VectorSubtract( points[(j+1)%3], points[ j ], nrm );
1062 CrossProduct( Enorm[ j ], nrm, p[ j ] );
1063 VectorNormalize( p[ j ], p[ j ] );
1064 p[j][3] = DotProduct( points[j], p[j] );
1065 //snap nearly axial side planes
1067 for ( k = 0; k < 3; k++ )
1069 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
1070 //Sys_Printf( "init plane %6.8f %6.8f %6.8f %6.8f\n", p[j][0], p[j][1], p[j][2], p[j][3]);
1076 VectorNormalize( p[j], p[j] );
1077 //Sys_Printf( "nrm plane %6.8f %6.8f %6.8f %6.8f\n", p[j][0], p[j][1], p[j][2], p[j][3]);
1078 VectorAdd( points[j], points[(j+1)%3], cnt );
1079 VectorScale( cnt, 0.5f, cnt );
1080 p[j][3] = DotProduct( cnt, p[j] );
1081 //Sys_Printf( "dst plane %6.8f %6.8f %6.8f %6.8f\n", p[j][0], p[j][1], p[j][2], p[j][3]);
1085 /* make back plane */
1086 VectorScale( plane, -1.0f, reverse );
1087 reverse[ 3 ] = -plane[ 3 ];
1088 reverse[3] += clipDepth;
1090 /* set up brush sides */
1091 buildBrush->numsides = 5;
1092 buildBrush->sides[ 0 ].shaderInfo = si;
1093 for ( j = 1; j < buildBrush->numsides; j++ ) {
1095 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
1096 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
1099 buildBrush->sides[ j ].shaderInfo = NULL; // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
1102 VectorCopy( points[0], points[3] ); // for cyclic usage
1104 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
1105 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
1106 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
1107 buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
1108 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 0, NULL );
1112 else if ( spf == 8 ){ //EXTRUDE_FACE_NORMALS
1114 /* make side planes */
1115 for ( j = 0; j < 3; j++ )
1117 VectorSubtract( points[(j+1)%3], points[ j ], nrm );
1118 CrossProduct( plane, nrm, p[ j ] );
1119 VectorNormalize( p[ j ], p[ j ] );
1120 p[j][3] = DotProduct( points[j], p[j] );
1121 //snap nearly axial side planes
1123 for ( k = 0; k < 3; k++ )
1125 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
1126 //Sys_Printf( "init plane %6.8f %6.8f %6.8f %6.8f\n", p[j][0], p[j][1], p[j][2], p[j][3]);
1132 VectorNormalize( p[j], p[j] );
1133 //Sys_Printf( "nrm plane %6.8f %6.8f %6.8f %6.8f\n", p[j][0], p[j][1], p[j][2], p[j][3]);
1134 VectorAdd( points[j], points[(j+1)%3], cnt );
1135 VectorScale( cnt, 0.5f, cnt );
1136 p[j][3] = DotProduct( cnt, p[j] );
1137 //Sys_Printf( "dst plane %6.8f %6.8f %6.8f %6.8f\n", p[j][0], p[j][1], p[j][2], p[j][3]);
1141 /* make back plane */
1142 VectorScale( plane, -1.0f, reverse );
1143 reverse[ 3 ] = -plane[ 3 ];
1144 reverse[3] += clipDepth;
1146 for ( j = 0; j < 3; j++ )
1148 for ( k = 0; k < 3; k++ )
1150 if ( fabs(p[j][k]) < 0.00005 && p[j][k] != 0.0 ){
1151 Sys_Printf( "nonax nrm %6.17f %6.17f %6.17f\n", p[j][0], p[j][1], p[j][2] );
1152 Sys_Printf( "frm src nrm %6.17f %6.17f %6.17f\n", plane[0], plane[1], plane[2]);
1157 /* set up brush sides */
1158 buildBrush->numsides = 5;
1159 buildBrush->sides[ 0 ].shaderInfo = si;
1160 for ( j = 1; j < buildBrush->numsides; j++ ) {
1162 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
1163 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
1166 buildBrush->sides[ j ].shaderInfo = NULL; // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
1169 VectorCopy( points[0], points[3] ); // for cyclic usage
1171 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
1172 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
1173 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
1174 buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
1175 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 0, NULL );
1179 else if ( spf == 256 ){ //PYRAMIDAL_CLIP
1181 /* calculate center */
1182 VectorAdd( points[ 0 ], points[ 1 ], cnt );
1183 VectorAdd( cnt, points[ 2 ], cnt );
1184 VectorScale( cnt, 0.3333333333333f, cnt );
1186 /* make back pyramid point */
1187 VectorMA( cnt, -clipDepth, plane, cnt );
1189 /* make 3 more planes */
1190 if( PlaneFromPoints( p[0], points[ 2 ], points[ 1 ], cnt ) &&
1191 PlaneFromPoints( p[1], points[ 1 ], points[ 0 ], cnt ) &&
1192 PlaneFromPoints( p[2], points[ 0 ], points[ 2 ], cnt ) ) {
1194 //check for dangerous planes
1195 while( (( p[0][0] != 0.0 || p[0][1] != 0.0 ) && fabs(p[0][0]) < 0.00025 && fabs(p[0][1]) < 0.00025) ||
1196 (( p[0][0] != 0.0 || p[0][2] != 0.0 ) && fabs(p[0][0]) < 0.00025 && fabs(p[0][2]) < 0.00025) ||
1197 (( p[0][2] != 0.0 || p[0][1] != 0.0 ) && fabs(p[0][2]) < 0.00025 && fabs(p[0][1]) < 0.00025) ||
1198 (( p[1][0] != 0.0 || p[1][1] != 0.0 ) && fabs(p[1][0]) < 0.00025 && fabs(p[1][1]) < 0.00025) ||
1199 (( p[1][0] != 0.0 || p[1][2] != 0.0 ) && fabs(p[1][0]) < 0.00025 && fabs(p[1][2]) < 0.00025) ||
1200 (( p[1][2] != 0.0 || p[1][1] != 0.0 ) && fabs(p[1][2]) < 0.00025 && fabs(p[1][1]) < 0.00025) ||
1201 (( p[2][0] != 0.0 || p[2][1] != 0.0 ) && fabs(p[2][0]) < 0.00025 && fabs(p[2][1]) < 0.00025) ||
1202 (( p[2][0] != 0.0 || p[2][2] != 0.0 ) && fabs(p[2][0]) < 0.00025 && fabs(p[2][2]) < 0.00025) ||
1203 (( p[2][2] != 0.0 || p[2][1] != 0.0 ) && fabs(p[2][2]) < 0.00025 && fabs(p[2][1]) < 0.00025) ) {
1204 VectorMA( cnt, -0.1f, plane, cnt );
1205 // Sys_Printf( "shifting pyramid point\n" );
1206 PlaneFromPoints( p[0], points[ 2 ], points[ 1 ], cnt );
1207 PlaneFromPoints( p[1], points[ 1 ], points[ 0 ], cnt );
1208 PlaneFromPoints( p[2], points[ 0 ], points[ 2 ], cnt );
1211 for ( j = 0; j < 3; j++ )
1213 for ( k = 0; k < 3; k++ )
1215 if ( fabs(p[j][k]) < 0.00005 && p[j][k] != 0.0 ){
1216 Sys_Printf( "nonax nrm %6.17f %6.17f %6.17f\n (%6.8f %6.8f %6.8f)\n (%6.8f %6.8f %6.8f)\n (%6.8f %6.8f %6.8f)\n", p[j][0], p[j][1], p[j][2], points[j][0], points[j][1], points[j][2], points[(j+1)%3][0], points[(j+1)%3][1], points[(j+1)%3][2], cnt[0], cnt[1], cnt[2] );
1221 /* set up brush sides */
1222 buildBrush->numsides = 4;
1223 buildBrush->sides[ 0 ].shaderInfo = si;
1224 for ( j = 1; j < buildBrush->numsides; j++ ) {
1226 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
1227 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
1230 buildBrush->sides[ j ].shaderInfo = NULL; // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
1233 VectorCopy( points[0], points[3] ); // for cyclic usage
1235 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
1236 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 1 ] ); // p[0] contains points[1] and points[2]
1237 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 0 ] ); // p[1] contains points[0] and points[1]
1238 buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
1242 Sys_Printf( "WARNING: triangle (%6.0f %6.0f %6.0f) (%6.0f %6.0f %6.0f) (%6.0f %6.0f %6.0f) of %s was not autoclipped\n", points[0][0], points[0][1], points[0][2], points[1][0], points[1][1], points[1][2], points[2][0], points[2][1], points[2][2], name );
1249 else if ( ( si->clipModel && !( spf ) ) || ( ( spawnFlags & 8090 ) == 2 ) ){ //default CLIPMODEL
1253 VectorCopy( plane, bestNormal );
1254 for ( j = 0; j < 3; j++ ){
1255 if ( fabs(bestNormal[j]) > fabs(bestNormal[(j+1)%3]) ){
1256 bestNormal[(j+1)%3] = 0.0;
1259 bestNormal[j] = 0.0;
1262 VectorNormalize( bestNormal, bestNormal );
1264 /* make side planes */
1265 for ( j = 0; j < 3; j++ )
1267 VectorSubtract( points[(j+1)%3], points[ j ], nrm );
1268 CrossProduct( bestNormal, nrm, p[ j ] );
1269 VectorNormalize( p[ j ], p[ j ] );
1270 p[j][3] = DotProduct( points[j], p[j] );
1273 /* make back plane */
1274 VectorScale( plane, -1.0f, reverse );
1275 reverse[ 3 ] = -plane[ 3 ];
1276 reverse[3] += DotProduct( bestNormal, plane ) * clipDepth;
1278 for ( j = 0; j < 3; j++ )
1280 for ( k = 0; k < 3; k++ )
1282 if ( fabs(p[j][k]) < 0.00025 && p[j][k] != 0.0 ){
1283 Sys_Printf( "nonax nrm %6.17f %6.17f %6.17f\n", p[j][0], p[j][1], p[j][2] );
1288 /* set up brush sides */
1289 buildBrush->numsides = 5;
1290 buildBrush->sides[ 0 ].shaderInfo = si;
1291 for ( j = 1; j < buildBrush->numsides; j++ ) {
1293 buildBrush->sides[ 0 ].shaderInfo = ShaderInfoForShader( "debugclip2" );
1294 buildBrush->sides[ j ].shaderInfo = ShaderInfoForShader( "debugclip" );
1297 buildBrush->sides[ j ].shaderInfo = NULL; // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
1300 VectorCopy( points[0], points[3] ); // for cyclic usage
1302 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
1303 buildBrush->sides[ 1 ].planenum = FindFloatPlane( p[0], p[0][ 3 ], 2, &points[ 0 ] ); // p[0] contains points[0] and points[1]
1304 buildBrush->sides[ 2 ].planenum = FindFloatPlane( p[1], p[1][ 3 ], 2, &points[ 1 ] ); // p[1] contains points[1] and points[2]
1305 buildBrush->sides[ 3 ].planenum = FindFloatPlane( p[2], p[2][ 3 ], 2, &points[ 2 ] ); // p[2] contains points[2] and points[0] (copied to points[3]
1306 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 0, NULL );
1311 if ( CreateBrushWindings( buildBrush ) ) {
1313 //% EmitBrushes( buildBrush, NULL, NULL );
1314 buildBrush->next = entities[ mapEntityNum ].brushes;
1315 entities[ mapEntityNum ].brushes = buildBrush;
1316 entities[ mapEntityNum ].numBrushes++;
1319 Sys_Printf( "WARNING: triangle (%6.0f %6.0f %6.0f) (%6.0f %6.0f %6.0f) (%6.0f %6.0f %6.0f) of %s was not autoclipped\n", points[0][0], points[0][1], points[0][2], points[1][0], points[1][1], points[1][2], points[2][0], points[2][1], points[2][2], name );
1324 normalEpsilon = normalEpsilon_save;
1326 else if ( spawnFlags & 8090 ){
1327 Sys_Printf( "WARNING: nonexistent clipping mode selected\n" );
1336 adds misc_model surfaces to the bsp
1339 void AddTriangleModels( entity_t *e ){
1340 int num, frame, skin, castShadows, recvShadows, spawnFlags;
1342 const char *targetName;
1343 const char *target, *model, *value;
1344 char shader[ MAX_QPATH ];
1345 shaderInfo_t *celShader;
1346 float temp, baseLightmapScale, lightmapScale, clipDepth;
1348 int lightmapSampleSize;
1349 vec3_t origin, scale, angles;
1352 remap_t *remap, *remap2;
1357 Sys_FPrintf( SYS_VRB, "--- AddTriangleModels ---\n" );
1360 /* get current brush entity targetname */
1361 if ( e == entities ) {
1366 targetName = ValueForKey( e, "targetname" );
1368 /* misc_model entities target non-worldspawn brush model entities */
1369 if ( targetName[ 0 ] == '\0' ) {
1374 /* get lightmap scale */
1375 /* vortex: added _ls key (short name of lightmapscale) */
1376 baseLightmapScale = 0.0f;
1377 if ( strcmp( "", ValueForKey( e, "lightmapscale" ) ) ||
1378 strcmp( "", ValueForKey( e, "_lightmapscale" ) ) ||
1379 strcmp( "", ValueForKey( e, "_ls" ) ) ) {
1380 baseLightmapScale = FloatForKey( e, "lightmapscale" );
1381 if ( baseLightmapScale <= 0.0f ) {
1382 baseLightmapScale = FloatForKey( e, "_lightmapscale" );
1384 if ( baseLightmapScale <= 0.0f ) {
1385 baseLightmapScale = FloatForKey( e, "_ls" );
1387 if ( baseLightmapScale < 0.0f ) {
1388 baseLightmapScale = 0.0f;
1390 if ( baseLightmapScale > 0.0f ) {
1391 Sys_Printf( "World Entity has lightmap scale of %.4f\n", baseLightmapScale );
1395 /* walk the entity list */
1396 for ( num = 1; num < numEntities; num++ )
1399 e2 = &entities[ num ];
1401 /* convert misc_models into raw geometry */
1402 if ( Q_stricmp( "misc_model", ValueForKey( e2, "classname" ) ) ) {
1406 /* ydnar: added support for md3 models on non-worldspawn models */
1407 target = ValueForKey( e2, "target" );
1408 if ( strcmp( target, targetName ) ) {
1412 /* get model name */
1413 model = ValueForKey( e2, "model" );
1414 if ( model[ 0 ] == '\0' ) {
1415 Sys_FPrintf( SYS_WRN, "WARNING: misc_model at %i %i %i without a model key\n",
1416 (int) origin[ 0 ], (int) origin[ 1 ], (int) origin[ 2 ] );
1420 /* get model frame */
1422 if ( strcmp( "", ValueForKey( e2, "_frame" ) ) ) {
1423 frame = IntForKey( e2, "_frame" );
1425 else if ( strcmp( "", ValueForKey( e2, "frame" ) ) ) {
1426 frame = IntForKey( e2, "frame" );
1429 /* worldspawn (and func_groups) default to cast/recv shadows in worldspawn group */
1430 if ( e == entities ) {
1431 castShadows = WORLDSPAWN_CAST_SHADOWS;
1432 recvShadows = WORLDSPAWN_RECV_SHADOWS;
1435 /* other entities don't cast any shadows, but recv worldspawn shadows */
1438 castShadows = ENTITY_CAST_SHADOWS;
1439 recvShadows = ENTITY_RECV_SHADOWS;
1442 /* get explicit shadow flags */
1443 GetEntityShadowFlags( e2, e, &castShadows, &recvShadows );
1445 /* get spawnflags */
1446 spawnFlags = IntForKey( e2, "spawnflags" );
1449 GetVectorForKey( e2, "origin", origin );
1450 VectorSubtract( origin, e->origin, origin ); /* offset by parent */
1453 scale[ 0 ] = scale[ 1 ] = scale[ 2 ] = 1.0f;
1454 temp = FloatForKey( e2, "modelscale" );
1455 if ( temp != 0.0f ) {
1456 scale[ 0 ] = scale[ 1 ] = scale[ 2 ] = temp;
1458 value = ValueForKey( e2, "modelscale_vec" );
1459 if ( value[ 0 ] != '\0' ) {
1460 sscanf( value, "%f %f %f", &scale[ 0 ], &scale[ 1 ], &scale[ 2 ] );
1463 /* get "angle" (yaw) or "angles" (pitch yaw roll) */
1464 angles[ 0 ] = angles[ 1 ] = angles[ 2 ] = 0.0f;
1465 angles[ 2 ] = FloatForKey( e2, "angle" );
1466 value = ValueForKey( e2, "angles" );
1467 if ( value[ 0 ] != '\0' ) {
1468 sscanf( value, "%f %f %f", &angles[ 1 ], &angles[ 2 ], &angles[ 0 ] );
1471 /* set transform matrix (thanks spog) */
1472 m4x4_identity( transform );
1473 m4x4_pivoted_transform_by_vec3( transform, origin, angles, eXYZ, scale, vec3_origin );
1475 /* get shader remappings */
1477 for ( ep = e2->epairs; ep != NULL; ep = ep->next )
1479 /* look for keys prefixed with "_remap" */
1480 if ( ep->key != NULL && ep->value != NULL &&
1481 ep->key[ 0 ] != '\0' && ep->value[ 0 ] != '\0' &&
1482 !Q_strncasecmp( ep->key, "_remap", 6 ) ) {
1483 /* create new remapping */
1485 remap = safe_malloc( sizeof( *remap ) );
1486 remap->next = remap2;
1487 strcpy( remap->from, ep->value );
1489 /* split the string */
1490 split = strchr( remap->from, ';' );
1491 if ( split == NULL ) {
1492 Sys_FPrintf( SYS_WRN, "WARNING: Shader _remap key found in misc_model without a ; character\n" );
1498 /* store the split */
1500 strcpy( remap->to, ( split + 1 ) );
1503 //% Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", remap->from, remap->to );
1507 /* ydnar: cel shader support */
1508 value = ValueForKey( e2, "_celshader" );
1509 if ( value[ 0 ] == '\0' ) {
1510 value = ValueForKey( &entities[ 0 ], "_celshader" );
1512 if ( value[ 0 ] != '\0' ) {
1513 sprintf( shader, "textures/%s", value );
1514 celShader = ShaderInfoForShader( shader );
1517 celShader = *globalCelShader ? ShaderInfoForShader( globalCelShader ) : NULL;
1520 /* jal : entity based _samplesize */
1521 lightmapSampleSize = 0;
1522 if ( strcmp( "", ValueForKey( e2, "_lightmapsamplesize" ) ) ) {
1523 lightmapSampleSize = IntForKey( e2, "_lightmapsamplesize" );
1525 else if ( strcmp( "", ValueForKey( e2, "_samplesize" ) ) ) {
1526 lightmapSampleSize = IntForKey( e2, "_samplesize" );
1528 else if ( strcmp( "", ValueForKey( e2, "_ss" ) ) ) {
1529 lightmapSampleSize = IntForKey( e2, "_ss" );
1532 if ( lightmapSampleSize < 0 ) {
1533 lightmapSampleSize = 0;
1536 if ( lightmapSampleSize > 0.0f ) {
1537 Sys_Printf( "misc_model has lightmap sample size of %.d\n", lightmapSampleSize );
1540 /* get lightmap scale */
1541 /* vortex: added _ls key (short name of lightmapscale) */
1542 lightmapScale = 0.0f;
1543 if ( strcmp( "", ValueForKey( e2, "lightmapscale" ) ) ||
1544 strcmp( "", ValueForKey( e2, "_lightmapscale" ) ) ||
1545 strcmp( "", ValueForKey( e2, "_ls" ) ) ) {
1546 lightmapScale = FloatForKey( e2, "lightmapscale" );
1547 if ( lightmapScale <= 0.0f ) {
1548 lightmapScale = FloatForKey( e2, "_lightmapscale" );
1550 if ( lightmapScale <= 0.0f ) {
1551 lightmapScale = FloatForKey( e2, "_ls" );
1553 if ( lightmapScale < 0.0f ) {
1554 lightmapScale = 0.0f;
1556 if ( lightmapScale > 0.0f ) {
1557 Sys_Printf( "misc_model has lightmap scale of %.4f\n", lightmapScale );
1561 /* jal : entity based _shadeangle */
1563 if ( strcmp( "", ValueForKey( e2, "_shadeangle" ) ) ) {
1564 shadeAngle = FloatForKey( e2, "_shadeangle" );
1566 /* vortex' aliases */
1567 else if ( strcmp( "", ValueForKey( e2, "_smoothnormals" ) ) ) {
1568 shadeAngle = FloatForKey( e2, "_smoothnormals" );
1570 else if ( strcmp( "", ValueForKey( e2, "_sn" ) ) ) {
1571 shadeAngle = FloatForKey( e2, "_sn" );
1573 else if ( strcmp( "", ValueForKey( e2, "_sa" ) ) ) {
1574 shadeAngle = FloatForKey( e2, "_sa" );
1576 else if ( strcmp( "", ValueForKey( e2, "_smooth" ) ) ) {
1577 shadeAngle = FloatForKey( e2, "_smooth" );
1580 if ( shadeAngle < 0.0f ) {
1584 if ( shadeAngle > 0.0f ) {
1585 Sys_Printf( "misc_model has shading angle of %.4f\n", shadeAngle );
1589 if ( strcmp( "", ValueForKey( e2, "_skin" ) ) ) {
1590 skin = IntForKey( e2, "_skin" );
1592 else if ( strcmp( "", ValueForKey( e2, "skin" ) ) ) {
1593 skin = IntForKey( e2, "skin" );
1596 clipDepth = clipDepthGlobal;
1597 if ( strcmp( "", ValueForKey( e2, "_clipdepth" ) ) ) {
1598 clipDepth = FloatForKey( e2, "_clipdepth" );
1599 Sys_Printf( "misc_model has autoclip depth of %.3f\n", clipDepth );
1603 /* insert the model */
1604 InsertModel( model, skin, frame, transform, remap, celShader, mapEntityNum, castShadows, recvShadows, spawnFlags, lightmapScale, lightmapSampleSize, shadeAngle, clipDepth );
1606 /* free shader remappings */
1607 while ( remap != NULL )
1609 remap2 = remap->next;