diff --git a/utils/mdldec/mdldec.c b/utils/mdldec/mdldec.c index 576728b5..fc4fa0e5 100644 --- a/utils/mdldec/mdldec.c +++ b/utils/mdldec/mdldec.c @@ -41,21 +41,17 @@ static void SequenceNameFix( void ) { int i, j, counter; qboolean hasduplicates = false; - mstudioseqdesc_t *seqdesc, *seqdesc1; + mstudioseqdesc_t *seqdesc = (mstudioseqdesc_t *)( (byte *)model_hdr + model_hdr->seqindex ), *seqdesc1; - for( i = 0; i < model_hdr->numseq; i++ ) + for( i = 0; i < model_hdr->numseq; ++i, ++seqdesc ) { - seqdesc = (mstudioseqdesc_t *)( (byte *)model_hdr + model_hdr->seqindex ) + i; - counter = 1; - for( j = 0; j < model_hdr->numseq; j++ ) - { - seqdesc1 = (mstudioseqdesc_t *)( (byte *)model_hdr + model_hdr->seqindex ) + j; + seqdesc1 = (mstudioseqdesc_t *)( (byte *)model_hdr + model_hdr->seqindex ); + for( j = 0; j < model_hdr->numseq; ++j, ++seqdesc1 ) if( j != i && !Q_strncmp( seqdesc1->label, seqdesc->label, sizeof( seqdesc1->label ) ) ) Q_snprintf( seqdesc1->label, sizeof( seqdesc1->label ), "%s_%i", seqdesc1->label, ++counter ); - } if( counter > 1 ) { @@ -79,15 +75,11 @@ BoneNameFix static void BoneNameFix( void ) { int i, counter = 0; - mstudiobone_t *bone; - - for( i = 0; i < model_hdr->numbones; i++ ) - { - bone = (mstudiobone_t *)( (byte *)model_hdr + model_hdr->boneindex ) + i; + mstudiobone_t *bone = (mstudiobone_t *)( (byte *)model_hdr + model_hdr->boneindex ); + for( i = 0; i < model_hdr->numbones; ++i, ++bone ) if( bone->name[0] == '\0' ) Q_snprintf( bone->name, sizeof( bone->name ), "MDLDEC_Bone%i", ++counter ); - } if( counter ) printf( "WARNING: Gived name to %i unnamed bone(s).\n", counter ); diff --git a/utils/mdldec/qc.c b/utils/mdldec/qc.c index 6ac563d1..9508f540 100644 --- a/utils/mdldec/qc.c +++ b/utils/mdldec/qc.c @@ -249,7 +249,7 @@ WriteSkinFamilyInfo static void WriteSkinFamilyInfo( FILE *fp ) { int i, j, k; - short *skinref, index; + short *skinref, *index; mstudiotexture_t *texture; if( texture_hdr->numskinfamilies < 2 ) @@ -260,23 +260,22 @@ static void WriteSkinFamilyInfo( FILE *fp ) fputs( "$texturegroup \"skinfamilies\"\n{\n", fp ); skinref = (short *)( (byte *)texture_hdr + texture_hdr->skinindex ); + texture = (mstudiotexture_t *)( (byte *)texture_hdr + texture_hdr->textureindex ); for( i = 0; i < texture_hdr->numskinfamilies; ++i ) { fputs( "\t{\n", fp ); - for( j = 0; j < texture_hdr->numskinref; ++j ) - { - index = *( skinref + i * texture_hdr->numskinref + j ); + index = skinref + i * texture_hdr->numskinref; + for( j = 0; j < texture_hdr->numskinref; ++j, ++index ) + { for( k = 0; k < texture_hdr->numskinfamilies; ++k ) { - if( index == *( skinref + k * texture_hdr->numskinref + j ) ) + if( *index == *( skinref + k * texture_hdr->numskinref + j ) ) continue; - texture = (mstudiotexture_t *)( (byte *)texture_hdr + texture_hdr->textureindex ) + index; - - fprintf( fp, "\t\t\"%s\"\n", texture->name ); + fprintf( fp, "\t\t\"%s\"\n", texture[*index].name ); break; } } @@ -301,15 +300,13 @@ static void WriteAttachmentInfo( FILE *fp ) if( !model_hdr->numattachments ) return; + attachment = (mstudioattachment_t *)( (byte *)model_hdr + model_hdr->attachmentindex ); + bone = (mstudiobone_t *)( (byte *)model_hdr + model_hdr->boneindex ); + fprintf( fp, "// %i attachment%s\n", model_hdr->numattachments, model_hdr->numattachments > 1 ? "s" : "" ); - for( i = 0; i < model_hdr->numattachments; ++i ) - { - attachment = (mstudioattachment_t *)( (byte *)model_hdr + model_hdr->attachmentindex ) + i; - bone = (mstudiobone_t *)( (byte *)model_hdr + model_hdr->boneindex ) + attachment->bone; - - fprintf( fp, "$attachment %i \"%s\" %f %f %f\n", i, bone->name, attachment->org[0], attachment->org[1], attachment->org[2] ); - } + for( i = 0; i < model_hdr->numattachments; ++i, ++attachment ) + fprintf( fp, "$attachment %i \"%s\" %f %f %f\n", i, bone[attachment->bone].name, attachment->org[0], attachment->org[1], attachment->org[2] ); fputs( "\n", fp ); } @@ -322,20 +319,18 @@ WriteBodyGroupInfo static void WriteBodyGroupInfo( FILE *fp ) { int i, j; - mstudiobodyparts_t *bodypart; + mstudiobodyparts_t *bodypart = (mstudiobodyparts_t *) ( (byte *)model_hdr + model_hdr->bodypartindex ); mstudiomodel_t *model; char modelname[64]; fprintf( fp, "// %i reference mesh%s\n", model_hdr->numbodyparts, model_hdr->numbodyparts > 1 ? "es" : "" ); - for( i = 0; i < model_hdr->numbodyparts; ++i ) + for( i = 0; i < model_hdr->numbodyparts; ++i, ++bodypart ) { - bodypart = (mstudiobodyparts_t *) ( (byte *)model_hdr + model_hdr->bodypartindex ) + i; + model = (mstudiomodel_t *)( (byte *)model_hdr + bodypart->modelindex ); if( bodypart->nummodels == 1 ) { - model = (mstudiomodel_t *)( (byte *)model_hdr + bodypart->modelindex ); - COM_FileBase( model->name, modelname, sizeof( modelname )); fprintf( fp, "$body \"%s\" \"%s\"\n", bodypart->name, modelname ); @@ -346,10 +341,8 @@ static void WriteBodyGroupInfo( FILE *fp ) fputs( "{\n", fp ); - for( j = 0; j < bodypart->nummodels; ++j ) + for( j = 0; j < bodypart->nummodels; ++j, ++model ) { - model = (mstudiomodel_t *)( (byte *)model_hdr + bodypart->modelindex ) + j; - if( !Q_strncmp( model->name, "blank", 5 ) ) { fputs( "\tblank\n", fp ); @@ -382,13 +375,13 @@ static void WriteControllerInfo( FILE *fp ) if( !model_hdr->numbonecontrollers ) return; + bonecontroller = (mstudiobonecontroller_t *)( (byte *)model_hdr + model_hdr->bonecontrollerindex ); + bone = (mstudiobone_t *)( (byte *)model_hdr + model_hdr->boneindex ); + fprintf( fp, "// %i bone controller%s\n", model_hdr->numbonecontrollers, model_hdr->numbonecontrollers > 1 ? "s" : "" ); - for( i = 0; i < model_hdr->numbonecontrollers; ++i ) + for( i = 0; i < model_hdr->numbonecontrollers; ++i, ++bonecontroller ) { - bonecontroller = (mstudiobonecontroller_t *)( (byte *)model_hdr + model_hdr->bonecontrollerindex ) + i; - bone = (mstudiobone_t *)( (byte *)model_hdr + model_hdr->boneindex ) + bonecontroller->bone; - GetMotionTypeString( bonecontroller->type & ~STUDIO_RLOOP, motion_types, sizeof( motion_types ), false ); fputs( "$controller ", fp ); @@ -399,7 +392,7 @@ static void WriteControllerInfo( FILE *fp ) fprintf( fp, "%i", bonecontroller->index ); fprintf( fp, " \"%s\" %s %f %f\n", - bone->name, motion_types, + bone[bonecontroller->bone].name, motion_types, bonecontroller->start, bonecontroller->end ); } @@ -420,18 +413,16 @@ static void WriteHitBoxInfo( FILE *fp ) if( !model_hdr->numhitboxes ) return; - fprintf( fp, "// %i hit box%s\n", model_hdr->numhitboxes, model_hdr->numhitboxes > 1 ? "es" : "" ); + hitbox = (mstudiobbox_t *)( (byte *)model_hdr + model_hdr->hitboxindex ); + bone = (mstudiobone_t *)( (byte *)model_hdr + model_hdr->boneindex ); - for( i = 0; i < model_hdr->numhitboxes; i++ ) - { - hitbox = (mstudiobbox_t *)( (byte *)model_hdr + model_hdr->hitboxindex ) + i; - bone = (mstudiobone_t *)( (byte *)model_hdr + model_hdr->boneindex ) + hitbox->bone; + fprintf( fp, "// %i hit box%s\n", model_hdr->numhitboxes, model_hdr->numhitboxes > 1 ? "es" : "" ); + for( i = 0; i < model_hdr->numhitboxes; ++i, ++hitbox ) fprintf( fp, "$hbox %i \"%s\" %f %f %f %f %f %f\n", - hitbox->group, bone->name, + hitbox->group, bone[hitbox->bone].name, hitbox->bbmin[0], hitbox->bbmin[1], hitbox->bbmin[2], hitbox->bbmax[0], hitbox->bbmax[1], hitbox->bbmax[2] ); - } fputs( "\n", fp ); } @@ -477,19 +468,18 @@ static void WriteSequenceInfo( FILE *fp ) if( model_hdr->numseq > 0 ) fprintf( fp, "// %i animation sequence%s\n", model_hdr->numseq, model_hdr->numseq > 1 ? "s" : "" ); + else return; - for( i = 0; i < model_hdr->numseq; ++i ) - { - seqdesc = (mstudioseqdesc_t *)( (byte *)model_hdr + model_hdr->seqindex ) + i; + seqdesc = (mstudioseqdesc_t *)( (byte *)model_hdr + model_hdr->seqindex ); + for( i = 0; i < model_hdr->numseq; ++i, ++seqdesc ) + { fprintf( fp, "$sequence \"%s\" {\n", seqdesc->label ); if( seqdesc->numblends > 1 ) { for( j = 0; j < seqdesc->numblends; j++ ) - { fprintf( fp, "\t\"%s_blend%02i\"\n", seqdesc->label, j + 1 ); - } } else { @@ -524,10 +514,10 @@ static void WriteSequenceInfo( FILE *fp ) printf( "WARNING: Something wrong with blending type for sequence: %s\n", seqdesc->label ); } - for( j = 0; j < seqdesc->numevents; j++ ) - { - event = (mstudioevent_t *)( (byte *)model_hdr + seqdesc->eventindex ) + j; + event = (mstudioevent_t *)( (byte *)model_hdr + seqdesc->eventindex ); + for( j = 0; j < seqdesc->numevents; ++j, ++event ) + { fprintf( fp, "\t{ event %i %i", event->event, event->frame ); if( event->options[0] != '\0' ) diff --git a/utils/mdldec/smd.c b/utils/mdldec/smd.c index d1ad5875..7c7ffa0c 100644 --- a/utils/mdldec/smd.c +++ b/utils/mdldec/smd.c @@ -53,14 +53,12 @@ FillBoneTransformMatrices static void FillBoneTransformMatrices( void ) { int i; - mstudiobone_t *bone; + mstudiobone_t *bone = (mstudiobone_t *)( (byte *)model_hdr + model_hdr->boneindex ); matrix3x4 bonematrix; vec4_t q; - for( i = 0; i < model_hdr->numbones; i++ ) + for( i = 0; i < model_hdr->numbones; ++i, ++bone ) { - bone = (mstudiobone_t *)( (byte *)model_hdr + model_hdr->boneindex ) + i; - AngleQuaternion( &bone->value[3], q, true ); Matrix3x4_FromOriginQuat( bonematrix, q, bone->value ); @@ -84,7 +82,7 @@ static void FillWorldTransformMatrices( void ) int i; mstudioboneinfo_t *boneinfo = (mstudioboneinfo_t *)( (byte *)model_hdr + model_hdr->boneindex + model_hdr->numbones * sizeof( mstudiobone_t ) ); - for( i = 0; i < model_hdr->numbones; i++, boneinfo++ ) + for( i = 0; i < model_hdr->numbones; ++i, ++boneinfo ) Matrix3x4_ConcatTransforms( worldtransform[i], bonetransform[i], boneinfo->poseToBone ); } @@ -186,16 +184,12 @@ WriteNodes static void WriteNodes( FILE *fp ) { int i; - mstudiobone_t *bone; + mstudiobone_t *bone = (mstudiobone_t *)( (byte *)model_hdr + model_hdr->boneindex ); fputs( "nodes\n", fp ); - for( i = 0; i < model_hdr->numbones; i++ ) - { - bone = (mstudiobone_t *)( (byte *)model_hdr + model_hdr->boneindex ) + i; - + for( i = 0; i < model_hdr->numbones; ++i, ++bone ) fprintf( fp, "%3i \"%s\" %i\n", i, bone->name, bone->parent ); - } fputs( "end\n", fp ); } @@ -208,15 +202,13 @@ WriteSkeleton static void WriteSkeleton( FILE *fp ) { int i, j; - mstudiobone_t *bone; + mstudiobone_t *bone = (mstudiobone_t *)( (byte *)model_hdr + model_hdr->boneindex ); fputs( "skeleton\n", fp ); fputs( "time 0\n", fp ); - for( i = 0; i < model_hdr->numbones; i++ ) + for( i = 0; i < model_hdr->numbones; ++i, ++bone ) { - bone = (mstudiobone_t *)( (byte *)model_hdr + model_hdr->boneindex ) + i; - fprintf( fp, "%3i", i ); for( j = 0; j < 6; j++ ) @@ -235,7 +227,7 @@ WriteTriangleInfo */ static void WriteTriangleInfo( FILE *fp, mstudiomodel_t *model, mstudiotexture_t *texture, mstudiotrivert_t **triverts, qboolean isevenstrip ) { - int i, j, k, l, indices[3]; + int i, j, k, l, index; int vert_index; int norm_index; int bone_index; @@ -249,19 +241,6 @@ static void WriteTriangleInfo( FILE *fp, mstudiomodel_t *model, mstudiotexture_t matrix3x4 bonematrix[MAXSTUDIOBONEWEIGHTS], skinmatrix, *pskinmatrix; mstudioboneweight_t *studioboneweights; - if( isevenstrip ) - { - indices[0] = 1; - indices[1] = 2; - indices[2] = 0; - } - else - { - indices[0] = 0; - indices[1] = 1; - indices[2] = 2; - } - vertbone = ( (byte *)model_hdr + model->vertinfoindex ); studioverts = (vec3_t *)( (byte *)model_hdr + model->vertindex ); studionorms = (vec3_t *)( (byte *)model_hdr + model->normindex ); @@ -274,14 +253,15 @@ static void WriteTriangleInfo( FILE *fp, mstudiomodel_t *model, mstudiotexture_t for( i = 0; i < 3; i++ ) { - vert_index = triverts[indices[i]]->vertindex; - norm_index = triverts[indices[i]]->normindex; + index = isevenstrip ? ( i + 1 ) % 3 : i; + vert_index = triverts[index]->vertindex; + norm_index = triverts[index]->normindex; bone_index = vertbone[vert_index]; if( model_hdr->flags & STUDIO_HAS_BONEWEIGHTS ) { valid_bones = 0, totalweight = 0; - memset(skinmatrix, 0, sizeof(matrix3x4)); + memset( skinmatrix, 0, sizeof( matrix3x4 ) ); for( j = 0; j < MAXSTUDIOBONEWEIGHTS; ++j ) if( studioboneweights[vert_index].bone[j] != -1 ) @@ -315,13 +295,13 @@ static void WriteTriangleInfo( FILE *fp, mstudiomodel_t *model, mstudiotexture_t if( texture->flags & STUDIO_NF_UV_COORDS ) { - u = HalfToFloat( triverts[indices[i]]->s ); - v = -HalfToFloat( triverts[indices[i]]->t ); + u = HalfToFloat( triverts[index]->s ); + v = -HalfToFloat( triverts[index]->t ); } else { - u = ( triverts[indices[i]]->s + 1.0f ) * s; - v = 1.0f - triverts[indices[i]]->t * t; + u = ( triverts[index]->s + 1.0f ) * s; + v = 1.0f - triverts[index]->t * t; } fprintf( fp, "%3i %f %f %f %f %f %f %f %f", @@ -354,16 +334,15 @@ WriteTriangles static void WriteTriangles( FILE *fp, mstudiomodel_t *model ) { int i, j, k; - mstudiomesh_t *mesh; + mstudiomesh_t *mesh = (mstudiomesh_t *)( (byte *)model_hdr + model->meshindex ); mstudiotexture_t *texture; mstudiotrivert_t *triverts[3]; short *tricmds; fputs( "triangles\n", fp ); - for( i = 0; i < model->nummesh; i++ ) + for( i = 0; i < model->nummesh; ++i, ++mesh ) { - mesh = (mstudiomesh_t *)( (byte *)model_hdr + model->meshindex ) + i; tricmds = (short *)( (byte *)model_hdr + mesh->triindex ); texture = (mstudiotexture_t *)( (byte *)texture_hdr + texture_hdr->textureindex ) + mesh->skinref; @@ -450,14 +429,12 @@ static void WriteFrameInfo( FILE *fp, mstudioanim_t *anim, mstudioseqdesc_t *seq int i, j; float scale; vec_t motion[6]; // x, y, z, xr, yr, zr - mstudiobone_t *bone; + mstudiobone_t *bone = (mstudiobone_t *)( (byte *)model_hdr + model_hdr->boneindex ); fprintf( fp, "time %i\n", frame ); - for( i = 0; i < model_hdr->numbones; i++, anim++ ) + for( i = 0; i < model_hdr->numbones; ++i, ++anim, ++bone ) { - bone = (mstudiobone_t *)( (byte *)model_hdr + model_hdr->boneindex ) + i; - CalcBonePosition( anim, bone, motion, frame ); if( bone->parent == -1 ) @@ -529,14 +506,14 @@ static void WriteReferences( void ) FillWorldTransformMatrices(); } - for( i = 0; i < model_hdr->numbodyparts; i++ ) + bodypart = (mstudiobodyparts_t *)( (byte *)model_hdr + model_hdr->bodypartindex ); + + for( i = 0; i < model_hdr->numbodyparts; ++i, ++bodypart ) { - bodypart = (mstudiobodyparts_t *)( (byte *)model_hdr + model_hdr->bodypartindex ) + i; + model = (mstudiomodel_t *)( (byte *)model_hdr + bodypart->modelindex ); - for( j = 0; j < bodypart->nummodels; j++ ) + for( j = 0; j < bodypart->nummodels; ++j, ++model ) { - model = (mstudiomodel_t *)( (byte *)model_hdr + bodypart->modelindex ) + j; - if( !Q_strncmp( model->name, "blank", 5 ) ) continue; @@ -588,12 +565,10 @@ static void WriteSequences( void ) int len; FILE *fp; char filename[MAX_SYSPATH]; - mstudioseqdesc_t *seqdesc; + mstudioseqdesc_t *seqdesc = (mstudioseqdesc_t *)( (byte *)model_hdr + model_hdr->seqindex ); - for( i = 0; i < model_hdr->numseq; i++ ) + for( i = 0; i < model_hdr->numseq; ++i, ++seqdesc ) { - seqdesc = (mstudioseqdesc_t *)( (byte *)model_hdr + model_hdr->seqindex ) + i; - for( j = 0; j < seqdesc->numblends; j++ ) { if( seqdesc->numblends == 1 ) diff --git a/utils/mdldec/texture.c b/utils/mdldec/texture.c index 5643393e..a281b44c 100644 --- a/utils/mdldec/texture.c +++ b/utils/mdldec/texture.c @@ -33,7 +33,7 @@ static void WriteBMP( mstudiotexture_t *texture ) int i, len; FILE *fp; const byte *p; - byte *palette, *pic, *buf; + byte *palette, *pic; char filename[MAX_SYSPATH], texturename[64]; rgba_t rgba_palette[256]; bmp_t bmp_hdr = {0,}; @@ -87,23 +87,17 @@ static void WriteBMP( mstudiotexture_t *texture ) fwrite( rgba_palette, sizeof( rgba_palette ), 1, fp ); - buf = malloc( texture_size ); - p = pic; p += ( bmp_hdr.height - 1 ) * bmp_hdr.width; for( i = 0; i < bmp_hdr.height; i++ ) { - memcpy( buf + bmp_hdr.width * i, p, bmp_hdr.width ); + fwrite( p, bmp_hdr.width, 1, fp ); p -= bmp_hdr.width; } - fwrite( buf, texture_size, 1, fp ); - fclose( fp ); - free( buf ); - printf( "Texture: %s\n", filename ); } @@ -115,13 +109,9 @@ WriteTextures void WriteTextures( void ) { int i; - mstudiotexture_t *texture; - - for( i = 0; i < texture_hdr->numtextures; i++ ) - { - texture = (mstudiotexture_t *)( (byte *)texture_hdr + texture_hdr->textureindex ) + i; + mstudiotexture_t *texture = (mstudiotexture_t *)( (byte *)texture_hdr + texture_hdr->textureindex ); + for( i = 0; i < texture_hdr->numtextures; ++i, ++texture ) WriteBMP( texture ); - } }