source: trunk/src/vector.cpp @ 1173

Last change on this file since 1173 was 1173, checked in by sam, 8 years ago

xbox: start working on an Xbox/Direct3D port.

  • Property svn:keywords set to Id
File size: 10.5 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#if defined _XBOX
16#   define _USE_MATH_DEFINES /* for M_PI */
17#   include <xtl.h>
18#   undef near /* Fuck Microsoft */
19#   undef far /* Fuck Microsoft again */
20#elif defined _WIN32
21#   define _USE_MATH_DEFINES /* for M_PI */
22#   define WIN32_LEAN_AND_MEAN
23#   include <windows.h>
24#   undef near /* Fuck Microsoft */
25#   undef far /* Fuck Microsoft again */
26#endif
27
28#include <cmath> /* for M_PI */
29#include <cstdlib> /* free() */
30#include <cstring> /* strdup() */
31
32#include "core.h"
33
34using namespace std;
35
36namespace lol
37{
38
39float dot(vec2 v1, vec2 v2)
40{
41    return v1.x * v2.x + v1.y * v2.y;
42}
43
44float dot(vec3 v1, vec3 v2)
45{
46    return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
47}
48
49float dot(vec4 v1, vec4 v2)
50{
51    return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w;
52}
53
54template<> vec3 cross(vec3 v1, vec3 v2)
55{
56    return vec3(v1.y * v2.z - v1.z * v2.y,
57                v1.z * v2.x - v1.x * v2.z,
58                v1.x * v2.y - v1.y * v2.x);
59}
60
61static inline float det3(float a, float b, float c,
62                         float d, float e, float f,
63                         float g, float h, float i)
64{
65    return a * (e * i - h * f)
66         + b * (f * g - i * d)
67         + c * (d * h - g * e);
68}
69
70static inline float cofact3(mat4 const &mat, int i, int j)
71{
72    return det3(mat[(i + 1) & 3][(j + 1) & 3],
73                mat[(i + 2) & 3][(j + 1) & 3],
74                mat[(i + 3) & 3][(j + 1) & 3],
75                mat[(i + 1) & 3][(j + 2) & 3],
76                mat[(i + 2) & 3][(j + 2) & 3],
77                mat[(i + 3) & 3][(j + 2) & 3],
78                mat[(i + 1) & 3][(j + 3) & 3],
79                mat[(i + 2) & 3][(j + 3) & 3],
80                mat[(i + 3) & 3][(j + 3) & 3]) * (((i + j) & 1) ? -1.0f : 1.0f);
81}
82
83template<> float mat4::det() const
84{
85    float ret = 0;
86    for (int n = 0; n < 4; n++)
87        ret += (*this)[n][0] * cofact3(*this, n, 0);
88    return ret;
89}
90
91template<> mat4 mat4::invert() const
92{
93    mat4 ret;
94    float d = det();
95    if (d)
96    {
97        d = 1.0f / d;
98        for (int j = 0; j < 4; j++)
99            for (int i = 0; i < 4; i++)
100                ret[j][i] = cofact3(*this, i, j) * d;
101    }
102    return ret;
103}
104
105template<> void vec2::printf() const
106{
107    Log::Debug("[ %6.6f %6.6f ]\n", x, y);
108}
109
110template<> void ivec2::printf() const
111{
112    Log::Debug("[ %i %i ]\n", x, y);
113}
114
115template<> void cmplx::printf() const
116{
117    Log::Debug("[ %6.6f %6.6f ]\n", x, y);
118}
119
120template<> void vec3::printf() const
121{
122    Log::Debug("[ %6.6f %6.6f %6.6f ]\n", x, y, z);
123}
124
125template<> void ivec3::printf() const
126{
127    Log::Debug("[ %i %i %i ]\n", x, y, z);
128}
129
130template<> void vec4::printf() const
131{
132    Log::Debug("[ %6.6f %6.6f %6.6f %6.6f ]\n", x, y, z, w);
133}
134
135template<> void ivec4::printf() const
136{
137    Log::Debug("[ %i %i %i %i ]\n", x, y, z, w);
138}
139
140template<> void quat::printf() const
141{
142    Log::Debug("[ %6.6f %6.6f %6.6f %6.6f ]\n", x, y, z, w);
143}
144
145template<> void mat4::printf() const
146{
147    mat4 const &p = *this;
148
149    Log::Debug("[ %6.6f %6.6f %6.6f %6.6f\n",
150               p[0][0], p[1][0], p[2][0], p[3][0]);
151    Log::Debug("  %6.6f %6.6f %6.6f %6.6f\n",
152               p[0][1], p[1][1], p[2][1], p[3][1]);
153    Log::Debug("  %6.6f %6.6f %6.6f %6.6f\n",
154               p[0][2], p[1][2], p[2][2], p[3][2]);
155    Log::Debug("  %6.6f %6.6f %6.6f %6.6f ]\n",
156               p[0][3], p[1][3], p[2][3], p[3][3]);
157}
158
159#if !defined __ANDROID__
160template<> std::ostream &operator<<(std::ostream &stream, ivec2 const &v)
161{
162    return stream << "(" << v.x << ", " << v.y << ")";
163}
164
165template<> std::ostream &operator<<(std::ostream &stream, icmplx const &v)
166{
167    return stream << "(" << v.x << ", " << v.y << ")";
168}
169
170template<> std::ostream &operator<<(std::ostream &stream, ivec3 const &v)
171{
172    return stream << "(" << v.x << ", " << v.y << ", " << v.z << ")";
173}
174
175template<> std::ostream &operator<<(std::ostream &stream, ivec4 const &v)
176{
177    return stream << "(" << v.x << ", " << v.y << ", "
178                         << v.z << ", " << v.w << ")";
179}
180
181template<> std::ostream &operator<<(std::ostream &stream, iquat const &v)
182{
183    return stream << "(" << v.x << ", " << v.y << ", "
184                         << v.z << ", " << v.w << ")";
185}
186
187template<> std::ostream &operator<<(std::ostream &stream, vec2 const &v)
188{
189    return stream << "(" << v.x << ", " << v.y << ")";
190}
191
192template<> std::ostream &operator<<(std::ostream &stream, cmplx const &v)
193{
194    return stream << "(" << v.x << ", " << v.y << ")";
195}
196
197template<> std::ostream &operator<<(std::ostream &stream, vec3 const &v)
198{
199    return stream << "(" << v.x << ", " << v.y << ", " << v.z << ")";
200}
201
202template<> std::ostream &operator<<(std::ostream &stream, vec4 const &v)
203{
204    return stream << "(" << v.x << ", " << v.y << ", "
205                         << v.z << ", " << v.w << ")";
206}
207
208template<> std::ostream &operator<<(std::ostream &stream, quat const &v)
209{
210    return stream << "(" << v.x << ", " << v.y << ", "
211                         << v.z << ", " << v.w << ")";
212}
213
214template<> std::ostream &operator<<(std::ostream &stream, mat4 const &m)
215{
216    stream << "((" << m[0][0] << ", " << m[1][0]
217            << ", " << m[2][0] << ", " << m[3][0] << "), ";
218    stream << "(" << m[0][1] << ", " << m[1][1]
219           << ", " << m[2][1] << ", " << m[3][1] << "), ";
220    stream << "(" << m[0][2] << ", " << m[1][2]
221           << ", " << m[2][2] << ", " << m[3][2] << "), ";
222    stream << "(" << m[0][3] << ", " << m[1][3]
223           << ", " << m[2][3] << ", " << m[3][3] << "))";
224    return stream;
225}
226#endif
227
228template<> mat4 mat4::translate(float x, float y, float z)
229{
230    mat4 ret(1.0f);
231    ret[3][0] = x;
232    ret[3][1] = y;
233    ret[3][2] = z;
234    return ret;
235}
236
237template<> mat4 mat4::translate(vec3 v)
238{
239    return translate(v.x, v.y, v.z);
240}
241
242template<> mat4 mat4::rotate(float angle, float x, float y, float z)
243{
244    angle *= (M_PI / 180.0f);
245
246    float st = sinf(angle);
247    float ct = cosf(angle);
248
249    float len = sqrtf(x * x + y * y + z * z);
250    float invlen = len ? 1.0f / len : 0.0f;
251    x *= invlen;
252    y *= invlen;
253    z *= invlen;
254
255    float mtx = (1.0f - ct) * x;
256    float mty = (1.0f - ct) * y;
257    float mtz = (1.0f - ct) * z;
258
259    mat4 ret(1.0f);
260
261    ret[0][0] = x * mtx + ct;
262    ret[0][1] = x * mty + st * z;
263    ret[0][2] = x * mtz - st * y;
264
265    ret[1][0] = y * mtx - st * z;
266    ret[1][1] = y * mty + ct;
267    ret[1][2] = y * mtz + st * x;
268
269    ret[2][0] = z * mtx + st * y;
270    ret[2][1] = z * mty - st * x;
271    ret[2][2] = z * mtz + ct;
272
273    return ret;
274}
275
276template<> mat4 mat4::rotate(float angle, vec3 v)
277{
278    return rotate(angle, v.x, v.y, v.z);
279}
280
281template<> mat4 mat4::rotate(quat q)
282{
283    mat4 ret(1.0f);
284    float n = norm(q);
285
286    if (!n)
287        return ret;
288
289    float s = 2.0f / n;
290
291    ret[0][0] = 1.0f - s * (q.y * q.y + q.z * q.z);
292    ret[0][1] = s * (q.x * q.y - q.z * q.w);
293    ret[0][2] = s * (q.x * q.z + q.y * q.w);
294
295    ret[1][0] = s * (q.x * q.y + q.z * q.w);
296    ret[1][1] = 1.0f - s * (q.z * q.z + q.x * q.x);
297    ret[1][2] = s * (q.y * q.z - q.x * q.w);
298
299    ret[2][0] = s * (q.x * q.z - q.y * q.w);
300    ret[2][1] = s * (q.y * q.z + q.x * q.w);
301    ret[2][2] = 1.0f - s * (q.x * q.x + q.y * q.y);
302
303    return ret;
304}
305
306template<> quat::Quat(mat4 const &m)
307{
308    /* See http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/christian.htm for a version with no branches */
309    float t = m[0][0] + m[1][1] + m[2][2];
310    if (t > 0)
311    {
312        w = 0.5f * sqrtf(1.0f + t);
313        float s = 0.25f / w;
314        x = s * (m[2][1] - m[1][2]);
315        y = s * (m[0][2] - m[2][0]);
316        z = s * (m[1][0] - m[0][1]);
317    }
318    else if (m[0][0] > m[1][1] && m[0][0] > m[2][2])
319    {
320        x = 0.5f * sqrtf(1.0f + m[0][0] - m[1][1] - m[2][2]);
321        float s = 0.25f / x;
322        y = s * (m[1][0] + m[0][1]);
323        z = s * (m[0][2] + m[2][0]);
324        w = s * (m[2][1] - m[1][2]);
325    }
326    else if (m[1][1] > m[2][2])
327    {
328        y = 0.5f * sqrtf(1.0f - m[0][0] + m[1][1] - m[2][2]);
329        float s = 0.25f / y;
330        x = s * (m[1][0] + m[0][1]);
331        z = s * (m[2][1] + m[1][2]);
332        w = s * (m[0][2] - m[2][0]);
333    }
334    else
335    {
336        z = 0.5f * sqrtf(1.0f - m[0][0] - m[1][1] + m[2][2]);
337        float s = 0.25f / z;
338        x = s * (m[0][2] + m[2][0]);
339        y = s * (m[2][1] + m[1][2]);
340        w = s * (m[1][0] - m[0][1]);
341    }
342}
343
344template<> mat4 mat4::lookat(vec3 eye, vec3 center, vec3 up)
345{
346    vec3 v3 = normalize(eye - center);
347    vec3 v2 = normalize(up);
348    vec3 v1 = normalize(cross(v2, v3));
349    v2 = cross(v3, v1);
350
351    mat4 orient(1.0f);
352    orient[0][0] = v1.x;
353    orient[0][1] = v2.x;
354    orient[0][2] = v3.x;
355    orient[1][0] = v1.y;
356    orient[1][1] = v2.y;
357    orient[1][2] = v3.y;
358    orient[2][0] = v1.z;
359    orient[2][1] = v2.z;
360    orient[2][2] = v3.z;
361
362    return orient * mat4::translate(-eye);
363}
364
365template<> mat4 mat4::ortho(float left, float right, float bottom,
366                            float top, float near, float far)
367{
368    float invrl = (right != left) ? 1.0f / (right - left) : 0.0f;
369    float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f;
370    float invfn = (far != near) ? 1.0f / (far - near) : 0.0f;
371
372    mat4 ret(0.0f);
373    ret[0][0] = 2.0f * invrl;
374    ret[1][1] = 2.0f * invtb;
375    ret[2][2] = -2.0f * invfn;
376    ret[3][0] = - (right + left) * invrl;
377    ret[3][1] = - (top + bottom) * invtb;
378    ret[3][2] = - (far + near) * invfn;
379    ret[3][3] = 1.0f;
380    return ret;
381}
382
383template<> mat4 mat4::frustum(float left, float right, float bottom,
384                              float top, float near, float far)
385{
386    float invrl = (right != left) ? 1.0f / (right - left) : 0.0f;
387    float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f;
388    float invfn = (far != near) ? 1.0f / (far - near) : 0.0f;
389
390    mat4 ret(0.0f);
391    ret[0][0] = 2.0f * near * invrl;
392    ret[1][1] = 2.0f * near * invtb;
393    ret[2][0] = (right + left) * invrl;
394    ret[2][1] = (top + bottom) * invtb;
395    ret[2][2] = - (far + near) * invfn;
396    ret[2][3] = -1.0f;
397    ret[3][2] = -2.0f * far * near * invfn;
398    return ret;
399}
400
401template<> mat4 mat4::perspective(float fov_y, float width,
402                                  float height, float near, float far)
403{
404    fov_y *= (M_PI / 180.0f);
405
406    float t2 = tanf(fov_y * 0.5f);
407    float t1 = t2 * width / height;
408
409    return frustum(-near * t1, near * t1, -near * t2, near * t2, near, far);
410}
411
412} /* namespace lol */
413
Note: See TracBrowser for help on using the repository browser.