diff options
author | Tyler Murphy <tylermurphy534@gmail.com> | 2023-01-19 13:39:40 -0500 |
---|---|---|
committer | Tyler Murphy <tylermurphy534@gmail.com> | 2023-01-19 13:39:40 -0500 |
commit | 76446f179d3de3b5635e8b4c40e1bd49ea99995c (patch) | |
tree | 9d79082ecde0866d27062f9b2d940f85caefbb21 /public/gl/math | |
parent | merge (diff) | |
download | webgl-76446f179d3de3b5635e8b4c40e1bd49ea99995c.tar.gz webgl-76446f179d3de3b5635e8b4c40e1bd49ea99995c.tar.bz2 webgl-76446f179d3de3b5635e8b4c40e1bd49ea99995c.zip |
cube working
Diffstat (limited to 'public/gl/math')
-rw-r--r-- | public/gl/math/Mat4.js | 84 | ||||
-rw-r--r-- | public/gl/math/Math.js | 3 | ||||
-rw-r--r-- | public/gl/math/Vec2.js | 78 | ||||
-rw-r--r-- | public/gl/math/Vec3.js | 19 |
4 files changed, 77 insertions, 107 deletions
diff --git a/public/gl/math/Mat4.js b/public/gl/math/Mat4.js index 3e7c9be..3481440 100644 --- a/public/gl/math/Mat4.js +++ b/public/gl/math/Mat4.js @@ -1,11 +1,27 @@ +const PI = Math.PI +const PI2 = PI * 2 +const PIH = PI * 0.5 +const cosFromSin = (sin, angle) => { + const pih = PI / 2; + const pi2 = PI * 2 + const cos = Math.sqrt(1.0 - sin * sin); + const a = angle + PIH; + const b = a - parseInt(a / PI2) * PI2; + if (b < 0.0) + b = PIH + b; + if (b >= PI) + return -cos; + return cos; +} + export class Mat4 { constructor() { this.data = [ - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0, - 0, 0, 0, 1 + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0 ] } @@ -65,25 +81,45 @@ export class Mat4 { } rot(v) { - const da = this.data; - const c3 = Math.cos(v.z) - const s3 = Math.sin(v.z) - const c2 = Math.cos(v.x) - const s2 = Math.sin(v.x) - const c1 = Math.cos(v.y) - const s1 = Math.sin(v.y) + this.rotX(v.x) + this.rotY(v.y) + this.rotZ(v.z) + + return this + } - da[0] = c1 * c3 + s1 * s2 * s3 - da[4] = c2 * s3 - da[8] = c1 * s2 * s3 - c3 * s1 + rotX(a) { + const s = Math.sin(a), c = cosFromSin(s, a); + const d = this.data; - da[1] = c3 * s1 * s2 - c1 * s3 - da[5] = c2 * c3 - da[9] = c1 * c3 * s2 + s1 * s3 + d[5] = c + d[6] = s + d[9] = -s + d[10] = c - da[2] = c2 * s1 - da[6] = -s2 - da[10] = c1 * c2 + return this + } + + rotY(a) { + const s = Math.sin(a), c = cosFromSin(s, a); + const d = this.data; + + d[0] = c + d[3] = -s + d[8] = c + d[10] = c + + return this + } + + rotZ(a) { + const s = Math.sin(a), c = cosFromSin(s, a); + const d = this.data; + + d[0] = c + d[1] = s + d[4] = -s + d[5] = c return this } @@ -100,12 +136,6 @@ export class Mat4 { } get() { - const d = this.data - return new Float32Array([ - d[0], d[4], d[8], d[12], - d[1], d[5], d[9], d[13], - d[2], d[6], d[10], d[14], - d[3], d[7], d[11], d[15] - ]) + return new Float32Array(this.data) } }
\ No newline at end of file diff --git a/public/gl/math/Math.js b/public/gl/math/Math.js new file mode 100644 index 0000000..ed38570 --- /dev/null +++ b/public/gl/math/Math.js @@ -0,0 +1,3 @@ +export const M = {} + +M.clamp = (num, min, max) => Math.min(Math.max(num, min), max)
\ No newline at end of file diff --git a/public/gl/math/Vec2.js b/public/gl/math/Vec2.js deleted file mode 100644 index 6156e40..0000000 --- a/public/gl/math/Vec2.js +++ /dev/null @@ -1,78 +0,0 @@ -export class Vec2 { - - constructor(x = 0, y = 0) { - this.x = x - this.y = y - } - - set(x,y) { - this.x = x - this.y = y - return this - } - - clone() { - return new Vec2(this.x, this.y) - } - - add(v) { - this.x += v.x - this.y += v.y - return this - } - - sub(v) { - this.x -= v.x - this.y -= v.y - return this - } - - copy(v) { - this.x = v.x - this.y = v.y - return this - } - - multV(v) { - this.x *= v.x - this.y *= v.y - return this - } - - multS(s) { - this.x *= s - this.y *= s - return this - } - - divV(v) { - this.x *= (1 / v.x) - this.y *= (1 / v.y) - return this - } - - divS(s) { - this.x *= (1 / s) - this.y *= (1 / s) - return this - } - - invert() { - this.x = this.x == 0 ? 0 : (1 / this.x) - this.y = this.y == 0 ? 0 : (1 / this.y) - return this - } - - normalize() { - return this.divS(this.length() || 1) - } - - dot(v) { - return this.x * v.x + this.y * v.y; - } - - length() { - return Math.sqrt(this.x * this.x + this.y * this.y); - } - -}
\ No newline at end of file diff --git a/public/gl/math/Vec3.js b/public/gl/math/Vec3.js index d77d49d..87ad778 100644 --- a/public/gl/math/Vec3.js +++ b/public/gl/math/Vec3.js @@ -20,14 +20,14 @@ export class Vec3 { add(v) { this.x += v.x this.y += v.y - this.x += v.x + this.z += v.z return this } sub(v) { this.x -= v.x this.y -= v.y - this.x -= v.z + this.z -= v.z return this } @@ -80,6 +80,21 @@ export class Vec3 { dot(v) { return this.x * v.x + this.y * v.y + this.z * v.z; } + + cross(v) { + return this.crossV(this, v) + } + + crossV(a, b) { + const ax = a.x, ay = a.y, az = a.z; + const bx = b.x, by = b.y, bz = b.z; + + this.x = ay * bz - az * by; + this.y = az * bx - ax * bz; + this.z = ax * by - ay * bx; + + return this; + } length() { return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z); |