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 "common/scummsys.h"
35
36namespace Wintermute {
37
38#if defined(SCUMMVM_USE_PRAGMA_PACK)
39#pragma pack(4)
40#endif
41
42struct DXVector2 {
43 float _x;
44 float _y;
45
46 DXVector2();
47 DXVector2(const float *pf);
48 DXVector2(float fx, float fy);
49
50 operator float* ();
51 operator const float* () const;
52
53 DXVector2 &operator += (const DXVector2 &);
54 DXVector2 &operator -= (const DXVector2 &);
55 DXVector2 &operator *= (float);
56 DXVector2 &operator /= (float);
57
58 DXVector2 operator + () const;
59 DXVector2 operator - () const;
60
61 DXVector2 operator + (const DXVector2 &) const;
62 DXVector2 operator - (const DXVector2 &) const;
63 DXVector2 operator * (float) const;
64 DXVector2 operator / (float) const;
65
66 friend DXVector2 operator * (float, const DXVector2 &);
67
68 bool operator == (const DXVector2 &) const;
69 bool operator != (const DXVector2 &) const;
70};
71
72struct DXVector3 {
73 float _x;
74 float _y;
75 float _z;
76
77 DXVector3() {}
78 DXVector3(const float *pf);
79 DXVector3(float fx, float fy, float fz);
80
81 operator float* ();
82 operator const float* () const;
83
84 DXVector3 &operator += (const DXVector3 &);
85 DXVector3 &operator -= (const DXVector3 &);
86 DXVector3 &operator *= (float);
87 DXVector3 &operator /= (float);
88
89 DXVector3 operator + () const;
90 DXVector3 operator - () const;
91
92 DXVector3 operator + (const DXVector3 &) const;
93 DXVector3 operator - (const DXVector3 &) const;
94 DXVector3 operator * (float) const;
95 DXVector3 operator / (float) const;
96
97 bool operator == (const DXVector3 &) const;
98 bool operator != (const DXVector3 &) const;
99};
100
101struct DXVector4 {
102 float _x;
103 float _y;
104 float _z;
105 float _w;
106
107 DXVector4() {}
108 DXVector4(const float *pf);
109 DXVector4(float fx, float fy, float fz, float fw);
110
111 operator float* ();
112 operator const float* () const;
113};
114
116 float _x;
117 float _y;
118 float _z;
119 float _w;
120
121 DXQuaternion() {}
122 DXQuaternion(const float *pf);
123 DXQuaternion(float fx, float fy, float fz, float fw);
124
125 operator float* ();
126 operator const float* () const;
127};
128
129struct DXPlane {
130 float _a;
131 float _b;
132 float _c;
133 float _d;
134
135 DXPlane() {}
136 DXPlane(float fa, float fb, float fc, float fd);
137};
138
139struct DXMatrix {
140 union {
141 struct {
142 float _11, _12, _13, _14;
143 float _21, _22, _23, _24;
144 float _31, _32, _33, _34;
145 float _41, _42, _43, _44;
146 } matrix;
147 float _m[4][4];
148 float _m4x4[16];
149 };
150
151 DXMatrix() {}
152 DXMatrix(const float *pf);
153
154 float &operator () (uint32 row, uint32 col);
155
156 operator float* ();
157 operator const float* () const;
158
159 DXMatrix operator * (const DXMatrix &) const;
160};
161
163 uint32 _x;
164 uint32 _y;
165 uint32 _width;
166 uint32 _height;
167 float _minZ;
168 float _maxZ;
169};
170
171#if defined(SCUMMVM_USE_PRAGMA_PACK)
172#pragma pack()
173#endif
174
175DXQuaternion *DXQuaternionRotationMatrix(DXQuaternion *out,const DXMatrix *m);
176DXMatrix *DXMatrixPerspectiveFovLH(DXMatrix *pout,float fovy, float aspect, float zn, float zf);
177DXMatrix *DXMatrixPerspectiveFovRH(DXMatrix *pout, float fovy, float aspect, float zn, float zf);
178DXMatrix *DXMatrixLookAtLH(DXMatrix *out, const DXVector3 *eye, const DXVector3 *at, const DXVector3 *up);
179DXMatrix *DXMatrixLookAtRH(DXMatrix *out, const DXVector3 *eye, const DXVector3 *at, const DXVector3 *up);
180DXMatrix *DXMatrixOrthoLH(DXMatrix *pout, float w, float h, float zn, float zf);
181DXMatrix *DXMatrixOrthoOffCenterLH(DXMatrix *pout, float l, float r, float b, float t, float zn, float zf);
182DXMatrix *DXMatrixInverse(DXMatrix *pout, float *pdeterminant, const DXMatrix *pm);
183DXPlane *DXPlaneFromPointNormal(DXPlane *pout, const DXVector3 *pvpoint, const DXVector3 *pvnormal);
184DXPlane *DXPlaneFromPoints(DXPlane *pout, const DXVector3 *pv1, const DXVector3 *pv2, const DXVector3 *pv3);
185DXVector3 *DXPlaneIntersectLine(DXVector3 *pout, const DXPlane *pp, const DXVector3 *pv1, const DXVector3 *pv2);
186DXVector3 *DXVec3Normalize(DXVector3 *pout, const DXVector3 *pv);
187DXMatrix *DXMatrixTranslation(DXMatrix *pout, float x, float y, float z);
188DXMatrix *DXMatrixScaling(DXMatrix *pout, float sx, float sy, float sz);
189DXMatrix *DXMatrixRotationY(DXMatrix *pout, float angle);
190DXMatrix *DXMatrixRotationZ(DXMatrix *pout, float angle);
191DXMatrix *DXMatrixRotationYawPitchRoll(DXMatrix *out, float yaw, float pitch, float roll);
192DXMatrix *DXMatrixRotationQuaternion(DXMatrix *pout, const DXQuaternion *pq);
193DXQuaternion *DXQuaternionSlerp(DXQuaternion *out, const DXQuaternion *q1, const DXQuaternion *q2, float t);
194DXVector4 *DXVec3Transform(DXVector4 *pout, const DXVector3 *pv, const DXMatrix *pm);
195DXVector3 *DXVec3TransformCoord(DXVector3 *pout, const DXVector3 *pv, const DXMatrix *pm);
196DXVector3 *DXVec3TransformNormal(DXVector3 *pout, const DXVector3 *pv, const DXMatrix *pm);
197DXMatrix *DXMatrixMultiply(DXMatrix *pout, const DXMatrix *pm1, const DXMatrix *pm2);
198DXVector3 *DXVec3Project(DXVector3 *pout, const DXVector3 *pv, const DXViewport *pviewport,
199 const DXMatrix *pprojection, const DXMatrix *pview, const DXMatrix *pworld);
200DXMatrix *DXMatrixTranspose(DXMatrix *pout, const DXMatrix *pm);
201DXVector4 *DXVec4Transform(DXVector4 *pout, const DXVector4 *pv, const DXMatrix *pm);
202DXMatrix *DXMatrixShadow(DXMatrix *pout, const DXVector4 *plight, const DXPlane *pplane);
203DXVector2 *DXVec2TransformCoord(DXVector2 *pout, const DXVector2 *pv, const DXMatrix *pm);
204
205static inline DXMatrix *DXMatrixIdentity(DXMatrix *pout) {
206 pout->_m[0][1] = 0.0f;
207 pout->_m[0][2] = 0.0f;
208 pout->_m[0][3] = 0.0f;
209 pout->_m[1][0] = 0.0f;
210 pout->_m[1][2] = 0.0f;
211 pout->_m[1][3] = 0.0f;
212 pout->_m[2][0] = 0.0f;
213 pout->_m[2][1] = 0.0f;
214 pout->_m[2][3] = 0.0f;
215 pout->_m[3][0] = 0.0f;
216 pout->_m[3][1] = 0.0f;
217 pout->_m[3][2] = 0.0f;
218 pout->_m[0][0] = 1.0f;
219 pout->_m[1][1] = 1.0f;
220 pout->_m[2][2] = 1.0f;
221 pout->_m[3][3] = 1.0f;
222 return pout;
223}
224
225static inline DXVector3 *DXVec3Lerp(DXVector3 *pout, const DXVector3 *pv1, const DXVector3 *pv2, float s) {
226 pout->_x = (1-s) * (pv1->_x) + s * (pv2->_x);
227 pout->_y = (1-s) * (pv1->_y) + s * (pv2->_y);
228 pout->_z = (1-s) * (pv1->_z) + s * (pv2->_z);
229 return pout;
230}
231
232static inline float DXQuaternionDot(const DXQuaternion *pq1, const DXQuaternion *pq2) {
233 return (pq1->_x) * (pq2->_x) + (pq1->_y) * (pq2->_y) + (pq1->_z) * (pq2->_z) + (pq1->_w) * (pq2->_w);
234}
235
236static inline DXVector3 *DXVec3Cross(DXVector3 *pout, const DXVector3 *pv1, const DXVector3 *pv2) {
237 DXVector3 temp;
238
239 temp._x = (pv1->_y) * (pv2->_z) - (pv1->_z) * (pv2->_y);
240 temp._y = (pv1->_z) * (pv2->_x) - (pv1->_x) * (pv2->_z);
241 temp._z = (pv1->_x) * (pv2->_y) - (pv1->_y) * (pv2->_x);
242 *pout = temp;
243 return pout;
244}
245
246static inline float DXVec3Dot(const DXVector3 *pv1, const DXVector3 *pv2) {
247 return (pv1->_x) * (pv2->_x) + (pv1->_y) * (pv2->_y) + (pv1->_z) * (pv2->_z);
248}
249
250static inline DXVector3 *DXVec3Subtract(DXVector3 *pout, const DXVector3 *pv1, const DXVector3 *pv2) {
251 pout->_x = pv1->_x - pv2->_x;
252 pout->_y = pv1->_y - pv2->_y;
253 pout->_z = pv1->_z - pv2->_z;
254 return pout;
255}
256
257static inline float DXVec3Length(const DXVector3 *pv) {
258 return sqrtf(pv->_x * pv->_x + pv->_y * pv->_y + pv->_z * pv->_z);
259}
260
261static inline float DXVec4Dot(const DXVector4 *pv1, const DXVector4 *pv2) {
262 return (pv1->_x) * (pv2->_x) + (pv1->_y) * (pv2->_y) + (pv1->_z) * (pv2->_z) + (pv1->_w) * (pv2->_w);
263}
264
265static inline float DXPlaneDot(const DXPlane *pp, const DXVector4 *pv) {
266 return ((pp->_a) * (pv->_x) + (pp->_b) * (pv->_y) + (pp->_c) * (pv->_z) + (pp->_d) * (pv->_w) );
267}
268
269static inline float DXVec2Length(const DXVector2 *pv) {
270 if (!pv)
271 return 0.0f;
272 return sqrtf(pv->_x * pv->_x + pv->_y * pv->_y);
273}
274
275} // End of namespace Wintermute
276
277#endif
Definition: xmath.h:139
Definition: xmath.h:129
Definition: xmath.h:115
Definition: xmath.h:42
Definition: xmath.h:72
Definition: xmath.h:101
Definition: xmath.h:162