aboutsummaryrefslogtreecommitdiff
path: root/src/animation/AnimBlendSequence.cpp
blob: 2ae150c1288a854d1480a6e8407b9c1f6c6a61d6 (plain) (blame)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "common.h"

#include "AnimBlendSequence.h"
#include "MemoryHeap.h"

CAnimBlendSequence::CAnimBlendSequence(void)
{
	type = 0;
	numFrames = 0;
	keyFrames = nil;
	keyFramesCompressed = nil;
#ifdef PED_SKIN
	boneTag = -1;
#endif
}

CAnimBlendSequence::~CAnimBlendSequence(void)
{
	if(keyFrames)
		RwFree(keyFrames);
	if(keyFramesCompressed)
		RwFree(keyFramesCompressed);
}

void
CAnimBlendSequence::SetName(char *name)
{
	strncpy(this->name, name, 24);
}

void
CAnimBlendSequence::SetNumFrames(int numFrames, bool translation)
{
	int sz;

	if(translation){
		sz = sizeof(KeyFrameTrans);
		type |= KF_ROT | KF_TRANS;
	}else{
		sz = sizeof(KeyFrame);
		type |= KF_ROT;
	}
	keyFrames = RwMalloc(sz * numFrames);
	this->numFrames = numFrames;
}

void
CAnimBlendSequence::RemoveQuaternionFlips(void)
{
	int i;
	CQuaternion last;
	KeyFrame *frame;

	if(numFrames < 2)
		return;

	frame = GetKeyFrame(0);
	last = frame->rotation;
	for(i = 1; i < numFrames; i++){
		frame = GetKeyFrame(i);
		if(DotProduct(last, frame->rotation) < 0.0f)
			frame->rotation = -frame->rotation;
		last = frame->rotation;
	}
}

void
CAnimBlendSequence::Uncompress(void)
{
	int i;

	if(numFrames == 0)
		return;

	PUSH_MEMID(MEMID_ANIMATION);

	float rotScale = 1.0f/4096.0f;
	float timeScale = 1.0f/60.0f;
	float transScale = 1.0f/128.0f;
	if(type & KF_TRANS){
		void *newKfs = RwMalloc(numFrames * sizeof(KeyFrameTrans));
		KeyFrameTransCompressed *ckf = (KeyFrameTransCompressed*)keyFramesCompressed;
		KeyFrameTrans *kf = (KeyFrameTrans*)newKfs;
		for(i = 0; i < numFrames; i++){
			kf->rotation.x = ckf->rot[0]*rotScale;
			kf->rotation.y = ckf->rot[1]*rotScale;
			kf->rotation.z = ckf->rot[2]*rotScale;
			kf->rotation.w = ckf->rot[3]*rotScale;
			kf->deltaTime = ckf->deltaTime*timeScale;
			kf->translation.x = ckf->trans[0]*transScale;
			kf->translation.y = ckf->trans[1]*transScale;
			kf->translation.z = ckf->trans[2]*transScale;
			kf++;
			ckf++;
		}
		keyFrames = newKfs;
	}else{
		void *newKfs = RwMalloc(numFrames * sizeof(KeyFrame));
		KeyFrameCompressed *ckf = (KeyFrameCompressed*)keyFramesCompressed;
		KeyFrame *kf = (KeyFrame*)newKfs;
		for(i = 0; i < numFrames; i++){
			kf->rotation.x = ckf->rot[0]*rotScale;
			kf->rotation.y = ckf->rot[1]*rotScale;
			kf->rotation.z = ckf->rot[2]*rotScale;
			kf->rotation.w = ckf->rot[3]*rotScale;
			kf->deltaTime = ckf->deltaTime*timeScale;
			kf++;
			ckf++;
		}
		keyFrames = newKfs;
	}
	REGISTER_MEMPTR(&keyFrames);

	RwFree(keyFramesCompressed);
	keyFramesCompressed = nil;

	POP_MEMID();
}

void
CAnimBlendSequence::CompressKeyframes(void)
{
	int i;

	if(numFrames == 0)
		return;

	PUSH_MEMID(MEMID_ANIMATION);

	float rotScale = 4096.0f;
	float timeScale = 60.0f;
	float transScale = 128.0f;
	if(type & KF_TRANS){
		void *newKfs = RwMalloc(numFrames * sizeof(KeyFrameTransCompressed));
		KeyFrameTransCompressed *ckf = (KeyFrameTransCompressed*)newKfs;
		KeyFrameTrans *kf = (KeyFrameTrans*)keyFrames;
		for(i = 0; i < numFrames; i++){
			ckf->rot[0] = kf->rotation.x*rotScale;
			ckf->rot[1] = kf->rotation.y*rotScale;
			ckf->rot[2] = kf->rotation.z*rotScale;
			ckf->rot[3] = kf->rotation.w*rotScale;
			ckf->deltaTime = kf->deltaTime*timeScale + 0.5f;
			ckf->trans[0] = kf->translation.x*transScale;
			ckf->trans[1] = kf->translation.y*transScale;
			ckf->trans[2] = kf->translation.z*transScale;
			kf++;
			ckf++;
		}
		keyFramesCompressed = newKfs;
	}else{
		void *newKfs = RwMalloc(numFrames * sizeof(KeyFrameCompressed));
		KeyFrameCompressed *ckf = (KeyFrameCompressed*)newKfs;
		KeyFrame *kf = (KeyFrame*)keyFrames;
		for(i = 0; i < numFrames; i++){
			ckf->rot[0] = kf->rotation.x*rotScale;
			ckf->rot[1] = kf->rotation.y*rotScale;
			ckf->rot[2] = kf->rotation.z*rotScale;
			ckf->rot[3] = kf->rotation.w*rotScale;
			ckf->deltaTime = kf->deltaTime*timeScale + 0.5f;
			kf++;
			ckf++;
		}
		keyFramesCompressed = newKfs;
	}
	REGISTER_MEMPTR(&keyFramesCompressed);

	POP_MEMID();
}

void
CAnimBlendSequence::RemoveUncompressedData(void)
{
	if(numFrames == 0)
		return;
	CompressKeyframes();
	RwFree(keyFrames);
	keyFrames = nil;
}

#ifdef USE_CUSTOM_ALLOCATOR
bool
CAnimBlendSequence::MoveMemory(void)
{
	if(keyFrames){
		void *newaddr = gMainHeap.MoveMemory(keyFrames);
		if(newaddr != keyFrames){
			keyFrames = newaddr;
			return true;
		}
	}else if(keyFramesCompressed){
		void *newaddr = gMainHeap.MoveMemory(keyFramesCompressed);
		if(newaddr != keyFramesCompressed){
			keyFramesCompressed = newaddr;
			return true;
		}
	}
	return false;
}
#endif