1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of LEXIExporter |
---|
4 | |
---|
5 | Copyright 2006 NDS Limited |
---|
6 | |
---|
7 | Author(s): |
---|
8 | Bo Krohn |
---|
9 | |
---|
10 | This program is free software; you can redistribute it and/or modify it under |
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
13 | version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
23 | ----------------------------------------------------------------------------- |
---|
24 | */ |
---|
25 | |
---|
26 | ///////////////////////////////////////////////////// |
---|
27 | // |
---|
28 | // Vector4 class |
---|
29 | // |
---|
30 | ///////////////////////////////////////////////////// |
---|
31 | |
---|
32 | #ifndef __CVector4_Class__ |
---|
33 | #define __CVector4_Class__ |
---|
34 | |
---|
35 | // |
---|
36 | |
---|
37 | class CVec4 { |
---|
38 | |
---|
39 | friend class CMatrix; |
---|
40 | |
---|
41 | public: |
---|
42 | |
---|
43 | static const CVec4 _zero; |
---|
44 | static const CVec4 _x; |
---|
45 | static const CVec4 _y; |
---|
46 | static const CVec4 _z; |
---|
47 | static const CVec4 _w; |
---|
48 | |
---|
49 | // Constructors/Destructor |
---|
50 | inline CVec4() |
---|
51 | { |
---|
52 | } |
---|
53 | inline CVec4(const CVec4& v) |
---|
54 | { |
---|
55 | x=v.x; |
---|
56 | y=v.y; |
---|
57 | z=v.z; |
---|
58 | w=v.w; |
---|
59 | } |
---|
60 | inline CVec4(float fX, float fY, float fZ, float fW) |
---|
61 | { |
---|
62 | x=fX; |
---|
63 | y=fY; |
---|
64 | z=fZ; |
---|
65 | w=fW; |
---|
66 | } |
---|
67 | inline CVec4(const float* v) |
---|
68 | { |
---|
69 | x=v[0]; |
---|
70 | y=v[1]; |
---|
71 | z=v[2]; |
---|
72 | w=v[3]; |
---|
73 | } |
---|
74 | |
---|
75 | // Direct access to vector |
---|
76 | float x, y, z, w; |
---|
77 | |
---|
78 | // Ccess operators |
---|
79 | inline float& operator [] (unsigned int iIndex) |
---|
80 | { |
---|
81 | return ((float*)&x)[iIndex]; |
---|
82 | } |
---|
83 | inline const float& operator [] (unsigned int iIndex) const |
---|
84 | { |
---|
85 | return ((const float*)&x)[iIndex]; |
---|
86 | } |
---|
87 | |
---|
88 | // Assignment |
---|
89 | inline CVec4& operator = (const CVec4& v) |
---|
90 | { |
---|
91 | x=v.x; |
---|
92 | y=v.y; |
---|
93 | z=v.z; |
---|
94 | w=v.w; |
---|
95 | |
---|
96 | return *this; |
---|
97 | } |
---|
98 | |
---|
99 | // Comparing |
---|
100 | inline bool operator == (const CVec4& v) const |
---|
101 | { |
---|
102 | return (x==v.x && y==v.y && z==v.z && w==v.w); |
---|
103 | } |
---|
104 | |
---|
105 | inline bool operator != (const CVec4& v) const |
---|
106 | { |
---|
107 | return (x!=v.x || y!=v.y || z!=v.z || w!=v.w); |
---|
108 | } |
---|
109 | |
---|
110 | // Operators |
---|
111 | inline CVec4 operator - () const |
---|
112 | { |
---|
113 | return CVec4(-x, -y, -z, -w); |
---|
114 | } |
---|
115 | |
---|
116 | inline CVec4 operator + (const CVec4& v) const |
---|
117 | { |
---|
118 | return CVec4(x+v.x, y+v.y, z+v.z, w+v.w); |
---|
119 | } |
---|
120 | |
---|
121 | inline CVec4 operator - (const CVec4& v) const |
---|
122 | { |
---|
123 | return CVec4(x-v.x, y-v.y, z-v.z, w-v.w); |
---|
124 | } |
---|
125 | |
---|
126 | inline CVec4 operator * (const CVec4& v) const |
---|
127 | { |
---|
128 | return CVec4(x*v.x, y*v.y, z*v.z, w*v.w); |
---|
129 | } |
---|
130 | |
---|
131 | inline CVec4 operator / (const CVec4& v) const |
---|
132 | { |
---|
133 | return CVec4(x/v.x, y/v.y, z/v.z, w/v.w); |
---|
134 | } |
---|
135 | |
---|
136 | inline void add(const CVec4& v) |
---|
137 | { |
---|
138 | x += v.x; |
---|
139 | y += v.y; |
---|
140 | z += v.z; |
---|
141 | w += v.w; |
---|
142 | } |
---|
143 | |
---|
144 | inline void add(const CVec4& v1, const CVec4& v2) |
---|
145 | { |
---|
146 | x = v1.x + v2.x; |
---|
147 | y = v1.y + v2.y; |
---|
148 | z = v1.z + v2.z; |
---|
149 | w = v1.w + v2.w; |
---|
150 | } |
---|
151 | |
---|
152 | inline CVec4& operator += (const CVec4& v) |
---|
153 | { |
---|
154 | x += v.x; |
---|
155 | y += v.y; |
---|
156 | z += v.z; |
---|
157 | w += v.w; |
---|
158 | return *this; |
---|
159 | } |
---|
160 | |
---|
161 | inline void subtract(const CVec4& v) |
---|
162 | { |
---|
163 | x -= v.x; |
---|
164 | y -= v.y; |
---|
165 | z -= v.z; |
---|
166 | w -= v.w; |
---|
167 | } |
---|
168 | |
---|
169 | inline void subtract(const CVec4& v1, const CVec4& v2) |
---|
170 | { |
---|
171 | x = v1.x - v2.x; |
---|
172 | y = v1.y - v2.y; |
---|
173 | z = v1.z - v2.z; |
---|
174 | w = v1.w - v2.w; |
---|
175 | } |
---|
176 | |
---|
177 | inline CVec4& operator -= (const CVec4& v) |
---|
178 | { |
---|
179 | x -= v.x; |
---|
180 | y -= v.y; |
---|
181 | z -= v.z; |
---|
182 | w -= v.w; |
---|
183 | return *this; |
---|
184 | } |
---|
185 | |
---|
186 | inline CVec4& operator *= (const CVec4& v) |
---|
187 | { |
---|
188 | x *= v.x; |
---|
189 | y *= v.y; |
---|
190 | z *= v.z; |
---|
191 | w *= v.w; |
---|
192 | return *this; |
---|
193 | } |
---|
194 | |
---|
195 | inline CVec4& operator /= (const CVec4& v) |
---|
196 | { |
---|
197 | x /= v.x; |
---|
198 | y /= v.y; |
---|
199 | z /= v.z; |
---|
200 | w /= v.w; |
---|
201 | return *this; |
---|
202 | } |
---|
203 | |
---|
204 | inline CVec4 operator * (float r) const |
---|
205 | { |
---|
206 | return CVec4(r*x, r*y, r*z, r*w); |
---|
207 | } |
---|
208 | |
---|
209 | inline CVec4 operator / (float r) const |
---|
210 | { |
---|
211 | float ri = 1.0f/r; |
---|
212 | return CVec4(ri*x, ri*y, ri*z, ri*w); |
---|
213 | } |
---|
214 | |
---|
215 | inline CVec4& operator *= (float r) |
---|
216 | { |
---|
217 | x *= r; |
---|
218 | y *= r; |
---|
219 | z *= r; |
---|
220 | w *= r; |
---|
221 | return *this; |
---|
222 | } |
---|
223 | |
---|
224 | inline CVec4& operator /= (float r) |
---|
225 | { |
---|
226 | float ri = 1.0f/r; |
---|
227 | x *= ri; |
---|
228 | y *= ri; |
---|
229 | z *= ri; |
---|
230 | w *= ri; |
---|
231 | return *this; |
---|
232 | } |
---|
233 | |
---|
234 | inline void scale(float scalar, const CVec4& v) |
---|
235 | { |
---|
236 | x = scalar * v.x; |
---|
237 | y = scalar * v.y; |
---|
238 | z = scalar * v.z; |
---|
239 | w = scalar * v.w; |
---|
240 | } |
---|
241 | |
---|
242 | inline void scale(float scalar) |
---|
243 | { |
---|
244 | x *= scalar; |
---|
245 | y *= scalar; |
---|
246 | z *= scalar; |
---|
247 | w *= scalar; |
---|
248 | } |
---|
249 | |
---|
250 | inline void lerp(const float& scalar, const CVec4& v1, const CVec4& v2) |
---|
251 | { |
---|
252 | x = v1.x + scalar * (v2.x - v1.x); |
---|
253 | y = v1.y + scalar * (v2.y - v1.y); |
---|
254 | z = v1.z + scalar * (v2.z - v1.z); |
---|
255 | w = v1.w + scalar * (v2.w - v1.w); |
---|
256 | } |
---|
257 | |
---|
258 | inline void lerp3(const float& scalar, const CVec4& v1, const CVec4& v2) |
---|
259 | { |
---|
260 | x = v1.x + scalar * (v2.x - v1.x); |
---|
261 | y = v1.y + scalar * (v2.y - v1.y); |
---|
262 | z = v1.z + scalar * (v2.z - v1.z); |
---|
263 | w = v1.w; |
---|
264 | } |
---|
265 | |
---|
266 | inline void normalize3() |
---|
267 | { |
---|
268 | float st = 1.0f / sqrtf(x*x + y*y + z*z); |
---|
269 | x *= st; |
---|
270 | y *= st; |
---|
271 | z *= st; |
---|
272 | } |
---|
273 | |
---|
274 | inline void normalize() |
---|
275 | { |
---|
276 | float st = 1.0f / sqrtf(x*x + y*y + z*z); |
---|
277 | x *= st; |
---|
278 | y *= st; |
---|
279 | z *= st; |
---|
280 | w *= st; |
---|
281 | } |
---|
282 | |
---|
283 | inline void clamp() |
---|
284 | { |
---|
285 | if(x < 0.0f) x = 0.0f; |
---|
286 | else if(x > 1.0f) x = 1.0f; |
---|
287 | if(y < 0.0f) y = 0.0f; |
---|
288 | else if(y > 1.0f) y = 1.0f; |
---|
289 | if(z < 0.0f) z = 0.0f; |
---|
290 | else if(z > 1.0f) z = 1.0f; |
---|
291 | if(w < 0.0f) w = 0.0f; |
---|
292 | else if(w > 1.0f) w = 1.0f; |
---|
293 | } |
---|
294 | |
---|
295 | inline void clamp(float min, float max) |
---|
296 | { |
---|
297 | if(x < min) x = min; |
---|
298 | else if(x > max) x = max; |
---|
299 | if(y < min) y = min; |
---|
300 | else if(y > max) y = max; |
---|
301 | if(z < min) z = min; |
---|
302 | else if(z > max) z = max; |
---|
303 | if(w < min) w = min; |
---|
304 | else if(w > max) w = max; |
---|
305 | } |
---|
306 | |
---|
307 | inline void zero() |
---|
308 | { |
---|
309 | x = y = z = w = 0.0f; |
---|
310 | } |
---|
311 | |
---|
312 | void fromMatrix(const CMatrix& m); |
---|
313 | }; |
---|
314 | |
---|
315 | // |
---|
316 | |
---|
317 | #endif // __CVector4_Class__ |
---|