-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassetghost.cpp
More file actions
367 lines (311 loc) · 12.5 KB
/
assetghost.cpp
File metadata and controls
367 lines (311 loc) · 12.5 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "assetghost.h"
AssetGhost::AssetGhost() :
secs_per_frame(0.2f)
{
SetS("_type", "assetghost");
}
AssetGhost::~AssetGhost()
{
}
void AssetGhost::Load()
{
WebAsset::Load(QUrl(GetS("_src_url")));
}
void AssetGhost::Unload()
{
frames.clear();
WebAsset::Unload();
}
void AssetGhost::SetFromFrames(const QVector <GhostFrame> & ghost_frames, const float secs_between_frames)
{
if (frames.size() == 2 && ghost_frames.size() == 2) {
QList <QString> unprocessed_edits = (frames[1].room_edits + ghost_frames[1].room_edits);
QList <QString> unprocessed_deletes = (frames[1].room_deletes + ghost_frames[1].room_deletes);
frames = ghost_frames;
frames[1].room_edits = unprocessed_edits;
frames[1].room_deletes = unprocessed_deletes;
}
else {
frames = ghost_frames;
}
secs_per_frame = secs_between_frames;
SetLoaded(true);
SetProcessed(true);
SetFinished(true);
}
void AssetGhost::LoadDataThread()
{
// qDebug() << "AssetGhost::LoadDataThread()" << url;
const QByteArray & ba = GetData();
QTextStream ifs(ba);
frames.clear();
while (!ifs.atEnd()) {
const QString line = ifs.readLine();
if (line.isEmpty()) {
continue;
}
else if (line[0] == '{') {
QVariantMap m = QJsonDocument::fromJson(line.toLatin1()).object().toVariantMap();
//convert JSON packet o into a GhostFrame
GhostFrame f;
AssetGhost::ConvertPacketToFrame(m, f);
if (f.chat_message.length() > 0 && !frames.empty()) {
frames.last().chat_message = f.chat_message;
}
else if (f.send_portal_url.length() > 0 && !frames.empty()) {
frames.last().send_portal_url = f.send_portal_url;
frames.last().send_portal_pos = f.send_portal_pos;
frames.last().send_portal_fwd = f.send_portal_fwd;
frames.last().send_portal_jsid = f.userid + "-" + f.send_portal_url;
}
else {
f.time_sec = float(frames.size())*secs_per_frame;
frames.push_back(f);
}
}
else {
secs_per_frame = 0.1f;
QStringList eachline = line.split(" ");
if (QString::compare(eachline.first(), "CHAT") == 0) {
if (!frames.empty()) {
for (int i=1; i<eachline.size(); ++i) {
frames.last().chat_message += QString(" ") + eachline[i];
}
frames.last().chat_message = frames.last().chat_message.trimmed();
}
}
else if (eachline.size() == 13 || eachline.size() == 16) {
GhostFrame frame;
frame.time_sec = float(frames.size())*secs_per_frame;
frame.pos = QVector3D(eachline[1].toFloat(), eachline[2].toFloat(), eachline[3].toFloat());
frame.dir = QVector3D(eachline[4].toFloat(), eachline[5].toFloat(), eachline[6].toFloat());
frame.head_xform.setColumn(2, QVector3D(eachline[7].toFloat(), eachline[8].toFloat(), eachline[9].toFloat()));
frame.head_xform.setColumn(1, QVector3D(eachline[10].toFloat(), eachline[11].toFloat(), eachline[12].toFloat()));
if (eachline.size() >= 16) {
frame.head_xform.setColumn(3, QVector4D(eachline[13].toFloat(), eachline[14].toFloat(), eachline[15].toFloat(), 1));
}
frame.head_xform.setColumn(0, QVector3D::crossProduct(frame.head_xform.column(1).toVector3D(), frame.head_xform.column(2).toVector3D()).normalized());
frames.push_back(frame);
}
}
}
// qDebug() << "AssetGhost::LoadDataThread() - Loaded frames/packets:" << frames.size() << packets.size();
SetProcessed(true);
ClearData();
}
void AssetGhost::ClearFrames()
{
frames.clear();
}
int AssetGhost::GetFrameIndex(const float time_sec)
{
const int i = int(time_sec/secs_per_frame);
if (i < 0 || i >= frames.size()) {
return -1;
}
return i;
}
GhostFrame & AssetGhost::GetFrameByIndex(const int i)
{
if (i >= 0 && i<frames.size()) {
return frames[i];
}
else {
qDebug() << "AssetGhost::GetFrameByIndex() - Warning invalid frame index" << i << "of" << frames.size();
frames.push_back(GhostFrame());
return frames.last();
}
}
int AssetGhost::GetNumFrames() const
{
return frames.size();
}
void AssetGhost::ClearEditsDeletes()
{
for (int i=0; i<frames.size(); ++i) {
frames[i].room_edits.clear();
frames[i].room_deletes.clear();
}
}
bool AssetGhost::GetGhostFrame(const float time_sec, GhostFrame & frame)
{
if (frames.empty()) {
frame = GhostFrame();
return true;
}
const int i1 = int(time_sec/secs_per_frame);
const int i2 = i1+1;
if (i1 < 0 || i1 >= frames.size()) {
frame = frames.first();
return true;
}
else if (i2 < 0 || i2 >= frames.size()) {
frame = frames.last();
return true;
}
else {
//interpolate between these two
frame = frames[i2];
const float interval = (frames[i2].time_sec - frames[i1].time_sec);
const float t = ((interval > 0.0f) ? ((time_sec - frames[i1].time_sec) / interval) : 1.0f); //49.12 - fixes NaN error
frame.pos = frames[i1].pos * (1.0f - t) + frames[i2].pos * t;
frame.dir = (frames[i1].dir * (1.0f - t) + frames[i2].dir * t).normalized();
frame.head_xform = MathUtil::InterpolateMatrices(frames[i1].head_xform, frames[i2].head_xform, t);
frame.cursor_active = (frames[i1].cursor_active && frames[i2].cursor_active);
if (frame.cursor_active) {
frame.cursor_xform = MathUtil::InterpolateMatrices(frames[i1].cursor_xform, frames[i2].cursor_xform, t);
frame.cscale = frames[i1].cscale * (1.0f - t) + frames[i2].cscale * t;
}
frame.hands.first = LeapHand::Interpolate(frames[i1].hands.first, frames[i2].hands.first, t);
frame.hands.second = LeapHand::Interpolate(frames[i1].hands.second, frames[i2].hands.second, t);
frame.current_sound_level = frames[i1].current_sound_level * (1.0f - t) + frames[i2].current_sound_level * t;
return false;
}
}
void AssetGhost::Update()
{
if (GetLoaded() && !GetProcessing()) {
SetProcessing(true);
QtConcurrent::run(this, &AssetGhost::LoadDataThread);
}
}
void AssetGhost::ConvertPacketToFrame(const QVariantMap & map, GhostFrame & frame)
{
QVariantMap m = map;
frame.userid = m["userId"].toString();
frame.roomid = m["roomId"].toString();
// qDebug() << "AssetGhost::ConvertPacketToFrame()" << frame.userid << frame.roomid;
if (QString::compare(m["method"].toString(), "chat") == 0) {
frame.chat_message = m["data"].toString();
return;
}
else if (QString::compare(m["method"].toString(), "portal") == 0) {
if (m.contains("data")) {
m = m["data"].toMap();
}
frame.send_portal_url = m["url"].toString();
QStringList list = m["pos"].toString().split(" ");
if (list.size() >= 3) {
frame.send_portal_pos = QVector3D(list[0].toFloat(), list[1].toFloat(), list[2].toFloat());
}
list = m["fwd"].toString().split(" ");
if (list.size() >= 3) {
frame.send_portal_fwd = QVector3D(list[0].toFloat(), list[1].toFloat(), list[2].toFloat());
}
return;
}
if (m.contains("data")) {
m = m["data"].toMap();
}
else if (m.contains("position")) {
m = m["position"].toMap();
}
else {
return;
}
if (m.contains("pos")) {
QStringList list = m["pos"].toString().split(" ");
if (list.size() >= 3) {
frame.pos = QVector3D(list[0].toFloat(), list[1].toFloat(), list[2].toFloat());
}
}
if (m.contains("dir")) {
QStringList list = m["dir"].toString().split(" ");
if (list.size() >= 3) {
frame.dir = QVector3D(list[0].toFloat(), list[1].toFloat(), list[2].toFloat());
}
}
if (m.contains("view_dir")) {
QStringList list = m["view_dir"].toString().split(" ");
if (list.size() >= 3) {
frame.head_xform.setColumn(2, QVector3D(list[0].toFloat(), list[1].toFloat(), list[2].toFloat()));
}
}
if (m.contains("up_dir")) {
QStringList list = m["up_dir"].toString().split(" ");
if (list.size() >= 3) {
frame.head_xform.setColumn(1, QVector3D(list[0].toFloat(), list[1].toFloat(), list[2].toFloat()));
}
}
if (m.contains("head_pos")) {
QStringList list = m["head_pos"].toString().split(" ");
if (list.size() >= 3) {
frame.head_xform.setColumn(3, QVector4D(list[0].toFloat(), list[1].toFloat(), list[2].toFloat(), 1));
}
}
frame.head_xform.setColumn(0, QVector3D::crossProduct(frame.head_xform.column(1).toVector3D(), frame.head_xform.column(2).toVector3D()).normalized());
if (m.contains("hmd_type")) {
frame.hmd_type = m["hmd_type"].toString();
}
//hacky thing so that animations for portal spawning and jumping, which are time-based, work
if (m.contains("anim_id")) {
frame.anim_id = m["anim_id"].toString();
}
if (m.contains("cpos")) {
frame.cursor_active = true;
frame.cursor_xform.setColumn(3, QVector4D(MathUtil::GetStringAsVector(m["cpos"].toString()), 1));
}
if (m.contains("cxdir")) {
frame.cursor_xform.setColumn(0, MathUtil::GetStringAsVector(m["cxdir"].toString()));
}
if (m.contains("cydir")) {
frame.cursor_xform.setColumn(1, MathUtil::GetStringAsVector(m["cydir"].toString()));
}
if (m.contains("czdir")) {
frame.cursor_xform.setColumn(2, MathUtil::GetStringAsVector(m["czdir"].toString()));
}
if (m.contains("cscale")) {
frame.cscale = m["cscale"].toString().toFloat();
}
if (m.contains("speaking")) {
frame.speaking = true;
}
if (m.contains("audio")) {
frame.sound_buffers.push_back(QByteArray::fromBase64(m["audio"].toByteArray())); //Old
}
if (m.contains("audio_opus")) {
QByteArray encoded = QByteArray::fromBase64(m["audio_opus"].toByteArray());
QByteArray decoded = AudioUtil::decode(encoded);
//qDebug() << "ENCODED MICBUFFER" << encoded.size(); // << compressed_buffer; // << mic_buffers.first().toBase64();
//qDebug() << "DECODED MICBUFFER" << AudioUtil::decode(encoded).size() << "\n";
frame.sound_buffers.push_back(decoded);
frame.current_sound_level = MathUtil::GetSoundLevel(decoded);
//frame.sound_buffers.push_back(QByteArray::fromBase64(m["audio"].toByteArray())); //Old
}
int i=0;
while (m.contains("audio_opus"+QString::number(i)) && i < 30) {
QByteArray encoded = QByteArray::fromBase64(m["audio_opus"+QString::number(i)].toByteArray());
QByteArray decoded = AudioUtil::decode(encoded);
//qDebug() << "ENCODED MICBUFFER" << encoded.size(); // << compressed_buffer; // << mic_buffers.first().toBase64();
//qDebug() << "DECODED MICBUFFER" << decoded.size() << "\n";
frame.sound_buffers.push_back(decoded);
frame.current_sound_level = MathUtil::GetSoundLevel(decoded);
++i;
}
if (m.contains("typing")) {
frame.typing = true;
}
if (m.contains("room_edit")) {
// qDebug() << "AssetGhost::ConvertPacketToFrame INCOMING ROOM EDIT" << m["room_edit"].toString();
frame.room_edits.push_back(MathUtil::DecodeString(m["room_edit"].toString()));
frame.editing = true;
}
if (m.contains("room_delete")) {
// qDebug() << "AssetGhost::ConvertPacketToFrame - INCOMING ROOM DELETE" << MathUtil::DecodeString(m["room_delete"].toString());
frame.room_deletes.push_back(MathUtil::DecodeString(m["room_delete"].toString()));
}
if (m.contains("hand0")) {
QVariantMap hand_map = m["hand0"].toMap();
frame.hands.first.SetJSON( hand_map);
frame.hands.first.is_active = true;
}
if (m.contains("hand1")) {
QVariantMap hand_map = m["hand1"].toMap();
frame.hands.second.SetJSON( hand_map);
frame.hands.second.is_active = true;
}
//we should make the assetghost from the custom data, if we have it
if (m.contains("avatar")) {
frame.avatar_data = MathUtil::DecodeString(m["avatar"].toString());
}
}