1 | #include "al_siteconfig.h" |
---|
2 | |
---|
3 | #include <AL/al.h> |
---|
4 | #include <math.h> |
---|
5 | #include <stdlib.h> |
---|
6 | |
---|
7 | #include "al_debug.h" |
---|
8 | #include "al_main.h" |
---|
9 | #include "al_vector.h" |
---|
10 | |
---|
11 | #ifndef M_PI |
---|
12 | #define M_PI 3.14159265358979323846 /* pi */ |
---|
13 | #endif /* M_PI */ |
---|
14 | |
---|
15 | |
---|
16 | /* |
---|
17 | * _alVectorMagnitude( const ALfloat *origin, const ALfloat *v2 ) |
---|
18 | * |
---|
19 | * Returns magnitude of v2 with origin at origin. |
---|
20 | * |
---|
21 | * FIXME: please check my math |
---|
22 | */ |
---|
23 | ALfloat _alVectorMagnitude( const ALfloat *origin, const ALfloat *v2 ) { |
---|
24 | ALfloat lsav[3]; |
---|
25 | ALfloat retval; |
---|
26 | |
---|
27 | _alVectorDistance( lsav, origin, v2 ); |
---|
28 | |
---|
29 | retval = sqrt( lsav[0] * lsav[0] + |
---|
30 | lsav[1] * lsav[1] + |
---|
31 | lsav[2] * lsav[2] ); |
---|
32 | |
---|
33 | retval = fabs( retval ); |
---|
34 | |
---|
35 | return retval; |
---|
36 | |
---|
37 | } |
---|
38 | |
---|
39 | /* |
---|
40 | * _alVectorNormalize( ALfloat *d, const ALfloat *s) |
---|
41 | * |
---|
42 | * Normalizes s, placing result in d. |
---|
43 | */ |
---|
44 | void _alVectorNormalize( ALfloat *d, const ALfloat *s) { |
---|
45 | ALfloat mag; |
---|
46 | |
---|
47 | mag = _alVectorMagnitudeAtZero( s ); |
---|
48 | |
---|
49 | if( mag == 0 ) { |
---|
50 | d[0] = 0.0; d[1] = 0.0; d[2] = 0.0; |
---|
51 | |
---|
52 | return; |
---|
53 | } |
---|
54 | |
---|
55 | d[0] = s[0] / mag; |
---|
56 | d[1] = s[1] / mag; |
---|
57 | d[2] = s[2] / mag; |
---|
58 | |
---|
59 | return; |
---|
60 | } |
---|
61 | |
---|
62 | /* |
---|
63 | * _alVectorAngleBetween( const ALfloat *origin, const ALfloat *v1, |
---|
64 | * const ALfloat *v2 ) |
---|
65 | * |
---|
66 | * Returns the angle between two vectors, with origins at origin. |
---|
67 | * |
---|
68 | * FIXME: please check my math |
---|
69 | */ |
---|
70 | ALfloat _alVectorAngleBetween( const ALfloat *origin, const ALfloat *v1, |
---|
71 | const ALfloat *v2 ) { |
---|
72 | ALfloat m1; /* |v2| */ |
---|
73 | ALfloat m2; /* |v1| */ |
---|
74 | ALfloat mt; /* |v1| * |v2| */ |
---|
75 | ALfloat dp; /* dot product */ |
---|
76 | ALfloat mag; |
---|
77 | |
---|
78 | m1 = _alVectorMagnitude( origin, v1 ); |
---|
79 | m2 = _alVectorMagnitude( origin, v2 ); |
---|
80 | dp = _alVectorDotp( origin, v1, v2 ); |
---|
81 | |
---|
82 | mt = m1 * m2; |
---|
83 | if(mt == 0.0f) { |
---|
84 | return M_PI / 2.0f; |
---|
85 | } |
---|
86 | |
---|
87 | mag = acos( dp / mt ); |
---|
88 | |
---|
89 | return mag; |
---|
90 | } |
---|
91 | |
---|
92 | /* |
---|
93 | * _alVectorDotp( const ALfloat *origin, const ALfloat *v1, |
---|
94 | * const ALfloat *v2 ) |
---|
95 | * |
---|
96 | * Returns dot product between v1 and v2, with origin at origin. |
---|
97 | */ |
---|
98 | ALfloat _alVectorDotp( const ALfloat *origin, const ALfloat *v1, |
---|
99 | const ALfloat *v2 ) { |
---|
100 | ALfloat o_inverse[3]; |
---|
101 | ALfloat v1_trans[3]; |
---|
102 | ALfloat v2_trans[3]; |
---|
103 | ALfloat retval = 0.0f; |
---|
104 | |
---|
105 | _alVectorInverse( o_inverse, origin ); |
---|
106 | |
---|
107 | _alVectorTranslate( v1_trans, v1, o_inverse ); |
---|
108 | _alVectorTranslate( v2_trans, v2, o_inverse ); |
---|
109 | |
---|
110 | retval += v1_trans[0] * v2_trans[0]; |
---|
111 | retval += v1_trans[1] * v2_trans[1]; |
---|
112 | retval += v1_trans[2] * v2_trans[2]; |
---|
113 | |
---|
114 | return retval; |
---|
115 | } |
---|