aboutsummaryrefslogtreecommitdiff
path: root/engines/titanic/star_control/star_camera.cpp
blob: 924ffaf153300fa5c59d5c5fcebf69a6e879215c (plain)
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "titanic/star_control/star_camera.h"
#include "titanic/star_control/camera_mover.h"
#include "titanic/star_control/daffine.h"
#include "titanic/star_control/fmatrix.h"
#include "titanic/star_control/fpoint.h"
#include "titanic/star_control/marked_camera_mover.h"
#include "titanic/star_control/unmarked_camera_mover.h"
#include "titanic/star_control/error_code.h"
#include "titanic/support/simple_file.h"
#include "titanic/titanic.h"

namespace Titanic {

FMatrix *CStarCamera::_priorOrientation;
FMatrix *CStarCamera::_newOrientation;

CStarCamera::CStarCamera(const CNavigationInfo *data) :
		_star_lock_state(ZERO_LOCKED), _mover(nullptr), _isMoved(false) {
	setupHandler(data);
}

CStarCamera::CStarCamera(CViewport *src) :
		_star_lock_state(ZERO_LOCKED), _mover(nullptr), _isMoved(false), _viewport(src) {
}

void CStarCamera::init() {
	_priorOrientation = nullptr;
	_newOrientation = nullptr;
}

void CStarCamera::deinit() {
	delete _priorOrientation;
	delete _newOrientation;
	_priorOrientation = nullptr;
	_newOrientation = nullptr;
}

bool CStarCamera::isLocked() { 
       return _mover->isLocked(); 
}

CStarCamera::~CStarCamera() {
	deleteHandler();
}

void CStarCamera::proc2(const CViewport *src) {
	_viewport.copyFrom(src);
}

void CStarCamera::proc3(const CNavigationInfo *src) {
	_mover->copyFrom(src);
}

void CStarCamera::setPosition(const FVector &v) {
	if (!isLocked()) {
		_viewport.setPosition(v);
		setIsMoved();
	}
}

void CStarCamera::setOrientation(const FVector &v) {
	if (!isLocked())
		_viewport.setOrientation(v);
}

void CStarCamera::proc6(int v) {
	if (!isLocked())
		_viewport.setC(v);
}

void CStarCamera::proc7(int v) {
	if (!isLocked())
		_viewport.set10(v);
}

void CStarCamera::proc8(int v) {
	if (!isLocked())
		_viewport.set14(v);
}

void CStarCamera::proc9(int v) {
	if (!isLocked())
		_viewport.set18(v);
}

void CStarCamera::proc10(int v) {
	if (!isLocked())
		_viewport.set1C(v);
}

void CStarCamera::proc11() {
	if (!isLocked())
		_viewport.fn12();
}

void CStarCamera::proc12(StarMode mode, double v2) {
	if (!isLocked())
		_viewport.fn13(mode, v2);
}

void CStarCamera::proc13(CViewport *dest) {
	*dest = _viewport;
}

void CStarCamera::setDestination(const FVector &v) {
	FMatrix orientation = _viewport.getOrientation();
	FVector oldPos = _viewport._position;

	_mover->moveTo(oldPos, v, orientation);
}

void CStarCamera::updatePosition(CErrorCode *errorCode) {
	if (!_priorOrientation)
		_priorOrientation = new FMatrix();
	if (!_newOrientation)
		_newOrientation = new FMatrix();

	*_priorOrientation = _viewport.getOrientation();
	*_newOrientation = *_priorOrientation;

	FVector priorPos = _viewport._position;
	FVector newPos = _viewport._position;
	_mover->updatePosition(*errorCode, newPos, *_newOrientation);

	if (newPos != priorPos) {
		_viewport.setPosition(newPos);
		setIsMoved();
	}

	if (*_priorOrientation != *_newOrientation) {
		_viewport.setOrientation(*_newOrientation);
	}
}

void CStarCamera::increaseForwardSpeed() {
	_mover->increaseForwardSpeed();
}

void CStarCamera::increaseBackwardSpeed() {
	_mover->increaseBackwardSpeed();
}

void CStarCamera::fullSpeed() {
	_mover->fullSpeed();
}

void CStarCamera::stop() {
	_mover->stop();
}

void CStarCamera::reposition(double factor) {
	if (!isLocked())
		_viewport.reposition(factor);
}

void CStarCamera::setPosition(const FPose &pose) {
	if (!isLocked()) {
		_viewport.setPosition(pose);
		setIsMoved();
	}
}

void CStarCamera::proc22(FMatrix &m) {
	if (!isLocked())
		_viewport.fn15(m);
}

FPose CStarCamera::getPose() {
	return _viewport.getPose();
}

FPose CStarCamera::getRawPose() {
	return _viewport.getRawPose();
}

double CStarCamera::getThreshold() const {
	return _viewport._field10;
}

double CStarCamera::proc26() const {
	return _viewport._field14;
}

int CStarCamera::proc27() const {
	return _viewport._field24;
}

FVector CStarCamera::getRelativePos(int index, const FVector &src) {
	FVector dest;

	dest._x = ((_viewport._valArray[index] + src._x) * _viewport._centerVector._x)
		/ (_viewport._centerVector._y * src._z);
	dest._y = src._y * _viewport._centerVector._x / (_viewport._centerVector._z * src._z);
	dest._z = src._z;
	return dest;
}

FVector CStarCamera::proc29(int index, const FVector &src) {
	return _viewport.fn16(index, src);
}

FVector CStarCamera::proc30(int index, const FVector &v) {
	return _viewport.fn17(index, v);
}

FVector CStarCamera::proc31(int index, const FVector &v) {
	return _viewport.fn18(index, v);
}

void CStarCamera::setViewportAngle(const FPoint &angles) {
	debug(DEBUG_DETAILED, "setViewportAngle %f %f", angles._x, angles._y);

	if (isLocked())
		return;
       switch(_star_lock_state) {
	case ZERO_LOCKED: {
		FPose subX(X_AXIS, angles._y);
		FPose subY(Y_AXIS, -angles._x); // needs to be negative or looking left will cause the view to go right
		FPose sub(subX, subY);
		proc22(sub);
       }
              break;

       case ONE_LOCKED: {
		FVector row1 = _matrix._row1;
		FPose poseX(X_AXIS, angles._y);
		FPose poseY(Y_AXIS, -angles._x); // needs to be negative or looking left will cause the view to go right
		FPose pose(poseX, poseY);

		FMatrix m1 = _viewport.getOrientation();
		FVector tempV1 = _viewport._position;
		FVector tempV2 = m1._row1 * 100000;
		FVector tempV3 = tempV2 + tempV1;
		FVector tempV4 = tempV3;

		tempV2 = m1._row2 * 100000;
		FVector tempV5 = m1._row3 * 100000;
		FVector tempV6 = tempV2 + tempV1;

		FVector tempV7 = tempV5 + tempV1;
		tempV5 = tempV6;
		tempV6 = tempV7;

		tempV1 -= row1;
		tempV4 -= row1;
		tempV5 -= row1;
		tempV6 -= row1;

		tempV1 = tempV1.fn5(pose);
		tempV4 = tempV4.fn5(pose);
		tempV5 = tempV5.fn5(pose);
		tempV6 = tempV6.fn5(pose);

		tempV4 -= tempV1;
		tempV5 -= tempV1;
		tempV6 -= tempV1;

              float unused_scale=0.0;
              if (!tempV4.normalize(unused_scale) ||
                  !tempV5.normalize(unused_scale) ||
                  !tempV6.normalize(unused_scale)) {  // Do the normalization, put the scale amount in unused_scale,
                                                      // but if it is unsuccessful, crash
                     assert(unused_scale);
              }

		tempV1 += row1;
		m1.set(tempV4, tempV5, tempV6);
		_viewport.setOrientation(m1);
		_viewport.setPosition(tempV1);
       }
              break;

       case TWO_LOCKED: {
		FVector tempV2;
		DAffine m1, m2, sub;
		DVector mrow1, mrow2, mrow3;
		DVector tempV1, diffV, multV, multV2, tempV3, tempV4, tempV5, tempV6, tempV7;
		DVector tempV8, tempV9, tempV10, tempV11, tempV12;
		DVector tempV13, tempV14, tempV15, tempV16;

		DAffine subX(0, _matrix._row1);
		DAffine subY(Y_AXIS, angles._y);

		tempV1 = _matrix._row2 - _matrix._row1;
		diffV = tempV1;
		m1 = diffV.rotXY();
		m1 = m1.compose(subX);
		subX = m1.inverseTransform();
		subX = subX.compose(subY);

		FMatrix m3 = _viewport.getOrientation();
		tempV2 = _viewport._position;
		multV._x = m3._row1._x * 1000000.0;
		multV._y = m3._row1._y * 1000000.0;
		multV._z = m3._row1._z * 1000000.0;
		tempV3._x = tempV2._x;
		tempV3._y = tempV2._y;
		tempV3._z = tempV2._z;
		multV2._z = m3._row2._z * 1000000.0;

		tempV1._x = multV._x + tempV3._x;
		tempV1._y = multV._y + tempV3._y;
		tempV1._z = multV._z + tempV3._z;
		mrow3._z = 0.0;
		mrow3._y = 0.0;
		mrow3._x = 0.0;
		multV2._x = m3._row2._x * 1000000.0;
		multV2._y = m3._row2._y * 1000000.0;
		mrow1 = tempV1;
		multV = multV2 + tempV3;
		mrow2 = multV;

		tempV7._z = m3._row3._z * 1000000.0 + tempV3._z;
		tempV7._y = m3._row3._y * 1000000.0 + tempV3._y;
		tempV7._x = m3._row3._x * 1000000.0 + tempV3._x;

		mrow3 = tempV8 = tempV7;
		tempV3 = tempV3.dAffMatrixProdVec(subX);
		mrow1 = mrow1.dAffMatrixProdVec(subX);
		mrow2 = mrow2.dAffMatrixProdVec(subX);
		mrow3 = mrow3.dAffMatrixProdVec(subX);

		tempV3 = tempV3.dAffMatrixProdVec(m1);
		mrow1 = mrow1.dAffMatrixProdVec(m1);
		mrow2 = mrow2.dAffMatrixProdVec(m1);
		mrow3 = mrow3.dAffMatrixProdVec(m1);

		mrow1 -= tempV3;
		mrow2 -= tempV3;
		mrow3 -= tempV3;

              double unused_scale=0.0;
              if (!mrow1.normalize(unused_scale) ||
                  !mrow2.normalize(unused_scale) ||
                  !mrow3.normalize(unused_scale)) {  // Do the normalization, put the scale amount in unused_scale,
                                                     // but if it is unsuccessful, crash
                     assert(unused_scale);
              }

		tempV16 = tempV3;

		m3.set(mrow1, mrow2, mrow3);
		_viewport.setOrientation(m3);
		_viewport.setPosition(tempV16);
       }
              break;

       //TODO: should three stars locked do anything in this function? Error?
       case THREE_LOCKED:
              break;
       }
}

bool CStarCamera::addLockedStar(const FVector v) {
	if (_star_lock_state == THREE_LOCKED)
		return false;

	CNavigationInfo data;
	_mover->copyTo(&data);
	deleteHandler();
	FVector &row = _matrix[(int)_star_lock_state];
       _star_lock_state = StarLockState( (int)_star_lock_state + 1);
	row = v;
	setupHandler(&data);
	return true;
}

bool CStarCamera::removeLockedStar() {
	if (_star_lock_state == ZERO_LOCKED)
		return false;

	CNavigationInfo data;
	_mover->copyTo(&data);
	deleteHandler();

	_star_lock_state = StarLockState( (int)_star_lock_state - 1);
	setupHandler(&data);
	return true;
}

void CStarCamera::proc36(double *v1, double *v2, double *v3, double *v4) {
	_viewport.fn19(v1, v2, v3, v4);
}

void CStarCamera::load(SimpleFile *file, int param) {
	_viewport.load(file, param);
}

void CStarCamera::save(SimpleFile *file, int indent) {
	_viewport.save(file, indent);
}

bool CStarCamera::setupHandler(const CNavigationInfo *src) {
	CCameraMover *mover = nullptr;

	switch (_star_lock_state) {
	case ZERO_LOCKED:
		mover = new CUnmarkedCameraMover(src);
		break;

	case ONE_LOCKED:
	case TWO_LOCKED:
	case THREE_LOCKED:
		mover = new CMarkedCameraMover(src);
		break;

	default:
		break;
	}

	if (mover) {
		assert(!_mover);
		_mover = mover;
		return true;
	} else {
		return false;
	}
}

void CStarCamera::deleteHandler() {
	if (_mover) {
		delete _mover;
		_mover = nullptr;
	}
}

void CStarCamera::lockMarker1(FVector v1, FVector v2, FVector v3) {
	if (_star_lock_state != ZERO_LOCKED)
		return;

	FVector tempV;
	double val1, val2, val3, val4, val5;
	double val6, val7, val8, val9;

	val1 = _viewport._centerVector._y * v1._x;
	tempV._z = _viewport._field10;
	val2 = _viewport._centerVector._y * tempV._z * v3._x;
	val3 = _viewport._centerVector._z * v1._y;
	val4 = _viewport._centerVector._z * tempV._z;
	val5 = val1 * v1._z / _viewport._centerVector._x;
	v3._z = v1._z;
	val6 = val4 * v3._y;
	val7 = val3 * v1._z / _viewport._centerVector._x;
	val8 = val6 / _viewport._centerVector._x;
	val9 = val2 / _viewport._centerVector._x;
	v3._x = val5 - _viewport._valArray[2];
	v3._y = val7;
	tempV._x = val9 - _viewport._valArray[2];
	tempV._y = val8;

       float unused_scale=0.0;
       if (!v3.normalize(unused_scale) ||
        !tempV.normalize(unused_scale)) {  // Do the normalization, put the scale amount in unused_scale,
                                           // but if it is unsuccessful, crash
              assert(unused_scale);
       }

	FMatrix matrix = _viewport.getOrientation();
	const FVector &pos = _viewport._position;
	_mover->proc10(v3, tempV, pos, matrix);

	CStarVector *sv = new CStarVector(this, v2);
	_mover->setVector(sv);
}

void CStarCamera::lockMarker2(CViewport *viewport, const FVector &v) {
	if (_star_lock_state != ONE_LOCKED)
		return;

	DAffine m2(X_AXIS, _matrix._row1);
	DVector tempV1 = v - _matrix._row1;
	DAffine m1 = tempV1.rotXY();
	m1 = m1.compose(m2);
	m2 = m1.inverseTransform();
	
	DVector tempV2 = _viewport._position;
	DAffine m4;
	m4._col1 = viewport->_position;
	m4._col2 = DVector(0.0, 0.0, 0.0);
	m4._col3 = DVector(0.0, 0.0, 0.0);
	m4._col4 = DVector(0.0, 0.0, 0.0);

	FMatrix m5 = viewport->getOrientation();
	double yVal1 = m5._row1._y * 1000000.0;
	double zVal1 = m5._row1._z * 1000000.0;
	double xVal1 = m5._row2._x * 1000000.0;
	double yVal2 = m5._row2._y * 1000000.0;
	double zVal2 = m5._row2._z * 1000000.0;
	double zVal3 = zVal1 + m4._col1._z;
	double yVal3 = yVal1 + m4._col1._y;
	double xVal2 = m5._row1._x * 1000000.0 + m4._col1._x;
	double zVal4 = zVal2 + m4._col1._z;
	double yVal4 = yVal2 + m4._col1._y;
	double xVal3 = xVal1 + m4._col1._x;

	DVector tempV4(xVal2, yVal3, zVal3);
	DVector tempV3(xVal3, yVal4, zVal4);
	m4._col3 = tempV4;

	FVector tempV5;
	tempV5._x = m5._row3._x * 1000000.0;
	tempV5._y = m5._row3._y * 1000000.0;
	m4._col2 = tempV3;

	tempV3._x = tempV5._x + m4._col1._x;
	tempV3._y = tempV5._y + m4._col1._y;
	tempV3._z = m5._row3._z * 1000000.0 + m4._col1._z;
	m4._col4 = tempV3;

	tempV2 = tempV2.dAffMatrixProdVec(m2);
	m4._col1 = m4._col1.dAffMatrixProdVec(m2);
	m4._col3 = m4._col3.dAffMatrixProdVec(m2);
	m4._col2 = m4._col2.dAffMatrixProdVec(m2);
	m4._col4 = m4._col4.dAffMatrixProdVec(m2);

	// Find the angle that gives the minimum distance
	DVector tempPos;
	double minDistance = 1.0e20;
	int minDegree = 0;
	for (int degree = 0; degree < 360; ++degree) {
		tempPos = m4._col1;
		tempPos.rotVectAxisY((double)degree);
		double distance = tempV2.getDistance(tempPos);

		if (distance < minDistance) {
			minDistance = distance;
			minDegree = degree;
		}
	}

	m4._col1.rotVectAxisY((double)minDegree);
	m4._col2.rotVectAxisY((double)minDegree);
	m4._col3.rotVectAxisY((double)minDegree);
	m4._col4.rotVectAxisY((double)minDegree);
	m4._col1 = m4._col1.dAffMatrixProdVec(m1);
	m4._col2 = m4._col2.dAffMatrixProdVec(m1);
	m4._col3 = m4._col3.dAffMatrixProdVec(m1);
	m4._col4 = m4._col4.dAffMatrixProdVec(m1);

	m4._col3 -= m4._col1;
	m4._col2 -= m4._col1;
	m4._col4 -= m4._col1;

       FMatrix m6 = _viewport.getOrientation();

       double unused_scale=0.0;
       if (!m4._col2.normalize(unused_scale) ||
           !m4._col3.normalize(unused_scale) ||
           !m4._col4.normalize(unused_scale) ) {  // Do the normalizations, put the scale amount in unused_scale,
                                                  // but if any of the normalizations are unsuccessful, crash
              assert(unused_scale);
       }
       
	m5.set(m4._col3, m4._col2, m4._col4);

	FVector newPos = m4._col1;
	
       if (_viewport._position != newPos) {
              // Only change view if positions are different
	       _mover->proc8(_viewport._position, newPos, m6, m5);
       }
	CStarVector *sv = new CStarVector(this, v);
	_mover->setVector(sv);
}

void CStarCamera::lockMarker3(CViewport *viewport, const FVector &v) {
	if (_star_lock_state != TWO_LOCKED)
		return;

	FMatrix newOr = viewport->getOrientation();
	FMatrix oldOr = _viewport.getOrientation();
	FVector newPos = viewport->_position;
	FVector oldPos = _viewport._position;

       if (oldPos != newPos) {
              // Only change view if positions are different
	       _mover->proc8(oldPos, newPos, oldOr, newOr);
       }

	CStarVector *sv = new CStarVector(this, v);
	_mover->setVector(sv);
}

} // End of namespace Titanic