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 
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  DXPlane(float fa, float fb, float fc, float fd);
115 };
116 
117 struct DXMatrix {
118  union {
119  struct {
120  float _11, _12, _13, _14;
121  float _21, _22, _23, _24;
122  float _31, _32, _33, _34;
123  float _41, _42, _43, _44;
124  } matrix;
125  float _m[4][4];
126  float _m4x4[16];
127  };
128 
129  DXMatrix() {}
130  DXMatrix(const float *pf);
131 
132  float &operator () (uint32 row, uint32 col);
133 
134  operator float* ();
135  operator const float* () const;
136 
137  DXMatrix operator * (const DXMatrix &) const;
138 };
139 
140 struct DXViewport {
141  uint32 _x;
142  uint32 _y;
143  uint32 _width;
144  uint32 _height;
145  float _minZ;
146  float _maxZ;
147 };
148 
149 #if defined(SCUMMVM_USE_PRAGMA_PACK)
150 #pragma pack()
151 #endif
152 
153 DXQuaternion *DXQuaternionRotationMatrix(DXQuaternion *out,const DXMatrix *m);
154 DXMatrix *DXMatrixPerspectiveFovLH(DXMatrix *pout,float fovy, float aspect, float zn, float zf);
155 DXMatrix *DXMatrixPerspectiveFovRH(DXMatrix *pout, float fovy, float aspect, float zn, float zf);
156 DXMatrix *DXMatrixLookAtLH(DXMatrix *out, const DXVector3 *eye, const DXVector3 *at, const DXVector3 *up);
157 DXMatrix *DXMatrixLookAtRH(DXMatrix *out, const DXVector3 *eye, const DXVector3 *at, const DXVector3 *up);
158 DXMatrix *DXMatrixOrthoLH(DXMatrix *pout, float w, float h, float zn, float zf);
159 DXMatrix *DXMatrixOrthoOffCenterLH(DXMatrix *pout, float l, float r, float b, float t, float zn, float zf);
160 DXMatrix *DXMatrixInverse(DXMatrix *pout, float *pdeterminant, const DXMatrix *pm);
161 DXPlane *DXPlaneFromPointNormal(DXPlane *pout, const DXVector3 *pvpoint, const DXVector3 *pvnormal);
162 DXPlane *DXPlaneFromPoints(DXPlane *pout, const DXVector3 *pv1, const DXVector3 *pv2, const DXVector3 *pv3);
163 DXVector3 *DXPlaneIntersectLine(DXVector3 *pout, const DXPlane *pp, const DXVector3 *pv1, const DXVector3 *pv2);
164 DXVector3 *DXVec3Normalize(DXVector3 *pout, const DXVector3 *pv);
165 DXMatrix *DXMatrixTranslation(DXMatrix *pout, float x, float y, float z);
166 DXMatrix *DXMatrixScaling(DXMatrix *pout, float sx, float sy, float sz);
167 DXMatrix *DXMatrixRotationY(DXMatrix *pout, float angle);
168 DXMatrix *DXMatrixRotationZ(DXMatrix *pout, float angle);
169 DXMatrix *DXMatrixRotationYawPitchRoll(DXMatrix *out, float yaw, float pitch, float roll);
170 DXMatrix *DXMatrixRotationQuaternion(DXMatrix *pout, const DXQuaternion *pq);
171 DXQuaternion *DXQuaternionSlerp(DXQuaternion *out, const DXQuaternion *q1, const DXQuaternion *q2, float t);
172 DXVector4 *DXVec3Transform(DXVector4 *pout, const DXVector3 *pv, const DXMatrix *pm);
173 DXVector3 *DXVec3TransformCoord(DXVector3 *pout, const DXVector3 *pv, const DXMatrix *pm);
174 DXVector3 *DXVec3TransformNormal(DXVector3 *pout, const DXVector3 *pv, const DXMatrix *pm);
175 DXMatrix *DXMatrixMultiply(DXMatrix *pout, const DXMatrix *pm1, const DXMatrix *pm2);
176 DXVector3 *DXVec3Project(DXVector3 *pout, const DXVector3 *pv, const DXViewport *pviewport,
177  const DXMatrix *pprojection, const DXMatrix *pview, const DXMatrix *pworld);
178 DXMatrix *DXMatrixTranspose(DXMatrix *pout, const DXMatrix *pm);
179 DXVector4 *DXVec4Transform(DXVector4 *pout, const DXVector4 *pv, const DXMatrix *pm);
180 DXMatrix *DXMatrixShadow(DXMatrix *pout, const DXVector4 *plight, const DXPlane *pplane);
181 
182 static inline DXMatrix *DXMatrixIdentity(DXMatrix *pout) {
183  (*pout)._m[0][1] = 0.0f;
184  (*pout)._m[0][2] = 0.0f;
185  (*pout)._m[0][3] = 0.0f;
186  (*pout)._m[1][0] = 0.0f;
187  (*pout)._m[1][2] = 0.0f;
188  (*pout)._m[1][3] = 0.0f;
189  (*pout)._m[2][0] = 0.0f;
190  (*pout)._m[2][1] = 0.0f;
191  (*pout)._m[2][3] = 0.0f;
192  (*pout)._m[3][0] = 0.0f;
193  (*pout)._m[3][1] = 0.0f;
194  (*pout)._m[3][2] = 0.0f;
195  (*pout)._m[0][0] = 1.0f;
196  (*pout)._m[1][1] = 1.0f;
197  (*pout)._m[2][2] = 1.0f;
198  (*pout)._m[3][3] = 1.0f;
199  return pout;
200 }
201 
202 static inline DXVector3 *DXVec3Lerp(DXVector3 *pout, const DXVector3 *pv1, const DXVector3 *pv2, float s) {
203  pout->_x = (1-s) * (pv1->_x) + s * (pv2->_x);
204  pout->_y = (1-s) * (pv1->_y) + s * (pv2->_y);
205  pout->_z = (1-s) * (pv1->_z) + s * (pv2->_z);
206  return pout;
207 }
208 
209 static inline float DXQuaternionDot(const DXQuaternion *pq1, const DXQuaternion *pq2) {
210  return (pq1->_x) * (pq2->_x) + (pq1->_y) * (pq2->_y) + (pq1->_z) * (pq2->_z) + (pq1->_w) * (pq2->_w);
211 }
212 
213 static inline DXVector3 *DXVec3Cross(DXVector3 *pout, const DXVector3 *pv1, const DXVector3 *pv2) {
214  DXVector3 temp;
215 
216  temp._x = (pv1->_y) * (pv2->_z) - (pv1->_z) * (pv2->_y);
217  temp._y = (pv1->_z) * (pv2->_x) - (pv1->_x) * (pv2->_z);
218  temp._z = (pv1->_x) * (pv2->_y) - (pv1->_y) * (pv2->_x);
219  *pout = temp;
220  return pout;
221 }
222 
223 static inline float DXVec3Dot(const DXVector3 *pv1, const DXVector3 *pv2) {
224  return (pv1->_x) * (pv2->_x) + (pv1->_y) * (pv2->_y) + (pv1->_z) * (pv2->_z);
225 }
226 
227 static inline DXVector3 *DXVec3Subtract(DXVector3 *pout, const DXVector3 *pv1, const DXVector3 *pv2) {
228  pout->_x = pv1->_x - pv2->_x;
229  pout->_y = pv1->_y - pv2->_y;
230  pout->_z = pv1->_z - pv2->_z;
231  return pout;
232 }
233 
234 static inline float DXVec3Length(const DXVector3 *pv) {
235  return sqrtf(pv->_x * pv->_x + pv->_y * pv->_y + pv->_z * pv->_z);
236 }
237 
238 static inline float DXVec4Dot(const DXVector4 *pv1, const DXVector4 *pv2) {
239  return (pv1->_x) * (pv2->_x) + (pv1->_y) * (pv2->_y) + (pv1->_z) * (pv2->_z) + (pv1->_w) * (pv2->_w);
240 }
241 
242 static inline float DXPlaneDot(const DXPlane *pp, const DXVector4 *pv) {
243  return ((pp->_a) * (pv->_x) + (pp->_b) * (pv->_y) + (pp->_c) * (pv->_z) + (pp->_d) * (pv->_w) );
244 }
245 
246 
247 } // End of namespace Wintermute
248 
249 #endif
Definition: xmath.h:50
Definition: xmath.h:42
Definition: xmath.h:140
Definition: xmath.h:107
Definition: xmath.h:117
Definition: xmath.h:93
Definition: xmath.h:79
Definition: achievements_tables.h:27