engine: mod_bmodel: merge Mod_LoadColoredLightmap and Mod_LoadDeluxemap into a single function

This commit is contained in:
Alibek Omarov
2024-11-17 04:35:20 +03:00
parent 0ceb0d9e11
commit 0813d2c7ce

View File

@@ -1353,7 +1353,6 @@ static void Mod_CalcSurfaceExtents( model_t *mod, msurface_t *surf )
int bmins[2], bmaxs[2];
int i, j, e, sample_size;
mextrasurf_t *info = surf->info;
int facenum = surf - mod->surfaces;
mtexinfo_t *tex;
mvertex_t *v;
@@ -1636,7 +1635,6 @@ Mod_SetupHull
static void Mod_SetupHull( dbspmodel_t *bmod, model_t *mod, poolhandle_t mempool, int headnode, int hullnum )
{
hull_t *hull = &mod->hulls[hullnum];
int count;
// assume no hull
hull->firstclipnode = hull->lastclipnode = 0;
@@ -1671,7 +1669,6 @@ static void Mod_SetupHull( dbspmodel_t *bmod, model_t *mod, poolhandle_t mempool
return; // no hull specified
CountClipNodes32_r( bmod->clipnodes_out, hull, headnode );
count = hull->lastclipnode;
// fit array to real count
hull->clipnodes = (mclipnode_t *)Mem_Malloc( mempool, sizeof( mclipnode_t ) * hull->lastclipnode );
@@ -1682,108 +1679,66 @@ static void Mod_SetupHull( dbspmodel_t *bmod, model_t *mod, poolhandle_t mempool
RemapClipNodes_r( bmod->clipnodes_out, hull, headnode );
}
/*
=================
Mod_LoadColoredLighting
=================
*/
static qboolean Mod_LoadColoredLighting( model_t *mod, dbspmodel_t *bmod )
static qboolean Mod_LoadLitfile( model_t *mod, const char *ext, size_t expected_size, color24 **out, size_t *outsize )
{
char modelname[64];
char path[64];
int iCompare;
fs_offset_t litdatasize;
byte *in;
char modelname[64], path[64];
int iCompare;
fs_offset_t datasize;
byte *in;
COM_FileBase( mod->name, modelname, sizeof( modelname ));
Q_snprintf( path, sizeof( path ), "maps/%s.lit", modelname );
Q_snprintf( path, sizeof( path ), "maps/%s.%s", modelname, ext );
// make sure what deluxemap is actual
if( !pfnCompareFileTime( path, mod->name, &iCompare ))
return false;
if( iCompare < 0 ) // this may happens if level-designer used -onlyents key for hlcsg
Con_Printf( S_WARN "%s probably is out of date\n", path );
in = FS_LoadFile( path, &litdatasize, false );
in = FS_LoadFile( path, &datasize, false );
Assert( in != NULL );
if( *(uint *)in != IDDELUXEMAPHEADER || *((uint *)in + 1) != DELUXEMAP_VERSION )
if( !in )
{
Mem_Free( in );
Con_Printf( S_ERROR "couldn't load %s\n", path );
return false;
}
if( datasize <= 8 ) // header + version
{
Con_Printf( S_ERROR "%s is too short\n", path );
goto cleanup_and_error;
}
if( LittleLong( ((uint *)in)[0] ) != IDDELUXEMAPHEADER )
{
Con_Printf( S_ERROR "%s is corrupted\n", path );
goto cleanup_and_error;
}
if( LittleLong( ((uint *)in)[1] ) != DELUXEMAP_VERSION )
{
Con_Printf( S_ERROR "has %s mismatched version (%u should be %u)\n", path, LittleLong( ((uint *)in)[1] ), DELUXEMAP_VERSION );
goto cleanup_and_error;
}
// skip header bytes
litdatasize -= 8;
datasize -= 8;
if( litdatasize != ( bmod->lightdatasize * 3 ))
if( datasize != expected_size )
{
Con_Printf( S_ERROR "%s has mismatched size (%li should be %zu)\n", path, (long)litdatasize, bmod->lightdatasize * 3 );
Mem_Free( in );
return false;
Con_Printf( S_ERROR "%s has mismatched size (%li should be %zu)\n", path, (long)datasize, expected_size );
goto cleanup_and_error;
}
mod->lightdata = Mem_Malloc( mod->mempool, litdatasize );
memcpy( mod->lightdata, in + 8, litdatasize );
SetBits( mod->flags, MODEL_COLORED_LIGHTING );
bmod->lightdatasize = litdatasize;
*out = Mem_Malloc( mod->mempool, datasize );
memcpy( *out, in + 8, datasize );
*outsize = datasize;
Mem_Free( in );
return true;
}
/*
=================
Mod_LoadDeluxemap
=================
*/
static void Mod_LoadDeluxemap( model_t *mod, dbspmodel_t *bmod )
{
char modelname[64];
fs_offset_t deluxdatasize;
char path[64];
int iCompare;
byte *in;
if( !FBitSet( host.features, ENGINE_LOAD_DELUXEDATA ))
return;
COM_FileBase( mod->name, modelname, sizeof( modelname ));
Q_snprintf( path, sizeof( path ), "maps/%s.dlit", modelname );
// make sure what deluxemap is actual
if( !pfnCompareFileTime( path, mod->name, &iCompare ))
return;
if( iCompare < 0 ) // this may happens if level-designer used -onlyents key for hlcsg
Con_Printf( S_WARN "%s probably is out of date\n", path );
in = FS_LoadFile( path, &deluxdatasize, false );
Assert( in != NULL );
if( *(uint *)in != IDDELUXEMAPHEADER || *((uint *)in + 1) != DELUXEMAP_VERSION )
{
Mem_Free( in );
return;
}
// skip header bytes
deluxdatasize -= 8;
if( deluxdatasize != bmod->lightdatasize )
{
Con_Reportf( S_ERROR "%s has mismatched size (%li should be %zu)\n", path, (long)deluxdatasize, bmod->lightdatasize );
Mem_Free( in );
return;
}
bmod->deluxedata_out = Mem_Malloc( mod->mempool, deluxdatasize );
memcpy( bmod->deluxedata_out, in + 8, deluxdatasize );
bmod->deluxdatasize = deluxdatasize;
cleanup_and_error:
Mem_Free( in );
return false;
}
/*
@@ -3366,7 +3321,8 @@ static void Mod_LoadLightVecs( model_t *mod, dbspmodel_t *bmod )
{
if( bmod->deluxdatasize > 0 )
Con_Printf( S_ERROR "%s: has mismatched size (%zu should be %zu)\n", __func__, bmod->deluxdatasize, bmod->lightdatasize );
else Mod_LoadDeluxemap( mod, bmod ); // old method
else
Mod_LoadLitfile( mod, "dlit", bmod->lightdatasize, &bmod->deluxedata_out, &bmod->deluxdatasize ); // old method
return;
}
@@ -3399,10 +3355,7 @@ Mod_LoadLighting
*/
static void Mod_LoadLighting( model_t *mod, dbspmodel_t *bmod )
{
int i, lightofs;
msurface_t *surf;
color24 *out;
byte *in;
int i;
if( !bmod->lightdatasize )
return;
@@ -3410,15 +3363,15 @@ static void Mod_LoadLighting( model_t *mod, dbspmodel_t *bmod )
switch( bmod->lightmap_samples )
{
case 1:
if( !Mod_LoadColoredLighting( mod, bmod ))
if( !Mod_LoadLitfile( mod, "lit", bmod->lightdatasize * 3, &mod->lightdata, &bmod->lightdatasize ))
{
mod->lightdata = out = (color24 *)Mem_Malloc( mod->mempool, bmod->lightdatasize * sizeof( color24 ));
in = bmod->lightdata;
mod->lightdata = (color24 *)Mem_Malloc( mod->mempool, bmod->lightdatasize * sizeof( color24 ));
// expand the white lighting data
for( i = 0; i < bmod->lightdatasize; i++, out++ )
out->r = out->g = out->b = *in++;
for( i = 0; i < bmod->lightdatasize; i++ )
mod->lightdata[i].r = mod->lightdata[i].g = mod->lightdata[i].b = bmod->lightdata[i];
}
else SetBits( mod->flags, MODEL_COLORED_LIGHTING );
break;
case 3: // load colored lighting
mod->lightdata = Mem_Malloc( mod->mempool, bmod->lightdatasize );
@@ -3440,30 +3393,34 @@ static void Mod_LoadLighting( model_t *mod, dbspmodel_t *bmod )
SetBits( world.flags, FWORLD_HAS_DELUXEMAP );
}
surf = mod->surfaces;
// setup lightdata pointers
for( i = 0; i < mod->numsurfaces; i++, surf++ )
if( !mod->lightdata )
return;
for( i = 0; i < mod->numsurfaces; i++ )
{
int lightofs;
if( bmod->version == QBSP2_VERSION )
lightofs = bmod->surfaces32[i].lightofs;
else lightofs = bmod->surfaces[i].lightofs;
else
lightofs = bmod->surfaces[i].lightofs;
if( mod->lightdata && lightofs != -1 )
if( lightofs != -1 )
{
int offset = (lightofs / bmod->lightmap_samples);
int offset = lightofs / bmod->lightmap_samples;
// NOTE: we divide offset by three because lighting and deluxemap keep their pointers
// into three-bytes structs and shadowmap just monochrome
surf->samples = mod->lightdata + offset;
mod->surfaces[i].samples = mod->lightdata + offset;
// if deluxemap is present setup it too
if( bmod->deluxedata_out )
surf->info->deluxemap = bmod->deluxedata_out + offset;
mod->surfaces[i].info->deluxemap = bmod->deluxedata_out + offset;
// will be used by mods
if( bmod->shadowdata_out )
surf->info->shadowmap = bmod->shadowdata_out + offset;
mod->surfaces[i].info->shadowmap = bmod->shadowdata_out + offset;
}
}
}