2
0
mirror of https://github.com/FWGS/xash3d-fwgs synced 2024-11-22 18:07:09 +01:00
xash3d-fwgs/utils/mdldec/smd.c

616 lines
13 KiB
C
Raw Permalink Normal View History

2020-03-04 05:08:14 +01:00
/*
smd.c - Studio Model Data format writer
Copyright (C) 2020 Andrey Akhmichin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "const.h"
#include "com_model.h"
#include "xash3d_mathlib.h"
#include "crtlib.h"
#include "studio.h"
#include "mdldec.h"
#include "smd.h"
#include "utils.h"
2020-03-04 05:08:14 +01:00
static matrix3x4 *bonetransform;
static matrix3x4 *worldtransform;
2020-03-04 05:08:14 +01:00
/*
============
CreateBoneTransformMatrices
2020-03-04 05:08:14 +01:00
============
*/
static qboolean CreateBoneTransformMatrices( matrix3x4 **matrix )
2020-03-04 05:08:14 +01:00
{
*matrix = calloc( model_hdr->numbones, sizeof( matrix3x4 ) );
2020-03-04 05:08:14 +01:00
if( !*matrix )
2020-03-04 05:08:14 +01:00
{
fputs( "ERROR: Couldn't allocate memory for bone transformation matrices!\n", stderr );
return false;
}
return true;
}
/*
============
FillBoneTransformMatrices
2020-03-04 05:08:14 +01:00
============
*/
static void FillBoneTransformMatrices( void )
{
int i;
2023-10-19 08:20:36 +02:00
mstudiobone_t *bone = (mstudiobone_t *)( (byte *)model_hdr + model_hdr->boneindex );
2020-03-04 05:08:14 +01:00
matrix3x4 bonematrix;
vec4_t q;
2023-10-19 08:20:36 +02:00
for( i = 0; i < model_hdr->numbones; ++i, ++bone )
2020-03-04 05:08:14 +01:00
{
AngleQuaternion( &bone->value[3], q, true );
Matrix3x4_FromOriginQuat( bonematrix, q, bone->value );
if( bone->parent == -1 )
{
Matrix3x4_Copy( bonetransform[i], bonematrix );
continue;
}
Matrix3x4_ConcatTransforms( bonetransform[i], bonetransform[bone->parent], bonematrix );
}
}
/*
============
FillWorldTransformMatrices
============
*/
static void FillWorldTransformMatrices( void )
{
int i;
mstudioboneinfo_t *boneinfo = (mstudioboneinfo_t *)( (byte *)model_hdr + model_hdr->boneindex + model_hdr->numbones * sizeof( mstudiobone_t ) );
2023-10-19 08:20:36 +02:00
for( i = 0; i < model_hdr->numbones; ++i, ++boneinfo )
Matrix3x4_ConcatTransforms( worldtransform[i], bonetransform[i], boneinfo->poseToBone );
}
2020-03-04 05:08:14 +01:00
/*
============
RemoveBoneTransformMatrices
============
*/
static void RemoveBoneTransformMatrices( matrix3x4 **matrix )
2020-03-04 05:08:14 +01:00
{
free( *matrix );
2020-03-04 05:08:14 +01:00
}
/*
============
ClipRotations
============
*/
static void ClipRotations( vec3_t angle )
{
int i;
for( i = 0; i < 3; i++ )
{
while( angle[i] >= M_PI_F )
angle[i] -= M_PI2_F;
while( angle[i] < -M_PI_F )
angle[i] += M_PI2_F;
}
}
/*
============
ProperBoneRotationZ
============
*/
2020-11-28 01:34:15 +01:00
static void ProperBoneRotationZ( vec_t *motion, float angle )
2020-03-04 05:08:14 +01:00
{
float tmp, rot;
2020-03-04 05:08:14 +01:00
rot = DEG2RAD( angle );
tmp = motion[0];
motion[0] = motion[1];
motion[1] = -tmp;
2020-03-04 05:08:14 +01:00
motion[5] += rot;
}
/*
============
CalcBonePosition
============
*/
static void CalcBonePosition( mstudioanim_t *anim, mstudiobone_t *bone, vec_t *motion, int frame )
{
int i, j;
float value;
mstudioanimvalue_t *animvalue;
for( i = 0; i < 6; i++ )
{
motion[i] = bone->value[i];
if( !anim->offset[i] )
continue;
animvalue = (mstudioanimvalue_t *)( (byte *)anim + anim->offset[i] );
j = frame;
while( animvalue->num.total <= j )
{
j -= animvalue->num.total;
animvalue += animvalue->num.valid + 1;
}
if( animvalue->num.valid > j )
value = animvalue[j + 1].value;
else
value = animvalue[animvalue->num.valid].value;
motion[i] += value * bone->scale[i];
}
}
/*
============
WriteNodes
============
*/
static void WriteNodes( FILE *fp )
{
int i;
2023-10-19 08:20:36 +02:00
mstudiobone_t *bone = (mstudiobone_t *)( (byte *)model_hdr + model_hdr->boneindex );
2020-03-04 05:08:14 +01:00
fputs( "nodes\n", fp );
2023-10-19 08:20:36 +02:00
for( i = 0; i < model_hdr->numbones; ++i, ++bone )
2020-03-04 05:08:14 +01:00
fprintf( fp, "%3i \"%s\" %i\n", i, bone->name, bone->parent );
fputs( "end\n", fp );
}
/*
============
WriteSkeleton
============
*/
static void WriteSkeleton( FILE *fp )
{
int i, j;
2023-10-19 08:20:36 +02:00
mstudiobone_t *bone = (mstudiobone_t *)( (byte *)model_hdr + model_hdr->boneindex );
2020-03-04 05:08:14 +01:00
fputs( "skeleton\n", fp );
fputs( "time 0\n", fp );
2023-10-19 08:20:36 +02:00
for( i = 0; i < model_hdr->numbones; ++i, ++bone )
2020-03-04 05:08:14 +01:00
{
fprintf( fp, "%3i", i );
for( j = 0; j < 6; j++ )
fprintf( fp, " %f", bone->value[j] );
fputs( "\n", fp );
}
fputs( "end\n", fp );
}
/*
============
WriteTriangleInfo
============
*/
static void WriteTriangleInfo( FILE *fp, mstudiomodel_t *model, mstudiotexture_t *texture, mstudiotrivert_t **triverts, qboolean isevenstrip )
{
2023-10-19 08:20:36 +02:00
int i, j, k, l, index;
int vert_index;
int norm_index;
int bone_index;
int valid_bones;
float s, t, u, v;
byte *vertbone;
vec3_t *studioverts;
vec3_t *studionorms;
vec3_t vert, norm;
float weights[MAXSTUDIOBONEWEIGHTS], oldweight, totalweight;
matrix3x4 bonematrix[MAXSTUDIOBONEWEIGHTS], skinmatrix, *pskinmatrix;
mstudioboneweight_t *studioboneweights;
2020-03-04 05:08:14 +01:00
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 );
2020-03-04 05:08:14 +01:00
s = 1.0f / texture->width;
t = 1.0f / texture->height;
fprintf( fp, "%s\n", texture->name );
for( i = 0; i < 3; i++ )
{
2023-10-19 08:20:36 +02:00
index = isevenstrip ? ( i + 1 ) % 3 : i;
vert_index = triverts[index]->vertindex;
norm_index = triverts[index]->normindex;
2020-03-04 05:08:14 +01:00
bone_index = vertbone[vert_index];
if( model_hdr->flags & STUDIO_HAS_BONEWEIGHTS )
{
valid_bones = 0, totalweight = 0;
2023-10-19 08:20:36 +02:00
memset( skinmatrix, 0, sizeof( matrix3x4 ) );
for( j = 0; j < MAXSTUDIOBONEWEIGHTS; ++j )
if( studioboneweights[vert_index].bone[j] != -1 )
valid_bones++;
for( j = 0; j < valid_bones; ++j )
{
Matrix3x4_Copy( bonematrix[j], worldtransform[studioboneweights[vert_index].bone[j]] );
weights[j] = studioboneweights[vert_index].weight[j] / 255.0f;
totalweight += weights[j];
}
oldweight = weights[0];
if( totalweight < 1.0f )
weights[0] += 1.0f - totalweight;
for( j = 0; j < valid_bones; ++j )
for( k = 0; k < 3; ++k )
for( l = 0; l < 4; ++l )
skinmatrix[k][l] += bonematrix[j][k][l] * weights[j];
pskinmatrix = &skinmatrix;
}
else
pskinmatrix = &bonetransform[bone_index];
Matrix3x4_VectorTransform( *pskinmatrix, studioverts[vert_index], vert );
Matrix3x4_VectorRotate( *pskinmatrix, studionorms[norm_index], norm );
VectorNormalize( norm );
2020-03-04 05:08:14 +01:00
if( texture->flags & STUDIO_NF_UV_COORDS )
{
2023-10-19 08:20:36 +02:00
u = HalfToFloat( triverts[index]->s );
v = -HalfToFloat( triverts[index]->t );
}
else
{
2023-10-19 08:20:36 +02:00
u = ( triverts[index]->s + 1.0f ) * s;
v = 1.0f - triverts[index]->t * t;
}
2020-03-04 05:08:14 +01:00
fprintf( fp, "%3i %f %f %f %f %f %f %f %f",
2020-03-04 05:08:14 +01:00
bone_index,
vert[0], vert[1], vert[2],
norm[0], norm[1], norm[2],
u, v );
if( model_hdr->flags & STUDIO_HAS_BONEWEIGHTS )
{
fprintf( fp, " %d", valid_bones );
weights[0] = oldweight;
for( j = 0; j < valid_bones; ++j )
fprintf( fp, " %d %f",
studioboneweights[vert_index].bone[j],
weights[j] );
}
fputs( "\n", fp );
2020-03-04 05:08:14 +01:00
}
}
/*
============
WriteTriangles
============
*/
static void WriteTriangles( FILE *fp, mstudiomodel_t *model )
{
int i, j, k;
2023-10-19 08:20:36 +02:00
mstudiomesh_t *mesh = (mstudiomesh_t *)( (byte *)model_hdr + model->meshindex );
2020-03-04 05:08:14 +01:00
mstudiotexture_t *texture;
mstudiotrivert_t *triverts[3];
short *tricmds;
fputs( "triangles\n", fp );
2023-10-19 08:20:36 +02:00
for( i = 0; i < model->nummesh; ++i, ++mesh )
2020-03-04 05:08:14 +01:00
{
tricmds = (short *)( (byte *)model_hdr + mesh->triindex );
texture = (mstudiotexture_t *)( (byte *)texture_hdr + texture_hdr->textureindex ) + mesh->skinref;
while( ( j = *( tricmds++ ) ) )
{
if( j >= 0 )
{
2020-11-28 01:34:15 +01:00
// triangle strip
2020-03-04 05:08:14 +01:00
for( k = 0; j > 0; j--, k++, tricmds += 4 )
{
if( k == 0 )
{
triverts[0] = (mstudiotrivert_t *)tricmds;
}
else if( k == 1 )
{
triverts[2] = (mstudiotrivert_t *)tricmds;
}
else if( k == 2 )
{
triverts[1] = (mstudiotrivert_t *)tricmds;
WriteTriangleInfo( fp, model, texture, triverts, true );
}
else if( k % 2 )
{
triverts[0] = triverts[2];
triverts[2] = (mstudiotrivert_t *)tricmds;
WriteTriangleInfo( fp, model, texture, triverts, false );
}
else
{
triverts[0] = triverts[1];
triverts[1] = (mstudiotrivert_t *)tricmds;
WriteTriangleInfo( fp, model, texture, triverts, true );
}
}
}
else
{
2020-11-28 01:34:15 +01:00
// triangle fan
2020-03-04 05:08:14 +01:00
j = abs( j );
for( k = 0; j > 0; j--, k++, tricmds += 4 )
{
if( k == 0 )
{
triverts[0] = (mstudiotrivert_t *)tricmds;
}
else if( k == 1 )
{
triverts[2] = (mstudiotrivert_t *)tricmds;
}
else if( k == 2 )
{
triverts[1] = (mstudiotrivert_t *)tricmds;
WriteTriangleInfo( fp, model, texture, triverts, false );
}
else
{
triverts[2] = triverts[1];
triverts[1] = (mstudiotrivert_t *)tricmds;
WriteTriangleInfo( fp, model, texture, triverts, false );
}
}
}
}
}
fputs( "end\n", fp );
}
/*
============
WriteFrameInfo
============
*/
static void WriteFrameInfo( FILE *fp, mstudioanim_t *anim, mstudioseqdesc_t *seqdesc, int frame )
{
int i, j;
float scale;
2020-03-04 05:08:14 +01:00
vec_t motion[6]; // x, y, z, xr, yr, zr
2023-10-19 08:20:36 +02:00
mstudiobone_t *bone = (mstudiobone_t *)( (byte *)model_hdr + model_hdr->boneindex );
2020-03-04 05:08:14 +01:00
fprintf( fp, "time %i\n", frame );
2023-10-19 08:20:36 +02:00
for( i = 0; i < model_hdr->numbones; ++i, ++anim, ++bone )
2020-03-04 05:08:14 +01:00
{
CalcBonePosition( anim, bone, motion, frame );
if( bone->parent == -1 )
2020-11-28 01:34:15 +01:00
{
if( seqdesc->numframes > 1 && frame > 0 )
{
scale = frame / (float)( seqdesc->numframes - 1 );
VectorMA( motion, scale, seqdesc->linearmovement, motion );
}
2020-11-28 01:34:15 +01:00
ProperBoneRotationZ( motion, 270.0f );
}
2020-03-04 05:08:14 +01:00
ClipRotations( &motion[3] );
fprintf( fp, "%3i ", i );
for( j = 0; j < 6; j++ )
fprintf( fp, " %f", motion[j] );
fputs( "\n", fp );
}
}
/*
============
WriteAnimations
============
*/
static void WriteAnimations( FILE *fp, mstudioseqdesc_t *seqdesc, int blend )
{
int i;
mstudioanim_t *anim;
fputs( "skeleton\n", fp );
anim = (mstudioanim_t *)( (byte *)anim_hdr[seqdesc->seqgroup] + seqdesc->animindex );
anim += blend * model_hdr->numbones;
for( i = 0; i < seqdesc->numframes; i++ )
WriteFrameInfo( fp, anim, seqdesc, i );
fputs( "end\n", fp );
}
/*
============
WriteReferences
============
*/
static void WriteReferences( void )
{
int i, j;
2020-11-28 01:34:15 +01:00
int len;
2020-03-04 05:08:14 +01:00
FILE *fp;
mstudiomodel_t *model;
mstudiobodyparts_t *bodypart;
char filename[MAX_SYSPATH];
if( !CreateBoneTransformMatrices( &bonetransform ) )
2020-03-04 05:08:14 +01:00
return;
FillBoneTransformMatrices();
if( model_hdr->flags & STUDIO_HAS_BONEINFO )
{
if( !CreateBoneTransformMatrices( &worldtransform ) )
return;
FillWorldTransformMatrices();
}
2023-10-19 08:20:36 +02:00
bodypart = (mstudiobodyparts_t *)( (byte *)model_hdr + model_hdr->bodypartindex );
for( i = 0; i < model_hdr->numbodyparts; ++i, ++bodypart )
2020-03-04 05:08:14 +01:00
{
2023-10-19 08:20:36 +02:00
model = (mstudiomodel_t *)( (byte *)model_hdr + bodypart->modelindex );
2020-03-04 05:08:14 +01:00
2023-10-19 08:20:36 +02:00
for( j = 0; j < bodypart->nummodels; ++j, ++model )
2020-03-04 05:08:14 +01:00
{
if( !Q_strncmp( model->name, "blank", 5 ) )
continue;
len = Q_snprintf( filename, MAX_SYSPATH, "%s%s.smd", destdir, model->name );
2020-03-04 05:08:14 +01:00
2020-11-28 01:34:15 +01:00
if( len == -1 )
2020-03-04 05:08:14 +01:00
{
fprintf( stderr, "ERROR: Destination path is too long. Couldn't write %s.smd\n", model->name );
goto _fail;
2020-03-04 05:08:14 +01:00
}
fp = fopen( filename, "w" );
if( !fp )
{
fprintf( stderr, "ERROR: Couldn't write %s\n", filename );
goto _fail;
2020-03-04 05:08:14 +01:00
}
fputs( "version 1\n", fp );
WriteNodes( fp );
WriteSkeleton( fp );
WriteTriangles( fp, model );
fclose( fp );
printf( "Reference: %s\n", filename );
}
}
_fail:
RemoveBoneTransformMatrices( &bonetransform );
if( model_hdr->flags & STUDIO_HAS_BONEINFO )
RemoveBoneTransformMatrices( &worldtransform );
2020-03-04 05:08:14 +01:00
}
/*
============
WriteSequences
============
*/
static void WriteSequences( void )
{
int i, j;
int len, namelen, emptyplace;
2020-03-04 05:08:14 +01:00
FILE *fp;
char path[MAX_SYSPATH];
2023-10-19 08:20:36 +02:00
mstudioseqdesc_t *seqdesc = (mstudioseqdesc_t *)( (byte *)model_hdr + model_hdr->seqindex );
2020-03-04 05:08:14 +01:00
len = Q_snprintf( path, MAX_SYSPATH, "%s" DEFAULT_SEQUENCEPATH, destdir );
if( len == -1 || !MakeDirectory( path ))
{
fputs( "ERROR: Destination path is too long or write permission denied. Couldn't create directory for sequences\n", stderr );
return;
}
emptyplace = MAX_SYSPATH - len;
2023-10-19 08:20:36 +02:00
for( i = 0; i < model_hdr->numseq; ++i, ++seqdesc )
2020-03-04 05:08:14 +01:00
{
for( j = 0; j < seqdesc->numblends; j++ )
{
if( seqdesc->numblends == 1 )
namelen = Q_snprintf( &path[len], emptyplace, "%s.smd", seqdesc->label );
2020-03-04 05:08:14 +01:00
else
namelen = Q_snprintf( &path[len], emptyplace, "%s_blend%02i.smd", seqdesc->label, j + 1 );
2020-03-04 05:08:14 +01:00
if( namelen == -1 )
2020-03-04 05:08:14 +01:00
{
fprintf( stderr, "ERROR: Destination path is too long. Couldn't write %s.smd\n", seqdesc->label );
2020-03-04 05:08:14 +01:00
return;
}
fp = fopen( path, "w" );
2020-03-04 05:08:14 +01:00
if( !fp )
{
fprintf( stderr, "ERROR: Couldn't write %s\n", path );
2020-03-04 05:08:14 +01:00
return;
}
fputs( "version 1\n", fp );
WriteNodes( fp );
WriteAnimations( fp, seqdesc, j );
fclose( fp );
printf( "Sequence: %s\n", path );
2020-03-04 05:08:14 +01:00
}
}
}
void WriteSMD( void )
{
WriteReferences();
WriteSequences();
}