File: interpolation.cpp

package info (click to toggle)
megaglest 3.13.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 13,416 kB
  • sloc: cpp: 144,271; ansic: 11,861; sh: 3,233; perl: 1,904; python: 1,751; objc: 142; asm: 42; makefile: 22
file content (121 lines) | stat: -rw-r--r-- 3,243 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// ==============================================================
//	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 <cassert>
#include <algorithm>

#include "model.h"
#include "conversion.h"
#include "util.h"
#include <stdexcept>
#include "platform_util.h"
#include "leak_dumper.h"

using namespace std;
using namespace Shared::Util;

namespace Shared{ namespace Graphics{

// =====================================================
//	class InterpolationData
// =====================================================

bool InterpolationData::enableInterpolation = true;

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;
	
	raw_frame_ofs = 0;
	
	this->mesh= mesh;
}

InterpolationData::~InterpolationData(){
	delete [] vertices;
	vertices=NULL;
	delete [] normals;
	normals=NULL;
}

void InterpolationData::update(float t, bool cycle){
	updateVertices(t, cycle);
	updateNormals(t, cycle);
}

void InterpolationData::updateVertices(float t, bool cycle) {
	update(mesh->getVertices(), vertices, t, cycle);
}

void InterpolationData::updateNormals(float t, bool cycle) {
	update(mesh->getNormals(), normals, t, cycle);
}

void InterpolationData::update(const Vec3f* src, Vec3f* &dest, 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) {
		//misc vars
		uint32 prevFrame;
		uint32 nextFrame;
		float localT;

		if(cycle == true) {
			prevFrame= min<uint32>(static_cast<uint32>(t*frameCount), frameCount-1);
			nextFrame= (prevFrame+1) % frameCount;
			localT= t*frameCount - prevFrame;
		}
		else {
			prevFrame= min<uint32> (static_cast<uint32> (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<frameCount);
		assert(nextFrame<frameCount);
		
		if(enableInterpolation) {
			if(!dest) { // not previously allocated
			      dest = new Vec3f[vertexCount];
			}
			for(uint32 j=0; j<vertexCount; ++j){
				dest[j]= src[prevFrameBase+j].lerp(localT, src[nextFrameBase+j]);
			}
		} else {
			raw_frame_ofs = prevFrameBase;
		}
	}
}

}}//end namespace