// ============================================================== // This file is part of Glest Shared Library (www.glest.org) // // Copyright (C) 2001-2008 MartiƱo Figueroa // // You can redistribute this code and/or modify it under // the terms of the GNU General Public License as published // by the Free Software Foundation; either version 2 of the // License, or (at your option) any later version // ============================================================== #include "interpolation.h" #include #include #include "model.h" #include "conversion.h" #include "util.h" #include #include "platform_util.h" #include "leak_dumper.h" using namespace std; using namespace Shared::Util; namespace Shared{ namespace Graphics{ // ===================================================== // class InterpolationData // ===================================================== bool InterpolationData::enableCache = false; InterpolationData::InterpolationData(const Mesh *mesh) { if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { throw megaglest_runtime_error("Loading graphics in headless server mode not allowed!"); } vertices= NULL; normals= NULL; this->mesh= mesh; if(mesh->getFrameCount()>1) { vertices= new Vec3f[mesh->getVertexCount()]; normals= new Vec3f[mesh->getVertexCount()]; } cacheVertices.clear(); cacheNormals.clear(); } InterpolationData::~InterpolationData(){ delete [] vertices; vertices=NULL; delete [] normals; normals=NULL; for(std::map >::iterator iterVert = cacheVertices.begin(); iterVert != cacheVertices.end(); ++iterVert) { for(std::map::iterator iterVert2 = iterVert->second.begin(); iterVert2 != iterVert->second.end(); ++iterVert2) { delete [] iterVert2->second; } } cacheVertices.clear(); for(std::map >::iterator iterVert = cacheNormals.begin(); iterVert != cacheNormals.end(); ++iterVert) { for(std::map::iterator iterVert2 = iterVert->second.begin(); iterVert2 != iterVert->second.end(); ++iterVert2) { delete [] iterVert2->second; } } cacheNormals.clear(); } void InterpolationData::update(float t, bool cycle){ updateVertices(t, cycle); updateNormals(t, cycle); } void InterpolationData::updateVertices(float t, bool cycle) { if(t <0.0f || t>1.0f) { printf("ERROR t = [%f] for cycle [%d] f [%d] v [%d]\n",t,cycle,mesh->getFrameCount(),mesh->getVertexCount()); } //assert(t>=0.0f && t<=1.0f); if(t < 0.0f || t > 1.0f) { throw megaglest_runtime_error("t < 0.0f || t > 1.0f t = [" + floatToStr(t,16) + "]"); assert(t >= 0.f && t <= 1.f); } uint32 frameCount= mesh->getFrameCount(); uint32 vertexCount= mesh->getVertexCount(); if(frameCount > 1) { if(enableCache == true) { std::map >::iterator iterFind = cacheVertices.find(t); if(iterFind != cacheVertices.end()) { std::map::iterator iterFind2 = iterFind->second.find(cycle); if(iterFind2 != iterFind->second.end()) { memcpy(vertices,iterFind2->second,sizeof(Vec3f) * vertexCount); return; } } cacheVertices[t][cycle] = new Vec3f[vertexCount]; } const Vec3f *meshVertices= mesh->getVertices(); //misc vars uint32 prevFrame; uint32 nextFrame; float localT; if(cycle == true) { prevFrame= min(static_cast(t*frameCount), frameCount-1); nextFrame= (prevFrame+1) % frameCount; localT= t*frameCount - prevFrame; } else { prevFrame= min (static_cast (t * (frameCount-1)), frameCount - 2); nextFrame= min(prevFrame + 1, frameCount - 1); localT= t * (frameCount-1) - prevFrame; //printf(" prevFrame=%d nextFrame=%d localT=%f\n",prevFrame,nextFrame,localT); } uint32 prevFrameBase= prevFrame*vertexCount; uint32 nextFrameBase= nextFrame*vertexCount; //assertions assert(prevFrame 1.0f) { throw megaglest_runtime_error("t < 0.0f || t > 1.0f t = [" + floatToStr(t,16) + "]"); assert(t>=0.0f && t<=1.0f); } uint32 frameCount= mesh->getFrameCount(); uint32 vertexCount= mesh->getVertexCount(); if(frameCount > 1) { if(enableCache == true) { std::map >::iterator iterFind = cacheNormals.find(t); if(iterFind != cacheNormals.end()) { std::map::iterator iterFind2 = iterFind->second.find(cycle); if(iterFind2 != iterFind->second.end()) { memcpy(normals,iterFind2->second,sizeof(Vec3f) * vertexCount); return; } } cacheNormals[t][cycle] = new Vec3f[vertexCount]; } const Vec3f *meshNormals= mesh->getNormals(); //misc vars uint32 prevFrame; uint32 nextFrame; float localT; if(cycle == true) { prevFrame= min(static_cast(t*frameCount), frameCount-1); nextFrame= (prevFrame+1) % frameCount; localT= t*frameCount - prevFrame; } else { prevFrame= min (static_cast (t * (frameCount-1)), frameCount - 2); nextFrame= min(prevFrame + 1, frameCount - 1); localT= t * (frameCount-1) - prevFrame; //printf(" prevFrame=%d nextFrame=%d localT=%f\n",prevFrame,nextFrame,localT); } uint32 prevFrameBase= prevFrame*vertexCount; uint32 nextFrameBase= nextFrame*vertexCount; //assertions assert(prevFrame