SBSPSS/Utils/MkAnim3d/MkAnim3d.cpp

347 lines
8.5 KiB
C++
Raw Normal View History

2001-01-11 19:35:41 +01:00
/**********************************/
2001-01-11 23:17:57 +01:00
/*** SpongeBob 3d Anim Creator ***/
2001-01-11 19:35:41 +01:00
/**********************************/
#include "stdio.h"
#include <misc.hpp>
2001-01-24 23:35:11 +01:00
#include <GFName.hpp>
2001-01-11 19:35:41 +01:00
#include <conio.h>
#include <iostream.h>
#include <vector>
#include <PsxLib.h>
#include <FaceStore.h>
2001-01-11 23:17:57 +01:00
#include "MkAnim3d.h"
2001-01-11 19:35:41 +01:00
using namespace std;
2001-01-11 23:17:57 +01:00
int QuatCount=0;
2001-01-30 18:39:20 +01:00
/*
buttbounceend.gin buttbouncestart.gin deathbackwards.gin deathdry.gin deathfall.gin deathforwards.gin deathspin.gin deathtar.gin electricshock.gin electricshockend.gin electricshockstart.gin faceback.gin facefront.gin fall.gin getup.gin getuprun.gin hitground.gin hover.gin hoverend.gin hoverstart.gin idlebreathe.gin idlehoola.gin idlelook.gin idlewigglearm.gin jumpend.gin karate.gin knockback.gin knockforward.gin run.gin runjumpend.gin runjumpstart.gin runstart.gin runstop.gin soakup.gin talk01.gin talk02.gin talk03.gin talk04.gin teeterback.gin teeterfront.gin
*/
2001-01-11 19:35:41 +01:00
2001-01-30 18:39:20 +01:00
/* duff anims
deathfall.gin
runjumpend.gin
runjumpstart.gin
2001-01-24 23:35:11 +01:00
2001-01-30 18:39:20 +01:00
*/
2001-01-11 19:35:41 +01:00
//***************************************************************************
char * CycleCommands(char *String,int Num)
{
2001-01-11 23:17:57 +01:00
//char Text[256],*TextPtr;
//int Count;
2001-01-11 19:35:41 +01:00
if (String[0]=='-' || String[0]=='/')
{
GString TpStr;
switch (String[1])
{
// Switches
case 'o':
OutStr = CheckFileString(String);
break;
case 'd':
DebugOn =true;
break;
2001-01-12 23:38:19 +01:00
case 's':
TpStr= CheckFileString(String);
Scale=atof(TpStr);
break;
2001-01-24 23:35:11 +01:00
case 'i':
IncludeFile= CheckFileString(String);
break;
2001-01-11 19:35:41 +01:00
default:
GObject::Error(ERR_FATAL,"Unknown switch %s",String);
break;
}
}
else
{
GString UpperName(String);
UpperName.Upper();
MyFiles.AddFile(UpperName);
}
return(String);
}
//***************************************************************************
//***************************************************************************
//***************************************************************************
2001-01-11 23:17:57 +01:00
void CMkAnim3d::Add(GString const &Filename)
2001-01-11 19:35:41 +01:00
{
2001-01-11 23:17:57 +01:00
CScene Scene;
int ThisBoneCount;
2001-01-24 23:35:11 +01:00
GFName Name=Filename;
2001-01-11 23:17:57 +01:00
2001-01-30 18:39:20 +01:00
printf("%s\t",Name.File());
2001-01-11 23:17:57 +01:00
Scene.Load(Filename);
2001-01-30 18:39:20 +01:00
// Process Anim
sAnim ThisAnim;
ThisAnim.Name=Name.File();
ThisAnim.Name.Upper();
ThisAnim.FrameCount=ProcessSkelMove(Scene,ThisAnim,1);
ProcessSkelAnim(Scene,ThisAnim,1);
AnimList.push_back(ThisAnim);
ThisBoneCount=ThisAnim.BoneAnim.size();
printf("\t(%i Bones, %i Frames)\n",ThisBoneCount,ThisAnim.FrameCount);
2001-01-11 23:17:57 +01:00
// Check Skeleton
if (BoneCount==-1)
{
BoneCount=ThisBoneCount;
}
else
{
if (BoneCount!=ThisBoneCount)
{
GObject::Error(ERR_FATAL,"Invalid skeleton\n");
}
}
2001-01-11 19:35:41 +01:00
}
//***************************************************************************
2001-01-12 23:38:19 +01:00
int CMkAnim3d::ProcessSkelMove(CScene &Scene,sAnim &ThisAnim,int Idx)
{
CNode &ThisNode=Scene.GetNode(Idx);
vector<sGinAnim> const &NodeAnim=ThisNode.GetAnim();
int FrameCount=NodeAnim.size();
vector<s32> &Move=ThisAnim.Move;
Move.resize(FrameCount);
for (int i=0; i<FrameCount; i++)
{
// Move[i].vx=round(NodeAnim[i].Pos.x*Scale);
Move[i]=-round(NodeAnim[i].Pos.y*Scale);
// Move[i].vz=round(NodeAnim[i].Pos.z*Scale);
}
return(FrameCount);
}
2001-01-11 19:35:41 +01:00
2001-01-12 23:38:19 +01:00
//***************************************************************************
void CMkAnim3d::ProcessSkelAnim(CScene &Scene,sAnim &ThisAnim,int Idx)
2001-01-11 19:35:41 +01:00
{
2001-01-11 23:17:57 +01:00
CNode &ThisNode=Scene.GetNode(Idx);
vector<sGinAnim> const &NodeAnim=ThisNode.GetAnim();
int FrameCount=NodeAnim.size();
2001-01-11 19:35:41 +01:00
2001-01-30 18:39:20 +01:00
// if (!ThisNode.Pts.size()) // Dont export Skin as bone
if (!ThisNode.GetTris().size()) // Dont export Skin as bone
2001-01-11 23:17:57 +01:00
{
sBoneAnim FrameList;
FrameList.Idx.resize(FrameCount);
for (int i=0; i<FrameCount; i++)
{
sGinAnim const &InFrame=NodeAnim[i];
sQuat ThisFrame;
2001-01-24 23:35:11 +01:00
Quaternion const &ThisQuat=InFrame.Ang;
2001-01-12 23:38:19 +01:00
ThisFrame.vx=round(ThisQuat.x*4096);
ThisFrame.vy=round(ThisQuat.y*4096);
ThisFrame.vz=round(ThisQuat.z*4096);
ThisFrame.vw=round(ThisQuat.w*4096);
2001-01-11 23:17:57 +01:00
FrameList.Idx[i]=QuatList.Add(ThisFrame);
QuatCount++;
}
ThisAnim.BoneAnim.push_back(FrameList);
}
2001-01-11 19:35:41 +01:00
2001-01-11 23:17:57 +01:00
int ChildCount=ThisNode.GetPruneChildCount();
2001-01-11 19:35:41 +01:00
2001-01-12 23:38:19 +01:00
for (int Loop=0;Loop<ChildCount;Loop++) ProcessSkelAnim(Scene,ThisAnim,ThisNode.PruneChildList[Loop]);
2001-01-11 19:35:41 +01:00
}
//***************************************************************************
2001-01-11 23:17:57 +01:00
void CMkAnim3d::Write(GString &Filename)
2001-01-11 19:35:41 +01:00
{
2001-01-11 23:17:57 +01:00
int Anim,AnimCount=AnimList.size();
sAnim3dFileHdr FileHdr;
sAnim3dHdr Hdr;
2001-01-11 19:35:41 +01:00
2001-01-11 23:17:57 +01:00
File=fopen(Filename,"wb");
2001-01-11 19:35:41 +01:00
2001-01-11 23:17:57 +01:00
// Write Dummy FileHdr
FileHdr.AnimCount=AnimCount;
FileHdr.BoneCount=BoneCount;
fwrite(&FileHdr,1,sizeof(sAnim3dFileHdr),File);
2001-01-11 19:35:41 +01:00
2001-01-11 23:17:57 +01:00
// Write Dummy AnimHdrs
2001-01-11 19:35:41 +01:00
2001-01-11 23:17:57 +01:00
for (Anim=0; Anim<AnimCount; Anim++)
{
fwrite(&Hdr,1,sizeof(sAnim3dHdr),File);
}
2001-01-12 23:38:19 +01:00
// Write QuatTable
FileHdr.QuatTable=(sQuat*)WriteQuatTable();
// Write Movements
2001-01-11 23:17:57 +01:00
for (Anim=0; Anim<AnimCount; Anim++)
{
2001-01-12 23:38:19 +01:00
AnimList[Anim].MoveOfs=WriteMove(AnimList[Anim]);
}
// Write Anims (u16 can cause address errors, so write last
for (Anim=0; Anim<AnimCount; Anim++)
{
AnimList[Anim].AnimOfs=WriteAnim(AnimList[Anim]);
2001-01-11 23:17:57 +01:00
}
// ReWrite FileHdr
fseek(File, 0, SEEK_SET);
fwrite(&FileHdr,1,sizeof(sAnim3dFileHdr),File);
// Rewrite Dummy AnimHdrs
for (Anim=0; Anim<AnimCount; Anim++)
{
Hdr.FrameCount=AnimList[Anim].FrameCount;
2001-01-12 23:38:19 +01:00
Hdr.Move=(s32*)AnimList[Anim].MoveOfs;
Hdr.Anim=(u16*)AnimList[Anim].AnimOfs;
2001-01-11 23:17:57 +01:00
fwrite(&Hdr,1,sizeof(sAnim3dHdr),File);
}
fclose(File);
2001-01-11 19:35:41 +01:00
}
2001-01-12 23:38:19 +01:00
//***************************************************************************
int CMkAnim3d::WriteMove(sAnim const &ThisAnim)
{
int Pos=ftell(File);
for (int Frame=0; Frame<ThisAnim.FrameCount; Frame++)
{
s32 ThisMove=ThisAnim.Move[Frame];
fwrite(&ThisMove,1,sizeof(s32),File);
// printf("%i %i %i\n",ThisMove.vx,ThisMove.vy,ThisMove.vz);
}
return(Pos);
}
2001-01-11 19:35:41 +01:00
//***************************************************************************
2001-01-11 23:17:57 +01:00
int CMkAnim3d::WriteAnim(sAnim const &ThisAnim)
2001-01-11 19:35:41 +01:00
{
2001-01-11 23:17:57 +01:00
int Pos=ftell(File);
2001-01-11 19:35:41 +01:00
2001-01-11 23:17:57 +01:00
for (int Frame=0; Frame<ThisAnim.FrameCount; Frame++)
{
for (int Bone=0; Bone<BoneCount; Bone++)
{
u16 ThisIdx=ThisAnim.BoneAnim[Bone].Idx[Frame];
fwrite(&ThisIdx,1,sizeof(u16),File);
}
}
return(Pos);
}
2001-01-11 19:35:41 +01:00
//***************************************************************************
2001-01-11 23:17:57 +01:00
int CMkAnim3d::WriteQuatTable()
2001-01-11 19:35:41 +01:00
{
int Pos=ftell(File);
2001-01-11 23:17:57 +01:00
int ListSize=QuatList.size();
2001-01-11 19:35:41 +01:00
for (int i=0; i<ListSize; i++)
{
2001-01-11 23:17:57 +01:00
sQuat &ThisQuat=QuatList[i];
fwrite(&ThisQuat,1,sizeof(sQuat),File);
2001-01-11 19:35:41 +01:00
}
2001-01-11 23:17:57 +01:00
//------------
printf("%i Anims\n",AnimList.size());
printf("QuatCount=%i\t=%i\n",QuatCount,QuatCount*sizeof(sQuat));
printf("ListSize=%i\t=%i\n",ListSize,(ListSize*sizeof(sQuat))+(QuatCount*sizeof(u16)));
return(Pos);
2001-01-11 19:35:41 +01:00
}
2001-01-24 23:35:11 +01:00
//***************************************************************************
void CMkAnim3d::WriteInclude(GString const &Filename)
{
GString Upper=Filename;
Upper.Upper();
GFName Name=Upper;
File=fopen(Filename,"wt");
fprintf(File,"// %s Header\n",Name.File());
fprintf(File,"\n");
fprintf(File,"#ifndef\t__ANIM_%s_HEADER__\n",Name.File());
fprintf(File,"#define\t__ANIM_%s_HEADER__\n",Name.File());
fprintf(File,"\n");
fprintf(File,"\n");
fprintf(File,"enum\tANIM_%s_LIST\n",Name.File());
fprintf(File,"{\n");
int ListSize=AnimList.size();
for (int i=0; i<ListSize; i++)
{
sAnim &ThisAnim=AnimList[i];
fprintf(File,"\tANIM_%s_%s",Name.File(),ThisAnim.Name);
if (i==0)
{
fprintf(File,"=0");
}
fprintf(File,",\n");
}
fprintf(File,"};\n");
fprintf(File,"\n");
fprintf(File,"#endif\n");
fclose(File);
}
2001-01-11 19:35:41 +01:00
//***************************************************************************
//***************************************************************************
//***************************************************************************
void Usage(char *ErrStr)
{
2001-01-11 23:17:57 +01:00
printf("\nMkAnim3d: by Dave\n");
printf("Usage: MkAnim3d <file> [ <file>.. ] [ switches.. ]\n");
2001-01-11 19:35:41 +01:00
printf("Switches:\n");
2001-01-11 23:17:57 +01:00
printf(" -o:[FILE] Set output File\n");
2001-01-11 19:35:41 +01:00
printf(" -d: Enable Debug output\n");
2001-01-12 23:38:19 +01:00
printf(" -s: Set Scale\n");
2001-01-11 19:35:41 +01:00
GObject::Error(ERR_FATAL,ErrStr);
}
//***************************************************************************
int main (int argc, char *argv[])
{
2001-01-11 23:17:57 +01:00
CMkAnim3d AnimBank;
2001-01-11 19:35:41 +01:00
CommandLine(argc,argv,CycleCommands);
if (OutStr.Empty()) Usage("No Output File Set\n");
vector<GString> const &Files = MyFiles.GetFileInfoVector();
for (int Loop=0; Loop<Files.size(); Loop++)
{
2001-01-11 23:17:57 +01:00
AnimBank.Add(Files[Loop]);
2001-01-11 19:35:41 +01:00
}
2001-01-11 23:17:57 +01:00
AnimBank.Write(OutStr);
2001-01-24 23:35:11 +01:00
if (!IncludeFile.Empty())
{
AnimBank.WriteInclude(IncludeFile);
}
2001-01-11 19:35:41 +01:00
return 0;
}