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_Printf( "WARNING: %s\n", str );
65 Sys_Printf( "ERROR: %s\n", str );
69 Error( "ERROR: %s\n", str );
78 callback for picomodel.lib
81 void PicoLoadFileFunc( char *name, byte **buffer, int *bufSize )
83 *bufSize = vfsLoadFile( (const char*) name, (void**) buffer, 0 );
90 finds an existing picoModel and returns a pointer to the picoModel_t struct or NULL if not found
93 picoModel_t *FindModel( char *name, int frame )
99 if( numPicoModels <= 0 )
100 memset( picoModels, 0, sizeof( picoModels ) );
103 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 ];
115 /* no matching picoModel found */
123 loads a picoModel and returns a pointer to the picoModel_t struct or NULL if not found
126 picoModel_t *LoadModel( char *name, int frame )
129 picoModel_t *model, **pm;
133 if( numPicoModels <= 0 )
134 memset( picoModels, 0, sizeof( picoModels ) );
137 if( name == NULL || name[ 0 ] == '\0' )
140 /* try to find existing picoModel */
141 model = FindModel( name, frame );
145 /* none found, so find first non-null picoModel */
147 for( i = 0; i < MAX_MODELS; i++ )
149 if( picoModels[ i ] == NULL )
151 pm = &picoModels[ i ];
156 /* too many picoModels? */
158 Error( "MAX_MODELS (%d) exceeded, there are too many model files referenced by the map.", MAX_MODELS );
160 /* attempt to parse model */
161 *pm = PicoLoadModel( (char*) name, frame );
163 /* if loading failed, make a bogus model to silence the rest of the warnings */
166 /* allocate a new model */
167 *pm = PicoNewModel();
172 PicoSetModelName( *pm, name );
173 PicoSetModelFrameNum( *pm, frame );
179 int numSurfaces, numVertexes;
183 Sys_Printf( "Model %s\n", name );
184 numSurfaces = PicoGetModelNumSurfaces( *pm );
185 for( i = 0; i < numSurfaces; i++ )
187 ps = PicoGetModelSurface( *pm, i );
188 numVertexes = PicoGetSurfaceNumVertexes( ps );
189 Sys_Printf( "Surface %d has %d vertexes\n", i, numVertexes );
198 /* return the picoModel */
205 InsertModel() - ydnar
206 adds a picomodel into the bsp
209 void InsertModel( char *name, int frame, m4x4_t transform, remap_t *remap, shaderInfo_t *celShader, int eNum, int castShadows, int recvShadows, int spawnFlags, float lightmapScale, float shadeAngle )
211 int i, j, k, s, numSurfaces;
212 m4x4_t identity, nTransform;
214 picoShader_t *shader;
215 picoSurface_t *surface;
217 mapDrawSurface_t *ds;
219 char *picoShaderName;
220 char shaderName[ MAX_QPATH ];
221 picoVec_t *xyz, *normal, *st;
223 picoIndex_t *indexes;
225 double normalEpsilon_save;
226 double distanceEpsilon_save;
230 model = LoadModel( name, frame );
234 /* handle null matrix */
235 if( transform == NULL )
237 m4x4_identity( identity );
238 transform = identity;
241 /* hack: Stable-1_2 and trunk have differing row/column major matrix order
242 this transpose is necessary with Stable-1_2
243 uncomment the following line with old m4x4_t (non 1.3/spog_branch) code */
244 //% m4x4_transpose( transform );
246 /* create transform matrix for normals */
247 memcpy( nTransform, transform, sizeof( m4x4_t ) );
248 if( m4x4_invert( nTransform ) )
249 Sys_FPrintf( SYS_VRB, "WARNING: Can't invert model transform matrix, using transpose instead\n" );
250 m4x4_transpose( nTransform );
252 /* fix bogus lightmap scale */
253 if( lightmapScale <= 0.0f )
254 lightmapScale = 1.0f;
256 /* each surface on the model will become a new map drawsurface */
257 numSurfaces = PicoGetModelNumSurfaces( model );
258 //% Sys_FPrintf( SYS_VRB, "Model %s has %d surfaces\n", name, numSurfaces );
259 for( s = 0; s < numSurfaces; s++ )
262 surface = PicoGetModelSurface( model, s );
263 if( surface == NULL )
266 /* only handle triangle surfaces initially (fixme: support patches) */
267 if( PicoGetSurfaceType( surface ) != PICO_TRIANGLES )
270 /* allocate a surface (ydnar: gs mods) */
271 ds = AllocDrawSurface( SURFACE_TRIANGLES );
272 ds->entityNum = eNum;
273 ds->castShadows = castShadows;
274 ds->recvShadows = recvShadows;
276 /* get shader name */
277 shader = PicoGetSurfaceShader( surface );
281 picoShaderName = PicoGetShaderName( shader );
283 /* handle shader remapping */
285 for( rm = remap; rm != NULL; rm = rm->next )
287 if( rm->from[ 0 ] == '*' && rm->from[ 1 ] == '\0' )
289 else if( !Q_stricmp( picoShaderName, rm->from ) )
291 Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", picoShaderName, rm->to );
292 picoShaderName = rm->to;
300 Sys_FPrintf( SYS_VRB, "Globbing %s to %s\n", picoShaderName, glob->to );
301 picoShaderName = glob->to;
304 /* shader renaming for sof2 */
305 if( renameModelShaders )
307 strcpy( shaderName, picoShaderName );
308 StripExtension( shaderName );
310 strcat( shaderName, "_RMG_BSP" );
312 strcat( shaderName, "_BSP" );
313 si = ShaderInfoForShader( shaderName );
316 si = ShaderInfoForShader( picoShaderName );
322 if( (si != NULL && si->forceMeta) || (spawnFlags & 4) ) /* 3rd bit */
323 ds->type = SURFACE_FORCED_META;
325 /* get shading angle from shader or entity */
326 if( si->shadeAngleDegrees )
327 ds->shadeAngleDegrees = si->shadeAngleDegrees;
328 else if( shadeAngle )
329 ds->shadeAngleDegrees = shadeAngle; /* otherwise it's 0 */
331 /* fix the surface's normals (jal: conditioned by shader info) */
332 if( !(spawnFlags & 64) && ( shadeAngle == 0.0f || ds->type != SURFACE_FORCED_META ) )
333 PicoFixSurfaceNormals( surface );
335 /* set lightmap scale */
336 ds->lightmapScale = lightmapScale;
338 /* set particulars */
339 ds->numVerts = PicoGetSurfaceNumVertexes( surface );
340 ds->verts = safe_malloc( ds->numVerts * sizeof( ds->verts[ 0 ] ) );
341 memset( ds->verts, 0, ds->numVerts * sizeof( ds->verts[ 0 ] ) );
343 ds->numIndexes = PicoGetSurfaceNumIndexes( surface );
344 ds->indexes = safe_malloc( ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
345 memset( ds->indexes, 0, ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
348 for( i = 0; i < ds->numVerts; i++ )
351 dv = &ds->verts[ i ];
354 xyz = PicoGetSurfaceXYZ( surface, i );
355 VectorCopy( xyz, dv->xyz );
356 m4x4_transform_point( transform, dv->xyz );
358 normal = PicoGetSurfaceNormal( surface, i );
359 VectorCopy( normal, dv->normal );
360 m4x4_transform_normal( nTransform, dv->normal );
361 VectorNormalize( dv->normal, dv->normal );
363 /* ydnar: tek-fu celshading support for flat shaded shit */
366 dv->st[ 0 ] = si->stFlat[ 0 ];
367 dv->st[ 1 ] = si->stFlat[ 1 ];
370 /* ydnar: gs mods: added support for explicit shader texcoord generation */
373 /* project the texture */
374 dv->st[ 0 ] = DotProduct( si->vecs[ 0 ], dv->xyz );
375 dv->st[ 1 ] = DotProduct( si->vecs[ 1 ], dv->xyz );
378 /* normal texture coordinates */
381 st = PicoGetSurfaceST( surface, 0, i );
382 dv->st[ 0 ] = st[ 0 ];
383 dv->st[ 1 ] = st[ 1 ];
386 /* set lightmap/color bits */
387 color = PicoGetSurfaceColor( surface, 0, i );
388 for( j = 0; j < MAX_LIGHTMAPS; j++ )
390 dv->lightmap[ j ][ 0 ] = 0.0f;
391 dv->lightmap[ j ][ 1 ] = 0.0f;
392 if(spawnFlags & 32) // spawnflag 32: model color -> alpha hack
394 dv->color[ j ][ 0 ] = 255.0f;
395 dv->color[ j ][ 1 ] = 255.0f;
396 dv->color[ j ][ 2 ] = 255.0f;
397 dv->color[ j ][ 3 ] = color[ 0 ] * 0.3f + color[ 1 ] * 0.59f + color[ 2 ] * 0.11f;
401 dv->color[ j ][ 0 ] = color[ 0 ];
402 dv->color[ j ][ 1 ] = color[ 1 ];
403 dv->color[ j ][ 2 ] = color[ 2 ];
404 dv->color[ j ][ 3 ] = color[ 3 ];
410 indexes = PicoGetSurfaceIndexes( surface, 0 );
411 for( i = 0; i < ds->numIndexes; i++ )
412 ds->indexes[ i ] = indexes[ i ];
415 ds->celShader = celShader;
417 /* ydnar: giant hack land: generate clipping brushes for model triangles */
418 if( si->clipModel || (spawnFlags & 2) ) /* 2nd bit */
420 vec3_t points[ 4 ], backs[ 3 ];
421 vec4_t plane, reverse, pa, pb, pc;
425 if( !si->clipModel &&
426 ((si->compileFlags & C_TRANSLUCENT) || !(si->compileFlags & C_SOLID)) )
429 /* walk triangle list */
430 for( i = 0; i < ds->numIndexes; i += 3 )
433 AUTOEXPAND_BY_REALLOC(mapplanes, (nummapplanes+64) << 1, allocatedmapplanes, 1024);
435 /* make points and back points */
436 for( j = 0; j < 3; j++ )
439 dv = &ds->verts[ ds->indexes[ i + j ] ];
442 VectorCopy( dv->xyz, points[ j ] );
443 VectorCopy( dv->xyz, backs[ j ] );
445 /* find nearest axial to normal and push back points opposite */
446 /* note: this doesn't work as well as simply using the plane of the triangle, below */
447 for( k = 0; k < 3; k++ )
449 if( fabs( dv->normal[ k ] ) >= fabs( dv->normal[ (k + 1) % 3 ] ) &&
450 fabs( dv->normal[ k ] ) >= fabs( dv->normal[ (k + 2) % 3 ] ) )
452 backs[ j ][ k ] += dv->normal[ k ] < 0.0f ? 64.0f : -64.0f;
458 VectorCopy( points[0], points[3] ); // for cyclic usage
460 /* make plane for triangle */
461 // div0: add some extra spawnflags:
462 // 0: snap normals to axial planes for extrusion
463 // 8: extrude with the original normals
464 // 16: extrude only with up/down normals (ideal for terrain)
465 // 24: extrude by distance zero (may need engine changes)
466 if( PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] ) )
469 float backPlaneDistance = 2;
471 if(spawnFlags & 8) // use a DOWN normal
475 // 24: normal as is, and zero width (broken)
476 VectorCopy(plane, bestNormal);
481 VectorCopy(plane, bestNormal);
488 // 16: UP/DOWN normal
489 VectorSet(bestNormal, 0, 0, (plane[2] >= 0 ? 1 : -1));
494 if(fabs(plane[0]) > fabs(plane[1])) // x>y
495 if(fabs(plane[1]) > fabs(plane[2])) // x>y, y>z
496 VectorSet(bestNormal, (plane[0] >= 0 ? 1 : -1), 0, 0);
498 if(fabs(plane[0]) > fabs(plane[2])) // x>z, z>=y
499 VectorSet(bestNormal, (plane[0] >= 0 ? 1 : -1), 0, 0);
501 VectorSet(bestNormal, 0, 0, (plane[2] >= 0 ? 1 : -1));
503 if(fabs(plane[1]) > fabs(plane[2])) // y>z, y>=x
504 VectorSet(bestNormal, 0, (plane[1] >= 0 ? 1 : -1), 0);
506 VectorSet(bestNormal, 0, 0, (plane[2] >= 0 ? 1 : -1));
511 buildBrush = AllocBrush( 48 );
512 buildBrush->entityNum = mapEntityNum;
513 buildBrush->original = buildBrush;
514 buildBrush->contentShader = si;
515 buildBrush->compileFlags = si->compileFlags;
516 buildBrush->contentFlags = si->contentFlags;
517 normalEpsilon_save = normalEpsilon;
518 distanceEpsilon_save = distanceEpsilon;
519 if(si->compileFlags & C_STRUCTURAL) // allow forced structural brushes here
521 buildBrush->detail = qfalse;
523 // only allow EXACT matches when snapping for these (this is mostly for caulk brushes inside a model)
524 if(normalEpsilon > 0)
526 if(distanceEpsilon > 0)
530 buildBrush->detail = qtrue;
532 /* regenerate back points */
533 for( j = 0; j < 3; j++ )
536 dv = &ds->verts[ ds->indexes[ i + j ] ];
538 // shift by some units
539 VectorMA(dv->xyz, -64.0f, bestNormal, backs[j]); // 64 prevents roundoff errors a bit
542 /* make back plane */
543 VectorScale( plane, -1.0f, reverse );
544 reverse[ 3 ] = -plane[ 3 ];
545 if((spawnFlags & 24) != 24)
546 reverse[3] += DotProduct(bestNormal, plane) * backPlaneDistance;
547 // that's at least sqrt(1/3) backPlaneDistance, unless in DOWN mode; in DOWN mode, we are screwed anyway if we encounter a plane that's perpendicular to the xy plane)
549 if( PlaneFromPoints( pa, points[ 2 ], points[ 1 ], backs[ 1 ] ) &&
550 PlaneFromPoints( pb, points[ 1 ], points[ 0 ], backs[ 0 ] ) &&
551 PlaneFromPoints( pc, points[ 0 ], points[ 2 ], backs[ 2 ] ) )
553 /* set up brush sides */
554 buildBrush->numsides = 5;
555 buildBrush->sides[ 0 ].shaderInfo = si;
556 for( j = 1; j < buildBrush->numsides; j++ )
557 buildBrush->sides[ j ].shaderInfo = NULL; // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
559 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
560 buildBrush->sides[ 1 ].planenum = FindFloatPlane( pa, pa[ 3 ], 2, &points[ 1 ] ); // pa contains points[1] and points[2]
561 buildBrush->sides[ 2 ].planenum = FindFloatPlane( pb, pb[ 3 ], 2, &points[ 0 ] ); // pb contains points[0] and points[1]
562 buildBrush->sides[ 3 ].planenum = FindFloatPlane( pc, pc[ 3 ], 2, &points[ 2 ] ); // pc contains points[2] and points[0] (copied to points[3]
563 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 3, backs );
571 normalEpsilon = normalEpsilon_save;
572 distanceEpsilon = distanceEpsilon_save;
575 if( CreateBrushWindings( buildBrush ) )
578 //% EmitBrushes( buildBrush, NULL, NULL );
579 buildBrush->next = entities[ mapEntityNum ].brushes;
580 entities[ mapEntityNum ].brushes = buildBrush;
581 entities[ mapEntityNum ].numBrushes++;
595 adds misc_model surfaces to the bsp
598 void AddTriangleModels( entity_t *e )
600 int num, frame, castShadows, recvShadows, spawnFlags;
602 const char *targetName;
603 const char *target, *model, *value;
604 char shader[ MAX_QPATH ];
605 shaderInfo_t *celShader;
606 float temp, baseLightmapScale, lightmapScale;
608 vec3_t origin, scale, angles;
611 remap_t *remap, *remap2;
616 Sys_FPrintf( SYS_VRB, "--- AddTriangleModels ---\n" );
618 /* get current brush entity targetname */
623 targetName = ValueForKey( e, "targetname" );
625 /* misc_model entities target non-worldspawn brush model entities */
626 if( targetName[ 0 ] == '\0' )
630 /* get lightmap scale */
631 /* vortex: added _ls key (short name of lightmapscale) */
632 baseLightmapScale = FloatForKey( e, "_lightmapscale" );
633 if( baseLightmapScale <= 0.0f )
634 baseLightmapScale = FloatForKey( e, "_ls" );
635 if( baseLightmapScale <= 0.0f )
636 baseLightmapScale = 0.0f;
638 /* walk the entity list */
639 for( num = 1; num < numEntities; num++ )
642 e2 = &entities[ num ];
644 /* convert misc_models into raw geometry */
645 if( Q_stricmp( "misc_model", ValueForKey( e2, "classname" ) ) )
648 /* ydnar: added support for md3 models on non-worldspawn models */
649 target = ValueForKey( e2, "target" );
650 if( strcmp( target, targetName ) )
654 model = ValueForKey( e2, "model" );
655 if( model[ 0 ] == '\0' )
657 Sys_Printf( "WARNING: misc_model at %i %i %i without a model key\n",
658 (int) origin[ 0 ], (int) origin[ 1 ], (int) origin[ 2 ] );
662 /* get model frame */
663 frame = IntForKey( e2, "_frame" );
665 /* worldspawn (and func_groups) default to cast/recv shadows in worldspawn group */
668 castShadows = WORLDSPAWN_CAST_SHADOWS;
669 recvShadows = WORLDSPAWN_RECV_SHADOWS;
672 /* other entities don't cast any shadows, but recv worldspawn shadows */
675 castShadows = ENTITY_CAST_SHADOWS;
676 recvShadows = ENTITY_RECV_SHADOWS;
679 /* get explicit shadow flags */
680 GetEntityShadowFlags( e2, e, &castShadows, &recvShadows );
683 spawnFlags = IntForKey( e2, "spawnflags" );
686 GetVectorForKey( e2, "origin", origin );
687 VectorSubtract( origin, e->origin, origin ); /* offset by parent */
690 scale[ 0 ] = scale[ 1 ] = scale[ 2 ] = 1.0f;
691 temp = FloatForKey( e2, "modelscale" );
693 scale[ 0 ] = scale[ 1 ] = scale[ 2 ] = temp;
694 value = ValueForKey( e2, "modelscale_vec" );
695 if( value[ 0 ] != '\0' )
696 sscanf( value, "%f %f %f", &scale[ 0 ], &scale[ 1 ], &scale[ 2 ] );
698 /* get "angle" (yaw) or "angles" (pitch yaw roll) */
699 angles[ 0 ] = angles[ 1 ] = angles[ 2 ] = 0.0f;
700 angles[ 2 ] = FloatForKey( e2, "angle" );
701 value = ValueForKey( e2, "angles" );
702 if( value[ 0 ] != '\0' )
703 sscanf( value, "%f %f %f", &angles[ 1 ], &angles[ 2 ], &angles[ 0 ] );
705 /* set transform matrix (thanks spog) */
706 m4x4_identity( transform );
707 m4x4_pivoted_transform_by_vec3( transform, origin, angles, eXYZ, scale, vec3_origin );
709 /* get shader remappings */
711 for( ep = e2->epairs; ep != NULL; ep = ep->next )
713 /* look for keys prefixed with "_remap" */
714 if( ep->key != NULL && ep->value != NULL &&
715 ep->key[ 0 ] != '\0' && ep->value[ 0 ] != '\0' &&
716 !Q_strncasecmp( ep->key, "_remap", 6 ) )
718 /* create new remapping */
720 remap = safe_malloc( sizeof( *remap ) );
721 remap->next = remap2;
722 strcpy( remap->from, ep->value );
724 /* split the string */
725 split = strchr( remap->from, ';' );
728 Sys_Printf( "WARNING: Shader _remap key found in misc_model without a ; character\n" );
734 /* store the split */
736 strcpy( remap->to, (split + 1) );
739 //% Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", remap->from, remap->to );
743 /* ydnar: cel shader support */
744 value = ValueForKey( e2, "_celshader" );
745 if( value[ 0 ] == '\0' )
746 value = ValueForKey( &entities[ 0 ], "_celshader" );
747 if( value[ 0 ] != '\0' )
749 sprintf( shader, "textures/%s", value );
750 celShader = ShaderInfoForShader( shader );
753 celShader = *globalCelShader ? ShaderInfoForShader(globalCelShader) : NULL;
755 /* get lightmap scale */
756 /* vortex: added _ls key (short name of lightmapscale) */
757 lightmapScale = FloatForKey( e2, "_lightmapscale" );
758 if( lightmapScale <= 0.0f )
759 lightmapScale = FloatForKey( e2, "_ls" );
760 if( lightmapScale <= 0.0f )
761 lightmapScale = baseLightmapScale;
763 /* jal : entity based _shadeangle */
765 if ( strcmp( "", ValueForKey( e2, "_shadeangle" ) ) )
766 shadeAngle = FloatForKey( e2, "_shadeangle" );
767 /* vortex' aliases */
768 else if ( strcmp( "", ValueForKey( mapEnt, "_smoothnormals" ) ) )
769 shadeAngle = FloatForKey( mapEnt, "_smoothnormals" );
770 else if ( strcmp( "", ValueForKey( mapEnt, "_sn" ) ) )
771 shadeAngle = FloatForKey( mapEnt, "_sn" );
772 else if ( strcmp( "", ValueForKey( mapEnt, "_smooth" ) ) )
773 shadeAngle = FloatForKey( mapEnt, "_smooth" );
775 if( shadeAngle < 0.0f )
778 if( shadeAngle > 0.0f )
779 Sys_Printf( "misc_model has shading angle of %.4f\n", shadeAngle );
781 /* insert the model */
782 InsertModel( (char*) model, frame, transform, remap, celShader, mapEntityNum, castShadows, recvShadows, spawnFlags, lightmapScale, shadeAngle );
784 /* free shader remappings */
785 while( remap != NULL )
787 remap2 = remap->next;