forked from jlamarche/MC3D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MC3DVector.c
380 lines (349 loc) · 9.28 KB
/
MC3DVector.c
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include "MC3DVector.h"
#pragma mark -
#pragma mark Normalize
void vec2Normalize(vec2 *vector) {
GLfloat vecMag = vec2Magnitude(*vector);
if (vecMag == 0.0) {
vector->x = 1.f;
vector->y = 0.f;
return;
}
vector->x /= vecMag;
vector->y /= vecMag;
}
void vec3Normalize(vec3 *vector) {
GLfloat vecMag = vec3Magnitude(*vector);
if ( vecMag == 0.0 )
{
vector->x = 1.0;
vector->y = 0.0;
vector->z = 0.0;
return;
}
vector->x /= vecMag;
vector->y /= vecMag;
vector->z /= vecMag;
}
void vec4Normalize(vec4 *vector) {
GLfloat vecMag = vec4Magnitude(*vector);
if (vecMag == 0.0) {
vector->x = 1.0;
vector->y = 0.0;
vector->z = 0.0;
vector->w = 1.0;
return;
}
vector->x /= vecMag;
vector->y /= vecMag;
vector->z /= vecMag;
vector->w /= vecMag;
}
#pragma mark -
#pragma mark Comparing Vectors
GLboolean vec2Equal(vec2 vector1, vec2 vector2) {
if (vector1.x == vector2.x && vector1.y == vector2.y)
return GL_TRUE;
return GL_FALSE;
}
GLboolean vec3Equal(vec3 vector1, vec3 vector2) {
if (vector1.x == vector2.x && vector1.y == vector2.y && vector1.z == vector2.z)
return GL_TRUE;
return GL_FALSE;
}
GLboolean vec4Equal(vec4 vector1, vec4 vector2) {
if (vector1.x == vector2.x && vector1.y == vector2.y && vector1.z == vector2.z && vector1.w == vector2.w)
return GL_TRUE;
return GL_FALSE;
}
#pragma mark -
#pragma mark Dot Product
GLfloat vec2DotProduct(vec2 vector1, vec2 vector2) {
return vector1.x*vector2.x + vector1.y*vector2.y;
}
GLfloat vec3DotProduct(vec3 vector1, vec3 vector2) {
return vector1.x*vector2.x + vector1.y*vector2.y + vector1.z*vector2.z;
}
GLfloat vec4DotProduct(vec4 vector1, vec4 vector2) {
return vector1.x*vector2.x + vector1.y*vector2.y + vector1.z*vector2.z + vector1.w*vector2.w;
}
#pragma mark -
#pragma mark Cross Product
vec3 vec3CrossProduct(vec3 vector1, vec3 vector2) {
vec3 ret;
ret.x = (vector1.y * vector2.z) - (vector1.z * vector2.y);
ret.y = (vector1.z * vector2.x) - (vector1.x * vector2.z);
ret.z = (vector1.x * vector2.y) - (vector1.y * vector2.x);
return ret;
}
#pragma mark -
#pragma mark Make With Points
vec2 vec2MakeWithPoints(vec2 start, vec2 end) {
vec2 ret;
ret.x = end.x - start.x;
ret.y = end.y - start.y;
vec2Normalize(&ret);
return ret;
}
vec3 vec3MakeWithPoints(vec3 start, vec3 end) {
vec3 ret;
ret.x = end.x - start.x;
ret.y = end.y - start.y;
ret.z = end.z - start.z;
vec3Normalize(&ret);
return ret;
}
vec4 vec4MakeWithPoints(vec4 start, vec4 end) {
vec4 ret;
ret.x = end.x - start.x;
ret.y = end.y - start.y;
ret.z = end.z - start.z;
ret.w = end.w - start.w;
vec4Normalize(&ret);
return ret;
}
#pragma mark -
#pragma mark Length
GLfloat vec2Length(vec2 vector) {
return sqrtf(vector.x * vector.x + vector.y + vector.y);
}
GLfloat vec3Length(vec3 vector) {
return sqrtf(vector.x * vector.x + vector.y + vector.y + vector.z * vector.z);
}
GLfloat vec4Length(vec4 vector) {
return sqrtf(vector.x * vector.x + vector.y + vector.y + vector.z * vector.z + vector.w * vector.w);
}
#pragma mark -
#pragma mark Vector Add
vec2 vec2Add(vec2 vector1, vec2 vector2) {
vec2 ret;
ret.x = vector1.x + vector2.x;
ret.y = vector1.y + vector2.y;
return ret;
}
vec3 vec3Add(vec3 vector1, vec3 vector2) {
vec3 ret;
ret.x = vector1.x + vector2.x;
ret.y = vector1.y + vector2.y;
ret.z = vector1.z + vector2.z;
return ret;
}
vec4 vec4Add(vec4 vector1, vec4 vector2) {
vec4 ret;
ret.x = vector1.x + vector2.x;
ret.y = vector1.y + vector2.y;
ret.z = vector1.z + vector2.z;
ret.w = vector1.w + vector2.w;
return ret;
}
void vec2AddTo(vec2 *vector1, vec2 vector2) {
vector1->x += vector2.x;
vector1->y += vector2.y;
}
void vec3AddTo(vec3 *vector1, vec3 vector2) {
vector1->x += vector2.x;
vector1->y += vector2.y;
vector1->z += vector2.z;
}
void vec4AddTo(vec4 *vector1, vec4 vector2) {
vector1->x += vector2.x;
vector1->y += vector2.y;
vector1->z += vector2.z;
vector1->w += vector2.w;
}
#pragma mark -
#pragma mark Vector Subtract
vec2 vec2Subtract(vec2 vector1, vec2 vector2) {
vec2 ret;
ret.x = vector1.x - vector2.x;
ret.y = vector1.y - vector2.y;
return ret;
}
vec3 vec3Subtract(vec3 vector1, vec3 vector2) {
vec3 ret;
ret.x = vector1.x - vector2.x;
ret.y = vector1.y - vector2.y;
ret.z = vector1.z - vector2.z;
return ret;
}
vec4 vec4Subtract(vec4 vector1, vec4 vector2) {
vec4 ret;
ret.x = vector1.x - vector2.x;
ret.y = vector1.y - vector2.y;
ret.z = vector1.z - vector2.z;
ret.w = vector1.w - vector2.w;
return ret;
}
void vec2SubtractFrom(vec2 *vector1, vec2 vector2) {
vector1->x -= vector2.x;
vector1->y -= vector2.y;
}
void vec3SubtractFrom(vec3 *vector1, vec3 vector2) {
vector1->x += vector2.x;
vector1->y += vector2.y;
vector1->z += vector2.z;
}
void vec4SubtractFrom(vec4 *vector1, vec4 vector2) {
vector1->x -= vector2.x;
vector1->y -= vector2.y;
vector1->z -= vector2.z;
vector1->w -= vector2.w;
}
#pragma mark -
#pragma mark Vector Multiplication
vec2 vec2Multiply(vec2 vector1, GLfloat scalar) {
vec2 ret;
ret.x = vector1.x * scalar;
ret.y = vector1.y * scalar;
return ret;
}
vec3 vec3Multiply(vec3 vector1, GLfloat scalar) {
vec3 ret;
ret.x = vector1.x * scalar;
ret.y = vector1.y * scalar;
ret.z = vector1.z * scalar;
return ret;
}
vec4 vec4Multiply(vec4 vector1, GLfloat scalar) {
vec4 ret;
ret.x = vector1.x * scalar;
ret.y = vector1.y * scalar;
ret.z = vector1.z * scalar;
ret.w = vector1.w * scalar;
return ret;
}
void vec2MultiplyBy(vec2 *vector1, GLfloat scalar) {
vector1->x *= scalar;
vector1->y *= scalar;
}
void vec3MultiplyBy(vec3 *vector1, GLfloat scalar) {
vector1->x *= scalar;
vector1->y *= scalar;
vector1->z *= scalar;
}
void vecrMultiplyBy(vec4 *vector1, GLfloat scalar) {
vector1->x *= scalar;
vector1->y *= scalar;
vector1->z *= scalar;
vector1->w *= scalar;
}
#pragma mark -
#pragma mark Vector Division
vec2 vec2Divide(vec2 vector1, GLfloat scalar) {
vec2 ret;
if (scalar == 0.f) {
printf("Attempt to divide by zero, returning origin");
ret.x = ret.y = 0.f;
}
ret.x = vector1.x / scalar;
ret.y = vector1.y / scalar;
return ret;
}
vec3 vec3Divide(vec3 vector1, GLfloat scalar) {
vec3 ret;
if (scalar == 0.f) {
printf("Attempt to divide by zero, returning origin");
ret.x = ret.y = ret.z = 0.f;
}
ret.x = vector1.x / scalar;
ret.y = vector1.y / scalar;
ret.z = vector1.z / scalar;
return ret;
}
vec4 vec4Divide(vec4 vector1, GLfloat scalar) {
vec4 ret;
if (scalar == 0.f) {
printf("Attempt to divide by zero, returning origin");
ret.x = ret.y = ret.z = ret.w = 0.f;
}
ret.x = vector1.x / scalar;
ret.y = vector1.y / scalar;
ret.z = vector1.z / scalar;
ret.w = vector1.w / scalar;
return ret;
}
void vec2DivideBy(vec2 *vector1, GLfloat scalar) {
if (scalar == 0.f) {
printf("Attempt to divide by zero, leaving original vector untouched");
return;
}
vector1->x /= scalar;
vector1->y /= scalar;
}
void vec3DivideBy(vec3 *vector1, GLfloat scalar) {
if (scalar == 0.f) {
printf("Attempt to divide by zero, leaving original vector untouched");
return;
}
vector1->x /= scalar;
vector1->y /= scalar;
vector1->z /= scalar;
}
void vec4DivideBy(vec4 *vector1, GLfloat scalar) {
if (scalar == 0.f) {
printf("Attempt to divide by zero, leaving original vector untouched");
return;
}
vector1->x /= scalar;
vector1->y /= scalar;
vector1->z /= scalar;
vector1->w /= scalar;
}
#pragma mark -
#pragma mark Vector Flipping
void vec2Flip(vec2 *vector) {
vector->x = -vector->x;
vector->y = -vector->y;
}
void vec3Flip(vec3 *vector) {
vector->x = -vector->x;
vector->y = -vector->y;
vector->z = -vector->z;
}
void vec4Flip(vec4 *vector) {
vector->x = -vector->x;
vector->y = -vector->y;
vector->z = -vector->z;
vector->w = -vector->w;
}
#pragma mark -
#pragma mark Linear Interpolation
vec2 vec2Lerp(vec2 start, vec2 finish, GLclampf amount) {
vec2 ret;
ret.x = start.x * (1.f - amount) + finish.x * amount;
ret.y = start.y * (1.f - amount) + finish.y * amount;
return ret;
}
vec3 vec3Lerp(vec3 start, vec3 finish, GLclampf amount) {
vec3 ret;
ret.x = start.x * (1.f - amount) + finish.x * amount;
ret.y = start.y * (1.f - amount) + finish.y * amount;
ret.z = start.z * (1.f - amount) + finish.z * amount;
return ret;
}
vec4 vec4Lerp(vec4 start, vec4 finish, GLclampf amount) {
vec4 ret;
ret.x = start.x * (1.f - amount) + finish.x * amount;
ret.y = start.y * (1.f - amount) + finish.y * amount;
ret.z = start.z * (1.f - amount) + finish.z * amount;
ret.w = start.w * (1.f - amount) + finish.w * amount;
return ret;
}
#pragma mark -
#pragma mark Quadratic Ease-In
vec2 vec2EaseIn(vec2 start, vec2 finish, GLclampf amount) {
return vec2Lerp(start, finish, amount * amount);
}
vec3 vec3EaseIn(vec3 start, vec3 finish, GLclampf amount) {
return vec3Lerp(start, finish, amount * amount);
}
vec4 vec4EaseIn(vec4 start, vec4 finish, GLclampf amount) {
return vec4Lerp(start, finish, amount * amount);
}
#pragma mark -
#pragma mark Triple Scalar Product
GLfloat vec3TripleScalarProduct(vec3 vector1, vec3 vector2, vec3 vector3) {
return
(vector1.x * (vector2.y * vector3.z - vector2.z * vector3.y)) +
(vector1.y * (-vector2.x * vector3.z + vector2.z * vector3.x)) +
(vector1.z * (vector2.x * vector3.y - vector2.y * vector3.x)) ;
}