forked from jlamarche/MC3D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MC3DVector.h
201 lines (172 loc) · 6.9 KB
/
MC3DVector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
// ^ ____________________
// / \ | |
// / _ \ | MartianCraft 3D |
// ( (_) ) | Platform-Agnostic |
// /| | |\ | 3D Foundation |
// //|__|__|\\ |___________________|
// (/ (|) \)
// \ /
// v
//
// Copyright 2011 Jeff LaMarche & MartianCraft. All rights reserved.
// http://www.martiancraft.com jeff@martiancraft.com
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following condition is met:
//
// Redistributions of source code must retain the above copyright notice, this
// condition and the following disclaimer.
//
// THIS SOFTWARE IS PROVIDED BY JEFF LAMARCHE & MARTIANCRAFT, LLC AS IS AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JEFF LAMARCHE &
// MARTIANCRAFT, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef __MG3DVector__
#define __MG3DVector__
#include "MC3DCommon.h"
#include "MC3DTypes.h"
#pragma mark -
#pragma mark Inline Methods
#ifdef __cplusplus
extern "C" {
#endif
// Certain methods that are called often are inlined for performance
static inline GLfloat vec2Magnitude(vec2 vector) {
return sqrtf((vector.x * vector.x) + (vector.y * vector.y));
}
static inline GLfloat vec3Magnitude(vec3 vector) {
return sqrtf((vector.x * vector.x) + (vector.y * vector.y) + (vector.z * vector.z));
}
static inline GLfloat vec4Magnitude(vec4 vector) {
return sqrtf((vector.x * vector.x) + (vector.y * vector.y) + (vector.z * vector.z) + (vector.w * vector.w));
}
static inline vec2 vec2Make(GLfloat x, GLfloat y) {
vec2 ret;
ret.x = x;
ret.y = y;
return ret;
}
static inline vec3 vec3Make(GLfloat x, GLfloat y, GLfloat z) {
vec3 ret;
ret.x = x;
ret.y = y;
ret.z = z;
return ret;
}
static inline vec4 vec4Make(GLfloat x, GLfloat y, GLfloat z, GLfloat w) {
vec4 ret;
ret.x = x;
ret.y = y;
ret.z = z;
ret.w = w;
return ret;
}
static inline ivec2 ivec2Make(GLint x, GLint y) {
ivec2 ret;
ret.x = x;
ret.y = y;
return ret;
}
static inline ivec3 ivec3Make(GLint x, GLint y, GLint z) {
ivec3 ret;
ret.x = x;
ret.y = y;
ret.z = z;
return ret;
}
static inline ivec4 ivec4Make(GLint x, GLint y, GLint z, GLint w) {
ivec4 ret;
ret.x = x;
ret.y = y;
ret.z = z;
ret.w = w;
return ret;
}
#pragma mark -
#pragma mark Method Prototypes
// Normalizing a vector (changing magnitude to 1.0 without changing direction)
void vec2Normalize(vec2 *vector);
void vec3Normalize(vec3 *vector);
void vec4Normalize(vec4 *vector);
// Comparing vectors
GLboolean vec2Equal(vec2 vector1, vec2 vector2);
GLboolean vec3Equal(vec3 vector1, vec3 vector2);
GLboolean vec4Equal(vec4 vector1, vec4 vector2);
// Dot Product: http://en.wikipedia.org/wiki/Dot_product
GLfloat vec2DotProduct(vec2 vector1, vec2 vector2);
GLfloat vec3DotProduct(vec3 vector1, vec3 vector3);
GLfloat vec4DotProduct(vec4 vector1, vec4 vector4);
// Cross Product: http://en.wikipedia.org/wiki/Cross_product
vec3 vec3CrossProduct(vec3 vector1, vec3 vector2);
// Creates a vector from two vertices
vec2 vec2MakeWithPoints(vec2 start, vec2 end);
vec3 vec3MakeWithPoints(vec3 start, vec3 end);
vec4 vec4MakeWithPoints(vec4 start, vec4 end);
// Calculating length of vector
GLfloat vec2Length(vec2 vector);
GLfloat vec3Length(vec3 vector);
GLfloat vec4Length(vec4 vector);
// Adds two vectors and returns results
vec2 vec2Add(vec2 vector1, vec2 vector2);
vec3 vec3Add(vec3 vector1, vec3 vector2);
vec4 vec4Add(vec4 vector1, vec4 vector2);
// Adds second vector to first, in place
void vec2AddTo(vec2 *vector1, vec2 vector2);
void vec3AddTo(vec3 *vector1, vec3 vector2);
void vec4AddTo(vec4 *vector1, vec4 vector2);
// Subtracting a vector from another vector and returning the result
vec2 vec2Subtract(vec2 vector1, vec2 vector2);
vec3 vec3Subtract(vec3 vector1, vec3 vector2);
vec4 vec4Subtract(vec4 vector1, vec4 vector2);
// Subracting the second vector from the first, in place
void vec2SubtractFrom(vec2 *vector1, vec2 vector2);
void vec3SubtractFrom(vec3 *vector1, vec3 vector2);
void vec4SubtractFrom(vec4 *vector1, vec4 vector2);
// Multiplying a vector by a scalar and returning the result
vec2 vec2Multiply(vec2 vector1, GLfloat scalar);
vec3 vec3Multiply(vec3 vector1, GLfloat scalar);
vec4 vec4Multiply(vec4 vector1, GLfloat scalar);
// Multiplying a vector by a scalar in place
void vec2MultiplyBy(vec2 *vector1, GLfloat scalar);
void vec3MultiplyBy(vec3 *vector1, GLfloat scalar);
void vecrMultiplyBy(vec4 *vector1, GLfloat scalar);
// Dividing a vector by a scalar and returning the result
vec2 vec2Divide(vec2 vector1, GLfloat scalar);
vec3 vec3Divide(vec3 vector1, GLfloat scalar);
vec4 vec4Divide(vec4 vector1, GLfloat scalar);
// Dividing a vector by a scalar in place
void vec2DivideBy(vec2 *vector1, GLfloat scalar);
void vec3DivideBy(vec3 *vector1, GLfloat scalar);
void vec4DivideBy(vec4 *vector1, GLfloat scalar);
// Flipping a vector so it points in opposite direction
void vec2Flip(vec2 *flip);
void vec3Flip(vec3 *flip);
void vec4Flip(vec4 *flip);
// Linear interpolation between two vectors, returning result, amount is from 0.0 to 1.0
vec2 vec2Lerp(vec2 start, vec2 finish, GLclampf amount);
vec3 vec3Lerp(vec3 start, vec3 finish, GLclampf amount);
vec4 vec4Lerp(vec4 start, vec4 finish, GLclampf amount);
// Ease-in interpolation between two vectors, returning result, amount is from 0.0 to 1.0
vec2 vec2EaseIn(vec2 start, vec2 finish, GLclampf amount);
vec3 vec3EaseIn(vec3 start, vec3 finish, GLclampf amount);
vec4 vec4EaseIn(vec4 start, vec4 finish, GLclampf amount);
// Ease-out interpolation between two vectors, returning result, amount is from 0.0 to 1.0
vec2 vec2EaseOut(vec2 start, vec2 finish, GLclampf amount);
vec3 vec3EaseOut(vec3 start, vec3 finish, GLclampf amount);
vec4 vec4EaseOut(vec4 start, vec4 finish, GLclampf amount);
// Ease-in-out interpolation between two vectors, returning results, amount is from 0.0 to 1.0
vec2 vec2EaseInOut(vec2 start, vec2 finish, GLclampf amount);
vec3 vec3EaseInOut(vec3 start, vec3 finish, GLclampf amount);
vec4 vec4EaseInOut(vec4 start, vec4 finish, GLclampf amount);
// Triple scalar product, used in some physics simulation calculations
GLfloat vec3TripleScalarProduct(vec3 vector1, vec3 vector2, vec3 vector3);
#ifdef __cplusplus
}
#endif
#endif