Mirrored rendering by libvitaboy Renderer

The next task is to add functionality to the far library to make it suitable for use directly by libvitaboy Renderer.# Please enter the commit message for your changes. Lines starting
This commit is contained in:
Fatbag 2012-10-27 23:45:56 -05:00
parent 6393955683
commit bb904c4698
14 changed files with 116 additions and 75 deletions

View file

@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.6)
project(far)
set(FAR_SOURCES
libfar.c
far.c
refpack_dec.c
)

View file

@ -1,24 +1,24 @@
/* config.h - libfar build configuration */
/* config.h - far build configuration */
/* Define if you have the <stdint.h> header file. */
#define HAVE_STDINT_H
/* compile-in support */
#define LIBFAR_ARCHIVEREAD
#define LIBFAR_ARCHIVEWRITE
#define LIBFAR_REFPACK_DECOMPRESS
#define LIBFAR_REFPACK_COMPRESS
#define LIBFAR_SUPPORT_FAR
#define LIBFAR_SUPPORT_DBPF
#define LIBFAR_SUPPORT_PERSIST
#define LIBFAR_DEBUGSUPPORT
#define LIBFAR_FILEIO
#define LIBFAR_EMBEDDEDFUNCTIONS
#define FAR_ARCHIVEREAD
#define FAR_ARCHIVEWRITE
#define FAR_REFPACK_DECOMPRESS
#define FAR_REFPACK_COMPRESS
#define FAR_SUPPORT_FAR
#define FAR_SUPPORT_DBPF
#define FAR_SUPPORT_PERSIST
#define FAR_DEBUGSUPPORT
#define FAR_FILEIO
#define FAR_EMBEDDEDFUNCTIONS
/* end of compile-in support */
/* preferences -- on non-numerical definitions, define to 1 for "yes", 0 for "no"; */
#define LIBFAR_DEFAULT_1A 0
#define LIBFAR_DEFAULT_DBPF_COMPRESSED 0
#define LIBFAR_DEFAULT_MAX_FILE_NAME_LENGTH 255
#define LIBFAR_DEFAULT_REFPACK_HNSV 0xFB
/* preferences -- on non-numerical definitions, define to 1 for "yes", 0 for "no" */
#define FAR_DEFAULT_1A 0
#define FAR_DEFAULT_DBPF_COMPRESSED 0
#define FAR_DEFAULT_MAX_FILE_NAME_LENGTH 255
#define FAR_DEFAULT_REFPACK_HNSV 0xFB
/* end of default preferences */

View file

@ -22,14 +22,14 @@
#include <string.h>
#include <stdint.h>
#include "libfar.h"
#include "far.h"
#if defined(LIBFAR_SUPPORT_PERSIST)
#define LIBFAR_MINSIZE_ANY MINSIZE_PERSIST
#elif defined(LIBFAR_SUPPORT_FAR)
#define LIBFAR_MINSIZE_ANY MINSIZE_FAR
#if defined(FAR_SUPPORT_PERSIST)
#define FAR_MINSIZE_ANY MINSIZE_PERSIST
#elif defined(FAR_SUPPORT_FAR)
#define FAR_MINSIZE_ANY MINSIZE_FAR
#else
#define LIBFAR_MINSIZE_ANY MINSIZE_DBPF
#define FAR_MINSIZE_ANY MINSIZE_DBPF
#endif
#ifndef read_int32
@ -52,10 +52,10 @@
/* These options can be changed during runtime */
static int libfarOptions[] = {
LIBFAR_DEFAULT_1A,
LIBFAR_DEFAULT_DBPF_COMPRESSED,
LIBFAR_DEFAULT_MAX_FILE_NAME_LENGTH,
LIBFAR_DEFAULT_REFPACK_HNSV
FAR_DEFAULT_1A,
FAR_DEFAULT_DBPF_COMPRESSED,
FAR_DEFAULT_MAX_FILE_NAME_LENGTH,
FAR_DEFAULT_REFPACK_HNSV
};
void libfar_set_option(int Option, int Value){
@ -68,22 +68,22 @@ int libfar_get_option(int Option){
int far_identify(const uint8_t * Buffer, unsigned FileSize)
{
if(!FileSize) FileSize = ~0;
else if(FileSize < LIBFAR_MINSIZE_ANY)
else if(FileSize < FAR_MINSIZE_ANY)
return FAR_TYPE_INVALID;
#ifdef LIBFAR_SUPPORT_FAR
#ifdef FAR_SUPPORT_FAR
if(FileSize >= MINSIZE_FAR)
if(!memcmp(Buffer, Header_FAR, 8))
return FAR_TYPE_FAR;
#endif
#ifdef LIBFAR_SUPPORT_DBPF
#ifdef FAR_SUPPORT_DBPF
if(FileSize >= MINSIZE_DBPF)
if(!memcmp(Buffer, Header_DBPF, 4))
return FAR_TYPE_DBPF;
#endif
#ifdef LIBFAR_SUPPORT_PERSIST
#ifdef FAR_SUPPORT_PERSIST
if(FileSize >= MINSIZE_PERSIST)
if(Buffer[0] == 0x01)
return FAR_TYPE_PERSIST;
@ -158,10 +158,10 @@ FAREntryNode * far_add_entry(FARFile * FARFileInfo, int Position)
int far_read_header(FARFile * FARFileInfo, const uint8_t * Buffer, unsigned FileSize)
{
if(!FileSize) FileSize = ~0;
else if(FileSize < LIBFAR_MINSIZE_ANY)
else if(FileSize < FAR_MINSIZE_ANY)
return 0;
#ifdef LIBFAR_SUPPORT_FAR
#ifdef FAR_SUPPORT_FAR
if(FARFileInfo->Type == FAR_TYPE_FAR){
FARFileInfo->MajorVersion = read_uint32(Buffer+8);
FARFileInfo->IndexOffset = read_uint32(Buffer+12);
@ -170,7 +170,7 @@ int far_read_header(FARFile * FARFileInfo, const uint8_t * Buffer, unsigned File
return 0;
if(FARFileInfo->MajorVersion == 1)
FARFileInfo->Revision = !libfarOptions[LIBFAR_CONFIG_DEFAULT_TO_1A];
FARFileInfo->Revision = !libfarOptions[FAR_CONFIG_DEFAULT_TO_1A];
if(FARFileInfo->IndexOffset > FileSize-4)
return 0;
@ -187,7 +187,7 @@ int far_read_header(FARFile * FARFileInfo, const uint8_t * Buffer, unsigned File
}
#endif
#ifdef LIBFAR_SUPPORT_DBPF
#ifdef FAR_SUPPORT_DBPF
if(FARFileInfo->Type == FAR_TYPE_DBPF){
int i;
@ -239,7 +239,7 @@ int far_read_entry(const FARFile * FARFileInfo, FAREntry * FAREntryInfo,
if(MaxEntrySize == 0) MaxEntrySize = ~0;
if(ArchiveSize == 0) ArchiveSize = ~0;
#ifdef LIBFAR_SUPPORT_FAR
#ifdef FAR_SUPPORT_FAR
if(FARFileInfo->Type == FAR_TYPE_FAR){
unsigned MinEntrySize =
(MajorVersion == 1) ? (
@ -269,7 +269,7 @@ int far_read_entry(const FARFile * FARFileInfo, FAREntry * FAREntryInfo,
}
if(FAREntryInfo->FilenameLength > MaxEntrySize - MinEntrySize) return 0;
if(FAREntryInfo->FilenameLength > (unsigned)libfarOptions[LIBFAR_CONFIG_MAX_FILE_NAME_LENGTH]) return 0;
if(FAREntryInfo->FilenameLength > (unsigned)libfarOptions[FAR_CONFIG_MAX_FILE_NAME_LENGTH]) return 0;
if(FAREntryInfo->CompressedSize > FAREntryInfo->DecompressedSize) return 0;
if(FAREntryInfo->DecompressedSize != 0){
@ -286,7 +286,7 @@ int far_read_entry(const FARFile * FARFileInfo, FAREntry * FAREntryInfo,
}
#endif
#ifdef LIBFAR_SUPPORT_DBPF
#ifdef FAR_SUPPORT_DBPF
if(FARFileInfo->Type == FAR_TYPE_DBPF){
if(MaxEntrySize < SIZEOF_ENTRY_DBPF) return 0;
@ -346,7 +346,7 @@ int far_read_entry_data(const FARFile * FARFileInfo, FAREntry * FAREntryInfo, ui
int Compressed = (FARFileInfo->Type == FAR_TYPE_FAR) ? (
(FARFileInfo->MajorVersion == 1) ? (
FAREntryInfo->DecompressedSize != FAREntryInfo->CompressedSize
) : FAREntryInfo->DataType == 0x80) : libfarOptions[LIBFAR_CONFIG_DBPF_COMPRESSED];
) : FAREntryInfo->DataType == 0x80) : libfarOptions[FAR_CONFIG_DBPF_COMPRESSED];
FAREntryInfo->CompressedData = Buffer+FAREntryInfo->DataOffset;
@ -390,7 +390,7 @@ int far_read_persist_data(PersistFile * PersistData, uint8_t * CompressedData)
if(!RefPackDecompress(PersistData->CompressedData, PersistData->CompressedSize-9,
PersistData->DecompressedData, PersistData->DecompressedSize,
libfarOptions[LIBFAR_CONFIG_REFPACK_HNSV])){
libfarOptions[FAR_CONFIG_REFPACK_HNSV])){
free(PersistData->DecompressedData);
return 0;
}

View file

@ -21,10 +21,10 @@
*/
/* libfarOptions array members */
#define LIBFAR_CONFIG_DEFAULT_TO_1A 0
#define LIBFAR_CONFIG_DBPF_COMPRESSED 1
#define LIBFAR_CONFIG_MAX_FILE_NAME_LENGTH 2
#define LIBFAR_CONFIG_REFPACK_HNSV 3
#define FAR_CONFIG_DEFAULT_TO_1A 0
#define FAR_CONFIG_DBPF_COMPRESSED 1
#define FAR_CONFIG_MAX_FILE_NAME_LENGTH 2
#define FAR_CONFIG_REFPACK_HNSV 3
/* Archive types */
#define FAR_TYPE_INVALID 0
@ -33,7 +33,7 @@
#define FAR_TYPE_PERSIST 3
/* Numerical constants */
#define LIBFAR_ARCHIVE_MINIMUM_SIZE 14
#define FAR_ARCHIVE_MINIMUM_SIZE 14
#define MINSIZE_FAR 20
#define MINSIZE_DBPF 64
#define MINSIZE_ENTRY_FAR_1A 16

View file

@ -22,7 +22,7 @@
#include <stdint.h>
#include <time.h>
#include "config.h"
#include "libfar.h"
#include "far.h"
enum {
profile_ts1 = 1,
@ -100,9 +100,9 @@ int main(int argc, char *argv[]){
** Handle profile settings
*/
if(!profile) profile = profile_tso;
libfar_set_option(LIBFAR_CONFIG_DEFAULT_TO_1A, (profile == profile_ts1));
libfar_set_option(LIBFAR_CONFIG_DBPF_COMPRESSED, (profile >= profile_sc4));
libfar_set_option(LIBFAR_CONFIG_REFPACK_HNSV, 0xFB);
libfar_set_option(FAR_CONFIG_DEFAULT_TO_1A, (profile == profile_ts1));
libfar_set_option(FAR_CONFIG_DBPF_COMPRESSED, (profile >= profile_sc4));
libfar_set_option(FAR_CONFIG_REFPACK_HNSV, 0xFB);
/****
** Open the file and read in the entire contents to memory

View file

@ -148,6 +148,7 @@ static int InitGL()
glDisable(GL_BLEND);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glFrontFace(GL_CW);
return 1;
}
@ -160,6 +161,7 @@ static int ResizeScene(uint16_t width, uint16_t height)
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f);
// glScalef(-1.0f, 1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
@ -300,28 +302,17 @@ static void AdvanceFrame(Skeleton_t& Skeleton, Animation_t& Animation, float Tim
Rotation_t& Rotation = Animation.Motions[i].Rotations[Frame];
Rotation_t& NextRotation = Animation.Motions[i].Rotations[NextFrame];
//Use Slerp to interpolate
float w1, w2 = 1;
float cosTheta = DotProduct(&Rotation, &NextRotation);
if(cosTheta < 0){
cosTheta *= -1;
w2 *= -1;
}
float theta = (float) acos(cosTheta);
float sinTheta = (float) sin(theta);
if(sinTheta > 0.001f){
w1 = (float) sin((1.0f-FractionShown)*theta)/sinTheta;
w2 *= (float) sin(FractionShown *theta)/sinTheta;
} else {
w1 = 1.0f - FractionShown;
w2 = FractionShown;
}
//Use nlerp to interpolate
float w1 = 1.0f - FractionShown, w2 = FractionShown;
if(DotProduct(&Rotation, &NextRotation) < 0)
w1 *= -1;
Bone.Rotation.x = w1*Rotation.x + w2*NextRotation.x;
Bone.Rotation.y = w1*Rotation.y + w2*NextRotation.y;
Bone.Rotation.z = w1*Rotation.z + w2*NextRotation.z;
Bone.Rotation.w = w1*Rotation.w + w2*NextRotation.w;
Normalize(&Bone.Rotation);
}
}
}

View file

@ -80,7 +80,7 @@ void ReadMotion(Animation_t& Animation, Motion_t& Motion){
unsigned pos = VBFile.getpos();
VBFile.seekto(Animation.TranslationsOffset + 12*Motion.FirstTranslation);
for(unsigned i=0; i<Motion.FrameCount; i++){
Motion.Translations[i].x = VBFile.readfloat();
Motion.Translations[i].x = -VBFile.readfloat();
Motion.Translations[i].y = VBFile.readfloat();
Motion.Translations[i].z = VBFile.readfloat();
}
@ -94,8 +94,8 @@ void ReadMotion(Animation_t& Animation, Motion_t& Motion){
VBFile.seekto(Animation.RotationsOffset + 16*Motion.FirstRotation);
for(unsigned i=0; i<Motion.FrameCount; i++){
Motion.Rotations[i].x = VBFile.readfloat();
Motion.Rotations[i].y = VBFile.readfloat();
Motion.Rotations[i].z = VBFile.readfloat();
Motion.Rotations[i].y = -VBFile.readfloat();
Motion.Rotations[i].z = -VBFile.readfloat();
Motion.Rotations[i].w = VBFile.readfloat();
}
VBFile.seekto(pos);

View file

@ -16,6 +16,7 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <math.h>
#include "libvitaboy.hpp"
VBFile_t VBFile;
@ -51,6 +52,17 @@ float DotProduct(Rotation_t * q1, Rotation_t * q2){
return q1->x*q2->x + q1->y*q2->y + q1->z*q2->z + q1->w*q2->w;
}
void Normalize(Rotation_t * q){
float magnitude = q->x*q->x + q->y*q->y + q->z*q->z + q->w*q->w;
if(magnitude != 0){
magnitude = 1.0f/sqrt(magnitude);
q->x *= magnitude;
q->y *= magnitude;
q->z *= magnitude;
q->w *= magnitude;
}
}
void FindQuaternionMatrix(float * Matrix, Rotation_t * Quaternion){
float x2 = Quaternion->x * Quaternion->x;
float y2 = Quaternion->y * Quaternion->y;

View file

@ -25,6 +25,10 @@
#include <stdio.h>
#include <FileHandler.hpp>
/****
** Bytestream
*/
class VBFile_t {
private:
const uint8_t *Buffer, *Position;
@ -139,6 +143,7 @@ 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);
@ -214,6 +219,7 @@ struct Appearance_t {
void ReadAppearance(Appearance_t& Appearance);
/****
** Binding (*.bnd)
*/

View file

@ -72,10 +72,10 @@ void ReadMesh(Mesh_t& Mesh){
Mesh.VertexData = (Vertex_t*) malloc(Mesh.TotalVertexCount * sizeof(Vertex_t));
Mesh.TransformedVertexData = (Vertex_t*) malloc(Mesh.TotalVertexCount * sizeof(Vertex_t));
for(unsigned i=0; i<Mesh.TotalVertexCount; i++){
Mesh.VertexData[i].Coord.x = VBFile.readfloat();
Mesh.VertexData[i].Coord.x = -VBFile.readfloat();
Mesh.VertexData[i].Coord.y = VBFile.readfloat();
Mesh.VertexData[i].Coord.z = VBFile.readfloat();
Mesh.TransformedVertexData[i].NormalCoord.x = VBFile.readfloat();
Mesh.TransformedVertexData[i].NormalCoord.x = -VBFile.readfloat();
Mesh.TransformedVertexData[i].NormalCoord.y = VBFile.readfloat();
Mesh.TransformedVertexData[i].NormalCoord.z = VBFile.readfloat();

View file

@ -49,7 +49,7 @@ void ReadBone(Skeleton_t& Skeleton, Bone_t& Bone, unsigned Index){
}
printf(" | Translation:\n");
Bone.Translation.x = VBFile.readfloat();
Bone.Translation.x = -VBFile.readfloat();
printf(" | | x: %g\n", Bone.Translation.x);
Bone.Translation.y = VBFile.readfloat();
printf(" | | y: %g\n", Bone.Translation.y);
@ -58,9 +58,9 @@ void ReadBone(Skeleton_t& Skeleton, Bone_t& Bone, unsigned Index){
printf(" | Rotation:\n");
Bone.Rotation.x = VBFile.readfloat();
printf(" | | x: %g\n", Bone.Rotation.x);
Bone.Rotation.y = VBFile.readfloat();
Bone.Rotation.y = -VBFile.readfloat();
printf(" | | y: %g\n", Bone.Rotation.y);
Bone.Rotation.z = VBFile.readfloat();
Bone.Rotation.z = -VBFile.readfloat();
printf(" | | z: %g\n", Bone.Rotation.z);
Bone.Rotation.w = VBFile.readfloat();
printf(" | | w: %g\n", Bone.Rotation.w);