From e3f6880ed29972dd46d3919c7ea938a92e5879c2 Mon Sep 17 00:00:00 2001 From: Fatbag Date: Sun, 19 Feb 2012 00:15:34 -0600 Subject: [PATCH] Working animations, albeit without time synchronization; will run too fast, but working nevertheless --- Libraries/libvitaboy/Renderer.cpp | 80 ++++++++++++++++++++++++++--- Libraries/libvitaboy/anim.cpp | 28 +++++++--- Libraries/libvitaboy/libvitaboy.cpp | 55 ++++++++++++++++---- Libraries/libvitaboy/libvitaboy.hpp | 21 +++++++- Libraries/libvitaboy/mesh.cpp | 16 ++++++ Libraries/libvitaboy/skel.cpp | 16 ++++++ Libraries/libvitaboy/vbparse.cpp | 2 +- 7 files changed, 190 insertions(+), 28 deletions(-) diff --git a/Libraries/libvitaboy/Renderer.cpp b/Libraries/libvitaboy/Renderer.cpp index 9f1ef88..8d03e4a 100644 --- a/Libraries/libvitaboy/Renderer.cpp +++ b/Libraries/libvitaboy/Renderer.cpp @@ -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 + + 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 // Header File For Windows #include // 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.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)) diff --git a/Libraries/libvitaboy/anim.cpp b/Libraries/libvitaboy/anim.cpp index 0af71c0..5c01cb2 100644 --- a/Libraries/libvitaboy/anim.cpp +++ b/Libraries/libvitaboy/anim.cpp @@ -1,3 +1,19 @@ +/* + libvitaboy - Copyright (c) 2012 Fatbag + + 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 + + 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; diff --git a/Libraries/libvitaboy/libvitaboy.hpp b/Libraries/libvitaboy/libvitaboy.hpp index 6c32db5..ec236b6 100644 --- a/Libraries/libvitaboy/libvitaboy.hpp +++ b/Libraries/libvitaboy/libvitaboy.hpp @@ -1,3 +1,19 @@ +/* + libvitaboy - Copyright (c) 2012 Fatbag + + 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 #include #include @@ -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; diff --git a/Libraries/libvitaboy/mesh.cpp b/Libraries/libvitaboy/mesh.cpp index f2029fb..74226a6 100644 --- a/Libraries/libvitaboy/mesh.cpp +++ b/Libraries/libvitaboy/mesh.cpp @@ -1,3 +1,19 @@ +/* + libvitaboy - Copyright (c) 2012 Fatbag + + 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){ diff --git a/Libraries/libvitaboy/skel.cpp b/Libraries/libvitaboy/skel.cpp index ba007aa..4eb0431 100644 --- a/Libraries/libvitaboy/skel.cpp +++ b/Libraries/libvitaboy/skel.cpp @@ -1,3 +1,19 @@ +/* + libvitaboy - Copyright (c) 2012 Fatbag + + 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; diff --git a/Libraries/libvitaboy/vbparse.cpp b/Libraries/libvitaboy/vbparse.cpp index cb42645..597fdf8 100644 --- a/Libraries/libvitaboy/vbparse.cpp +++ b/Libraries/libvitaboy/vbparse.cpp @@ -1,5 +1,5 @@ /* - vbparse.cpp - Copyright (c) 2012 Fatbag + libvitaboy - Copyright (c) 2012 Fatbag Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above