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/core | |
parent | merge (diff) | |
download | webgl-76446f179d3de3b5635e8b4c40e1bd49ea99995c.tar.gz webgl-76446f179d3de3b5635e8b4c40e1bd49ea99995c.tar.bz2 webgl-76446f179d3de3b5635e8b4c40e1bd49ea99995c.zip |
cube working
Diffstat (limited to 'public/gl/core')
-rw-r--r-- | public/gl/core/Camera.js | 36 | ||||
-rw-r--r-- | public/gl/core/Entity.js | 37 | ||||
-rw-r--r-- | public/gl/core/Renderer.js | 20 | ||||
-rw-r--r-- | public/gl/core/Shader.js | 3 |
4 files changed, 75 insertions, 21 deletions
diff --git a/public/gl/core/Camera.js b/public/gl/core/Camera.js index 870c13f..353b5e9 100644 --- a/public/gl/core/Camera.js +++ b/public/gl/core/Camera.js @@ -9,10 +9,38 @@ export class Camera { } view() { - return new Mat4() - .identity() - .rot(this.rotation.clone().multS(Math.PI/180)) - .pos(this.position.clone().multS(-1)) + const view = new Mat4() + const d = view.data + + const c3 = Math.cos(this.rotation.z * (Math.PI / 180)) + const s3 = Math.sin(this.rotation.z * (Math.PI / 180)) + const c2 = Math.cos(this.rotation.x * (Math.PI / 180)) + const s2 = Math.sin(this.rotation.x * (Math.PI / 180)) + const c1 = Math.cos(this.rotation.y * (Math.PI / 180)) + const s1 = Math.sin(this.rotation.y * (Math.PI / 180)) + + const u = new Vec3((c1 * c3 + s1 * s2 * s3), (c2 * s3), (c1 * s2 * s3 - c3 * s1)); + const v = new Vec3((c3 * s1 * s2 - c1 * s3), (c2 * c3), (c1 * c3 * s2 + s1 * s3)); + const w = new Vec3((c2 * s1), (-s2), (c1 * c2)); + + d[0] = u.x; + d[1] = v.x; + d[2] = w.x; + d[3] = 1 + d[4] = u.y; + d[5] = v.y; + d[6] = w.y; + d[7] = 1 + d[8] = u.z; + d[9] = v.z; + d[10] = w.z; + d[11] = 1 + d[12] = -u.dot(this.position) + d[13] = -v.dot(this.position) + d[14] = -w.dot(this.position) + d[15] = 1 + + return view } }
\ No newline at end of file diff --git a/public/gl/core/Entity.js b/public/gl/core/Entity.js index 43ea87d..e24680a 100644 --- a/public/gl/core/Entity.js +++ b/public/gl/core/Entity.js @@ -18,11 +18,38 @@ export class Entity { this.rotation.x %= 360; this.rotation.y %= 360; this.rotation.z %= 360; - return new Mat4() - .identity() - .pos(this.position) - .rot(this.rotation.clone().multS(Math.PI/180)) - .scale(this.scale) + + const tran = new Mat4().identity() + const d = tran.data + + const c3 = Math.cos(this.rotation.z * (Math.PI / 180)) + const s3 = Math.sin(this.rotation.z * (Math.PI / 180)) + const c2 = Math.cos(this.rotation.x * (Math.PI / 180)) + const s2 = Math.sin(this.rotation.x * (Math.PI / 180)) + const c1 = Math.cos(this.rotation.y * (Math.PI / 180)) + const s1 = Math.sin(this.rotation.y * (Math.PI / 180)) + + d[0] = this.scale.x * (c1 * c3 + s1 * s2 * s3); + d[1] = this.scale.x * (c2 * s3) + d[2] = this.scale.x * (c1 * s2 * s3 - c3 * s1) + d[3] = 0 + + d[4] = this.scale.y * (c3 * s1 * s2 - c1 * s3) + d[5] = this.scale.y * (c2 * c3) + d[6] = this.scale.y * (c1 * c3 * s2 + s1 * s3) + d[7] = 0 + + d[8] = this.scale.z * (c2 * s1) + d[9] = this.scale.z * (-s2) + d[10] = this.scale.z * (c1 * c2) + d[11] = 0 + + d[12] = this.position.x + d[13] = this.position.y + d[14] = this.position.z + d[15] = 1 + + return tran } }
\ No newline at end of file diff --git a/public/gl/core/Renderer.js b/public/gl/core/Renderer.js index b3b2121..7c96379 100644 --- a/public/gl/core/Renderer.js +++ b/public/gl/core/Renderer.js @@ -61,20 +61,18 @@ export class Renderer { } proj() { + const proj = new Mat4() + const d = proj.data + const tanHalfFovy = Math.tan((this.FOV * (Math.PI/180))/ 2.0); const aspect = canvas.width / canvas.height - const f = 1.0 / Math.tan((this.FOV * (Math.PI/180)) / 2) - const fa = f / aspect - const nf = 1.0 / (this.NEAR - this.FAR) - const c1 = (this.NEAR + this.FAR) * nf - const c2 = this.NEAR * this.FAR * nf * 2 + d[0] = 1.0 / (aspect / tanHalfFovy) + d[5] = 1.0 / tanHalfFovy + d[10] = this.FAR / (this.FAR - this.NEAR) + d[11] = 1.0 + d[14] = -(this.FAR * this.NEAR) / (this.FAR) - return new Mat4().set( - fa, 0, 0, 0, - 0, f, 0, 0, - 0, 0, c1, c2, - 0, 0, -1, 0 - ) + return proj } }
\ No newline at end of file diff --git a/public/gl/core/Shader.js b/public/gl/core/Shader.js index f5e0a88..0e7e528 100644 --- a/public/gl/core/Shader.js +++ b/public/gl/core/Shader.js @@ -88,7 +88,8 @@ export class Shader { } loadMat4(name, m) { - gl.uniformMatrix4fv(this.#uniforms[name], gl.FALSE, new Float32Array(m.get())) + // console.log(name, m.get()) + gl.uniformMatrix4fv(this.#uniforms[name], gl.FALSE, m.get()) } loadBool(name, bool) { |