mysimulation/library/libvitaboy/libvitaboy.hpp

445 lines
8.3 KiB
C++
Raw Permalink Normal View History

/*
libvitaboy - Open source OpenGL TSO character animation library
libvitaboy.hpp - Copyright (c) 2012 Niotso Project <http://niotso.org/>
Author(s): Fatbag <X-Fi6@phppoll.org>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef LIBVITABOY_HPP
#define LIBVITABOY_HPP
2012-02-04 01:19:52 -06:00
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
2024-04-28 09:04:43 -04:00
#include <filehandler.h>
2012-02-04 01:19:52 -06:00
/****
** Bytestream
*/
2024-04-28 09:04:43 -04:00
class VBFile_t
{
private:
2012-02-04 01:19:52 -06:00
const uint8_t *Buffer, *Position;
unsigned Size;
2024-04-28 09:04:43 -04:00
public:
inline void set(const void *_Buffer, unsigned _Size)
{
Buffer = (const uint8_t *)_Buffer;
Position = (const uint8_t *)_Buffer;
2012-02-04 01:19:52 -06:00
Size = _Size;
}
2024-04-28 09:04:43 -04:00
inline unsigned getpos()
{
return Position - Buffer;
2012-02-04 01:19:52 -06:00
}
2024-04-28 09:04:43 -04:00
inline void seekto(unsigned offset)
{
Position = Buffer + offset;
2012-02-04 01:19:52 -06:00
}
2024-04-28 09:04:43 -04:00
inline void seekahead(unsigned count)
{
Position += count;
}
2024-04-28 09:04:43 -04:00
inline void seekback(unsigned count)
{
Position -= count;
}
2024-04-28 09:04:43 -04:00
inline uint32_t readint32()
{
uint32_t value = (uint32_t)((Position[0] << (8 * 3)) | (Position[1] << (8 * 2)) | (Position[2] << (8 * 1)) | (Position[3] << (8 * 0)));
2012-02-04 01:19:52 -06:00
Position += 4;
return value;
}
2024-04-28 09:04:43 -04:00
inline uint32_t readint16()
{
uint16_t value = (uint16_t)((Position[0] << (8 * 1)) | (Position[1] << (8 * 0)));
2012-02-04 01:19:52 -06:00
Position += 2;
return value;
}
2024-04-28 09:04:43 -04:00
inline uint32_t readint8()
{
uint8_t value = (uint8_t)((Position[0] << (8 * 0)));
2012-02-04 01:19:52 -06:00
Position += 1;
return value;
}
2024-04-28 09:04:43 -04:00
inline float readfloat()
{
union
{
uint32_t i;
float f;
} value;
value.i = (uint32_t)((Position[0] << (8 * 0)) | (Position[1] << (8 * 1)) | (Position[2] << (8 * 2)) | (Position[3] << (8 * 3)));
2012-02-04 01:19:52 -06:00
Position += 4;
return value.f;
2012-02-04 01:19:52 -06:00
}
2024-04-28 09:04:43 -04:00
inline void readbytes(void *Destination, unsigned length)
{
2012-02-04 01:19:52 -06:00
memcpy(Destination, Position, length);
Position += length;
}
2024-04-28 09:04:43 -04:00
inline char *readstring()
{
// Read a Pascal string with 1 length byte
2012-02-04 01:19:52 -06:00
unsigned length = readint8();
2024-04-28 09:04:43 -04:00
char *string = (char *)malloc(length + 1);
2012-02-04 01:19:52 -06:00
readbytes(string, length);
string[length] = '\0';
return string;
}
2024-04-28 09:04:43 -04:00
inline char *readstring2()
{
// Read a Pascal string with 2 length bytes
2012-02-04 01:19:52 -06:00
unsigned length = readint16();
2024-04-28 09:04:43 -04:00
char *string = (char *)malloc(length + 1);
2012-02-04 01:19:52 -06:00
readbytes(string, length);
string[length] = '\0';
return string;
}
};
extern VBFile_t VBFile;
/****
** Common
*/
2024-04-28 09:04:43 -04:00
enum ReadGroup
{
NOGROUP,
READGROUP
};
2024-04-28 09:04:43 -04:00
struct Translation_t
{
2012-02-04 01:19:52 -06:00
float x, y, z;
};
2024-04-28 09:04:43 -04:00
struct Rotation_t
{
float x, y, z, w;
2012-02-04 01:19:52 -06:00
};
2024-04-28 09:04:43 -04:00
struct KeyValuePair_t
{
char *Key;
char *Value;
2012-02-04 01:19:52 -06:00
};
2024-04-28 09:04:43 -04:00
struct Prop_t
{
uint32_t EntriesCount;
2024-04-28 09:04:43 -04:00
KeyValuePair_t *Entries;
};
2024-04-28 09:04:43 -04:00
struct PropsList_t
{
2012-02-04 01:19:52 -06:00
uint32_t PropsCount;
2024-04-28 09:04:43 -04:00
Prop_t *Props;
2012-02-04 01:19:52 -06:00
};
2024-04-28 09:04:43 -04:00
void ReadAsset(Asset_t &Asset, bool ReadGroup);
void ReadPropEntry(KeyValuePair_t &Entry);
void ReadPropEntries(Prop_t &Prop);
void ReadPropsList(PropsList_t &PropsList);
float DotProduct(Rotation_t *q1, Rotation_t *q2);
void Normalize(Rotation_t *q);
void CombineQuaternions(Rotation_t *Destination, Rotation_t *Source);
void FindQuaternionMatrix(float *Matrix, Rotation_t *Quaternion);
/****
** Animation (*.anim)
*/
2024-04-28 09:04:43 -04:00
struct TimeProp_t
{
2012-02-04 01:19:52 -06:00
uint32_t ID;
PropsList_t PropsList;
};
2024-04-28 09:04:43 -04:00
struct TimePropsList_t
{
2012-02-04 01:19:52 -06:00
uint32_t TimePropsCount;
2024-04-28 09:04:43 -04:00
TimeProp_t *TimeProps;
2012-02-04 01:19:52 -06:00
};
2024-04-28 09:04:43 -04:00
struct Motion_t
{
2012-02-04 01:19:52 -06:00
uint32_t Unknown;
2024-04-28 09:04:43 -04:00
char *BoneName;
2012-02-04 01:19:52 -06:00
uint32_t FrameCount;
2024-04-28 09:04:43 -04:00
float Duration; // Converted to seconds
2012-02-04 01:19:52 -06:00
uint8_t HasTranslation;
uint8_t HasRotation;
uint32_t FirstTranslation;
uint32_t FirstRotation;
2024-04-28 09:04:43 -04:00
Translation_t *Translations;
Rotation_t *Rotations;
2012-02-04 01:19:52 -06:00
uint8_t HasPropsLists;
uint32_t PropsListsCount;
2024-04-28 09:04:43 -04:00
PropsList_t *PropsLists;
2012-02-04 01:19:52 -06:00
uint8_t HasTimePropsLists;
uint32_t TimePropsListsCount;
2024-04-28 09:04:43 -04:00
TimePropsList_t *TimePropsLists;
2012-02-04 01:19:52 -06:00
};
2024-04-28 09:04:43 -04:00
struct Animation_t
{
2012-02-04 01:19:52 -06:00
uint32_t Version;
2024-04-28 09:04:43 -04:00
char *Name;
float Duration; // Converted to seconds
2012-02-04 01:19:52 -06:00
float Distance;
uint8_t IsMoving;
uint32_t TranslationsCount;
uint32_t RotationsCount;
uint32_t MotionsCount;
unsigned TranslationsOffset;
unsigned RotationsOffset;
2024-04-28 09:04:43 -04:00
Motion_t *Motions;
2012-02-04 01:19:52 -06:00
};
2024-04-28 09:04:43 -04:00
void ReadAnimation(Animation_t &Animation);
void ReadMotion(Animation_t &Animation, Motion_t &Motion);
void ReadPropsLists(Motion_t &Motion);
void ReadTimePropsList(TimePropsList_t &TimePropsList);
void ReadTimePropsLists(Motion_t &Motion);
/****
** Appearance (*.apr)
*/
2024-04-28 09:04:43 -04:00
struct Appearance_t
{
uint32_t Version;
Asset_t Thumbnail;
uint32_t BindingCount;
2024-04-28 09:04:43 -04:00
Asset_t *Bindings;
};
2024-04-28 09:04:43 -04:00
void ReadAppearance(Appearance_t &Appearance);
/****
** Binding (*.bnd)
*/
2024-04-28 09:04:43 -04:00
struct Binding_t
{
uint32_t Version;
2024-04-28 09:04:43 -04:00
char *BoneName;
uint32_t MeshDef;
Asset_t Mesh;
uint32_t AppearanceDef;
Asset_t Appearance;
};
2024-04-28 09:04:43 -04:00
void ReadBinding(Binding_t &Binding);
/****
** Collection (*.col)
*/
2024-04-28 09:04:43 -04:00
struct PODef_t
{
uint32_t Index;
Asset_t PO;
};
2024-04-28 09:04:43 -04:00
struct Collection_t
{
uint32_t POCount;
2024-04-28 09:04:43 -04:00
PODef_t *PurchasableOutfits;
};
2024-04-28 09:04:43 -04:00
void ReadCollection(Collection_t &Collection);
/****
** Hand Group (*.hag)
*/
2024-04-28 09:04:43 -04:00
struct HandGroup_t
{
uint32_t Version;
Asset_t HandAppearances[18];
};
2024-04-28 09:04:43 -04:00
void ReadHandGroup(HandGroup_t &HandGroup);
/****
** Mesh (*.mesh)
*/
2024-04-28 09:04:43 -04:00
struct TextureVertex_t
{
float u, v;
};
2024-04-28 09:04:43 -04:00
struct Coord_t
{
float x, y, z;
};
2024-04-28 09:04:43 -04:00
struct TextureCoord_t
{
float u, v;
};
2024-04-28 09:04:43 -04:00
struct NormalCoord_t
{
float x, y, z;
};
2024-04-28 09:04:43 -04:00
struct BlendData_t
{
float Weight;
unsigned OtherVertex;
};
2024-04-28 09:04:43 -04:00
struct Vertex_t
{
Coord_t Coord;
TextureCoord_t TextureCoord;
NormalCoord_t NormalCoord;
unsigned BoneIndex;
BlendData_t BlendData;
};
2024-04-28 09:04:43 -04:00
struct Face_t
{
unsigned VertexA, VertexB, VertexC;
};
2024-04-28 09:04:43 -04:00
struct BoneBinding_t
{
unsigned BoneIndex;
unsigned FirstRealVertex;
unsigned RealVertexCount;
unsigned FirstBlendVertex;
unsigned BlendVertexCount;
};
2024-04-28 09:04:43 -04:00
struct Mesh_t
{
uint32_t Version;
uint32_t BoneCount;
2024-04-28 09:04:43 -04:00
char **BoneNames;
uint32_t FaceCount;
2024-04-28 09:04:43 -04:00
Face_t *FaceData;
uint32_t BindingCount;
2024-04-28 09:04:43 -04:00
BoneBinding_t *BoneBindings;
uint32_t RealVertexCount;
uint32_t BlendVertexCount;
uint32_t TotalVertexCount;
2024-04-28 09:04:43 -04:00
Vertex_t *VertexData;
Vertex_t *TransformedVertexData;
};
2024-04-28 09:04:43 -04:00
void ReadMesh(Mesh_t &Mesh);
/****
** Outfit (*.oft)
*/
2024-04-28 09:04:43 -04:00
enum OutfitColor
{
OutfitColor_Light,
OutfitColor_Medium,
OutfitColor_Dark
};
2024-04-28 09:04:43 -04:00
enum OutfitRegion
{
OutfitRegion_Head = 0,
OutfitRegion_Body = 18
};
2024-04-28 09:04:43 -04:00
struct Outfit_t
{
uint32_t Version;
uint32_t Unknown;
Asset_t Appearance[3];
uint32_t Group;
uint32_t Region;
};
2024-04-28 09:04:43 -04:00
void ReadOutfit(Outfit_t &Outfit);
/****
** Purchasable Outfit (*.po)
*/
2024-04-28 09:04:43 -04:00
struct PurchasableOutfit_t
{
uint32_t Version;
uint32_t Unknown;
uint32_t OutfitDef;
Asset_t Outfit;
uint32_t CollectionDef;
Asset_t Collection;
};
2024-04-28 09:04:43 -04:00
void ReadPurchasableOutfit(PurchasableOutfit_t &PurchasableOutfit);
/****
** Skeleton (*.skel)
*/
2024-04-28 09:04:43 -04:00
struct Bone_t
{
uint32_t Unknown;
2024-04-28 09:04:43 -04:00
char *Name;
char *ParentsName;
uint8_t HasProps;
PropsList_t PropsList;
Translation_t Translation;
Rotation_t Rotation;
uint32_t CanTranslate;
uint32_t CanRotate;
uint32_t CanBlend;
float WiggleValue;
float WigglePower;
unsigned ChildrenCount;
2024-04-28 09:04:43 -04:00
Bone_t **Children;
};
2024-04-28 09:04:43 -04:00
struct Skeleton_t
{
uint32_t Version;
2024-04-28 09:04:43 -04:00
char *Name;
uint16_t BoneCount;
2024-04-28 09:04:43 -04:00
Bone_t *Bones;
};
2024-04-28 09:04:43 -04:00
void ReadSkeleton(Skeleton_t &Bone);
void ReadBone(Skeleton_t &Skeleton, Bone_t &Bone, unsigned Index);
unsigned FindBone(Skeleton_t &Skeleton, const char *BoneName, unsigned Count);
#endif