Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Converted Vector.c to Javascript, parsing but not tested. Rest of Mat…

…rix now.
  • Loading branch information...
commit 9f83e24e3d672203a0af9c08b702c4fba7fa2830 1 parent 2863757
stephen white web3d0 authored
4 demos/camera/SphereView.j
@@ -6,6 +6,10 @@
6 6 @import "../../materials/RandomColorMaterial.j"
7 7 @import "../../renderers/RendererManager.j"
8 8
  9 +@import "../../math/Matrix.j"
  10 +@import "../../math/Vector.j"
  11 +
  12 +
9 13 @implementation SphereView : GLView {
10 14 GLContext _glContext;
11 15
51 math/Matrix.h
@@ -15,54 +15,3 @@
15 15 #define _CGRectIsEmpty(aRect) (aRect.size.width <= 0.0 || aRect.size.height <= 0.0)
16 16 #define _CGInsetIsEmpty(anInset) ((anInset).top === 0 && (anInset).right === 0 && (anInset).bottom === 0 && (anInset).left === 0)
17 17 */
18   -/*
19   -struct Vector;
20   -
21   -struct Matrix {
22   - float m[16];
23   -};
24   -
25   -void Matrix_loadIdentity(Matrix * matrix1);
26   -Matrix Matrix_identity();
27   -
28   -Matrix Matrix_withValues(float m0, float m4, float m8, float m12,
29   - float m1, float m5, float m9, float m13,
30   - float m2, float m6, float m10, float m14,
31   - float m3, float m7, float m11, float m15);
32   -Matrix Matrix_fromDirectionVectors(struct Vector right, struct Vector up, struct Vector front);
33   -
34   -void Matrix_multiply(Matrix * matrix1, Matrix matrix2);
35   -Matrix Matrix_multiplied(Matrix matrix1, Matrix matrix2);
36   -
37   -void Matrix_translate(Matrix * matrix1, float x, float y, float z);
38   -Matrix Matrix_translated(Matrix matrix1, float x, float y, float z);
39   -
40   -void Matrix_scale(Matrix * matrix, float x, float y, float z);
41   -Matrix Matrix_scaled(Matrix matrix, float x, float y, float z);
42   -
43   -void Matrix_rotate(Matrix * matrix, struct Vector axis, float angle);
44   -Matrix Matrix_rotated(Matrix matrix, struct Vector axis, float angle);
45   -
46   -void Matrix_shearX(Matrix * matrix, float y, float z);
47   -Matrix Matrix_shearedX(Matrix matrix, float y, float z);
48   -
49   -void Matrix_shearY(Matrix * matrix, float x, float z);
50   -Matrix Matrix_shearedY(Matrix matrix, float x, float z);
51   -
52   -void Matrix_shearZ(Matrix * matrix, float x, float y);
53   -Matrix Matrix_shearedZ(Matrix matrix, float x, float y);
54   -
55   -void Matrix_applyPerspective(Matrix * matrix, float fovY, float aspect, float zNear, float zFar);
56   -Matrix Matrix_perspective(Matrix matrix, float fovY, float aspect, float zNear, float zFar);
57   -
58   -void Matrix_transpose(Matrix * matrix);
59   -Matrix Matrix_transposed(Matrix matrix);
60   -
61   -float Matrix_determinant(Matrix matrix);
62   -
63   -void Matrix_invert(Matrix * matrix);
64   -Matrix Matrix_inverted(Matrix matrix);
65   -
66   -struct Vector Matrix_multiplyVector(Matrix matrix, struct Vector vector);
67   -
68   -*/
41 math/Quaternion.h
... ... @@ -0,0 +1,41 @@
  1 +#ifndef __QUATERNION_H__
  2 +#define __QUATERNION_H__
  3 +
  4 +typedef struct Quaternion Quaternion;
  5 +
  6 +struct Vector;
  7 +struct Matrix;
  8 +
  9 +struct Quaternion {
  10 + float x;
  11 + float y;
  12 + float z;
  13 + float w;
  14 +};
  15 +
  16 +void Quaternion_loadIdentity(Quaternion * quaternion);
  17 +Quaternion Quaternion_identity();
  18 +Quaternion Quaternion_withValues(float x, float y, float z, float w);
  19 +
  20 +Quaternion Quaternion_fromVector(struct Vector vector);
  21 +struct Vector Quaternion_toVector(Quaternion quaternion);
  22 +Quaternion Quaternion_fromAxisAngle(struct Vector axis, float angle);
  23 +void Quaternion_toAxisAngle(Quaternion quaternion, struct Vector * axis, float * angle);
  24 +struct Matrix Quaternion_toMatrix(Quaternion quaternion);
  25 +
  26 +void Quaternion_normalize(Quaternion * quaternion);
  27 +Quaternion Quaternion_normalized(Quaternion quaternion);
  28 +
  29 +void Quaternion_multiply(Quaternion * quaternion1, Quaternion quaternion2);
  30 +Quaternion Quaternion_multiplied(Quaternion quaternion1, Quaternion quaternion2);
  31 +Quaternion Quaternion_slerp(Quaternion start, Quaternion end, float alpha);
  32 +
  33 +void Quaternion_rotate(Quaternion * quaternion, struct Vector axis, float angle);
  34 +Quaternion Quaternion_rotated(Quaternion quaternion, struct Vector axis, float angle);
  35 +
  36 +void Quaternion_invert(Quaternion * quaternion);
  37 +Quaternion Quaternion_inverted(Quaternion quaternion);
  38 +
  39 +struct Vector Quaternion_multiplyVector(Quaternion quaternion, struct Vector vector);
  40 +
  41 +#endif
221 math/Quaternion.j
... ... @@ -0,0 +1,221 @@
  1 +#include "Quaternion.h"
  2 +
  3 +#include <math.h>
  4 +#include <stdlib.h>
  5 +
  6 +#include "Matrix.h"
  7 +#include "Vector.h"
  8 +
  9 +void Quaternion_loadIdentity(Quaternion * quaternion) {
  10 + quaternion->x = 0.0f;
  11 + quaternion->y = 0.0f;
  12 + quaternion->z = 0.0f;
  13 + quaternion->w = 1.0f;
  14 +}
  15 +
  16 +Quaternion Quaternion_identity() {
  17 + Quaternion quaternion;
  18 +
  19 + Quaternion_loadIdentity(&quaternion);
  20 + return quaternion;
  21 +}
  22 +
  23 +Quaternion Quaternion_withValues(float x, float y, float z, float w) {
  24 + Quaternion quaternion;
  25 +
  26 + quaternion.x = x;
  27 + quaternion.y = y;
  28 + quaternion.z = z;
  29 + quaternion.w = w;
  30 + return quaternion;
  31 +}
  32 +
  33 +Quaternion Quaternion_fromVector(Vector vector) {
  34 + Quaternion quaternion;
  35 +
  36 + quaternion.x = vector.x;
  37 + quaternion.y = vector.y;
  38 + quaternion.z = vector.z;
  39 + quaternion.w = 0.0f;
  40 + return quaternion;
  41 +}
  42 +
  43 +Vector Quaternion_toVector(Quaternion quaternion) {
  44 + Vector vector;
  45 +
  46 + vector.x = quaternion.x;
  47 + vector.y = quaternion.y;
  48 + vector.z = quaternion.z;
  49 +
  50 + return vector;
  51 +}
  52 +
  53 +Quaternion Quaternion_fromAxisAngle(Vector axis, float angle) {
  54 + Quaternion quaternion;
  55 + float sinAngle;
  56 +
  57 + angle *= 0.5f;
  58 + Vector_normalize(&axis);
  59 + sinAngle = sin(angle);
  60 + quaternion.x = (axis.x * sinAngle);
  61 + quaternion.y = (axis.y * sinAngle);
  62 + quaternion.z = (axis.z * sinAngle);
  63 + quaternion.w = cos(angle);
  64 +
  65 + return quaternion;
  66 +}
  67 +
  68 +void Quaternion_toAxisAngle(Quaternion quaternion, Vector * axis, float * angle) {
  69 + float sinAngle;
  70 +
  71 + Quaternion_normalize(&quaternion);
  72 + sinAngle = sqrt(1.0f - (quaternion.w * quaternion.w));
  73 + if (fabs(sinAngle) < 0.0005f) sinAngle = 1.0f;
  74 +
  75 + if (axis != NULL) {
  76 + axis->x = (quaternion.x / sinAngle);
  77 + axis->y = (quaternion.y / sinAngle);
  78 + axis->z = (quaternion.z / sinAngle);
  79 + }
  80 +
  81 + if (angle != NULL) {
  82 + *angle = (acos(quaternion.w) * 2.0f);
  83 + }
  84 +}
  85 +
  86 +Matrix Quaternion_toMatrix(Quaternion quaternion) {
  87 + Matrix matrix;
  88 +
  89 + matrix.m[0] = (1.0f - (2.0f * ((quaternion.y * quaternion.y) + (quaternion.z * quaternion.z))));
  90 + matrix.m[1] = (2.0f * ((quaternion.x * quaternion.y) + (quaternion.z * quaternion.w)));
  91 + matrix.m[2] = (2.0f * ((quaternion.x * quaternion.z) - (quaternion.y * quaternion.w)));
  92 + matrix.m[3] = 0.0f;
  93 + matrix.m[4] = (2.0f * ((quaternion.x * quaternion.y) - (quaternion.z * quaternion.w)));
  94 + matrix.m[5] = (1.0f - (2.0f * ((quaternion.x * quaternion.x) + (quaternion.z * quaternion.z))));
  95 + matrix.m[6] = (2.0f * ((quaternion.y * quaternion.z) + (quaternion.x * quaternion.w)));
  96 + matrix.m[7] = 0.0f;
  97 + matrix.m[8] = (2.0f * ((quaternion.x * quaternion.z) + (quaternion.y * quaternion.w)));
  98 + matrix.m[9] = (2.0f * ((quaternion.y * quaternion.z) - (quaternion.x * quaternion.w)));
  99 + matrix.m[10] = (1.0f - (2.0f * ((quaternion.x * quaternion.x) + (quaternion.y * quaternion.y))));
  100 + matrix.m[11] = 0.0f;
  101 + matrix.m[12] = 0.0f;
  102 + matrix.m[13] = 0.0f;
  103 + matrix.m[14] = 0.0f;
  104 + matrix.m[15] = 1.0f;
  105 +
  106 + return matrix;
  107 +}
  108 +
  109 +void Quaternion_normalize(Quaternion * quaternion) {
  110 + float magnitude;
  111 +
  112 + magnitude = sqrt((quaternion->x * quaternion->x) +
  113 + (quaternion->y * quaternion->y) +
  114 + (quaternion->z * quaternion->z) +
  115 + (quaternion->w * quaternion->w));
  116 + quaternion->x /= magnitude;
  117 + quaternion->y /= magnitude;
  118 + quaternion->z /= magnitude;
  119 + quaternion->w /= magnitude;
  120 +}
  121 +
  122 +Quaternion Quaternion_normalized(Quaternion quaternion) {
  123 + Quaternion_normalize(&quaternion);
  124 + return quaternion;
  125 +}
  126 +
  127 +void Quaternion_multiply(Quaternion * quaternion1, Quaternion quaternion2) {
  128 + Vector vector1, vector2, cross;
  129 + float angle;
  130 +
  131 + vector1 = Quaternion_toVector(*quaternion1);
  132 + vector2 = Quaternion_toVector(quaternion2);
  133 + angle = (quaternion1->w * quaternion2.w) - Vector_dot(vector1, vector2);
  134 +
  135 + cross = Vector_cross(vector1, vector2);
  136 + vector1.x *= quaternion2.w;
  137 + vector1.y *= quaternion2.w;
  138 + vector1.z *= quaternion2.w;
  139 + vector2.x *= quaternion1->w;
  140 + vector2.y *= quaternion1->w;
  141 + vector2.z *= quaternion1->w;
  142 +
  143 + quaternion1->x = vector1.x + vector2.x + cross.x;
  144 + quaternion1->y = vector1.y + vector2.y + cross.y;
  145 + quaternion1->z = vector1.z + vector2.z + cross.z;
  146 + quaternion1->w = angle;
  147 +}
  148 +
  149 +Quaternion Quaternion_multiplied(Quaternion quaternion1, Quaternion quaternion2) {
  150 + Quaternion_multiply(&quaternion1, quaternion2);
  151 + return quaternion1;
  152 +}
  153 +
  154 +#define SLERP_TO_LERP_SWITCH_THRESHOLD 0.01f
  155 +
  156 +Quaternion Quaternion_slerp(Quaternion start, Quaternion end, float alpha) {
  157 + float startWeight, endWeight, difference;
  158 + Quaternion result;
  159 +
  160 + difference = (start.x * end.x) + (start.y * end.y) + (start.z * end.z) + (start.w * end.w);
  161 + if ((1.0f - fabs(difference)) > SLERP_TO_LERP_SWITCH_THRESHOLD) {
  162 + float theta, oneOverSinTheta;
  163 +
  164 + theta = acos(fabs(difference));
  165 + oneOverSinTheta = 1.0f / sin(theta);
  166 + startWeight = sin(theta * (1.0f - alpha)) * oneOverSinTheta;
  167 + endWeight = sin(theta * alpha) * oneOverSinTheta;
  168 + if (difference < 0.0f) {
  169 + startWeight = -startWeight;
  170 + }
  171 + } else {
  172 + startWeight = 1.0f - alpha;
  173 + endWeight = alpha;
  174 + }
  175 + result.x = (start.x * startWeight) + (end.x * endWeight);
  176 + result.y = (start.y * startWeight) + (end.y * endWeight);
  177 + result.z = (start.z * startWeight) + (end.z * endWeight);
  178 + result.w = (start.w * startWeight) + (end.w * endWeight);
  179 + Quaternion_normalize(&result);
  180 +
  181 + return result;
  182 +}
  183 +
  184 +void Quaternion_rotate(Quaternion * quaternion, Vector axis, float angle) {
  185 + Quaternion rotationQuaternion;
  186 +
  187 + rotationQuaternion = Quaternion_fromAxisAngle(axis, angle);
  188 + Quaternion_multiply(quaternion, rotationQuaternion);
  189 +}
  190 +
  191 +Quaternion Quaternion_rotated(Quaternion quaternion, Vector axis, float angle) {
  192 + Quaternion_rotate(&quaternion, axis, angle);
  193 + return quaternion;
  194 +}
  195 +
  196 +void Quaternion_invert(Quaternion * quaternion) {
  197 + float length;
  198 +
  199 + length = 1.0f / ((quaternion->x * quaternion->x) +
  200 + (quaternion->y * quaternion->y) +
  201 + (quaternion->z * quaternion->z) +
  202 + (quaternion->w * quaternion->w));
  203 + quaternion->x *= -length;
  204 + quaternion->y *= -length;
  205 + quaternion->z *= -length;
  206 + quaternion->w *= length;
  207 +}
  208 +
  209 +Quaternion Quaternion_inverted(Quaternion quaternion) {
  210 + Quaternion_invert(&quaternion);
  211 + return quaternion;
  212 +}
  213 +
  214 +Vector Quaternion_multiplyVector(Quaternion quaternion, Vector vector) {
  215 + Quaternion vectorQuaternion, inverseQuaternion, result;
  216 +
  217 + vectorQuaternion = Quaternion_fromVector(vector);
  218 + inverseQuaternion = Quaternion_inverted(quaternion);
  219 + result = Quaternion_multiplied(quaternion, Quaternion_multiplied(vectorQuaternion, inverseQuaternion));
  220 + return Quaternion_toVector(result);
  221 +}
0  math/Vector.h
No changes.
44 math/Vector.c → math/Vector.j
... ... @@ -1,7 +1,6 @@
1   -#include "Vector.h"
2 1
3   -Vector Vector_withValues(float x, float y, float z) {
4   - Vector vector;
  2 +function Vector_withValues( x, y, z) {
  3 + var vector = {};
5 4
6 5 vector.x = x;
7 6 vector.y = y;
@@ -9,42 +8,51 @@ Vector Vector_withValues(float x, float y, float z) {
9 8 return vector;
10 9 }
11 10
12   -void Vector_normalize(Vector * vector) {
13   - float magnitude;
  11 +function Vector_withVector( vector) {
  12 + var result = {};
  13 +
  14 + result.x = matrix.x;
  15 + result.y = matrix.y;
  16 + result.z = matrix.z;
  17 + return result;
  18 +}
  19 +
  20 +function Vector_normalize( vector) {
  21 + var magnitude;
14 22
15   - magnitude = sqrt((vector->x * vector->x) + (vector->y * vector->y) + (vector->z * vector->z));
16   - vector->x /= magnitude;
17   - vector->y /= magnitude;
18   - vector->z /= magnitude;
  23 + magnitude = Math.sqrt((vector.x * vector.x) + (vector.y * vector.y) + (vector.z * vector.z));
  24 + vector.x /= magnitude;
  25 + vector.y /= magnitude;
  26 + vector.z /= magnitude;
19 27 }
20 28
21   -Vector Vector_normalized(Vector vector) {
22   - Vector_normalize(&vector);
  29 +function Vector_normalized( vector) {
  30 + Vector_normalize(vector);
23 31 return vector;
24 32 }
25 33
26   -float Vector_magnitude(Vector vector) {
  34 +function Vector_magnitude( vector) {
27 35 return sqrt((vector.x * vector.x) + (vector.y * vector.y) + (vector.z * vector.z));
28 36 }
29 37
30   -float Vector_magnitudeSquared(Vector vector) {
  38 +function Vector_magnitudeSquared( vector) {
31 39 return ((vector.x * vector.x) + (vector.y * vector.y) + (vector.z * vector.z));
32 40 }
33 41
34   -Vector Vector_add(Vector vector1, Vector vector2) {
  42 +function Vector_add( vector1, vector2) {
35 43 return Vector_withValues((vector1.x + vector2.x), (vector1.y + vector2.y), (vector1.z + vector2.z));
36 44 }
37 45
38   -Vector Vector_subtract(Vector vector1, Vector vector2) {
  46 +function Vector_subtract( vector1, vector2) {
39 47 return Vector_withValues((vector1.x - vector2.x), (vector1.y - vector2.y), (vector1.z - vector2.z));
40 48 }
41 49
42   -float Vector_dot(Vector vector1, Vector vector2) {
  50 +function Vector_dot( vector1, vector2) {
43 51 return ((vector1.x * vector2.x) + (vector1.y * vector2.y) + (vector1.z * vector2.z));
44 52 }
45 53
46   -Vector Vector_cross(Vector vector1, Vector vector2) {
47   - Vector result;
  54 +function Vector_cross( vector1, vector2) {
  55 + var result = {};
48 56
49 57 result.x = ((vector1.y * vector2.z) - (vector1.z * vector2.y));
50 58 result.y = ((vector1.z * vector2.x) - (vector1.x * vector2.z));

0 comments on commit 9f83e24

Please sign in to comment.
Something went wrong with that request. Please try again.