1 /* ResidualVM - A 3D game interpreter
2 *
3 * ResidualVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the AUTHORS
5 * file distributed with this source distribution.
6 *
7 * Additional copyright for this file:
8 * Copyright (C) 1999-2000 Revolution Software Ltd.
9 * This code is based on source code created by Revolution Software,
10 * used with permission.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 *
26 */
27
28 #include "engines/icb/p4_generic_pc.h"
29
30 namespace ICB {
31
pcApplyMatrixLV(MATRIX * m,VECTOR * invec,VECTOR * outvec)32 inline void pcApplyMatrixLV(MATRIX *m, VECTOR *invec, VECTOR *outvec) {
33 outvec->vx = ((int32)m->m[0][0] * invec->vx + (int32)m->m[0][1] * invec->vy + (int32)m->m[0][2] * invec->vz) >> 12;
34 outvec->vy = ((int32)m->m[1][0] * invec->vx + (int32)m->m[1][1] * invec->vy + (int32)m->m[1][2] * invec->vz) >> 12;
35 outvec->vz = ((int32)m->m[2][0] * invec->vx + (int32)m->m[2][1] * invec->vy + (int32)m->m[2][2] * invec->vz) >> 12;
36 }
37
WorldToFilm(const PXvector_PC & worldpos,const PCcamera & camera,bool8 & is_onfilm,PXvector_PC & filmpos)38 void WorldToFilm(const PXvector_PC &worldpos, const PCcamera &camera, bool8 &is_onfilm, PXvector_PC &filmpos) {
39 VECTOR scrn;
40 VECTOR vWorldPos;
41 vWorldPos.vx = (int32)worldpos.x;
42 vWorldPos.vy = (int32)worldpos.y;
43 vWorldPos.vz = (int32)worldpos.z;
44
45 pcApplyMatrixLV(const_cast<MATRIX *>(&camera.view), &vWorldPos, &scrn);
46 scrn.vx += camera.view.t[0];
47 scrn.vy += camera.view.t[1];
48 scrn.vz += camera.view.t[2];
49
50 if (scrn.vz) {
51 scrn.vx = camera.focLen * scrn.vx / scrn.vz;
52 scrn.vy = camera.focLen * scrn.vy / scrn.vz;
53 }
54 filmpos.x = (float)scrn.vx;
55 filmpos.y = (float)-scrn.vy; // px convention is upside down y
56 filmpos.z = (float)-scrn.vz / 4.0f; // px convention is -ve z : PSX also has zscale * 4 to make it more accurate
57
58 if ((scrn.vx < -SCREEN_WIDTH / 2) || (scrn.vx > SCREEN_WIDTH / 2) || (scrn.vy < -SCREEN_DEPTH / 2) || (scrn.vy > SCREEN_DEPTH / 2))
59 is_onfilm = FALSE8;
60 else
61 is_onfilm = TRUE8;
62 }
63
AngleOfVector(PXreal x,PXreal y)64 PXreal AngleOfVector(PXreal x, PXreal y) {
65 if (fabs(x) > 0.0001f) { /*smallest value safe for atan2*/
66 return (float)atan2(y, x) / (float)M_PI / 2;
67 } else {
68 // atan2's y/x would be infinite, so treat as special case
69 if (y > 0.0f)
70 return (0.5f /*half turn*/ / 2);
71 else
72 return (-0.5f /*half turn*/ / 2);
73 }
74 }
75
76 } // End of namespace ICB
77