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 )
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;
228 model = LoadModel( name, frame );
232 /* handle null matrix */
233 if( transform == NULL )
235 m4x4_identity( identity );
236 transform = identity;
239 /* hack: Stable-1_2 and trunk have differing row/column major matrix order
240 this transpose is necessary with Stable-1_2
241 uncomment the following line with old m4x4_t (non 1.3/spog_branch) code */
242 //% m4x4_transpose( transform );
244 /* create transform matrix for normals */
245 memcpy( nTransform, transform, sizeof( m4x4_t ) );
246 if( m4x4_invert( nTransform ) )
247 Sys_FPrintf( SYS_VRB, "WARNING: Can't invert model transform matrix, using transpose instead\n" );
248 m4x4_transpose( nTransform );
250 /* fix bogus lightmap scale */
251 if( lightmapScale <= 0.0f )
252 lightmapScale = 1.0f;
254 /* each surface on the model will become a new map drawsurface */
255 numSurfaces = PicoGetModelNumSurfaces( model );
256 //% Sys_FPrintf( SYS_VRB, "Model %s has %d surfaces\n", name, numSurfaces );
257 for( s = 0; s < numSurfaces; s++ )
260 surface = PicoGetModelSurface( model, s );
261 if( surface == NULL )
264 /* only handle triangle surfaces initially (fixme: support patches) */
265 if( PicoGetSurfaceType( surface ) != PICO_TRIANGLES )
268 /* fix the surface's normals */
269 PicoFixSurfaceNormals( surface );
271 /* allocate a surface (ydnar: gs mods) */
272 ds = AllocDrawSurface( SURFACE_TRIANGLES );
273 ds->entityNum = eNum;
274 ds->castShadows = castShadows;
275 ds->recvShadows = recvShadows;
277 /* get shader name */
278 shader = PicoGetSurfaceShader( surface );
282 picoShaderName = PicoGetShaderName( shader );
284 /* handle shader remapping */
286 for( rm = remap; rm != NULL; rm = rm->next )
288 if( rm->from[ 0 ] == '*' && rm->from[ 1 ] == '\0' )
290 else if( !Q_stricmp( picoShaderName, rm->from ) )
292 Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", picoShaderName, rm->to );
293 picoShaderName = rm->to;
301 Sys_FPrintf( SYS_VRB, "Globbing %s to %s\n", picoShaderName, glob->to );
302 picoShaderName = glob->to;
305 /* shader renaming for sof2 */
306 if( renameModelShaders )
308 strcpy( shaderName, picoShaderName );
309 StripExtension( shaderName );
311 strcat( shaderName, "_RMG_BSP" );
313 strcat( shaderName, "_BSP" );
314 si = ShaderInfoForShader( shaderName );
317 si = ShaderInfoForShader( picoShaderName );
322 /* set lightmap scale */
323 ds->lightmapScale = lightmapScale;
326 if( (si != NULL && si->forceMeta) || (spawnFlags & 4) ) /* 3rd bit */
327 ds->type = SURFACE_FORCED_META;
329 /* set particulars */
330 ds->numVerts = PicoGetSurfaceNumVertexes( surface );
331 ds->verts = safe_malloc( ds->numVerts * sizeof( ds->verts[ 0 ] ) );
332 memset( ds->verts, 0, ds->numVerts * sizeof( ds->verts[ 0 ] ) );
334 ds->numIndexes = PicoGetSurfaceNumIndexes( surface );
335 ds->indexes = safe_malloc( ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
336 memset( ds->indexes, 0, ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
339 for( i = 0; i < ds->numVerts; i++ )
342 dv = &ds->verts[ i ];
345 xyz = PicoGetSurfaceXYZ( surface, i );
346 VectorCopy( xyz, dv->xyz );
347 m4x4_transform_point( transform, dv->xyz );
349 normal = PicoGetSurfaceNormal( surface, i );
350 VectorCopy( normal, dv->normal );
351 m4x4_transform_normal( nTransform, dv->normal );
352 VectorNormalize( dv->normal, dv->normal );
354 /* ydnar: tek-fu celshading support for flat shaded shit */
357 dv->st[ 0 ] = si->stFlat[ 0 ];
358 dv->st[ 1 ] = si->stFlat[ 1 ];
361 /* ydnar: gs mods: added support for explicit shader texcoord generation */
364 /* project the texture */
365 dv->st[ 0 ] = DotProduct( si->vecs[ 0 ], dv->xyz );
366 dv->st[ 1 ] = DotProduct( si->vecs[ 1 ], dv->xyz );
369 /* normal texture coordinates */
372 st = PicoGetSurfaceST( surface, 0, i );
373 dv->st[ 0 ] = st[ 0 ];
374 dv->st[ 1 ] = st[ 1 ];
377 /* set lightmap/color bits */
378 color = PicoGetSurfaceColor( surface, 0, i );
379 for( j = 0; j < MAX_LIGHTMAPS; j++ )
381 dv->lightmap[ j ][ 0 ] = 0.0f;
382 dv->lightmap[ j ][ 1 ] = 0.0f;
383 dv->color[ j ][ 0 ] = color[ 0 ];
384 dv->color[ j ][ 1 ] = color[ 1 ];
385 dv->color[ j ][ 2 ] = color[ 2 ];
386 dv->color[ j ][ 3 ] = color[ 3 ];
391 indexes = PicoGetSurfaceIndexes( surface, 0 );
392 for( i = 0; i < ds->numIndexes; i++ )
393 ds->indexes[ i ] = indexes[ i ];
396 ds->celShader = celShader;
398 /* ydnar: giant hack land: generate clipping brushes for model triangles */
399 if( si->clipModel || (spawnFlags & 2) ) /* 2nd bit */
401 vec3_t points[ 3 ], backs[ 3 ];
402 vec4_t plane, reverse, pa, pb, pc;
407 if( !si->clipModel &&
408 ((si->compileFlags & C_TRANSLUCENT) || !(si->compileFlags & C_SOLID)) )
412 if( (nummapplanes + 64) >= (MAX_MAP_PLANES >> 1) )
415 /* walk triangle list */
416 for( i = 0; i < ds->numIndexes; i += 3 )
419 if( (nummapplanes + 64) >= (MAX_MAP_PLANES >> 1) )
421 Sys_Printf( "WARNING: MAX_MAP_PLANES (%d) hit generating clip brushes for model %s.\n",
422 MAX_MAP_PLANES, name );
426 /* make points and back points */
427 for( j = 0; j < 3; j++ )
430 dv = &ds->verts[ ds->indexes[ i + j ] ];
433 VectorCopy( dv->xyz, points[ j ] );
434 VectorCopy( dv->xyz, backs[ j ] );
436 /* find nearest axial to normal and push back points opposite */
437 /* note: this doesn't work as well as simply using the plane of the triangle, below */
438 for( k = 0; k < 3; k++ )
440 if( fabs( dv->normal[ k ] ) > fabs( dv->normal[ (k + 1) % 3 ] ) &&
441 fabs( dv->normal[ k ] ) > fabs( dv->normal[ (k + 2) % 3 ] ) )
443 backs[ j ][ k ] += dv->normal[ k ] < 0.0f ? 64.0f : -64.0f;
449 /* make plane for triangle */
450 if( PlaneFromPoints( plane, points[ 0 ], points[ 1 ], points[ 2 ] ) )
452 /* regenerate back points */
453 for( j = 0; j < 3; j++ )
456 dv = &ds->verts[ ds->indexes[ i + j ] ];
459 VectorCopy( dv->xyz, backs[ j ] );
461 /* find nearest axial to plane normal and push back points opposite */
462 for( k = 0; k < 3; k++ )
464 if( fabs( plane[ k ] ) > fabs( plane[ (k + 1) % 3 ] ) &&
465 fabs( plane[ k ] ) > fabs( plane[ (k + 2) % 3 ] ) )
467 backs[ j ][ k ] += plane[ k ] < 0.0f ? 64.0f : -64.0f;
473 /* make back plane */
474 VectorScale( plane, -1.0f, reverse );
475 reverse[ 3 ] = -(plane[ 3 ] - 1);
477 /* make back pyramid point */
478 VectorCopy( points[ 0 ], nadir );
479 VectorAdd( nadir, points[ 1 ], nadir );
480 VectorAdd( nadir, points[ 2 ], nadir );
481 VectorScale( nadir, 0.3333333333333f, nadir );
482 VectorMA( nadir, -2.0f, plane, nadir );
484 /* make 3 more planes */
485 //% if( PlaneFromPoints( pa, points[ 2 ], points[ 1 ], nadir ) &&
486 //% PlaneFromPoints( pb, points[ 1 ], points[ 0 ], nadir ) &&
487 //% PlaneFromPoints( pc, points[ 0 ], points[ 2 ], nadir ) )
488 if( PlaneFromPoints( pa, points[ 2 ], points[ 1 ], backs[ 1 ] ) &&
489 PlaneFromPoints( pb, points[ 1 ], points[ 0 ], backs[ 0 ] ) &&
490 PlaneFromPoints( pc, points[ 0 ], points[ 2 ], backs[ 2 ] ) )
493 buildBrush = AllocBrush( 48 );
495 buildBrush->entityNum = mapEntityNum;
496 buildBrush->original = buildBrush;
497 buildBrush->contentShader = si;
498 buildBrush->compileFlags = si->compileFlags;
499 buildBrush->contentFlags = si->contentFlags;
500 buildBrush->detail = qtrue;
502 /* set up brush sides */
503 buildBrush->numsides = 5;
504 for( j = 0; j < buildBrush->numsides; j++ )
505 buildBrush->sides[ j ].shaderInfo = si;
506 buildBrush->sides[ 0 ].planenum = FindFloatPlane( plane, plane[ 3 ], 3, points );
507 buildBrush->sides[ 1 ].planenum = FindFloatPlane( pa, pa[ 3 ], 1, &points[ 2 ] );
508 buildBrush->sides[ 2 ].planenum = FindFloatPlane( pb, pb[ 3 ], 1, &points[ 1 ] );
509 buildBrush->sides[ 3 ].planenum = FindFloatPlane( pc, pc[ 3 ], 1, &points[ 0 ] );
510 buildBrush->sides[ 4 ].planenum = FindFloatPlane( reverse, reverse[ 3 ], 3, points );
513 if( CreateBrushWindings( buildBrush ) )
516 //% EmitBrushes( buildBrush, NULL, NULL );
517 buildBrush->next = entities[ mapEntityNum ].brushes;
518 entities[ mapEntityNum ].brushes = buildBrush;
519 entities[ mapEntityNum ].numBrushes++;
534 adds misc_model surfaces to the bsp
537 void AddTriangleModels( entity_t *e )
539 int num, frame, castShadows, recvShadows, spawnFlags;
541 const char *targetName;
542 const char *target, *model, *value;
543 char shader[ MAX_QPATH ];
544 shaderInfo_t *celShader;
545 float temp, baseLightmapScale, lightmapScale;
546 vec3_t origin, scale, angles;
549 remap_t *remap, *remap2;
554 Sys_FPrintf( SYS_VRB, "--- AddTriangleModels ---\n" );
556 /* get current brush entity targetname */
561 targetName = ValueForKey( e, "targetname" );
563 /* misc_model entities target non-worldspawn brush model entities */
564 if( targetName[ 0 ] == '\0' )
568 /* get lightmap scale */
569 baseLightmapScale = FloatForKey( e, "_lightmapscale" );
570 if( baseLightmapScale <= 0.0f )
571 baseLightmapScale = 0.0f;
573 /* walk the entity list */
574 for( num = 1; num < numEntities; num++ )
577 e2 = &entities[ num ];
579 /* convert misc_models into raw geometry */
580 if( Q_stricmp( "misc_model", ValueForKey( e2, "classname" ) ) )
583 /* ydnar: added support for md3 models on non-worldspawn models */
584 target = ValueForKey( e2, "target" );
585 if( strcmp( target, targetName ) )
589 model = ValueForKey( e2, "model" );
590 if( model[ 0 ] == '\0' )
592 Sys_Printf( "WARNING: misc_model at %i %i %i without a model key\n",
593 (int) origin[ 0 ], (int) origin[ 1 ], (int) origin[ 2 ] );
597 /* get model frame */
598 frame = IntForKey( e2, "_frame" );
600 /* worldspawn (and func_groups) default to cast/recv shadows in worldspawn group */
603 castShadows = WORLDSPAWN_CAST_SHADOWS;
604 recvShadows = WORLDSPAWN_RECV_SHADOWS;
607 /* other entities don't cast any shadows, but recv worldspawn shadows */
610 castShadows = ENTITY_CAST_SHADOWS;
611 recvShadows = ENTITY_RECV_SHADOWS;
614 /* get explicit shadow flags */
615 GetEntityShadowFlags( e2, e, &castShadows, &recvShadows );
618 spawnFlags = IntForKey( e2, "spawnflags" );
621 GetVectorForKey( e2, "origin", origin );
622 VectorSubtract( origin, e->origin, origin ); /* offset by parent */
625 scale[ 0 ] = scale[ 1 ] = scale[ 2 ] = 1.0f;
626 temp = FloatForKey( e2, "modelscale" );
628 scale[ 0 ] = scale[ 1 ] = scale[ 2 ] = temp;
629 value = ValueForKey( e2, "modelscale_vec" );
630 if( value[ 0 ] != '\0' )
631 sscanf( value, "%f %f %f", &scale[ 0 ], &scale[ 1 ], &scale[ 2 ] );
633 /* get "angle" (yaw) or "angles" (pitch yaw roll) */
634 angles[ 0 ] = angles[ 1 ] = angles[ 2 ] = 0.0f;
635 angles[ 2 ] = FloatForKey( e2, "angle" );
636 value = ValueForKey( e2, "angles" );
637 if( value[ 0 ] != '\0' )
638 sscanf( value, "%f %f %f", &angles[ 1 ], &angles[ 2 ], &angles[ 0 ] );
640 /* set transform matrix (thanks spog) */
641 m4x4_identity( transform );
642 m4x4_pivoted_transform_by_vec3( transform, origin, angles, eXYZ, scale, vec3_origin );
644 /* get shader remappings */
646 for( ep = e2->epairs; ep != NULL; ep = ep->next )
648 /* look for keys prefixed with "_remap" */
649 if( ep->key != NULL && ep->value != NULL &&
650 ep->key[ 0 ] != '\0' && ep->value[ 0 ] != '\0' &&
651 !Q_strncasecmp( ep->key, "_remap", 6 ) )
653 /* create new remapping */
655 remap = safe_malloc( sizeof( *remap ) );
656 remap->next = remap2;
657 strcpy( remap->from, ep->value );
659 /* split the string */
660 split = strchr( remap->from, ';' );
663 Sys_Printf( "WARNING: Shader _remap key found in misc_model without a ; character\n" );
669 /* store the split */
671 strcpy( remap->to, (split + 1) );
674 //% Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", remap->from, remap->to );
678 /* ydnar: cel shader support */
679 value = ValueForKey( e2, "_celshader" );
680 if( value[ 0 ] == '\0' )
681 value = ValueForKey( &entities[ 0 ], "_celshader" );
682 if( value[ 0 ] != '\0' )
684 sprintf( shader, "textures/%s", value );
685 celShader = ShaderInfoForShader( shader );
690 /* get lightmap scale */
691 lightmapScale = FloatForKey( e2, "_lightmapscale" );
692 if( lightmapScale <= 0.0f )
693 lightmapScale = baseLightmapScale;
695 /* insert the model */
696 InsertModel( (char*) model, frame, transform, remap, celShader, mapEntityNum, castShadows, recvShadows, spawnFlags, lightmapScale );
698 /* free shader remappings */
699 while( remap != NULL )
701 remap2 = remap->next;