ScummVM API documentation
xmath.h
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 modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (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, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * Code originated from Wine sources.
24  * Copyright (C) 2008 David Adam
25  * Copyright (C) 2008 Luis Busquets
26  * Copyright (C) 2008 Jérôme Gardou
27  * Copyright (C) 2008 Philip Nilsson
28  * Copyright (C) 2008 Henri Verbeet
29  */
30 
31 #ifndef WINTERMUTE_XMATH_H
32 #define WINTERMUTE_XMATH_H
33 
34 #include "engines/wintermute/math/rect32.h"
35 
36 namespace Wintermute {
37 
38 #if defined(SCUMMVM_USE_PRAGMA_PACK)
39 #pragma pack(4)
40 #endif
41 
42 struct DXVector2 {
43  float _x;
44  float _y;
45 
46  DXVector2() {}
47  DXVector2(float fx, float fy);
48 };
49 
50 struct DXVector3 {
51  float _x;
52  float _y;
53  float _z;
54 
55  DXVector3() {}
56  DXVector3(const float *pf);
57  DXVector3(float fx, float fy, float fz);
58 
59  operator float* ();
60  operator const float* () const;
61 
62  DXVector3 &operator += (const DXVector3 &);
63  DXVector3 &operator -= (const DXVector3 &);
64  DXVector3 &operator *= (float);
65  DXVector3 &operator /= (float);
66 
67  DXVector3 operator + () const;
68  DXVector3 operator - () const;
69 
70  DXVector3 operator + (const DXVector3 &) const;
71  DXVector3 operator - (const DXVector3 &) const;
72  DXVector3 operator * (float) const;
73  DXVector3 operator / (float) const;
74 
75  bool operator == (const DXVector3 &) const;
76  bool operator != (const DXVector3 &) const;
77 };
78 
79 struct DXVector4 {
80  float _x;
81  float _y;
82  float _z;
83  float _w;
84 
85  DXVector4() {}
86  DXVector4(const float *pf);
87  DXVector4(float fx, float fy, float fz, float fw);
88 
89  operator float* ();
90  operator const float* () const;
91 };
92 
93 struct DXQuaternion {
94  float _x;
95  float _y;
96  float _z;
97  float _w;
98 
99  DXQuaternion() {}
100  DXQuaternion(const float *pf);
101  DXQuaternion(float fx, float fy, float fz, float fw);
102 
103  operator float* ();
104  operator const float* () const;
105 };
106 
107 struct DXPlane {
108  float _a;
109  float _b;
110  float _c;
111  float _d;
112 
113  DXPlane() {}
114 };
115 
116 struct DXMatrix {
117  union {
118  struct {
119  float _11, _12, _13, _14;
120  float _21, _22, _23, _24;
121  float _31, _32, _33, _34;
122  float _41, _42, _43, _44;
123  } matrix;
124  float _m[4][4];
125  float _m4x4[16];
126  };
127 
128  DXMatrix() {}
129  DXMatrix(const float *pf);
130 
131  float &operator () (uint32 row, uint32 col);
132 
133  operator float* ();
134  operator const float* () const;
135 
136  DXMatrix operator * (const DXMatrix &) const;
137 };
138 
139 struct DXViewport {
140  uint32 _x;
141  uint32 _y;
142  uint32 _width;
143  uint32 _height;
144  float _minZ;
145  float _maxZ;
146 };
147 
148 #if defined(SCUMMVM_USE_PRAGMA_PACK)
149 #pragma pack()
150 #endif
151 
152 DXQuaternion *DXQuaternionRotationMatrix(DXQuaternion *out,const DXMatrix *m);
153 DXMatrix *DXMatrixPerspectiveFovLH(DXMatrix *pout,float fovy, float aspect, float zn, float zf);
154 DXMatrix *DXMatrixPerspectiveFovRH(DXMatrix *pout, float fovy, float aspect, float zn, float zf);
155 DXMatrix *DXMatrixLookAtLH(DXMatrix *out, const DXVector3 *eye, const DXVector3 *at, const DXVector3 *up);
156 DXMatrix *DXMatrixLookAtRH(DXMatrix *out, const DXVector3 *eye, const DXVector3 *at, const DXVector3 *up);
157 DXMatrix *DXMatrixOrthoLH(DXMatrix *pout, float w, float h, float zn, float zf);
158 DXMatrix *DXMatrixOrthoOffCenterLH(DXMatrix *pout, float l, float r, float b, float t, float zn, float zf);
159 DXMatrix *DXMatrixInverse(DXMatrix *pout, float *pdeterminant, const DXMatrix *pm);
160 DXPlane *DXPlaneFromPointNormal(DXPlane *pout, const DXVector3 *pvpoint, const DXVector3 *pvnormal);
161 DXPlane *DXPlaneFromPoints(DXPlane *pout, const DXVector3 *pv1, const DXVector3 *pv2, const DXVector3 *pv3);
162 DXVector3 *DXPlaneIntersectLine(DXVector3 *pout, const DXPlane *pp, const DXVector3 *pv1, const DXVector3 *pv2);
163 DXVector3 *DXVec3Normalize(DXVector3 *pout, const DXVector3 *pv);
164 DXMatrix *DXMatrixTranslation(DXMatrix *pout, float x, float y, float z);
165 DXMatrix *DXMatrixScaling(DXMatrix *pout, float sx, float sy, float sz);
166 DXMatrix *DXMatrixRotationY(DXMatrix *pout, float angle);
167 DXMatrix *DXMatrixRotationZ(DXMatrix *pout, float angle);
168 DXMatrix *DXMatrixRotationYawPitchRoll(DXMatrix *out, float yaw, float pitch, float roll);
169 DXMatrix *DXMatrixRotationQuaternion(DXMatrix *pout, const DXQuaternion *pq);
170 DXQuaternion *DXQuaternionSlerp(DXQuaternion *out, const DXQuaternion *q1, const DXQuaternion *q2, float t);
171 DXVector4 *DXVec3Transform(DXVector4 *pout, const DXVector3 *pv, const DXMatrix *pm);
172 DXVector3 *DXVec3TransformCoord(DXVector3 *pout, const DXVector3 *pv, const DXMatrix *pm);
173 DXVector3 *DXVec3TransformNormal(DXVector3 *pout, const DXVector3 *pv, const DXMatrix *pm);
174 DXMatrix *DXMatrixMultiply(DXMatrix *pout, const DXMatrix *pm1, const DXMatrix *pm2);
175 DXVector3 *DXVec3Project(DXVector3 *pout, const DXVector3 *pv, const DXViewport *pviewport,
176  const DXMatrix *pprojection, const DXMatrix *pview, const DXMatrix *pworld);
177 DXMatrix *DXMatrixTranspose(DXMatrix *pout, const DXMatrix *pm);
178 DXVector4 *DXVec4Transform(DXVector4 *pout, const DXVector4 *pv, const DXMatrix *pm);
179 
180 static inline DXMatrix *DXMatrixIdentity(DXMatrix *pout) {
181  (*pout)._m[0][1] = 0.0f;
182  (*pout)._m[0][2] = 0.0f;
183  (*pout)._m[0][3] = 0.0f;
184  (*pout)._m[1][0] = 0.0f;
185  (*pout)._m[1][2] = 0.0f;
186  (*pout)._m[1][3] = 0.0f;
187  (*pout)._m[2][0] = 0.0f;
188  (*pout)._m[2][1] = 0.0f;
189  (*pout)._m[2][3] = 0.0f;
190  (*pout)._m[3][0] = 0.0f;
191  (*pout)._m[3][1] = 0.0f;
192  (*pout)._m[3][2] = 0.0f;
193  (*pout)._m[0][0] = 1.0f;
194  (*pout)._m[1][1] = 1.0f;
195  (*pout)._m[2][2] = 1.0f;
196  (*pout)._m[3][3] = 1.0f;
197  return pout;
198 }
199 
200 static inline DXVector3 *DXVec3Lerp(DXVector3 *pout, const DXVector3 *pv1, const DXVector3 *pv2, float s) {
201  pout->_x = (1-s) * (pv1->_x) + s * (pv2->_x);
202  pout->_y = (1-s) * (pv1->_y) + s * (pv2->_y);
203  pout->_z = (1-s) * (pv1->_z) + s * (pv2->_z);
204  return pout;
205 }
206 
207 static inline float DXQuaternionDot(const DXQuaternion *pq1, const DXQuaternion *pq2) {
208  return (pq1->_x) * (pq2->_x) + (pq1->_y) * (pq2->_y) + (pq1->_z) * (pq2->_z) + (pq1->_w) * (pq2->_w);
209 }
210 
211 static inline DXVector3 *DXVec3Cross(DXVector3 *pout, const DXVector3 *pv1, const DXVector3 *pv2) {
212  DXVector3 temp;
213 
214  temp._x = (pv1->_y) * (pv2->_z) - (pv1->_z) * (pv2->_y);
215  temp._y = (pv1->_z) * (pv2->_x) - (pv1->_x) * (pv2->_z);
216  temp._z = (pv1->_x) * (pv2->_y) - (pv1->_y) * (pv2->_x);
217  *pout = temp;
218  return pout;
219 }
220 
221 static inline float DXVec3Dot(const DXVector3 *pv1, const DXVector3 *pv2) {
222  return (pv1->_x) * (pv2->_x) + (pv1->_y) * (pv2->_y) + (pv1->_z) * (pv2->_z);
223 }
224 
225 static inline DXVector3 *DXVec3Subtract(DXVector3 *pout, const DXVector3 *pv1, const DXVector3 *pv2) {
226  pout->_x = pv1->_x - pv2->_x;
227  pout->_y = pv1->_y - pv2->_y;
228  pout->_z = pv1->_z - pv2->_z;
229  return pout;
230 }
231 
232 static inline float DXVec3Length(const DXVector3 *pv) {
233  return sqrtf(pv->_x * pv->_x + pv->_y * pv->_y + pv->_z * pv->_z);
234 }
235 
236 } // End of namespace Wintermute
237 
238 #endif
Definition: xmath.h:50
Definition: xmath.h:42
Definition: xmath.h:139
Definition: xmath.h:107
Definition: xmath.h:116
Definition: xmath.h:93
Definition: xmath.h:79
Definition: achievements_tables.h:27