Kukatz 3D  0.1
Török Attila szakdolgozata
projects/Kukatz 3D/src/animation.cpp
00001 /*
00002  * animation.cpp - Kukatz 3D
00003  * Copyright (c) 2011 - TÖRÖK Attila (torokati44@gmail.com)
00004  *
00005  * This software is provided 'as-is', without any express or implied
00006  * warranty. In no event will the authors be held liable for any damages
00007  * arising from the use of this software.
00008  *
00009  * Permission is granted to anyone to use this software for any purpose,
00010  * including commercial applications, and to alter it and redistribute it
00011  * freely, subject to the following restrictions:
00012  *
00013  *       1. The origin of this software must not be misrepresented; you must not
00014  *                      claim that you wrote the original software. If you use this software
00015  *                      in a product, an acknowledgment in the product documentation would be
00016  *                      appreciated but is not required.
00017  *
00018  *       2. Altered source versions must be plainly marked as such, and must not be
00019  *                      misrepresented as being the original software.
00020  *
00021  *       3. This notice may not be removed or altered from any source
00022  *                      distribution.
00023  */
00024 
00025 #include "animation.hpp"
00026 
00027 #include <iostream>
00028 #include <cassert>
00029 #include <cmath> // for HUGE_VALF
00030 
00031 #include "opengl.hpp"
00032 
00033 #include "resourcemanager.hpp"
00034 
00035 Animation::Animation(std::string identifier,
00036                 const std::vector< std::pair< float, std::string > >& mesh_list):
00037         Resource(identifier), duration(mesh_list.back().first - mesh_list.front().first)
00038 {
00039         assert(mesh_list.size() >= 2); // packer won't allow this...
00040         
00041         for (size_t i = 0; i < mesh_list.size(); ++i)
00042         {
00043                 Mesh* current = ResourceManager::instance().meshes[mesh_list[i].second];
00044                 
00045                 meshes.push_back(std::make_pair< float, Mesh* >(
00046                         mesh_list[i].first, current));
00047         }
00048 }
00049 
00050 void Animation::get_interpolation(float t, unsigned int& from, float& ratio)
00051 {
00052         from = 0;
00053         
00054         if (t <= 0)
00055         {
00056                 from = 0;
00057         }
00058         else
00059         {
00060                 if (t >= duration)
00061                 {
00062                         from = meshes.size() - 2;
00063                 }
00064                 else
00065                 {
00066                         while (meshes[from + 1].first <= t)
00067                         {
00068                                 ++from;
00069                         }
00070                 }
00071         }
00072         
00073         ratio = (t - meshes[from].first) / (meshes[from + 1].first - meshes[from].first);
00074 }
00075 
00076 sf::Vector3f Animation::get_sphere_center_at(float t)
00077 {
00078         unsigned int from;
00079         float ratio;
00080         get_interpolation(t, from, ratio);
00081         
00082         return meshes[from].second->sphere_center * (1.0f - ratio) +
00083                 meshes[from + 1].second->sphere_center * ratio;
00084 }
00085 
00086 float Animation::get_sphere_radius_at(float t)
00087 {
00088         unsigned int from;
00089         float ratio;
00090         get_interpolation(t, from, ratio);
00091         
00092         return meshes[from].second->sphere_radius * (1.0f - ratio) +
00093                 meshes[from + 1].second->sphere_radius * ratio;
00094 }
00095 
00096 // Currently there is no extrapolation!
00097 void Animation::draw_at(float t)
00098 {
00099         unsigned int from;
00100         float ratio;
00101         get_interpolation(t, from, ratio);
00102         
00103         ResourceManager* rmp = ResourceManager::instance_ptr();
00104         
00105         glUniform1fARB(rmp->ratio_loc, ratio);
00106         glVertexAttribPointerARB(rmp->pos2_loc, 3, GL_FLOAT, GL_FALSE, 32,
00107                 (char*)0 + ((meshes[from + 1].second->vertex_offset) - (meshes[from].second->vertex_offset)));
00108         //glVertexAttribPointerARB(rmp->nor2_loc, 3, GL_FLOAT, GL_FALSE, 32,
00109         //      (char*)12 + ((meshes[from + 1].second->vertex_offset) - (meshes[from].second->vertex_offset)));
00110         glVertexAttribPointerARB(rmp->tex2_loc, 2, GL_FLOAT, GL_FALSE, 32,
00111                 (char*)24 + ((meshes[from + 1].second->vertex_offset) - (meshes[from].second->vertex_offset)));
00112         
00113         meshes[from].second->draw();
00114 }
00115 
00116 Animation::~Animation()
00117 {
00118         
00119 }
 Összes Osztályok