1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "bladerunner/view.h"
24 
25 #include "common/debug.h"
26 #include "common/stream.h"
27 #include "common/util.h"
28 
29 namespace BladeRunner {
30 
readVqa(Common::ReadStream * stream)31 bool View::readVqa(Common::ReadStream *stream) {
32 	_frame = stream->readUint32LE();
33 
34 	float d[12];
35 	for (int i = 0; i != 12; ++i) {
36 		d[i] = stream->readFloatLE();
37 	}
38 
39 	_frameViewMatrix = Matrix4x3(d);
40 
41 	float fovX = stream->readFloatLE();
42 
43 	setFovX(fovX);
44 	calculateSliceViewMatrix();
45 	calculateCameraPosition();
46 
47 	return true;
48 }
49 
setFovX(float fovX)50 void View::setFovX(float fovX) {
51 	_fovX = fovX;
52 
53 	_viewportPosition.x = 320.0f;
54 	_viewportPosition.y = 240.0f;
55 	_viewportPosition.z = 320.0f / tan(_fovX / 2.0f);
56 }
57 
calculateSliceViewMatrix()58 void View::calculateSliceViewMatrix() {
59 	Matrix4x3 mRotation = rotationMatrixX(float(M_PI) / 2.0f);
60 	Matrix4x3 mInvert(-1.0f,  0.0f, 0.0f, 0.0f,
61 	                   0.0f, -1.0f, 0.0f, 0.0f,
62 	                   0.0f,  0.0f, 1.0f, 0.0f);
63 	_sliceViewMatrix = mInvert * (_frameViewMatrix * mRotation);
64 }
65 
calculateCameraPosition()66 void View::calculateCameraPosition() {
67 	Matrix4x3 invertedMatrix = invertMatrix(_sliceViewMatrix);
68 
69 	_cameraPosition.x = invertedMatrix(0, 3);
70 	_cameraPosition.y = invertedMatrix(1, 3);
71 	_cameraPosition.z = invertedMatrix(2, 3);
72 }
73 
calculateScreenPosition(Vector3 worldPosition)74 Vector3 View::calculateScreenPosition(Vector3 worldPosition) {
75 	Vector3 viewPosition = _frameViewMatrix * worldPosition;
76 	return Vector3(
77 		_viewportPosition.x - ((viewPosition.x / ABS(viewPosition.z)) * ABS(_viewportPosition.z)),
78 		_viewportPosition.y - ((viewPosition.y / ABS(viewPosition.z)) * ABS(_viewportPosition.z)),
79 		viewPosition.z
80 	);
81 }
82 
83 } // End of namespace BladeRunner
84