Working animations, albeit without time synchronization; will run too fast, but working nevertheless

This commit is contained in:
Fatbag 2012-02-19 00:15:34 -06:00
parent b05b32d5f5
commit e3f6880ed2
7 changed files with 190 additions and 28 deletions

View file

@ -1,10 +1,18 @@
/*
* This Code Was Created By Jeff Molofee 2000
* A HUGE Thanks To Fredric Echols For Cleaning Up
* And Optimizing The Base Code, Making It More Flexible!
* If You've Found This Code Useful, Please Let Me Know.
* Visit My Site At nehe.gamedev.net
*/
libvitaboy - Copyright (c) 2012 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.
*/
#include <windows.h> // Header File For Windows
#include <gl\gl.h> // Header File For The OpenGL32 Library
@ -22,6 +30,8 @@ bool keys[256] = {0}; // Array Used For The Keyboard Routine
bool active=TRUE; // Window Active Flag Set To TRUE By Default
bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default
bool press = false;
float zoom = -10;
struct CharacterPlacement_t {
Vertex_t Translation;
@ -33,7 +43,7 @@ CharacterPlacement_t Character = {{0,-3,0}, {0,0,0}};
Skeleton_t Skeleton;
unsigned TextureCount = 3;
GLuint texture[3]; // Storage For One Texture ( NEW )
GLuint texture[3];
enum { Texture_Body, Texture_Head, Texture_Hand };
const char* TexturePaths[] = {"body.jpg", "head.jpg", "hand.jpg"};
@ -43,6 +53,8 @@ enum { Mesh_Body, Mesh_Head, Mesh_LHand, Mesh_RHand };
unsigned Mesh_UseTexture[] = { Texture_Body, Texture_Head, Texture_Hand, Texture_Hand };
const char* MeshActivate[] = {NULL, "HEAD", "L_HAND", "R_HAND"};
Animation_t Animation;
bool ShowMesh = true;
bool ShowSkeleton = true;
@ -52,7 +64,7 @@ int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
for(int i=0; i<3; i++){
/* load an image file directly as a new OpenGL texture */
texture[i] = SOIL_load_OGL_texture(TexturePaths[i], SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS);
texture[i] = SOIL_load_OGL_texture(TexturePaths[i], SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, 0);
if(texture[i] == 0)
return false;
@ -201,6 +213,30 @@ void DrawMeshes(){
glDisable(GL_TEXTURE_2D);
}
void AdvanceFrame(Skeleton_t& Skeleton, Animation_t& Animation){
static unsigned Frame = 0;
for(unsigned i=0; i<Animation.MotionsCount; i++){
Bone_t& Bone = Skeleton.Bones[FindBone(Skeleton, Animation.Motions[i].BoneName, Skeleton.BoneCount)];
if(Animation.Motions[i].HasTranslation){
Translation_t& Translation = Animation.Motions[i].Translations[Frame];
Bone.Translation.x = Translation.x;
Bone.Translation.y = Translation.y;
Bone.Translation.z = Translation.z;
}
if(Animation.Motions[i].HasRotation){
Rotation_t& Rotation = Animation.Motions[i].Rotations[Frame];
Bone.Rotation.x = Rotation.x;
Bone.Rotation.y = Rotation.y;
Bone.Rotation.z = Rotation.z;
Bone.Rotation.w = Rotation.w;
}
}
if(++Frame >= Animation.Motions[0].FrameCount) Frame = 0;
}
void DrawBonesSkeleton(Bone_t& Bone){
glPointSize(5.0);
glTranslatef(Bone.Translation.x, Bone.Translation.y, Bone.Translation.z);
@ -237,6 +273,7 @@ int DrawGLScene(void) // Here's Where We Do All The Drawing
if(keys['I']){ Character.Translation.y+=0.05f; }
if(keys['J']){ Character.Translation.x-=0.05f; }
if(keys['L']){ Character.Translation.x+=0.05f; }
if(keys['N']){ AdvanceFrame(Skeleton, Animation); }
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
@ -677,6 +714,33 @@ int WINAPI WinMain( HINSTANCE, // Instance
VBFile.set(InData, FileSize);
ReadMesh(Meshes[3]);
free(InData);
hFile = CreateFile("animation.anim", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if(hFile == INVALID_HANDLE_VALUE){
if(GetLastError() == ERROR_FILE_NOT_FOUND){
MessageBox(NULL, "animation.anim does not exist.", "Error", MB_OK);
return 0;
}
MessageBox(NULL, "animation.anim could not be opened for reading.", "Error", MB_OK);
return 0;
}
FileSize = GetFileSize(hFile, NULL);
InData = (uint8_t*) malloc(FileSize);
if(InData == NULL){
MessageBox(NULL, "Memory for animation.anim could not be allocated.", "Error", MB_OK);
return 0;
}
if(!ReadFile(hFile, InData, FileSize, &bytestransferred, NULL) || bytestransferred != FileSize){
MessageBox(NULL, "animation.anim could not be read.", "Error", MB_OK);
return 0;
}
CloseHandle(hFile);
VBFile.set(InData, FileSize);
ReadAnimation(Animation);
free(InData);
AdvanceFrame(Skeleton, Animation);
// Create Our OpenGL Window
if (!CreateGLWindow("libvitaboy - Renderer",640,480,16,fullscreen))

View file

@ -1,3 +1,19 @@
/*
libvitaboy - Copyright (c) 2012 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.
*/
#include "libvitaboy.hpp"
static unsigned motionnumber = 0;
@ -49,18 +65,18 @@ void ReadMotion(Animation_t& Animation, Motion_t& Motion){
printf(" | HasTranslation: %u\n", Motion.HasTranslation);
Motion.HasRotation = VBFile.readint8();
printf(" | HasRotation: %u\n", Motion.HasRotation);
Motion.TranslationsOffset = VBFile.readint32();
Motion.FirstTranslation = VBFile.readint32();
if(Motion.HasTranslation)
printf(" | TranslationsOffset: %u\n", Motion.TranslationsOffset);
Motion.RotationsOffset = VBFile.readint32();
printf(" | FirstTranslation: %u\n", Motion.FirstTranslation);
Motion.FirstRotation = VBFile.readint32();
if(Motion.HasRotation)
printf(" | RotationsOffset: %u\n", Motion.RotationsOffset);
printf(" | FirstRotation: %u\n", Motion.FirstRotation);
if(Motion.HasTranslation){
Motion.Translations = (Translation_t*) malloc(Motion.FrameCount * sizeof(Translation_t));
unsigned pos = VBFile.getpos();
VBFile.seekto(Animation.TranslationsOffset + 12*Motion.TranslationsOffset);
VBFile.seekto(Animation.TranslationsOffset + 12*Motion.FirstTranslation);
for(unsigned i=0; i<Motion.FrameCount; i++){
Motion.Translations[i].x = VBFile.readfloat();
Motion.Translations[i].y = VBFile.readfloat();
@ -73,7 +89,7 @@ void ReadMotion(Animation_t& Animation, Motion_t& Motion){
Motion.Rotations = (Rotation_t*) malloc(Motion.FrameCount * sizeof(Rotation_t));
unsigned pos = VBFile.getpos();
VBFile.seekto(Animation.RotationsOffset + 16*Motion.RotationsOffset);
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();

View file

@ -1,3 +1,19 @@
/*
libvitaboy - Copyright (c) 2012 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.
*/
#include "libvitaboy.hpp"
VBFile_t VBFile;
@ -20,6 +36,23 @@ void ReadPropEntries(Prop_t& Prop){
}
}
void CombineQuaternions(Rotation_t * Destination, Rotation_t * Source){
// the constructor takes its arguments as (x, y, z, w)
float dx = Destination->x;
float dy = Destination->y;
float dz = Destination->z;
float dw = Destination->w;
float sx = Source->x;
float sy = Source->y;
float sz = Source->z;
float sw = Source->w;
Destination->x = dw*sx + dx*sw + dy*sz - dz*sy;
Destination->y = dw*sy + dy*sw + dz*sx - dx*sz;
Destination->z = dw*sz + dz*sw + dx*sy - dy*sx;
Destination->w = dw*sw - dx*sx - dy*sy - dz*sz;
}
void FindQuaternionMatrix(float * Matrix, Rotation_t * Quaternion){
float x2 = Quaternion->x * Quaternion->x;
float y2 = Quaternion->y * Quaternion->y;
@ -30,17 +63,17 @@ void FindQuaternionMatrix(float * Matrix, Rotation_t * Quaternion){
float wx = Quaternion->w * Quaternion->x;
float wy = Quaternion->w * Quaternion->y;
float wz = Quaternion->w * Quaternion->z;
Matrix[0] = 1.0f - 2.0f * (y2 + z2);
Matrix[1] = 2.0f * (xy - wz);
Matrix[2] = 2.0f * (xz + wy);
Matrix[3] = 0.0f;
Matrix[4] = 2.0f * (xy + wz);
Matrix[5] = 1.0f - 2.0f * (x2 + z2);
Matrix[6] = 2.0f * (yz - wx);
Matrix[7] = 0.0f;
Matrix[8] = 2.0f * (xz - wy);
Matrix[9] = 2.0f * (yz + wx);
Matrix[0] = 1.0f - 2.0f * (y2 + z2);
Matrix[1] = 2.0f * (xy - wz);
Matrix[2] = 2.0f * (xz + wy);
Matrix[3] = 0.0f;
Matrix[4] = 2.0f * (xy + wz);
Matrix[5] = 1.0f - 2.0f * (x2 + z2);
Matrix[6] = 2.0f * (yz - wx);
Matrix[7] = 0.0f;
Matrix[8] = 2.0f * (xz - wy);
Matrix[9] = 2.0f * (yz + wx);
Matrix[10] = 1.0f - 2.0f * (x2 + y2);
Matrix[11] = 0.0f;
Matrix[12] = 0.0f;

View file

@ -1,3 +1,19 @@
/*
libvitaboy - Copyright (c) 2012 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.
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
@ -106,6 +122,7 @@ struct PropsList_t {
void ReadPropEntry(KeyValuePair_t& Entry);
void ReadPropEntries(Prop_t& Prop);
void ReadPropsList(PropsList_t& PropsList);
void CombineQuaternions(Rotation_t * Destination, Rotation_t * Source);
void FindQuaternionMatrix(float * Matrix, Rotation_t * Quaternion);
@ -130,8 +147,8 @@ struct Motion_t {
float Duration;
uint8_t HasTranslation;
uint8_t HasRotation;
uint32_t TranslationsOffset;
uint32_t RotationsOffset;
uint32_t FirstTranslation;
uint32_t FirstRotation;
Translation_t * Translations;
Rotation_t * Rotations;

View file

@ -1,3 +1,19 @@
/*
libvitaboy - Copyright (c) 2012 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.
*/
#include "libvitaboy.hpp"
void ReadMesh(Mesh_t& Mesh){

View file

@ -1,3 +1,19 @@
/*
libvitaboy - Copyright (c) 2012 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.
*/
#include "libvitaboy.hpp"
static unsigned bonenumber = 0;

View file

@ -1,5 +1,5 @@
/*
vbparse.cpp - Copyright (c) 2012 Fatbag <X-Fi6@phppoll.org>
libvitaboy - Copyright (c) 2012 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