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 ){
82 *bufSize = vfsLoadFile( (const char*) 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( 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( 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( (char*) 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( char *name, int frame, m4x4_t transform, remap_t *remap, shaderInfo_t *celShader, int eNum, int castShadows, int recvShadows, int spawnFlags, float lightmapScale ){
214 int i, j, k, s, 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;
231 model = LoadModel( name, frame );
232 if ( model == NULL ) {
236 /* handle null matrix */
237 if ( transform == NULL ) {
238 m4x4_identity( identity );
239 transform = identity;
242 /* hack: Stable-1_2 and trunk have differing row/column major matrix order
243 this transpose is necessary with Stable-1_2
244 uncomment the following line with old m4x4_t (non 1.3/spog_branch) code */
245 //% m4x4_transpose( transform );
247 /* create transform matrix for normals */
248 memcpy( nTransform, transform, sizeof( m4x4_t ) );
249 if ( m4x4_invert( nTransform ) ) {
250 Sys_FPrintf( SYS_VRB, "WARNING: Can't invert model transform matrix, using transpose instead\n" );
252 m4x4_transpose( nTransform );
254 /* fix bogus lightmap scale */
255 if ( lightmapScale <= 0.0f ) {
256 lightmapScale = 1.0f;
259 /* each surface on the model will become a new map drawsurface */
260 numSurfaces = PicoGetModelNumSurfaces( model );
261 //% Sys_FPrintf( SYS_VRB, "Model %s has %d surfaces\n", name, numSurfaces );
262 for ( s = 0; s < numSurfaces; s++ )
265 surface = PicoGetModelSurface( model, s );
266 if ( surface == NULL ) {
270 /* only handle triangle surfaces initially (fixme: support patches) */
271 if ( PicoGetSurfaceType( surface ) != PICO_TRIANGLES ) {
275 /* fix the surface's normals */
276 PicoFixSurfaceNormals( surface );
278 /* allocate a surface (ydnar: gs mods) */
279 ds = AllocDrawSurface( SURFACE_TRIANGLES );
280 ds->entityNum = eNum;
281 ds->castShadows = castShadows;
282 ds->recvShadows = recvShadows;
284 /* get shader name */
285 shader = PicoGetSurfaceShader( surface );
286 if ( shader == NULL ) {
290 picoShaderName = PicoGetShaderName( shader );
293 /* handle shader remapping */
295 for ( rm = remap; rm != NULL; rm = rm->next )
297 if ( rm->from[ 0 ] == '*' && rm->from[ 1 ] == '\0' ) {
300 else if ( !Q_stricmp( picoShaderName, rm->from ) ) {
301 Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", picoShaderName, rm->to );
302 picoShaderName = rm->to;
308 if ( glob != NULL ) {
309 Sys_FPrintf( SYS_VRB, "Globbing %s to %s\n", picoShaderName, glob->to );
310 picoShaderName = glob->to;
313 /* shader renaming for sof2 */
314 if ( renameModelShaders ) {
315 strcpy( shaderName, picoShaderName );
316 StripExtension( shaderName );
317 if ( spawnFlags & 1 ) {
318 strcat( shaderName, "_RMG_BSP" );
321 strcat( shaderName, "_BSP" );
323 si = ShaderInfoForShader( shaderName );
326 si = ShaderInfoForShader( picoShaderName );
332 /* set lightmap scale */
333 ds->lightmapScale = lightmapScale;
336 if ( ( si != NULL && si->forceMeta ) || ( spawnFlags & 4 ) ) { /* 3rd bit */
337 ds->type = SURFACE_FORCED_META;
340 /* set particulars */
341 ds->numVerts = PicoGetSurfaceNumVertexes( surface );
342 ds->verts = safe_malloc( ds->numVerts * sizeof( ds->verts[ 0 ] ) );
343 memset( ds->verts, 0, ds->numVerts * sizeof( ds->verts[ 0 ] ) );
345 ds->numIndexes = PicoGetSurfaceNumIndexes( surface );
346 ds->indexes = safe_malloc( ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
347 memset( ds->indexes, 0, ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
350 for ( i = 0; i < ds->numVerts; i++ )
353 dv = &ds->verts[ i ];
356 xyz = PicoGetSurfaceXYZ( surface, i );
357 VectorCopy( xyz, dv->xyz );
358 m4x4_transform_point( transform, dv->xyz );
360 normal = PicoGetSurfaceNormal( surface, i );
361 VectorCopy( normal, dv->normal );
362 m4x4_transform_normal( nTransform, dv->normal );
363 VectorNormalize( dv->normal, dv->normal );
365 /* ydnar: tek-fu celshading support for flat shaded shit */
367 dv->st[ 0 ] = si->stFlat[ 0 ];
368 dv->st[ 1 ] = si->stFlat[ 1 ];
371 /* ydnar: gs mods: added support for explicit shader texcoord generation */
372 else if ( si->tcGen ) {
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 dv->color[ j ][ 0 ] = color[ 0 ];
393 dv->color[ j ][ 1 ] = color[ 1 ];
394 dv->color[ j ][ 2 ] = color[ 2 ];
395 dv->color[ j ][ 3 ] = color[ 3 ];
400 indexes = PicoGetSurfaceIndexes( surface, 0 );
401 for ( i = 0; i < ds->numIndexes; i++ )
402 ds->indexes[ i ] = indexes[ i ];
405 ds->celShader = celShader;
407 /* ydnar: giant hack land: generate clipping brushes for model triangles */
408 if ( si->clipModel || ( spawnFlags & 2 ) ) { /* 2nd bit */
409 vec3_t points[ 3 ], backs[ 3 ];
410 vec4_t plane, reverse, pa, pb, pc;
415 if ( !si->clipModel &&
416 ( ( si->compileFlags & C_TRANSLUCENT ) || !( si->compileFlags & C_SOLID ) ) ) {
421 if ( ( nummapplanes + 64 ) >= ( MAX_MAP_PLANES >> 1 ) ) {
425 /* walk triangle list */
426 for ( i = 0; i < ds->numIndexes; i += 3 )
429 if ( ( nummapplanes + 64 ) >= ( MAX_MAP_PLANES >> 1 ) ) {
430 Sys_Printf( "WARNING: MAX_MAP_PLANES (%d) hit generating clip brushes for model %s.\n",
431 MAX_MAP_PLANES, name );
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 #if !Q3MAP2_EXPERIMENTAL_MODEL_CLIPPING_FIX
444 // The code below is totally unneeded regardless of Q3MAP2_EXPERIMENTAL_MODEL_CLIPPING_FIX.
445 // backs is reinitialized further below. However, the extra code does not hurt anything.
446 VectorCopy( dv->xyz, backs[ j ] );
448 /* find nearest axial to normal and push back points opposite */
449 /* note: this doesn't work as well as simply using the plane of the triangle, below */
450 for ( k = 0; k < 3; k++ )
452 if ( fabs( dv->normal[ k ] ) > fabs( dv->normal[ ( k + 1 ) % 3 ] ) &&
453 fabs( dv->normal[ k ] ) > fabs( dv->normal[ ( k + 2 ) % 3 ] ) ) {
454 backs[ j ][ k ] += dv->normal[ k ] < 0.0f ? 64.0f : -64.0f;
461 /* make plane for triangle */
462 if ( PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] ) ) {
463 /* regenerate back points */
464 for ( j = 0; j < 3; j++ )
467 dv = &ds->verts[ ds->indexes[ i + j ] ];
470 VectorCopy( dv->xyz, backs[ j ] );
472 /* find nearest axial to plane normal and push back points opposite */
473 for ( k = 0; k < 3; k++ )
475 #if Q3MAP2_EXPERIMENTAL_MODEL_CLIPPING_FIX
476 if ( fabs( plane[ k ] ) >= fabs( plane[ ( k + 1 ) % 3 ] ) &&
477 fabs( plane[ k ] ) >= fabs( plane[ ( k + 2 ) % 3 ] ) )
479 // This code is broken for 45 degree angles where there
480 // is no clear winner.
481 if ( fabs( plane[ k ] ) > fabs( plane[ ( k + 1 ) % 3 ] ) &&
482 fabs( plane[ k ] ) > fabs( plane[ ( k + 2 ) % 3 ] ) )
485 backs[ j ][ k ] += plane[ k ] < 0.0f ? 64.0f : -64.0f;
491 /* make back plane */
492 VectorScale( plane, -1.0f, reverse );
493 reverse[ 3 ] = -( plane[ 3 ] - 1 );
495 /* make back pyramid point */
496 VectorCopy( points[ 0 ], nadir );
497 VectorAdd( nadir, points[ 1 ], nadir );
498 VectorAdd( nadir, points[ 2 ], nadir );
499 VectorScale( nadir, 0.3333333333333f, nadir );
500 VectorMA( nadir, -2.0f, plane, nadir );
502 /* make 3 more planes */
503 //% if( PlaneFromPoints( pa, points[ 2 ], points[ 1 ], nadir ) &&
504 //% PlaneFromPoints( pb, points[ 1 ], points[ 0 ], nadir ) &&
505 //% PlaneFromPoints( pc, points[ 0 ], points[ 2 ], nadir ) )
506 if ( PlaneFromPoints( pa, points[ 2 ], points[ 1 ], backs[ 1 ] ) &&
507 PlaneFromPoints( pb, points[ 1 ], points[ 0 ], backs[ 0 ] ) &&
508 PlaneFromPoints( pc, points[ 0 ], points[ 2 ], backs[ 2 ] ) ) {
510 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 buildBrush->detail = qtrue;
519 /* set up brush sides */
520 buildBrush->numsides = 5;
521 for ( j = 0; j < buildBrush->numsides; j++ )
522 buildBrush->sides[ j ].shaderInfo = si;
523 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
524 buildBrush->sides[ 1 ].planenum = FindFloatPlane( pa, pa[ 3 ], 1, &points[ 2 ] );
525 buildBrush->sides[ 2 ].planenum = FindFloatPlane( pb, pb[ 3 ], 1, &points[ 1 ] );
526 buildBrush->sides[ 3 ].planenum = FindFloatPlane( pc, pc[ 3 ], 1, &points[ 0 ] );
527 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 3, points );
530 if ( CreateBrushWindings( buildBrush ) ) {
532 //% EmitBrushes( buildBrush, NULL, NULL );
533 buildBrush->next = entities[ mapEntityNum ].brushes;
534 entities[ mapEntityNum ].brushes = buildBrush;
535 entities[ mapEntityNum ].numBrushes++;
551 adds misc_model surfaces to the bsp
554 void AddTriangleModels( entity_t *e ){
555 int num, frame, castShadows, recvShadows, spawnFlags;
557 const char *targetName;
558 const char *target, *model, *value;
559 char shader[ MAX_QPATH ];
560 shaderInfo_t *celShader;
561 float temp, baseLightmapScale, lightmapScale;
562 vec3_t origin, scale, angles;
565 remap_t *remap, *remap2;
570 Sys_FPrintf( SYS_VRB, "--- AddTriangleModels ---\n" );
572 /* get current brush entity targetname */
573 if ( e == entities ) {
578 targetName = ValueForKey( e, "targetname" );
580 /* misc_model entities target non-worldspawn brush model entities */
581 if ( targetName[ 0 ] == '\0' ) {
586 /* get lightmap scale */
587 baseLightmapScale = FloatForKey( e, "_lightmapscale" );
588 if ( baseLightmapScale <= 0.0f ) {
589 baseLightmapScale = 0.0f;
592 /* walk the entity list */
593 for ( num = 1; num < numEntities; num++ )
596 e2 = &entities[ num ];
598 /* convert misc_models into raw geometry */
599 if ( Q_stricmp( "misc_model", ValueForKey( e2, "classname" ) ) ) {
603 /* ydnar: added support for md3 models on non-worldspawn models */
604 target = ValueForKey( e2, "target" );
605 if ( strcmp( target, targetName ) ) {
610 model = ValueForKey( e2, "model" );
611 if ( model[ 0 ] == '\0' ) {
612 Sys_Printf( "WARNING: misc_model at %i %i %i without a model key\n",
613 (int) origin[ 0 ], (int) origin[ 1 ], (int) origin[ 2 ] );
617 /* get model frame */
618 frame = IntForKey( e2, "_frame" );
620 /* worldspawn (and func_groups) default to cast/recv shadows in worldspawn group */
621 if ( e == entities ) {
622 castShadows = WORLDSPAWN_CAST_SHADOWS;
623 recvShadows = WORLDSPAWN_RECV_SHADOWS;
626 /* other entities don't cast any shadows, but recv worldspawn shadows */
629 castShadows = ENTITY_CAST_SHADOWS;
630 recvShadows = ENTITY_RECV_SHADOWS;
633 /* get explicit shadow flags */
634 GetEntityShadowFlags( e2, e, &castShadows, &recvShadows );
637 spawnFlags = IntForKey( e2, "spawnflags" );
640 GetVectorForKey( e2, "origin", origin );
641 VectorSubtract( origin, e->origin, origin ); /* offset by parent */
644 scale[ 0 ] = scale[ 1 ] = scale[ 2 ] = 1.0f;
645 temp = FloatForKey( e2, "modelscale" );
646 if ( temp != 0.0f ) {
647 scale[ 0 ] = scale[ 1 ] = scale[ 2 ] = temp;
649 value = ValueForKey( e2, "modelscale_vec" );
650 if ( value[ 0 ] != '\0' ) {
651 sscanf( value, "%f %f %f", &scale[ 0 ], &scale[ 1 ], &scale[ 2 ] );
654 /* get "angle" (yaw) or "angles" (pitch yaw roll) */
655 angles[ 0 ] = angles[ 1 ] = angles[ 2 ] = 0.0f;
656 angles[ 2 ] = FloatForKey( e2, "angle" );
657 value = ValueForKey( e2, "angles" );
658 if ( value[ 0 ] != '\0' ) {
659 sscanf( value, "%f %f %f", &angles[ 1 ], &angles[ 2 ], &angles[ 0 ] );
662 /* set transform matrix (thanks spog) */
663 m4x4_identity( transform );
664 m4x4_pivoted_transform_by_vec3( transform, origin, angles, eXYZ, scale, vec3_origin );
666 /* get shader remappings */
668 for ( ep = e2->epairs; ep != NULL; ep = ep->next )
670 /* look for keys prefixed with "_remap" */
671 if ( ep->key != NULL && ep->value != NULL &&
672 ep->key[ 0 ] != '\0' && ep->value[ 0 ] != '\0' &&
673 !Q_strncasecmp( ep->key, "_remap", 6 ) ) {
674 /* create new remapping */
676 remap = safe_malloc( sizeof( *remap ) );
677 remap->next = remap2;
678 strcpy( remap->from, ep->value );
680 /* split the string */
681 split = strchr( remap->from, ';' );
682 if ( split == NULL ) {
683 Sys_Printf( "WARNING: Shader _remap key found in misc_model without a ; character\n" );
689 /* store the split */
691 strcpy( remap->to, ( split + 1 ) );
694 //% Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", remap->from, remap->to );
698 /* ydnar: cel shader support */
699 value = ValueForKey( e2, "_celshader" );
700 if ( value[ 0 ] == '\0' ) {
701 value = ValueForKey( &entities[ 0 ], "_celshader" );
703 if ( value[ 0 ] != '\0' ) {
704 sprintf( shader, "textures/%s", value );
705 celShader = ShaderInfoForShader( shader );
711 /* get lightmap scale */
712 lightmapScale = FloatForKey( e2, "_lightmapscale" );
713 if ( lightmapScale <= 0.0f ) {
714 lightmapScale = baseLightmapScale;
717 /* insert the model */
718 InsertModel( (char*) model, frame, transform, remap, celShader, mapEntityNum, castShadows, recvShadows, spawnFlags, lightmapScale );
720 /* free shader remappings */
721 while ( remap != NULL )
723 remap2 = remap->next;