webgl/public/gl/core/Camera.js

46 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2023-01-19 03:43:02 +00:00
import { Vec3 } from '../math/Vec3.js'
import { Mat4 } from '../math/Mat4.js'
export class Camera {
constructor() {
this.position = new Vec3()
this.rotation = new Vec3()
}
view() {
2023-01-19 18:39:40 +00:00
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;
2023-01-20 02:21:17 +00:00
d[3] = 0
2023-01-19 18:39:40 +00:00
d[4] = u.y;
d[5] = v.y;
d[6] = w.y;
2023-01-20 02:21:17 +00:00
d[7] = 0
2023-01-19 18:39:40 +00:00
d[8] = u.z;
d[9] = v.z;
d[10] = w.z;
2023-01-20 02:21:17 +00:00
d[11] = 0
2023-01-19 18:39:40 +00:00
d[12] = -u.dot(this.position)
d[13] = -v.dot(this.position)
d[14] = -w.dot(this.position)
d[15] = 1
return view
2023-01-19 03:43:02 +00:00
}
}