]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/q3map2/model.c
skinfiles: use the VFS
[xonotic/netradiant.git] / tools / quake3 / q3map2 / model.c
index 72ce6a5d89e028c3e52beb52c3dcd1b281b75850..00ff693eb47a3bf1600a5fac154a3097396ddeb6 100644 (file)
@@ -90,7 +90,7 @@ FindModel() - ydnar
 finds an existing picoModel and returns a pointer to the picoModel_t struct or NULL if not found
 */
 
-picoModel_t *FindModel( char *name, int frame )
+picoModel_t *FindModel( const char *name, int frame )
 {
        int                     i;
        
@@ -123,7 +123,7 @@ LoadModel() - ydnar
 loads a picoModel and returns a pointer to the picoModel_t struct or NULL if not found
 */
 
-picoModel_t *LoadModel( char *name, int frame )
+picoModel_t *LoadModel( const char *name, int frame )
 {
        int                             i;
        picoModel_t             *model, **pm;
@@ -169,7 +169,7 @@ picoModel_t *LoadModel( char *name, int frame )
                        return NULL;
                
                /* set data */
-               PicoSetModelName( *pm, name );
+               PicoSetModelName( *pm, (char*) name );
                PicoSetModelFrameNum( *pm, frame );
        }
        
@@ -206,7 +206,7 @@ InsertModel() - ydnar
 adds a picomodel into the bsp
 */
 
-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 )
+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 )
 {
        int                                     i, j, k, s, numSurfaces;
        m4x4_t                          identity, nTransform;
@@ -222,14 +222,79 @@ void InsertModel( char *name, int frame, m4x4_t transform, remap_t *remap, shade
        byte                            *color;
        picoIndex_t                     *indexes;
        remap_t                         *rm, *glob;
+       skinfile_t                      *sf, *sf2;
        double                          normalEpsilon_save;
        double                          distanceEpsilon_save;
+       char                            skinfilename[ MAX_QPATH ];
+       char                            *skinfilecontent;
+       int                                     skinfilesize;
+       char                            *skinfileptr, *skinfilenextptr;
+       FILE                            *skinfilehandle;
        
        
        /* get model */
        model = LoadModel( name, frame );
        if( model == NULL )
                return;
+
+       /* load skin file */
+       snprintf(skinfilename, sizeof(skinfilename), "%s_%d.skin", name, skin);
+       skinfilename[sizeof(skinfilename)-1] = 0;
+       skinfilehandle = fopen(skinfilename, "r");
+       skinfilesize = vfsLoadFile(skinfilename, (void**) &skinfilecontent, 0);
+       if(skinfilesize < 0)
+       {
+               /* fallback to skin 0 if invalid */
+               snprintf(skinfilename, sizeof(skinfilename), "%s_0.skin", name);
+               skinfilename[sizeof(skinfilename)-1] = 0;
+               skinfilesize = vfsLoadFile(skinfilename, (void**) &skinfilecontent, 0);
+               if(skinfilesize < 0)
+                       Sys_Printf( "Skin %d of %s does not exist, using 0 instead\n", skin, name );
+       }
+       sf = NULL;
+       if(skinfilesize)
+       {
+               Sys_Printf( "Using skin %d of %s\n", skin, name );
+               int pos;
+               for(skinfileptr = skinfilecontent; *skinfileptr; skinfileptr = skinfilenextptr)
+               {
+                       // for fscanf
+                       char format[64];
+
+                       skinfilenextptr = strchr(skinfileptr, '\r');
+                       if(skinfilenextptr)
+                       {
+                               *skinfilenextptr++ = 0;
+                       }
+                       else
+                       {
+                               skinfilenextptr = strchr(skinfileptr, '\n');
+                               if(skinfilenextptr)
+                                       *skinfilenextptr++ = 0;
+                               else
+                                       skinfilenextptr = skinfileptr + strlen(skinfileptr);
+                       }
+
+                       /* create new item */
+                       sf2 = sf;
+                       sf = safe_malloc( sizeof( *sf ) );
+                       sf->next = sf2;
+
+                       sprintf(format, "replace %%%ds %%%ds%%n", (int)sizeof(sf->name)-1, (int)sizeof(sf->to)-1);
+                       pos = 0;
+                       if(sscanf(skinfileptr, format, &sf->name, &sf->to, &pos) > 0 && pos > 0)
+                               continue;
+                       sprintf(format, "%%%ds,%%%ds%%n", (int)sizeof(sf->name)-1, (int)sizeof(sf->to)-1);
+                       pos = 0;
+                       if(sscanf(skinfileptr, format, &sf->name, &sf->to, &pos) > 0 && pos > 0)
+                               continue;
+
+                       /* invalid input line -> discard sf struct */
+                       free(sf);
+                       sf = sf2;
+               }
+               free(skinfilecontent);
+       }
        
        /* handle null matrix */
        if( transform == NULL )
@@ -252,6 +317,10 @@ void InsertModel( char *name, int frame, m4x4_t transform, remap_t *remap, shade
        /* fix bogus lightmap scale */
        if( lightmapScale <= 0.0f )
                lightmapScale = 1.0f;
+
+       /* fix bogus shade angle */
+       if( shadeAngle <= 0.0f )
+               shadeAngle = 0.0f;
        
        /* each surface on the model will become a new map drawsurface */
        numSurfaces = PicoGetModelNumSurfaces( model );
@@ -279,7 +348,27 @@ void InsertModel( char *name, int frame, m4x4_t transform, remap_t *remap, shade
                        picoShaderName = "";
                else
                        picoShaderName = PicoGetShaderName( shader );
-               
+
+               /* handle .skin file */
+               if(sf)
+               {
+                       picoShaderName = NULL;
+                       for(sf2 = sf; sf2 != NULL; sf2 = sf2->next)
+                       {
+                               if( !Q_stricmp( surface->name, sf2->name ) )
+                               {
+                                       Sys_FPrintf( SYS_VRB, "Skin file: mapping %s to %s\n", surface->name, sf2->to );
+                                       picoShaderName = sf2->to;
+                                       break;
+                               }
+                       }
+                       if(!picoShaderName)
+                       {
+                               Sys_FPrintf( SYS_VRB, "Skin file: not mapping %s\n", surface->name );
+                               continue;
+                       }
+               }
+
                /* handle shader remapping */
                glob = NULL;
                for( rm = remap; rm != NULL; rm = rm->next )
@@ -322,18 +411,21 @@ void InsertModel( char *name, int frame, m4x4_t transform, remap_t *remap, shade
                if( (si != NULL && si->forceMeta) || (spawnFlags & 4) ) /* 3rd bit */
                        ds->type = SURFACE_FORCED_META;
 
-               /* get shading angle from shader or entity */
-               if( si->shadeAngleDegrees )
-                       ds->shadeAngleDegrees = si->shadeAngleDegrees;
-               else if( shadeAngle )
-                       ds->shadeAngleDegrees = shadeAngle; /* otherwise it's 0 */
-
                /* fix the surface's normals (jal: conditioned by shader info) */
                if( !(spawnFlags & 64) && ( shadeAngle == 0.0f || ds->type != SURFACE_FORCED_META ) )
                        PicoFixSurfaceNormals( surface );
+
+               /* set sample size */
+               if( lightmapSampleSize > 0.0f )
+                       ds->sampleSize = lightmapSampleSize;
                
                /* set lightmap scale */
-               ds->lightmapScale = lightmapScale;
+               if( lightmapScale > 0.0f )
+                       ds->lightmapScale = lightmapScale;
+
+               /* set shading angle */
+               if( shadeAngle > 0.0f )
+                       ds->shadeAngleDegrees = shadeAngle;
                
                /* set particulars */
                ds->numVerts = PicoGetSurfaceNumVertexes( surface );
@@ -394,7 +486,7 @@ void InsertModel( char *name, int frame, m4x4_t transform, remap_t *remap, shade
                                        dv->color[ j ][ 0 ] = 255.0f;
                                        dv->color[ j ][ 1 ] = 255.0f;
                                        dv->color[ j ][ 2 ] = 255.0f;
-                                       dv->color[ j ][ 3 ] = color[ 0 ] * 0.3f + color[ 1 ] * 0.59f + color[ 2 ] * 0.11f;
+                                       dv->color[ j ][ 3 ] = RGBTOGRAY( color );
                                }
                                else
                                {
@@ -422,8 +514,7 @@ void InsertModel( char *name, int frame, m4x4_t transform, remap_t *remap, shade
                        
                        
                        /* temp hack */
-                       if( !si->clipModel &&
-                               ((si->compileFlags & C_TRANSLUCENT) || !(si->compileFlags & C_SOLID)) )
+                       if( !si->clipModel && !(si->compileFlags & C_SOLID) )
                                continue;
                        
                        /* walk triangle list */
@@ -597,7 +688,7 @@ adds misc_model surfaces to the bsp
 
 void AddTriangleModels( entity_t *e )
 {
-       int                             num, frame, castShadows, recvShadows, spawnFlags;
+       int                             num, frame, skin, castShadows, recvShadows, spawnFlags;
        entity_t                *e2;
        const char              *targetName;
        const char              *target, *model, *value;
@@ -605,6 +696,7 @@ void AddTriangleModels( entity_t *e )
        shaderInfo_t    *celShader;
        float                   temp, baseLightmapScale, lightmapScale;
        float                   shadeAngle;
+       int                             lightmapSampleSize;
        vec3_t                  origin, scale, angles;
        m4x4_t                  transform;
        epair_t                 *ep;
@@ -629,11 +721,22 @@ void AddTriangleModels( entity_t *e )
        
        /* get lightmap scale */
        /* vortex: added _ls key (short name of lightmapscale) */
-       baseLightmapScale = FloatForKey( e, "_lightmapscale" );
-       if( baseLightmapScale <= 0.0f )
-               baseLightmapScale = FloatForKey( e, "_ls" );
-       if( baseLightmapScale <= 0.0f )
-               baseLightmapScale = 0.0f;
+       baseLightmapScale = 0.0f;
+       if( strcmp( "", ValueForKey( e, "lightmapscale" ) ) ||
+               strcmp( "", ValueForKey( e, "_lightmapscale" ) ) || 
+               strcmp( "", ValueForKey( e, "_ls" ) ) )
+       {
+               baseLightmapScale = FloatForKey( e, "lightmapscale" );
+               if( baseLightmapScale <= 0.0f )
+                       baseLightmapScale = FloatForKey( e, "_lightmapscale" );
+               if( baseLightmapScale <= 0.0f )
+                       baseLightmapScale = FloatForKey( e, "_ls" );
+               if( baseLightmapScale < 0.0f )
+                       baseLightmapScale = 0.0f;
+               if( baseLightmapScale > 0.0f )
+                       Sys_Printf( "World Entity has lightmap scale of %.4f\n", baseLightmapScale );
+       }
+       
        
        /* walk the entity list */
        for( num = 1; num < numEntities; num++ )
@@ -751,35 +854,60 @@ void AddTriangleModels( entity_t *e )
                }
                else
                        celShader = *globalCelShader ? ShaderInfoForShader(globalCelShader) : NULL;
-               
+
+               /* jal : entity based _samplesize */
+               lightmapSampleSize = 0;
+               if ( strcmp( "", ValueForKey( e2, "_lightmapsamplesize" ) ) )
+                       lightmapSampleSize = IntForKey( e2, "_lightmapsamplesize" );
+               else if ( strcmp( "", ValueForKey( e2, "_samplesize" ) ) )
+                       lightmapSampleSize = IntForKey( e2, "_samplesize" );
+
+               if( lightmapSampleSize < 0 )
+                       lightmapSampleSize = 0;
+
+               if( lightmapSampleSize > 0.0f )
+                       Sys_Printf( "misc_model has lightmap sample size of %.d\n", lightmapSampleSize );
+
                /* get lightmap scale */
                /* vortex: added _ls key (short name of lightmapscale) */
-               lightmapScale = FloatForKey( e2, "_lightmapscale" );
-               if( lightmapScale <= 0.0f )
-                       lightmapScale = FloatForKey( e2, "_ls" );
-               if( lightmapScale <= 0.0f )
-                       lightmapScale = baseLightmapScale;
+               lightmapScale = 0.0f;
+               if( strcmp( "", ValueForKey( e2, "lightmapscale" ) ) ||
+                       strcmp( "", ValueForKey( e2, "_lightmapscale" ) ) || 
+                       strcmp( "", ValueForKey( e2, "_ls" ) ) )
+               {
+                       lightmapScale = FloatForKey( e2, "lightmapscale" );
+                       if( lightmapScale <= 0.0f )
+                               lightmapScale = FloatForKey( e2, "_lightmapscale" );
+                       if( lightmapScale <= 0.0f )
+                               lightmapScale = FloatForKey( e2, "_ls" );
+                       if( lightmapScale < 0.0f )
+                               lightmapScale = 0.0f;
+                       if( lightmapScale > 0.0f )
+                               Sys_Printf( "misc_model has lightmap scale of %.4f\n", lightmapScale );
+               }
 
                /* jal : entity based _shadeangle */
                shadeAngle = 0.0f;
                if ( strcmp( "", ValueForKey( e2, "_shadeangle" ) ) )
                        shadeAngle = FloatForKey( e2, "_shadeangle" );
                /* vortex' aliases */
-               else if ( strcmp( "", ValueForKey( mapEnt, "_smoothnormals" ) ) )
-                       shadeAngle = FloatForKey( mapEnt, "_smoothnormals" );
-               else if ( strcmp( "", ValueForKey( mapEnt, "_sn" ) ) )
-                       shadeAngle = FloatForKey( mapEnt, "_sn" );
-               else if ( strcmp( "", ValueForKey( mapEnt, "_smooth" ) ) )
-                       shadeAngle = FloatForKey( mapEnt, "_smooth" );
+               else if ( strcmp( "", ValueForKey( e2, "_smoothnormals" ) ) )
+                       shadeAngle = FloatForKey( e2, "_smoothnormals" );
+               else if ( strcmp( "", ValueForKey( e2, "_sn" ) ) )
+                       shadeAngle = FloatForKey( e2, "_sn" );
+               else if ( strcmp( "", ValueForKey( e2, "_smooth" ) ) )
+                       shadeAngle = FloatForKey( e2, "_smooth" );
 
                if( shadeAngle < 0.0f )
                        shadeAngle = 0.0f;
 
                if( shadeAngle > 0.0f )
                        Sys_Printf( "misc_model has shading angle of %.4f\n", shadeAngle );
-               
+
+               skin = IntForKey(e2, "skin");
+
                /* insert the model */
-               InsertModel( (char*) model, frame, transform, remap, celShader, mapEntityNum, castShadows, recvShadows, spawnFlags, lightmapScale, shadeAngle );
+               InsertModel( model, skin, frame, transform, remap, celShader, mapEntityNum, castShadows, recvShadows, spawnFlags, lightmapScale, lightmapSampleSize, shadeAngle );
                
                /* free shader remappings */
                while( remap != NULL )