From 92e976952e019e77588c26cbed6b6127290ded98 Mon Sep 17 00:00:00 2001 From: Andrey Akhmichin <15944199+nekonomicon@users.noreply.github.com> Date: Sun, 30 Mar 2025 21:04:32 +0500 Subject: [PATCH] utils: mdldec: fix UV coords shifting yet again. --- utils/mdldec/mdldec.c | 3 ++- utils/mdldec/smd.c | 35 +++++++++++++++++++++++++++++------ utils/mdldec/texture.c | 3 +++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/utils/mdldec/mdldec.c b/utils/mdldec/mdldec.c index f185ce36..8c55ab2e 100644 --- a/utils/mdldec/mdldec.c +++ b/utils/mdldec/mdldec.c @@ -78,7 +78,8 @@ static void TextureNameFix( void ) { ExtractFileName( texture->name, sizeof( texture->name )); - if( !IsValidName( texture->name )) + if( !( ( ( texture->width == 1 || texture->height == 1 ) + && texture->name[0] == '#' ) || IsValidName( texture->name ))) Q_snprintf( texture->name, sizeof( texture->name ), "MDLDEC_Texture%i.bmp", ++protected ); } diff --git a/utils/mdldec/smd.c b/utils/mdldec/smd.c index 1d9bd268..e7da491d 100644 --- a/utils/mdldec/smd.c +++ b/utils/mdldec/smd.c @@ -227,7 +227,8 @@ static void WriteTriangleInfo( FILE *fp, mstudiomodel_t *model, mstudiotexture_t int norm_index; int bone_index; int valid_bones; - float s, t, u, v; + int width, height; + float u, v; byte *vertbone; vec3_t *studioverts; vec3_t *studionorms; @@ -235,16 +236,19 @@ static void WriteTriangleInfo( FILE *fp, mstudiomodel_t *model, mstudiotexture_t float weights[MAXSTUDIOBONEWEIGHTS], oldweight, totalweight; matrix3x4 bonematrix[MAXSTUDIOBONEWEIGHTS], skinmatrix, *pskinmatrix; mstudioboneweight_t *studioboneweights; + char buffer[64]; vertbone = ( (byte *)model_hdr + model->vertinfoindex ); studioverts = (vec3_t *)( (byte *)model_hdr + model->vertindex ); studionorms = (vec3_t *)( (byte *)model_hdr + model->normindex ); studioboneweights = (mstudioboneweight_t *)( (byte *)model_hdr + model->blendvertinfoindex ); - s = 1.0f / texture->width; - t = 1.0f / texture->height; + Q_strncpy( buffer, texture->name, sizeof( buffer )); - fprintf( fp, "%s\n", texture->name ); + // Many filesystems couldn't write files if "#" is first character in the name. + if( buffer[0] == '#' ) buffer[0] = 's'; + + fprintf( fp, "%s\n", buffer ); for( i = 0; i < 3; i++ ) { @@ -293,10 +297,29 @@ static void WriteTriangleInfo( FILE *fp, mstudiomodel_t *model, mstudiotexture_t u = HalfToFloat( triverts[index]->s ); v = -HalfToFloat( triverts[index]->t ); } + else if( texture->width == 1 || texture->height == 1 ) + { + if( texture->name[0] == '#' ) + { + Q_strncpy( buffer, &texture->name[1], 4 ); + width = Q_atoi( buffer ); + + Q_strncpy( buffer, &texture->name[4], 4 ); + height = Q_atoi( buffer ); + + u = (float)triverts[index]->s / width; + v = 1.0f - (float)triverts[index]->t / height; + } + else + { + u = (float)triverts[index]->s; + v = 1.0f - (float)triverts[index]->t; + } + } else { - u = ( triverts[index]->s + 1.0f ) * s; - v = 1.0f - triverts[index]->t * t; + u = (float)triverts[index]->s / ( texture->width - 1 ); + v = 1.0f - (float)triverts[index]->t / ( texture->height - 1 ); } fprintf( fp, "%3i %f %f %f %f %f %f %f %f", diff --git a/utils/mdldec/texture.c b/utils/mdldec/texture.c index 42ddf560..ef85f5c0 100644 --- a/utils/mdldec/texture.c +++ b/utils/mdldec/texture.c @@ -164,6 +164,9 @@ void WriteTextures( void ) return; } + // Many filesystems couldn't write files if "#" is first character in the name. + if( texture->name[0] == '#' ) texture->name[0] = 's'; + if( !Q_stricmp( COM_FileExtension( texture->name ), "tga" )) WriteTGA( fp, texture ); else